Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(593)

Side by Side Diff: content/browser/renderer_host/media/video_capture_manager.cc

Issue 2769543002: [Mojo Video Capture] Introduce abstraction VideoCaptureSystem (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/renderer_host/media/video_capture_manager.h" 5 #include "content/browser/renderer_host/media/video_capture_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 #endif 51 #endif
52 52
53 #if defined(OS_ANDROID) 53 #if defined(OS_ANDROID)
54 #include "content/browser/media/capture/screen_capture_device_android.h" 54 #include "content/browser/media/capture/screen_capture_device_android.h"
55 #endif 55 #endif
56 56
57 #endif // defined(ENABLE_SCREEN_CAPTURE) 57 #endif // defined(ENABLE_SCREEN_CAPTURE)
58 58
59 namespace { 59 namespace {
60 60
61 // Compares two VideoCaptureFormat by checking smallest frame_size area, then
62 // by _largest_ frame_rate. Used to order a VideoCaptureFormats vector so that
63 // the first entry for a given resolution has the largest frame rate, as needed
64 // by the ConsolidateCaptureFormats() method.
65 bool IsCaptureFormatSmaller(const media::VideoCaptureFormat& format1,
66 const media::VideoCaptureFormat& format2) {
67 DCHECK(format1.frame_size.GetCheckedArea().IsValid());
68 DCHECK(format2.frame_size.GetCheckedArea().IsValid());
69 if (format1.frame_size.GetCheckedArea().ValueOrDefault(0) ==
70 format2.frame_size.GetCheckedArea().ValueOrDefault(0)) {
71 return format1.frame_rate > format2.frame_rate;
72 }
73 return format1.frame_size.GetCheckedArea().ValueOrDefault(0) <
74 format2.frame_size.GetCheckedArea().ValueOrDefault(0);
75 }
76
77 bool IsCaptureFormatSizeEqual(const media::VideoCaptureFormat& format1,
78 const media::VideoCaptureFormat& format2) {
79 DCHECK(format1.frame_size.GetCheckedArea().IsValid());
80 DCHECK(format2.frame_size.GetCheckedArea().IsValid());
81 return format1.frame_size.GetCheckedArea().ValueOrDefault(0) ==
82 format2.frame_size.GetCheckedArea().ValueOrDefault(0);
83 }
84
85 // This function receives a list of capture formats, removes duplicated
86 // resolutions while keeping the highest frame rate for each, and forcing I420
87 // pixel format.
88 void ConsolidateCaptureFormats(media::VideoCaptureFormats* formats) {
89 if (formats->empty())
90 return;
91 std::sort(formats->begin(), formats->end(), IsCaptureFormatSmaller);
92 // Due to the ordering imposed, the largest frame_rate is kept while removing
93 // duplicated resolutions.
94 media::VideoCaptureFormats::iterator last =
95 std::unique(formats->begin(), formats->end(), IsCaptureFormatSizeEqual);
96 formats->erase(last, formats->end());
97 // Mark all formats as I420, since this is what the renderer side will get
98 // anyhow: the actual pixel format is decided at the device level.
99 // Don't do this for Y16 format as it is handled separatelly.
100 for (auto& format : *formats) {
101 if (format.pixel_format != media::PIXEL_FORMAT_Y16)
102 format.pixel_format = media::PIXEL_FORMAT_I420;
103 }
104 }
105
106 // Used for logging capture events. 61 // Used for logging capture events.
107 // Elements in this enum should not be deleted or rearranged; the only 62 // Elements in this enum should not be deleted or rearranged; the only
108 // permitted operation is to add new elements before NUM_VIDEO_CAPTURE_EVENT. 63 // permitted operation is to add new elements before NUM_VIDEO_CAPTURE_EVENT.
109 enum VideoCaptureEvent { 64 enum VideoCaptureEvent {
110 VIDEO_CAPTURE_START_CAPTURE = 0, 65 VIDEO_CAPTURE_START_CAPTURE = 0,
111 VIDEO_CAPTURE_STOP_CAPTURE_OK = 1, 66 VIDEO_CAPTURE_STOP_CAPTURE_OK = 1,
112 VIDEO_CAPTURE_STOP_CAPTURE_DUE_TO_ERROR = 2, 67 VIDEO_CAPTURE_STOP_CAPTURE_DUE_TO_ERROR = 2,
113 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE = 3, 68 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE = 3,
114 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DESKTOP_OR_TAB = 4, 69 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DESKTOP_OR_TAB = 4,
115 NUM_VIDEO_CAPTURE_EVENT 70 NUM_VIDEO_CAPTURE_EVENT
116 }; 71 };
117 72
118 void LogVideoCaptureEvent(VideoCaptureEvent event) { 73 void LogVideoCaptureEvent(VideoCaptureEvent event) {
119 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureManager.Event", event, 74 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureManager.Event", event,
120 NUM_VIDEO_CAPTURE_EVENT); 75 NUM_VIDEO_CAPTURE_EVENT);
121 } 76 }
122 77
123 const media::VideoCaptureSessionId kFakeSessionId = -1; 78 const media::VideoCaptureSessionId kFakeSessionId = -1;
124 79
125 } // namespace 80 } // namespace
126 81
127 namespace content { 82 namespace content {
128 83
129 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported
130 // video formats.
131 struct VideoCaptureManager::DeviceInfo {
132 DeviceInfo();
133 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor);
134 DeviceInfo(const DeviceInfo& other);
135 ~DeviceInfo();
136 DeviceInfo& operator=(const DeviceInfo& other);
137
138 media::VideoCaptureDeviceDescriptor descriptor;
139 media::VideoCaptureFormats supported_formats;
140 };
141
142 // Class used for queuing request for starting a device. 84 // Class used for queuing request for starting a device.
143 class VideoCaptureManager::CaptureDeviceStartRequest { 85 class VideoCaptureManager::CaptureDeviceStartRequest {
144 public: 86 public:
145 CaptureDeviceStartRequest(VideoCaptureController* controller, 87 CaptureDeviceStartRequest(VideoCaptureController* controller,
146 media::VideoCaptureSessionId session_id, 88 media::VideoCaptureSessionId session_id,
147 const media::VideoCaptureParams& params); 89 const media::VideoCaptureParams& params);
148 VideoCaptureController* controller() const { return controller_; } 90 VideoCaptureController* controller() const { return controller_; }
149 media::VideoCaptureSessionId session_id() const { return session_id_; } 91 media::VideoCaptureSessionId session_id() const { return session_id_; }
150 media::VideoCaptureParams params() const { return params_; } 92 media::VideoCaptureParams params() const { return params_; }
151 93
152 private: 94 private:
153 VideoCaptureController* const controller_; 95 VideoCaptureController* const controller_;
154 const media::VideoCaptureSessionId session_id_; 96 const media::VideoCaptureSessionId session_id_;
155 const media::VideoCaptureParams params_; 97 const media::VideoCaptureParams params_;
156 }; 98 };
157 99
158 VideoCaptureManager::DeviceInfo::DeviceInfo() = default;
159
160 VideoCaptureManager::DeviceInfo::DeviceInfo(
161 media::VideoCaptureDeviceDescriptor descriptor)
162 : descriptor(descriptor) {}
163
164 VideoCaptureManager::DeviceInfo::DeviceInfo(
165 const VideoCaptureManager::DeviceInfo& other) = default;
166
167 VideoCaptureManager::DeviceInfo::~DeviceInfo() = default;
168
169 VideoCaptureManager::DeviceInfo& VideoCaptureManager::DeviceInfo::operator=(
170 const VideoCaptureManager::DeviceInfo& other) = default;
171
172 VideoCaptureManager::CaptureDeviceStartRequest::CaptureDeviceStartRequest( 100 VideoCaptureManager::CaptureDeviceStartRequest::CaptureDeviceStartRequest(
173 VideoCaptureController* controller, 101 VideoCaptureController* controller,
174 media::VideoCaptureSessionId session_id, 102 media::VideoCaptureSessionId session_id,
175 const media::VideoCaptureParams& params) 103 const media::VideoCaptureParams& params)
176 : controller_(controller), session_id_(session_id), params_(params) {} 104 : controller_(controller), session_id_(session_id), params_(params) {}
177 105
178 VideoCaptureManager::VideoCaptureManager( 106 VideoCaptureManager::VideoCaptureManager(
179 std::unique_ptr<media::VideoCaptureDeviceFactory> factory, 107 std::unique_ptr<media::VideoCaptureSystem> video_capture_system,
180 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner) 108 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner)
181 : device_task_runner_(std::move(device_task_runner)), 109 : device_task_runner_(std::move(device_task_runner)),
182 new_capture_session_id_(1), 110 new_capture_session_id_(1),
183 video_capture_device_factory_(std::move(factory)) {} 111 video_capture_system_(std::move(video_capture_system)) {}
184 112
185 VideoCaptureManager::~VideoCaptureManager() { 113 VideoCaptureManager::~VideoCaptureManager() {
186 DCHECK(controllers_.empty()); 114 DCHECK(controllers_.empty());
187 DCHECK(device_start_request_queue_.empty()); 115 DCHECK(device_start_request_queue_.empty());
188 } 116 }
189 117
190 void VideoCaptureManager::AddVideoCaptureObserver( 118 void VideoCaptureManager::AddVideoCaptureObserver(
191 media::VideoCaptureObserver* observer) { 119 media::VideoCaptureObserver* observer) {
192 DCHECK(observer); 120 DCHECK(observer);
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); 121 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 23 matching lines...) Expand all
217 MediaStreamProviderListener* listener) { 145 MediaStreamProviderListener* listener) {
218 DCHECK_CURRENTLY_ON(BrowserThread::IO); 146 DCHECK_CURRENTLY_ON(BrowserThread::IO);
219 listeners_.RemoveObserver(listener); 147 listeners_.RemoveObserver(listener);
220 } 148 }
221 149
222 void VideoCaptureManager::EnumerateDevices( 150 void VideoCaptureManager::EnumerateDevices(
223 const EnumerationCallback& client_callback) { 151 const EnumerationCallback& client_callback) {
224 DCHECK_CURRENTLY_ON(BrowserThread::IO); 152 DCHECK_CURRENTLY_ON(BrowserThread::IO);
225 DVLOG(1) << "VideoCaptureManager::EnumerateDevices"; 153 DVLOG(1) << "VideoCaptureManager::EnumerateDevices";
226 154
227 // Bind a callback to ConsolidateDevicesInfoOnDeviceThread() with an argument 155 // OK to use base::Unretained(video_capture_system_) since we own the
228 // for another callback to OnDevicesInfoEnumerated() to be run in the current 156 // |video_capture_system_| and |this| is bound in
229 // loop, i.e. IO loop. Pass a timer for UMA histogram collection. 157 // |devices_enumerated_callback|.
230 base::Callback<void(std::unique_ptr<VideoCaptureDeviceDescriptors>)>
231 devices_enumerated_callback = base::Bind(
232 &VideoCaptureManager::ConsolidateDevicesInfoOnDeviceThread, this,
233 media::BindToCurrentLoop(base::Bind(
234 &VideoCaptureManager::OnDevicesInfoEnumerated, this,
235 base::Owned(new base::ElapsedTimer()), client_callback)),
236 devices_info_cache_);
237 // OK to use base::Unretained() since we own the VCDFactory and |this| is
238 // bound in |devices_enumerated_callback|.
239 device_task_runner_->PostTask( 158 device_task_runner_->PostTask(
240 FROM_HERE, 159 FROM_HERE,
241 base::Bind(&media::VideoCaptureDeviceFactory::EnumerateDeviceDescriptors, 160 base::Bind(&media::VideoCaptureSystem::GetDeviceInfosAsync,
242 base::Unretained(video_capture_device_factory_.get()), 161 base::Unretained(video_capture_system_.get()),
243 devices_enumerated_callback)); 162 // Pass a timer for UMA histogram collection.
244 } 163 media::BindToCurrentLoop(base::Bind(
245 164 &VideoCaptureManager::OnDeviceInfosReceived, this,
246 const media::VideoCaptureDeviceDescriptor* 165 base::Owned(new base::ElapsedTimer()), client_callback))));
247 VideoCaptureManager::LookupDeviceDescriptor(const std::string& id) {
248 const DeviceInfo* info = GetDeviceInfoById(id);
249 return info ? (&info->descriptor) : nullptr;
250 } 166 }
251 167
252 int VideoCaptureManager::Open(const MediaStreamDevice& device) { 168 int VideoCaptureManager::Open(const MediaStreamDevice& device) {
253 DCHECK_CURRENTLY_ON(BrowserThread::IO); 169 DCHECK_CURRENTLY_ON(BrowserThread::IO);
254 170
255 // Generate a new id for the session being opened. 171 // Generate a new id for the session being opened.
256 const media::VideoCaptureSessionId capture_session_id = 172 const media::VideoCaptureSessionId capture_session_id =
257 new_capture_session_id_++; 173 new_capture_session_id_++;
258 174
259 DCHECK(sessions_.find(capture_session_id) == sessions_.end()); 175 DCHECK(sessions_.find(capture_session_id) == sessions_.end());
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 std::find_if(++request_iter, device_start_request_queue_.end(), 246 std::find_if(++request_iter, device_start_request_queue_.end(),
331 [controller](const CaptureDeviceStartRequest& request) { 247 [controller](const CaptureDeviceStartRequest& request) {
332 return request.controller() == controller; 248 return request.controller() == controller;
333 }); 249 });
334 if (request_iter != device_start_request_queue_.end()) { 250 if (request_iter != device_start_request_queue_.end()) {
335 device_start_request_queue_.erase(request_iter); 251 device_start_request_queue_.erase(request_iter);
336 return; 252 return;
337 } 253 }
338 } 254 }
339 255
340 const DeviceInfo* device_info = GetDeviceInfoById(controller->device_id()); 256 const media::VideoCaptureDeviceInfo* device_info =
257 GetDeviceInfoById(controller->device_id());
341 if (device_info != nullptr) { 258 if (device_info != nullptr) {
342 for (auto& observer : capture_observers_) 259 for (auto& observer : capture_observers_)
343 observer.OnVideoCaptureStopped(device_info->descriptor.facing); 260 observer.OnVideoCaptureStopped(device_info->descriptor.facing);
344 } 261 }
345 262
346 DVLOG(3) << "DoStopDevice. Send stop request for device = " 263 DVLOG(3) << "DoStopDevice. Send stop request for device = "
347 << controller->device_id() 264 << controller->device_id()
348 << " serial_id = " << controller->serial_id() << "."; 265 << " serial_id = " << controller->serial_id() << ".";
349 controller->OnLog(base::StringPrintf("Stopping device: id: %s", 266 controller->OnLog(base::StringPrintf("Stopping device: id: %s",
350 controller->device_id().c_str())); 267 controller->device_id().c_str()));
(...skipping 10 matching lines...) Expand all
361 DCHECK_CURRENTLY_ON(BrowserThread::IO); 278 DCHECK_CURRENTLY_ON(BrowserThread::IO);
362 DeviceStartQueue::iterator request = device_start_request_queue_.begin(); 279 DeviceStartQueue::iterator request = device_start_request_queue_.begin();
363 if (request == device_start_request_queue_.end()) 280 if (request == device_start_request_queue_.end())
364 return; 281 return;
365 282
366 VideoCaptureController* const controller = request->controller(); 283 VideoCaptureController* const controller = request->controller();
367 284
368 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " 285 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = "
369 << controller->device_id() 286 << controller->device_id()
370 << " start id = " << controller->serial_id(); 287 << " start id = " << controller->serial_id();
288 // The unit test VideoCaptureManagerTest.OpenNotExisting requires us to fail
289 // synchronously if the stream_type is MEDIA_DEVICE_VIDEO_CAPTURE and no
290 // DeviceInfo matching the requested id is present (which is the case when
291 // requesting a device with a bogus id). Note, that since other types of
292 // failure during startup of the device are allowed to be reported
293 // asynchronously, this requirement is questionable.
294 // TODO(chfremer): Check if any production code actually depends on this
295 // requirement. If not, relax the requirement in the test and remove the below
296 // if block.
297 if (controller->stream_type() == MEDIA_DEVICE_VIDEO_CAPTURE) {
298 const media::VideoCaptureDeviceInfo* device_info =
299 GetDeviceInfoById(controller->device_id());
300 if (!device_info) {
301 OnDeviceStartFailed(controller);
302 return;
303 }
304 }
305
306 media::VideoCaptureDeviceInfo* device_info =
307 GetDeviceInfoById(controller->device_id());
308 DCHECK(device_info);
309 for (auto& observer : capture_observers_)
310 observer.OnVideoCaptureStarted(device_info->descriptor.facing);
371 311
372 // The method CreateAndStartDeviceAsync() is going to run asynchronously. 312 // The method CreateAndStartDeviceAsync() is going to run asynchronously.
373 // Since we may be removing the controller while it is executing, we need to 313 // Since we may be removing the controller while it is executing, we need to
374 // pass it shared ownership to itself so that it stays alive while executing. 314 // pass it shared ownership to itself so that it stays alive while executing.
375 // And since the execution may make callbacks into |this|, we also need 315 // And since the execution may make callbacks into |this|, we also need
376 // to pass it shared ownership to |this|. 316 // to pass it shared ownership to |this|.
377 // TODO(chfremer): Check if request->params() can actually be different from 317 // TODO(chfremer): Check if request->params() can actually be different from
378 // controller->parameters, and simplify if this is not the case. 318 // controller->parameters, and simplify if this is not the case.
379 controller->CreateAndStartDeviceAsync( 319 controller->CreateAndStartDeviceAsync(
380 request->params(), static_cast<BuildableDeviceCallbacks*>(this), 320 request->params(), static_cast<BuildableDeviceCallbacks*>(this),
381 base::Bind([](scoped_refptr<VideoCaptureManager>, 321 base::Bind([](scoped_refptr<VideoCaptureManager>,
382 scoped_refptr<VideoCaptureController>) {}, 322 scoped_refptr<VideoCaptureController>) {},
383 scoped_refptr<VideoCaptureManager>(this), 323 scoped_refptr<VideoCaptureManager>(this),
384 GetControllerSharedRef(controller))); 324 GetControllerSharedRef(controller)));
385 } 325 }
386 326
387 void VideoCaptureManager::WillStartDevice(media::VideoFacingMode facing_mode) { 327 void VideoCaptureManager::OnDeviceStarted(VideoCaptureController* controller) {
388 for (auto& observer : capture_observers_)
389 observer.OnVideoCaptureStarted(facing_mode);
390 }
391
392 void VideoCaptureManager::DidStartDevice(VideoCaptureController* controller) {
393 DVLOG(3) << __func__; 328 DVLOG(3) << __func__;
394 DCHECK_CURRENTLY_ON(BrowserThread::IO); 329 DCHECK_CURRENTLY_ON(BrowserThread::IO);
395 DCHECK(!device_start_request_queue_.empty()); 330 DCHECK(!device_start_request_queue_.empty());
396 DCHECK_EQ(controller, device_start_request_queue_.begin()->controller()); 331 DCHECK_EQ(controller, device_start_request_queue_.begin()->controller());
397 DCHECK(controller); 332 DCHECK(controller);
333
398 if (controller->stream_type() == MEDIA_DESKTOP_VIDEO_CAPTURE) { 334 if (controller->stream_type() == MEDIA_DESKTOP_VIDEO_CAPTURE) {
399 const media::VideoCaptureSessionId session_id = 335 const media::VideoCaptureSessionId session_id =
400 device_start_request_queue_.front().session_id(); 336 device_start_request_queue_.front().session_id();
401 DCHECK(session_id != kFakeSessionId); 337 DCHECK(session_id != kFakeSessionId);
402 MaybePostDesktopCaptureWindowId(session_id); 338 MaybePostDesktopCaptureWindowId(session_id);
403 } 339 }
404 340
405 auto it = photo_request_queue_.begin(); 341 auto it = photo_request_queue_.begin();
406 while (it != photo_request_queue_.end()) { 342 while (it != photo_request_queue_.end()) {
407 auto request = it++; 343 auto request = it++;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 return GetDeviceSupportedFormats(it->second.id, supported_formats); 514 return GetDeviceSupportedFormats(it->second.id, supported_formats);
579 } 515 }
580 516
581 bool VideoCaptureManager::GetDeviceSupportedFormats( 517 bool VideoCaptureManager::GetDeviceSupportedFormats(
582 const std::string& device_id, 518 const std::string& device_id,
583 media::VideoCaptureFormats* supported_formats) { 519 media::VideoCaptureFormats* supported_formats) {
584 DCHECK_CURRENTLY_ON(BrowserThread::IO); 520 DCHECK_CURRENTLY_ON(BrowserThread::IO);
585 DCHECK(supported_formats->empty()); 521 DCHECK(supported_formats->empty());
586 522
587 // Return all available formats of the device, regardless its started state. 523 // Return all available formats of the device, regardless its started state.
588 DeviceInfo* existing_device = GetDeviceInfoById(device_id); 524 media::VideoCaptureDeviceInfo* existing_device = GetDeviceInfoById(device_id);
589 if (existing_device) 525 if (existing_device)
590 *supported_formats = existing_device->supported_formats; 526 *supported_formats = existing_device->supported_formats;
591 return true; 527 return true;
592 } 528 }
593 529
594 bool VideoCaptureManager::GetDeviceFormatsInUse( 530 bool VideoCaptureManager::GetDeviceFormatsInUse(
595 media::VideoCaptureSessionId capture_session_id, 531 media::VideoCaptureSessionId capture_session_id,
596 media::VideoCaptureFormats* formats_in_use) { 532 media::VideoCaptureFormats* formats_in_use) {
597 DCHECK_CURRENTLY_ON(BrowserThread::IO); 533 DCHECK_CURRENTLY_ON(BrowserThread::IO);
598 DCHECK(formats_in_use->empty()); 534 DCHECK(formats_in_use->empty());
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 } 672 }
737 673
738 void VideoCaptureManager::OnClosed( 674 void VideoCaptureManager::OnClosed(
739 MediaStreamType stream_type, 675 MediaStreamType stream_type,
740 media::VideoCaptureSessionId capture_session_id) { 676 media::VideoCaptureSessionId capture_session_id) {
741 DCHECK_CURRENTLY_ON(BrowserThread::IO); 677 DCHECK_CURRENTLY_ON(BrowserThread::IO);
742 for (auto& listener : listeners_) 678 for (auto& listener : listeners_)
743 listener.Closed(stream_type, capture_session_id); 679 listener.Closed(stream_type, capture_session_id);
744 } 680 }
745 681
746 void VideoCaptureManager::OnDevicesInfoEnumerated( 682 void VideoCaptureManager::OnDeviceInfosReceived(
747 base::ElapsedTimer* timer, 683 base::ElapsedTimer* timer,
748 const EnumerationCallback& client_callback, 684 const EnumerationCallback& client_callback,
749 const VideoCaptureManager::DeviceInfos& new_devices_info_cache) { 685 const std::vector<media::VideoCaptureDeviceInfo>& device_infos) {
750 DCHECK_CURRENTLY_ON(BrowserThread::IO); 686 DCHECK_CURRENTLY_ON(BrowserThread::IO);
751 UMA_HISTOGRAM_TIMES( 687 UMA_HISTOGRAM_TIMES(
752 "Media.VideoCaptureManager.GetAvailableDevicesInfoOnDeviceThreadTime", 688 "Media.VideoCaptureManager.GetAvailableDevicesInfoOnDeviceThreadTime",
753 timer->Elapsed()); 689 timer->Elapsed());
754 devices_info_cache_ = new_devices_info_cache; 690 devices_info_cache_ = device_infos;
755 691
756 // Walk the |devices_info_cache_| and produce a 692 // Walk the |devices_info_cache_| and produce a
757 // media::VideoCaptureDeviceDescriptors for return purposes. 693 // media::VideoCaptureDeviceDescriptors for |client_callback|.
758 media::VideoCaptureDeviceDescriptors devices; 694 media::VideoCaptureDeviceDescriptors devices;
759 std::vector<std::tuple<media::VideoCaptureDeviceDescriptor, 695 std::vector<std::tuple<media::VideoCaptureDeviceDescriptor,
760 media::VideoCaptureFormats>> 696 media::VideoCaptureFormats>>
761 descriptors_and_formats; 697 descriptors_and_formats;
762 for (const auto& it : devices_info_cache_) { 698 for (const auto& it : devices_info_cache_) {
763 devices.emplace_back(it.descriptor); 699 devices.emplace_back(it.descriptor);
764 descriptors_and_formats.emplace_back(it.descriptor, it.supported_formats); 700 descriptors_and_formats.emplace_back(it.descriptor, it.supported_formats);
765 MediaInternals::GetInstance()->UpdateVideoCaptureDeviceCapabilities( 701 MediaInternals::GetInstance()->UpdateVideoCaptureDeviceCapabilities(
766 descriptors_and_formats); 702 descriptors_and_formats);
767 } 703 }
768 704
769 client_callback.Run(devices); 705 client_callback.Run(devices);
770 } 706 }
771 707
772 void VideoCaptureManager::ConsolidateDevicesInfoOnDeviceThread(
773 base::Callback<void(const VideoCaptureManager::DeviceInfos&)>
774 on_devices_enumerated_callback,
775 const VideoCaptureManager::DeviceInfos& old_device_info_cache,
776 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors_snapshot) {
777 DCHECK(device_task_runner_->BelongsToCurrentThread());
778 // Construct |new_devices_info_cache| with the cached devices that are still
779 // present in the system, and remove their names from |names_snapshot|, so we
780 // keep there the truly new devices.
781 VideoCaptureManager::DeviceInfos new_devices_info_cache;
782 for (const auto& device_info : old_device_info_cache) {
783 for (VideoCaptureDeviceDescriptors::iterator it =
784 descriptors_snapshot->begin();
785 it != descriptors_snapshot->end(); ++it) {
786 if (device_info.descriptor.device_id == it->device_id) {
787 new_devices_info_cache.push_back(device_info);
788 descriptors_snapshot->erase(it);
789 break;
790 }
791 }
792 }
793
794 // Get the device info for the new devices in |names_snapshot|.
795 for (const auto& it : *descriptors_snapshot) {
796 DeviceInfo device_info(it);
797 video_capture_device_factory_->GetSupportedFormats(
798 it, &device_info.supported_formats);
799 ConsolidateCaptureFormats(&device_info.supported_formats);
800 new_devices_info_cache.push_back(device_info);
801 }
802
803 on_devices_enumerated_callback.Run(new_devices_info_cache);
804 }
805
806 void VideoCaptureManager::DestroyControllerIfNoClients( 708 void VideoCaptureManager::DestroyControllerIfNoClients(
807 VideoCaptureController* controller) { 709 VideoCaptureController* controller) {
808 DCHECK_CURRENTLY_ON(BrowserThread::IO); 710 DCHECK_CURRENTLY_ON(BrowserThread::IO);
809 // Removal of the last client stops the device. 711 // Removal of the last client stops the device.
810 if (!controller->HasActiveClient() && !controller->HasPausedClient()) { 712 if (!controller->HasActiveClient() && !controller->HasPausedClient()) {
811 DVLOG(1) << "VideoCaptureManager stopping device (type = " 713 DVLOG(1) << "VideoCaptureManager stopping device (type = "
812 << controller->stream_type() 714 << controller->stream_type()
813 << ", id = " << controller->device_id() << ")"; 715 << ", id = " << controller->device_id() << ")";
814 716
815 // The VideoCaptureController is removed from |controllers_| immediately. 717 // The VideoCaptureController is removed from |controllers_| immediately.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 VideoCaptureController* controller) const { 766 VideoCaptureController* controller) const {
865 DCHECK_CURRENTLY_ON(BrowserThread::IO); 767 DCHECK_CURRENTLY_ON(BrowserThread::IO);
866 768
867 for (const auto& entry : controllers_) { 769 for (const auto& entry : controllers_) {
868 if (entry.get() == controller) 770 if (entry.get() == controller)
869 return entry; 771 return entry;
870 } 772 }
871 return nullptr; 773 return nullptr;
872 } 774 }
873 775
874 VideoCaptureManager::DeviceInfo* VideoCaptureManager::GetDeviceInfoById( 776 media::VideoCaptureDeviceInfo* VideoCaptureManager::GetDeviceInfoById(
875 const std::string& id) { 777 const std::string& id) {
876 for (auto& it : devices_info_cache_) { 778 for (auto& it : devices_info_cache_) {
877 if (it.descriptor.device_id == id) 779 if (it.descriptor.device_id == id)
878 return &it; 780 return &it;
879 } 781 }
880 return nullptr; 782 return nullptr;
881 } 783 }
882 784
883 VideoCaptureController* VideoCaptureManager::GetOrCreateController( 785 VideoCaptureController* VideoCaptureManager::GetOrCreateController(
884 media::VideoCaptureSessionId capture_session_id, 786 media::VideoCaptureSessionId capture_session_id,
(...skipping 10 matching lines...) Expand all
895 VideoCaptureController* const existing_device = 797 VideoCaptureController* const existing_device =
896 LookupControllerByMediaTypeAndDeviceId(device_info.type, device_info.id); 798 LookupControllerByMediaTypeAndDeviceId(device_info.type, device_info.id);
897 if (existing_device) { 799 if (existing_device) {
898 DCHECK_EQ(device_info.type, existing_device->stream_type()); 800 DCHECK_EQ(device_info.type, existing_device->stream_type());
899 return existing_device; 801 return existing_device;
900 } 802 }
901 803
902 VideoCaptureController* new_controller = new VideoCaptureController( 804 VideoCaptureController* new_controller = new VideoCaptureController(
903 device_info.id, device_info.type, params, 805 device_info.id, device_info.type, params,
904 base::MakeUnique<InProcessBuildableVideoCaptureDevice>( 806 base::MakeUnique<InProcessBuildableVideoCaptureDevice>(
905 device_task_runner_, video_capture_device_factory_.get())); 807 device_task_runner_, video_capture_system_.get()));
906 controllers_.emplace_back(new_controller); 808 controllers_.emplace_back(new_controller);
907 return new_controller; 809 return new_controller;
908 } 810 }
909 811
910 base::Optional<CameraCalibration> VideoCaptureManager::GetCameraCalibration( 812 base::Optional<CameraCalibration> VideoCaptureManager::GetCameraCalibration(
911 const std::string& device_id) { 813 const std::string& device_id) {
912 VideoCaptureManager::DeviceInfo* info = GetDeviceInfoById(device_id); 814 media::VideoCaptureDeviceInfo* info = GetDeviceInfoById(device_id);
913 if (!info) 815 if (!info)
914 return base::Optional<CameraCalibration>(); 816 return base::Optional<CameraCalibration>();
915 return info->descriptor.camera_calibration; 817 return info->descriptor.camera_calibration;
916 } 818 }
917 819
918 #if defined(OS_ANDROID) 820 #if defined(OS_ANDROID)
919 void VideoCaptureManager::OnApplicationStateChange( 821 void VideoCaptureManager::OnApplicationStateChange(
920 base::android::ApplicationState state) { 822 base::android::ApplicationState state) {
921 DCHECK_CURRENTLY_ON(BrowserThread::IO); 823 DCHECK_CURRENTLY_ON(BrowserThread::IO);
922 824
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 // Session ID is only valid for Screen capture. So we can fake it to 869 // Session ID is only valid for Screen capture. So we can fake it to
968 // resume video capture devices here. 870 // resume video capture devices here.
969 QueueStartDevice(kFakeSessionId, controller.get(), 871 QueueStartDevice(kFakeSessionId, controller.get(),
970 controller->parameters()); 872 controller->parameters());
971 } 873 }
972 } 874 }
973 } 875 }
974 #endif // defined(OS_ANDROID) 876 #endif // defined(OS_ANDROID)
975 877
976 } // namespace content 878 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698