Add ICU message format support
[chromium-blink-merge.git] / content / renderer / media / webrtc_audio_renderer_unittest.cc
blobd94ac97520d93246f18af66ab56d881bd44e6ef6
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;
25 namespace content {
27 namespace {
29 const int kHardwareSampleRate = 44100;
30 const int kHardwareBufferSize = 512;
32 class MockAudioOutputIPC : public media::AudioOutputIPC {
33 public:
34 MockAudioOutputIPC() {}
35 virtual ~MockAudioOutputIPC() {}
37 MOCK_METHOD3(CreateStream, void(media::AudioOutputIPCDelegate* delegate,
38 const media::AudioParameters& params,
39 int session_id));
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,
47 int request_id));
50 class FakeAudioOutputDevice
51 : NON_EXPORTED_BASE(public media::AudioOutputDevice) {
52 public:
53 FakeAudioOutputDevice(
54 scoped_ptr<media::AudioOutputIPC> ipc,
55 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
56 : AudioOutputDevice(ipc.Pass(),
57 io_task_runner) {}
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_METHOD0(GetOutputDevice, media::OutputDevice*());
65 protected:
66 virtual ~FakeAudioOutputDevice() {}
69 class MockAudioDeviceFactory : public AudioDeviceFactory {
70 public:
71 MockAudioDeviceFactory() {}
72 virtual ~MockAudioDeviceFactory() {}
73 MOCK_METHOD1(CreateOutputDevice, media::AudioOutputDevice*(int));
74 MOCK_METHOD1(CreateInputDevice, media::AudioInputDevice*(int));
77 class MockAudioRendererSource : public WebRtcAudioRendererSource {
78 public:
79 MockAudioRendererSource() {}
80 virtual ~MockAudioRendererSource() {}
81 MOCK_METHOD4(RenderData, void(media::AudioBus* audio_bus,
82 int sample_rate,
83 int audio_delay_milliseconds,
84 base::TimeDelta* current_time));
85 MOCK_METHOD1(RemoveAudioRenderer, void(WebRtcAudioRenderer* renderer));
88 } // namespace
90 class WebRtcAudioRendererTest : public testing::Test {
91 protected:
92 WebRtcAudioRendererTest()
93 : message_loop_(new base::MessageLoopForIO),
94 mock_ipc_(new MockAudioOutputIPC()),
95 mock_output_device_(new FakeAudioOutputDevice(
96 scoped_ptr<media::AudioOutputIPC>(mock_ipc_),
97 message_loop_->task_runner())),
98 factory_(new MockAudioDeviceFactory()),
99 source_(new MockAudioRendererSource()),
100 stream_(new rtc::RefCountedObject<MockMediaStream>("label")),
101 renderer_(new WebRtcAudioRenderer(message_loop_->task_runner(),
102 stream_,
105 44100,
106 kHardwareBufferSize)) {
107 EXPECT_CALL(*factory_.get(), CreateOutputDevice(1))
108 .WillOnce(Return(mock_output_device_.get()));
109 EXPECT_CALL(*mock_output_device_.get(), Start());
110 EXPECT_TRUE(renderer_->Initialize(source_.get()));
111 renderer_proxy_ = renderer_->CreateSharedAudioRendererProxy(stream_);
114 // Used to construct |mock_output_device_|.
115 scoped_ptr<base::MessageLoopForIO> message_loop_;
116 MockAudioOutputIPC* mock_ipc_; // Owned by AudioOuputDevice.
118 scoped_refptr<FakeAudioOutputDevice> mock_output_device_;
119 scoped_ptr<MockAudioDeviceFactory> factory_;
120 scoped_ptr<MockAudioRendererSource> source_;
121 scoped_refptr<webrtc::MediaStreamInterface> stream_;
122 scoped_refptr<WebRtcAudioRenderer> renderer_;
123 scoped_refptr<MediaStreamAudioRenderer> renderer_proxy_;
126 // Verify that the renderer will be stopped if the only proxy is stopped.
127 TEST_F(WebRtcAudioRendererTest, StopRenderer) {
128 renderer_proxy_->Start();
130 // |renderer_| has only one proxy, stopping the proxy should stop the sink of
131 // |renderer_|.
132 EXPECT_CALL(*mock_output_device_.get(), Stop());
133 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
134 renderer_proxy_->Stop();
137 // Verify that the renderer will not be stopped unless the last proxy is
138 // stopped.
139 TEST_F(WebRtcAudioRendererTest, MultipleRenderers) {
140 renderer_proxy_->Start();
142 // Create a vector of renderer proxies from the |renderer_|.
143 std::vector<scoped_refptr<MediaStreamAudioRenderer> > renderer_proxies_;
144 static const int kNumberOfRendererProxy = 5;
145 for (int i = 0; i < kNumberOfRendererProxy; ++i) {
146 scoped_refptr<MediaStreamAudioRenderer> renderer_proxy(
147 renderer_->CreateSharedAudioRendererProxy(stream_));
148 renderer_proxy->Start();
149 renderer_proxies_.push_back(renderer_proxy);
152 // Stop the |renderer_proxy_| should not stop the sink since it is used by
153 // other proxies.
154 EXPECT_CALL(*mock_output_device_.get(), Stop()).Times(0);
155 renderer_proxy_->Stop();
157 for (int i = 0; i < kNumberOfRendererProxy; ++i) {
158 if (i != kNumberOfRendererProxy -1) {
159 EXPECT_CALL(*mock_output_device_.get(), Stop()).Times(0);
160 } else {
161 // When the last proxy is stopped, the sink will stop.
162 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
163 EXPECT_CALL(*mock_output_device_.get(), Stop());
165 renderer_proxies_[i]->Stop();
169 // Verify that the sink of the renderer is using the expected sample rate and
170 // buffer size.
171 TEST_F(WebRtcAudioRendererTest, VerifySinkParameters) {
172 renderer_proxy_->Start();
173 #if defined(OS_LINUX) || defined(OS_MACOSX)
174 static const int kExpectedBufferSize = kHardwareSampleRate / 100;
175 #elif defined(OS_ANDROID)
176 static const int kExpectedBufferSize = 2 * kHardwareSampleRate / 100;
177 #else
178 // Windows.
179 static const int kExpectedBufferSize = kHardwareBufferSize;
180 #endif
181 EXPECT_EQ(kExpectedBufferSize, renderer_->frames_per_buffer());
182 EXPECT_EQ(kHardwareSampleRate, renderer_->sample_rate());
183 EXPECT_EQ(2, renderer_->channels());
185 EXPECT_CALL(*mock_output_device_.get(), Stop());
186 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
187 renderer_proxy_->Stop();
190 } // namespace content