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
;
29 const int kHardwareSampleRate
= 44100;
30 const int kHardwareBufferSize
= 512;
32 class MockAudioOutputIPC
: public media::AudioOutputIPC
{
34 MockAudioOutputIPC() {}
35 virtual ~MockAudioOutputIPC() {}
37 MOCK_METHOD3(CreateStream
, void(media::AudioOutputIPCDelegate
* delegate
,
38 const media::AudioParameters
& params
,
40 MOCK_METHOD0(PlayStream
, void());
41 MOCK_METHOD0(PauseStream
, void());
42 MOCK_METHOD0(CloseStream
, void());
43 MOCK_METHOD1(SetVolume
, void(double volume
));
44 MOCK_METHOD3(SwitchOutputDevice
,
45 void(const std::string
& device_id
,
46 const GURL
& security_origin
,
50 class FakeAudioOutputDevice
51 : NON_EXPORTED_BASE(public media::AudioOutputDevice
) {
53 FakeAudioOutputDevice(
54 scoped_ptr
<media::AudioOutputIPC
> ipc
,
55 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
56 : AudioOutputDevice(ipc
.Pass(),
58 MOCK_METHOD0(Start
, void());
59 MOCK_METHOD0(Stop
, void());
60 MOCK_METHOD0(Pause
, void());
61 MOCK_METHOD0(Play
, void());
62 MOCK_METHOD1(SetVolume
, bool(double volume
));
63 MOCK_METHOD3(SwitchOutputDevice
,
64 void(const std::string
&,
65 const GURL
& security_origin
,
66 const media::SwitchOutputDeviceCB
& callback
));
69 virtual ~FakeAudioOutputDevice() {}
72 class MockAudioDeviceFactory
: public AudioDeviceFactory
{
74 MockAudioDeviceFactory() {}
75 virtual ~MockAudioDeviceFactory() {}
76 MOCK_METHOD1(CreateOutputDevice
, media::AudioOutputDevice
*(int));
77 MOCK_METHOD1(CreateInputDevice
, media::AudioInputDevice
*(int));
80 class MockAudioRendererSource
: public WebRtcAudioRendererSource
{
82 MockAudioRendererSource() {}
83 virtual ~MockAudioRendererSource() {}
84 MOCK_METHOD4(RenderData
, void(media::AudioBus
* audio_bus
,
86 int audio_delay_milliseconds
,
87 base::TimeDelta
* current_time
));
88 MOCK_METHOD1(RemoveAudioRenderer
, void(WebRtcAudioRenderer
* renderer
));
93 class WebRtcAudioRendererTest
: public testing::Test
{
95 WebRtcAudioRendererTest()
96 : message_loop_(new base::MessageLoopForIO
),
97 mock_ipc_(new MockAudioOutputIPC()),
98 mock_output_device_(new FakeAudioOutputDevice(
99 scoped_ptr
<media::AudioOutputIPC
>(mock_ipc_
),
100 message_loop_
->task_runner())),
101 factory_(new MockAudioDeviceFactory()),
102 source_(new MockAudioRendererSource()),
103 stream_(new rtc::RefCountedObject
<MockMediaStream
>("label")),
104 renderer_(new WebRtcAudioRenderer(message_loop_
->task_runner(),
109 kHardwareBufferSize
)) {
110 EXPECT_CALL(*factory_
.get(), CreateOutputDevice(1))
111 .WillOnce(Return(mock_output_device_
.get()));
112 EXPECT_CALL(*mock_output_device_
.get(), Start());
113 EXPECT_TRUE(renderer_
->Initialize(source_
.get()));
114 renderer_proxy_
= renderer_
->CreateSharedAudioRendererProxy(stream_
);
117 // Used to construct |mock_output_device_|.
118 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
119 MockAudioOutputIPC
* mock_ipc_
; // Owned by AudioOuputDevice.
121 scoped_refptr
<FakeAudioOutputDevice
> mock_output_device_
;
122 scoped_ptr
<MockAudioDeviceFactory
> factory_
;
123 scoped_ptr
<MockAudioRendererSource
> source_
;
124 scoped_refptr
<webrtc::MediaStreamInterface
> stream_
;
125 scoped_refptr
<WebRtcAudioRenderer
> renderer_
;
126 scoped_refptr
<MediaStreamAudioRenderer
> renderer_proxy_
;
129 // Verify that the renderer will be stopped if the only proxy is stopped.
130 TEST_F(WebRtcAudioRendererTest
, StopRenderer
) {
131 renderer_proxy_
->Start();
133 // |renderer_| has only one proxy, stopping the proxy should stop the sink of
135 EXPECT_CALL(*mock_output_device_
.get(), Stop());
136 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
137 renderer_proxy_
->Stop();
140 // Verify that the renderer will not be stopped unless the last proxy is
142 TEST_F(WebRtcAudioRendererTest
, MultipleRenderers
) {
143 renderer_proxy_
->Start();
145 // Create a vector of renderer proxies from the |renderer_|.
146 std::vector
<scoped_refptr
<MediaStreamAudioRenderer
> > renderer_proxies_
;
147 static const int kNumberOfRendererProxy
= 5;
148 for (int i
= 0; i
< kNumberOfRendererProxy
; ++i
) {
149 scoped_refptr
<MediaStreamAudioRenderer
> renderer_proxy(
150 renderer_
->CreateSharedAudioRendererProxy(stream_
));
151 renderer_proxy
->Start();
152 renderer_proxies_
.push_back(renderer_proxy
);
155 // Stop the |renderer_proxy_| should not stop the sink since it is used by
157 EXPECT_CALL(*mock_output_device_
.get(), Stop()).Times(0);
158 renderer_proxy_
->Stop();
160 for (int i
= 0; i
< kNumberOfRendererProxy
; ++i
) {
161 if (i
!= kNumberOfRendererProxy
-1) {
162 EXPECT_CALL(*mock_output_device_
.get(), Stop()).Times(0);
164 // When the last proxy is stopped, the sink will stop.
165 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
166 EXPECT_CALL(*mock_output_device_
.get(), Stop());
168 renderer_proxies_
[i
]->Stop();
172 // Verify that the sink of the renderer is using the expected sample rate and
174 TEST_F(WebRtcAudioRendererTest
, VerifySinkParameters
) {
175 renderer_proxy_
->Start();
176 #if defined(OS_LINUX) || defined(OS_MACOSX)
177 static const int kExpectedBufferSize
= kHardwareSampleRate
/ 100;
178 #elif defined(OS_ANDROID)
179 static const int kExpectedBufferSize
= 2 * kHardwareSampleRate
/ 100;
182 static const int kExpectedBufferSize
= kHardwareBufferSize
;
184 EXPECT_EQ(kExpectedBufferSize
, renderer_
->frames_per_buffer());
185 EXPECT_EQ(kHardwareSampleRate
, renderer_
->sample_rate());
186 EXPECT_EQ(2, renderer_
->channels());
188 EXPECT_CALL(*mock_output_device_
.get(), Stop());
189 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
190 renderer_proxy_
->Stop();
193 } // namespace content