| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "base/logging.h" | |
| 6 #include "build/build_config.h" | |
| 7 #include "content/public/renderer/media_stream_audio_sink.h" | |
| 8 #include "content/renderer/media/mock_constraint_factory.h" | |
| 9 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | |
| 10 #include "content/renderer/media/webrtc_audio_capturer.h" | |
| 11 #include "content/renderer/media/webrtc_local_audio_track.h" | |
| 12 #include "media/base/audio_bus.h" | |
| 13 #include "media/base/audio_parameters.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
| 17 | |
| 18 using ::testing::_; | |
| 19 using ::testing::AtLeast; | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 class MockCapturerSource : public media::AudioCapturerSource { | |
| 26 public: | |
| 27 MockCapturerSource() {} | |
| 28 MOCK_METHOD3(Initialize, void(const media::AudioParameters& params, | |
| 29 CaptureCallback* callback, | |
| 30 int session_id)); | |
| 31 MOCK_METHOD0(Start, void()); | |
| 32 MOCK_METHOD0(Stop, void()); | |
| 33 MOCK_METHOD1(SetVolume, void(double volume)); | |
| 34 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable)); | |
| 35 | |
| 36 protected: | |
| 37 ~MockCapturerSource() override {} | |
| 38 }; | |
| 39 | |
| 40 class MockMediaStreamAudioSink : public MediaStreamAudioSink { | |
| 41 public: | |
| 42 MockMediaStreamAudioSink() {} | |
| 43 ~MockMediaStreamAudioSink() override {} | |
| 44 void OnData(const media::AudioBus& audio_bus, | |
| 45 base::TimeTicks estimated_capture_time) override { | |
| 46 EXPECT_EQ(audio_bus.channels(), params_.channels()); | |
| 47 EXPECT_EQ(audio_bus.frames(), params_.frames_per_buffer()); | |
| 48 EXPECT_FALSE(estimated_capture_time.is_null()); | |
| 49 OnDataCallback(); | |
| 50 } | |
| 51 MOCK_METHOD0(OnDataCallback, void()); | |
| 52 void OnSetFormat(const media::AudioParameters& params) override { | |
| 53 params_ = params; | |
| 54 FormatIsSet(); | |
| 55 } | |
| 56 MOCK_METHOD0(FormatIsSet, void()); | |
| 57 | |
| 58 private: | |
| 59 media::AudioParameters params_; | |
| 60 }; | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 class WebRtcAudioCapturerTest : public testing::Test { | |
| 65 protected: | |
| 66 WebRtcAudioCapturerTest() | |
| 67 #if defined(OS_ANDROID) | |
| 68 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 69 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 960) { | |
| 70 // Android works with a buffer size bigger than 20ms. | |
| 71 #else | |
| 72 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 73 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128) { | |
| 74 #endif | |
| 75 } | |
| 76 | |
| 77 void VerifyAudioParams(const blink::WebMediaConstraints& constraints, | |
| 78 bool need_audio_processing) { | |
| 79 const std::unique_ptr<WebRtcAudioCapturer> capturer = | |
| 80 WebRtcAudioCapturer::CreateCapturer( | |
| 81 -1, StreamDeviceInfo( | |
| 82 MEDIA_DEVICE_AUDIO_CAPTURE, "", "", params_.sample_rate(), | |
| 83 params_.channel_layout(), params_.frames_per_buffer()), | |
| 84 constraints, nullptr, nullptr); | |
| 85 const scoped_refptr<MockCapturerSource> capturer_source( | |
| 86 new MockCapturerSource()); | |
| 87 EXPECT_CALL(*capturer_source.get(), Initialize(_, capturer.get(), -1)); | |
| 88 EXPECT_CALL(*capturer_source.get(), SetAutomaticGainControl(true)); | |
| 89 EXPECT_CALL(*capturer_source.get(), Start()); | |
| 90 capturer->SetCapturerSource(capturer_source, params_); | |
| 91 | |
| 92 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
| 93 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
| 94 const std::unique_ptr<WebRtcLocalAudioTrack> track( | |
| 95 new WebRtcLocalAudioTrack(adapter.get())); | |
| 96 capturer->AddTrack(track.get()); | |
| 97 | |
| 98 // Connect a mock sink to the track. | |
| 99 std::unique_ptr<MockMediaStreamAudioSink> sink( | |
| 100 new MockMediaStreamAudioSink()); | |
| 101 track->AddSink(sink.get()); | |
| 102 | |
| 103 int delay_ms = 65; | |
| 104 bool key_pressed = true; | |
| 105 double volume = 0.9; | |
| 106 | |
| 107 std::unique_ptr<media::AudioBus> audio_bus = | |
| 108 media::AudioBus::Create(params_); | |
| 109 audio_bus->Zero(); | |
| 110 | |
| 111 media::AudioCapturerSource::CaptureCallback* callback = | |
| 112 static_cast<media::AudioCapturerSource::CaptureCallback*>( | |
| 113 capturer.get()); | |
| 114 | |
| 115 // Verify the sink is getting the correct values. | |
| 116 EXPECT_CALL(*sink, FormatIsSet()); | |
| 117 EXPECT_CALL(*sink, OnDataCallback()).Times(AtLeast(1)); | |
| 118 callback->Capture(audio_bus.get(), delay_ms, volume, key_pressed); | |
| 119 | |
| 120 track->RemoveSink(sink.get()); | |
| 121 EXPECT_CALL(*capturer_source.get(), Stop()); | |
| 122 capturer->Stop(); | |
| 123 } | |
| 124 | |
| 125 media::AudioParameters params_; | |
| 126 }; | |
| 127 | |
| 128 TEST_F(WebRtcAudioCapturerTest, VerifyAudioParamsWithAudioProcessing) { | |
| 129 // Turn off the default constraints to verify that the sink will get packets | |
| 130 // with a buffer size smaller than 10ms. | |
| 131 MockConstraintFactory constraint_factory; | |
| 132 constraint_factory.DisableDefaultAudioConstraints(); | |
| 133 VerifyAudioParams(constraint_factory.CreateWebMediaConstraints(), false); | |
| 134 } | |
| 135 | |
| 136 TEST_F(WebRtcAudioCapturerTest, FailToCreateCapturerWithWrongConstraints) { | |
| 137 MockConstraintFactory constraint_factory; | |
| 138 const std::string dummy_constraint = "dummy"; | |
| 139 // Set a non-audio constraint. | |
| 140 constraint_factory.basic().width.setExact(240); | |
| 141 | |
| 142 std::unique_ptr<WebRtcAudioCapturer> capturer( | |
| 143 WebRtcAudioCapturer::CreateCapturer( | |
| 144 0, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", "", | |
| 145 params_.sample_rate(), params_.channel_layout(), | |
| 146 params_.frames_per_buffer()), | |
| 147 constraint_factory.CreateWebMediaConstraints(), NULL, NULL)); | |
| 148 EXPECT_TRUE(capturer.get() == NULL); | |
| 149 } | |
| 150 | |
| 151 | |
| 152 } // namespace content | |
| OLD | NEW |