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
3 // found in the LICENSE file.
5 #include "remoting/host/audio_silence_detector.h"
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
15 // playback by dropping them.
16 int kSilencePeriodThresholdSeconds
= 1;
20 AudioSilenceDetector::AudioSilenceDetector(int threshold
)
21 : threshold_(threshold
),
22 silence_length_max_(0),
24 DCHECK_GE(threshold_
, 0);
27 AudioSilenceDetector::~AudioSilenceDetector() {
30 void AudioSilenceDetector::Reset(int sampling_rate
, int channels
) {
31 DCHECK_GT(sampling_rate
, 0);
34 sampling_rate
* channels
* kSilencePeriodThresholdSeconds
;
37 bool AudioSilenceDetector::IsSilence(const int16
* samples
,
38 size_t samples_count
) {
39 bool silent_packet
= true;
40 // 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
42 // amount of data it processes is relaively small.
43 for (size_t i
= 0; i
< samples_count
; ++i
) {
44 if (abs(samples
[i
]) > threshold_
) {
45 silent_packet
= false;
55 silence_length_
+= samples_count
;
56 return silence_length_
> silence_length_max_
;
59 } // namespace remoting