1 // Copyright 2014 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 "components/copresence/mediums/audio/audio_player.h"
6
7 #include "base/bind.h"
8 #include "base/memory/weak_ptr.h"
9 #include "components/copresence/public/copresence_constants.h"
10 #include "components/copresence/test/audio_test_support.h"
11 #include "media/audio/audio_manager.h"
12 #include "media/audio/audio_manager_base.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace {
17
18 class TestAudioOutputStream : public media::AudioOutputStream {
19 public:
20 typedef base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>
21 GatherSamplesCallback;
TestAudioOutputStream(int default_frame_count,int max_frame_count,GatherSamplesCallback gather_callback)22 TestAudioOutputStream(int default_frame_count,
23 int max_frame_count,
24 GatherSamplesCallback gather_callback)
25 : default_frame_count_(default_frame_count),
26 max_frame_count_(max_frame_count),
27 gather_callback_(gather_callback),
28 callback_(NULL) {
29 caller_loop_ = base::MessageLoop::current();
30 }
31
~TestAudioOutputStream()32 virtual ~TestAudioOutputStream() {}
33
Open()34 virtual bool Open() OVERRIDE { return true; }
Start(AudioSourceCallback * callback)35 virtual void Start(AudioSourceCallback* callback) OVERRIDE {
36 callback_ = callback;
37 GatherPlayedSamples();
38 }
Stop()39 virtual void Stop() OVERRIDE {}
SetVolume(double volume)40 virtual void SetVolume(double volume) OVERRIDE {}
GetVolume(double * volume)41 virtual void GetVolume(double* volume) OVERRIDE {}
Close()42 virtual void Close() OVERRIDE {}
43
44 private:
GatherPlayedSamples()45 void GatherPlayedSamples() {
46 int frames = 0, total_frames = 0;
47 do {
48 // Call back into the player to get samples that it wants us to play.
49 scoped_ptr<media::AudioBus> dest =
50 media::AudioBus::Create(1, default_frame_count_);
51 frames = callback_->OnMoreData(dest.get(), media::AudioBuffersState());
52 total_frames += frames;
53 // Send the samples given to us by the player to the gather callback.
54 caller_loop_->PostTask(
55 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames));
56 } while (frames && total_frames < max_frame_count_);
57 }
58
59 int default_frame_count_;
60 int max_frame_count_;
61 GatherSamplesCallback gather_callback_;
62 AudioSourceCallback* callback_;
63 base::MessageLoop* caller_loop_;
64
65 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream);
66 };
67
68 } // namespace
69
70 namespace copresence {
71
72 class AudioPlayerTest : public testing::Test,
73 public base::SupportsWeakPtr<AudioPlayerTest> {
74 public:
AudioPlayerTest()75 AudioPlayerTest() : buffer_index_(0), player_(NULL) {
76 if (!media::AudioManager::Get())
77 media::AudioManager::CreateForTesting();
78 }
79
~AudioPlayerTest()80 virtual ~AudioPlayerTest() { DeletePlayer(); }
81
CreatePlayer()82 void CreatePlayer() {
83 DeletePlayer();
84 player_ = new AudioPlayer();
85 player_->set_output_stream_for_testing(new TestAudioOutputStream(
86 kDefaultFrameCount,
87 kMaxFrameCount,
88 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr())));
89 player_->Initialize();
90 }
91
DeletePlayer()92 void DeletePlayer() {
93 if (!player_)
94 return;
95 player_->Finalize();
96 player_ = NULL;
97 }
98
PlayAndVerifySamples(const scoped_refptr<media::AudioBusRefCounted> & samples)99 void PlayAndVerifySamples(
100 const scoped_refptr<media::AudioBusRefCounted>& samples) {
101 buffer_ = media::AudioBus::Create(1, kMaxFrameCount);
102 player_->Play(samples);
103 player_->FlushAudioLoopForTesting();
104
105 int differences = 0;
106 for (int i = 0; i < samples->frames(); ++i)
107 differences += (buffer_->channel(0)[i] != samples->channel(0)[i]);
108 ASSERT_EQ(0, differences);
109
110 buffer_.reset();
111 }
112
GatherSamples(scoped_ptr<media::AudioBus> bus,int frames)113 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) {
114 if (!buffer_.get())
115 return;
116 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get());
117 buffer_index_ += frames;
118 }
119
120 protected:
IsPlaying()121 bool IsPlaying() {
122 player_->FlushAudioLoopForTesting();
123 return player_->is_playing_;
124 }
125
126 static const int kDefaultFrameCount = 1024;
127 static const int kMaxFrameCount = 1024 * 10;
128
129 scoped_ptr<media::AudioBus> buffer_;
130 int buffer_index_;
131
132 AudioPlayer* player_;
133 base::MessageLoop message_loop_;
134 };
135
TEST_F(AudioPlayerTest,BasicPlayAndStop)136 TEST_F(AudioPlayerTest, BasicPlayAndStop) {
137 CreatePlayer();
138 scoped_refptr<media::AudioBusRefCounted> samples =
139 media::AudioBusRefCounted::Create(1, 7331);
140
141 player_->Play(samples);
142 EXPECT_TRUE(IsPlaying());
143 player_->Stop();
144 EXPECT_FALSE(IsPlaying());
145 player_->Play(samples);
146
147 EXPECT_TRUE(IsPlaying());
148 player_->Stop();
149 EXPECT_FALSE(IsPlaying());
150 player_->Play(samples);
151
152 EXPECT_TRUE(IsPlaying());
153 player_->Stop();
154 EXPECT_FALSE(IsPlaying());
155
156 DeletePlayer();
157 }
158
TEST_F(AudioPlayerTest,OutOfOrderPlayAndStopMultiple)159 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) {
160 CreatePlayer();
161 scoped_refptr<media::AudioBusRefCounted> samples =
162 media::AudioBusRefCounted::Create(1, 1337);
163
164 player_->Stop();
165 player_->Stop();
166 player_->Stop();
167 EXPECT_FALSE(IsPlaying());
168
169 player_->Play(samples);
170 player_->Play(samples);
171 EXPECT_TRUE(IsPlaying());
172
173 player_->Stop();
174 player_->Stop();
175 EXPECT_FALSE(IsPlaying());
176
177 DeletePlayer();
178 }
179
TEST_F(AudioPlayerTest,PlayingEndToEnd)180 TEST_F(AudioPlayerTest, PlayingEndToEnd) {
181 const int kNumSamples = kDefaultFrameCount * 10;
182 CreatePlayer();
183
184 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples));
185
186 DeletePlayer();
187 }
188
189 } // namespace copresence
190