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

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

Issue 17846002: Refactor the VideoCaptureDevice::Name struct. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 5 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 | Annotate | Revision Log
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 <set> 7 #include <set>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 DCHECK(IsOnDeviceThread()); 135 DCHECK(IsOnDeviceThread());
136 136
137 media::VideoCaptureDevice::Names device_names; 137 media::VideoCaptureDevice::Names device_names;
138 GetAvailableDevices(stream_type, &device_names); 138 GetAvailableDevices(stream_type, &device_names);
139 139
140 scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray()); 140 scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray());
141 for (media::VideoCaptureDevice::Names::iterator it = 141 for (media::VideoCaptureDevice::Names::iterator it =
142 device_names.begin(); it != device_names.end(); ++it) { 142 device_names.begin(); it != device_names.end(); ++it) {
143 bool opened = DeviceOpened(*it); 143 bool opened = DeviceOpened(*it);
144 devices->push_back(StreamDeviceInfo( 144 devices->push_back(StreamDeviceInfo(
145 stream_type, it->device_name, it->unique_id, opened)); 145 stream_type, it->name(), it->id(), opened));
146 } 146 }
147 147
148 PostOnDevicesEnumerated(stream_type, devices.Pass()); 148 PostOnDevicesEnumerated(stream_type, devices.Pass());
149 } 149 }
150 150
151 void VideoCaptureManager::OnOpen(int capture_session_id, 151 void VideoCaptureManager::OnOpen(int capture_session_id,
152 const StreamDeviceInfo& device) { 152 const StreamDeviceInfo& device) {
153 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.OnOpenTime"); 153 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.OnOpenTime");
154 DCHECK(IsOnDeviceThread()); 154 DCHECK(IsOnDeviceThread());
155 DCHECK(devices_.find(capture_session_id) == devices_.end()); 155 DCHECK(devices_.find(capture_session_id) == devices_.end());
156 DVLOG(1) << "VideoCaptureManager::OnOpen, id " << capture_session_id; 156 DVLOG(1) << "VideoCaptureManager::OnOpen, id " << capture_session_id;
157 157
158 // Check if another session has already opened this device. If so, just 158 // Check if another session has already opened this device. If so, just
159 // use that opened device. 159 // use that opened device.
160 media::VideoCaptureDevice* video_capture_device = GetOpenedDevice(device); 160 media::VideoCaptureDevice* video_capture_device = GetOpenedDevice(device);
161 if (video_capture_device) { 161 if (video_capture_device) {
162 DeviceEntry& new_entry = devices_[capture_session_id]; 162 DeviceEntry& new_entry = devices_[capture_session_id];
163 new_entry.stream_type = device.device.type; 163 new_entry.stream_type = device.device.type;
164 new_entry.capture_device = video_capture_device; 164 new_entry.capture_device = video_capture_device;
165 PostOnOpened(device.device.type, capture_session_id); 165 PostOnOpened(device.device.type, capture_session_id);
166 return; 166 return;
167 } 167 }
168 168
169 // Open the device. 169 // Open the device.
170 media::VideoCaptureDevice::Name vc_device_name;
171 vc_device_name.device_name = device.device.name;
172 vc_device_name.unique_id = device.device.id;
173 170
174 if (use_fake_device_) { 171 switch (device.device.type) {
175 video_capture_device = 172 case MEDIA_DEVICE_VIDEO_CAPTURE: {
176 media::FakeVideoCaptureDevice::Create(vc_device_name); 173 // We look up the device id from the renderer in our local enumeration
177 } else { 174 // since the renderer does not have all the information that might be
178 switch (device.device.type) { 175 // held in the browser-side VideoCaptureDevice::Name structure.
179 case MEDIA_DEVICE_VIDEO_CAPTURE: { 176 media::VideoCaptureDevice::Name* found =
180 video_capture_device = 177 video_capture_devices_.FindById(device.device.id);
181 media::VideoCaptureDevice::Create(vc_device_name); 178 if (found) {
182 break; 179 video_capture_device = use_fake_device_ ?
180 media::FakeVideoCaptureDevice::Create(*found) :
181 media::VideoCaptureDevice::Create(*found);
183 } 182 }
184 case MEDIA_TAB_VIDEO_CAPTURE: { 183 break;
185 video_capture_device = WebContentsVideoCaptureDevice::Create( 184 }
186 vc_device_name.unique_id); 185 case MEDIA_TAB_VIDEO_CAPTURE: {
187 break; 186 video_capture_device = WebContentsVideoCaptureDevice::Create(
188 } 187 device.device.id);
189 case MEDIA_SCREEN_VIDEO_CAPTURE: { 188 break;
189 }
190 case MEDIA_SCREEN_VIDEO_CAPTURE: {
190 #if defined(ENABLE_SCREEN_CAPTURE) 191 #if defined(ENABLE_SCREEN_CAPTURE)
191 scoped_refptr<base::SequencedWorkerPool> blocking_pool = 192 scoped_refptr<base::SequencedWorkerPool> blocking_pool =
192 BrowserThread::GetBlockingPool(); 193 BrowserThread::GetBlockingPool();
193 video_capture_device = new ScreenCaptureDevice( 194 video_capture_device = new ScreenCaptureDevice(
194 blocking_pool->GetSequencedTaskRunner( 195 blocking_pool->GetSequencedTaskRunner(
195 blocking_pool->GetSequenceToken())); 196 blocking_pool->GetSequenceToken()));
196 #endif // defined(ENABLE_SCREEN_CAPTURE) 197 #endif // defined(ENABLE_SCREEN_CAPTURE)
197 break; 198 break;
198 } 199 }
199 default: { 200 default: {
200 NOTIMPLEMENTED(); 201 NOTIMPLEMENTED();
201 break; 202 break;
202 }
203 } 203 }
204 } 204 }
205
205 if (!video_capture_device) { 206 if (!video_capture_device) {
206 PostOnError(capture_session_id, kDeviceNotAvailable); 207 PostOnError(capture_session_id, kDeviceNotAvailable);
207 return; 208 return;
208 } 209 }
209 210
210 DeviceEntry& new_entry = devices_[capture_session_id]; 211 DeviceEntry& new_entry = devices_[capture_session_id];
211 new_entry.stream_type = device.device.type; 212 new_entry.stream_type = device.device.type;
212 new_entry.capture_device = video_capture_device; 213 new_entry.capture_device = video_capture_device;
213 PostOnOpened(device.device.type, capture_session_id); 214 PostOnOpened(device.device.type, capture_session_id);
214 } 215 }
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 return device_loop_->BelongsToCurrentThread(); 402 return device_loop_->BelongsToCurrentThread();
402 } 403 }
403 404
404 void VideoCaptureManager::GetAvailableDevices( 405 void VideoCaptureManager::GetAvailableDevices(
405 MediaStreamType stream_type, 406 MediaStreamType stream_type,
406 media::VideoCaptureDevice::Names* device_names) { 407 media::VideoCaptureDevice::Names* device_names) {
407 DCHECK(IsOnDeviceThread()); 408 DCHECK(IsOnDeviceThread());
408 409
409 switch (stream_type) { 410 switch (stream_type) {
410 case MEDIA_DEVICE_VIDEO_CAPTURE: 411 case MEDIA_DEVICE_VIDEO_CAPTURE:
412 // Cache the latest enumeration of video capture devices.
413 // We'll refer to this list again in OnOpen to avoid having to
414 // enumerate the devices again.
415 video_capture_devices_.clear();
411 if (!use_fake_device_) { 416 if (!use_fake_device_) {
412 media::VideoCaptureDevice::GetDeviceNames(device_names); 417 media::VideoCaptureDevice::GetDeviceNames(&video_capture_devices_);
413 } else { 418 } else {
414 media::FakeVideoCaptureDevice::GetDeviceNames(device_names); 419 media::FakeVideoCaptureDevice::GetDeviceNames(&video_capture_devices_);
415 } 420 }
421 *device_names = video_capture_devices_;
416 break; 422 break;
417 423
418 case MEDIA_SCREEN_VIDEO_CAPTURE: 424 case MEDIA_SCREEN_VIDEO_CAPTURE:
419 device_names->clear(); 425 device_names->clear();
420 break; 426 break;
421 427
422 default: 428 default:
423 NOTREACHED(); 429 NOTREACHED();
424 break; 430 break;
425 } 431 }
426 } 432 }
427 433
428 bool VideoCaptureManager::DeviceOpened( 434 bool VideoCaptureManager::DeviceOpened(
429 const media::VideoCaptureDevice::Name& device_name) { 435 const media::VideoCaptureDevice::Name& device_name) {
430 DCHECK(IsOnDeviceThread()); 436 DCHECK(IsOnDeviceThread());
431 437
432 for (VideoCaptureDevices::iterator it = devices_.begin(); 438 for (VideoCaptureDevices::iterator it = devices_.begin();
433 it != devices_.end(); ++it) { 439 it != devices_.end(); ++it) {
434 if (device_name.unique_id == 440 if (device_name.id() == it->second.capture_device->device_name().id()) {
435 it->second.capture_device->device_name().unique_id) {
436 // We've found the device! 441 // We've found the device!
437 return true; 442 return true;
438 } 443 }
439 } 444 }
440 return false; 445 return false;
441 } 446 }
442 447
443 media::VideoCaptureDevice* VideoCaptureManager::GetOpenedDevice( 448 media::VideoCaptureDevice* VideoCaptureManager::GetOpenedDevice(
444 const StreamDeviceInfo& device_info) { 449 const StreamDeviceInfo& device_info) {
445 DCHECK(IsOnDeviceThread()); 450 DCHECK(IsOnDeviceThread());
446 451
447 for (VideoCaptureDevices::iterator it = devices_.begin(); 452 for (VideoCaptureDevices::iterator it = devices_.begin();
448 it != devices_.end(); it++) { 453 it != devices_.end(); it++) {
449 if (device_info.device.id == 454 if (device_info.device.id ==
450 it->second.capture_device->device_name().unique_id) { 455 it->second.capture_device->device_name().id()) {
451 return it->second.capture_device; 456 return it->second.capture_device;
452 } 457 }
453 } 458 }
454 return NULL; 459 return NULL;
455 } 460 }
456 461
457 bool VideoCaptureManager::DeviceInUse( 462 bool VideoCaptureManager::DeviceInUse(
458 const media::VideoCaptureDevice* video_capture_device) { 463 const media::VideoCaptureDevice* video_capture_device) {
459 DCHECK(IsOnDeviceThread()); 464 DCHECK(IsOnDeviceThread());
460 465
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 // Solution for not using MediaStreamManager. 553 // Solution for not using MediaStreamManager.
549 // This session id won't be returned by Open(). 554 // This session id won't be returned by Open().
550 if (capture_session_id == kStartOpenSessionId) { 555 if (capture_session_id == kStartOpenSessionId) {
551 media::VideoCaptureDevice::Names device_names; 556 media::VideoCaptureDevice::Names device_names;
552 GetAvailableDevices(MEDIA_DEVICE_VIDEO_CAPTURE, &device_names); 557 GetAvailableDevices(MEDIA_DEVICE_VIDEO_CAPTURE, &device_names);
553 if (device_names.empty()) { 558 if (device_names.empty()) {
554 // No devices available. 559 // No devices available.
555 return NULL; 560 return NULL;
556 } 561 }
557 StreamDeviceInfo device(MEDIA_DEVICE_VIDEO_CAPTURE, 562 StreamDeviceInfo device(MEDIA_DEVICE_VIDEO_CAPTURE,
558 device_names.front().device_name, 563 device_names.front().name(),
559 device_names.front().unique_id, false); 564 device_names.front().id(),
565 false);
560 566
561 // Call OnOpen to open using the first device in the list. 567 // Call OnOpen to open using the first device in the list.
562 OnOpen(capture_session_id, device); 568 OnOpen(capture_session_id, device);
563 569
564 VideoCaptureDevices::iterator dit = devices_.find(capture_session_id); 570 VideoCaptureDevices::iterator dit = devices_.find(capture_session_id);
565 if (dit != devices_.end()) { 571 if (dit != devices_.end()) {
566 return dit->second.capture_device; 572 return dit->second.capture_device;
567 } 573 }
568 } 574 }
569 return NULL; 575 return NULL;
570 } 576 }
571 577
572 } // namespace content 578 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698