Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_renderer_unittest.cc
blob86d1fb6cebfff263a1d5ecb091f8746fc56dc70f
1 // Copyright 2014 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 <string>
6 #include <vector>
8 #include "base/single_thread_task_runner.h"
9 #include "content/public/renderer/media_stream_audio_renderer.h"
10 #include "content/renderer/media/audio_device_factory.h"
11 #include "content/renderer/media/audio_message_filter.h"
12 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
13 #include "content/renderer/media/webrtc_audio_device_impl.h"
14 #include "content/renderer/media/webrtc_audio_renderer.h"
15 #include "media/audio/audio_output_device.h"
16 #include "media/audio/audio_output_ipc.h"
17 #include "media/base/audio_bus.h"
18 #include "media/base/mock_audio_renderer_sink.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
23 using testing::Return;
24 using testing::_;
26 namespace content {
28 namespace {
30 const int kHardwareSampleRate = 44100;
31 const int kHardwareBufferSize = 512;
33 class MockAudioOutputIPC : public media::AudioOutputIPC {
34 public:
35 MockAudioOutputIPC() {}
36 virtual ~MockAudioOutputIPC() {}
38 MOCK_METHOD4(RequestDeviceAuthorization,
39 void(media::AudioOutputIPCDelegate* delegate,
40 int session_id,
41 const std::string& device_id,
42 const url::Origin& security_origin));
43 MOCK_METHOD2(CreateStream,
44 void(media::AudioOutputIPCDelegate* delegate,
45 const media::AudioParameters& params));
46 MOCK_METHOD0(PlayStream, void());
47 MOCK_METHOD0(PauseStream, void());
48 MOCK_METHOD0(CloseStream, void());
49 MOCK_METHOD1(SetVolume, void(double volume));
50 MOCK_METHOD2(SwitchOutputDevice,
51 void(const std::string& device_id,
52 const url::Origin& security_origin));
55 class FakeAudioOutputDevice
56 : NON_EXPORTED_BASE(public media::AudioOutputDevice) {
57 public:
58 FakeAudioOutputDevice(
59 scoped_ptr<media::AudioOutputIPC> ipc,
60 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
61 : AudioOutputDevice(ipc.Pass(),
62 io_task_runner,
64 std::string(),
65 url::Origin()) {}
66 MOCK_METHOD0(Start, void());
67 MOCK_METHOD0(Stop, void());
68 MOCK_METHOD0(Pause, void());
69 MOCK_METHOD0(Play, void());
70 MOCK_METHOD1(SetVolume, bool(double volume));
71 MOCK_METHOD0(GetOutputDevice, media::OutputDevice*());
73 protected:
74 virtual ~FakeAudioOutputDevice() {}
77 class MockAudioDeviceFactory : public AudioDeviceFactory {
78 public:
79 MockAudioDeviceFactory() {}
80 virtual ~MockAudioDeviceFactory() {}
81 MOCK_METHOD4(CreateOutputDevice,
82 media::AudioOutputDevice*(int,
83 int,
84 const std::string&,
85 const url::Origin&));
86 MOCK_METHOD1(CreateInputDevice, media::AudioInputDevice*(int));
89 class MockAudioRendererSource : public WebRtcAudioRendererSource {
90 public:
91 MockAudioRendererSource() {}
92 virtual ~MockAudioRendererSource() {}
93 MOCK_METHOD4(RenderData, void(media::AudioBus* audio_bus,
94 int sample_rate,
95 int audio_delay_milliseconds,
96 base::TimeDelta* current_time));
97 MOCK_METHOD1(RemoveAudioRenderer, void(WebRtcAudioRenderer* renderer));
100 } // namespace
102 class WebRtcAudioRendererTest : public testing::Test {
103 protected:
104 WebRtcAudioRendererTest()
105 : message_loop_(new base::MessageLoopForIO),
106 mock_ipc_(new MockAudioOutputIPC()),
107 mock_output_device_(new FakeAudioOutputDevice(
108 scoped_ptr<media::AudioOutputIPC>(mock_ipc_),
109 message_loop_->task_runner())),
110 factory_(new MockAudioDeviceFactory()),
111 source_(new MockAudioRendererSource()),
112 stream_(new rtc::RefCountedObject<MockMediaStream>("label")),
113 renderer_(new WebRtcAudioRenderer(message_loop_->task_runner(),
114 stream_,
117 44100,
118 kHardwareBufferSize)) {
119 EXPECT_CALL(*factory_.get(), CreateOutputDevice(1, _, _, _))
120 .WillOnce(Return(mock_output_device_.get()));
121 EXPECT_CALL(*mock_output_device_.get(), Start());
122 EXPECT_TRUE(renderer_->Initialize(source_.get()));
123 renderer_proxy_ = renderer_->CreateSharedAudioRendererProxy(stream_);
126 // Used to construct |mock_output_device_|.
127 scoped_ptr<base::MessageLoopForIO> message_loop_;
128 MockAudioOutputIPC* mock_ipc_; // Owned by AudioOuputDevice.
130 scoped_refptr<FakeAudioOutputDevice> mock_output_device_;
131 scoped_ptr<MockAudioDeviceFactory> factory_;
132 scoped_ptr<MockAudioRendererSource> source_;
133 scoped_refptr<webrtc::MediaStreamInterface> stream_;
134 scoped_refptr<WebRtcAudioRenderer> renderer_;
135 scoped_refptr<MediaStreamAudioRenderer> renderer_proxy_;
138 // Verify that the renderer will be stopped if the only proxy is stopped.
139 TEST_F(WebRtcAudioRendererTest, StopRenderer) {
140 renderer_proxy_->Start();
142 // |renderer_| has only one proxy, stopping the proxy should stop the sink of
143 // |renderer_|.
144 EXPECT_CALL(*mock_output_device_.get(), Stop());
145 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
146 renderer_proxy_->Stop();
149 // Verify that the renderer will not be stopped unless the last proxy is
150 // stopped.
151 TEST_F(WebRtcAudioRendererTest, MultipleRenderers) {
152 renderer_proxy_->Start();
154 // Create a vector of renderer proxies from the |renderer_|.
155 std::vector<scoped_refptr<MediaStreamAudioRenderer> > renderer_proxies_;
156 static const int kNumberOfRendererProxy = 5;
157 for (int i = 0; i < kNumberOfRendererProxy; ++i) {
158 scoped_refptr<MediaStreamAudioRenderer> renderer_proxy(
159 renderer_->CreateSharedAudioRendererProxy(stream_));
160 renderer_proxy->Start();
161 renderer_proxies_.push_back(renderer_proxy);
164 // Stop the |renderer_proxy_| should not stop the sink since it is used by
165 // other proxies.
166 EXPECT_CALL(*mock_output_device_.get(), Stop()).Times(0);
167 renderer_proxy_->Stop();
169 for (int i = 0; i < kNumberOfRendererProxy; ++i) {
170 if (i != kNumberOfRendererProxy -1) {
171 EXPECT_CALL(*mock_output_device_.get(), Stop()).Times(0);
172 } else {
173 // When the last proxy is stopped, the sink will stop.
174 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
175 EXPECT_CALL(*mock_output_device_.get(), Stop());
177 renderer_proxies_[i]->Stop();
181 // Verify that the sink of the renderer is using the expected sample rate and
182 // buffer size.
183 TEST_F(WebRtcAudioRendererTest, VerifySinkParameters) {
184 renderer_proxy_->Start();
185 #if defined(OS_LINUX) || defined(OS_MACOSX)
186 static const int kExpectedBufferSize = kHardwareSampleRate / 100;
187 #elif defined(OS_ANDROID)
188 static const int kExpectedBufferSize = 2 * kHardwareSampleRate / 100;
189 #else
190 // Windows.
191 static const int kExpectedBufferSize = kHardwareBufferSize;
192 #endif
193 EXPECT_EQ(kExpectedBufferSize, renderer_->frames_per_buffer());
194 EXPECT_EQ(kHardwareSampleRate, renderer_->sample_rate());
195 EXPECT_EQ(2, renderer_->channels());
197 EXPECT_CALL(*mock_output_device_.get(), Stop());
198 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
199 renderer_proxy_->Stop();
202 } // namespace content