| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/webrtc_audio_renderer.h" | 5 #include "content/renderer/media/webrtc_audio_renderer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "content/public/renderer/media_stream_audio_renderer.h" | 14 #include "content/public/renderer/media_stream_audio_renderer.h" |
| 15 #include "content/renderer/media/audio_device_factory.h" | 15 #include "content/renderer/media/audio_device_factory.h" |
| 16 #include "content/renderer/media/audio_message_filter.h" | |
| 17 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.
h" | |
| 18 #include "content/renderer/media/webrtc_audio_device_impl.h" | 16 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 19 #include "media/audio/audio_output_device.h" | 17 #include "media/base/audio_renderer_sink.h" |
| 20 #include "media/audio/audio_output_ipc.h" | 18 #include "media/base/output_device.h" |
| 21 #include "media/base/audio_bus.h" | |
| 22 #include "media/base/mock_audio_renderer_sink.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "third_party/WebKit/public/platform/WebMediaStream.h" | 21 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 26 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 22 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 27 #include "third_party/WebKit/public/web/WebHeap.h" | 23 #include "third_party/WebKit/public/web/WebHeap.h" |
| 28 #include "third_party/webrtc/api/mediastreaminterface.h" | 24 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 29 | 25 |
| 30 using testing::Return; | 26 using testing::Return; |
| 31 using testing::_; | 27 using testing::_; |
| 32 | 28 |
| 33 namespace content { | 29 namespace content { |
| 34 | 30 |
| 35 namespace { | 31 namespace { |
| 36 | 32 |
| 37 const int kHardwareSampleRate = 44100; | 33 const int kHardwareSampleRate = 44100; |
| 38 const int kHardwareBufferSize = 512; | 34 const int kHardwareBufferSize = 512; |
| 39 const char kDefaultOutputDeviceId[] = ""; | 35 const char kDefaultOutputDeviceId[] = ""; |
| 40 const char kOtherOutputDeviceId[] = "other-output-device"; | 36 const char kOtherOutputDeviceId[] = "other-output-device"; |
| 41 const char kInvalidOutputDeviceId[] = "invalid-device"; | 37 const char kInvalidOutputDeviceId[] = "invalid-device"; |
| 42 | 38 |
| 43 class MockAudioOutputIPC : public media::AudioOutputIPC { | 39 class FakeAudioOutputDevice |
| 40 : NON_EXPORTED_BASE(public media::AudioRendererSink), |
| 41 NON_EXPORTED_BASE(public media::OutputDevice) { |
| 44 public: | 42 public: |
| 45 MockAudioOutputIPC() {} | 43 FakeAudioOutputDevice(const std::string& device_id) : device_id_(device_id) {} |
| 46 virtual ~MockAudioOutputIPC() {} | 44 MOCK_METHOD2(Initialize, |
| 47 | 45 void(const media::AudioParameters&, RenderCallback*)); |
| 48 MOCK_METHOD4(RequestDeviceAuthorization, | |
| 49 void(media::AudioOutputIPCDelegate* delegate, | |
| 50 int session_id, | |
| 51 const std::string& device_id, | |
| 52 const url::Origin& security_origin)); | |
| 53 MOCK_METHOD2(CreateStream, | |
| 54 void(media::AudioOutputIPCDelegate* delegate, | |
| 55 const media::AudioParameters& params)); | |
| 56 MOCK_METHOD0(PlayStream, void()); | |
| 57 MOCK_METHOD0(PauseStream, void()); | |
| 58 MOCK_METHOD0(CloseStream, void()); | |
| 59 MOCK_METHOD1(SetVolume, void(double volume)); | |
| 60 MOCK_METHOD2(SwitchOutputDevice, | |
| 61 void(const std::string& device_id, | |
| 62 const url::Origin& security_origin)); | |
| 63 }; | |
| 64 | |
| 65 class FakeAudioOutputDevice | |
| 66 : NON_EXPORTED_BASE(public media::AudioOutputDevice) { | |
| 67 public: | |
| 68 FakeAudioOutputDevice( | |
| 69 scoped_ptr<media::AudioOutputIPC> ipc, | |
| 70 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | |
| 71 const std::string& device_id) | |
| 72 : AudioOutputDevice(std::move(ipc), | |
| 73 io_task_runner, | |
| 74 0, | |
| 75 std::string(), | |
| 76 url::Origin()), | |
| 77 device_id_(device_id) {} | |
| 78 MOCK_METHOD0(Start, void()); | 46 MOCK_METHOD0(Start, void()); |
| 79 MOCK_METHOD0(Stop, void()); | 47 MOCK_METHOD0(Stop, void()); |
| 80 MOCK_METHOD0(Pause, void()); | 48 MOCK_METHOD0(Pause, void()); |
| 81 MOCK_METHOD0(Play, void()); | 49 MOCK_METHOD0(Play, void()); |
| 82 MOCK_METHOD1(SetVolume, bool(double volume)); | 50 MOCK_METHOD1(SetVolume, bool(double volume)); |
| 51 OutputDevice* GetOutputDevice() { return this; } |
| 52 MOCK_METHOD3(SwitchOutputDevice, |
| 53 void(const std::string&, |
| 54 const url::Origin&, |
| 55 const media::SwitchOutputDeviceCB&)); |
| 83 | 56 |
| 84 media::OutputDeviceStatus GetDeviceStatus() override { | 57 media::OutputDeviceStatus GetDeviceStatus() override { |
| 85 return device_id_ == kInvalidOutputDeviceId | 58 return device_id_ == kInvalidOutputDeviceId |
| 86 ? media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL | 59 ? media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL |
| 87 : media::OUTPUT_DEVICE_STATUS_OK; | 60 : media::OUTPUT_DEVICE_STATUS_OK; |
| 88 } | 61 } |
| 89 | 62 |
| 90 std::string GetDeviceId() const { return device_id_; } | 63 std::string GetDeviceId() const { return device_id_; } |
| 91 | 64 |
| 92 media::AudioParameters GetOutputParameters() override { | 65 media::AudioParameters GetOutputParameters() override { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 122 MOCK_METHOD1(MockSwitchDeviceCallback, void(media::OutputDeviceStatus)); | 95 MOCK_METHOD1(MockSwitchDeviceCallback, void(media::OutputDeviceStatus)); |
| 123 void SwitchDeviceCallback(base::RunLoop* loop, | 96 void SwitchDeviceCallback(base::RunLoop* loop, |
| 124 media::OutputDeviceStatus result) { | 97 media::OutputDeviceStatus result) { |
| 125 MockSwitchDeviceCallback(result); | 98 MockSwitchDeviceCallback(result); |
| 126 loop->Quit(); | 99 loop->Quit(); |
| 127 } | 100 } |
| 128 | 101 |
| 129 protected: | 102 protected: |
| 130 WebRtcAudioRendererTest() | 103 WebRtcAudioRendererTest() |
| 131 : message_loop_(new base::MessageLoopForIO), | 104 : message_loop_(new base::MessageLoopForIO), |
| 132 mock_ipc_(nullptr), | |
| 133 source_(new MockAudioRendererSource()) { | 105 source_(new MockAudioRendererSource()) { |
| 134 blink::WebVector<blink::WebMediaStreamTrack> dummy_tracks; | 106 blink::WebVector<blink::WebMediaStreamTrack> dummy_tracks; |
| 135 stream_.initialize("new stream", dummy_tracks, dummy_tracks); | 107 stream_.initialize("new stream", dummy_tracks, dummy_tracks); |
| 136 } | 108 } |
| 137 | 109 |
| 138 void SetupRenderer(const std::string& device_id) { | 110 void SetupRenderer(const std::string& device_id) { |
| 139 renderer_ = new WebRtcAudioRenderer(message_loop_->task_runner(), stream_, | 111 renderer_ = new WebRtcAudioRenderer(message_loop_->task_runner(), stream_, |
| 140 1, 1, device_id, url::Origin()); | 112 1, 1, device_id, url::Origin()); |
| 141 EXPECT_CALL(*this, MockCreateOutputDevice(1, _, device_id, _)); | 113 EXPECT_CALL( |
| 114 *this, MockCreateAudioRendererSink(AudioDeviceFactory::kSourceWebRTC, _, |
| 115 _, device_id, _)); |
| 142 EXPECT_TRUE(renderer_->Initialize(source_.get())); | 116 EXPECT_TRUE(renderer_->Initialize(source_.get())); |
| 143 | 117 |
| 144 renderer_proxy_ = renderer_->CreateSharedAudioRendererProxy(stream_); | 118 renderer_proxy_ = renderer_->CreateSharedAudioRendererProxy(stream_); |
| 145 } | 119 } |
| 146 | |
| 147 MOCK_METHOD1(CreateInputDevice, media::AudioInputDevice*(int)); | 120 MOCK_METHOD1(CreateInputDevice, media::AudioInputDevice*(int)); |
| 148 MOCK_METHOD4(MockCreateOutputDevice, | 121 MOCK_METHOD4(CreateOutputDevice, |
| 149 media::AudioOutputDevice*(int, | 122 media::AudioOutputDevice*(int, |
| 150 int, | 123 int, |
| 151 const std::string&, | 124 const std::string&, |
| 152 const url::Origin&)); | 125 const url::Origin&)); |
| 153 media::AudioOutputDevice* CreateOutputDevice( | 126 MOCK_METHOD5(CreateRestartableAudioRendererSink, |
| 127 media::RestartableAudioRendererSink*(SourceType, |
| 128 int, |
| 129 int, |
| 130 const std::string&, |
| 131 const url::Origin&)); |
| 132 MOCK_METHOD5(MockCreateAudioRendererSink, |
| 133 media::AudioRendererSink*(SourceType, |
| 134 int, |
| 135 int, |
| 136 const std::string&, |
| 137 const url::Origin&)); |
| 138 media::AudioRendererSink* CreateAudioRendererSink( |
| 139 SourceType source_type, |
| 154 int render_frame_id, | 140 int render_frame_id, |
| 155 int session_id, | 141 int session_id, |
| 156 const std::string& device_id, | 142 const std::string& device_id, |
| 157 const url::Origin& security_origin) { | 143 const url::Origin& security_origin) { |
| 158 MockAudioOutputIPC* fake_ipc = new MockAudioOutputIPC(); | 144 FakeAudioOutputDevice* fake_device = new FakeAudioOutputDevice(device_id); |
| 159 FakeAudioOutputDevice* fake_device = | |
| 160 new FakeAudioOutputDevice(scoped_ptr<media::AudioOutputIPC>(fake_ipc), | |
| 161 message_loop_->task_runner(), device_id); | |
| 162 if (device_id != kInvalidOutputDeviceId) { | 145 if (device_id != kInvalidOutputDeviceId) { |
| 163 mock_output_device_ = fake_device; | 146 mock_output_device_ = fake_device; |
| 164 mock_ipc_ = fake_ipc; | |
| 165 EXPECT_CALL(*mock_output_device_.get(), Start()); | 147 EXPECT_CALL(*mock_output_device_.get(), Start()); |
| 166 } | 148 } |
| 167 | 149 |
| 168 MockCreateOutputDevice(render_frame_id, session_id, device_id, | 150 MockCreateAudioRendererSink(source_type, render_frame_id, session_id, |
| 169 security_origin); | 151 device_id, security_origin); |
| 170 return fake_device; | 152 return fake_device; |
| 171 } | 153 } |
| 172 | 154 |
| 173 void TearDown() override { | 155 void TearDown() override { |
| 174 renderer_proxy_ = nullptr; | 156 renderer_proxy_ = nullptr; |
| 175 renderer_ = nullptr; | 157 renderer_ = nullptr; |
| 176 stream_.reset(); | 158 stream_.reset(); |
| 177 source_.reset(); | 159 source_.reset(); |
| 178 mock_output_device_ = nullptr; | 160 mock_output_device_ = nullptr; |
| 179 blink::WebHeap::collectAllGarbageForTesting(); | 161 blink::WebHeap::collectAllGarbageForTesting(); |
| 180 } | 162 } |
| 181 | 163 |
| 182 // Used to construct |mock_output_device_|. | |
| 183 scoped_ptr<base::MessageLoopForIO> message_loop_; | 164 scoped_ptr<base::MessageLoopForIO> message_loop_; |
| 184 MockAudioOutputIPC* mock_ipc_; // Owned by AudioOuputDevice. | |
| 185 | |
| 186 scoped_refptr<FakeAudioOutputDevice> mock_output_device_; | 165 scoped_refptr<FakeAudioOutputDevice> mock_output_device_; |
| 187 scoped_ptr<MockAudioRendererSource> source_; | 166 scoped_ptr<MockAudioRendererSource> source_; |
| 188 blink::WebMediaStream stream_; | 167 blink::WebMediaStream stream_; |
| 189 scoped_refptr<WebRtcAudioRenderer> renderer_; | 168 scoped_refptr<WebRtcAudioRenderer> renderer_; |
| 190 scoped_refptr<MediaStreamAudioRenderer> renderer_proxy_; | 169 scoped_refptr<MediaStreamAudioRenderer> renderer_proxy_; |
| 191 }; | 170 }; |
| 192 | 171 |
| 193 // Verify that the renderer will be stopped if the only proxy is stopped. | 172 // Verify that the renderer will be stopped if the only proxy is stopped. |
| 194 TEST_F(WebRtcAudioRendererTest, StopRenderer) { | 173 TEST_F(WebRtcAudioRendererTest, StopRenderer) { |
| 195 SetupRenderer(kDefaultOutputDeviceId); | 174 SetupRenderer(kDefaultOutputDeviceId); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); | 253 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); |
| 275 renderer_proxy_->Stop(); | 254 renderer_proxy_->Stop(); |
| 276 } | 255 } |
| 277 | 256 |
| 278 TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) { | 257 TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) { |
| 279 SetupRenderer(kDefaultOutputDeviceId); | 258 SetupRenderer(kDefaultOutputDeviceId); |
| 280 EXPECT_EQ(kDefaultOutputDeviceId, mock_output_device_->GetDeviceId()); | 259 EXPECT_EQ(kDefaultOutputDeviceId, mock_output_device_->GetDeviceId()); |
| 281 renderer_proxy_->Start(); | 260 renderer_proxy_->Start(); |
| 282 | 261 |
| 283 EXPECT_CALL(*mock_output_device_.get(), Stop()); | 262 EXPECT_CALL(*mock_output_device_.get(), Stop()); |
| 284 EXPECT_CALL(*this, MockCreateOutputDevice(_, _, kOtherOutputDeviceId, _)); | 263 EXPECT_CALL(*this, |
| 264 MockCreateAudioRendererSink(AudioDeviceFactory::kSourceWebRTC, _, |
| 265 _, kOtherOutputDeviceId, _)); |
| 285 EXPECT_CALL(*source_.get(), AudioRendererThreadStopped()); | 266 EXPECT_CALL(*source_.get(), AudioRendererThreadStopped()); |
| 286 EXPECT_CALL(*this, MockSwitchDeviceCallback(media::OUTPUT_DEVICE_STATUS_OK)); | 267 EXPECT_CALL(*this, MockSwitchDeviceCallback(media::OUTPUT_DEVICE_STATUS_OK)); |
| 287 base::RunLoop loop; | 268 base::RunLoop loop; |
| 288 renderer_proxy_->GetOutputDevice()->SwitchOutputDevice( | 269 renderer_proxy_->GetOutputDevice()->SwitchOutputDevice( |
| 289 kOtherOutputDeviceId, url::Origin(), | 270 kOtherOutputDeviceId, url::Origin(), |
| 290 base::Bind(&WebRtcAudioRendererTest::SwitchDeviceCallback, | 271 base::Bind(&WebRtcAudioRendererTest::SwitchDeviceCallback, |
| 291 base::Unretained(this), &loop)); | 272 base::Unretained(this), &loop)); |
| 292 loop.Run(); | 273 loop.Run(); |
| 293 EXPECT_EQ(kOtherOutputDeviceId, mock_output_device_->GetDeviceId()); | 274 EXPECT_EQ(kOtherOutputDeviceId, mock_output_device_->GetDeviceId()); |
| 294 | 275 |
| 295 EXPECT_CALL(*mock_output_device_.get(), Stop()); | 276 EXPECT_CALL(*mock_output_device_.get(), Stop()); |
| 296 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); | 277 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); |
| 297 renderer_proxy_->Stop(); | 278 renderer_proxy_->Stop(); |
| 298 } | 279 } |
| 299 | 280 |
| 300 TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceInvalidDevice) { | 281 TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceInvalidDevice) { |
| 301 SetupRenderer(kDefaultOutputDeviceId); | 282 SetupRenderer(kDefaultOutputDeviceId); |
| 302 EXPECT_EQ(kDefaultOutputDeviceId, mock_output_device_->GetDeviceId()); | 283 EXPECT_EQ(kDefaultOutputDeviceId, mock_output_device_->GetDeviceId()); |
| 303 renderer_proxy_->Start(); | 284 renderer_proxy_->Start(); |
| 304 | 285 |
| 305 EXPECT_CALL(*this, MockCreateOutputDevice(_, _, kInvalidOutputDeviceId, _)); | 286 EXPECT_CALL(*this, |
| 287 MockCreateAudioRendererSink(AudioDeviceFactory::kSourceWebRTC, _, |
| 288 _, kInvalidOutputDeviceId, _)); |
| 306 EXPECT_CALL(*this, MockSwitchDeviceCallback( | 289 EXPECT_CALL(*this, MockSwitchDeviceCallback( |
| 307 media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL)); | 290 media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL)); |
| 308 base::RunLoop loop; | 291 base::RunLoop loop; |
| 309 renderer_proxy_->GetOutputDevice()->SwitchOutputDevice( | 292 renderer_proxy_->GetOutputDevice()->SwitchOutputDevice( |
| 310 kInvalidOutputDeviceId, url::Origin(), | 293 kInvalidOutputDeviceId, url::Origin(), |
| 311 base::Bind(&WebRtcAudioRendererTest::SwitchDeviceCallback, | 294 base::Bind(&WebRtcAudioRendererTest::SwitchDeviceCallback, |
| 312 base::Unretained(this), &loop)); | 295 base::Unretained(this), &loop)); |
| 313 loop.Run(); | 296 loop.Run(); |
| 314 EXPECT_EQ(kDefaultOutputDeviceId, mock_output_device_->GetDeviceId()); | 297 EXPECT_EQ(kDefaultOutputDeviceId, mock_output_device_->GetDeviceId()); |
| 315 | 298 |
| 316 EXPECT_CALL(*mock_output_device_.get(), Stop()); | 299 EXPECT_CALL(*mock_output_device_.get(), Stop()); |
| 317 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); | 300 EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); |
| 318 renderer_proxy_->Stop(); | 301 renderer_proxy_->Stop(); |
| 319 } | 302 } |
| 320 | 303 |
| 321 } // namespace content | 304 } // namespace content |
| OLD | NEW |