OLD | NEW |
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/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "content/browser/renderer_host/media/video_capture_controller.h" | 12 #include "content/browser/renderer_host/media/video_capture_controller.h" |
13 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 13 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
14 #include "content/browser/renderer_host/media/web_contents_video_capture_device.
h" | 14 #include "content/browser/renderer_host/media/web_contents_video_capture_device.
h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/common/media_stream_request.h" | 16 #include "content/public/common/media_stream_request.h" |
17 #include "media/video/capture/fake_video_capture_device.h" | 17 #include "media/video/capture/fake_video_capture_device.h" |
18 #include "media/video/capture/video_capture_device.h" | 18 #include "media/video/capture/video_capture_device.h" |
19 | 19 |
20 using content::BrowserThread; | 20 namespace content { |
21 | |
22 namespace media_stream { | |
23 | 21 |
24 // Starting id for the first capture session. | 22 // Starting id for the first capture session. |
25 // VideoCaptureManager::kStartOpenSessionId is used as default id without | 23 // VideoCaptureManager::kStartOpenSessionId is used as default id without |
26 // explicitly calling open device. | 24 // explicitly calling open device. |
27 enum { kFirstSessionId = VideoCaptureManager::kStartOpenSessionId + 1 }; | 25 enum { kFirstSessionId = VideoCaptureManager::kStartOpenSessionId + 1 }; |
28 | 26 |
29 struct VideoCaptureManager::Controller { | 27 struct VideoCaptureManager::Controller { |
30 Controller( | 28 Controller( |
31 VideoCaptureController* vc_controller, | 29 VideoCaptureController* vc_controller, |
32 VideoCaptureControllerEventHandler* handler) | 30 VideoCaptureControllerEventHandler* handler) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 124 |
127 media::VideoCaptureDevice::Names device_names; | 125 media::VideoCaptureDevice::Names device_names; |
128 GetAvailableDevices(&device_names); | 126 GetAvailableDevices(&device_names); |
129 | 127 |
130 StreamDeviceInfoArray devices; | 128 StreamDeviceInfoArray devices; |
131 for (media::VideoCaptureDevice::Names::iterator it = | 129 for (media::VideoCaptureDevice::Names::iterator it = |
132 device_names.begin(); it != device_names.end(); ++it) { | 130 device_names.begin(); it != device_names.end(); ++it) { |
133 bool opened = DeviceOpened(*it); | 131 bool opened = DeviceOpened(*it); |
134 // NOTE: Only support enumeration of the MEDIA_DEVICE_VIDEO_CAPTURE type. | 132 // NOTE: Only support enumeration of the MEDIA_DEVICE_VIDEO_CAPTURE type. |
135 devices.push_back(StreamDeviceInfo( | 133 devices.push_back(StreamDeviceInfo( |
136 content::MEDIA_DEVICE_VIDEO_CAPTURE, it->device_name, | 134 MEDIA_DEVICE_VIDEO_CAPTURE, it->device_name, it->unique_id, opened)); |
137 it->unique_id, opened)); | |
138 } | 135 } |
139 | 136 |
140 PostOnDevicesEnumerated(devices); | 137 PostOnDevicesEnumerated(devices); |
141 } | 138 } |
142 | 139 |
143 void VideoCaptureManager::OnOpen(int capture_session_id, | 140 void VideoCaptureManager::OnOpen(int capture_session_id, |
144 const StreamDeviceInfo& device) { | 141 const StreamDeviceInfo& device) { |
145 DCHECK(IsOnDeviceThread()); | 142 DCHECK(IsOnDeviceThread()); |
146 DCHECK(devices_.find(capture_session_id) == devices_.end()); | 143 DCHECK(devices_.find(capture_session_id) == devices_.end()); |
147 DVLOG(1) << "VideoCaptureManager::OnOpen, id " << capture_session_id; | 144 DVLOG(1) << "VideoCaptureManager::OnOpen, id " << capture_session_id; |
(...skipping 12 matching lines...) Expand all Loading... |
160 // Open the device. | 157 // Open the device. |
161 media::VideoCaptureDevice::Name vc_device_name; | 158 media::VideoCaptureDevice::Name vc_device_name; |
162 vc_device_name.device_name = device.name; | 159 vc_device_name.device_name = device.name; |
163 vc_device_name.unique_id = device.device_id; | 160 vc_device_name.unique_id = device.device_id; |
164 | 161 |
165 if (use_fake_device_) { | 162 if (use_fake_device_) { |
166 video_capture_device = | 163 video_capture_device = |
167 media::FakeVideoCaptureDevice::Create(vc_device_name); | 164 media::FakeVideoCaptureDevice::Create(vc_device_name); |
168 } else { | 165 } else { |
169 switch (device.stream_type) { | 166 switch (device.stream_type) { |
170 case content::MEDIA_DEVICE_VIDEO_CAPTURE: | 167 case MEDIA_DEVICE_VIDEO_CAPTURE: |
171 video_capture_device = | 168 video_capture_device = |
172 media::VideoCaptureDevice::Create(vc_device_name); | 169 media::VideoCaptureDevice::Create(vc_device_name); |
173 break; | 170 break; |
174 case content::MEDIA_TAB_VIDEO_CAPTURE: | 171 case MEDIA_TAB_VIDEO_CAPTURE: |
175 video_capture_device = content::WebContentsVideoCaptureDevice::Create( | 172 video_capture_device = WebContentsVideoCaptureDevice::Create( |
176 vc_device_name.unique_id); | 173 vc_device_name.unique_id); |
177 break; | 174 break; |
178 default: | 175 default: |
179 NOTIMPLEMENTED(); | 176 NOTIMPLEMENTED(); |
180 break; | 177 break; |
181 } | 178 } |
182 } | 179 } |
183 if (!video_capture_device) { | 180 if (!video_capture_device) { |
184 PostOnError(capture_session_id, kDeviceNotAvailable); | 181 PostOnError(capture_session_id, kDeviceNotAvailable); |
185 return; | 182 return; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 279 |
283 if (!stopped_cb.is_null()) | 280 if (!stopped_cb.is_null()) |
284 stopped_cb.Run(); | 281 stopped_cb.Run(); |
285 | 282 |
286 if (capture_session_id == kStartOpenSessionId) { | 283 if (capture_session_id == kStartOpenSessionId) { |
287 // This device was opened from Start(), not Open(). Close it! | 284 // This device was opened from Start(), not Open(). Close it! |
288 OnClose(capture_session_id); | 285 OnClose(capture_session_id); |
289 } | 286 } |
290 } | 287 } |
291 | 288 |
292 void VideoCaptureManager::OnOpened(content::MediaStreamDeviceType stream_type, | 289 void VideoCaptureManager::OnOpened(MediaStreamDeviceType stream_type, |
293 int capture_session_id) { | 290 int capture_session_id) { |
294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
295 if (!listener_) { | 292 if (!listener_) { |
296 // Listener has been removed. | 293 // Listener has been removed. |
297 return; | 294 return; |
298 } | 295 } |
299 listener_->Opened(stream_type, capture_session_id); | 296 listener_->Opened(stream_type, capture_session_id); |
300 } | 297 } |
301 | 298 |
302 void VideoCaptureManager::OnClosed(content::MediaStreamDeviceType stream_type, | 299 void VideoCaptureManager::OnClosed(MediaStreamDeviceType stream_type, |
303 int capture_session_id) { | 300 int capture_session_id) { |
304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
305 if (!listener_) { | 302 if (!listener_) { |
306 // Listener has been removed. | 303 // Listener has been removed. |
307 return; | 304 return; |
308 } | 305 } |
309 listener_->Closed(stream_type, capture_session_id); | 306 listener_->Closed(stream_type, capture_session_id); |
310 } | 307 } |
311 | 308 |
312 void VideoCaptureManager::OnDevicesEnumerated( | 309 void VideoCaptureManager::OnDevicesEnumerated( |
313 const StreamDeviceInfoArray& devices) { | 310 const StreamDeviceInfoArray& devices) { |
314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
315 if (!listener_) { | 312 if (!listener_) { |
316 // Listener has been removed. | 313 // Listener has been removed. |
317 return; | 314 return; |
318 } | 315 } |
319 // NOTE: Only support enumeration of the MEDIA_DEVICE_VIDEO_CAPTURE type. | 316 // NOTE: Only support enumeration of the MEDIA_DEVICE_VIDEO_CAPTURE type. |
320 listener_->DevicesEnumerated(content::MEDIA_DEVICE_VIDEO_CAPTURE, devices); | 317 listener_->DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, devices); |
321 } | 318 } |
322 | 319 |
323 void VideoCaptureManager::OnError(content::MediaStreamDeviceType stream_type, | 320 void VideoCaptureManager::OnError(MediaStreamDeviceType stream_type, |
324 int capture_session_id, | 321 int capture_session_id, |
325 MediaStreamProviderError error) { | 322 MediaStreamProviderError error) { |
326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
327 if (!listener_) { | 324 if (!listener_) { |
328 // Listener has been removed. | 325 // Listener has been removed. |
329 return; | 326 return; |
330 } | 327 } |
331 listener_->Error(stream_type, capture_session_id, error); | 328 listener_->Error(stream_type, capture_session_id, error); |
332 } | 329 } |
333 | 330 |
334 void VideoCaptureManager::PostOnOpened( | 331 void VideoCaptureManager::PostOnOpened( |
335 content::MediaStreamDeviceType stream_type, int capture_session_id) { | 332 MediaStreamDeviceType stream_type, int capture_session_id) { |
336 DCHECK(IsOnDeviceThread()); | 333 DCHECK(IsOnDeviceThread()); |
337 BrowserThread::PostTask(BrowserThread::IO, | 334 BrowserThread::PostTask(BrowserThread::IO, |
338 FROM_HERE, | 335 FROM_HERE, |
339 base::Bind(&VideoCaptureManager::OnOpened, this, | 336 base::Bind(&VideoCaptureManager::OnOpened, this, |
340 stream_type, capture_session_id)); | 337 stream_type, capture_session_id)); |
341 } | 338 } |
342 | 339 |
343 void VideoCaptureManager::PostOnClosed( | 340 void VideoCaptureManager::PostOnClosed( |
344 content::MediaStreamDeviceType stream_type, int capture_session_id) { | 341 MediaStreamDeviceType stream_type, int capture_session_id) { |
345 DCHECK(IsOnDeviceThread()); | 342 DCHECK(IsOnDeviceThread()); |
346 BrowserThread::PostTask(BrowserThread::IO, | 343 BrowserThread::PostTask(BrowserThread::IO, |
347 FROM_HERE, | 344 FROM_HERE, |
348 base::Bind(&VideoCaptureManager::OnClosed, this, | 345 base::Bind(&VideoCaptureManager::OnClosed, this, |
349 stream_type, capture_session_id)); | 346 stream_type, capture_session_id)); |
350 } | 347 } |
351 | 348 |
352 void VideoCaptureManager::PostOnDevicesEnumerated( | 349 void VideoCaptureManager::PostOnDevicesEnumerated( |
353 const StreamDeviceInfoArray& devices) { | 350 const StreamDeviceInfoArray& devices) { |
354 DCHECK(IsOnDeviceThread()); | 351 DCHECK(IsOnDeviceThread()); |
355 BrowserThread::PostTask(BrowserThread::IO, | 352 BrowserThread::PostTask(BrowserThread::IO, |
356 FROM_HERE, | 353 FROM_HERE, |
357 base::Bind(&VideoCaptureManager::OnDevicesEnumerated, | 354 base::Bind(&VideoCaptureManager::OnDevicesEnumerated, |
358 this, devices)); | 355 this, devices)); |
359 } | 356 } |
360 | 357 |
361 void VideoCaptureManager::PostOnError(int capture_session_id, | 358 void VideoCaptureManager::PostOnError(int capture_session_id, |
362 MediaStreamProviderError error) { | 359 MediaStreamProviderError error) { |
363 DCHECK(IsOnDeviceThread()); | 360 DCHECK(IsOnDeviceThread()); |
364 content::MediaStreamDeviceType stream_type = | 361 MediaStreamDeviceType stream_type = MEDIA_DEVICE_VIDEO_CAPTURE; |
365 content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
366 VideoCaptureDevices::const_iterator it = devices_.find(capture_session_id); | 362 VideoCaptureDevices::const_iterator it = devices_.find(capture_session_id); |
367 if (it != devices_.end()) | 363 if (it != devices_.end()) |
368 stream_type = it->second.stream_type; | 364 stream_type = it->second.stream_type; |
369 BrowserThread::PostTask(BrowserThread::IO, | 365 BrowserThread::PostTask(BrowserThread::IO, |
370 FROM_HERE, | 366 FROM_HERE, |
371 base::Bind(&VideoCaptureManager::OnError, this, | 367 base::Bind(&VideoCaptureManager::OnError, this, |
372 stream_type, capture_session_id, error)); | 368 stream_type, capture_session_id, error)); |
373 } | 369 } |
374 | 370 |
375 bool VideoCaptureManager::IsOnDeviceThread() const { | 371 bool VideoCaptureManager::IsOnDeviceThread() const { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 // Solution for not using MediaStreamManager. | 505 // Solution for not using MediaStreamManager. |
510 // This session id won't be returned by Open(). | 506 // This session id won't be returned by Open(). |
511 if (capture_session_id == kStartOpenSessionId) { | 507 if (capture_session_id == kStartOpenSessionId) { |
512 media::VideoCaptureDevice::Names device_names; | 508 media::VideoCaptureDevice::Names device_names; |
513 GetAvailableDevices(&device_names); | 509 GetAvailableDevices(&device_names); |
514 if (device_names.empty()) { | 510 if (device_names.empty()) { |
515 // No devices available. | 511 // No devices available. |
516 return NULL; | 512 return NULL; |
517 } | 513 } |
518 // NOTE: Only support enumeration of the MEDIA_DEVICE_VIDEO_CAPTURE type. | 514 // NOTE: Only support enumeration of the MEDIA_DEVICE_VIDEO_CAPTURE type. |
519 StreamDeviceInfo device(content::MEDIA_DEVICE_VIDEO_CAPTURE, | 515 StreamDeviceInfo device(MEDIA_DEVICE_VIDEO_CAPTURE, |
520 device_names.front().device_name, | 516 device_names.front().device_name, |
521 device_names.front().unique_id, false); | 517 device_names.front().unique_id, false); |
522 | 518 |
523 // Call OnOpen to open using the first device in the list. | 519 // Call OnOpen to open using the first device in the list. |
524 OnOpen(capture_session_id, device); | 520 OnOpen(capture_session_id, device); |
525 | 521 |
526 VideoCaptureDevices::iterator dit = devices_.find(capture_session_id); | 522 VideoCaptureDevices::iterator dit = devices_.find(capture_session_id); |
527 if (dit != devices_.end()) { | 523 if (dit != devices_.end()) { |
528 return dit->second.capture_device; | 524 return dit->second.capture_device; |
529 } | 525 } |
530 } | 526 } |
531 return NULL; | 527 return NULL; |
532 } | 528 } |
533 | 529 |
534 } // namespace media_stream | 530 } // namespace content |
OLD | NEW |