Chromium Code Reviews| 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 // 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. | |
| 19 // | |
| 20 // Usage example (on Windows): | |
| 21 // | |
| 22 // class WASAPIAudioInputStream : public AgcAudioStream<AudioInputStream> { | |
| 23 // public: | |
| 24 // WASAPIAudioInputStream(); | |
| 25 // ... | |
| 26 // }; | |
| 27 // | |
| 28 // Calling SetAutomaticGainControl(true) starts a periodic timer which calls | |
| 29 // OnTimer() approximately once every second. OnTimer() calls the | |
| 30 // QueryAndStoreNewMicrophoneVolume() method which asks the actual microphone | |
| 31 // about its current volume level. This value is normalized and stored so it | |
| 32 // can be read by GetAgcVolume() when the real-time audio thread needs the | |
| 33 // value. The main idea behind this scheme is to avoid accessing the audio | |
| 34 // hardware from the real-time audio thread and to ensure that we don't take | |
| 35 // new microphone-level samples too often (~1 Hz is a suitable compromise). | |
| 36 // | |
| 37 // This class should be created and destroyed on the audio manager thread and | |
| 38 // a thread checker is added to ensure that this is the case (uses DCHECK). | |
| 39 // All methods except GetAgcVolume() should be called on the creating thread | |
| 40 // as well to ensure that thread safety is maintained. It will also guarantee | |
| 41 // that the periodic timer runs on the audio manager thread. | |
| 42 // |normalized_volume_|, which is updated by QueryAndStoreNewMicrophoneVolume() | |
| 43 // and read in GetAgcVolume(), is protected by a lock to ensure that it can | |
| 44 // be accessed from any real-time audio thread that needs it to update the its | |
| 45 // AGC volume. | |
| 46 | |
| 47 namespace media { | |
| 48 | |
| 49 template <typename AudioInterface> | |
| 50 class MEDIA_EXPORT AgcAudioStream : public AudioInterface { | |
| 51 public: | |
| 52 // Time between two successive timer events. | |
| 53 static const int kIntervalBetweenVolumeUpdatesMs = 1000; | |
| 54 | |
| 55 AgcAudioStream() | |
| 56 : agc_is_enabled_(false), max_volume_(0.0), normalized_volume_(0.0) { | |
| 57 DVLOG(1) << __FUNCTION__; | |
| 58 } | |
| 59 | |
| 60 virtual ~AgcAudioStream() { | |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 62 DVLOG(1) << __FUNCTION__; | |
| 63 } | |
| 64 | |
| 65 protected: | |
| 66 // Stores a new microphone volume level by checking the audio input device. | |
| 67 // Called on the audio manager thread. | |
| 68 void UpdateAgcVolume() { | |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 70 | |
| 71 // We take new volume samples once every second when the AGC is enabled. | |
| 72 // To ensure that a new setting has an immediate effect, the new volume | |
| 73 // setting is cached here. It will ensure that the next OnData() callback | |
| 74 // will contain a new valid volume level. If this approach was not taken, | |
| 75 // we could report invalid volume levels to the client for a time period | |
| 76 // of up to one second. | |
| 77 QueryAndStoreNewMicrophoneVolume(); | |
| 78 } | |
| 79 | |
| 80 // the latest stored volume level if AGC is enabled. | |
| 81 // Called at each capture callback on a real-time capture thread (platform | |
| 82 // dependent). | |
| 83 void GetAgcVolume(double* normalized_volume) { | |
| 84 base::AutoLock lock(lock_); | |
| 85 *normalized_volume = normalized_volume_; | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 // Sets the automatic gain control (AGC) to on or off. When AGC is enabled, | |
| 90 // the microphone volume is queried periodically and the volume level can | |
| 91 // be read in each AudioInputCallback::OnData() callback and fed to the | |
| 92 // render-side AGC. | |
| 93 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE { | |
| 94 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; | |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 96 if (agc_is_enabled_ == enabled) | |
| 97 return; | |
| 98 if (enabled) { | |
| 99 timer_.Start(FROM_HERE, | |
| 100 base::TimeDelta::FromMilliseconds(kIntervalBetweenVolumeUpdatesMs), | |
| 101 this, &AgcAudioStream::OnTimer); | |
| 102 } else if (timer_.IsRunning()) { | |
| 103 timer_.Stop(); | |
| 104 } | |
| 105 agc_is_enabled_ = enabled; | |
| 106 } | |
| 107 | |
| 108 // Gets the current automatic gain control state. | |
| 109 virtual bool GetAutomaticGainControl() OVERRIDE { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 return agc_is_enabled_; | |
| 112 } | |
| 113 | |
| 114 // Takes a new microphone volume sample and stores it in |normalized_volume_|. | |
| 115 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux. | |
| 116 void QueryAndStoreNewMicrophoneVolume() { | |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 118 DCHECK(timer_.IsRunning()); | |
| 119 | |
| 120 // Cach the maximum volume if this is the first time we ask for it. | |
| 121 if (max_volume_ == 0.0) | |
| 122 max_volume_ = static_cast<AudioInterface*>(this)->GetMaxVolume(); | |
|
henrika (OOO until Aug 14)
2013/05/27 15:39:07
Should now work on all platforms. Only Windows com
| |
| 123 | |
| 124 // Retrieve the current volume level by asking the audio hardware. | |
| 125 // Range is normalized to [0.0,1.0] or [0.0, 1.5] on Linux. | |
| 126 if (max_volume_ != 0.0) { | |
| 127 double normalized_volume = | |
| 128 static_cast<AudioInterface*>(this)->GetVolume() / max_volume_; | |
| 129 base::AutoLock auto_lock(lock_); | |
| 130 normalized_volume_ = normalized_volume; | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 // This method is called periodically when AGC is enabled and always on the | |
| 135 // audio manager thread. We use it to read the current microphone level and | |
| 136 // to store it so it can be read by the main capture thread. By using this | |
| 137 // approach, we can avoid accessing audio hardware from a real-time audio | |
| 138 // thread and it leads to a more stable capture performance. | |
| 139 void OnTimer() { | |
| 140 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 141 QueryAndStoreNewMicrophoneVolume(); | |
| 142 } | |
| 143 | |
| 144 // Ensures that this class is created and destroyed on the same thread. | |
| 145 base::ThreadChecker thread_checker_; | |
| 146 | |
| 147 // Repeating timer which cancels itself when it goes out of scope. | |
| 148 // Used to check the microphone volume periodically. | |
| 149 base::RepeatingTimer<AgcAudioStream<AudioInterface> > timer_; | |
| 150 | |
| 151 // True when automatic gain control is enabled, false otherwise. | |
| 152 bool agc_is_enabled_; | |
| 153 | |
| 154 // Stores the maximum volume which is used for normalization to a volume | |
| 155 // range of [0.0, 1.0]. | |
| 156 double max_volume_; | |
| 157 | |
| 158 // Contains last result of internal call to GetVolume(). We save resources | |
| 159 // by not querying the capture volume for each callback. Guarded by |lock_|. | |
| 160 // The range is normalized to [0.0, 1.0]. | |
| 161 double normalized_volume_; | |
| 162 | |
| 163 // Protects |normalized_volume_| . | |
| 164 base::Lock lock_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(AgcAudioStream<AudioInterface>); | |
| 167 }; | |
| 168 | |
| 169 } // namespace media | |
| 170 | |
| 171 #endif // MEDIA_AUDIO_AGC_AUDIO_STREAM_H_ | |
| OLD | NEW |