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 #ifndef MEDIA_AUDIO_AGC_AUDIO_STREAM_H_
6 #define MEDIA_AUDIO_AGC_AUDIO_STREAM_H_
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/lock.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/timer/timer.h"
13 #include "media/audio/audio_io.h"
15 // The template based AgcAudioStream implements platform-independent parts
16 // of the AudioInterface interface. Supported interfaces to pass as
17 // AudioInterface are AudioIntputStream and AudioOutputStream. Each platform-
18 // dependent implementation should derive from this class.
20 // Usage example (on Windows):
22 // class WASAPIAudioInputStream : public AgcAudioStream<AudioInputStream> {
24 // WASAPIAudioInputStream();
30 // 1) User creates AgcAudioStream<AudioInputStream>
31 // 2) User calls AudioInputStream::SetAutomaticGainControl(true) =>
32 // AGC usage is now initialized but not yet started.
33 // 3) User calls AudioInputStream::Start() => implementation calls
34 // AgcAudioStream<AudioInputStream>::StartAgc() which detects that AGC
35 // is enabled and then starts the periodic AGC timer.
36 // 4) Microphone volume samples are now taken and included in all
37 // AudioInputCallback::OnData() callbacks.
38 // 5) User calls AudioInputStream::Stop() => implementation calls
39 // AgcAudioStream<AudioInputStream>::StopAgc() which stops the timer.
41 // Note that, calling AudioInputStream::SetAutomaticGainControl(false) while
42 // AGC measurements are active will not have an effect until StopAgc(),
43 // StartAgc() are called again since SetAutomaticGainControl() only sets a
46 // Calling SetAutomaticGainControl(true) enables the AGC and StartAgc() starts
47 // a periodic timer which calls QueryAndStoreNewMicrophoneVolume()
48 // approximately once every second. QueryAndStoreNewMicrophoneVolume() asks
49 // the actual microphone about its current volume level. This value is
50 // normalized and stored so it can be read by GetAgcVolume() when the real-time
51 // audio thread needs the value. The main idea behind this scheme is to avoid
52 // accessing the audio hardware from the real-time audio thread and to ensure
53 // that we don't take new microphone-level samples too often (~1 Hz is a
54 // suitable compromise). The timer will be active until StopAgc() is called.
56 // This class should be created and destroyed on the audio manager thread and
57 // a thread checker is added to ensure that this is the case (uses DCHECK).
58 // All methods except GetAgcVolume() should be called on the creating thread
59 // as well to ensure that thread safety is maintained. It will also guarantee
60 // that the periodic timer runs on the audio manager thread.
61 // |normalized_volume_|, which is updated by QueryAndStoreNewMicrophoneVolume()
62 // and read in GetAgcVolume(), is protected by a lock to ensure that it can
63 // be accessed from any real-time audio thread that needs it to update the its
68 template <typename AudioInterface
>
69 class MEDIA_EXPORT AgcAudioStream
: public AudioInterface
{
71 // Time between two successive timer events.
72 static const int kIntervalBetweenVolumeUpdatesMs
= 1000;
75 : agc_is_enabled_(false), max_volume_(0.0), normalized_volume_(0.0) {
78 virtual ~AgcAudioStream() {
79 DCHECK(thread_checker_
.CalledOnValidThread());
83 // Starts the periodic timer which periodically checks and updates the
84 // current microphone volume level.
85 // The timer is only started if AGC mode is first enabled using the
86 // SetAutomaticGainControl() method.
88 DCHECK(thread_checker_
.CalledOnValidThread());
89 if (!agc_is_enabled_
|| timer_
.IsRunning())
92 // Query and cache the volume to avoid sending 0 as volume to AGC at the
93 // beginning of the audio stream, otherwise AGC will try to raise the
95 QueryAndStoreNewMicrophoneVolume();
97 timer_
.Start(FROM_HERE
,
98 base::TimeDelta::FromMilliseconds(kIntervalBetweenVolumeUpdatesMs
),
99 this, &AgcAudioStream::QueryAndStoreNewMicrophoneVolume
);
102 // Stops the periodic timer which periodically checks and updates the
103 // current microphone volume level.
105 DCHECK(thread_checker_
.CalledOnValidThread());
106 if (timer_
.IsRunning())
110 // Stores a new microphone volume level by checking the audio input device.
111 // Called on the audio manager thread.
112 void UpdateAgcVolume() {
113 DCHECK(thread_checker_
.CalledOnValidThread());
115 if (!timer_
.IsRunning())
118 // We take new volume samples once every second when the AGC is enabled.
119 // To ensure that a new setting has an immediate effect, the new volume
120 // setting is cached here. It will ensure that the next OnData() callback
121 // will contain a new valid volume level. If this approach was not taken,
122 // we could report invalid volume levels to the client for a time period
123 // of up to one second.
124 QueryAndStoreNewMicrophoneVolume();
127 // Gets the latest stored volume level if AGC is enabled.
128 // Called at each capture callback on a real-time capture thread (platform
130 void GetAgcVolume(double* normalized_volume
) {
131 base::AutoLock
lock(lock_
);
132 *normalized_volume
= normalized_volume_
;
136 // Sets the automatic gain control (AGC) to on or off. When AGC is enabled,
137 // the microphone volume is queried periodically and the volume level can
138 // be read in each AudioInputCallback::OnData() callback and fed to the
139 // render-side AGC. User must call StartAgc() as well to start measuring
140 // the microphone level.
141 virtual bool SetAutomaticGainControl(bool enabled
) override
{
142 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled
<< ")";
143 DCHECK(thread_checker_
.CalledOnValidThread());
144 agc_is_enabled_
= enabled
;
148 // Gets the current automatic gain control state.
149 virtual bool GetAutomaticGainControl() override
{
150 DCHECK(thread_checker_
.CalledOnValidThread());
151 return agc_is_enabled_
;
154 // Takes a new microphone volume sample and stores it in |normalized_volume_|.
155 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux.
156 // This method is called periodically when AGC is enabled and always on the
157 // audio manager thread. We use it to read the current microphone level and
158 // to store it so it can be read by the main capture thread. By using this
159 // approach, we can avoid accessing audio hardware from a real-time audio
160 // thread and it leads to a more stable capture performance.
161 void QueryAndStoreNewMicrophoneVolume() {
162 DCHECK(thread_checker_
.CalledOnValidThread());
164 // Cach the maximum volume if this is the first time we ask for it.
165 if (max_volume_
== 0.0)
166 max_volume_
= static_cast<AudioInterface
*>(this)->GetMaxVolume();
168 // Retrieve the current volume level by asking the audio hardware.
169 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux.
170 if (max_volume_
!= 0.0) {
171 double normalized_volume
=
172 static_cast<AudioInterface
*>(this)->GetVolume() / max_volume_
;
173 base::AutoLock
auto_lock(lock_
);
174 normalized_volume_
= normalized_volume
;
178 // Ensures that this class is created and destroyed on the same thread.
179 base::ThreadChecker thread_checker_
;
181 // Repeating timer which cancels itself when it goes out of scope.
182 // Used to check the microphone volume periodically.
183 base::RepeatingTimer
<AgcAudioStream
<AudioInterface
> > timer_
;
185 // True when automatic gain control is enabled, false otherwise.
186 bool agc_is_enabled_
;
188 // Stores the maximum volume which is used for normalization to a volume
189 // range of [0.0, 1.0].
192 // Contains last result of internal call to GetVolume(). We save resources
193 // by not querying the capture volume for each callback. Guarded by |lock_|.
194 // The range is normalized to [0.0, 1.0].
195 double normalized_volume_
;
197 // Protects |normalized_volume_| .
200 DISALLOW_COPY_AND_ASSIGN(AgcAudioStream
<AudioInterface
>);
205 #endif // MEDIA_AUDIO_AGC_AUDIO_STREAM_H_