OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/audio_modem/audio_player.h" | 5 #include "components/audio_modem/audio_player.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/run_loop.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/test/test_message_loop.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "components/audio_modem/audio_player_impl.h" | 12 #include "components/audio_modem/audio_player_impl.h" |
15 #include "components/audio_modem/public/audio_modem_types.h" | 13 #include "components/audio_modem/public/audio_modem_types.h" |
16 #include "components/audio_modem/test/random_samples.h" | 14 #include "components/audio_modem/test/random_samples.h" |
| 15 #include "media/audio/audio_manager.h" |
17 #include "media/audio/audio_manager_base.h" | 16 #include "media/audio/audio_manager_base.h" |
18 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 class TestAudioOutputStream : public media::AudioOutputStream { | 22 class TestAudioOutputStream : public media::AudioOutputStream { |
24 public: | 23 public: |
25 using GatherSamplesCallback = | 24 using GatherSamplesCallback = |
26 base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>; | 25 base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 }; | 70 }; |
72 | 71 |
73 } // namespace | 72 } // namespace |
74 | 73 |
75 namespace audio_modem { | 74 namespace audio_modem { |
76 | 75 |
77 class AudioPlayerTest : public testing::Test, | 76 class AudioPlayerTest : public testing::Test, |
78 public base::SupportsWeakPtr<AudioPlayerTest> { | 77 public base::SupportsWeakPtr<AudioPlayerTest> { |
79 public: | 78 public: |
80 AudioPlayerTest() : buffer_index_(0), player_(nullptr) { | 79 AudioPlayerTest() : buffer_index_(0), player_(nullptr) { |
81 audio_manager_ = media::AudioManager::CreateForTesting( | 80 if (!media::AudioManager::Get()) |
82 base::ThreadTaskRunnerHandle::Get()); | 81 media::AudioManager::CreateForTesting(); |
83 base::RunLoop().RunUntilIdle(); | |
84 } | 82 } |
85 | 83 |
86 ~AudioPlayerTest() override { DeletePlayer(); } | 84 ~AudioPlayerTest() override { DeletePlayer(); } |
87 | 85 |
88 void CreatePlayer() { | 86 void CreatePlayer() { |
89 DeletePlayer(); | 87 DeletePlayer(); |
90 player_ = new AudioPlayerImpl(); | 88 player_ = new AudioPlayerImpl(); |
91 player_->set_output_stream_for_testing(new TestAudioOutputStream( | 89 player_->set_output_stream_for_testing(new TestAudioOutputStream( |
92 kDefaultFrameCount, | 90 kDefaultFrameCount, |
93 kMaxFrameCount, | 91 kMaxFrameCount, |
94 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr()))); | 92 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr()))); |
95 player_->Initialize(); | 93 player_->Initialize(); |
96 base::RunLoop().RunUntilIdle(); | |
97 } | 94 } |
98 | 95 |
99 void DeletePlayer() { | 96 void DeletePlayer() { |
100 if (!player_) | 97 if (!player_) |
101 return; | 98 return; |
102 player_->Finalize(); | 99 player_->Finalize(); |
103 player_ = nullptr; | 100 player_ = nullptr; |
104 base::RunLoop().RunUntilIdle(); | |
105 } | 101 } |
106 | 102 |
107 void PlayAndVerifySamples( | 103 void PlayAndVerifySamples( |
108 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 104 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
109 DCHECK_LT(samples->frames(), kMaxFrameCount); | 105 DCHECK_LT(samples->frames(), kMaxFrameCount); |
110 | 106 |
111 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | 107 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); |
112 buffer_index_ = 0; | 108 buffer_index_ = 0; |
113 player_->Play(samples); | 109 player_->Play(samples); |
| 110 player_->FlushAudioLoopForTesting(); |
114 player_->Stop(); | 111 player_->Stop(); |
115 base::RunLoop().RunUntilIdle(); | |
116 | 112 |
117 int differences = 0; | 113 int differences = 0; |
118 for (int i = 0; i < kMaxFrameCount; ++i) { | 114 for (int i = 0; i < kMaxFrameCount; ++i) { |
119 differences += (buffer_->channel(0)[i] != | 115 differences += (buffer_->channel(0)[i] != |
120 samples->channel(0)[i % samples->frames()]); | 116 samples->channel(0)[i % samples->frames()]); |
121 } | 117 } |
122 ASSERT_EQ(0, differences); | 118 ASSERT_EQ(0, differences); |
123 | 119 |
124 buffer_.reset(); | 120 buffer_.reset(); |
125 } | 121 } |
126 | 122 |
127 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { | 123 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { |
128 if (!buffer_.get()) | 124 if (!buffer_.get()) |
129 return; | 125 return; |
130 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | 126 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); |
131 buffer_index_ += frames; | 127 buffer_index_ += frames; |
132 } | 128 } |
133 | 129 |
134 protected: | 130 protected: |
135 bool IsPlaying() { | 131 bool IsPlaying() { |
136 base::RunLoop().RunUntilIdle(); | 132 player_->FlushAudioLoopForTesting(); |
137 return player_->is_playing_; | 133 return player_->is_playing_; |
138 } | 134 } |
139 | 135 |
140 static const int kDefaultFrameCount = 1024; | 136 static const int kDefaultFrameCount = 1024; |
141 static const int kMaxFrameCount = 1024 * 100; | 137 static const int kMaxFrameCount = 1024 * 100; |
142 | 138 |
143 base::TestMessageLoop message_loop_; | |
144 media::ScopedAudioManagerPtr audio_manager_; | |
145 scoped_ptr<media::AudioBus> buffer_; | 139 scoped_ptr<media::AudioBus> buffer_; |
146 int buffer_index_; | 140 int buffer_index_; |
147 | 141 |
148 // Deleted by calling Finalize() on the object. | 142 // Deleted by calling Finalize() on the object. |
149 AudioPlayerImpl* player_; | 143 AudioPlayerImpl* player_; |
| 144 base::MessageLoop message_loop_; |
150 }; | 145 }; |
151 | 146 |
152 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | 147 TEST_F(AudioPlayerTest, BasicPlayAndStop) { |
153 CreatePlayer(); | 148 CreatePlayer(); |
154 scoped_refptr<media::AudioBusRefCounted> samples = | 149 scoped_refptr<media::AudioBusRefCounted> samples = |
155 media::AudioBusRefCounted::Create(1, 7331); | 150 media::AudioBusRefCounted::Create(1, 7331); |
156 | 151 |
157 player_->Play(samples); | 152 player_->Play(samples); |
158 EXPECT_TRUE(IsPlaying()); | 153 EXPECT_TRUE(IsPlaying()); |
| 154 player_->Stop(); |
| 155 EXPECT_FALSE(IsPlaying()); |
| 156 player_->Play(samples); |
159 | 157 |
| 158 EXPECT_TRUE(IsPlaying()); |
| 159 player_->Stop(); |
| 160 EXPECT_FALSE(IsPlaying()); |
| 161 player_->Play(samples); |
| 162 |
| 163 EXPECT_TRUE(IsPlaying()); |
160 player_->Stop(); | 164 player_->Stop(); |
161 EXPECT_FALSE(IsPlaying()); | 165 EXPECT_FALSE(IsPlaying()); |
162 | 166 |
163 player_->Play(samples); | |
164 EXPECT_TRUE(IsPlaying()); | |
165 | |
166 player_->Stop(); | |
167 EXPECT_FALSE(IsPlaying()); | |
168 | |
169 player_->Play(samples); | |
170 EXPECT_TRUE(IsPlaying()); | |
171 | |
172 player_->Stop(); | |
173 EXPECT_FALSE(IsPlaying()); | |
174 | |
175 DeletePlayer(); | 167 DeletePlayer(); |
176 } | 168 } |
177 | 169 |
178 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) { | 170 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) { |
179 CreatePlayer(); | 171 CreatePlayer(); |
180 scoped_refptr<media::AudioBusRefCounted> samples = | 172 scoped_refptr<media::AudioBusRefCounted> samples = |
181 media::AudioBusRefCounted::Create(1, 1337); | 173 media::AudioBusRefCounted::Create(1, 1337); |
182 | 174 |
183 player_->Stop(); | 175 player_->Stop(); |
184 player_->Stop(); | 176 player_->Stop(); |
(...skipping 28 matching lines...) Expand all Loading... |
213 | 205 |
214 PlayAndVerifySamples( | 206 PlayAndVerifySamples( |
215 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); | 207 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); |
216 | 208 |
217 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); | 209 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); |
218 | 210 |
219 DeletePlayer(); | 211 DeletePlayer(); |
220 } | 212 } |
221 | 213 |
222 } // namespace audio_modem | 214 } // namespace audio_modem |
OLD | NEW |