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

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

Issue 2787703004: [Mojo Video Capture] Fix VideoCaptureManager exposing implementation details to clients (Closed)
Patch Set: Rebase to April 5 Created 3 years, 8 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/media_stream_manager.h" 5 #include "content/browser/renderer_host/media/media_stream_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "content/public/browser/web_contents_media_capture_id.h" 42 #include "content/public/browser/web_contents_media_capture_id.h"
43 #include "content/public/common/content_client.h" 43 #include "content/public/common/content_client.h"
44 #include "content/public/common/content_switches.h" 44 #include "content/public/common/content_switches.h"
45 #include "content/public/common/media_stream_request.h" 45 #include "content/public/common/media_stream_request.h"
46 #include "crypto/hmac.h" 46 #include "crypto/hmac.h"
47 #include "media/audio/audio_device_description.h" 47 #include "media/audio/audio_device_description.h"
48 #include "media/audio/audio_system.h" 48 #include "media/audio/audio_system.h"
49 #include "media/base/audio_parameters.h" 49 #include "media/base/audio_parameters.h"
50 #include "media/base/channel_layout.h" 50 #include "media/base/channel_layout.h"
51 #include "media/base/media_switches.h" 51 #include "media/base/media_switches.h"
52 #include "media/capture/video/video_capture_system.h" 52 #include "media/capture/video/video_capture_device_factory.h"
53 #include "media/capture/video/video_capture_system_impl.h"
53 #include "url/gurl.h" 54 #include "url/gurl.h"
54 #include "url/origin.h" 55 #include "url/origin.h"
55 56
56 #if defined(OS_WIN) 57 #if defined(OS_WIN)
57 #include "base/win/scoped_com_initializer.h" 58 #include "base/win/scoped_com_initializer.h"
58 #endif 59 #endif
59 60
60 #if defined(OS_CHROMEOS) 61 #if defined(OS_CHROMEOS)
61 #include "chromeos/audio/cras_audio_handler.h" 62 #include "chromeos/audio/cras_audio_handler.h"
62 #endif 63 #endif
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 MediaStreamManager* msm = g_media_stream_manager_tls_ptr.Pointer()->Get(); 387 MediaStreamManager* msm = g_media_stream_manager_tls_ptr.Pointer()->Get();
387 if (!msm) { 388 if (!msm) {
388 DLOG(ERROR) << "No MediaStreamManager on the IO thread. " << message; 389 DLOG(ERROR) << "No MediaStreamManager on the IO thread. " << message;
389 return; 390 return;
390 } 391 }
391 392
392 msm->AddLogMessageOnIOThread(message); 393 msm->AddLogMessageOnIOThread(message);
393 } 394 }
394 395
395 MediaStreamManager::MediaStreamManager(media::AudioSystem* audio_system) 396 MediaStreamManager::MediaStreamManager(media::AudioSystem* audio_system)
397 : MediaStreamManager(audio_system, nullptr, nullptr) {}
398
399 MediaStreamManager::MediaStreamManager(
400 media::AudioSystem* audio_system,
401 std::unique_ptr<media::VideoCaptureSystem> video_capture_system,
402 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner)
396 : audio_system_(audio_system), 403 : audio_system_(audio_system),
397 #if defined(OS_WIN) 404 #if defined(OS_WIN)
398 video_capture_thread_("VideoCaptureThread"), 405 video_capture_thread_("VideoCaptureThread"),
399 #endif 406 #endif
400 use_fake_ui_(base::CommandLine::ForCurrentProcess()->HasSwitch( 407 use_fake_ui_(base::CommandLine::ForCurrentProcess()->HasSwitch(
401 switches::kUseFakeUIForMediaStream)) { 408 switches::kUseFakeUIForMediaStream)) {
402 DCHECK(audio_system_); 409 DCHECK(audio_system_);
403 410
404 // Some unit tests create the MSM in the IO thread and assumes the 411 if (!video_capture_system) {
405 // initialization is done synchronously. 412 video_capture_system = base::MakeUnique<media::VideoCaptureSystemImpl>(
406 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { 413 media::VideoCaptureDeviceFactory::CreateFactory(
407 InitializeDeviceManagersOnIOThread(); 414 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)));
408 } else {
409 BrowserThread::PostTask(
410 BrowserThread::IO, FROM_HERE,
411 base::Bind(&MediaStreamManager::InitializeDeviceManagersOnIOThread,
412 base::Unretained(this)));
413 } 415 }
416 if (!device_task_runner) {
417 device_task_runner = audio_system_->GetTaskRunner();
418 #if defined(OS_WIN)
419 // Use an STA Video Capture Thread to try to avoid crashes on enumeration of
420 // buggy third party Direct Show modules, http://crbug.com/428958.
421 video_capture_thread_.init_com_with_mta(false);
422 CHECK(video_capture_thread_.Start());
423 device_task_runner = video_capture_thread_.task_runner();
424 #endif
425 }
426 InitializeMaybeAsync(std::move(video_capture_system),
427 std::move(device_task_runner));
414 428
415 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); 429 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
416 // BrowserMainLoop always creates the PowerMonitor instance before creating 430 // BrowserMainLoop always creates the PowerMonitor instance before creating
417 // MediaStreamManager, but power_monitor may be NULL in unit tests. 431 // MediaStreamManager, but power_monitor may be NULL in unit tests.
418 if (power_monitor) 432 if (power_monitor)
419 power_monitor->AddObserver(this); 433 power_monitor->AddObserver(this);
420 } 434 }
421 435
422 MediaStreamManager::~MediaStreamManager() { 436 MediaStreamManager::~MediaStreamManager() {
423 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::IO)); 437 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::IO));
(...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 const std::string& label, 1221 const std::string& label,
1208 DeviceRequest* request, 1222 DeviceRequest* request,
1209 const MediaStreamDevices& devices) { 1223 const MediaStreamDevices& devices) {
1210 if (!request->callback.is_null()) 1224 if (!request->callback.is_null())
1211 request->callback.Run(devices, std::move(request->ui_proxy)); 1225 request->callback.Run(devices, std::move(request->ui_proxy));
1212 1226
1213 // Delete the request since it is done. 1227 // Delete the request since it is done.
1214 DeleteRequest(label); 1228 DeleteRequest(label);
1215 } 1229 }
1216 1230
1217 void MediaStreamManager::InitializeDeviceManagersOnIOThread() { 1231 void MediaStreamManager::InitializeMaybeAsync(
1218 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1232 std::unique_ptr<media::VideoCaptureSystem> video_capture_system,
1233 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner) {
1234 // Some unit tests initialize the MSM in the IO thread and assume the
1235 // initialization is done synchronously. Other clients call this from a
1236 // different thread and expect initialization to run asynchronously.
1237 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
1238 BrowserThread::PostTask(
1239 BrowserThread::IO, FROM_HERE,
1240 base::Bind(&MediaStreamManager::InitializeMaybeAsync,
1241 base::Unretained(this), base::Passed(&video_capture_system),
1242 std::move(device_task_runner)));
1243 return;
1244 }
1245
1219 // Store a pointer to |this| on the IO thread to avoid having to jump to the 1246 // Store a pointer to |this| on the IO thread to avoid having to jump to the
1220 // UI thread to fetch a pointer to the MSM. In particular on Android, it can 1247 // UI thread to fetch a pointer to the MSM. In particular on Android, it can
1221 // be problematic to post to a UI thread from arbitrary worker threads since 1248 // be problematic to post to a UI thread from arbitrary worker threads since
1222 // attaching to the VM is required and we may have to access the MSM from 1249 // attaching to the VM is required and we may have to access the MSM from
1223 // callback threads that we don't own and don't want to attach. 1250 // callback threads that we don't own and don't want to attach.
1224 g_media_stream_manager_tls_ptr.Pointer()->Set(this); 1251 g_media_stream_manager_tls_ptr.Pointer()->Set(this);
1225 1252
1226 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is 1253 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is
1227 // fixed. 1254 // fixed.
1228 tracked_objects::ScopedTracker tracking_profile1( 1255 tracked_objects::ScopedTracker tracking_profile1(
1229 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1256 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1230 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 1")); 1257 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 1"));
1231 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1232 1258
1233 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is 1259 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is
1234 // fixed. 1260 // fixed.
1235 tracked_objects::ScopedTracker tracking_profile2( 1261 tracked_objects::ScopedTracker tracking_profile2(
1236 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1262 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1237 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 2")); 1263 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 2"));
1238 audio_input_device_manager_ = 1264 audio_input_device_manager_ =
1239 new AudioInputDeviceManager(audio_system_->GetAudioManager()); 1265 new AudioInputDeviceManager(audio_system_->GetAudioManager());
1240 audio_input_device_manager_->RegisterListener(this); 1266 audio_input_device_manager_->RegisterListener(this);
1241 1267
1242 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is 1268 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is
1243 // fixed. 1269 // fixed.
1244 tracked_objects::ScopedTracker tracking_profile3( 1270 tracked_objects::ScopedTracker tracking_profile3(
1245 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1271 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1246 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 3")); 1272 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 3"));
1247 // We want to be notified of IO message loop destruction to delete the thread 1273 // We want to be notified of IO message loop destruction to delete the thread
1248 // and the device managers. 1274 // and the device managers.
1249 base::MessageLoop::current()->AddDestructionObserver(this); 1275 base::MessageLoop::current()->AddDestructionObserver(this);
1250 1276
1251 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is 1277 // TODO(dalecurtis): Remove ScopedTracker below once crbug.com/457525 is
1252 // fixed. 1278 // fixed.
1253 tracked_objects::ScopedTracker tracking_profile4( 1279 tracked_objects::ScopedTracker tracking_profile4(
1254 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1280 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1255 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 4")); 1281 "457525 MediaStreamManager::InitializeDeviceManagersOnIOThread 4"));
1256 auto video_capture_system = base::MakeUnique<media::VideoCaptureSystem>( 1282
1257 media::VideoCaptureDeviceFactory::CreateFactory(
1258 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)));
1259 #if defined(OS_WIN)
1260 // Use an STA Video Capture Thread to try to avoid crashes on enumeration of
1261 // buggy third party Direct Show modules, http://crbug.com/428958.
1262 video_capture_thread_.init_com_with_mta(false);
1263 CHECK(video_capture_thread_.Start());
1264 video_capture_manager_ = new VideoCaptureManager( 1283 video_capture_manager_ = new VideoCaptureManager(
1265 std::move(video_capture_system), video_capture_thread_.task_runner()); 1284 std::move(video_capture_system), std::move(device_task_runner));
1266 #else
1267 video_capture_manager_ = new VideoCaptureManager(
1268 std::move(video_capture_system), audio_system_->GetTaskRunner());
1269 #endif
1270
1271 video_capture_manager_->RegisterListener(this); 1285 video_capture_manager_->RegisterListener(this);
1272 1286
1273 media_devices_manager_.reset( 1287 media_devices_manager_.reset(
1274 new MediaDevicesManager(audio_system_, video_capture_manager_, this)); 1288 new MediaDevicesManager(audio_system_, video_capture_manager_, this));
1275 } 1289 }
1276 1290
1277 void MediaStreamManager::Opened(MediaStreamType stream_type, 1291 void MediaStreamManager::Opened(MediaStreamType stream_type,
1278 int capture_session_id) { 1292 int capture_session_id) {
1279 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1293 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1280 DVLOG(1) << "Opened({stream_type = " << stream_type << "} " 1294 DVLOG(1) << "Opened({stream_type = " << stream_type << "} "
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
1761 request->ui_proxy->OnStarted( 1775 request->ui_proxy->OnStarted(
1762 base::Bind(&MediaStreamManager::StopMediaStreamFromBrowser, 1776 base::Bind(&MediaStreamManager::StopMediaStreamFromBrowser,
1763 base::Unretained(this), label), 1777 base::Unretained(this), label),
1764 base::Bind(&MediaStreamManager::OnMediaStreamUIWindowId, 1778 base::Bind(&MediaStreamManager::OnMediaStreamUIWindowId,
1765 base::Unretained(this), request->video_type(), 1779 base::Unretained(this), request->video_type(),
1766 request->devices)); 1780 request->devices));
1767 } 1781 }
1768 } 1782 }
1769 1783
1770 } // namespace content 1784 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698