| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/copresence/mediums/audio/audio_player.h" | 5 #include "components/copresence/mediums/audio/audio_player.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "components/copresence/mediums/audio/audio_player_impl.h" | 9 #include "components/copresence/mediums/audio/audio_player_impl.h" |
| 10 #include "components/copresence/public/copresence_constants.h" | 10 #include "components/copresence/public/copresence_constants.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 void DeletePlayer() { | 93 void DeletePlayer() { |
| 94 if (!player_) | 94 if (!player_) |
| 95 return; | 95 return; |
| 96 player_->Finalize(); | 96 player_->Finalize(); |
| 97 player_ = nullptr; | 97 player_ = nullptr; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void PlayAndVerifySamples( | 100 void PlayAndVerifySamples( |
| 101 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 101 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
| 102 DCHECK_LT(samples->frames(), kMaxFrameCount); |
| 103 |
| 102 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | 104 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); |
| 105 buffer_index_ = 0; |
| 103 player_->Play(samples); | 106 player_->Play(samples); |
| 104 player_->FlushAudioLoopForTesting(); | 107 player_->FlushAudioLoopForTesting(); |
| 108 player_->Stop(); |
| 105 | 109 |
| 106 int differences = 0; | 110 int differences = 0; |
| 107 for (int i = 0; i < samples->frames(); ++i) | 111 for (int i = 0; i < kMaxFrameCount; ++i) { |
| 108 differences += (buffer_->channel(0)[i] != samples->channel(0)[i]); | 112 differences += (buffer_->channel(0)[i] != |
| 113 samples->channel(0)[i % samples->frames()]); |
| 114 } |
| 109 ASSERT_EQ(0, differences); | 115 ASSERT_EQ(0, differences); |
| 110 | 116 |
| 111 buffer_.reset(); | 117 buffer_.reset(); |
| 112 } | 118 } |
| 113 | 119 |
| 114 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { | 120 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { |
| 115 if (!buffer_.get()) | 121 if (!buffer_.get()) |
| 116 return; | 122 return; |
| 117 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | 123 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); |
| 118 buffer_index_ += frames; | 124 buffer_index_ += frames; |
| 119 } | 125 } |
| 120 | 126 |
| 121 protected: | 127 protected: |
| 122 bool IsPlaying() { | 128 bool IsPlaying() { |
| 123 player_->FlushAudioLoopForTesting(); | 129 player_->FlushAudioLoopForTesting(); |
| 124 return player_->is_playing_; | 130 return player_->is_playing_; |
| 125 } | 131 } |
| 126 | 132 |
| 127 static const int kDefaultFrameCount = 1024; | 133 static const int kDefaultFrameCount = 1024; |
| 128 static const int kMaxFrameCount = 1024 * 10; | 134 static const int kMaxFrameCount = 1024 * 100; |
| 129 | 135 |
| 130 scoped_ptr<media::AudioBus> buffer_; | 136 scoped_ptr<media::AudioBus> buffer_; |
| 131 int buffer_index_; | 137 int buffer_index_; |
| 132 | 138 |
| 133 // Deleted by calling Finalize() on the object. | 139 // Deleted by calling Finalize() on the object. |
| 134 AudioPlayerImpl* player_; | 140 AudioPlayerImpl* player_; |
| 135 base::MessageLoop message_loop_; | 141 base::MessageLoop message_loop_; |
| 136 }; | 142 }; |
| 137 | 143 |
| 138 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | 144 TEST_F(AudioPlayerTest, BasicPlayAndStop) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 EXPECT_TRUE(IsPlaying()); | 179 EXPECT_TRUE(IsPlaying()); |
| 174 | 180 |
| 175 player_->Stop(); | 181 player_->Stop(); |
| 176 player_->Stop(); | 182 player_->Stop(); |
| 177 EXPECT_FALSE(IsPlaying()); | 183 EXPECT_FALSE(IsPlaying()); |
| 178 | 184 |
| 179 DeletePlayer(); | 185 DeletePlayer(); |
| 180 } | 186 } |
| 181 | 187 |
| 182 TEST_F(AudioPlayerTest, PlayingEndToEnd) { | 188 TEST_F(AudioPlayerTest, PlayingEndToEnd) { |
| 183 const int kNumSamples = kDefaultFrameCount * 10; | 189 const int kNumSamples = kDefaultFrameCount * 7 + 321; |
| 184 CreatePlayer(); | 190 CreatePlayer(); |
| 185 | 191 |
| 186 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | 192 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); |
| 187 | 193 |
| 188 DeletePlayer(); | 194 DeletePlayer(); |
| 189 } | 195 } |
| 190 | 196 |
| 197 TEST_F(AudioPlayerTest, PlayingEndToEndRepeated) { |
| 198 const int kNumSamples = kDefaultFrameCount * 7 + 537; |
| 199 CreatePlayer(); |
| 200 |
| 201 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); |
| 202 |
| 203 PlayAndVerifySamples( |
| 204 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); |
| 205 |
| 206 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); |
| 207 |
| 208 DeletePlayer(); |
| 209 } |
| 210 |
| 191 } // namespace copresence | 211 } // namespace copresence |
| OLD | NEW |