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 <cstdlib> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "components/copresence/common/copresence_constants.h" | |
| 12 #include "media/audio/audio_manager.h" | |
| 13 #include "media/audio/audio_manager_base.h" | |
| 14 #include "media/base/audio_bus.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class TestAudioOutputStream : public media::AudioOutputStream { | |
| 20 public: | |
| 21 typedef base::Callback<void(scoped_ptr<media::AudioBus>, int frames)> | |
| 22 GatherSamplesCallback; | |
| 23 TestAudioOutputStream(int default_frame_count, | |
| 24 int max_frame_count, | |
| 25 GatherSamplesCallback gather_callback) | |
| 26 : default_frame_count_(default_frame_count), | |
| 27 max_frame_count_(max_frame_count), | |
| 28 gather_callback_(gather_callback), | |
| 29 callback_(NULL) { | |
| 30 caller_loop_ = base::MessageLoop::current(); | |
| 31 } | |
| 32 | |
| 33 virtual ~TestAudioOutputStream() {} | |
| 34 | |
| 35 virtual bool Open() OVERRIDE { return true; } | |
| 36 virtual void Start(AudioSourceCallback* callback) OVERRIDE { | |
| 37 callback_ = callback; | |
| 38 GatherPlayedSamples(); | |
| 39 } | |
| 40 virtual void Stop() OVERRIDE {} | |
| 41 virtual void SetVolume(double volume) OVERRIDE {} | |
| 42 virtual void GetVolume(double* volume) OVERRIDE {} | |
| 43 virtual void Close() OVERRIDE {} | |
| 44 | |
| 45 private: | |
| 46 void GatherPlayedSamples() { | |
| 47 int frames = 0, total_frames = 0; | |
| 48 do { | |
| 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; | |
|
Daniel Erat
2014/07/28 21:18:18
does this mean that total_frames can exceed max_fr
rkc
2014/07/29 00:33:35
If total_frames equals the max frame count, we bre
| |
| 53 caller_loop_->PostTask( | |
| 54 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames)); | |
| 55 } while (frames && total_frames < max_frame_count_); | |
| 56 } | |
| 57 | |
| 58 int default_frame_count_; | |
| 59 int max_frame_count_; | |
| 60 GatherSamplesCallback gather_callback_; | |
| 61 AudioSourceCallback* callback_; | |
| 62 base::MessageLoop* caller_loop_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream); | |
| 65 }; | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 namespace copresence { | |
| 70 | |
| 71 class AudioPlayerTest : public testing::Test, | |
| 72 public base::SupportsWeakPtr<AudioPlayerTest> { | |
| 73 public: | |
| 74 AudioPlayerTest() : buffer_index_(0), player_(NULL) { | |
| 75 if (!media::AudioManager::Get()) | |
| 76 media::AudioManager::CreateForTesting(); | |
| 77 } | |
| 78 | |
| 79 virtual ~AudioPlayerTest() { DeletePlayer(); } | |
| 80 | |
| 81 void CreatePlayer() { | |
| 82 DeletePlayer(); | |
| 83 player_ = new AudioPlayer(new TestAudioOutputStream( | |
| 84 kDefaultFrameCount, | |
| 85 kMaxFrameCount, | |
| 86 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr()))); | |
| 87 } | |
| 88 | |
| 89 void DeletePlayer() { | |
| 90 if (!player_) | |
| 91 return; | |
| 92 player_->Finalize(); | |
| 93 player_ = NULL; | |
| 94 } | |
| 95 | |
| 96 void PlayAndVerifySamples( | |
| 97 const scoped_refptr<media::AudioBusRefCounted>& samples) { | |
| 98 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | |
| 99 player_->Play(samples); | |
| 100 player_->FlushAudioLoop(); | |
| 101 | |
| 102 int differences = 0; | |
| 103 for (int i = 0; i < samples->frames(); ++i) | |
| 104 differences += (buffer_->channel(0)[i] != samples->channel(0)[i]); | |
| 105 ASSERT_EQ(0, differences); | |
|
Daniel Erat
2014/07/28 21:18:18
is it worthwhile logging the indices of the frames
rkc
2014/07/29 00:33:35
Not really. If we have any differences at all, thi
| |
| 106 | |
| 107 buffer_.reset(); | |
| 108 } | |
| 109 | |
| 110 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { | |
| 111 if (!buffer_.get()) | |
| 112 return; | |
| 113 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | |
| 114 buffer_index_ += frames; | |
| 115 } | |
| 116 | |
| 117 protected: | |
| 118 // Populate random samples given a random seed into the samples array. | |
| 119 void PopulateSamples(int random_seed, size_t size, float* samples) { | |
|
Daniel Erat
2014/07/28 21:18:18
this seems like it's duplicated across multiple fi
rkc
2014/07/29 00:33:35
Done.
| |
| 120 srand(random_seed); | |
| 121 for (size_t i = 0; i < size; ++i) | |
| 122 samples[i] = (2.0 * rand() / RAND_MAX) - 1; | |
| 123 } | |
| 124 | |
| 125 static const int kDefaultFrameCount = 1024; | |
| 126 static const int kMaxFrameCount = 1024 * 10; | |
| 127 | |
| 128 scoped_ptr<media::AudioBus> buffer_; | |
| 129 int buffer_index_; | |
| 130 | |
| 131 AudioPlayer* player_; | |
| 132 | |
| 133 base::MessageLoop message_loop_; | |
| 134 }; | |
| 135 | |
| 136 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | |
| 137 CreatePlayer(); | |
| 138 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 139 media::AudioBusRefCounted::Create(1, 7331); | |
| 140 | |
| 141 player_->Play(samples); | |
| 142 EXPECT_TRUE(player_->IsPlaying()); | |
| 143 player_->Stop(); | |
| 144 EXPECT_FALSE(player_->IsPlaying()); | |
| 145 player_->Play(samples); | |
| 146 | |
| 147 EXPECT_TRUE(player_->IsPlaying()); | |
| 148 player_->Stop(); | |
| 149 EXPECT_FALSE(player_->IsPlaying()); | |
| 150 player_->Play(samples); | |
| 151 | |
| 152 EXPECT_TRUE(player_->IsPlaying()); | |
| 153 player_->Stop(); | |
| 154 EXPECT_FALSE(player_->IsPlaying()); | |
| 155 | |
| 156 DeletePlayer(); | |
| 157 } | |
| 158 | |
| 159 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) { | |
| 160 CreatePlayer(); | |
| 161 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 162 media::AudioBusRefCounted::Create(1, 1337); | |
| 163 | |
| 164 player_->Stop(); | |
| 165 player_->Stop(); | |
| 166 player_->Stop(); | |
| 167 EXPECT_FALSE(player_->IsPlaying()); | |
| 168 | |
| 169 player_->Play(samples); | |
| 170 player_->Play(samples); | |
| 171 EXPECT_TRUE(player_->IsPlaying()); | |
| 172 | |
| 173 player_->Stop(); | |
| 174 player_->Stop(); | |
| 175 EXPECT_FALSE(player_->IsPlaying()); | |
| 176 | |
| 177 DeletePlayer(); | |
| 178 } | |
| 179 | |
| 180 TEST_F(AudioPlayerTest, RecordingEndToEnd) { | |
| 181 const int kNumSamples = kDefaultFrameCount * 10; | |
| 182 CreatePlayer(); | |
| 183 | |
| 184 scoped_refptr<media::AudioBusRefCounted> samples = | |
| 185 media::AudioBusRefCounted::Create(1, kNumSamples); | |
| 186 PopulateSamples(0x1337, kNumSamples, samples->channel(0)); | |
| 187 | |
| 188 PlayAndVerifySamples(samples); | |
| 189 | |
| 190 DeletePlayer(); | |
| 191 } | |
| 192 | |
| 193 } // namespace copresence | |
| OLD | NEW |