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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "media/capture/video/fake_video_capture_device.h" | 13 #include "media/capture/video/fake_video_capture_device.h" |
14 #include "media/capture/video/video_capture_device_info.h" | 14 #include "media/capture/video/video_capture_device_info.h" |
15 #include "mojo/public/cpp/bindings/strong_binding.h" | 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
16 #include "services/video_capture/device_media_to_mojo_adapter.h" | 16 #include "services/video_capture/device_media_to_mojo_adapter.h" |
17 | 17 |
18 namespace { | |
19 | |
20 // Translates a set of device infos reported by a VideoCaptureSystem to a set | |
21 // of device infos that the video capture service exposes to its client. | |
22 // The Video Capture Service instances of VideoCaptureDeviceClient to | |
23 // convert the formats provided by the VideoCaptureDevice instances. Here, we | |
24 // translate the set of supported formats as reported by the |device_factory_| | |
25 // to what will be output by the VideoCaptureDeviceClient we connect to it. | |
26 // TODO(chfremer): A cleaner design would be to have this translation | |
27 // happen in VideoCaptureDeviceClient, and talk only to VideoCaptureDeviceClient | |
28 // instead of VideoCaptureSystem. | |
29 static void TranslateDeviceInfos( | |
30 const video_capture::mojom::DeviceFactory::GetDeviceInfosCallback& callback, | |
31 const std::vector<media::VideoCaptureDeviceInfo>& device_infos) { | |
32 std::vector<media::VideoCaptureDeviceInfo> translated_device_infos; | |
33 for (const auto& device_info : device_infos) { | |
34 media::VideoCaptureDeviceInfo translated_device_info; | |
35 translated_device_info.descriptor = device_info.descriptor; | |
36 for (const auto& format : device_info.supported_formats) { | |
37 media::VideoCaptureFormat translated_format; | |
38 switch (format.pixel_format) { | |
39 case media::PIXEL_FORMAT_I420: | |
40 case media::PIXEL_FORMAT_MJPEG: | |
41 translated_format.pixel_format = media::PIXEL_FORMAT_I420; | |
42 break; | |
43 case media::PIXEL_FORMAT_Y16: | |
44 translated_format.pixel_format = media::PIXEL_FORMAT_Y16; | |
45 default: | |
46 // Any other format cannot be consumed by VideoCaptureDeviceClient. | |
47 continue; | |
48 } | |
49 translated_format.frame_size = format.frame_size; | |
50 translated_format.frame_rate = format.frame_rate; | |
51 translated_format.pixel_storage = media::PIXEL_STORAGE_CPU; | |
52 if (base::ContainsValue(translated_device_info.supported_formats, | |
53 translated_format)) | |
54 continue; | |
55 translated_device_info.supported_formats.push_back(translated_format); | |
56 } | |
57 if (translated_device_info.supported_formats.empty()) | |
58 continue; | |
59 translated_device_infos.push_back(translated_device_info); | |
60 } | |
61 callback.Run(translated_device_infos); | |
62 } | |
63 | |
64 } // anonymous namespace | |
65 | |
66 namespace video_capture { | 18 namespace video_capture { |
67 | 19 |
68 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = | 20 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = |
69 default; | 21 default; |
70 | 22 |
71 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = | 23 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = |
72 default; | 24 default; |
73 | 25 |
74 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( | 26 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( |
75 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; | 27 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
76 | 28 |
77 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& | 29 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& |
78 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( | 30 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( |
79 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; | 31 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
80 | 32 |
81 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( | 33 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( |
82 std::unique_ptr<media::VideoCaptureSystem> capture_system, | 34 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory, |
83 const media::VideoCaptureJpegDecoderFactoryCB& | 35 const media::VideoCaptureJpegDecoderFactoryCB& |
84 jpeg_decoder_factory_callback) | 36 jpeg_decoder_factory_callback) |
85 : capture_system_(std::move(capture_system)), | 37 : device_factory_(std::move(device_factory)), |
86 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} | 38 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} |
87 | 39 |
88 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; | 40 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; |
89 | 41 |
90 void DeviceFactoryMediaToMojoAdapter::GetDeviceInfos( | 42 void DeviceFactoryMediaToMojoAdapter::GetDeviceInfos( |
91 const GetDeviceInfosCallback& callback) { | 43 const GetDeviceInfosCallback& callback) { |
92 capture_system_->GetDeviceInfosAsync( | 44 NOTIMPLEMENTED(); |
93 base::Bind(&TranslateDeviceInfos, callback)); | |
94 } | 45 } |
95 | 46 |
96 void DeviceFactoryMediaToMojoAdapter::CreateDevice( | 47 void DeviceFactoryMediaToMojoAdapter::CreateDevice( |
97 const std::string& device_id, | 48 const std::string& device_id, |
98 mojom::DeviceRequest device_request, | 49 mojom::DeviceRequest device_request, |
99 const CreateDeviceCallback& callback) { | 50 const CreateDeviceCallback& callback) { |
100 auto active_device_iter = active_devices_by_id_.find(device_id); | 51 auto active_device_iter = active_devices_by_id_.find(device_id); |
101 if (active_device_iter != active_devices_by_id_.end()) { | 52 if (active_device_iter != active_devices_by_id_.end()) { |
102 // The requested device is already in use. | 53 // The requested device is already in use. |
103 // Revoke the access and close the device, then bind to the new request. | 54 // Revoke the access and close the device, then bind to the new request. |
104 ActiveDeviceEntry& device_entry = active_device_iter->second; | 55 ActiveDeviceEntry& device_entry = active_device_iter->second; |
105 device_entry.binding->Unbind(); | 56 device_entry.binding->Unbind(); |
106 device_entry.device->Stop(); | 57 device_entry.device->Stop(); |
107 device_entry.binding->Bind(std::move(device_request)); | 58 device_entry.binding->Bind(std::move(device_request)); |
108 device_entry.binding->set_connection_error_handler(base::Bind( | 59 device_entry.binding->set_connection_error_handler(base::Bind( |
109 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 60 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
110 base::Unretained(this), device_id)); | 61 base::Unretained(this), device_id)); |
111 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 62 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
112 return; | 63 return; |
113 } | 64 } |
114 | 65 |
| 66 // Create device |
| 67 media::VideoCaptureDeviceDescriptor descriptor; |
| 68 if (LookupDescriptorFromId(device_id, &descriptor) == false) { |
| 69 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
| 70 return; |
| 71 } |
115 std::unique_ptr<media::VideoCaptureDevice> media_device = | 72 std::unique_ptr<media::VideoCaptureDevice> media_device = |
116 capture_system_->CreateDevice(device_id); | 73 device_factory_->CreateDevice(descriptor); |
117 if (media_device == nullptr) { | 74 if (media_device == nullptr) { |
118 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | 75 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
119 return; | 76 return; |
120 } | 77 } |
121 | 78 |
122 // Add entry to active_devices to keep track of it | 79 // Add entry to active_devices to keep track of it |
123 ActiveDeviceEntry device_entry; | 80 ActiveDeviceEntry device_entry; |
124 device_entry.device = base::MakeUnique<DeviceMediaToMojoAdapter>( | 81 device_entry.device = base::MakeUnique<DeviceMediaToMojoAdapter>( |
125 std::move(media_device), jpeg_decoder_factory_callback_); | 82 std::move(media_device), jpeg_decoder_factory_callback_); |
126 device_entry.binding = base::MakeUnique<mojo::Binding<mojom::Device>>( | 83 device_entry.binding = base::MakeUnique<mojo::Binding<mojom::Device>>( |
127 device_entry.device.get(), std::move(device_request)); | 84 device_entry.device.get(), std::move(device_request)); |
128 device_entry.binding->set_connection_error_handler(base::Bind( | 85 device_entry.binding->set_connection_error_handler(base::Bind( |
129 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 86 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
130 base::Unretained(this), device_id)); | 87 base::Unretained(this), device_id)); |
131 active_devices_by_id_[device_id] = std::move(device_entry); | 88 active_devices_by_id_[device_id] = std::move(device_entry); |
132 | 89 |
133 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 90 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
134 } | 91 } |
135 | 92 |
136 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( | 93 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( |
137 const std::string& device_id) { | 94 const std::string& device_id) { |
138 active_devices_by_id_[device_id].device->Stop(); | 95 active_devices_by_id_[device_id].device->Stop(); |
139 active_devices_by_id_.erase(device_id); | 96 active_devices_by_id_.erase(device_id); |
140 } | 97 } |
141 | 98 |
| 99 bool DeviceFactoryMediaToMojoAdapter::LookupDescriptorFromId( |
| 100 const std::string& device_id, |
| 101 media::VideoCaptureDeviceDescriptor* descriptor) { |
| 102 media::VideoCaptureDeviceDescriptors descriptors; |
| 103 device_factory_->GetDeviceDescriptors(&descriptors); |
| 104 auto descriptor_iter = std::find_if( |
| 105 descriptors.begin(), descriptors.end(), |
| 106 [&device_id](const media::VideoCaptureDeviceDescriptor& descriptor) { |
| 107 return descriptor.device_id == device_id; |
| 108 }); |
| 109 if (descriptor_iter == descriptors.end()) |
| 110 return false; |
| 111 *descriptor = *descriptor_iter; |
| 112 return true; |
| 113 } |
| 114 |
142 } // namespace video_capture | 115 } // namespace video_capture |
OLD | NEW |