OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/audio_silence_detector.h" | 5 #include "remoting/host/audio_silence_detector.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 namespace remoting { | 9 namespace remoting { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Silence period threshold in seconds. Silence intervals shorter than this | 13 // Silence period threshold in seconds. Silence intervals shorter than this |
14 // value are still encoded and sent to the client, so that we don't disrupt | 14 // value are still encoded and sent to the client, so that we don't disrupt |
15 // playback by dropping them. | 15 // playback by dropping them. |
16 int kSilencePeriodThresholdSeconds = 1; | 16 int kSilencePeriodThresholdSeconds = 1; |
17 | 17 |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 AudioSilenceDetector::AudioSilenceDetector(int threshold) | 20 AudioSilenceDetector::AudioSilenceDetector(int threshold) |
21 : threshold_(threshold), | 21 : threshold_(threshold), |
22 silence_length_max_(0), | 22 silence_length_max_(0), |
23 silence_length_(0) { | 23 silence_length_(0), |
| 24 channels_(0) { |
24 DCHECK_GE(threshold_, 0); | 25 DCHECK_GE(threshold_, 0); |
25 } | 26 } |
26 | 27 |
27 AudioSilenceDetector::~AudioSilenceDetector() { | 28 AudioSilenceDetector::~AudioSilenceDetector() { |
28 } | 29 } |
29 | 30 |
30 void AudioSilenceDetector::Reset(int sampling_rate, int channels) { | 31 void AudioSilenceDetector::Reset(int sampling_rate, int channels) { |
31 DCHECK_GT(sampling_rate, 0); | 32 DCHECK_GT(sampling_rate, 0); |
| 33 DCHECK_GT(channels, 0); |
32 silence_length_ = 0; | 34 silence_length_ = 0; |
33 silence_length_max_ = | 35 silence_length_max_ = |
34 sampling_rate * channels * kSilencePeriodThresholdSeconds; | 36 sampling_rate * channels * kSilencePeriodThresholdSeconds; |
| 37 channels_ = channels; |
35 } | 38 } |
36 | 39 |
37 bool AudioSilenceDetector::IsSilence(const int16_t* samples, | 40 bool AudioSilenceDetector::IsSilence(const int16_t* samples, |
38 size_t samples_count) { | 41 size_t frames) { |
| 42 const int samples_count = frames * channels(); |
39 bool silent_packet = true; | 43 bool silent_packet = true; |
40 // Potentially this loop can be optimized (e.g. using SSE or adding special | 44 // Potentially this loop can be optimized (e.g. using SSE or adding special |
41 // case for threshold_==0), but it's not worth worrying about because the | 45 // case for threshold_==0), but it's not worth worrying about because the |
42 // amount of data it processes is relaively small. | 46 // amount of data it processes is relaively small. |
43 for (size_t i = 0; i < samples_count; ++i) { | 47 for (int i = 0; i < samples_count; ++i) { |
44 if (abs(samples[i]) > threshold_) { | 48 if (abs(samples[i]) > threshold_) { |
45 silent_packet = false; | 49 silent_packet = false; |
46 break; | 50 break; |
47 } | 51 } |
48 } | 52 } |
49 | 53 |
50 if (!silent_packet) { | 54 if (!silent_packet) { |
51 silence_length_ = 0; | 55 silence_length_ = 0; |
52 return false; | 56 return false; |
53 } | 57 } |
54 | 58 |
55 silence_length_ += samples_count; | 59 silence_length_ += samples_count; |
56 return silence_length_ > silence_length_max_; | 60 return silence_length_ > silence_length_max_; |
57 } | 61 } |
58 | 62 |
| 63 int AudioSilenceDetector::channels() const { |
| 64 return channels_; |
| 65 } |
| 66 |
59 } // namespace remoting | 67 } // namespace remoting |
OLD | NEW |