| 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 23 matching lines...) Expand all Loading... |
| 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 media::VideoCaptureDeviceDescriptors descriptors; |
| 43 device_factory_->GetDeviceDescriptors(&descriptors); | 43 device_factory_->GetDeviceDescriptors(&descriptors); |
| 44 callback.Run(descriptors); | 44 callback.Run(std::move(descriptors)); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats( | 47 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats( |
| 48 const media::VideoCaptureDeviceDescriptor& device_descriptor, | 48 const std::string& device_id, |
| 49 const GetSupportedFormatsCallback& callback) { | 49 const GetSupportedFormatsCallback& callback) { |
| 50 media::VideoCaptureDeviceDescriptor descriptor; |
| 51 media::VideoCaptureFormats media_formats; |
| 52 if (LookupDescriptorFromId(device_id, &descriptor)) |
| 53 device_factory_->GetSupportedFormats(descriptor, &media_formats); |
| 50 std::vector<VideoCaptureFormat> result; | 54 std::vector<VideoCaptureFormat> result; |
| 51 std::vector<media::VideoCaptureFormat> media_formats; | |
| 52 device_factory_->GetSupportedFormats(device_descriptor, &media_formats); | |
| 53 for (const auto& media_format : media_formats) { | 55 for (const auto& media_format : media_formats) { |
| 54 // The Video Capture Service requires devices to deliver frames either in | 56 // The Video Capture Service requires devices to deliver frames either in |
| 55 // I420 or MJPEG formats. | 57 // I420 or MJPEG formats. |
| 56 // TODO(chfremer): Add support for Y16 format. See crbug.com/624436. | 58 // TODO(chfremer): Add support for Y16 format. See crbug.com/624436. |
| 57 if (media_format.pixel_format != media::PIXEL_FORMAT_I420 && | 59 if (media_format.pixel_format != media::PIXEL_FORMAT_I420 && |
| 58 media_format.pixel_format != media::PIXEL_FORMAT_MJPEG) { | 60 media_format.pixel_format != media::PIXEL_FORMAT_MJPEG) { |
| 59 continue; | 61 continue; |
| 60 } | 62 } |
| 61 VideoCaptureFormat format; | 63 VideoCaptureFormat format; |
| 62 format.frame_size = media_format.frame_size; | 64 format.frame_size = media_format.frame_size; |
| 63 format.frame_rate = media_format.frame_rate; | 65 format.frame_rate = media_format.frame_rate; |
| 64 if (base::ContainsValue(result, format)) | 66 if (base::ContainsValue(result, format)) |
| 65 continue; // Result already contains this format | 67 continue; // Result already contains this format |
| 66 result.push_back(format); | 68 result.push_back(format); |
| 67 } | 69 } |
| 68 callback.Run(std::move(result)); | 70 callback.Run(std::move(result)); |
| 69 } | 71 } |
| 70 | 72 |
| 71 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy( | 73 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy( |
| 72 const media::VideoCaptureDeviceDescriptor& device_descriptor, | 74 const std::string& device_id, |
| 73 mojom::VideoCaptureDeviceProxyRequest proxy_request, | 75 mojom::VideoCaptureDeviceProxyRequest proxy_request, |
| 74 const CreateDeviceProxyCallback& callback) { | 76 const CreateDeviceProxyCallback& callback) { |
| 75 if (active_devices_.find(device_descriptor) != active_devices_.end()) { | 77 auto active_device_iter = active_devices_by_id_.find(device_id); |
| 78 if (active_device_iter != active_devices_by_id_.end()) { |
| 76 // The requested device is already in use. | 79 // The requested device is already in use. |
| 77 // Revoke the access and close the device, then bind to the new request. | 80 // Revoke the access and close the device, then bind to the new request. |
| 78 ActiveDeviceEntry& device_entry = active_devices_[device_descriptor]; | 81 ActiveDeviceEntry& device_entry = active_device_iter->second; |
| 79 device_entry.binding->Unbind(); | 82 device_entry.binding->Unbind(); |
| 80 device_entry.device_proxy->Stop(); | 83 device_entry.device_proxy->Stop(); |
| 81 device_entry.binding->Bind(std::move(proxy_request)); | 84 device_entry.binding->Bind(std::move(proxy_request)); |
| 82 device_entry.binding->set_connection_error_handler(base::Bind( | 85 device_entry.binding->set_connection_error_handler(base::Bind( |
| 83 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 86 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
| 84 base::Unretained(this), device_descriptor)); | 87 base::Unretained(this), device_id)); |
| 85 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 88 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
| 86 return; | 89 return; |
| 87 } | 90 } |
| 88 | 91 |
| 92 // Create device |
| 93 media::VideoCaptureDeviceDescriptor descriptor; |
| 94 if (LookupDescriptorFromId(device_id, &descriptor) == false) { |
| 95 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
| 96 return; |
| 97 } |
| 89 std::unique_ptr<media::VideoCaptureDevice> media_device = | 98 std::unique_ptr<media::VideoCaptureDevice> media_device = |
| 90 device_factory_->CreateDevice(device_descriptor); | 99 device_factory_->CreateDevice(descriptor); |
| 91 if (media_device == nullptr) { | 100 if (media_device == nullptr) { |
| 92 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | 101 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
| 93 return; | 102 return; |
| 94 } | 103 } |
| 95 | 104 |
| 96 // Add entry to |active_devices| to keep track of it | 105 // Add entry to active_devices to keep track of it |
| 97 ActiveDeviceEntry device_entry; | 106 ActiveDeviceEntry device_entry; |
| 98 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>( | 107 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>( |
| 99 std::move(media_device), jpeg_decoder_factory_callback_); | 108 std::move(media_device), jpeg_decoder_factory_callback_); |
| 100 device_entry.binding = | 109 device_entry.binding = |
| 101 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>( | 110 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>( |
| 102 device_entry.device_proxy.get(), std::move(proxy_request)); | 111 device_entry.device_proxy.get(), std::move(proxy_request)); |
| 103 device_entry.binding->set_connection_error_handler(base::Bind( | 112 device_entry.binding->set_connection_error_handler(base::Bind( |
| 104 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 113 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
| 105 base::Unretained(this), device_descriptor)); | 114 base::Unretained(this), device_id)); |
| 106 active_devices_[device_descriptor] = std::move(device_entry); | 115 active_devices_by_id_[device_id] = std::move(device_entry); |
| 107 | 116 |
| 108 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 117 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
| 109 } | 118 } |
| 110 | 119 |
| 111 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( | 120 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( |
| 112 const media::VideoCaptureDeviceDescriptor& descriptor) { | 121 const std::string& device_id) { |
| 113 active_devices_[descriptor].device_proxy->Stop(); | 122 active_devices_by_id_[device_id].device_proxy->Stop(); |
| 114 active_devices_.erase(descriptor); | 123 active_devices_by_id_.erase(device_id); |
| 124 } |
| 125 |
| 126 bool DeviceFactoryMediaToMojoAdapter::LookupDescriptorFromId( |
| 127 const std::string& device_id, |
| 128 media::VideoCaptureDeviceDescriptor* descriptor) { |
| 129 media::VideoCaptureDeviceDescriptors descriptors; |
| 130 device_factory_->GetDeviceDescriptors(&descriptors); |
| 131 auto descriptor_iter = std::find_if( |
| 132 descriptors.begin(), descriptors.end(), |
| 133 [&device_id](const media::VideoCaptureDeviceDescriptor& descriptor) { |
| 134 return descriptor.device_id == device_id; |
| 135 }); |
| 136 if (descriptor_iter == descriptors.end()) |
| 137 return false; |
| 138 *descriptor = *descriptor_iter; |
| 139 return true; |
| 115 } | 140 } |
| 116 | 141 |
| 117 } // namespace video_capture | 142 } // namespace video_capture |
| OLD | NEW |