summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_silence_detector_unittest.cc
blob: 52e6958923c809c13f7a7448cd063609ac948571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/audio/audio_silence_detector.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "base/time.h"
#include "media/base/audio_bus.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::InvokeWithoutArgs;
using ::testing::TestWithParam;
using ::testing::Values;

namespace media {

static const int kSampleRate = 48000;
static const int kFramesPerBuffer = 128;

namespace {

class MockObserver {
 public:
  MOCK_METHOD1(OnAudible, void(bool));
};

struct TestScenario {
  const float* data;
  int num_channels;
  int num_frames;
  float value_range;

  TestScenario(const float* d, int c, int f, float v)
      : data(d), num_channels(c), num_frames(f), value_range(v) {}
};

}  // namespace

class AudioSilenceDetectorTest : public TestWithParam<TestScenario> {
 public:
  AudioSilenceDetectorTest()
      : audio_manager_thread_(new base::Thread("AudioThread")),
        notification_received_(false, false) {
    audio_manager_thread_->Start();
    audio_message_loop_ = audio_manager_thread_->message_loop_proxy();
  }

  virtual ~AudioSilenceDetectorTest() {
    SyncWithAudioThread();
  }

  AudioSilenceDetector* silence_detector() const {
    return silence_detector_.get();
  }

  void StartSilenceDetector(float threshold, MockObserver* observer) {
    audio_message_loop_->PostTask(
        FROM_HERE,
        base::Bind(&AudioSilenceDetectorTest::StartDetectorOnAudioThread,
                   base::Unretained(this), threshold, observer));
    SyncWithAudioThread();
  }

  void StopSilenceDetector() {
    audio_message_loop_->PostTask(
        FROM_HERE,
        base::Bind(&AudioSilenceDetectorTest::StopDetectorOnAudioThread,
                   base::Unretained(this)));
    SyncWithAudioThread();
  }

  // Creates an AudioBus, sized and populated with kFramesPerBuffer frames of
  // data.  The given test |data| is repeated to fill the buffer.
  scoped_ptr<AudioBus> CreatePopulatedBuffer(
      const float* data, int num_channels, int num_frames) {
    scoped_ptr<AudioBus> bus = AudioBus::Create(num_channels, kFramesPerBuffer);
    for (int ch = 0; ch < num_channels; ++ch) {
      for (int frames = 0; frames < kFramesPerBuffer; frames += num_frames) {
        const int num_to_copy = std::min(num_frames, kFramesPerBuffer - frames);
        memcpy(bus->channel(ch) + frames, data + num_frames * ch,
               sizeof(float) * num_to_copy);
      }
    }
    return bus.Pass();
  }

  void SignalNotificationReceived() {
    notification_received_.Signal();
  }

  void WaitForNotificationReceived() {
    notification_received_.Wait();
  }

  // Post a task on the audio thread and block until it is executed.  This
  // provides a barrier: All previous tasks pending on the audio thread have
  // completed before this method returns.
  void SyncWithAudioThread() {
    base::WaitableEvent done(false, false);
    audio_message_loop_->PostTask(
        FROM_HERE,
        base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
    done.Wait();
  }

 private:
  void StartDetectorOnAudioThread(float threshold, MockObserver* observer) {
    const AudioSilenceDetector::AudibleCallback notify_is_audible =
        base::Bind(&MockObserver::OnAudible, base::Unretained(observer));
    silence_detector_.reset(new AudioSilenceDetector(
        kSampleRate, base::TimeDelta::FromMilliseconds(1), threshold));
    silence_detector_->Start(notify_is_audible);
  }

  void StopDetectorOnAudioThread() {
    silence_detector_->Stop(true);
    silence_detector_.reset();
  }

  scoped_ptr<base::Thread> audio_manager_thread_;
  scoped_refptr<base::MessageLoopProxy> audio_message_loop_;

  base::WaitableEvent notification_received_;

  scoped_ptr<AudioSilenceDetector> silence_detector_;

  DISALLOW_COPY_AND_ASSIGN(AudioSilenceDetectorTest);
};

TEST_P(AudioSilenceDetectorTest, TriggersAtVariousThresholds) {
  static const float kThresholdsToTry[] =
      { 0.0f, 0.125f, 0.25f, 0.5f, 0.75f, 1.0f };

  const TestScenario& scenario = GetParam();

  for (size_t i = 0; i < arraysize(kThresholdsToTry); ++i) {
    MockObserver observer;

    // Start the detector.  This should cause a single callback to indicate
    // we're starting out in silence.
    EXPECT_CALL(observer, OnAudible(false))
        .WillOnce(InvokeWithoutArgs(
            this, &AudioSilenceDetectorTest::SignalNotificationReceived))
        .RetiresOnSaturation();
    StartSilenceDetector(kThresholdsToTry[i], &observer);
    WaitForNotificationReceived();

    // Send more data to the silence detector.  For some test scenarios, the
    // sound data will trigger a callback.
    const bool expect_a_callback = (kThresholdsToTry[i] < scenario.value_range);
    if (expect_a_callback) {
      EXPECT_CALL(observer, OnAudible(true))
          .WillOnce(InvokeWithoutArgs(
              this, &AudioSilenceDetectorTest::SignalNotificationReceived))
          .RetiresOnSaturation();
    }
    scoped_ptr<AudioBus> bus = CreatePopulatedBuffer(
        scenario.data, scenario.num_channels, scenario.num_frames);
    silence_detector()->Scan(bus.get(), bus->frames());
    if (expect_a_callback)
      WaitForNotificationReceived();

    // Stop the detector.  This should cause another callback to indicate we're
    // ending in silence.
    EXPECT_CALL(observer, OnAudible(false))
        .WillOnce(InvokeWithoutArgs(
            this, &AudioSilenceDetectorTest::SignalNotificationReceived))
        .RetiresOnSaturation();
    StopSilenceDetector();
    WaitForNotificationReceived();

    SyncWithAudioThread();
  }
}

static const float kAllZeros[] = {
  // left channel
  0.0f,
  // right channel
  0.0f
};

static const float kAllOnes[] = {
  // left channel
  1.0f,
  // right channel
  1.0f
};

static const float kSilentLeftChannel[] = {
  // left channel
  0.5f, 0.5f, 0.5f, 0.5f,
  // right channel
  0.0f, 0.25f, 0.0f, 0.0f
};

static const float kSilentRightChannel[] = {
  // left channel
  1.0f, 1.0f, 0.75f, 0.5f, 1.0f,
  // right channel
  0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};

static const float kAtDifferentVolumesAndBias[] = {
  // left channel
  1.0f, 0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f,
  // right channel
  0.0f, 0.2f, 0.4f, 0.6f, 0.4f, 0.2f, 0.0f, 0.2f, 0.4f, 0.6f, 0.4f, 0.2f
};

INSTANTIATE_TEST_CASE_P(
    Scenarios, AudioSilenceDetectorTest,
    Values(
        TestScenario(kAllZeros, 2, 1, 0.0f),
        TestScenario(kAllOnes, 2, 1, 0.0f),
        TestScenario(kSilentLeftChannel, 2, 4, 0.25f),
        TestScenario(kSilentRightChannel, 2, 5, 0.5f),
        TestScenario(kAtDifferentVolumesAndBias, 2, 12, 0.6f)));

}  // namespace media