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 #include "content/renderer/media/media_stream_audio_level_calculator.h" | 5 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 | 9 |
10 namespace content { | 10 namespace content { |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator() | 32 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator() |
33 : counter_(0), | 33 : counter_(0), |
34 max_amplitude_(0), | 34 max_amplitude_(0), |
35 level_(0) { | 35 level_(0) { |
36 } | 36 } |
37 | 37 |
38 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() { | 38 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() { |
39 } | 39 } |
40 | 40 |
41 int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data, | 41 int MediaStreamAudioLevelCalculator::Calculate( |
42 int number_of_channels, | 42 const int16* audio_data, |
43 int number_of_frames) { | 43 int number_of_channels, |
| 44 int number_of_frames, |
| 45 bool force_report_nonzero_energy) { |
44 DCHECK(thread_checker_.CalledOnValidThread()); | 46 DCHECK(thread_checker_.CalledOnValidThread()); |
45 // |level_| is updated every 10 callbacks. For the case where callback comes | 47 // |level_| is updated every 10 callbacks. For the case where callback comes |
46 // every 10ms, |level_| will be updated approximately every 100ms. | 48 // every 10ms, |level_| will be updated approximately every 100ms. |
47 static const int kUpdateFrequency = 10; | 49 static const int kUpdateFrequency = 10; |
48 | 50 |
49 int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames); | 51 int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames); |
50 max_amplitude_ = std::max(max_amplitude_, max); | 52 max_amplitude_ = std::max(max_amplitude_, max); |
51 | 53 |
52 if (counter_++ == kUpdateFrequency) { | 54 if (counter_++ == kUpdateFrequency) { |
53 level_ = max_amplitude_; | 55 level_ = (max_amplitude_ == 0 ? |
| 56 force_report_nonzero_energy : max_amplitude_); |
54 | 57 |
55 // Decay the absolute maximum amplitude by 1/4. | 58 // Decay the absolute maximum amplitude by 1/4. |
56 max_amplitude_ >>= 2; | 59 max_amplitude_ >>= 2; |
57 | 60 |
58 // Reset the counter. | 61 // Reset the counter. |
59 counter_ = 0; | 62 counter_ = 0; |
60 } | 63 } |
61 | 64 |
62 return level_; | 65 return level_; |
63 } | 66 } |
64 | 67 |
65 } // namespace content | 68 } // namespace content |
OLD | NEW |