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