[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_capturer_unittest.cc
blob1151dcef6ac1d15706625a42b1ba6862a3785ef9
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"
17 using ::testing::_;
18 using ::testing::AtLeast;
20 namespace content {
22 namespace {
24 class MockCapturerSource : public media::AudioCapturerSource {
25 public:
26 MockCapturerSource() {}
27 MOCK_METHOD3(Initialize, void(const media::AudioParameters& params,
28 CaptureCallback* callback,
29 int session_id));
30 MOCK_METHOD0(Start, void());
31 MOCK_METHOD0(Stop, void());
32 MOCK_METHOD1(SetVolume, void(double volume));
33 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable));
35 protected:
36 virtual ~MockCapturerSource() {}
39 class MockMediaStreamAudioSink : public MediaStreamAudioSink {
40 public:
41 MockMediaStreamAudioSink() {}
42 ~MockMediaStreamAudioSink() {}
43 virtual 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());
48 OnDataCallback();
50 MOCK_METHOD0(OnDataCallback, void());
51 virtual void OnSetFormat(const media::AudioParameters& params) override {
52 params_ = params;
53 FormatIsSet();
55 MOCK_METHOD0(FormatIsSet, void());
57 private:
58 media::AudioParameters params_;
61 } // namespace
63 class WebRtcAudioCapturerTest : public testing::Test {
64 protected:
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.
70 #else
71 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
72 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128) {
73 #endif
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(),
81 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_->SetCapturerSourceForTesting(capturer_source_, params_);
90 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
91 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
92 track_.reset(new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL));
93 track_->Start();
95 // Connect a mock sink to the track.
96 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
97 track_->AddSink(sink.get());
99 int delay_ms = 65;
100 bool key_pressed = true;
101 double volume = 0.9;
103 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create(params_);
104 audio_bus->Zero();
106 media::AudioCapturerSource::CaptureCallback* callback =
107 static_cast<media::AudioCapturerSource::CaptureCallback*>(
108 capturer_.get());
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);
115 track_->RemoveSink(sink.get());
116 EXPECT_CALL(*capturer_source_.get(), Stop());
117 capturer_->Stop();
120 media::AudioParameters params_;
121 scoped_refptr<MockCapturerSource> capturer_source_;
122 scoped_refptr<WebRtcAudioCapturer> capturer_;
123 scoped_ptr<WebRtcLocalAudioTrack> track_;
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);
134 TEST_F(WebRtcAudioCapturerTest, FailToCreateCapturerWithWrongConstraints) {
135 MockMediaConstraintFactory constraint_factory;
136 const std::string dummy_constraint = "dummy";
137 constraint_factory.AddMandatory(dummy_constraint, true);
139 scoped_refptr<WebRtcAudioCapturer> capturer(
140 WebRtcAudioCapturer::CreateCapturer(
141 0, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE,
142 "", "", params_.sample_rate(),
143 params_.channel_layout(),
144 params_.frames_per_buffer()),
145 constraint_factory.CreateWebMediaConstraints(), NULL, NULL)
147 EXPECT_TRUE(capturer.get() == NULL);
151 } // namespace content