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 class MockAudioOutputIPC
: public media::AudioOutputIPC
{
29 MockAudioOutputIPC() {}
30 virtual ~MockAudioOutputIPC() {}
32 MOCK_METHOD3(CreateStream
, void(media::AudioOutputIPCDelegate
* delegate
,
33 const media::AudioParameters
& params
,
35 MOCK_METHOD0(PlayStream
, void());
36 MOCK_METHOD0(PauseStream
, void());
37 MOCK_METHOD0(CloseStream
, void());
38 MOCK_METHOD1(SetVolume
, void(double volume
));
41 class FakeAudioOutputDevice
42 : NON_EXPORTED_BASE(public media::AudioOutputDevice
) {
44 FakeAudioOutputDevice(
45 scoped_ptr
<media::AudioOutputIPC
> ipc
,
46 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
47 : AudioOutputDevice(ipc
.Pass(),
49 MOCK_METHOD0(Start
, void());
50 MOCK_METHOD0(Stop
, void());
51 MOCK_METHOD0(Pause
, void());
52 MOCK_METHOD0(Play
, void());
53 MOCK_METHOD1(SetVolume
, bool(double volume
));
56 virtual ~FakeAudioOutputDevice() {}
59 class MockAudioDeviceFactory
: public AudioDeviceFactory
{
61 MockAudioDeviceFactory() {}
62 virtual ~MockAudioDeviceFactory() {}
63 MOCK_METHOD1(CreateOutputDevice
, media::AudioOutputDevice
*(int));
64 MOCK_METHOD1(CreateInputDevice
, media::AudioInputDevice
*(int));
67 class MockAudioRendererSource
: public WebRtcAudioRendererSource
{
69 MockAudioRendererSource() {}
70 virtual ~MockAudioRendererSource() {}
71 MOCK_METHOD4(RenderData
, void(media::AudioBus
* audio_bus
,
73 int audio_delay_milliseconds
,
74 base::TimeDelta
* current_time
));
75 MOCK_METHOD1(RemoveAudioRenderer
, void(WebRtcAudioRenderer
* renderer
));
80 class WebRtcAudioRendererTest
: public testing::Test
{
82 WebRtcAudioRendererTest()
83 : message_loop_(new base::MessageLoopForIO
),
84 mock_ipc_(new MockAudioOutputIPC()),
85 mock_output_device_(new FakeAudioOutputDevice(
86 scoped_ptr
<media::AudioOutputIPC
>(mock_ipc_
),
87 message_loop_
->message_loop_proxy())),
88 factory_(new MockAudioDeviceFactory()),
89 source_(new MockAudioRendererSource()),
90 stream_(new rtc::RefCountedObject
<MockMediaStream
>("label")),
91 renderer_(new WebRtcAudioRenderer(stream_
, 1, 1, 1, 44100, 441)) {
92 EXPECT_CALL(*factory_
.get(), CreateOutputDevice(1))
93 .WillOnce(Return(mock_output_device_
));
94 EXPECT_CALL(*mock_output_device_
, Start());
95 EXPECT_TRUE(renderer_
->Initialize(source_
.get()));
96 renderer_proxy_
= renderer_
->CreateSharedAudioRendererProxy(stream_
);
99 // Used to construct |mock_output_device_|.
100 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
101 MockAudioOutputIPC
* mock_ipc_
; // Owned by AudioOuputDevice.
103 scoped_refptr
<FakeAudioOutputDevice
> mock_output_device_
;
104 scoped_ptr
<MockAudioDeviceFactory
> factory_
;
105 scoped_ptr
<MockAudioRendererSource
> source_
;
106 scoped_refptr
<webrtc::MediaStreamInterface
> stream_
;
107 scoped_refptr
<WebRtcAudioRenderer
> renderer_
;
108 scoped_refptr
<MediaStreamAudioRenderer
> renderer_proxy_
;
111 // Verify that the renderer will be stopped if the only proxy is stopped.
112 TEST_F(WebRtcAudioRendererTest
, StopRenderer
) {
113 renderer_proxy_
->Start();
115 // |renderer_| has only one proxy, stopping the proxy should stop the sink of
117 EXPECT_CALL(*mock_output_device_
, Stop());
118 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
119 renderer_proxy_
->Stop();
122 // Verify that the renderer will not be stopped unless the last proxy is
124 TEST_F(WebRtcAudioRendererTest
, MultipleRenderers
) {
125 renderer_proxy_
->Start();
127 // Create a vector of renderer proxies from the |renderer_|.
128 std::vector
<scoped_refptr
<MediaStreamAudioRenderer
> > renderer_proxies_
;
129 static const int kNumberOfRendererProxy
= 5;
130 for (int i
= 0; i
< kNumberOfRendererProxy
; ++i
) {
131 scoped_refptr
<MediaStreamAudioRenderer
> renderer_proxy(
132 renderer_
->CreateSharedAudioRendererProxy(stream_
));
133 renderer_proxy
->Start();
134 renderer_proxies_
.push_back(renderer_proxy
);
137 // Stop the |renderer_proxy_| should not stop the sink since it is used by
139 EXPECT_CALL(*mock_output_device_
, Stop()).Times(0);
140 renderer_proxy_
->Stop();
142 for (int i
= 0; i
< kNumberOfRendererProxy
; ++i
) {
143 if (i
!= kNumberOfRendererProxy
-1) {
144 EXPECT_CALL(*mock_output_device_
, Stop()).Times(0);
146 // When the last proxy is stopped, the sink will stop.
147 EXPECT_CALL(*source_
.get(), RemoveAudioRenderer(renderer_
.get()));
148 EXPECT_CALL(*mock_output_device_
, Stop());
150 renderer_proxies_
[i
]->Stop();
154 } // namespace content