OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/video_capture/device_factory_media_to_mojo_adapter.h" |
| 6 |
| 7 #include <sstream> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "media/capture/video/fake_video_capture_device.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 #include "services/video_capture/device_mock_to_media_adapter.h" |
| 14 #include "services/video_capture/video_capture_device_proxy_impl.h" |
| 15 |
| 16 namespace video_capture { |
| 17 |
| 18 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = |
| 19 default; |
| 20 |
| 21 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = |
| 22 default; |
| 23 |
| 24 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( |
| 25 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
| 26 |
| 27 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& |
| 28 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( |
| 29 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
| 30 |
| 31 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( |
| 32 std::unique_ptr<media::VideoCaptureDeviceFactory> device_factory, |
| 33 const media::VideoCaptureJpegDecoderFactoryCB& |
| 34 jpeg_decoder_factory_callback) |
| 35 : device_factory_(std::move(device_factory)), |
| 36 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} |
| 37 |
| 38 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; |
| 39 |
| 40 void DeviceFactoryMediaToMojoAdapter::EnumerateDeviceDescriptors( |
| 41 const EnumerateDeviceDescriptorsCallback& callback) { |
| 42 media::VideoCaptureDeviceDescriptors descriptors; |
| 43 device_factory_->GetDeviceDescriptors(&descriptors); |
| 44 callback.Run(descriptors); |
| 45 } |
| 46 |
| 47 void DeviceFactoryMediaToMojoAdapter::GetSupportedFormats( |
| 48 const media::VideoCaptureDeviceDescriptor& device_descriptor, |
| 49 const GetSupportedFormatsCallback& callback) { |
| 50 std::vector<VideoCaptureFormat> result; |
| 51 NOTIMPLEMENTED(); |
| 52 callback.Run(std::move(result)); |
| 53 } |
| 54 |
| 55 void DeviceFactoryMediaToMojoAdapter::CreateDeviceProxy( |
| 56 const media::VideoCaptureDeviceDescriptor& device_descriptor, |
| 57 mojom::VideoCaptureDeviceProxyRequest proxy_request, |
| 58 const CreateDeviceProxyCallback& callback) { |
| 59 if (active_devices_.find(device_descriptor) != active_devices_.end()) { |
| 60 // The requested device is already in use. |
| 61 // Revoke the access and close the device, then bind to the new request. |
| 62 ActiveDeviceEntry& device_entry = active_devices_[device_descriptor]; |
| 63 device_entry.binding->Unbind(); |
| 64 device_entry.device_proxy->Stop(); |
| 65 device_entry.binding->Bind(std::move(proxy_request)); |
| 66 device_entry.binding->set_connection_error_handler(base::Bind( |
| 67 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
| 68 base::Unretained(this), device_descriptor)); |
| 69 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
| 70 return; |
| 71 } |
| 72 |
| 73 std::unique_ptr<media::VideoCaptureDevice> media_device = |
| 74 device_factory_->CreateDevice(device_descriptor); |
| 75 if (media_device == nullptr) { |
| 76 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
| 77 return; |
| 78 } |
| 79 |
| 80 // Add entry to |active_devices| to keep track of it |
| 81 ActiveDeviceEntry device_entry; |
| 82 device_entry.device_proxy = base::MakeUnique<VideoCaptureDeviceProxyImpl>( |
| 83 std::move(media_device), jpeg_decoder_factory_callback_); |
| 84 device_entry.binding = |
| 85 base::MakeUnique<mojo::Binding<mojom::VideoCaptureDeviceProxy>>( |
| 86 device_entry.device_proxy.get(), std::move(proxy_request)); |
| 87 device_entry.binding->set_connection_error_handler(base::Bind( |
| 88 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
| 89 base::Unretained(this), device_descriptor)); |
| 90 active_devices_[device_descriptor] = std::move(device_entry); |
| 91 |
| 92 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
| 93 } |
| 94 |
| 95 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( |
| 96 const media::VideoCaptureDeviceDescriptor& descriptor) { |
| 97 active_devices_[descriptor].device_proxy->Stop(); |
| 98 active_devices_.erase(descriptor); |
| 99 } |
| 100 |
| 101 } // namespace video_capture |
OLD | NEW |