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.
4
5#include "remoting/host/audio_silence_detector.h"
6
7#include "base/basictypes.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace remoting {
11
12namespace {
13
14const int kSamplingRate = 1000;
15
16void TestSilenceDetector(AudioSilenceDetector* target,
17                         const int16* samples, int samples_count,
18                         bool silence_expected) {
19  target->Reset(kSamplingRate, 1);
20  bool silence_started = false;
21  int threshold_length = 0;
22  for (int i = 0; i < 3 * kSamplingRate / samples_count; ++i) {
23    bool result = target->IsSilence(samples, samples_count);
24    if (silence_started) {
25      ASSERT_TRUE(result);
26    } else if (result) {
27      silence_started = true;
28      threshold_length = i * samples_count;
29    }
30  }
31
32  // Check that the silence was detected if it was expected.
33  EXPECT_EQ(silence_expected, silence_started);
34
35  if (silence_expected) {
36    // Check that silence threshold is between 0.5 and 2 seconds.
37    EXPECT_GE(threshold_length, kSamplingRate / 2);
38    EXPECT_LE(threshold_length, kSamplingRate * 2);
39  }
40}
41
42}  // namespace
43
44TEST(AudioSilenceDetectorTest, Silence) {
45  const int16 kSamples[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
46
47  AudioSilenceDetector target(0);
48  TestSilenceDetector(&target, kSamples, arraysize(kSamples), true);
49}
50
51TEST(AudioSilenceDetectorTest, Sound) {
52  const int16 kSamples[] = {65, 73, 83, 89, 92, -1, 5, 9, 123, 0};
53
54  AudioSilenceDetector target(0);
55  TestSilenceDetector(&target, kSamples, arraysize(kSamples), false);
56}
57
58TEST(AudioSilenceDetectorTest, Threshold) {
59  const int16 kSamples[] = {0, 0, 0, 0, 1, 0, 0, -1, 0, 0};
60
61  AudioSilenceDetector target1(0);
62  TestSilenceDetector(&target1, kSamples, arraysize(kSamples), false);
63
64  AudioSilenceDetector target2(1);
65  TestSilenceDetector(&target2, kSamples, arraysize(kSamples), true);
66}
67
68}  // namespace remoting
69