Chromium Code Reviews| 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/public/copresence_constants.h" | |
| 10 #include "components/copresence/test/audio_test_support.h" | |
| 11 #include "media/audio/audio_manager.h" | |
| 12 #include "media/audio/audio_manager_base.h" | |
| 13 #include "media/base/audio_bus.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class TestAudioOutputStream : public media::AudioOutputStream { | |
| 19 public: | |
| 20 typedef base::Callback<void(scoped_ptr<media::AudioBus>, int frames)> | |
| 21 GatherSamplesCallback; | |
| 22 TestAudioOutputStream(int default_frame_count, | |
| 23 int max_frame_count, | |
| 24 GatherSamplesCallback gather_callback) | |
| 25 : default_frame_count_(default_frame_count), | |
| 26 max_frame_count_(max_frame_count), | |
| 27 gather_callback_(gather_callback), | |
| 28 callback_(NULL) { | |
| 29 caller_loop_ = base::MessageLoop::current(); | |
| 30 } | |
| 31 | |
| 32 virtual ~TestAudioOutputStream() {} | |
| 33 | |
| 34 virtual bool Open() OVERRIDE { return true; } | |
| 35 virtual void Start(AudioSourceCallback* callback) OVERRIDE { | |
| 36 callback_ = callback; | |
| 37 GatherPlayedSamples(); | |
| 38 } | |
| 39 virtual void Stop() OVERRIDE {} | |
| 40 virtual void SetVolume(double volume) OVERRIDE {} | |
| 41 virtual void GetVolume(double* volume) OVERRIDE {} | |
| 42 virtual void Close() OVERRIDE {} | |
| 43 | |
| 44 private: | |
| 45 void GatherPlayedSamples() { | |
| 46 int frames = 0, total_frames = 0; | |
| 47 do { | |
| 48 // Call back into the player to get samples that it wants us to play. | |
| 49 scoped_ptr<media::AudioBus> dest = | |
| 50 media::AudioBus::Create(1, default_frame_count_); | |
| 51 frames = callback_->OnMoreData(dest.get(), media::AudioBuffersState()); | |
| 52 total_frames += frames; | |
| 53 // Send the samples given to us by the player to the gather callback. | |
| 54 caller_loop_->PostTask( | |
| 55 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames)); | |
| 56 } while (frames && total_frames < max_frame_count_); | |
| 57 } | |
| 58 | |
| 59 int default_frame_count_; | |
| 60 int max_frame_count_; | |
| 61 GatherSamplesCallback gather_callback_; | |
| 62 AudioSourceCallback* callback_; | |
| 63 base::MessageLoop* caller_loop_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream); | |
| 66 }; | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 namespace copresence { | |
| 71 | |
| 72 class AudioPlayerTest : public testing::Test, | |
| 73 public base::SupportsWeakPtr<AudioPlayerTest> { | |
| 74 public: | |
| 75 AudioPlayerTest() : buffer_index_(0), player_(NULL) { | |
| 76 if (!media::AudioManager::Get()) | |
|
Daniel Erat
2014/07/31 22:31:15
under which circumstances is this created already?
rkc
2014/08/01 21:08:57
This is a global manager, so once we've created it
| |
| 77 media::AudioManager::CreateForTesting(); | |
| 78 } | |
| 79 | |
| 80 virtual ~AudioPlayerTest() { DeletePlayer(); } | |
| 81 | |
| 82 void CreatePlayer() { | |
| 83 DeletePlayer(); | |
| 84 player_ = new AudioPlayer(); | |
| 85 player_->set_output_stream_for_testing(new TestAudioOutputStream( | |
| 86 kDefaultFrameCount, | |
| 87 kMaxFrameCount, | |
| 88 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr()))); | |
| 89 player_->Initialize(); | |
| 90 } | |
| 91 | |
| 92 void DeletePlayer() { | |
| 93 if (!player_) | |
| 94 return; | |
| 95 player_->Finalize(); | |
| 96 player_ = NULL; | |
| 97 } | |
| 98 | |
| 99 void PlayAndVerifySamples( | |
| 100 const scoped_refptr<media::AudioBusRefCounted>& samples) { | |
| 101 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | |
| 102 player_->Play(samples); | |
| 103 player_->FlushAudioLoopForTesting(); | |
| 104 | |
| 105 int differences = 0; | |
| 106 for (int i = 0; i < samples->frames(); ++i) | |
| 107 differences += (buffer_->channel(0)[i] != samples->channel(0)[i]); | |
| 108 ASSERT_EQ(0, differences); | |
| 109 | |
| 110 buffer_.reset(); | |
| 111 } | |
| 112 | |
| 113 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { | |
| 114 if (!buffer_.get()) | |
| 115 return; | |
| 116 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | |
| 117 buffer_index_ += frames; | |
| 118 } | |
| 119 | |
| 120 protected: | |
| 121 bool IsPlaying() { | |
| 122 player_->FlushAudioLoopForTesting(); | |
| 123 return player_->is_playing_; | |
| 124 } | |
| 125 | |
| 126 static const int kDefaultFrameCount = 1024; | |
| 127 static const int kMaxFrameCount = 1024 * 10; | |
| 128 | |
| 129 scoped_ptr<media::AudioBus> buffer_; | |
| 130 int buffer_index_; | |
| 131 | |
| 132 AudioPlayer* player_; | |
| 133 | |
| 134 base::MessageLoop message_loop_; | |
|
Daniel Erat
2014/07/31 22:31:15
create message_loop_ first?
rkc
2014/08/01 21:08:57
I don't understand. This loop will get created bef
| |
| 135 }; | |
| 136 | |
| 137 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | |
| 138 CreatePlayer(); | |
| 139 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 140 media::AudioBusRefCounted::Create(1, 7331); | |
| 141 | |
| 142 player_->Play(samples); | |
| 143 EXPECT_TRUE(IsPlaying()); | |
| 144 player_->Stop(); | |
| 145 EXPECT_FALSE(IsPlaying()); | |
| 146 player_->Play(samples); | |
| 147 | |
| 148 EXPECT_TRUE(IsPlaying()); | |
| 149 player_->Stop(); | |
| 150 EXPECT_FALSE(IsPlaying()); | |
| 151 player_->Play(samples); | |
| 152 | |
| 153 EXPECT_TRUE(IsPlaying()); | |
| 154 player_->Stop(); | |
| 155 EXPECT_FALSE(IsPlaying()); | |
| 156 | |
| 157 DeletePlayer(); | |
| 158 } | |
| 159 | |
| 160 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) { | |
| 161 CreatePlayer(); | |
| 162 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 163 media::AudioBusRefCounted::Create(1, 1337); | |
| 164 | |
| 165 player_->Stop(); | |
| 166 player_->Stop(); | |
| 167 player_->Stop(); | |
| 168 EXPECT_FALSE(IsPlaying()); | |
| 169 | |
| 170 player_->Play(samples); | |
| 171 player_->Play(samples); | |
| 172 EXPECT_TRUE(IsPlaying()); | |
| 173 | |
| 174 player_->Stop(); | |
| 175 player_->Stop(); | |
| 176 EXPECT_FALSE(IsPlaying()); | |
| 177 | |
| 178 DeletePlayer(); | |
| 179 } | |
| 180 | |
| 181 TEST_F(AudioPlayerTest, PlayingEndToEnd) { | |
| 182 const int kNumSamples = kDefaultFrameCount * 10; | |
| 183 CreatePlayer(); | |
| 184 | |
| 185 PlayAndVerifySamples(CreateRandomAudioRefCounted(1, kNumSamples)); | |
| 186 | |
| 187 DeletePlayer(); | |
| 188 } | |
| 189 | |
| 190 } // namespace copresence | |
| OLD | NEW |