OLD | NEW |
---|---|
(Empty) | |
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 #ifndef MEDIA_AUDIO_AGC_AUDIO_STREAM_H_ | |
6 #define MEDIA_AUDIO_AGC_AUDIO_STREAM_H_ | |
7 | |
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.h" | |
13 #include "media/audio/audio_io.h" | |
14 | |
15 namespace media { | |
16 | |
17 // TODO(henrika): add info about threading | |
tommi (sloooow) - chröme
2013/05/27 13:54:36
Will you do this now?
henrika (OOO until Aug 14)
2013/05/27 15:37:36
Done. Sorry.
| |
18 | |
19 // The template based AgcAudioStream implements platform-independent parts | |
20 // of the AudioInterface interface. Supported interfaces to pass as | |
21 // AudioInterface are AudioIntputStream and AudioOutputStream. Each platform- | |
22 // dependent implementation should derive from this class. | |
23 // | |
24 // Usage example (on Windows): | |
25 // | |
26 // class WASAPIAudioInputStream : public AgcAudioStream<AudioInputStream> { | |
27 // public: | |
28 // WASAPIAudioInputStream(); | |
29 // ... | |
30 // }; | |
31 // | |
32 template <typename AudioInterface> | |
33 class MEDIA_EXPORT AgcAudioStream : public AudioInterface { | |
34 public: | |
35 // Time between two successive timer events. | |
36 enum { kIntervalBetweenVolumeUpdatesMs = 1000 }; | |
tommi (sloooow) - chröme
2013/05/27 13:54:36
I don't think Chrome in general declares constants
henrika (OOO until Aug 14)
2013/05/27 15:37:36
Done.
| |
37 | |
38 AgcAudioStream() | |
39 : agc_is_enabled_(false), max_volume_(0.0), normalized_volume_(0.0) { | |
40 DVLOG(1) << __FUNCTION__; | |
41 } | |
42 | |
43 virtual ~AgcAudioStream() { | |
44 DCHECK(thread_checker_.CalledOnValidThread()); | |
45 DVLOG(1) << __FUNCTION__; | |
46 } | |
47 | |
48 protected: | |
49 // Stores a new microphone volume level by checking the audio input device. | |
50 // Called on the audio manager thread. | |
51 void UpdateAgcVolume() { | |
52 DCHECK(thread_checker_.CalledOnValidThread()); | |
53 | |
54 // We take new volume samples once every second when the AGC is enabled. | |
55 // To ensure that a new setting has an immediate effect, the new volume | |
56 // setting is cached here. It will ensure that the next OnData() callback | |
57 // will contain a new valid volume level. If this approach was not taken, | |
58 // we could report invalid volume levels to the client for a time period | |
59 // of up to one second. | |
60 QueryAndStoreNewMicrophoneVolume(); | |
61 } | |
62 | |
63 // the latest stored volume level if AGC is enabled. | |
64 // Called at each capture callback on a real-time capture thread (platform | |
65 // dependent). | |
66 void GetAgcVolume(double* normalized_volume) { | |
67 base::AutoLock lock(lock_); | |
68 *normalized_volume = normalized_volume_; | |
69 } | |
70 | |
71 private: | |
72 // Sets the automatic gain control (AGC) to on or off. When AGC is enabled, | |
73 // the microphone volume is queried periodically and the volume level can | |
74 // be read in each AudioInputCallback::OnData() callback and fed to the | |
75 // render-side AGC. | |
76 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE { | |
77 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; | |
78 DCHECK(thread_checker_.CalledOnValidThread()); | |
79 if (enabled) { | |
80 timer_.Start(FROM_HERE, | |
81 base::TimeDelta::FromMilliseconds(kIntervalBetweenVolumeUpdatesMs), | |
82 this, &AgcAudioStream::OnTimer); | |
83 } else if (timer_.IsRunning()) | |
tommi (sloooow) - chröme
2013/05/27 13:54:36
{}
(if one side of an else has a brace, the other
henrika (OOO until Aug 14)
2013/05/27 15:37:36
Done.
| |
84 timer_.Stop(); | |
85 agc_is_enabled_ = enabled; | |
tommi (sloooow) - chröme
2013/05/27 13:54:36
What I meant here is that what if these two values
henrika (OOO until Aug 14)
2013/05/27 15:37:36
Done.
| |
86 } | |
87 | |
88 // Gets the current automatic gain control state. | |
89 virtual bool GetAutomaticGainControl() OVERRIDE { | |
90 DCHECK(thread_checker_.CalledOnValidThread()); | |
91 return agc_is_enabled_; | |
92 } | |
93 | |
94 // Takes a new microphone volume sample and stores it in |normalized_volume_|. | |
95 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux. | |
96 void QueryAndStoreNewMicrophoneVolume() { | |
97 DCHECK(thread_checker_.CalledOnValidThread()); | |
98 DCHECK(timer_.IsRunning()); | |
99 | |
100 // Cach the maximum volume if this is the first time we ask for it. | |
101 if (max_volume_ == 0.0) | |
102 max_volume_ = GetMaxVolume(); | |
103 | |
104 // Retrieve the current volume level by asking the audio hardware. | |
105 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux. | |
106 if (max_volume_ != 0.0) { | |
107 double normalized_volume = GetVolume() / max_volume_; | |
108 { | |
tommi (sloooow) - chröme
2013/05/27 13:54:36
nit: there's no need for this scope since the next
henrika (OOO until Aug 14)
2013/05/27 15:37:36
Done.
| |
109 base::AutoLock auto_lock(lock_); | |
110 normalized_volume_ = normalized_volume; | |
111 } | |
112 } | |
113 } | |
114 | |
115 // This method is called periodically when AGC is enabled and always on the | |
116 // audio manager thread. We use it to read the current microphone level and | |
117 // to store it so it can be read by the main capture thread. By using this | |
118 // approach, we can avoid accessing audio hardware from a real-time audio | |
119 // thread and it leads to a more stable capture performance. | |
120 void OnTimer() { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 QueryAndStoreNewMicrophoneVolume(); | |
123 } | |
124 | |
125 // Ensures that this class is created and destroyed on the same thread. | |
126 base::ThreadChecker thread_checker_; | |
127 | |
128 // Repeating timer which cancels itself when it goes out of scope. | |
129 // Used to check the microphone volume periodically. | |
130 base::RepeatingTimer<AgcAudioStream<AudioInterface> > timer_; | |
131 | |
132 // True when automatic gain control is enabled, false otherwise. | |
133 bool agc_is_enabled_; | |
134 | |
135 // Stores the maximum volume which is used for normalization to a volume | |
136 // range of [0.0, 1.0]. | |
137 double max_volume_; | |
138 | |
139 // Contains last result of internal call to GetVolume(). We save resources | |
140 // by not querying the capture volume for each callback. Guarded by |lock_|. | |
141 // The range is normalized to [0.0, 1.0]. | |
142 double normalized_volume_; | |
143 | |
144 // Protects |normalized_volume_| . | |
145 base::Lock lock_; | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(AgcAudioStream<AudioInterface>); | |
148 }; | |
149 | |
150 } // namespace media | |
151 | |
152 #endif // MEDIA_AUDIO_AGC_AUDIO_STREAM_H_ | |
OLD | NEW |