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" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 continue; | 54 continue; |
55 translated_device_info.supported_formats.push_back(translated_format); | 55 translated_device_info.supported_formats.push_back(translated_format); |
56 } | 56 } |
57 if (translated_device_info.supported_formats.empty()) | 57 if (translated_device_info.supported_formats.empty()) |
58 continue; | 58 continue; |
59 translated_device_infos.push_back(translated_device_info); | 59 translated_device_infos.push_back(translated_device_info); |
60 } | 60 } |
61 callback.Run(translated_device_infos); | 61 callback.Run(translated_device_infos); |
62 } | 62 } |
63 | 63 |
| 64 static void DiscardDeviceInfosAndCallContinuation( |
| 65 base::Closure continuation, |
| 66 const std::vector<media::VideoCaptureDeviceInfo>&) { |
| 67 continuation.Run(); |
| 68 } |
| 69 |
64 } // anonymous namespace | 70 } // anonymous namespace |
65 | 71 |
66 namespace video_capture { | 72 namespace video_capture { |
67 | 73 |
68 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = | 74 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry() = |
69 default; | 75 default; |
70 | 76 |
71 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = | 77 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::~ActiveDeviceEntry() = |
72 default; | 78 default; |
73 | 79 |
74 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( | 80 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::ActiveDeviceEntry( |
75 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; | 81 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
76 | 82 |
77 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& | 83 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry& |
78 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( | 84 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry::operator=( |
79 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; | 85 DeviceFactoryMediaToMojoAdapter::ActiveDeviceEntry&& other) = default; |
80 | 86 |
81 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( | 87 DeviceFactoryMediaToMojoAdapter::DeviceFactoryMediaToMojoAdapter( |
| 88 std::unique_ptr<service_manager::ServiceContextRef> service_ref, |
82 std::unique_ptr<media::VideoCaptureSystem> capture_system, | 89 std::unique_ptr<media::VideoCaptureSystem> capture_system, |
83 const media::VideoCaptureJpegDecoderFactoryCB& | 90 const media::VideoCaptureJpegDecoderFactoryCB& |
84 jpeg_decoder_factory_callback) | 91 jpeg_decoder_factory_callback) |
85 : capture_system_(std::move(capture_system)), | 92 : service_ref_(std::move(service_ref)), |
86 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback) {} | 93 capture_system_(std::move(capture_system)), |
| 94 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback), |
| 95 has_called_get_device_infos_(false), |
| 96 weak_factory_(this) {} |
87 | 97 |
88 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; | 98 DeviceFactoryMediaToMojoAdapter::~DeviceFactoryMediaToMojoAdapter() = default; |
89 | 99 |
90 void DeviceFactoryMediaToMojoAdapter::GetDeviceInfos( | 100 void DeviceFactoryMediaToMojoAdapter::GetDeviceInfos( |
91 const GetDeviceInfosCallback& callback) { | 101 const GetDeviceInfosCallback& callback) { |
92 capture_system_->GetDeviceInfosAsync( | 102 capture_system_->GetDeviceInfosAsync( |
93 base::Bind(&TranslateDeviceInfos, callback)); | 103 base::Bind(&TranslateDeviceInfos, callback)); |
| 104 has_called_get_device_infos_ = true; |
94 } | 105 } |
95 | 106 |
96 void DeviceFactoryMediaToMojoAdapter::CreateDevice( | 107 void DeviceFactoryMediaToMojoAdapter::CreateDevice( |
97 const std::string& device_id, | 108 const std::string& device_id, |
98 mojom::DeviceRequest device_request, | 109 mojom::DeviceRequest device_request, |
99 const CreateDeviceCallback& callback) { | 110 const CreateDeviceCallback& callback) { |
100 auto active_device_iter = active_devices_by_id_.find(device_id); | 111 auto active_device_iter = active_devices_by_id_.find(device_id); |
101 if (active_device_iter != active_devices_by_id_.end()) { | 112 if (active_device_iter != active_devices_by_id_.end()) { |
102 // The requested device is already in use. | 113 // The requested device is already in use. |
103 // Revoke the access and close the device, then bind to the new request. | 114 // Revoke the access and close the device, then bind to the new request. |
104 ActiveDeviceEntry& device_entry = active_device_iter->second; | 115 ActiveDeviceEntry& device_entry = active_device_iter->second; |
105 device_entry.binding->Unbind(); | 116 device_entry.binding->Unbind(); |
106 device_entry.device->Stop(); | 117 device_entry.device->Stop(); |
107 device_entry.binding->Bind(std::move(device_request)); | 118 device_entry.binding->Bind(std::move(device_request)); |
108 device_entry.binding->set_connection_error_handler(base::Bind( | 119 device_entry.binding->set_connection_error_handler(base::Bind( |
109 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 120 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
110 base::Unretained(this), device_id)); | 121 base::Unretained(this), device_id)); |
111 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 122 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
112 return; | 123 return; |
113 } | 124 } |
114 | 125 |
| 126 const auto create_and_add_new_device_cb = |
| 127 base::Bind(&DeviceFactoryMediaToMojoAdapter::CreateAndAddNewDevice, |
| 128 weak_factory_.GetWeakPtr(), device_id, |
| 129 base::Passed(&device_request), callback); |
| 130 |
| 131 if (has_called_get_device_infos_) { |
| 132 create_and_add_new_device_cb.Run(); |
| 133 return; |
| 134 } |
| 135 |
| 136 capture_system_->GetDeviceInfosAsync(base::Bind( |
| 137 &DiscardDeviceInfosAndCallContinuation, create_and_add_new_device_cb)); |
| 138 } |
| 139 |
| 140 void DeviceFactoryMediaToMojoAdapter::CreateAndAddNewDevice( |
| 141 const std::string& device_id, |
| 142 mojom::DeviceRequest device_request, |
| 143 const CreateDeviceCallback& callback) { |
115 std::unique_ptr<media::VideoCaptureDevice> media_device = | 144 std::unique_ptr<media::VideoCaptureDevice> media_device = |
116 capture_system_->CreateDevice(device_id); | 145 capture_system_->CreateDevice(device_id); |
117 if (media_device == nullptr) { | 146 if (media_device == nullptr) { |
118 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | 147 callback.Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); |
119 return; | 148 return; |
120 } | 149 } |
121 | 150 |
122 // Add entry to active_devices to keep track of it | 151 // Add entry to active_devices to keep track of it |
123 ActiveDeviceEntry device_entry; | 152 ActiveDeviceEntry device_entry; |
124 device_entry.device = base::MakeUnique<DeviceMediaToMojoAdapter>( | 153 device_entry.device = base::MakeUnique<DeviceMediaToMojoAdapter>( |
125 std::move(media_device), jpeg_decoder_factory_callback_); | 154 service_ref_->Clone(), std::move(media_device), |
| 155 jpeg_decoder_factory_callback_); |
126 device_entry.binding = base::MakeUnique<mojo::Binding<mojom::Device>>( | 156 device_entry.binding = base::MakeUnique<mojo::Binding<mojom::Device>>( |
127 device_entry.device.get(), std::move(device_request)); | 157 device_entry.device.get(), std::move(device_request)); |
128 device_entry.binding->set_connection_error_handler(base::Bind( | 158 device_entry.binding->set_connection_error_handler(base::Bind( |
129 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, | 159 &DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose, |
130 base::Unretained(this), device_id)); | 160 base::Unretained(this), device_id)); |
131 active_devices_by_id_[device_id] = std::move(device_entry); | 161 active_devices_by_id_[device_id] = std::move(device_entry); |
132 | 162 |
133 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); | 163 callback.Run(mojom::DeviceAccessResultCode::SUCCESS); |
134 } | 164 } |
135 | 165 |
136 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( | 166 void DeviceFactoryMediaToMojoAdapter::OnClientConnectionErrorOrClose( |
137 const std::string& device_id) { | 167 const std::string& device_id) { |
138 active_devices_by_id_[device_id].device->Stop(); | 168 active_devices_by_id_[device_id].device->Stop(); |
139 active_devices_by_id_.erase(device_id); | 169 active_devices_by_id_.erase(device_id); |
140 } | 170 } |
141 | 171 |
142 } // namespace video_capture | 172 } // namespace video_capture |
OLD | NEW |