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 private: 60 // Used by the unittests to get a list of fake devices. 61 friend class MediaStreamDispatcherHostTest; 62 void GetFakeDeviceNames(media::AudioDeviceNames* device_names); 63 64 typedef std::vector<StreamDeviceInfo> StreamDeviceList; 65 virtual ~AudioInputDeviceManager(); 66 67 // Enumerates audio input devices on media stream device thread. 68 void EnumerateOnDeviceThread(MediaStreamType stream_type); 69 // Opens the device on media stream device thread. 70 void OpenOnDeviceThread(int session_id, const StreamDeviceInfo& info); 71 72 // Callback used by EnumerateOnDeviceThread(), called with a list of 73 // enumerated devices on IO thread. 74 void DevicesEnumeratedOnIOThread(MediaStreamType stream_type, 75 scoped_ptr<StreamDeviceInfoArray> devices); 76 // Callback used by OpenOnDeviceThread(), called with the session_id 77 // referencing the opened device on IO thread. 78 void OpenedOnIOThread(int session_id, const StreamDeviceInfo& info); 79 // Callback used by CloseOnDeviceThread(), called with the session_id 80 // referencing the closed device on IO thread. 81 void ClosedOnIOThread(MediaStreamType type, int session_id); 82 83 // Verifies that the calling thread is media stream device thread. 84 bool IsOnDeviceThread() const; 85 86 // Helper to return iterator to the device referenced by |session_id|. If no 87 // device is found, it will return devices_.end(). 88 StreamDeviceList::iterator GetDevice(int session_id); 89 90 // Only accessed on Browser::IO thread. 91 MediaStreamProviderListener* listener_; 92 int next_capture_session_id_; 93 bool use_fake_device_; 94 StreamDeviceList devices_; 95 96 media::AudioManager* const audio_manager_; // Weak. 97 98 // The message loop of media stream device thread that this object runs on. 99 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_; 100 101 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManager); 102 }; 103 104 } // namespace content 105 106 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_ 107