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.
7 #include "content/renderer/media/audio_device_factory.h"
8 #include "content/renderer/media/audio_message_filter.h"
9 #include "content/renderer/media/media_stream_audio_renderer.h"
10 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
11 #include "content/renderer/media/webrtc_audio_device_impl.h"
12 #include "content/renderer/media/webrtc_audio_renderer.h"
13 #include "media/audio/audio_output_device.h"
14 #include "media/audio/audio_output_ipc.h"
15 #include "media/base/audio_bus.h"
16 #include "media/base/mock_audio_renderer_sink.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
21 using testing::Return
;
27 const int kHardwareSampleRate
= 44100;
28 const int kHardwareBufferSize
= 512;
30 class MockAudioOutputIPC
: public media::AudioOutputIPC
{
32 MockAudioOutputIPC() {}
33 virtual ~MockAudioOutputIPC() {}
35 MOCK_METHOD3(CreateStream
, void(media::AudioOutputIPCDelegate
* delegate
,
36 const media::AudioParameters
& params
,
38 MOCK_METHOD0(PlayStream
, void());
39 MOCK_METHOD0(PauseStream
, void());
40 MOCK_METHOD0(CloseStream
, void());
41 MOCK_METHOD1(SetVolume
, void(double volume
));
44 class FakeAudioOutputDevice
45 : NON_EXPORTED_BASE(public media::AudioOutputDevice
) {
47 FakeAudioOutputDevice(
48 scoped_ptr
<media::AudioOutputIPC
> ipc
,
49 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
50 : AudioOutputDevice(ipc
.Pass(),
52 MOCK_METHOD0(Start
, void());
53 MOCK_METHOD0(Stop
, void());
54 MOCK_METHOD0(Pause
, void());
55 MOCK_METHOD0(Play
, void());
56 MOCK_METHOD1(SetVolume
, bool(double volume
));
59 virtual ~FakeAudioOutputDevice() {}
62 class MockAudioDeviceFactory
: public AudioDeviceFactory
{
64 MockAudioDeviceFactory() {}
65 virtual ~MockAudioDeviceFactory() {}
66 MOCK_METHOD1(CreateOutputDevice
, media::AudioOutputDevice
*(int));
67 MOCK_METHOD1(CreateInputDevice
, media::AudioInputDevice
*(int));
70 class MockAudioRendererSource
: public WebRtcAudioRendererSource
{
72 MockAudioRendererSource() {}
73 virtual ~MockAudioRendererSource() {}
74 MOCK_METHOD4(RenderData
, void(media::AudioBus
* audio_bus
,
76 int audio_delay_milliseconds
,
77 base::TimeDelta
* current_time
));
78 MOCK_METHOD1(RemoveAudioRenderer
, void(WebRtcAudioRenderer
* renderer
));
83 class WebRtcAudioRendererTest
: public testing::Test
{
85 WebRtcAudioRendererTest()
86 : message_loop_(new base::MessageLoopForIO
),
87 mock_ipc_(new MockAudioOutputIPC()),
88 mock_output_device_(new FakeAudioOutputDevice(
89 scoped_ptr
<media::AudioOutputIPC
>(mock_ipc_
),
90 message_loop_
->message_loop_proxy())),
91 factory_(new MockAudioDeviceFactory()),
92 source_(new MockAudioRendererSource()),
93 stream_(new rtc::RefCountedObject
<MockMediaStream
>("label")),
94 renderer_(new WebRtcAudioRenderer(message_loop_
->message_loop_proxy(),
95 stream_
, 1, 1, 1, 44100,
96 kHardwareBufferSize
)) {
97 EXPECT_CALL(*factory_
.get(), CreateOutputDevice(1))
98 .WillOnce(Return(mock_output_device_
.get()));
99 EXPECT_CALL(*mock_output_device_
.get(), Start());
100 EXPECT_TRUE(renderer_
->Initialize(source_
.get()));
101 renderer_proxy_
= renderer_
->CreateSharedAudioRendererProxy(stream_
);
104 // Used to construct |mock_output_device_|.
105 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
106 MockAudioOutputIPC
* mock_ipc_
; // Owned by AudioOuputDevice.
108 scoped_refptr
<FakeAudioOutputDevice
> mock_output_device_
;
109 scoped_ptr
<MockAudioDeviceFactory
> factory_
;
110 scoped_ptr
<MockAudioRendererSource
> source_
;
111 scoped_refptr
<webrtc::MediaStreamInterface
> stream_
;
112 scoped_refptr
<WebRtcAudioRenderer
> renderer_
;
113 scoped_refptr
<MediaStreamAudioRenderer
> renderer_proxy_
;
116 // Verify that the renderer will be stopped if the only proxy is stopped.
117 TEST_F(WebRtcAudioRendererTest
, StopRenderer
) {
118 renderer_proxy_
->Start();
120 // |renderer_| has only one proxy, stopping the proxy should stop the sink of
122 EXPECT_CALL(*mock_output_device_
.get(), Stop());
123 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
124 renderer_proxy_
->Stop();
127 // Verify that the renderer will not be stopped unless the last proxy is
129 TEST_F(WebRtcAudioRendererTest
, MultipleRenderers
) {
130 renderer_proxy_
->Start();
132 // Create a vector of renderer proxies from the |renderer_|.
133 std::vector
<scoped_refptr
<MediaStreamAudioRenderer
> > renderer_proxies_
;
134 static const int kNumberOfRendererProxy
= 5;
135 for (int i
= 0; i
< kNumberOfRendererProxy
; ++i
) {
136 scoped_refptr
<MediaStreamAudioRenderer
> renderer_proxy(
137 renderer_
->CreateSharedAudioRendererProxy(stream_
));
138 renderer_proxy
->Start();
139 renderer_proxies_
.push_back(renderer_proxy
);
142 // Stop the |renderer_proxy_| should not stop the sink since it is used by
144 EXPECT_CALL(*mock_output_device_
.get(), Stop()).Times(0);
145 renderer_proxy_
->Stop();
147 for (int i
= 0; i
< kNumberOfRendererProxy
; ++i
) {
148 if (i
!= kNumberOfRendererProxy
-1) {
149 EXPECT_CALL(*mock_output_device_
.get(), Stop()).Times(0);
151 // When the last proxy is stopped, the sink will stop.
152 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
153 EXPECT_CALL(*mock_output_device_
.get(), Stop());
155 renderer_proxies_
[i
]->Stop();
159 // Verify that the sink of the renderer is using the expected sample rate and
161 TEST_F(WebRtcAudioRendererTest
, VerifySinkParameters
) {
162 renderer_proxy_
->Start();
163 #if defined(OS_LINUX) || defined(OS_MACOSX)
164 static const int kExpectedBufferSize
= kHardwareSampleRate
/ 100;
165 #elif defined(OS_ANDROID)
166 static const int kExpectedBufferSize
= 2 * kHardwareSampleRate
/ 100;
169 static const int kExpectedBufferSize
= kHardwareBufferSize
;
171 EXPECT_EQ(kExpectedBufferSize
, renderer_
->frames_per_buffer());
172 EXPECT_EQ(kHardwareSampleRate
, renderer_
->sample_rate());
173 EXPECT_EQ(2, renderer_
->channels());
175 EXPECT_CALL(*mock_output_device_
.get(), Stop());
176 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
177 renderer_proxy_
->Stop();
180 } // namespace content