| 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 <memory> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/location.h" | 10 #include "base/location.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 11 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 12 #include "base/test/test_message_loop.h" | 14 #include "base/test/test_message_loop.h" |
| 13 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 14 #include "components/audio_modem/audio_player_impl.h" | 16 #include "components/audio_modem/audio_player_impl.h" |
| 15 #include "components/audio_modem/public/audio_modem_types.h" | 17 #include "components/audio_modem/public/audio_modem_types.h" |
| 16 #include "components/audio_modem/test/random_samples.h" | 18 #include "components/audio_modem/test/random_samples.h" |
| 17 #include "media/audio/audio_manager_base.h" | 19 #include "media/audio/audio_manager_base.h" |
| 18 #include "media/base/audio_bus.h" | 20 #include "media/base/audio_bus.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 class TestAudioOutputStream : public media::AudioOutputStream { | 25 class TestAudioOutputStream : public media::AudioOutputStream { |
| 24 public: | 26 public: |
| 25 using GatherSamplesCallback = | 27 using GatherSamplesCallback = |
| 26 base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>; | 28 base::Callback<void(std::unique_ptr<media::AudioBus>, int frames)>; |
| 27 TestAudioOutputStream(int default_frame_count, | 29 TestAudioOutputStream(int default_frame_count, |
| 28 int max_frame_count, | 30 int max_frame_count, |
| 29 GatherSamplesCallback gather_callback) | 31 GatherSamplesCallback gather_callback) |
| 30 : default_frame_count_(default_frame_count), | 32 : default_frame_count_(default_frame_count), |
| 31 max_frame_count_(max_frame_count), | 33 max_frame_count_(max_frame_count), |
| 32 gather_callback_(gather_callback), | 34 gather_callback_(gather_callback), |
| 33 callback_(nullptr) { | 35 callback_(nullptr) { |
| 34 caller_loop_ = base::MessageLoop::current(); | 36 caller_loop_ = base::MessageLoop::current(); |
| 35 } | 37 } |
| 36 | 38 |
| 37 ~TestAudioOutputStream() override {} | 39 ~TestAudioOutputStream() override {} |
| 38 | 40 |
| 39 bool Open() override { return true; } | 41 bool Open() override { return true; } |
| 40 void Start(AudioSourceCallback* callback) override { | 42 void Start(AudioSourceCallback* callback) override { |
| 41 callback_ = callback; | 43 callback_ = callback; |
| 42 GatherPlayedSamples(); | 44 GatherPlayedSamples(); |
| 43 } | 45 } |
| 44 void Stop() override {} | 46 void Stop() override {} |
| 45 void SetVolume(double volume) override {} | 47 void SetVolume(double volume) override {} |
| 46 void GetVolume(double* volume) override {} | 48 void GetVolume(double* volume) override {} |
| 47 void Close() override {} | 49 void Close() override {} |
| 48 | 50 |
| 49 private: | 51 private: |
| 50 void GatherPlayedSamples() { | 52 void GatherPlayedSamples() { |
| 51 int frames = 0, total_frames = 0; | 53 int frames = 0, total_frames = 0; |
| 52 do { | 54 do { |
| 53 // Call back into the player to get samples that it wants us to play. | 55 // Call back into the player to get samples that it wants us to play. |
| 54 scoped_ptr<media::AudioBus> dest = | 56 std::unique_ptr<media::AudioBus> dest = |
| 55 media::AudioBus::Create(1, default_frame_count_); | 57 media::AudioBus::Create(1, default_frame_count_); |
| 56 frames = callback_->OnMoreData(dest.get(), 0, 0); | 58 frames = callback_->OnMoreData(dest.get(), 0, 0); |
| 57 total_frames += frames; | 59 total_frames += frames; |
| 58 // Send the samples given to us by the player to the gather callback. | 60 // Send the samples given to us by the player to the gather callback. |
| 59 caller_loop_->task_runner()->PostTask( | 61 caller_loop_->task_runner()->PostTask( |
| 60 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames)); | 62 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames)); |
| 61 } while (frames && total_frames < max_frame_count_); | 63 } while (frames && total_frames < max_frame_count_); |
| 62 } | 64 } |
| 63 | 65 |
| 64 int default_frame_count_; | 66 int default_frame_count_; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 int differences = 0; | 119 int differences = 0; |
| 118 for (int i = 0; i < kMaxFrameCount; ++i) { | 120 for (int i = 0; i < kMaxFrameCount; ++i) { |
| 119 differences += (buffer_->channel(0)[i] != | 121 differences += (buffer_->channel(0)[i] != |
| 120 samples->channel(0)[i % samples->frames()]); | 122 samples->channel(0)[i % samples->frames()]); |
| 121 } | 123 } |
| 122 ASSERT_EQ(0, differences); | 124 ASSERT_EQ(0, differences); |
| 123 | 125 |
| 124 buffer_.reset(); | 126 buffer_.reset(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { | 129 void GatherSamples(std::unique_ptr<media::AudioBus> bus, int frames) { |
| 128 if (!buffer_.get()) | 130 if (!buffer_.get()) |
| 129 return; | 131 return; |
| 130 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | 132 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); |
| 131 buffer_index_ += frames; | 133 buffer_index_ += frames; |
| 132 } | 134 } |
| 133 | 135 |
| 134 protected: | 136 protected: |
| 135 bool IsPlaying() { | 137 bool IsPlaying() { |
| 136 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
| 137 return player_->is_playing_; | 139 return player_->is_playing_; |
| 138 } | 140 } |
| 139 | 141 |
| 140 static const int kDefaultFrameCount = 1024; | 142 static const int kDefaultFrameCount = 1024; |
| 141 static const int kMaxFrameCount = 1024 * 100; | 143 static const int kMaxFrameCount = 1024 * 100; |
| 142 | 144 |
| 143 base::TestMessageLoop message_loop_; | 145 base::TestMessageLoop message_loop_; |
| 144 media::ScopedAudioManagerPtr audio_manager_; | 146 media::ScopedAudioManagerPtr audio_manager_; |
| 145 scoped_ptr<media::AudioBus> buffer_; | 147 std::unique_ptr<media::AudioBus> buffer_; |
| 146 int buffer_index_; | 148 int buffer_index_; |
| 147 | 149 |
| 148 // Deleted by calling Finalize() on the object. | 150 // Deleted by calling Finalize() on the object. |
| 149 AudioPlayerImpl* player_; | 151 AudioPlayerImpl* player_; |
| 150 }; | 152 }; |
| 151 | 153 |
| 152 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | 154 TEST_F(AudioPlayerTest, BasicPlayAndStop) { |
| 153 CreatePlayer(); | 155 CreatePlayer(); |
| 154 scoped_refptr<media::AudioBusRefCounted> samples = | 156 scoped_refptr<media::AudioBusRefCounted> samples = |
| 155 media::AudioBusRefCounted::Create(1, 7331); | 157 media::AudioBusRefCounted::Create(1, 7331); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 215 |
| 214 PlayAndVerifySamples( | 216 PlayAndVerifySamples( |
| 215 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); | 217 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); |
| 216 | 218 |
| 217 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); | 219 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); |
| 218 | 220 |
| 219 DeletePlayer(); | 221 DeletePlayer(); |
| 220 } | 222 } |
| 221 | 223 |
| 222 } // namespace audio_modem | 224 } // namespace audio_modem |
| OLD | NEW |