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.
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.
13 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
14 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
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"
33 class CONTENT_EXPORT AudioInputDeviceManager
: public MediaStreamProvider
{
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
;
41 explicit AudioInputDeviceManager(media::AudioManager
* audio_manager
);
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
);
49 // MediaStreamProvider implementation, called on IO thread.
50 void Register(MediaStreamProviderListener
* listener
,
51 const scoped_refptr
<base::SingleThreadTaskRunner
>&
52 device_task_runner
) override
;
53 void EnumerateDevices(MediaStreamType stream_type
) override
;
54 int Open(const StreamDeviceInfo
& device
) override
;
55 void Close(int session_id
) override
;
58 bool ShouldUseFakeDevice() const;
60 #if defined(OS_CHROMEOS)
61 // Registers and unregisters that a stream using keyboard mic has been opened
62 // or closed. Keeps count of how many such streams are open and activates and
63 // inactivates the keyboard mic accordingly. The (in)activation is done on the
64 // UI thread and for the register case a callback must therefor be provided
65 // which is called when activated.
66 // Called on the IO thread.
67 void RegisterKeyboardMicStream(const base::Closure
& callback
);
68 void UnregisterKeyboardMicStream();
72 // Used by the unittests to get a list of fake devices.
73 friend class MediaStreamDispatcherHostTest
;
74 void GetFakeDeviceNames(media::AudioDeviceNames
* device_names
);
76 typedef std::vector
<StreamDeviceInfo
> StreamDeviceList
;
77 ~AudioInputDeviceManager() override
;
79 // Enumerates audio input devices on media stream device thread.
80 void EnumerateOnDeviceThread(MediaStreamType stream_type
);
81 // Opens the device on media stream device thread.
82 void OpenOnDeviceThread(int session_id
, const StreamDeviceInfo
& info
);
84 // Callback used by EnumerateOnDeviceThread(), called with a list of
85 // enumerated devices on IO thread.
86 void DevicesEnumeratedOnIOThread(MediaStreamType stream_type
,
87 scoped_ptr
<StreamDeviceInfoArray
> devices
);
88 // Callback used by OpenOnDeviceThread(), called with the session_id
89 // referencing the opened device on IO thread.
90 void OpenedOnIOThread(int session_id
, const StreamDeviceInfo
& info
);
91 // Callback used by CloseOnDeviceThread(), called with the session_id
92 // referencing the closed device on IO thread.
93 void ClosedOnIOThread(MediaStreamType type
, int session_id
);
95 // Verifies that the calling thread is media stream device thread.
96 bool IsOnDeviceThread() const;
98 // Helper to return iterator to the device referenced by |session_id|. If no
99 // device is found, it will return devices_.end().
100 StreamDeviceList::iterator
GetDevice(int session_id
);
102 #if defined(OS_CHROMEOS)
103 // Calls Cras audio handler and sets keyboard mic active status.
104 void SetKeyboardMicStreamActiveOnUIThread(bool active
);
107 // Only accessed on Browser::IO thread.
108 MediaStreamProviderListener
* listener_
;
109 int next_capture_session_id_
;
110 bool use_fake_device_
;
111 StreamDeviceList devices_
;
113 #if defined(OS_CHROMEOS)
114 // Keeps count of how many streams are using keyboard mic.
115 int keyboard_mic_streams_count_
;
118 media::AudioManager
* const audio_manager_
; // Weak.
120 // The message loop of media stream device thread that this object runs on.
121 scoped_refptr
<base::SingleThreadTaskRunner
> device_task_runner_
;
123 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManager
);
126 } // namespace content
128 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_