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 3 // found in the LICENSE file. 4 // 5 // AudioInputDeviceManager manages the audio input devices. In particular it 6 // communicates with MediaStreamManager and AudioInputRendererHost on the 7 // browser IO thread, handles queries like 8 // enumerate/open/close/GetOpenedDeviceInfoById from MediaStreamManager and 9 // GetOpenedDeviceInfoById from AudioInputRendererHost. 10 // The work for enumerate/open/close is handled asynchronously on Media Stream 11 // device thread, while GetOpenedDeviceInfoById is synchronous on the IO thread. 12 13 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_ 14 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_ 15 16 #include <map> 17 18 #include "base/basictypes.h" 19 #include "base/memory/ref_counted.h" 20 #include "base/threading/thread.h" 21 #include "content/browser/renderer_host/media/media_stream_provider.h" 22 #include "content/common/content_export.h" 23 #include "content/common/media/media_stream_options.h" 24 #include "content/public/common/media_stream_request.h" 25 #include "media/audio/audio_device_name.h" 26 27 namespace media { 28 class AudioManager; 29 } 30 31 namespace content { 32 33 class CONTENT_EXPORT AudioInputDeviceManager : public MediaStreamProvider { 34 public: 35 // Calling Start() with this kFakeOpenSessionId will open the default device, 36 // even though Open() has not been called. This is used to be able to use the 37 // AudioInputDeviceManager before MediaStream is implemented. 38 // TODO(xians): Remove it when the webrtc unittest does not need it any more. 39 static const int kFakeOpenSessionId; 40 41 explicit AudioInputDeviceManager(media::AudioManager* audio_manager); 42 43 // Gets the opened device info by |session_id|. Returns NULL if the device 44 // is not opened, otherwise the opened device. Called on IO thread. 45 const StreamDeviceInfo* GetOpenedDeviceInfoById(int session_id); 46 47 // MediaStreamProvider implementation, called on IO thread. 48 virtual void Register(MediaStreamProviderListener* listener, 49 const scoped_refptr<base::SingleThreadTaskRunner>& 50 device_task_runner) OVERRIDE; 51 virtual void Unregister() OVERRIDE; 52 virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE; 53 virtual int Open(const StreamDeviceInfo& device) OVERRIDE; 54 virtual void Close(int session_id) OVERRIDE; 55 56 void UseFakeDevice(); 57 bool ShouldUseFakeDevice() const; 58 59 #if defined(OS_CHROMEOS) 60 // Registers and unregisters that a stream using keyboard mic has been opened 61 // or closed. Keeps count of how many such streams are open and activates and 62 // inactivates the keyboard mic accordingly. The (in)activation is done on the 63 // UI thread and for the register case a callback must therefor be provided 64 // which is called when activated. 65 // Called on the IO thread. 66 void RegisterKeyboardMicStream(const base::Closure& callback); 67 void UnregisterKeyboardMicStream(); 68 #endif 69 70 private: 71 // Used by the unittests to get a list of fake devices. 72 friend class MediaStreamDispatcherHostTest; 73 void GetFakeDeviceNames(media::AudioDeviceNames* device_names); 74 75 typedef std::vector<StreamDeviceInfo> StreamDeviceList; 76 virtual ~AudioInputDeviceManager(); 77 78 // Enumerates audio input devices on media stream device thread. 79 void EnumerateOnDeviceThread(MediaStreamType stream_type); 80 // Opens the device on media stream device thread. 81 void OpenOnDeviceThread(int session_id, const StreamDeviceInfo& info); 82 83 // Callback used by EnumerateOnDeviceThread(), called with a list of 84 // enumerated devices on IO thread. 85 void DevicesEnumeratedOnIOThread(MediaStreamType stream_type, 86 scoped_ptr<StreamDeviceInfoArray> devices); 87 // Callback used by OpenOnDeviceThread(), called with the session_id 88 // referencing the opened device on IO thread. 89 void OpenedOnIOThread(int session_id, const StreamDeviceInfo& info); 90 // Callback used by CloseOnDeviceThread(), called with the session_id 91 // referencing the closed device on IO thread. 92 void ClosedOnIOThread(MediaStreamType type, int session_id); 93 94 // Verifies that the calling thread is media stream device thread. 95 bool IsOnDeviceThread() const; 96 97 // Helper to return iterator to the device referenced by |session_id|. If no 98 // device is found, it will return devices_.end(). 99 StreamDeviceList::iterator GetDevice(int session_id); 100 101 #if defined(OS_CHROMEOS) 102 // Calls Cras audio handler and sets keyboard mic active status. 103 void SetKeyboardMicStreamActiveOnUIThread(bool active); 104 #endif 105 106 // Only accessed on Browser::IO thread. 107 MediaStreamProviderListener* listener_; 108 int next_capture_session_id_; 109 bool use_fake_device_; 110 StreamDeviceList devices_; 111 112 #if defined(OS_CHROMEOS) 113 // Keeps count of how many streams are using keyboard mic. 114 int keyboard_mic_streams_count_; 115 #endif 116 117 media::AudioManager* const audio_manager_; // Weak. 118 119 // The message loop of media stream device thread that this object runs on. 120 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; 121 122 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManager); 123 }; 124 125 } // namespace content 126 127 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_ 128