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.
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
;
30 const int kHardwareSampleRate
= 44100;
31 const int kHardwareBufferSize
= 512;
33 class MockAudioOutputIPC
: public media::AudioOutputIPC
{
35 MockAudioOutputIPC() {}
36 virtual ~MockAudioOutputIPC() {}
38 MOCK_METHOD4(RequestDeviceAuthorization
,
39 void(media::AudioOutputIPCDelegate
* delegate
,
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
) {
58 FakeAudioOutputDevice(
59 scoped_ptr
<media::AudioOutputIPC
> ipc
,
60 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
61 : AudioOutputDevice(ipc
.Pass(),
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
*());
74 virtual ~FakeAudioOutputDevice() {}
77 class MockAudioDeviceFactory
: public AudioDeviceFactory
{
79 MockAudioDeviceFactory() {}
80 virtual ~MockAudioDeviceFactory() {}
81 MOCK_METHOD4(CreateOutputDevice
,
82 media::AudioOutputDevice
*(int,
86 MOCK_METHOD1(CreateInputDevice
, media::AudioInputDevice
*(int));
89 class MockAudioRendererSource
: public WebRtcAudioRendererSource
{
91 MockAudioRendererSource() {}
92 virtual ~MockAudioRendererSource() {}
93 MOCK_METHOD4(RenderData
, void(media::AudioBus
* audio_bus
,
95 int audio_delay_milliseconds
,
96 base::TimeDelta
* current_time
));
97 MOCK_METHOD1(RemoveAudioRenderer
, void(WebRtcAudioRenderer
* renderer
));
102 class WebRtcAudioRendererTest
: public testing::Test
{
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(),
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
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
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
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);
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
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;
191 static const int kExpectedBufferSize
= kHardwareBufferSize
;
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