| 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" |
| 9 #include "base/synchronization/lock.h" |
| 8 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.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 WebRtcLocalAudioTrack to calculate the level of |
| 17 // the audio signal. And the audio level will be eventually used by the volume | 19 // the audio signal. And the audio level will be eventually used by the volume |
| 18 // animation UI. | 20 // animation UI. |
| 19 // The algorithm used by this class is the same as how it is done in | 21 // The algorithm used by this class is the same as how it is done in |
| 20 // third_party/webrtc/voice_engine/level_indicator.cc. | 22 // third_party/webrtc/voice_engine/level_indicator.cc. |
| 21 class MediaStreamAudioLevelCalculator { | 23 class MediaStreamAudioLevelCalculator { |
| 22 public: | 24 public: |
| 25 // Provides thread-safe access to the current signal level. |
| 26 class ReportedLevel : public base::RefCountedThreadSafe<ReportedLevel> { |
| 27 public: |
| 28 ReportedLevel(); |
| 29 |
| 30 float Get() const; |
| 31 |
| 32 private: |
| 33 friend class base::RefCountedThreadSafe<ReportedLevel>; |
| 34 friend class MediaStreamAudioLevelCalculator; |
| 35 |
| 36 ~ReportedLevel(); |
| 37 |
| 38 mutable base::Lock lock_; |
| 39 float level_; |
| 40 }; |
| 41 |
| 23 MediaStreamAudioLevelCalculator(); | 42 MediaStreamAudioLevelCalculator(); |
| 24 ~MediaStreamAudioLevelCalculator(); | 43 ~MediaStreamAudioLevelCalculator(); |
| 25 | 44 |
| 26 // Calculates the signal level of the audio data, returning the absolute value | 45 // Calculates the signal level of the audio data. |
| 27 // of the amplitude of the signal. | 46 void Calculate(const media::AudioBus& audio_bus, |
| 28 float Calculate(const media::AudioBus& audio_bus); | 47 bool assume_nonzero_energy); |
| 48 |
| 49 // Returns a thread-safe accessor to the current absolute value of the |
| 50 // amplitude of the signal. |
| 51 scoped_refptr<ReportedLevel> reported_level() const { |
| 52 return reported_level_; |
| 53 } |
| 29 | 54 |
| 30 private: | 55 private: |
| 31 // Used to DCHECK that the constructor and Calculate() are always called on | 56 // 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 | 57 // 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 | 58 // different thread, which can be either the main render thread or a new |
| 34 // audio thread where WebRtcLocalAudioTrack::OnSetFormat() is called. | 59 // audio thread where WebRtcLocalAudioTrack::OnSetFormat() is called. |
| 35 base::ThreadChecker thread_checker_; | 60 base::ThreadChecker thread_checker_; |
| 36 | 61 |
| 37 int counter_; | 62 int counter_; |
| 38 float max_amplitude_; | 63 float max_amplitude_; |
| 39 float level_; | 64 const scoped_refptr<ReportedLevel> reported_level_; |
| 40 }; | 65 }; |
| 41 | 66 |
| 42 } // namespace content | 67 } // namespace content |
| 43 | 68 |
| 44 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ | 69 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ |
| OLD | NEW |