| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/threading/thread_checker.h" |
| 9 #include "base/synchronization/lock.h" | |
| 10 #include "content/common/content_export.h" | |
| 11 | 9 |
| 12 namespace media { | 10 namespace media { |
| 13 class AudioBus; | 11 class AudioBus; |
| 14 } | 12 } |
| 15 | 13 |
| 16 namespace content { | 14 namespace content { |
| 17 | 15 |
| 18 // This class is used by the WebRtcAudioCapturer to calculate the level of the | 16 // This class is used by the WebRtcLocalAudioTrack to calculate the level of |
| 19 // audio signal. And the audio level will be eventually used by the volume | 17 // the audio signal. And the audio level will be eventually used by the volume |
| 20 // animation UI. | 18 // animation UI. |
| 21 // | |
| 22 // The algorithm used by this class is the same as how it is done in | 19 // The algorithm used by this class is the same as how it is done in |
| 23 // third_party/webrtc/voice_engine/level_indicator.cc. | 20 // third_party/webrtc/voice_engine/level_indicator.cc. |
| 24 class CONTENT_EXPORT MediaStreamAudioLevelCalculator { | 21 class MediaStreamAudioLevelCalculator { |
| 25 public: | 22 public: |
| 26 // Provides thread-safe access to the current signal level. This object is | |
| 27 // intended to be passed to modules running on other threads that poll for the | |
| 28 // current signal level. | |
| 29 class Level : public base::RefCountedThreadSafe<Level> { | |
| 30 public: | |
| 31 float GetCurrent() const; | |
| 32 | |
| 33 private: | |
| 34 friend class MediaStreamAudioLevelCalculator; | |
| 35 friend class base::RefCountedThreadSafe<Level>; | |
| 36 | |
| 37 Level(); | |
| 38 ~Level(); | |
| 39 | |
| 40 void Set(float level); | |
| 41 | |
| 42 mutable base::Lock lock_; | |
| 43 float level_; | |
| 44 }; | |
| 45 | |
| 46 MediaStreamAudioLevelCalculator(); | 23 MediaStreamAudioLevelCalculator(); |
| 47 ~MediaStreamAudioLevelCalculator(); | 24 ~MediaStreamAudioLevelCalculator(); |
| 48 | 25 |
| 49 const scoped_refptr<Level>& level() const { return level_; } | 26 // Calculates the signal level of the audio data, returning the absolute value |
| 50 | 27 // of the amplitude of the signal. |
| 51 // Scans the audio signal in |audio_bus| and computes a new signal level | 28 float Calculate(const media::AudioBus& audio_bus); |
| 52 // exposed by Level. If |assume_nonzero_energy| is true, then a completely | |
| 53 // zero'ed-out |audio_bus| will be accounted for as having a very faint, | |
| 54 // non-zero level. | |
| 55 void Calculate(const media::AudioBus& audio_bus, bool assume_nonzero_energy); | |
| 56 | 29 |
| 57 private: | 30 private: |
| 31 // Used to DCHECK that the constructor and Calculate() are always called on |
| 32 // the same audio thread. Note that the destructor will be called on a |
| 33 // different thread, which can be either the main render thread or a new |
| 34 // audio thread where WebRtcLocalAudioTrack::OnSetFormat() is called. |
| 35 base::ThreadChecker thread_checker_; |
| 36 |
| 58 int counter_; | 37 int counter_; |
| 59 float max_amplitude_; | 38 float max_amplitude_; |
| 60 const scoped_refptr<Level> level_; | 39 float level_; |
| 61 }; | 40 }; |
| 62 | 41 |
| 63 } // namespace content | 42 } // namespace content |
| 64 | 43 |
| 65 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ | 44 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ |
| OLD | NEW |