| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/media/audio_track_recorder.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "content/renderer/media/media_stream_audio_source.h" |
| 10 #include "content/renderer/media/mock_media_constraint_factory.h" |
| 11 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
| 12 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 13 #include "media/audio/simple_sources.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/web/WebHeap.h" |
| 17 |
| 18 using ::testing::_; |
| 19 using ::testing::DoAll; |
| 20 using ::testing::InSequence; |
| 21 using ::testing::Mock; |
| 22 using ::testing::Return; |
| 23 using ::testing::SaveArg; |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Input audio format. |
| 28 const media::AudioParameters::Format kInputFormat = |
| 29 media::AudioParameters::AUDIO_PCM_LOW_LATENCY; |
| 30 const int kBitsPerSample = 16; |
| 31 const int kSamplingRate = 48000; |
| 32 const int kFramesPerBuffer = 480; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 namespace content { |
| 37 |
| 38 ACTION_P(RunClosure, closure) { |
| 39 closure.Run(); |
| 40 } |
| 41 |
| 42 class AudioTrackRecorderTest : public testing::Test { |
| 43 public: |
| 44 AudioTrackRecorderTest() |
| 45 : params1_(kInputFormat, |
| 46 media::CHANNEL_LAYOUT_MONO, |
| 47 kSamplingRate, |
| 48 kBitsPerSample, |
| 49 kFramesPerBuffer), |
| 50 params2_(kInputFormat, |
| 51 media::CHANNEL_LAYOUT_STEREO, |
| 52 kSamplingRate, |
| 53 kBitsPerSample, |
| 54 kFramesPerBuffer), |
| 55 mono_source_(1 /* # channels */, 440, kSamplingRate), |
| 56 stereo_source_(2 /* # channels */, 440, kSamplingRate) { |
| 57 PrepareBlinkTrack(); |
| 58 audio_track_recorder_.reset(new AudioTrackRecorder( |
| 59 blink_track_, base::Bind(&AudioTrackRecorderTest::OnEncodedAudio, |
| 60 base::Unretained(this)))); |
| 61 } |
| 62 |
| 63 ~AudioTrackRecorderTest() { |
| 64 audio_track_recorder_.reset(); |
| 65 blink_track_.reset(); |
| 66 blink::WebHeap::collectAllGarbageForTesting(); |
| 67 } |
| 68 |
| 69 scoped_ptr<media::AudioBus> NextAudioBus(int num_channels, |
| 70 const base::TimeDelta& duration) { |
| 71 // Only supports up to two channels for now. |
| 72 EXPECT_TRUE(num_channels == 1 || num_channels == 2); |
| 73 const int num_samples = static_cast<int>((kSamplingRate * duration) / |
| 74 base::TimeDelta::FromSeconds(1)); |
| 75 scoped_ptr<media::AudioBus> bus( |
| 76 media::AudioBus::Create(num_channels, num_samples)); |
| 77 if (num_channels == 1) { |
| 78 mono_source_.OnMoreData(bus.get(), 0); |
| 79 } else { |
| 80 // two channels |
| 81 stereo_source_.OnMoreData(bus.get(), 0); |
| 82 } |
| 83 return bus.Pass(); |
| 84 } |
| 85 |
| 86 MOCK_METHOD3(DoOnEncodedAudio, |
| 87 void(const media::AudioParameters& params, |
| 88 std::string encoded_data, |
| 89 base::TimeTicks timestamp)); |
| 90 |
| 91 void OnEncodedAudio(const media::AudioParameters& params, |
| 92 scoped_ptr<std::string> encoded_data, |
| 93 base::TimeTicks timestamp) { |
| 94 EXPECT_TRUE(!encoded_data->empty()); |
| 95 DoOnEncodedAudio(params, *encoded_data, timestamp); |
| 96 } |
| 97 |
| 98 const base::MessageLoop message_loop_; |
| 99 |
| 100 // ATR and WebMediaStreamTrack for fooling it. |
| 101 scoped_ptr<AudioTrackRecorder> audio_track_recorder_; |
| 102 blink::WebMediaStreamTrack blink_track_; |
| 103 |
| 104 // Two different sets of AudioParameters for testing re-init of ATR. |
| 105 media::AudioParameters params1_; |
| 106 media::AudioParameters params2_; |
| 107 |
| 108 // AudioSources for creating AudioBuses. |
| 109 media::SineWaveAudioSource mono_source_; |
| 110 media::SineWaveAudioSource stereo_source_; |
| 111 |
| 112 private: |
| 113 // Prepares a blink track of a given MediaStreamType and attaches the native |
| 114 // track, which can be used to capture audio data and pass it to the producer. |
| 115 // Adapted from media::WebRTCLocalAudioSourceProviderTest. |
| 116 void PrepareBlinkTrack() { |
| 117 MockMediaConstraintFactory constraint_factory; |
| 118 scoped_refptr<WebRtcAudioCapturer> capturer( |
| 119 WebRtcAudioCapturer::CreateCapturer( |
| 120 -1, StreamDeviceInfo(), |
| 121 constraint_factory.CreateWebMediaConstraints(), NULL, NULL)); |
| 122 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
| 123 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 124 scoped_ptr<WebRtcLocalAudioTrack> native_track( |
| 125 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL)); |
| 126 blink::WebMediaStreamSource audio_source; |
| 127 audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"), |
| 128 blink::WebMediaStreamSource::TypeAudio, |
| 129 base::UTF8ToUTF16("dummy_source_name"), |
| 130 false /* remote */, true /* readonly */); |
| 131 blink_track_.initialize(blink::WebString::fromUTF8("audio_track"), |
| 132 audio_source); |
| 133 blink_track_.setExtraData(native_track.release()); |
| 134 } |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(AudioTrackRecorderTest); |
| 137 }; |
| 138 |
| 139 TEST_F(AudioTrackRecorderTest, OnData) { |
| 140 InSequence s; |
| 141 base::RunLoop run_loop; |
| 142 base::Closure quit_closure = run_loop.QuitClosure(); |
| 143 |
| 144 // Give ATR initial audio parameters. |
| 145 audio_track_recorder_->OnSetFormat(params1_); |
| 146 // TODO(ajose): consider adding WillOnce(SaveArg...) and inspecting, as done |
| 147 // in VTR unittests. http://crbug.com/548856 |
| 148 // TODO(ajose): Using 10ms chunks due to hard-coded 100fps framerate. |
| 149 // Need to figure out what to do about framerate. |
| 150 const base::TimeTicks time1 = base::TimeTicks::Now(); |
| 151 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, time1)).Times(1); |
| 152 audio_track_recorder_->OnData( |
| 153 *NextAudioBus(params1_.channels(), base::TimeDelta::FromMilliseconds(10)), |
| 154 time1); |
| 155 |
| 156 // Send more audio. |
| 157 const base::TimeTicks time2 = base::TimeTicks::Now(); |
| 158 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(1); |
| 159 audio_track_recorder_->OnData( |
| 160 *NextAudioBus(params1_.channels(), base::TimeDelta::FromMilliseconds(10)), |
| 161 time2); |
| 162 |
| 163 // Give ATR new audio parameters. |
| 164 audio_track_recorder_->OnSetFormat(params2_); |
| 165 // Send audio with different params. |
| 166 const base::TimeTicks time3 = base::TimeTicks::Now(); |
| 167 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) |
| 168 .Times(1) |
| 169 .WillOnce(RunClosure(quit_closure)); |
| 170 audio_track_recorder_->OnData( |
| 171 *NextAudioBus(params2_.channels(), base::TimeDelta::FromMilliseconds(10)), |
| 172 time3); |
| 173 |
| 174 run_loop.Run(); |
| 175 Mock::VerifyAndClearExpectations(this); |
| 176 } |
| 177 |
| 178 } // namespace content |
| OLD | NEW |