Chromium Code Reviews| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); | 102 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); |
| 103 player_->Play(samples); | 103 player_->Play(samples); |
| 104 player_->FlushAudioLoopForTesting(); | 104 player_->FlushAudioLoopForTesting(); |
| 105 | 105 |
| 106 int differences = 0; | 106 int differences = 0; |
| 107 for (int i = 0; i < samples->frames(); ++i) | 107 for (int i = 0; i < kMaxFrameCount; ++i) { |
| 108 differences += (buffer_->channel(0)[i] != samples->channel(0)[i]); | 108 differences += (buffer_->channel(0)[i] != |
| 109 samples->channel(0)[i % samples->frames()]); | |
| 110 } | |
| 109 ASSERT_EQ(0, differences); | 111 ASSERT_EQ(0, differences); |
| 110 | 112 |
| 111 buffer_.reset(); | 113 buffer_.reset(); |
| 112 } | 114 } |
| 113 | 115 |
| 114 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { | 116 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { |
| 115 if (!buffer_.get()) | 117 if (!buffer_.get()) |
| 116 return; | 118 return; |
| 117 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); | 119 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); |
| 118 buffer_index_ += frames; | 120 buffer_index_ += frames; |
| 119 } | 121 } |
| 120 | 122 |
| 121 protected: | 123 protected: |
| 122 bool IsPlaying() { | 124 bool IsPlaying() { |
| 123 player_->FlushAudioLoopForTesting(); | 125 player_->FlushAudioLoopForTesting(); |
| 124 return player_->is_playing_; | 126 return player_->is_playing_; |
| 125 } | 127 } |
| 126 | 128 |
| 127 static const int kDefaultFrameCount = 1024; | 129 static const int kDefaultFrameCount = 1024; |
| 128 static const int kMaxFrameCount = 1024 * 10; | 130 static const int kMaxFrameCount = 1024 * 100; |
| 129 | 131 |
| 130 scoped_ptr<media::AudioBus> buffer_; | 132 scoped_ptr<media::AudioBus> buffer_; |
| 131 int buffer_index_; | 133 int buffer_index_; |
| 132 | 134 |
| 133 // Deleted by calling Finalize() on the object. | 135 // Deleted by calling Finalize() on the object. |
| 134 AudioPlayerImpl* player_; | 136 AudioPlayerImpl* player_; |
| 135 base::MessageLoop message_loop_; | 137 base::MessageLoop message_loop_; |
| 136 }; | 138 }; |
| 137 | 139 |
| 138 TEST_F(AudioPlayerTest, BasicPlayAndStop) { | 140 TEST_F(AudioPlayerTest, BasicPlayAndStop) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 EXPECT_TRUE(IsPlaying()); | 175 EXPECT_TRUE(IsPlaying()); |
| 174 | 176 |
| 175 player_->Stop(); | 177 player_->Stop(); |
| 176 player_->Stop(); | 178 player_->Stop(); |
| 177 EXPECT_FALSE(IsPlaying()); | 179 EXPECT_FALSE(IsPlaying()); |
| 178 | 180 |
| 179 DeletePlayer(); | 181 DeletePlayer(); |
| 180 } | 182 } |
| 181 | 183 |
| 182 TEST_F(AudioPlayerTest, PlayingEndToEnd) { | 184 TEST_F(AudioPlayerTest, PlayingEndToEnd) { |
| 183 const int kNumSamples = kDefaultFrameCount * 10; | 185 // Ensure that kNumsamples is never over kMaxFrameCount. |
|
Charlie
2015/01/29 17:35:03
This and the additional instance below should be D
rkc
2015/01/29 17:42:42
Added the DCHECK to PlayAndVerifySamples instead,
| |
| 186 const int kNumSamples = kDefaultFrameCount * 32; | |
| 184 CreatePlayer(); | 187 CreatePlayer(); |
| 185 | 188 |
| 186 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | 189 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); |
| 190 | |
| 191 DeletePlayer(); | |
| 192 } | |
| 193 | |
| 194 TEST_F(AudioPlayerTest, PlayingEndToEndOddSizes) { | |
| 195 // Ensure that kNumsamples is never over kMaxFrameCount. | |
| 196 const int kNumSamples = kDefaultFrameCount * 7 + 321; | |
| 197 CreatePlayer(); | |
| 198 | |
| 199 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples)); | |
| 187 | 200 |
| 188 DeletePlayer(); | 201 DeletePlayer(); |
| 189 } | 202 } |
| 190 | 203 |
| 191 } // namespace copresence | 204 } // namespace copresence |
| OLD | NEW |