Chromium Code Reviews| 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 | |
| 17 using ::testing::_; | |
| 18 using ::testing::DoAll; | |
| 19 using ::testing::InSequence; | |
| 20 using ::testing::Mock; | |
| 21 using ::testing::Return; | |
| 22 using ::testing::SaveArg; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // Input audio format. | |
| 27 const media::AudioParameters::Format kInputFormat = | |
| 28 media::AudioParameters::AUDIO_PCM_LOW_LATENCY; | |
| 29 const int kNumChannels = 1; | |
| 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 EncodedAudioHandlerInterface { | |
| 43 public: | |
| 44 virtual void OnEncodedAudio(const media::AudioParameters& params, | |
| 45 scoped_ptr<std::string> encoded_data, | |
| 46 base::TimeTicks timestamp) = 0; | |
| 47 virtual ~EncodedAudioHandlerInterface() {} | |
| 48 }; | |
| 49 | |
| 50 class AudioTrackRecorderTest : public testing::Test, | |
| 51 public EncodedAudioHandlerInterface { | |
|
mcasas
2015/10/19 20:02:09
Actually you don't need to define this interface/
ajose
2015/10/20 03:21:12
Done.
| |
| 52 public: | |
| 53 AudioTrackRecorderTest() | |
| 54 : params1_(kInputFormat, | |
| 55 media::CHANNEL_LAYOUT_MONO, | |
| 56 kSamplingRate, | |
| 57 kBitsPerSample, | |
| 58 kFramesPerBuffer), | |
| 59 params2_(kInputFormat, | |
| 60 media::CHANNEL_LAYOUT_STEREO, | |
| 61 kSamplingRate, | |
| 62 kBitsPerSample, | |
| 63 kFramesPerBuffer), | |
| 64 source_(kNumChannels, 440, kSamplingRate) { | |
| 65 PrepareBlinkTrackOfType(MEDIA_DEVICE_AUDIO_CAPTURE); | |
| 66 audio_track_recorder_.reset(new AudioTrackRecorder( | |
| 67 blink_track_, base::Bind(&AudioTrackRecorderTest::OnEncodedAudio, | |
| 68 base::Unretained(this)))); | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<media::AudioBus> NextAudioBus(const base::TimeDelta& duration) { | |
| 72 const int num_samples = static_cast<int>((kSamplingRate * duration) / | |
| 73 base::TimeDelta::FromSeconds(1)); | |
| 74 scoped_ptr<media::AudioBus> bus( | |
| 75 media::AudioBus::Create(kNumChannels, num_samples)); | |
| 76 source_.OnMoreData(bus.get(), 0); | |
| 77 return bus.Pass(); | |
| 78 } | |
| 79 | |
| 80 MOCK_METHOD3(DoOnEncodedAudio, | |
| 81 void(const media::AudioParameters& params, | |
| 82 std::string encoded_data, | |
| 83 base::TimeTicks timestamp)); | |
| 84 | |
| 85 void OnEncodedAudio(const media::AudioParameters& params, | |
| 86 scoped_ptr<std::string> encoded_data, | |
| 87 base::TimeTicks timestamp) { | |
| 88 EXPECT_TRUE(!encoded_data->empty()); | |
| 89 DoOnEncodedAudio(params, *encoded_data, timestamp); | |
| 90 } | |
| 91 | |
| 92 const base::MessageLoop message_loop_; | |
| 93 | |
| 94 // ATR and WebMediaStreamTrack for fooling it. | |
| 95 scoped_ptr<AudioTrackRecorder> audio_track_recorder_; | |
| 96 blink::WebMediaStreamTrack blink_track_; | |
| 97 | |
| 98 // Two different sets of AudioParameters for testing re-init of ATR. | |
| 99 media::AudioParameters params1_; | |
| 100 media::AudioParameters params2_; | |
| 101 | |
| 102 // AudioSource for creating AudioBuses. | |
| 103 media::SineWaveAudioSource source_; | |
| 104 | |
| 105 private: | |
| 106 // Prepares a blink track of a given MediaStreamType and attaches the native | |
| 107 // track, which can be used to capture audio data and pass it to the producer. | |
| 108 // Taken from media::SpeechRecognitionAudioSinkTest | |
| 109 void PrepareBlinkTrackOfType(const MediaStreamType device_type) { | |
| 110 StreamDeviceInfo device_info(device_type, "Mock device", "mock_device_id"); | |
| 111 MockMediaConstraintFactory constraint_factory; | |
| 112 const blink::WebMediaConstraints constraints = | |
| 113 constraint_factory.CreateWebMediaConstraints(); | |
| 114 scoped_refptr<WebRtcAudioCapturer> capturer( | |
| 115 WebRtcAudioCapturer::CreateCapturer(-1, device_info, constraints, NULL, | |
| 116 NULL)); | |
| 117 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
| 118 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 119 scoped_ptr<WebRtcLocalAudioTrack> native_track( | |
| 120 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL)); | |
| 121 blink::WebMediaStreamSource blink_audio_source; | |
| 122 blink_audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"), | |
| 123 blink::WebMediaStreamSource::TypeAudio, | |
| 124 base::UTF8ToUTF16("dummy_source_name"), | |
| 125 false /* remote */, true /* readonly */); | |
| 126 MediaStreamSource::SourceStoppedCallback cb; | |
| 127 blink_audio_source.setExtraData( | |
| 128 new MediaStreamAudioSource(-1, device_info, cb, NULL)); | |
| 129 blink_track_.initialize(blink::WebString::fromUTF8("dummy_track"), | |
| 130 blink_audio_source); | |
| 131 blink_track_.setExtraData(native_track.release()); | |
| 132 } | |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(AudioTrackRecorderTest); | |
| 135 }; | |
| 136 | |
| 137 TEST_F(AudioTrackRecorderTest, OnSetFormat) { | |
| 138 audio_track_recorder_->OnSetFormat(params1_); | |
|
mcasas
2015/10/19 20:02:09
What's the point of this?
Suggestion: You can add
ajose
2015/10/20 03:21:12
Acknowledged.
| |
| 139 } | |
| 140 | |
| 141 TEST_F(AudioTrackRecorderTest, OnData) { | |
| 142 audio_track_recorder_->OnSetFormat(params1_); | |
| 143 InSequence s; | |
| 144 base::RunLoop run_loop; | |
| 145 base::Closure quit_closure = run_loop.QuitClosure(); | |
| 146 | |
| 147 // TODO(ajose): consider adding WillOnce(SaveArg...) and inspecting, as done | |
| 148 // in VTR unittests. | |
| 149 // TODO(ajose): Using 10ms chunks due to hard-coded 100fps framerate. | |
| 150 // Need to figure out what to do about framerate. | |
| 151 const base::TimeTicks time1 = base::TimeTicks::Now(); | |
| 152 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, time1)).Times(1); | |
| 153 audio_track_recorder_->OnData( | |
| 154 *NextAudioBus(base::TimeDelta::FromMilliseconds(10)), 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(base::TimeDelta::FromMilliseconds(10)), time2); | |
| 161 | |
| 162 // Send audio with different params to force ATR to re-init. | |
| 163 const base::TimeTicks time3 = base::TimeTicks::Now(); | |
| 164 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)) | |
| 165 .Times(1) | |
| 166 .WillOnce(RunClosure(quit_closure)); | |
| 167 audio_track_recorder_->OnData( | |
| 168 *NextAudioBus(base::TimeDelta::FromMilliseconds(10)), time3); | |
| 169 | |
| 170 run_loop.Run(); | |
| 171 Mock::VerifyAndClearExpectations(this); | |
| 172 } | |
| 173 | |
| 174 } // namespace content | |
| OLD | NEW |