summaryrefslogtreecommitdiffstats
path: root/media/audio/fake_audio_consumer_unittest.cc
blob: ab97eaea8c76c78fa263bd41793d26b58be50a00 (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
// Copyright (c) 2012 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 "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "media/audio/audio_parameters.h"
#include "media/audio/fake_audio_consumer.h"
#include "media/audio/simple_sources.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

static const int kTestCallbacks = 5;

class FakeAudioConsumerTest : public testing::Test {
 public:
  FakeAudioConsumerTest()
      : params_(
            AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_STEREO, 44100, 8, 128),
        fake_consumer_(message_loop_.message_loop_proxy(), params_),
        source_(params_.channels(), 200.0, params_.sample_rate()) {
    time_between_callbacks_ = base::TimeDelta::FromMicroseconds(
        params_.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
        static_cast<float>(params_.sample_rate()));
  }

  ~FakeAudioConsumerTest() override {}

  void ConsumeData(AudioBus* audio_bus) {
    source_.OnMoreData(audio_bus, 0);
  }

  void RunOnAudioThread() {
    ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
    fake_consumer_.Start(base::Bind(
        &FakeAudioConsumerTest::ConsumeData, base::Unretained(this)));
  }

  void RunOnceOnAudioThread() {
    ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
    RunOnAudioThread();
    // Start() should immediately post a task to run the source callback, so we
    // should end up with only a single callback being run.
    message_loop_.PostTask(FROM_HERE, base::Bind(
        &FakeAudioConsumerTest::EndTest, base::Unretained(this), 1));
  }

  void StopStartOnAudioThread() {
    ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
    fake_consumer_.Stop();
    RunOnAudioThread();
  }

  void TimeCallbacksOnAudioThread(int callbacks) {
    ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());

    if (source_.callbacks() == 0) {
      RunOnAudioThread();
      start_time_ = base::TimeTicks::Now();
    }

    // Keep going until we've seen the requested number of callbacks.
    if (source_.callbacks() < callbacks) {
      message_loop_.PostDelayedTask(FROM_HERE, base::Bind(
          &FakeAudioConsumerTest::TimeCallbacksOnAudioThread,
          base::Unretained(this), callbacks), time_between_callbacks_ / 2);
    } else {
      end_time_ = base::TimeTicks::Now();
      EndTest(callbacks);
    }
  }

  void EndTest(int callbacks) {
    ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
    fake_consumer_.Stop();
    EXPECT_LE(callbacks, source_.callbacks());
    message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
  }

 protected:
  base::MessageLoop message_loop_;
  AudioParameters params_;
  FakeAudioConsumer fake_consumer_;
  SineWaveAudioSource source_;
  base::TimeTicks start_time_;
  base::TimeTicks end_time_;
  base::TimeDelta time_between_callbacks_;

 private:
  DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumerTest);
};

// Ensure the fake audio stream runs on the audio thread and handles fires
// callbacks to the AudioSourceCallback.
TEST_F(FakeAudioConsumerTest, FakeStreamBasicCallback) {
  message_loop_.PostTask(FROM_HERE, base::Bind(
      &FakeAudioConsumerTest::RunOnceOnAudioThread,
      base::Unretained(this)));
  message_loop_.Run();
}

// Ensure the time between callbacks is sane.
TEST_F(FakeAudioConsumerTest, TimeBetweenCallbacks) {
  message_loop_.PostTask(FROM_HERE, base::Bind(
      &FakeAudioConsumerTest::TimeCallbacksOnAudioThread,
      base::Unretained(this), kTestCallbacks));
  message_loop_.Run();

  // There are only (kTestCallbacks - 1) intervals between kTestCallbacks.
  base::TimeDelta actual_time_between_callbacks =
      (end_time_ - start_time_) / (source_.callbacks() - 1);

  // Ensure callback time is no faster than the expected time between callbacks.
  EXPECT_TRUE(actual_time_between_callbacks >= time_between_callbacks_);

  // Softly check if the callback time is no slower than twice the expected time
  // between callbacks.  Since this test runs on the bots we can't be too strict
  // with the bounds.
  if (actual_time_between_callbacks > 2 * time_between_callbacks_)
    LOG(ERROR) << "Time between fake audio callbacks is too large!";
}

// Ensure Start()/Stop() on the stream doesn't generate too many callbacks.  See
// http://crbug.com/159049
TEST_F(FakeAudioConsumerTest, StartStopClearsCallbacks) {
  message_loop_.PostTask(FROM_HERE, base::Bind(
      &FakeAudioConsumerTest::TimeCallbacksOnAudioThread,
      base::Unretained(this), kTestCallbacks));

  // Issue a Stop() / Start() in between expected callbacks to maximize the
  // chance of catching the FakeAudioOutputStream doing the wrong thing.
  message_loop_.PostDelayedTask(FROM_HERE, base::Bind(
      &FakeAudioConsumerTest::StopStartOnAudioThread,
      base::Unretained(this)), time_between_callbacks_ / 2);

  // EndTest() will ensure the proper number of callbacks have occurred.
  message_loop_.Run();
}

}  // namespace media