OLD | NEW |
| (Empty) |
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/mediums/audio/audio_player_impl.h" | |
10 #include "components/copresence/public/copresence_constants.h" | |
11 #include "components/copresence/test/audio_test_support.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" | |
16 | |
17 namespace { | |
18 | |
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(); | |
31 } | |
32 | |
33 ~TestAudioOutputStream() override {} | |
34 | |
35 bool Open() override { return true; } | |
36 void Start(AudioSourceCallback* callback) override { | |
37 callback_ = callback; | |
38 GatherPlayedSamples(); | |
39 } | |
40 void Stop() override {} | |
41 void SetVolume(double volume) override {} | |
42 void GetVolume(double* volume) override {} | |
43 void Close() override {} | |
44 | |
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_); | |
58 } | |
59 | |
60 int default_frame_count_; | |
61 int max_frame_count_; | |
62 GatherSamplesCallback gather_callback_; | |
63 AudioSourceCallback* callback_; | |
64 base::MessageLoop* caller_loop_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream); | |
67 }; | |
68 | |
69 } // namespace | |
70 | |
71 namespace copresence { | |
72 | |
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(); | |
79 } | |
80 | |
81 ~AudioPlayerTest() override { DeletePlayer(); } | |
82 | |
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(); | |
91 } | |
92 | |
93 void DeletePlayer() { | |
94 if (!player_) | |
95 return; | |
96 player_->Finalize(); | |
97 player_ = nullptr; | |
98 } | |
99 | |
100 void PlayAndVerifySamples( | |
101 const scoped_refptr<media::AudioBusRefCounted>& samples) { | |
102 DCHECK_LT(samples->frames(), kMaxFrameCount); | |
103 | |
104 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | |
105 buffer_index_ = 0; | |
106 player_->Play(samples); | |
107 player_->FlushAudioLoopForTesting(); | |
108 player_->Stop(); | |
109 | |
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()]); | |
114 } | |
115 ASSERT_EQ(0, differences); | |
116 | |
117 buffer_.reset(); | |
118 } | |
119 | |
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; | |
125 } | |
126 | |
127 protected: | |
128 bool IsPlaying() { | |
129 player_->FlushAudioLoopForTesting(); | |
130 return player_->is_playing_; | |
131 } | |
132 | |
133 static const int kDefaultFrameCount = 1024; | |
134 static const int kMaxFrameCount = 1024 * 100; | |
135 | |
136 scoped_ptr<media::AudioBus> buffer_; | |
137 int buffer_index_; | |
138 | |
139 // Deleted by calling Finalize() on the object. | |
140 AudioPlayerImpl* player_; | |
141 base::MessageLoop message_loop_; | |
142 }; | |
143 | |
144 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | |
145 CreatePlayer(); | |
146 scoped_refptr<media::AudioBusRefCounted> samples = | |
147 media::AudioBusRefCounted::Create(1, 7331); | |
148 | |
149 player_->Play(samples); | |
150 EXPECT_TRUE(IsPlaying()); | |
151 player_->Stop(); | |
152 EXPECT_FALSE(IsPlaying()); | |
153 player_->Play(samples); | |
154 | |
155 EXPECT_TRUE(IsPlaying()); | |
156 player_->Stop(); | |
157 EXPECT_FALSE(IsPlaying()); | |
158 player_->Play(samples); | |
159 | |
160 EXPECT_TRUE(IsPlaying()); | |
161 player_->Stop(); | |
162 EXPECT_FALSE(IsPlaying()); | |
163 | |
164 DeletePlayer(); | |
165 } | |
166 | |
167 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) { | |
168 CreatePlayer(); | |
169 scoped_refptr<media::AudioBusRefCounted> samples = | |
170 media::AudioBusRefCounted::Create(1, 1337); | |
171 | |
172 player_->Stop(); | |
173 player_->Stop(); | |
174 player_->Stop(); | |
175 EXPECT_FALSE(IsPlaying()); | |
176 | |
177 player_->Play(samples); | |
178 player_->Play(samples); | |
179 EXPECT_TRUE(IsPlaying()); | |
180 | |
181 player_->Stop(); | |
182 player_->Stop(); | |
183 EXPECT_FALSE(IsPlaying()); | |
184 | |
185 DeletePlayer(); | |
186 } | |
187 | |
188 TEST_F(AudioPlayerTest, PlayingEndToEnd) { | |
189 const int kNumSamples = kDefaultFrameCount * 7 + 321; | |
190 CreatePlayer(); | |
191 | |
192 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | |
193 | |
194 DeletePlayer(); | |
195 } | |
196 | |
197 TEST_F(AudioPlayerTest, PlayingEndToEndRepeated) { | |
198 const int kNumSamples = kDefaultFrameCount * 7 + 537; | |
199 CreatePlayer(); | |
200 | |
201 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | |
202 | |
203 PlayAndVerifySamples( | |
204 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); | |
205 | |
206 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); | |
207 | |
208 DeletePlayer(); | |
209 } | |
210 | |
211 } // namespace copresence | |
OLD | NEW |