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 |
18 namespace video_capture { | 66 namespace video_capture { |
19 | 67 |
20 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = | 68 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = |
21 default; | 69 default; |
22 | 70 |
23 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = | 71 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = |
24 default; | 72 default; |
25 | 73 |
26 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( | 74 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( |
27 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; | 75 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
28 | 76 |
29 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& | 77 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& |
30 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( | 78 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( |
31 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; | 79 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
32 | 80 |
33 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( | 81 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( |
34 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory, | 82 std::unique_ptr<media::VideoCaptureSystem> capture_system, |
35 const media::VideoCaptureJpegDecoderFactoryCB& | 83 const media::VideoCaptureJpegDecoderFactoryCB& |
36 jpeg_decoder_factory_callback) | 84 jpeg_decoder_factory_callback) |
37 : device_factory_(std::move(device_factory)), | 85 : capture_system_(std::move(capture_system)), |
38 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} | 86 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} |
39 | 87 |
40 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; | 88 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; |
41 | 89 |
42 void DeviceFactoryMediaToMojoAdapter::GetDeviceInfos( | 90 void DeviceFactoryMediaToMojoAdapter::GetDeviceInfos( |
43 const GetDeviceInfosCallback& callback) { | 91 const GetDeviceInfosCallback& callback) { |
44 NOTIMPLEMENTED(); | 92 capture_system_->GetDeviceInfosAsync( |
| 93 base::Bind(&TranslateDeviceInfos, callback)); |
45 } | 94 } |
46 | 95 |
47 void DeviceFactoryMediaToMojoAdapter::CreateDevice( | 96 void DeviceFactoryMediaToMojoAdapter::CreateDevice( |
48 const std::string& device_id, | 97 const std::string& device_id, |
49 mojom::DeviceRequest device_request, | 98 mojom::DeviceRequest device_request, |
50 const CreateDeviceCallback& callback) { | 99 const CreateDeviceCallback& callback) { |
51 auto active_device_iter = active_devices_by_id_.find(device_id); | 100 auto active_device_iter = active_devices_by_id_.find(device_id); |
52 if (active_device_iter != active_devices_by_id_.end()) { | 101 if (active_device_iter != active_devices_by_id_.end()) { |
53 // The requested device is already in use. | 102 // The requested device is already in use. |
54 // Revoke the access and close the device, then bind to the new request. | 103 // Revoke the access and close the device, then bind to the new request. |
55 ActiveDeviceEntry& device_entry = active_device_iter->second; | 104 ActiveDeviceEntry& device_entry = active_device_iter->second; |
56 device_entry.binding->Unbind(); | 105 device_entry.binding->Unbind(); |
57 device_entry.device->Stop(); | 106 device_entry.device->Stop(); |
58 device_entry.binding->Bind(std::move(device_request)); | 107 device_entry.binding->Bind(std::move(device_request)); |
59 device_entry.binding->set_connection_error_handler(base::Bind( | 108 device_entry.binding->set_connection_error_handler(base::Bind( |
60 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 109 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
61 base::Unretained(this), device_id)); | 110 base::Unretained(this), device_id)); |
62 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 111 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
63 return; | 112 return; |
64 } | 113 } |
65 | 114 |
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 } | |
72 std::unique_ptr<media::VideoCaptureDevice> media_device = | 115 std::unique_ptr<media::VideoCaptureDevice> media_device = |
73 device_factory_->CreateDevice(descriptor); | 116 capture_system_->CreateDevice(device_id); |
74 if (media_device == nullptr) { | 117 if (media_device == nullptr) { |
75 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | 118 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
76 return; | 119 return; |
77 } | 120 } |
78 | 121 |
79 // Add entry to active_devices to keep track of it | 122 // Add entry to active_devices to keep track of it |
80 ActiveDeviceEntry device_entry; | 123 ActiveDeviceEntry device_entry; |
81 device_entry.device = base::MakeUnique<DeviceMediaToMojoAdapter>( | 124 device_entry.device = base::MakeUnique<DeviceMediaToMojoAdapter>( |
82 std::move(media_device), jpeg_decoder_factory_callback_); | 125 std::move(media_device), jpeg_decoder_factory_callback_); |
83 device_entry.binding = base::MakeUnique<mojo::Binding<mojom::Device>>( | 126 device_entry.binding = base::MakeUnique<mojo::Binding<mojom::Device>>( |
84 device_entry.device.get(), std::move(device_request)); | 127 device_entry.device.get(), std::move(device_request)); |
85 device_entry.binding->set_connection_error_handler(base::Bind( | 128 device_entry.binding->set_connection_error_handler(base::Bind( |
86 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 129 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
87 base::Unretained(this), device_id)); | 130 base::Unretained(this), device_id)); |
88 active_devices_by_id_[device_id] = std::move(device_entry); | 131 active_devices_by_id_[device_id] = std::move(device_entry); |
89 | 132 |
90 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 133 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
91 } | 134 } |
92 | 135 |
93 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( | 136 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( |
94 const std::string& device_id) { | 137 const std::string& device_id) { |
95 active_devices_by_id_[device_id].device->Stop(); | 138 active_devices_by_id_[device_id].device->Stop(); |
96 active_devices_by_id_.erase(device_id); | 139 active_devices_by_id_.erase(device_id); |
97 } | 140 } |
98 | 141 |
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 | |
115 } // namespace video_capture | 142 } // namespace video_capture |
OLD | NEW |