| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/audio_modem/audio_player.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/test/test_message_loop.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | |
| 16 #include "components/audio_modem/audio_player_impl.h" | |
| 17 #include "components/audio_modem/public/audio_modem_types.h" | |
| 18 #include "components/audio_modem/test/random_samples.h" | |
| 19 #include "media/audio/audio_manager_base.h" | |
| 20 #include "media/base/audio_bus.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 class TestAudioOutputStream : public media::AudioOutputStream { | |
| 26 public: | |
| 27 using GatherSamplesCallback = | |
| 28 base::Callback<void(std::unique_ptr<media::AudioBus>, int frames)>; | |
| 29 TestAudioOutputStream(int default_frame_count, | |
| 30 int max_frame_count, | |
| 31 GatherSamplesCallback gather_callback) | |
| 32 : default_frame_count_(default_frame_count), | |
| 33 max_frame_count_(max_frame_count), | |
| 34 gather_callback_(gather_callback), | |
| 35 callback_(nullptr) { | |
| 36 caller_loop_ = base::MessageLoop::current(); | |
| 37 } | |
| 38 | |
| 39 ~TestAudioOutputStream() override {} | |
| 40 | |
| 41 bool Open() override { return true; } | |
| 42 void Start(AudioSourceCallback* callback) override { | |
| 43 callback_ = callback; | |
| 44 GatherPlayedSamples(); | |
| 45 } | |
| 46 void Stop() override {} | |
| 47 void SetVolume(double volume) override {} | |
| 48 void GetVolume(double* volume) override {} | |
| 49 void Close() override {} | |
| 50 | |
| 51 private: | |
| 52 void GatherPlayedSamples() { | |
| 53 int frames = 0, total_frames = 0; | |
| 54 do { | |
| 55 // Call back into the player to get samples that it wants us to play. | |
| 56 std::unique_ptr<media::AudioBus> dest = | |
| 57 media::AudioBus::Create(1, default_frame_count_); | |
| 58 frames = callback_->OnMoreData(dest.get(), 0, 0); | |
| 59 total_frames += frames; | |
| 60 // Send the samples given to us by the player to the gather callback. | |
| 61 caller_loop_->task_runner()->PostTask( | |
| 62 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames)); | |
| 63 } while (frames && total_frames < max_frame_count_); | |
| 64 } | |
| 65 | |
| 66 int default_frame_count_; | |
| 67 int max_frame_count_; | |
| 68 GatherSamplesCallback gather_callback_; | |
| 69 AudioSourceCallback* callback_; | |
| 70 base::MessageLoop* caller_loop_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream); | |
| 73 }; | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 namespace audio_modem { | |
| 78 | |
| 79 class AudioPlayerTest : public testing::Test, | |
| 80 public base::SupportsWeakPtr<AudioPlayerTest> { | |
| 81 public: | |
| 82 AudioPlayerTest() : buffer_index_(0), player_(nullptr) { | |
| 83 audio_manager_ = media::AudioManager::CreateForTesting( | |
| 84 base::ThreadTaskRunnerHandle::Get()); | |
| 85 base::RunLoop().RunUntilIdle(); | |
| 86 } | |
| 87 | |
| 88 ~AudioPlayerTest() override { DeletePlayer(); } | |
| 89 | |
| 90 void CreatePlayer() { | |
| 91 DeletePlayer(); | |
| 92 player_ = new AudioPlayerImpl(); | |
| 93 player_->set_output_stream_for_testing(new TestAudioOutputStream( | |
| 94 kDefaultFrameCount, | |
| 95 kMaxFrameCount, | |
| 96 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr()))); | |
| 97 player_->Initialize(); | |
| 98 base::RunLoop().RunUntilIdle(); | |
| 99 } | |
| 100 | |
| 101 void DeletePlayer() { | |
| 102 if (!player_) | |
| 103 return; | |
| 104 player_->Finalize(); | |
| 105 player_ = nullptr; | |
| 106 base::RunLoop().RunUntilIdle(); | |
| 107 } | |
| 108 | |
| 109 void PlayAndVerifySamples( | |
| 110 const scoped_refptr<media::AudioBusRefCounted>& samples) { | |
| 111 DCHECK_LT(samples->frames(), kMaxFrameCount); | |
| 112 | |
| 113 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | |
| 114 buffer_index_ = 0; | |
| 115 player_->Play(samples); | |
| 116 player_->Stop(); | |
| 117 base::RunLoop().RunUntilIdle(); | |
| 118 | |
| 119 int differences = 0; | |
| 120 for (int i = 0; i < kMaxFrameCount; ++i) { | |
| 121 differences += (buffer_->channel(0)[i] != | |
| 122 samples->channel(0)[i % samples->frames()]); | |
| 123 } | |
| 124 ASSERT_EQ(0, differences); | |
| 125 | |
| 126 buffer_.reset(); | |
| 127 } | |
| 128 | |
| 129 void GatherSamples(std::unique_ptr<media::AudioBus> bus, int frames) { | |
| 130 if (!buffer_.get()) | |
| 131 return; | |
| 132 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | |
| 133 buffer_index_ += frames; | |
| 134 } | |
| 135 | |
| 136 protected: | |
| 137 bool IsPlaying() { | |
| 138 base::RunLoop().RunUntilIdle(); | |
| 139 return player_->is_playing_; | |
| 140 } | |
| 141 | |
| 142 static const int kDefaultFrameCount = 1024; | |
| 143 static const int kMaxFrameCount = 1024 * 100; | |
| 144 | |
| 145 base::TestMessageLoop message_loop_; | |
| 146 media::ScopedAudioManagerPtr audio_manager_; | |
| 147 std::unique_ptr<media::AudioBus> buffer_; | |
| 148 int buffer_index_; | |
| 149 | |
| 150 // Deleted by calling Finalize() on the object. | |
| 151 AudioPlayerImpl* player_; | |
| 152 }; | |
| 153 | |
| 154 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | |
| 155 CreatePlayer(); | |
| 156 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 157 media::AudioBusRefCounted::Create(1, 7331); | |
| 158 | |
| 159 player_->Play(samples); | |
| 160 EXPECT_TRUE(IsPlaying()); | |
| 161 | |
| 162 player_->Stop(); | |
| 163 EXPECT_FALSE(IsPlaying()); | |
| 164 | |
| 165 player_->Play(samples); | |
| 166 EXPECT_TRUE(IsPlaying()); | |
| 167 | |
| 168 player_->Stop(); | |
| 169 EXPECT_FALSE(IsPlaying()); | |
| 170 | |
| 171 player_->Play(samples); | |
| 172 EXPECT_TRUE(IsPlaying()); | |
| 173 | |
| 174 player_->Stop(); | |
| 175 EXPECT_FALSE(IsPlaying()); | |
| 176 | |
| 177 DeletePlayer(); | |
| 178 } | |
| 179 | |
| 180 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) { | |
| 181 CreatePlayer(); | |
| 182 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 183 media::AudioBusRefCounted::Create(1, 1337); | |
| 184 | |
| 185 player_->Stop(); | |
| 186 player_->Stop(); | |
| 187 player_->Stop(); | |
| 188 EXPECT_FALSE(IsPlaying()); | |
| 189 | |
| 190 player_->Play(samples); | |
| 191 player_->Play(samples); | |
| 192 EXPECT_TRUE(IsPlaying()); | |
| 193 | |
| 194 player_->Stop(); | |
| 195 player_->Stop(); | |
| 196 EXPECT_FALSE(IsPlaying()); | |
| 197 | |
| 198 DeletePlayer(); | |
| 199 } | |
| 200 | |
| 201 TEST_F(AudioPlayerTest, PlayingEndToEnd) { | |
| 202 const int kNumSamples = kDefaultFrameCount * 7 + 321; | |
| 203 CreatePlayer(); | |
| 204 | |
| 205 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | |
| 206 | |
| 207 DeletePlayer(); | |
| 208 } | |
| 209 | |
| 210 TEST_F(AudioPlayerTest, PlayingEndToEndRepeated) { | |
| 211 const int kNumSamples = kDefaultFrameCount * 7 + 537; | |
| 212 CreatePlayer(); | |
| 213 | |
| 214 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | |
| 215 | |
| 216 PlayAndVerifySamples( | |
| 217 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); | |
| 218 | |
| 219 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); | |
| 220 | |
| 221 DeletePlayer(); | |
| 222 } | |
| 223 | |
| 224 } // namespace audio_modem | |
| OLD | NEW |