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