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