Update V8 to version 4.7.1.
[chromium-blink-merge.git] / components / audio_modem / audio_player_unittest.cc
blob5d43d0156bbdd3ffda03fc21e898e39a6aabb303
1 // Copyright 2015 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 "components/audio_modem/audio_player.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "components/audio_modem/audio_player_impl.h"
12 #include "components/audio_modem/public/audio_modem_types.h"
13 #include "components/audio_modem/test/random_samples.h"
14 #include "media/audio/audio_manager.h"
15 #include "media/audio/audio_manager_base.h"
16 #include "media/base/audio_bus.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace {
21 class TestAudioOutputStream : public media::AudioOutputStream {
22 public:
23 using GatherSamplesCallback =
24 base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>;
25 TestAudioOutputStream(int default_frame_count,
26 int max_frame_count,
27 GatherSamplesCallback gather_callback)
28 : default_frame_count_(default_frame_count),
29 max_frame_count_(max_frame_count),
30 gather_callback_(gather_callback),
31 callback_(nullptr) {
32 caller_loop_ = base::MessageLoop::current();
35 ~TestAudioOutputStream() override {}
37 bool Open() override { return true; }
38 void Start(AudioSourceCallback* callback) override {
39 callback_ = callback;
40 GatherPlayedSamples();
42 void Stop() override {}
43 void SetVolume(double volume) override {}
44 void GetVolume(double* volume) override {}
45 void Close() override {}
47 private:
48 void GatherPlayedSamples() {
49 int frames = 0, total_frames = 0;
50 do {
51 // Call back into the player to get samples that it wants us to play.
52 scoped_ptr<media::AudioBus> dest =
53 media::AudioBus::Create(1, default_frame_count_);
54 frames = callback_->OnMoreData(dest.get(), 0);
55 total_frames += frames;
56 // Send the samples given to us by the player to the gather callback.
57 caller_loop_->task_runner()->PostTask(
58 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames));
59 } while (frames && total_frames < max_frame_count_);
62 int default_frame_count_;
63 int max_frame_count_;
64 GatherSamplesCallback gather_callback_;
65 AudioSourceCallback* callback_;
66 base::MessageLoop* caller_loop_;
68 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream);
71 } // namespace
73 namespace audio_modem {
75 class AudioPlayerTest : public testing::Test,
76 public base::SupportsWeakPtr<AudioPlayerTest> {
77 public:
78 AudioPlayerTest() : buffer_index_(0), player_(nullptr) {
79 if (!media::AudioManager::Get())
80 media::AudioManager::CreateForTesting();
83 ~AudioPlayerTest() override { DeletePlayer(); }
85 void CreatePlayer() {
86 DeletePlayer();
87 player_ = new AudioPlayerImpl();
88 player_->set_output_stream_for_testing(new TestAudioOutputStream(
89 kDefaultFrameCount,
90 kMaxFrameCount,
91 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr())));
92 player_->Initialize();
95 void DeletePlayer() {
96 if (!player_)
97 return;
98 player_->Finalize();
99 player_ = nullptr;
102 void PlayAndVerifySamples(
103 const scoped_refptr<media::AudioBusRefCounted>& samples) {
104 DCHECK_LT(samples->frames(), kMaxFrameCount);
106 buffer_ = media::AudioBus::Create(1, kMaxFrameCount);
107 buffer_index_ = 0;
108 player_->Play(samples);
109 player_->FlushAudioLoopForTesting();
110 player_->Stop();
112 int differences = 0;
113 for (int i = 0; i < kMaxFrameCount; ++i) {
114 differences += (buffer_->channel(0)[i] !=
115 samples->channel(0)[i % samples->frames()]);
117 ASSERT_EQ(0, differences);
119 buffer_.reset();
122 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) {
123 if (!buffer_.get())
124 return;
125 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get());
126 buffer_index_ += frames;
129 protected:
130 bool IsPlaying() {
131 player_->FlushAudioLoopForTesting();
132 return player_->is_playing_;
135 static const int kDefaultFrameCount = 1024;
136 static const int kMaxFrameCount = 1024 * 100;
138 scoped_ptr<media::AudioBus> buffer_;
139 int buffer_index_;
141 // Deleted by calling Finalize() on the object.
142 AudioPlayerImpl* player_;
143 base::MessageLoop message_loop_;
146 TEST_F(AudioPlayerTest, BasicPlayAndStop) {
147 CreatePlayer();
148 scoped_refptr<media::AudioBusRefCounted> samples =
149 media::AudioBusRefCounted::Create(1, 7331);
151 player_->Play(samples);
152 EXPECT_TRUE(IsPlaying());
153 player_->Stop();
154 EXPECT_FALSE(IsPlaying());
155 player_->Play(samples);
157 EXPECT_TRUE(IsPlaying());
158 player_->Stop();
159 EXPECT_FALSE(IsPlaying());
160 player_->Play(samples);
162 EXPECT_TRUE(IsPlaying());
163 player_->Stop();
164 EXPECT_FALSE(IsPlaying());
166 DeletePlayer();
169 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) {
170 CreatePlayer();
171 scoped_refptr<media::AudioBusRefCounted> samples =
172 media::AudioBusRefCounted::Create(1, 1337);
174 player_->Stop();
175 player_->Stop();
176 player_->Stop();
177 EXPECT_FALSE(IsPlaying());
179 player_->Play(samples);
180 player_->Play(samples);
181 EXPECT_TRUE(IsPlaying());
183 player_->Stop();
184 player_->Stop();
185 EXPECT_FALSE(IsPlaying());
187 DeletePlayer();
190 TEST_F(AudioPlayerTest, PlayingEndToEnd) {
191 const int kNumSamples = kDefaultFrameCount * 7 + 321;
192 CreatePlayer();
194 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples));
196 DeletePlayer();
199 TEST_F(AudioPlayerTest, PlayingEndToEndRepeated) {
200 const int kNumSamples = kDefaultFrameCount * 7 + 537;
201 CreatePlayer();
203 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples));
205 PlayAndVerifySamples(
206 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123));
208 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2));
210 DeletePlayer();
213 } // namespace audio_modem