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/threading/thread_checker.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/synchronization/lock.h" |
| 10 #include "content/common/content_export.h" |
9 | 11 |
10 namespace media { | 12 namespace media { |
11 class AudioBus; | 13 class AudioBus; |
12 } | 14 } |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 | 17 |
16 // This class is used by the WebRtcLocalAudioTrack to calculate the level of | 18 // This class is used by the WebRtcAudioCapturer to calculate the level of the |
17 // the audio signal. And the audio level will be eventually used by the volume | 19 // audio signal. And the audio level will be eventually used by the volume |
18 // animation UI. | 20 // animation UI. |
| 21 // |
19 // The algorithm used by this class is the same as how it is done in | 22 // The algorithm used by this class is the same as how it is done in |
20 // third_party/webrtc/voice_engine/level_indicator.cc. | 23 // third_party/webrtc/voice_engine/level_indicator.cc. |
21 class MediaStreamAudioLevelCalculator { | 24 class CONTENT_EXPORT MediaStreamAudioLevelCalculator { |
22 public: | 25 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 |
23 MediaStreamAudioLevelCalculator(); | 46 MediaStreamAudioLevelCalculator(); |
24 ~MediaStreamAudioLevelCalculator(); | 47 ~MediaStreamAudioLevelCalculator(); |
25 | 48 |
26 // Calculates the signal level of the audio data, returning the absolute value | 49 scoped_refptr<Level> level() const { return level_; } |
27 // of the amplitude of the signal. | 50 |
28 float Calculate(const media::AudioBus& audio_bus); | 51 // Scans the audio signal in |audio_bus| and computes a new signal level |
| 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); |
29 | 56 |
30 private: | 57 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 | |
37 int counter_; | 58 int counter_; |
38 float max_amplitude_; | 59 float max_amplitude_; |
39 float level_; | 60 const scoped_refptr<Level> level_; |
40 }; | 61 }; |
41 | 62 |
42 } // namespace content | 63 } // namespace content |
43 | 64 |
44 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ | 65 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ |
OLD | NEW |