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.
5 #include "base/logging.h"
6 #include "content/public/renderer/media_stream_audio_sink.h"
7 #include "content/renderer/media/mock_media_constraint_factory.h"
8 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
9 #include "content/renderer/media/webrtc_audio_capturer.h"
10 #include "content/renderer/media/webrtc_local_audio_track.h"
11 #include "media/audio/audio_parameters.h"
12 #include "media/base/audio_bus.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
18 using ::testing::AtLeast
;
24 class MockCapturerSource
: public media::AudioCapturerSource
{
26 MockCapturerSource() {}
27 MOCK_METHOD3(Initialize
, void(const media::AudioParameters
& params
,
28 CaptureCallback
* callback
,
30 MOCK_METHOD0(Start
, void());
31 MOCK_METHOD0(Stop
, void());
32 MOCK_METHOD1(SetVolume
, void(double volume
));
33 MOCK_METHOD1(SetAutomaticGainControl
, void(bool enable
));
36 ~MockCapturerSource() override
{}
39 class MockMediaStreamAudioSink
: public MediaStreamAudioSink
{
41 MockMediaStreamAudioSink() {}
42 ~MockMediaStreamAudioSink() override
{}
43 void OnData(const media::AudioBus
& audio_bus
,
44 base::TimeTicks estimated_capture_time
) override
{
45 EXPECT_EQ(audio_bus
.channels(), params_
.channels());
46 EXPECT_EQ(audio_bus
.frames(), params_
.frames_per_buffer());
47 EXPECT_FALSE(estimated_capture_time
.is_null());
50 MOCK_METHOD0(OnDataCallback
, void());
51 void OnSetFormat(const media::AudioParameters
& params
) override
{
55 MOCK_METHOD0(FormatIsSet
, void());
58 media::AudioParameters params_
;
63 class WebRtcAudioCapturerTest
: public testing::Test
{
65 WebRtcAudioCapturerTest()
66 #if defined(OS_ANDROID)
67 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
68 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 960) {
69 // Android works with a buffer size bigger than 20ms.
71 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
72 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 128) {
76 void VerifyAudioParams(const blink::WebMediaConstraints
& constraints
,
77 bool need_audio_processing
) {
78 capturer_
= WebRtcAudioCapturer::CreateCapturer(
79 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE
, "", "",
80 params_
.sample_rate(), params_
.channel_layout(),
81 params_
.frames_per_buffer()),
82 constraints
, NULL
, NULL
);
83 capturer_source_
= new MockCapturerSource();
84 EXPECT_CALL(*capturer_source_
.get(), Initialize(_
, capturer_
.get(), -1));
85 EXPECT_CALL(*capturer_source_
.get(), SetAutomaticGainControl(true));
86 EXPECT_CALL(*capturer_source_
.get(), Start());
87 capturer_
->SetCapturerSource(capturer_source_
, params_
);
89 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
90 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
91 track_
.reset(new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
94 // Connect a mock sink to the track.
95 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
96 track_
->AddSink(sink
.get());
99 bool key_pressed
= true;
102 scoped_ptr
<media::AudioBus
> audio_bus
= media::AudioBus::Create(params_
);
105 media::AudioCapturerSource::CaptureCallback
* callback
=
106 static_cast<media::AudioCapturerSource::CaptureCallback
*>(
109 // Verify the sink is getting the correct values.
110 EXPECT_CALL(*sink
, FormatIsSet());
111 EXPECT_CALL(*sink
, OnDataCallback()).Times(AtLeast(1));
112 callback
->Capture(audio_bus
.get(), delay_ms
, volume
, key_pressed
);
114 track_
->RemoveSink(sink
.get());
115 EXPECT_CALL(*capturer_source_
.get(), Stop());
119 media::AudioParameters params_
;
120 scoped_refptr
<MockCapturerSource
> capturer_source_
;
121 scoped_refptr
<WebRtcAudioCapturer
> capturer_
;
122 scoped_ptr
<WebRtcLocalAudioTrack
> track_
;
125 TEST_F(WebRtcAudioCapturerTest
, VerifyAudioParamsWithAudioProcessing
) {
126 // Turn off the default constraints to verify that the sink will get packets
127 // with a buffer size smaller than 10ms.
128 MockMediaConstraintFactory constraint_factory
;
129 constraint_factory
.DisableDefaultAudioConstraints();
130 VerifyAudioParams(constraint_factory
.CreateWebMediaConstraints(), false);
133 TEST_F(WebRtcAudioCapturerTest
, FailToCreateCapturerWithWrongConstraints
) {
134 MockMediaConstraintFactory constraint_factory
;
135 const std::string dummy_constraint
= "dummy";
136 constraint_factory
.AddMandatory(dummy_constraint
, true);
138 scoped_refptr
<WebRtcAudioCapturer
> capturer(
139 WebRtcAudioCapturer::CreateCapturer(
140 0, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE
, "", "",
141 params_
.sample_rate(), params_
.channel_layout(),
142 params_
.frames_per_buffer()),
143 constraint_factory
.CreateWebMediaConstraints(), NULL
, NULL
));
144 EXPECT_TRUE(capturer
.get() == NULL
);
148 } // namespace content