// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE-CHROMIUM file. #include "browser/media/media_capture_devices_dispatcher.h" #include "base/logging.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/media_devices_monitor.h" #include "content/public/common/desktop_media_id.h" #include "content/public/common/media_stream_request.h" namespace brightray { using content::BrowserThread; using content::MediaStreamDevices; namespace { // Finds a device in |devices| that has |device_id|, or NULL if not found. const content::MediaStreamDevice* FindDeviceWithId( const content::MediaStreamDevices& devices, const std::string& device_id) { content::MediaStreamDevices::const_iterator iter = devices.begin(); for (; iter != devices.end(); ++iter) { if (iter->id == device_id) { return &(*iter); } } return NULL; }; } // namespace MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() { return Singleton::get(); } MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() : devices_enumerated_(false), is_device_enumeration_disabled_(false) { // MediaCaptureDevicesDispatcher is a singleton. It should be created on // UI thread. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); } MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() {} const MediaStreamDevices& MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); if (!is_device_enumeration_disabled_ && !devices_enumerated_) { content::EnsureMonitorCaptureDevices(); devices_enumerated_ = true; } return audio_devices_; } const MediaStreamDevices& MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); if (!is_device_enumeration_disabled_ && !devices_enumerated_) { content::EnsureMonitorCaptureDevices(); devices_enumerated_ = true; } return video_devices_; } void MediaCaptureDevicesDispatcher::GetDefaultDevices( bool audio, bool video, content::MediaStreamDevices* devices) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(audio || video); if (audio) { const content::MediaStreamDevice* device = GetFirstAvailableAudioDevice(); if (device) devices->push_back(*device); } if (video) { const content::MediaStreamDevice* device = GetFirstAvailableVideoDevice(); if (device) devices->push_back(*device); } } const content::MediaStreamDevice* MediaCaptureDevicesDispatcher::GetRequestedAudioDevice( const std::string& requested_audio_device_id) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); const content::MediaStreamDevice* const device = FindDeviceWithId(audio_devices, requested_audio_device_id); return device; } const content::MediaStreamDevice* MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); if (audio_devices.empty()) return NULL; return &(*audio_devices.begin()); } const content::MediaStreamDevice* MediaCaptureDevicesDispatcher::GetRequestedVideoDevice( const std::string& requested_video_device_id) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); const content::MediaStreamDevice* const device = FindDeviceWithId(video_devices, requested_video_device_id); return device; } const content::MediaStreamDevice* MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); if (video_devices.empty()) return NULL; return &(*video_devices.begin()); } void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() { is_device_enumeration_disabled_ = true; } void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged( const content::MediaStreamDevices& devices) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, base::Bind(&MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread, base::Unretained(this), devices)); } void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged( const content::MediaStreamDevices& devices) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, base::Bind(&MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread, base::Unretained(this), devices)); } void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( int render_process_id, int render_view_id, int page_request_id, const content::MediaStreamDevice& device, content::MediaRequestState state) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); } void MediaCaptureDevicesDispatcher::OnAudioStreamPlayingChanged( int render_process_id, int render_view_id, int stream_id, bool is_playing, float power_dbfs, bool clipped) { } void MediaCaptureDevicesDispatcher::OnCreatingAudioStream( int render_process_id, int render_view_id) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); } void MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread( const content::MediaStreamDevices& devices) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); devices_enumerated_ = true; audio_devices_ = devices; } void MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread( const content::MediaStreamDevices& devices){ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); devices_enumerated_ = true; video_devices_ = devices; } } // namespace brightray