Returning fetching back.
[chromium-blink-merge.git] / components / audio_modem / audio_player_unittest.cc
blob3e19c0635e6c0ee234f4c5d14af276a2506292a6
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/memory/weak_ptr.h"
9 #include "components/audio_modem/audio_player_impl.h"
10 #include "components/audio_modem/public/audio_modem_types.h"
11 #include "components/audio_modem/test/random_samples.h"
12 #include "media/audio/audio_manager.h"
13 #include "media/audio/audio_manager_base.h"
14 #include "media/base/audio_bus.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace {
19 class TestAudioOutputStream : public media::AudioOutputStream {
20 public:
21 using GatherSamplesCallback =
22 base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>;
23 TestAudioOutputStream(int default_frame_count,
24 int max_frame_count,
25 GatherSamplesCallback gather_callback)
26 : default_frame_count_(default_frame_count),
27 max_frame_count_(max_frame_count),
28 gather_callback_(gather_callback),
29 callback_(nullptr) {
30 caller_loop_ = base::MessageLoop::current();
33 ~TestAudioOutputStream() override {}
35 bool Open() override { return true; }
36 void Start(AudioSourceCallback* callback) override {
37 callback_ = callback;
38 GatherPlayedSamples();
40 void Stop() override {}
41 void SetVolume(double volume) override {}
42 void GetVolume(double* volume) override {}
43 void Close() override {}
45 private:
46 void GatherPlayedSamples() {
47 int frames = 0, total_frames = 0;
48 do {
49 // Call back into the player to get samples that it wants us to play.
50 scoped_ptr<media::AudioBus> dest =
51 media::AudioBus::Create(1, default_frame_count_);
52 frames = callback_->OnMoreData(dest.get(), 0);
53 total_frames += frames;
54 // Send the samples given to us by the player to the gather callback.
55 caller_loop_->PostTask(
56 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames));
57 } while (frames && total_frames < max_frame_count_);
60 int default_frame_count_;
61 int max_frame_count_;
62 GatherSamplesCallback gather_callback_;
63 AudioSourceCallback* callback_;
64 base::MessageLoop* caller_loop_;
66 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream);
69 } // namespace
71 namespace audio_modem {
73 class AudioPlayerTest : public testing::Test,
74 public base::SupportsWeakPtr<AudioPlayerTest> {
75 public:
76 AudioPlayerTest() : buffer_index_(0), player_(nullptr) {
77 if (!media::AudioManager::Get())
78 media::AudioManager::CreateForTesting();
81 ~AudioPlayerTest() override { DeletePlayer(); }
83 void CreatePlayer() {
84 DeletePlayer();
85 player_ = new AudioPlayerImpl();
86 player_->set_output_stream_for_testing(new TestAudioOutputStream(
87 kDefaultFrameCount,
88 kMaxFrameCount,
89 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr())));
90 player_->Initialize();
93 void DeletePlayer() {
94 if (!player_)
95 return;
96 player_->Finalize();
97 player_ = nullptr;
100 void PlayAndVerifySamples(
101 const scoped_refptr<media::AudioBusRefCounted>& samples) {
102 DCHECK_LT(samples->frames(), kMaxFrameCount);
104 buffer_ = media::AudioBus::Create(1, kMaxFrameCount);
105 buffer_index_ = 0;
106 player_->Play(samples);
107 player_->FlushAudioLoopForTesting();
108 player_->Stop();
110 int differences = 0;
111 for (int i = 0; i < kMaxFrameCount; ++i) {
112 differences += (buffer_->channel(0)[i] !=
113 samples->channel(0)[i % samples->frames()]);
115 ASSERT_EQ(0, differences);
117 buffer_.reset();
120 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) {
121 if (!buffer_.get())
122 return;
123 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get());
124 buffer_index_ += frames;
127 protected:
128 bool IsPlaying() {
129 player_->FlushAudioLoopForTesting();
130 return player_->is_playing_;
133 static const int kDefaultFrameCount = 1024;
134 static const int kMaxFrameCount = 1024 * 100;
136 scoped_ptr<media::AudioBus> buffer_;
137 int buffer_index_;
139 // Deleted by calling Finalize() on the object.
140 AudioPlayerImpl* player_;
141 base::MessageLoop message_loop_;
144 TEST_F(AudioPlayerTest, BasicPlayAndStop) {
145 CreatePlayer();
146 scoped_refptr<media::AudioBusRefCounted> samples =
147 media::AudioBusRefCounted::Create(1, 7331);
149 player_->Play(samples);
150 EXPECT_TRUE(IsPlaying());
151 player_->Stop();
152 EXPECT_FALSE(IsPlaying());
153 player_->Play(samples);
155 EXPECT_TRUE(IsPlaying());
156 player_->Stop();
157 EXPECT_FALSE(IsPlaying());
158 player_->Play(samples);
160 EXPECT_TRUE(IsPlaying());
161 player_->Stop();
162 EXPECT_FALSE(IsPlaying());
164 DeletePlayer();
167 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) {
168 CreatePlayer();
169 scoped_refptr<media::AudioBusRefCounted> samples =
170 media::AudioBusRefCounted::Create(1, 1337);
172 player_->Stop();
173 player_->Stop();
174 player_->Stop();
175 EXPECT_FALSE(IsPlaying());
177 player_->Play(samples);
178 player_->Play(samples);
179 EXPECT_TRUE(IsPlaying());
181 player_->Stop();
182 player_->Stop();
183 EXPECT_FALSE(IsPlaying());
185 DeletePlayer();
188 TEST_F(AudioPlayerTest, PlayingEndToEnd) {
189 const int kNumSamples = kDefaultFrameCount * 7 + 321;
190 CreatePlayer();
192 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples));
194 DeletePlayer();
197 TEST_F(AudioPlayerTest, PlayingEndToEndRepeated) {
198 const int kNumSamples = kDefaultFrameCount * 7 + 537;
199 CreatePlayer();
201 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples));
203 PlayAndVerifySamples(
204 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123));
206 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2));
208 DeletePlayer();
211 } // namespace audio_modem