1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
content / browser / renderer_host / media / render_frame_audio_output_stream_factory_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/media/render_frame_audio_output_stream_factory.h"
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/unguessable_token.h"
#include "content/browser/media/forwarding_audio_stream_factory.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_renderer_host.h"
#include "media/audio/audio_system_impl.h"
#include "media/audio/fake_audio_log_factory.h"
#include "media/audio/fake_audio_manager.h"
#include "media/audio/test_audio_thread.h"
#include "media/base/audio_parameters.h"
#include "media/mojo/mojom/audio_output_stream.mojom.h"
#include "media/mojo/mojom/audio_stream_factory.mojom.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/audio/public/cpp/fake_stream_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Test;
using ::testing::Mock;
using ::testing::Ne;
using ::testing::StrictMock;
namespace content {
class RenderFrameAudioOutputStreamFactoryTest
: public RenderViewHostTestHarness {
public:
RenderFrameAudioOutputStreamFactoryTest()
: audio_manager_(std::make_unique<media::TestAudioThread>(),
&log_factory_),
audio_system_(media::AudioSystemImpl::CreateInstance()),
media_stream_manager_(
std::make_unique<MediaStreamManager>(audio_system_.get())) {}
~RenderFrameAudioOutputStreamFactoryTest() override {}
void SetUp() override {
RenderViewHostTestHarness::SetUp();
RenderFrameHostTester::For(main_rfh())->InitializeRenderFrameIfNeeded();
// Set up the ForwardingAudioStreamFactory.
ForwardingAudioStreamFactory::OverrideAudioStreamFactoryBinderForTesting(
base::BindRepeating(
&RenderFrameAudioOutputStreamFactoryTest::BindFactory,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void TearDown() override {
ForwardingAudioStreamFactory::OverrideAudioStreamFactoryBinderForTesting(
base::NullCallback());
audio_manager_.Shutdown();
RenderViewHostTestHarness::TearDown();
}
void BindFactory(
mojo::PendingReceiver<media::mojom::AudioStreamFactory> receiver) {
audio_service_stream_factory_.receiver_.Bind(std::move(receiver));
}
class MockStreamFactory : public audio::FakeStreamFactory {
public:
MockStreamFactory() {}
~MockStreamFactory() override {}
void CreateOutputStream(
mojo::PendingReceiver<media::mojom::AudioOutputStream> stream_receiver,
mojo::PendingAssociatedRemote<media::mojom::AudioOutputStreamObserver>
observer,
mojo::PendingRemote<media::mojom::AudioLog> log,
const std::string& output_device_id,
const media::AudioParameters& params,
const base::UnguessableToken& group_id,
CreateOutputStreamCallback created_callback) override {
last_created_callback_ = std::move(created_callback);
}
void CreateSwitchableOutputStream(
mojo::PendingReceiver<media::mojom::AudioOutputStream> stream,
mojo::PendingReceiver<media::mojom::DeviceSwitchInterface>
device_switch_receiver,
mojo::PendingAssociatedRemote<media::mojom::AudioOutputStreamObserver>
observer,
mojo::PendingRemote<media::mojom::AudioLog> log,
const std::string& device_id,
const media::AudioParameters& params,
const base::UnguessableToken& group_id,
CreateOutputStreamCallback created_callback) override {
last_created_callback_ = std::move(created_callback);
}
bool last_created_callback() const {
return !last_created_callback_.is_null();
}
private:
CreateOutputStreamCallback last_created_callback_;
};
using MockAuthorizationCallback = StrictMock<
base::MockCallback<base::OnceCallback<void(media::OutputDeviceStatus,
const media::AudioParameters&,
const std::string&)>>>;
const char* kDefaultDeviceId = "default";
const char* kDeviceId =
"111122223333444455556666777788889999aaaabbbbccccddddeeeeffff";
const media::AudioParameters kParams =
media::AudioParameters::UnavailableDeviceParams();
MockStreamFactory audio_service_stream_factory_;
media::FakeAudioLogFactory log_factory_;
media::FakeAudioManager audio_manager_;
std::unique_ptr<media::AudioSystem> audio_system_;
std::unique_ptr<MediaStreamManager> media_stream_manager_;
};
TEST_F(RenderFrameAudioOutputStreamFactoryTest, ConstructDestruct) {
mojo::Remote<blink::mojom::RendererAudioOutputStreamFactory> factory_remote;
RenderFrameAudioOutputStreamFactory factory(
main_rfh(), audio_system_.get(), media_stream_manager_.get(),
factory_remote.BindNewPipeAndPassReceiver());
}
TEST_F(RenderFrameAudioOutputStreamFactoryTest,
RequestDeviceAuthorizationForDefaultDevice_StatusOk) {
mojo::Remote<blink::mojom::RendererAudioOutputStreamFactory> factory_remote;
RenderFrameAudioOutputStreamFactory factory(
main_rfh(), audio_system_.get(), media_stream_manager_.get(),
factory_remote.BindNewPipeAndPassReceiver());
mojo::Remote<media::mojom::AudioOutputStreamProvider> provider_remote;
MockAuthorizationCallback mock_callback;
factory_remote->RequestDeviceAuthorization(
provider_remote.BindNewPipeAndPassReceiver(), std::nullopt,
kDefaultDeviceId, mock_callback.Get());
EXPECT_CALL(mock_callback,
Run(media::OUTPUT_DEVICE_STATUS_OK, _, std::string()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, factory.CurrentNumberOfProvidersForTesting());
}
TEST_F(
RenderFrameAudioOutputStreamFactoryTest,
RequestDeviceAuthorizationForDefaultDeviceAndDestroyProviderPtr_CleansUp) {
mojo::Remote<blink::mojom::RendererAudioOutputStreamFactory> factory_remote;
RenderFrameAudioOutputStreamFactory factory(
main_rfh(), audio_system_.get(), media_stream_manager_.get(),
factory_remote.BindNewPipeAndPassReceiver());
mojo::Remote<media::mojom::AudioOutputStreamProvider> provider_remote;
MockAuthorizationCallback mock_callback;
factory_remote->RequestDeviceAuthorization(
provider_remote.BindNewPipeAndPassReceiver(), std::nullopt,
kDefaultDeviceId, mock_callback.Get());
provider_remote.reset();
EXPECT_CALL(mock_callback,
Run(media::OUTPUT_DEVICE_STATUS_OK, _, std::string()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, factory.CurrentNumberOfProvidersForTesting());
}
TEST_F(
RenderFrameAudioOutputStreamFactoryTest,
RequestDeviceAuthorizationForNondefaultDeviceWithoutAuthorization_Fails) {
mojo::Remote<blink::mojom::RendererAudioOutputStreamFactory> factory_remote;
RenderFrameAudioOutputStreamFactory factory(
main_rfh(), audio_system_.get(), media_stream_manager_.get(),
factory_remote.BindNewPipeAndPassReceiver());
mojo::Remote<media::mojom::AudioOutputStreamProvider> provider_remote;
MockAuthorizationCallback mock_callback;
factory_remote->RequestDeviceAuthorization(
provider_remote.BindNewPipeAndPassReceiver(), std::nullopt, kDeviceId,
mock_callback.Get());
EXPECT_CALL(mock_callback,
Run(Ne(media::OUTPUT_DEVICE_STATUS_OK), _, std::string()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, factory.CurrentNumberOfProvidersForTesting());
}
TEST_F(RenderFrameAudioOutputStreamFactoryTest,
CreateStream_CreatesStreamAndFreesProvider) {
mojo::Remote<blink::mojom::RendererAudioOutputStreamFactory> factory_remote;
RenderFrameAudioOutputStreamFactory factory(
main_rfh(), audio_system_.get(), media_stream_manager_.get(),
factory_remote.BindNewPipeAndPassReceiver());
mojo::Remote<media::mojom::AudioOutputStreamProvider> provider_remote;
MockAuthorizationCallback mock_callback;
factory_remote->RequestDeviceAuthorization(
provider_remote.BindNewPipeAndPassReceiver(), std::nullopt,
kDefaultDeviceId, mock_callback.Get());
{
mojo::PendingRemote<media::mojom::AudioOutputStreamProviderClient> client;
std::ignore = client.InitWithNewPipeAndPassReceiver();
provider_remote->Acquire(kParams, std::move(client));
}
media::mojom::AudioStreamFactory::CreateOutputStreamCallback created_callback;
EXPECT_CALL(mock_callback,
Run(media::OUTPUT_DEVICE_STATUS_OK, _, std::string()));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(!!audio_service_stream_factory_.last_created_callback());
EXPECT_EQ(0u, factory.CurrentNumberOfProvidersForTesting());
}
TEST_F(RenderFrameAudioOutputStreamFactoryTest,
CreateStreamAfterFactoryDestruction_Fails) {
mojo::Remote<media::mojom::AudioOutputStreamProvider> provider_remote;
MockAuthorizationCallback mock_callback;
{
mojo::Remote<blink::mojom::RendererAudioOutputStreamFactory> factory_remote;
RenderFrameAudioOutputStreamFactory factory(
main_rfh(), audio_system_.get(), media_stream_manager_.get(),
factory_remote.BindNewPipeAndPassReceiver());
factory_remote->RequestDeviceAuthorization(
provider_remote.BindNewPipeAndPassReceiver(), std::nullopt,
kDefaultDeviceId, mock_callback.Get());
media::mojom::AudioStreamFactory::CreateOutputStreamCallback
created_callback;
EXPECT_CALL(mock_callback,
Run(media::OUTPUT_DEVICE_STATUS_OK, _, std::string()));
base::RunLoop().RunUntilIdle();
}
// Now factory is destructed. Trying to create a stream should fail.
{
mojo::PendingRemote<media::mojom::AudioOutputStreamProviderClient> client;
std::ignore = client.InitWithNewPipeAndPassReceiver();
provider_remote->Acquire(kParams, std::move(client));
}
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(!!audio_service_stream_factory_.last_created_callback());
}
} // namespace content