Chromium Code Reviews| 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 "content/renderer/media/audio_track_recorder.h" | 5 #include "content/renderer/media/audio_track_recorder.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/renderer/media/media_stream_audio_source.h" | 13 #include "content/renderer/media/media_stream_audio_source.h" |
| 14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | 14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
| 15 #include "content/renderer/media/webrtc_local_audio_track.h" | 15 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 16 #include "media/audio/simple_sources.h" | 16 #include "media/audio/simple_sources.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/WebKit/public/web/WebHeap.h" | 19 #include "third_party/WebKit/public/web/WebHeap.h" |
| 20 #include "third_party/opus/src/include/opus.h" | 20 #include "third_party/opus/src/include/opus.h" |
| 21 | 21 |
| 22 using ::testing::_; | 22 using ::testing::_; |
| 23 using ::testing::DoAll; | 23 using ::testing::DoAll; |
| 24 using ::testing::InSequence; | 24 using ::testing::InSequence; |
| 25 using ::testing::Mock; | 25 using ::testing::Mock; |
| 26 using ::testing::Return; | 26 using ::testing::Return; |
| 27 using ::testing::SaveArg; | 27 using ::testing::SaveArg; |
| 28 using ::testing::TestWithParam; | 28 using ::testing::TestWithParam; |
| 29 using ::testing::ValuesIn; | 29 using ::testing::ValuesIn; |
| 30 using base::TimeTicks; | |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 const int kDefaultBitsPerSample = 16; | 34 const int kDefaultBitsPerSample = 16; |
| 34 const int kDefaultSampleRate = 48000; | 35 const int kDefaultSampleRate = 48000; |
| 35 // The |frames_per_buffer| field of AudioParameters is not used by ATR. | 36 // The |frames_per_buffer| field of AudioParameters is not used by ATR. |
| 36 const int kIgnoreFramesPerBuffer = 1; | 37 const int kIgnoreFramesPerBuffer = 1; |
| 37 | 38 |
| 38 // The following parameters replicate those in audio_track_recorder.cc, see this | 39 // The following parameters replicate those in audio_track_recorder.cc, see this |
| 39 // file for explanations. | 40 // file for explanations. |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 second_params_.channels(), second_params_.sample_rate() * | 162 second_params_.channels(), second_params_.sample_rate() * |
| 162 kMediaStreamAudioTrackBufferDurationMs / | 163 kMediaStreamAudioTrackBufferDurationMs / |
| 163 base::Time::kMillisecondsPerSecond)); | 164 base::Time::kMillisecondsPerSecond)); |
| 164 second_source_.OnMoreData(bus.get(), 0, 0); | 165 second_source_.OnMoreData(bus.get(), 0, 0); |
| 165 return bus; | 166 return bus; |
| 166 } | 167 } |
| 167 | 168 |
| 168 MOCK_METHOD3(DoOnEncodedAudio, | 169 MOCK_METHOD3(DoOnEncodedAudio, |
| 169 void(const media::AudioParameters& params, | 170 void(const media::AudioParameters& params, |
| 170 std::string encoded_data, | 171 std::string encoded_data, |
| 171 base::TimeTicks timestamp)); | 172 TimeTicks timestamp)); |
| 172 | 173 |
| 173 void OnEncodedAudio(const media::AudioParameters& params, | 174 void OnEncodedAudio(const media::AudioParameters& params, |
| 174 scoped_ptr<std::string> encoded_data, | 175 scoped_ptr<std::string> encoded_data, |
| 175 base::TimeTicks timestamp) { | 176 TimeTicks timestamp) { |
| 176 EXPECT_TRUE(!encoded_data->empty()); | 177 EXPECT_TRUE(!encoded_data->empty()); |
| 177 // Decode |encoded_data| and check we get the expected number of frames | 178 // Decode |encoded_data| and check we get the expected number of frames |
| 178 // per buffer. | 179 // per buffer. |
| 179 EXPECT_EQ(kDefaultSampleRate * kOpusBufferDurationMs / 1000, | 180 EXPECT_EQ(kDefaultSampleRate * kOpusBufferDurationMs / 1000, |
| 180 opus_decode_float( | 181 opus_decode_float( |
| 181 opus_decoder_, reinterpret_cast<uint8_t*>( | 182 opus_decoder_, reinterpret_cast<uint8_t*>( |
| 182 string_as_array(encoded_data.get())), | 183 string_as_array(encoded_data.get())), |
| 183 encoded_data->size(), buffer_.get(), kFramesPerBuffer, 0)); | 184 encoded_data->size(), buffer_.get(), kFramesPerBuffer, 0)); |
| 184 | 185 |
| 185 DoOnEncodedAudio(params, *encoded_data, timestamp); | 186 DoOnEncodedAudio(params, *encoded_data, timestamp); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 InSequence s; | 230 InSequence s; |
| 230 base::RunLoop run_loop; | 231 base::RunLoop run_loop; |
| 231 base::Closure quit_closure = run_loop.QuitClosure(); | 232 base::Closure quit_closure = run_loop.QuitClosure(); |
| 232 | 233 |
| 233 // Give ATR initial audio parameters. | 234 // Give ATR initial audio parameters. |
| 234 audio_track_recorder_->OnSetFormat(first_params_); | 235 audio_track_recorder_->OnSetFormat(first_params_); |
| 235 | 236 |
| 236 // TODO(ajose): consider adding WillOnce(SaveArg...) and inspecting, as done | 237 // TODO(ajose): consider adding WillOnce(SaveArg...) and inspecting, as done |
| 237 // in VTR unittests. http://crbug.com/548856 | 238 // in VTR unittests. http://crbug.com/548856 |
| 238 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(1); | 239 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(1); |
| 239 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), | 240 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); |
| 240 base::TimeTicks::Now()); | 241 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) |
| 241 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) { | 242 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); |
| 242 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), | |
| 243 base::TimeTicks::Now()); | |
| 244 } | |
| 245 | 243 |
| 246 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) | 244 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) |
| 247 .Times(1) | 245 .Times(1) |
| 248 // Only reset the decoder once we've heard back: | 246 // Only reset the decoder once we've heard back: |
| 249 .WillOnce(RunClosure(base::Bind(&AudioTrackRecorderTest::ResetDecoder, | 247 .WillOnce(RunClosure(base::Bind(&AudioTrackRecorderTest::ResetDecoder, |
| 250 base::Unretained(this), second_params_))); | 248 base::Unretained(this), second_params_))); |
| 251 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), | 249 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); |
| 252 base::TimeTicks::Now()); | 250 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) |
| 253 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) { | 251 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); |
| 254 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), | |
| 255 base::TimeTicks::Now()); | |
| 256 } | |
| 257 | 252 |
| 258 // If the amount of samples/10ms buffer is not an integer (e.g. 22050Hz) we | 253 // If the amount of samples/10ms buffer is not an integer (e.g. 22050Hz) we |
| 259 // need an extra OnData() to account for the round-off error. | 254 // need an extra OnData() to account for the round-off error. |
| 260 if (GetParam().sample_rate % 100) { | 255 if (GetParam().sample_rate % 100) |
| 261 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), | 256 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); |
| 262 base::TimeTicks::Now()); | |
| 263 } | |
| 264 | 257 |
| 265 // Give ATR new audio parameters. | 258 // Give ATR new audio parameters. |
| 266 audio_track_recorder_->OnSetFormat(second_params_); | 259 audio_track_recorder_->OnSetFormat(second_params_); |
| 267 | 260 |
| 268 // Send audio with different params. | 261 // Send audio with different params. |
| 269 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) | 262 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) |
| 270 .Times(1) | 263 .Times(1) |
| 271 .WillOnce(RunClosure(quit_closure)); | 264 .WillOnce(RunClosure(quit_closure)); |
| 272 audio_track_recorder_->OnData(*GetSecondSourceAudioBus(), | 265 audio_track_recorder_->OnData(*GetSecondSourceAudioBus(), TimeTicks::Now()); |
| 273 base::TimeTicks::Now()); | 266 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) |
| 274 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) { | 267 audio_track_recorder_->OnData(*GetSecondSourceAudioBus(), TimeTicks::Now()); |
| 275 audio_track_recorder_->OnData(*GetSecondSourceAudioBus(), | |
| 276 base::TimeTicks::Now()); | |
| 277 } | |
| 278 | 268 |
| 279 run_loop.Run(); | 269 run_loop.Run(); |
| 280 Mock::VerifyAndClearExpectations(this); | 270 Mock::VerifyAndClearExpectations(this); |
| 281 } | 271 } |
| 282 | 272 |
| 283 INSTANTIATE_TEST_CASE_P(, AudioTrackRecorderTest, ValuesIn(kATRTestParams)); | 273 INSTANTIATE_TEST_CASE_P(, AudioTrackRecorderTest, ValuesIn(kATRTestParams)); |
|
emircan
2016/03/22 18:09:30
Move this after l.302.
mcasas
2016/03/22 20:21:02
Done.
| |
| 284 | 274 |
| 275 TEST_P(AudioTrackRecorderTest, PauseResume) { | |
| 276 InSequence s; | |
| 277 base::RunLoop run_loop; | |
| 278 base::Closure quit_closure = run_loop.QuitClosure(); | |
| 279 | |
| 280 // Give ATR initial audio parameters. | |
| 281 audio_track_recorder_->OnSetFormat(first_params_); | |
| 282 | |
| 283 audio_track_recorder_->Pause(); | |
| 284 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(0); | |
| 285 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); | |
| 286 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) | |
| 287 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); | |
| 288 | |
| 289 audio_track_recorder_->Resume(); | |
| 290 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) | |
| 291 .Times(1) | |
| 292 .WillOnce(RunClosure(quit_closure)); | |
| 293 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); | |
| 294 for (int i = 0; i < kRatioInputToOutputFrames - 1; ++i) | |
| 295 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); | |
| 296 | |
| 297 if (GetParam().sample_rate % 100) | |
| 298 audio_track_recorder_->OnData(*GetFirstSourceAudioBus(), TimeTicks::Now()); | |
| 299 | |
| 300 run_loop.Run(); | |
| 301 Mock::VerifyAndClearExpectations(this); | |
| 302 } | |
| 303 | |
| 285 } // namespace content | 304 } // namespace content |
| OLD | NEW |