OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/video_capture/device_factory_media_to_mojo_adapter.h" | 5 #include "services/video_capture/device_factory_media_to_mojo_adapter.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
32 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory, | 32 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory, |
33 const media::VideoCaptureJpegDecoderFactoryCB& | 33 const media::VideoCaptureJpegDecoderFactoryCB& |
34 jpeg_decoder_factory_callback) | 34 jpeg_decoder_factory_callback) |
35 : device_factory_(std::move(device_factory)), | 35 : device_factory_(std::move(device_factory)), |
36 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} | 36 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} |
37 | 37 |
38 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; | 38 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; |
39 | 39 |
40 void DeviceFactoryMediaToMojoAdapter::EnumerateDeviceDescriptors( | 40 void DeviceFactoryMediaToMojoAdapter::EnumerateDeviceDescriptors( |
41 const EnumerateDeviceDescriptorsCallback& callback) { | 41 const EnumerateDeviceDescriptorsCallback& callback) { |
42 media::VideoCaptureDeviceDescriptors descriptors; | 42 RefreshDescriptors(); |
43 device_factory_->GetDeviceDescriptors(&descriptors); | 43 std::vector<media::VideoCaptureDeviceDescriptor> descriptors; |
44 callback.Run(descriptors); | 44 for (const auto& entry : descriptors_by_id_) { |
45 descriptors.push_back(entry.second); | |
46 } | |
47 callback.Run(std::move(descriptors)); | |
45 } | 48 } |
46 | 49 |
47 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats( | 50 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats( |
48 const media::VideoCaptureDeviceDescriptor& device_descriptor, | 51 const std::string& device_id, |
49 const GetSupportedFormatsCallback& callback) { | 52 const GetSupportedFormatsCallback& callback) { |
53 RefreshDescriptors(); | |
50 std::vector<VideoCaptureFormat> result; | 54 std::vector<VideoCaptureFormat> result; |
51 std::vector<media::VideoCaptureFormat> media_formats; | 55 media::VideoCaptureFormats media_formats; |
52 device_factory_->GetSupportedFormats(device_descriptor, &media_formats); | 56 auto descriptor_iter = descriptors_by_id_.find(device_id); |
57 if (descriptor_iter != descriptors_by_id_.end()) { | |
58 device_factory_->GetSupportedFormats(descriptor_iter->second, | |
59 &media_formats); | |
60 } | |
53 for (const auto& media_format : media_formats) { | 61 for (const auto& media_format : media_formats) { |
54 // The Video Capture Service requires devices to deliver frames either in | 62 // The Video Capture Service requires devices to deliver frames either in |
55 // I420 or MJPEG formats. | 63 // I420 or MJPEG formats. |
56 // TODO(chfremer): Add support for Y16 format. See crbug.com/624436. | 64 // TODO(chfremer): Add support for Y16 format. See crbug.com/624436. |
57 if (media_format.pixel_format != media::PIXEL_FORMAT_I420 && | 65 if (media_format.pixel_format != media::PIXEL_FORMAT_I420 && |
58 media_format.pixel_format != media::PIXEL_FORMAT_MJPEG) { | 66 media_format.pixel_format != media::PIXEL_FORMAT_MJPEG) { |
59 continue; | 67 continue; |
60 } | 68 } |
61 VideoCaptureFormat format; | 69 VideoCaptureFormat format; |
62 format.frame_size = media_format.frame_size; | 70 format.frame_size = media_format.frame_size; |
63 format.frame_rate = media_format.frame_rate; | 71 format.frame_rate = media_format.frame_rate; |
64 if (base::ContainsValue(result, format)) | 72 if (base::ContainsValue(result, format)) |
65 continue; // Result already contains this format | 73 continue; // Result already contains this format |
66 result.push_back(format); | 74 result.push_back(format); |
67 } | 75 } |
68 callback.Run(std::move(result)); | 76 callback.Run(std::move(result)); |
69 } | 77 } |
70 | 78 |
71 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy( | 79 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy( |
72 const media::VideoCaptureDeviceDescriptor& device_descriptor, | 80 const std::string& device_id, |
73 mojom::VideoCaptureDeviceProxyRequest proxy_request, | 81 mojom::VideoCaptureDeviceProxyRequest proxy_request, |
74 const CreateDeviceProxyCallback& callback) { | 82 const CreateDeviceProxyCallback& callback) { |
75 if (active_devices_.find(device_descriptor) != active_devices_.end()) { | 83 RefreshDescriptors(); |
84 auto active_device_iter = active_devices_by_id_.find(device_id); | |
85 if (active_device_iter != active_devices_by_id_.end()) { | |
76 // The requested device is already in use. | 86 // The requested device is already in use. |
77 // Revoke the access and close the device, then bind to the new request. | 87 // Revoke the access and close the device, then bind to the new request. |
78 ActiveDeviceEntry& device_entry = active_devices_[device_descriptor]; | 88 ActiveDeviceEntry& device_entry = active_device_iter->second; |
79 device_entry.binding->Unbind(); | 89 device_entry.binding->Unbind(); |
80 device_entry.device_proxy->Stop(); | 90 device_entry.device_proxy->Stop(); |
81 device_entry.binding->Bind(std::move(proxy_request)); | 91 device_entry.binding->Bind(std::move(proxy_request)); |
82 device_entry.binding->set_connection_error_handler(base::Bind( | 92 device_entry.binding->set_connection_error_handler(base::Bind( |
83 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 93 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
84 base::Unretained(this), device_descriptor)); | 94 base::Unretained(this), device_id)); |
85 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 95 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
86 return; | 96 return; |
87 } | 97 } |
88 | 98 |
99 // Create device | |
100 auto descriptor_iter = descriptors_by_id_.find(device_id); | |
101 if (descriptor_iter == descriptors_by_id_.end()) { | |
102 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | |
103 return; | |
104 } | |
105 | |
89 std::unique_ptr<media::VideoCaptureDevice> media_device = | 106 std::unique_ptr<media::VideoCaptureDevice> media_device = |
90 device_factory_->CreateDevice(device_descriptor); | 107 device_factory_->CreateDevice(descriptor_iter->second); |
91 if (media_device == nullptr) { | 108 if (media_device == nullptr) { |
92 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | 109 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
93 return; | 110 return; |
94 } | 111 } |
95 | 112 |
96 // Add entry to |active_devices| to keep track of it | 113 // Add entry to active_devices to keep track of it |
97 ActiveDeviceEntry device_entry; | 114 ActiveDeviceEntry device_entry; |
98 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>( | 115 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>( |
99 std::move(media_device), jpeg_decoder_factory_callback_); | 116 std::move(media_device), jpeg_decoder_factory_callback_); |
100 device_entry.binding = | 117 device_entry.binding = |
101 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>( | 118 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>( |
102 device_entry.device_proxy.get(), std::move(proxy_request)); | 119 device_entry.device_proxy.get(), std::move(proxy_request)); |
103 device_entry.binding->set_connection_error_handler(base::Bind( | 120 device_entry.binding->set_connection_error_handler(base::Bind( |
104 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 121 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
105 base::Unretained(this), device_descriptor)); | 122 base::Unretained(this), device_id)); |
106 active_devices_[device_descriptor] = std::move(device_entry); | 123 active_devices_by_id_[device_id] = std::move(device_entry); |
107 | 124 |
108 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 125 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
109 } | 126 } |
110 | 127 |
111 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( | 128 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( |
112 const media::VideoCaptureDeviceDescriptor& descriptor) { | 129 const std::string& device_id) { |
113 active_devices_[descriptor].device_proxy->Stop(); | 130 active_devices_by_id_[device_id].device_proxy->Stop(); |
114 active_devices_.erase(descriptor); | 131 active_devices_by_id_.erase(device_id); |
132 } | |
133 | |
134 void DeviceFactoryMediaToMojoAdapter::RefreshDescriptors() { | |
135 std::vector<media::VideoCaptureDeviceDescriptor> descriptors; | |
136 device_factory_->GetDeviceDescriptors(&descriptors); | |
137 descriptors_by_id_.clear(); | |
yzshen1
2016/11/12 00:27:29
Why do we need to store descriptors_by_id_ as a cl
chfremer
2016/11/14 17:38:05
Excellent observation. Thank you.
Fixed.
| |
138 for (const auto& entry : descriptors) { | |
139 descriptors_by_id_[entry.device_id] = entry; | |
140 } | |
115 } | 141 } |
116 | 142 |
117 } // namespace video_capture | 143 } // namespace video_capture |
OLD | NEW |