-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeMicWavPlayerLib.cpp
More file actions
516 lines (488 loc) · 15.1 KB
/
FakeMicWavPlayerLib.cpp
File metadata and controls
516 lines (488 loc) · 15.1 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include "FakeMicWavPlayerLib.h"
#include "FakeLib/FakeLib.h"
#include "FakeLib/CallBacks.h"
#include "FakeLib/FakeLibImplementation.h"
#include "FakeLib/FakeLibUtils.hpp"
#include "OggPlayer.h"
#include <utility>
#include <algorithm>
FakeLib FakeMicWavPlayer::fakeLib;
sink_infos_t FakeMicWavPlayer::fakeCombinedSink;
sink_infos_t FakeMicWavPlayer::sourceLoopbackSink;
sink_infos_t FakeMicWavPlayer::userVolumeControlSink;
void FakeMicWavPlayer::clean() {
using namespace FakeLibUtils;
// Looping until all combined sink modules are deleted (up to 10 times max)
for (int i = 0; i < 10; ++i) {
auto result = fakeLib
.clear_commands()
.get_module_list()
.run_commands();
auto module_list = extract<info_list<module_infos_t>>(result);
module_infos_t module;
try {
module = find_by_name(module_list, "module-combine-sink");
} catch (ObjectNotFoundError&) {
// Good, there is no more "module-null-sink" modules loaded
break;
}
std::cerr << "Deleting combined sink module\n";
// deleting the module
fakeLib
.clear_commands()
.unload_module(module.index)
.run_commands();
}
// Looping until all null sink modules are deleted (up to 10 times max)
for (int i = 0; i < 10; ++i) {
auto result = fakeLib
.clear_commands()
.get_module_list()
.run_commands();
auto module_list = extract<info_list<module_infos_t>>(result);
module_infos_t module;
try {
module = find_by_name(module_list, "module-null-sink");
} catch (ObjectNotFoundError&) {
// Good, there is no more "module-null-sink" modules loaded
break;
}
std::cerr << "Deleting null sink module\n";
// deleting the module
fakeLib
.clear_commands()
.unload_module(module.index)
.run_commands();
}
// Looping until all sourceLoopbackSinkName is deleted (up to 10 times max)
for (int i = 0; i < 10; ++i) {
auto result = fakeLib
.clear_commands()
.get_module_list()
.run_commands();
auto module_list = extract<info_list<module_infos_t>>(result);
module_infos_t module;
try {
module = find_by_name(module_list, "module-loopback");
} catch (ObjectNotFoundError&) {
// Good, there is no more "module-null-sink" modules loaded
break;
}
std::cerr << "Deleting loopback module\n";
// deleting the module
fakeLib
.clear_commands()
.unload_module(module.index)
.run_commands();
}
}
static bool load_module(
const std::string& module,
std::string args,
const std::string& description,
std::string name = ""
)
{
if (module == "module-null-sink"
|| module == "module-combine-sink"
) {
auto dev_desc = description;
std::replace(std::begin(dev_desc), std::end(dev_desc), ' ', '_');
std::replace(std::begin(dev_desc), std::end(dev_desc), '.', '-');
args += " sink_properties=device.description="+dev_desc;
}
std::cerr << "[log] Load " << module << " args for " << description << " : `" << args << "`\n";
using namespace FakeLibUtils;
auto result = FakeMicWavPlayer::fakeLib
.clear_commands()
.load_module(module,
args,
description)
.run_commands();
auto load_module_infos = extract<load_module_t>(result);
if (!load_module_infos.success) {
std::cerr << "Couldn't load " << description << ", exitting\n";
return true;
}
return false;
}
template<typename user_type>
static auto get_info_from_load_module_and_check(
const std::string& module,
const std::string& args,
const std::string& description,
const std::string& nameToCheck,
std::function<
decltype(std::declval<FakeLib>().run_commands())()
> fetch_info_list_func) {
using namespace FakeLibUtils;
auto result = fetch_info_list_func();
auto list = extract<info_list<user_type>>(result);
user_type info;
try {
info = find_by_name(list, nameToCheck);
} catch (ObjectNotFoundError&) {
// Doesn't exist, we create it with the give module and module args
if (load_module(
module,
args,
description,
nameToCheck
)) {
throw CantLoadModuleError();
}
result = fetch_info_list_func();
list = extract<info_list<user_type>>(result);
try {
info = find_by_name(list, nameToCheck);
} catch (ObjectNotFoundError&) {
// Still doesn't exist, exitting
std::cerr << "[error] Couldn't load the module " << module << " for " << description << '\n';
throw CantLoadModuleError();
}
}
return info;
}
static auto get_sink_from_load_module_and_check(
const std::string& module,
const std::string& args,
const std::string& description,
const std::string& nameToCheck) {
return get_info_from_load_module_and_check<sink_infos_t>(
module,
args,
description,
nameToCheck,
[]() {
return FakeMicWavPlayer::fakeLib
.clear_commands()
.get_sink_list()
.run_commands();
}
);
}
static auto get_source_from_load_module_and_check(
const std::string& module,
const std::string& args,
const std::string& description,
const std::string& nameToCheck) {
return get_info_from_load_module_and_check<source_infos_t>(
module,
args,
description,
nameToCheck,
[]() {
return FakeMicWavPlayer::fakeLib
.clear_commands()
.get_source_list()
.run_commands();
}
);
}
int FakeMicWavPlayer::init(
std::string source,
std::string combinedSlavesList,
std::string sourceProcessBinary)
{
using namespace FakeLibUtils;
// Placing default values
if (combinedSlavesList == "default")
combinedSlavesList = defaultCombinedSlavesList;
if (sourceProcessBinary == "default")
sourceProcessBinary = defaultSourceProcessBinary;
// Finding the source source output corresponding to the source process binary passed as argument
source_output_infos_t process_source_output;
info_list<source_output_infos_t> source_output_list;
try {
fakeLib.clear_commands();
fakeLib.get_source_output_list();
auto result = fakeLib.run_commands();
source_output_list = extract<info_list<source_output_infos_t>>(result);
process_source_output = find_by_process_binary(source_output_list, sourceProcessBinary);
} catch (ObjectNotFoundError&){
print_source_output_list(source_output_list);
std::cerr << "[error] Source output for `" << sourceProcessBinary << "` not found, exiting\n";
clean();
return 1;
}
// Creating ource LoopBack Mixer sink if doesn't exist
source_infos_t sourceLoopBackMixerMonitor;
try {
sourceLoopBackMixerMonitor = get_source_from_load_module_and_check(
"module-null-sink",
std::string("sink_name=")+sourceLoopBackMixerSinkName,
"The Null sink for source loopback mixer",
sourceLoopBackMixerMonitorName);
} catch (CantLoadModuleError&) {
auto result = fakeLib
.clear_commands()
.get_sink_list()
.run_commands();
auto sink_list = extract<info_list<sink_infos_t>>(result);
print_sink_list(sink_list);
clean();
return 1;
}
// Creating loopback sink if doesn't exist
try {
sourceLoopbackSink = get_sink_from_load_module_and_check(
"module-combine-sink",
std::string("sink_name=")+sourceLoopbackSinkName+std::string(" slaves=")+sourceLoopBackMixerSinkName,
"The Combined Sink for source loopback",
sourceLoopbackSinkName);
} catch (CantLoadModuleError&) {
clean();
return 1;
}
{
auto result = fakeLib
.clear_commands()
.get_source_list()
.run_commands();
auto list = extract<info_list<source_infos_t>>(result);
print_source_list(list);
}
if (load_module("module-loopback",
std::string("source=")+source+std::string(" sink=")+sourceLoopBackMixerSinkName,
"The source loopback module")) {
// Couldn't load the module
clean();
return 1;
}
// Creating user volume control sink if doesn't exist
try {
auto args = std::string("sink_name=")+userVolumeControlSinkName+std::string(" slaves=")+combinedSlavesList;
userVolumeControlSink = get_sink_from_load_module_and_check(
"module-combine-sink",
args,
"The Fake Sink to control the user volume",
userVolumeControlSinkName
);
} catch (CantLoadModuleError&) {
clean();
return 1;
}
// Creating fake combined sink if doesn't exist
try {
auto args = std::string("sink_name=")+fakeCombinedSinkName+std::string(" slaves=")+sourceLoopbackSinkName+std::string(",")+userVolumeControlSinkName;
fakeCombinedSink = get_sink_from_load_module_and_check(
"module-combine-sink",
args,
"The fake combined sink module",
fakeCombinedSinkName
);
} catch (CantLoadModuleError&) {
clean();
return 1;
}
std::cerr << "[log] Fake Combined Sink Index : " << fakeCombinedSink.index << '\n';
std::cerr << "[log] Source Loopback Index : " << sourceLoopbackSink.index << '\n';
std::cerr << "[log] User Volume Control Sink Index : " << userVolumeControlSink.index << '\n';
std::cerr << "[log] Source Loop Back Mixer Monitor Source Index : " << sourceLoopBackMixerMonitor.index << '\n';
// At this point the application is ready to play
// Setting the Fake Combined Sink volume to 100%
{
auto result = fakeLib
.clear_commands()
.get_sink_list()
.run_commands();
auto sink_list = extract<info_list<sink_infos_t>>(result);
auto sink = find_by_name(sink_list, fakeCombinedSinkName);
fakeLib
.clear_commands()
.set_sink_volume(sink.index, 100.0)
.run_commands();
}
// Setting the Source Loopback Sink volume to 100%
{
auto result = fakeLib
.clear_commands()
.get_sink_list()
.run_commands();
auto sink_list = extract<info_list<sink_infos_t>>(result);
auto sink = find_by_name(sink_list, sourceLoopbackSinkName);
fakeLib
.clear_commands()
.set_sink_volume(sink.index, 100.0)
.run_commands();
}
// Setting the Source Loopback Mixer Sink volume to 100%
{
auto result = fakeLib
.clear_commands()
.get_sink_list()
.run_commands();
auto sink_list = extract<info_list<sink_infos_t>>(result);
auto sink = find_by_name(sink_list, sourceLoopBackMixerSinkName);
fakeLib
.clear_commands()
.set_sink_volume(sink.index, 100.0)
.run_commands();
}
// Setting the User Volume Control Sink volume to 9%
{
auto result = fakeLib
.clear_commands()
.get_sink_list()
.run_commands();
auto sink_list = extract<info_list<sink_infos_t>>(result);
auto sink = find_by_name(sink_list, userVolumeControlSinkName);
fakeLib
.clear_commands()
.set_sink_volume(sink.index, 0.1)
.run_commands();
}
// Move the found source output port to the fake combined monitor
{
auto result = fakeLib
.clear_commands()
.move_source_output_port(process_source_output.index, sourceLoopBackMixerMonitor.index)
.run_commands();
move_source_output_port_t move_source_output_port_result;
try {
move_source_output_port_result = extract<move_source_output_port_t>(result);
if (move_source_output_port_result.success == 0) {
throw ObjectNotFoundError();
}
} catch (ObjectNotFoundError&) {
std::cerr << "[error] couldn't move process source output port to monitor source at " << sourceLoopBackMixerMonitor.index << ", exiting\n";
clean();
return 1;
}
if (move_source_output_port_result.success == 0) {
std::cerr << "[error] couldn't move process source output port to monitor source at " << sourceLoopBackMixerMonitor.index << ", exiting\n";
clean();
return 1;
}
std::cerr << "[log] Successfully moved the source output of " << sourceProcessBinary << " to " << sourceLoopBackMixerMonitor.index << '\n';
}
return 0;
}
int FakeMicWavPlayer::initWithAudioFile(const std::string& fileName,
std::string source,
std::string combinedSlavesList,
std::string sourceProcessBinary)
{
using namespace FakeLibUtils;
// Init
if (init(source, combinedSlavesList, sourceProcessBinary) != 0) {
return 1;
}
// Now we need to play
std::cerr << "Initializing...\n";
if (OggPlayer::init(fileName, fakeCombinedSinkName) != 0)
return 1;
std::cerr << "Initialized. Playing\n";
// Setting volume of Ogg Player
{
auto result = fakeLib
.clear_commands()
.get_sink_input_list()
.run_commands();
auto sink_input_list = extract<info_list<sink_input_infos_t>>(result);
try {
auto fakePlayerSinkInput = find_by_name(sink_input_list, "Playing Music with Fake");
fakeLib
.clear_commands()
.set_sink_input_volume(fakePlayerSinkInput.index, 100.0) // Setting volume to 100%
.run_commands();
} catch (ObjectNotFoundError&) {
std::cerr << "[error] Couldn't found the Ogg Player, things might not work\n";
}
}
return 0;
}
int FakeMicWavPlayer::initWithSinkInput(const std::string& sinkInputName,
std::string source,
std::string combinedSlavesList,
std::string sourceProcessBinary)
{
using namespace FakeLibUtils;
sink_input_infos_t sink_input;
{
auto result = fakeLib
.clear_commands()
.get_sink_input_list()
.run_commands();
auto sink_input_list = extract<info_list<sink_input_infos_t>>(result);
// Finding sink input
try {
sink_input = find_by_process_binary(sink_input_list, sinkInputName);
} catch (ObjectNotFoundError&) {
std::cerr << "[error] Sink Input `" << sinkInputName << "` not found, exitting\n";
return 1;
}
}
std::cerr << "[log] Sink Input `" << sinkInputName << "` Index : " << sink_input.index << '\n';
// Init
if (init(source, combinedSlavesList, sourceProcessBinary) != 0) {
return 1;
}
// Move application audio stream to the fake combined sink
{
auto result = fakeLib
.clear_commands()
.move_sink_input_port(sink_input.index, fakeCombinedSink.index)
.run_commands();
try {
auto move_result = extract<move_sink_input_port_t>(result);
if (move_result.success == 0) {
throw ObjectNotFoundError();
}
} catch (ObjectNotFoundError&) {
std::cerr << "[error] couldn't move `" << sinkInputName << "` to fakeCombinedSink, exitting\n";
clean();
return 1;
}
}
{
auto result = fakeLib
.clear_commands()
.get_sink_input_list()
.run_commands();
auto sink_input_list = extract<info_list<sink_input_infos_t>>(result);
sink_input_infos_t sink_input;
try {
sink_input = find_by_process_binary(sink_input_list, sinkInputName);
} catch (ObjectNotFoundError&) {
// Should not happen because it has already been found above...
std::cerr << "[error] Sink Input `" << sinkInputName << "` not found, exitting\n";
return 1;
}
if (sink_input.sink != fakeCombinedSink.index) {
std::cerr << "[error] Failed to move Sink Input `" << sinkInputName << "` of Index: " << sink_input.index << " to Fake Combined Sink of Index: " << fakeCombinedSink.index << '\n';
print_sink_input_list(sink_input_list);
return 1;
}
}
std::cerr << "[log] Successfully moved the application `" << sinkInputName << " to the fake combined sink" << '\n';
return 0;
}
int FakeMicWavPlayer::playNonBlocking() {
return OggPlayer::playNonBlocking();
}
int FakeMicWavPlayer::cleanPlayer() {
return OggPlayer::clean();
}
int FakeMicWavPlayer::set_source_volume(double volume) {
if (volume < 0.0)
throw NegativeVolumeError();
if (volume > 200.0)
throw TooHighVolumeError();
fakeLib
.clear_commands()
.set_sink_volume(sourceLoopbackSink.index, volume)
.run_commands();
return 0;
}
int FakeMicWavPlayer::set_user_volume(double volume) {
if (volume < 0.0)
throw NegativeVolumeError();
if (volume > 200.0)
throw TooHighVolumeError();
fakeLib
.clear_commands()
.set_sink_volume(userVolumeControlSink.index, volume)
.run_commands();
return 0;
}