Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_capturer_unittest.cc
blob92d291f9fc8c306b4c520fa99e1c625be98767d3
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 ~MockCapturerSource() override {}
39 class MockMediaStreamAudioSink : public MediaStreamAudioSink {
40 public:
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());
48 OnDataCallback();
50 MOCK_METHOD0(OnDataCallback, void());
51 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(), 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));
92 track_->Start();
94 // Connect a mock sink to the track.
95 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
96 track_->AddSink(sink.get());
98 int delay_ms = 65;
99 bool key_pressed = true;
100 double volume = 0.9;
102 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create(params_);
103 audio_bus->Zero();
105 media::AudioCapturerSource::CaptureCallback* callback =
106 static_cast<media::AudioCapturerSource::CaptureCallback*>(
107 capturer_.get());
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());
116 capturer_->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