Chromium Code Reviews| 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/media_stream_manager.h" | 5 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "content/browser/browser_main_loop.h" | |
| 17 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 18 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 18 #include "content/browser/renderer_host/media/device_request_message_filter.h" | 19 #include "content/browser/renderer_host/media/device_request_message_filter.h" |
| 19 #include "content/browser/renderer_host/media/media_stream_requester.h" | 20 #include "content/browser/renderer_host/media/media_stream_requester.h" |
| 20 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 21 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 21 #include "content/browser/renderer_host/media/video_capture_manager.h" | 22 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 22 #include "content/browser/renderer_host/media/web_contents_capture_util.h" | 23 #include "content/browser/renderer_host/media/web_contents_capture_util.h" |
| 23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/content_browser_client.h" | 25 #include "content/public/browser/content_browser_client.h" |
| 25 #include "content/public/browser/media_device_id.h" | 26 #include "content/public/browser/media_device_id.h" |
| 26 #include "content/public/browser/media_observer.h" | 27 #include "content/public/browser/media_observer.h" |
| 27 #include "content/public/browser/media_request_state.h" | 28 #include "content/public/browser/media_request_state.h" |
| 29 #include "content/public/browser/render_process_host.h" | |
| 28 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
| 29 #include "content/public/common/media_stream_request.h" | 31 #include "content/public/common/media_stream_request.h" |
| 30 #include "media/audio/audio_manager_base.h" | 32 #include "media/audio/audio_manager_base.h" |
| 31 #include "media/audio/audio_parameters.h" | 33 #include "media/audio/audio_parameters.h" |
| 32 #include "media/base/channel_layout.h" | 34 #include "media/base/channel_layout.h" |
| 33 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 34 | 36 |
| 35 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 36 #include "base/win/scoped_com_initializer.h" | 38 #include "base/win/scoped_com_initializer.h" |
| 37 #endif | 39 #endif |
| 38 | 40 |
| 41 static void DoAddLogMessage(const std::string& message) { | |
| 42 // Must be on the UI thread to access BrowserMainLoop. | |
| 43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 44 content::BrowserMainLoop::GetInstance()->media_stream_manager()-> | |
| 45 AddLogMessageOnUIThread(message); | |
| 46 } | |
| 47 | |
| 39 namespace content { | 48 namespace content { |
| 40 | 49 |
| 41 namespace { | 50 namespace { |
| 42 // Creates a random label used to identify requests. | 51 // Creates a random label used to identify requests. |
| 43 std::string RandomLabel() { | 52 std::string RandomLabel() { |
| 44 // An earlier PeerConnection spec, | 53 // An earlier PeerConnection spec, |
| 45 // http://dev.w3.org/2011/webrtc/editor/webrtc.html, specified the | 54 // http://dev.w3.org/2011/webrtc/editor/webrtc.html, specified the |
| 46 // MediaStream::label alphabet as containing 36 characters from | 55 // MediaStream::label alphabet as containing 36 characters from |
| 47 // range: U+0021, U+0023 to U+0027, U+002A to U+002B, U+002D to U+002E, | 56 // range: U+0021, U+0023 to U+0027, U+002A to U+002B, U+002D to U+002E, |
| 48 // U+0030 to U+0039, U+0041 to U+005A, U+005E to U+007E. | 57 // U+0030 to U+0039, U+0041 to U+005A, U+005E to U+007E. |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 const GURL security_origin; | 255 const GURL security_origin; |
| 247 | 256 |
| 248 const MediaStreamRequestType request_type; | 257 const MediaStreamRequestType request_type; |
| 249 | 258 |
| 250 const StreamOptions options; | 259 const StreamOptions options; |
| 251 | 260 |
| 252 ResourceContext* resource_context; | 261 ResourceContext* resource_context; |
| 253 | 262 |
| 254 StreamDeviceInfoArray devices; | 263 StreamDeviceInfoArray devices; |
| 255 | 264 |
| 265 AddWebRtcLogCallback logging_callback; | |
| 266 | |
| 256 // Callback to the requester which audio/video devices have been selected. | 267 // Callback to the requester which audio/video devices have been selected. |
| 257 // It can be null if the requester has no interest to know the result. | 268 // It can be null if the requester has no interest to know the result. |
| 258 // Currently it is only used by |DEVICE_ACCESS| type. | 269 // Currently it is only used by |DEVICE_ACCESS| type. |
| 259 MediaStreamManager::MediaRequestResponseCallback callback; | 270 MediaStreamManager::MediaRequestResponseCallback callback; |
| 260 | 271 |
| 261 scoped_ptr<MediaStreamUIProxy> ui_proxy; | 272 scoped_ptr<MediaStreamUIProxy> ui_proxy; |
| 262 | 273 |
| 263 private: | 274 private: |
| 264 std::vector<MediaRequestState> state_; | 275 std::vector<MediaRequestState> state_; |
| 265 scoped_ptr<MediaStreamRequest> ui_request_; | 276 scoped_ptr<MediaStreamRequest> ui_request_; |
| (...skipping 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1294 #if defined(OS_WIN) | 1305 #if defined(OS_WIN) |
| 1295 device_thread_->init_com_with_mta(true); | 1306 device_thread_->init_com_with_mta(true); |
| 1296 #endif | 1307 #endif |
| 1297 CHECK(device_thread_->Start()); | 1308 CHECK(device_thread_->Start()); |
| 1298 | 1309 |
| 1299 audio_input_device_manager_ = new AudioInputDeviceManager(audio_manager_); | 1310 audio_input_device_manager_ = new AudioInputDeviceManager(audio_manager_); |
| 1300 audio_input_device_manager_->Register( | 1311 audio_input_device_manager_->Register( |
| 1301 this, device_thread_->message_loop_proxy().get()); | 1312 this, device_thread_->message_loop_proxy().get()); |
| 1302 | 1313 |
| 1303 video_capture_manager_ = new VideoCaptureManager(); | 1314 video_capture_manager_ = new VideoCaptureManager(); |
| 1304 video_capture_manager_->Register(this, | 1315 video_capture_manager_->Register( |
| 1305 device_thread_->message_loop_proxy().get()); | 1316 this, device_thread_->message_loop_proxy().get()); |
|
Henrik Grunell
2014/01/17 08:51:25
Nit: AFAICT from the style guide, the code layout
perkj_chrome
2014/01/20 11:07:08
I think Henrik is wrong and vrk is correct :->
Henrik Grunell
2014/01/20 16:23:08
I don't have a very strong opinion on this, do wha
vrk (LEFT CHROMIUM)
2014/01/22 02:25:42
I believe either way is acceptable, but I reverted
| |
| 1306 | 1317 |
| 1307 // We want to be notified of IO message loop destruction to delete the thread | 1318 // We want to be notified of IO message loop destruction to delete the thread |
| 1308 // and the device managers. | 1319 // and the device managers. |
| 1309 io_loop_ = base::MessageLoop::current(); | 1320 io_loop_ = base::MessageLoop::current(); |
| 1310 io_loop_->AddDestructionObserver(this); | 1321 io_loop_->AddDestructionObserver(this); |
| 1311 | 1322 |
| 1312 if (CommandLine::ForCurrentProcess()->HasSwitch( | 1323 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 1313 switches::kUseFakeDeviceForMediaStream)) { | 1324 switches::kUseFakeDeviceForMediaStream)) { |
| 1314 DVLOG(1) << "Using fake device"; | 1325 DVLOG(1) << "Using fake device"; |
| 1315 UseFakeDevice(); | 1326 UseFakeDevice(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1384 } | 1395 } |
| 1385 } | 1396 } |
| 1386 | 1397 |
| 1387 void MediaStreamManager::Closed(MediaStreamType stream_type, | 1398 void MediaStreamManager::Closed(MediaStreamType stream_type, |
| 1388 int capture_session_id) { | 1399 int capture_session_id) { |
| 1389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1400 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 1390 } | 1401 } |
| 1391 | 1402 |
| 1392 void MediaStreamManager::DevicesEnumerated( | 1403 void MediaStreamManager::DevicesEnumerated( |
| 1393 MediaStreamType stream_type, const StreamDeviceInfoArray& devices) { | 1404 MediaStreamType stream_type, const StreamDeviceInfoArray& devices) { |
| 1394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
|
perkj_chrome
2014/01/20 11:07:08
Here all device enumerations end up for both audio
vrk (LEFT CHROMIUM)
2014/01/22 02:25:42
OK, done.
| |
| 1395 DVLOG(1) << "DevicesEnumerated(" | 1406 DVLOG(1) << "DevicesEnumerated(" |
| 1396 << ", {stream_type = " << stream_type << "})"; | 1407 << ", {stream_type = " << stream_type << "})"; |
| 1397 | 1408 |
| 1398 // Only cache the device list when the device list has been changed. | 1409 // Only cache the device list when the device list has been changed. |
| 1399 bool need_update_clients = false; | 1410 bool need_update_clients = false; |
| 1400 EnumerationCache* cache = | 1411 EnumerationCache* cache = |
| 1401 stream_type == MEDIA_DEVICE_AUDIO_CAPTURE ? | 1412 stream_type == MEDIA_DEVICE_AUDIO_CAPTURE ? |
| 1402 &audio_enumeration_cache_ : &video_enumeration_cache_; | 1413 &audio_enumeration_cache_ : &video_enumeration_cache_; |
| 1403 if (!cache->valid || | 1414 if (!cache->valid || |
| 1404 devices.size() != cache->devices.size() || | 1415 devices.size() != cache->devices.size() || |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1459 else | 1470 else |
| 1460 PostRequestToUI(*it, request); | 1471 PostRequestToUI(*it, request); |
| 1461 break; | 1472 break; |
| 1462 } | 1473 } |
| 1463 } | 1474 } |
| 1464 label_list.clear(); | 1475 label_list.clear(); |
| 1465 --active_enumeration_ref_count_[stream_type]; | 1476 --active_enumeration_ref_count_[stream_type]; |
| 1466 DCHECK_GE(active_enumeration_ref_count_[stream_type], 0); | 1477 DCHECK_GE(active_enumeration_ref_count_[stream_type], 0); |
| 1467 } | 1478 } |
| 1468 | 1479 |
| 1480 // static | |
| 1481 void MediaStreamManager::AddLogMessage(const std::string& message) { | |
| 1482 BrowserThread::PostTask( | |
| 1483 BrowserThread::UI, FROM_HERE, | |
| 1484 base::Bind(::DoAddLogMessage, message)); | |
| 1485 } | |
| 1486 | |
| 1487 void MediaStreamManager::AddLogMessageOnUIThread(const std::string& message) { | |
| 1488 // Must be on the UI thread to access RenderProcessHost from process ID. | |
| 1489 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 1490 | |
| 1491 // Add logs for each pending request. | |
| 1492 for (DeviceRequests::iterator it = requests_.begin(); it != requests_.end(); | |
| 1493 ++it) { | |
| 1494 DeviceRequest* request = it->second; | |
| 1495 // Log the message to all renderers that are requesting a MediaStream or | |
| 1496 // device enumeration. | |
| 1497 if (request->request_type == MEDIA_GENERATE_STREAM || | |
| 1498 request->request_type == MEDIA_ENUMERATE_DEVICES) { | |
| 1499 content::RenderProcessHost* render_process_host = | |
| 1500 content::RenderProcessHost::FromID(request->requesting_process_id); | |
| 1501 render_process_host->WebRtcLogMessage(message); | |
| 1502 } | |
| 1503 } | |
| 1504 } | |
| 1505 | |
| 1469 void MediaStreamManager::HandleAccessRequestResponse( | 1506 void MediaStreamManager::HandleAccessRequestResponse( |
| 1470 const std::string& label, | 1507 const std::string& label, |
| 1471 const MediaStreamDevices& devices) { | 1508 const MediaStreamDevices& devices) { |
| 1472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 1473 DVLOG(1) << "HandleAccessRequestResponse(" | 1510 DVLOG(1) << "HandleAccessRequestResponse(" |
| 1474 << ", {label = " << label << "})"; | 1511 << ", {label = " << label << "})"; |
| 1475 | 1512 |
| 1476 DeviceRequest* request = FindRequest(label); | 1513 DeviceRequest* request = FindRequest(label); |
| 1477 if (!request) { | 1514 if (!request) { |
| 1478 // The request has been canceled before the UI returned. | 1515 // The request has been canceled before the UI returned. |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1694 } | 1731 } |
| 1695 | 1732 |
| 1696 // Always do enumeration even though some enumeration is in progress, | 1733 // Always do enumeration even though some enumeration is in progress, |
| 1697 // because those enumeration commands could be sent before these devices | 1734 // because those enumeration commands could be sent before these devices |
| 1698 // change. | 1735 // change. |
| 1699 ++active_enumeration_ref_count_[stream_type]; | 1736 ++active_enumeration_ref_count_[stream_type]; |
| 1700 GetDeviceManager(stream_type)->EnumerateDevices(stream_type); | 1737 GetDeviceManager(stream_type)->EnumerateDevices(stream_type); |
| 1701 } | 1738 } |
| 1702 | 1739 |
| 1703 } // namespace content | 1740 } // namespace content |
| OLD | NEW |