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_media_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/audio/audio_parameters.h" | |
13 #include "media/base/audio_bus.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 capturer_ = WebRtcAudioCapturer::CreateCapturer( | |
80 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", "", | |
81 params_.sample_rate(), params_.channel_layout(), | |
82 params_.frames_per_buffer()), | |
83 constraints, NULL, NULL); | |
84 capturer_source_ = new MockCapturerSource(); | |
85 EXPECT_CALL(*capturer_source_.get(), Initialize(_, capturer_.get(), -1)); | |
86 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
87 EXPECT_CALL(*capturer_source_.get(), Start()); | |
88 capturer_->SetCapturerSource(capturer_source_, params_); | |
89 | |
90 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | |
91 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | |
92 track_.reset(new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); | |
93 track_->Start(); | |
94 | |
95 // Connect a mock sink to the track. | |
96 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | |
97 track_->AddSink(sink.get()); | |
98 | |
99 int delay_ms = 65; | |
100 bool key_pressed = true; | |
101 double volume = 0.9; | |
102 | |
103 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create(params_); | |
104 audio_bus->Zero(); | |
105 | |
106 media::AudioCapturerSource::CaptureCallback* callback = | |
107 static_cast<media::AudioCapturerSource::CaptureCallback*>( | |
108 capturer_.get()); | |
109 | |
110 // Verify the sink is getting the correct values. | |
111 EXPECT_CALL(*sink, FormatIsSet()); | |
112 EXPECT_CALL(*sink, OnDataCallback()).Times(AtLeast(1)); | |
113 callback->Capture(audio_bus.get(), delay_ms, volume, key_pressed); | |
114 | |
115 track_->RemoveSink(sink.get()); | |
116 EXPECT_CALL(*capturer_source_.get(), Stop()); | |
117 capturer_->Stop(); | |
118 } | |
119 | |
120 media::AudioParameters params_; | |
121 scoped_refptr<MockCapturerSource> capturer_source_; | |
122 scoped_refptr<WebRtcAudioCapturer> capturer_; | |
123 scoped_ptr<WebRtcLocalAudioTrack> track_; | |
124 }; | |
125 | |
126 TEST_F(WebRtcAudioCapturerTest, VerifyAudioParamsWithAudioProcessing) { | |
127 // Turn off the default constraints to verify that the sink will get packets | |
128 // with a buffer size smaller than 10ms. | |
129 MockMediaConstraintFactory constraint_factory; | |
130 constraint_factory.DisableDefaultAudioConstraints(); | |
131 VerifyAudioParams(constraint_factory.CreateWebMediaConstraints(), false); | |
132 } | |
133 | |
134 TEST_F(WebRtcAudioCapturerTest, FailToCreateCapturerWithWrongConstraints) { | |
135 MockMediaConstraintFactory constraint_factory; | |
136 const std::string dummy_constraint = "dummy"; | |
137 constraint_factory.AddMandatory(dummy_constraint, true); | |
138 | |
139 scoped_refptr<WebRtcAudioCapturer> capturer( | |
140 WebRtcAudioCapturer::CreateCapturer( | |
141 0, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", "", | |
142 params_.sample_rate(), params_.channel_layout(), | |
143 params_.frames_per_buffer()), | |
144 constraint_factory.CreateWebMediaConstraints(), NULL, NULL)); | |
145 EXPECT_TRUE(capturer.get() == NULL); | |
146 } | |
147 | |
148 | |
149 } // namespace content | |
OLD | NEW |