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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
|
// Copyright 2005-2016 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "mumble_pch.hpp"
#include "PulseAudio.h"
#include "Global.h"
#include "MainWindow.h"
#include "Timer.h"
#include "User.h"
static const char *mumble_sink_input = "Mumble Speakers";
static const char *mumble_echo = "Mumble Speakers (Echo)";
static PulseAudioSystem *pasys = NULL;
#define NBLOCKS 8
class PulseAudioInputRegistrar : public AudioInputRegistrar {
public:
PulseAudioInputRegistrar();
virtual AudioInput *create();
virtual const QList<audioDevice> getDeviceChoices();
virtual void setDeviceChoice(const QVariant &, Settings &);
virtual bool canEcho(const QString &) const;
};
class PulseAudioOutputRegistrar : public AudioOutputRegistrar {
public:
PulseAudioOutputRegistrar();
virtual AudioOutput *create();
virtual const QList<audioDevice> getDeviceChoices();
virtual void setDeviceChoice(const QVariant &, Settings &);
bool canMuteOthers() const;
};
class PulseAudioInit : public DeferInit {
public:
PulseAudioInputRegistrar *airPulseAudio;
PulseAudioOutputRegistrar *aorPulseAudio;
void initialize() {
pasys = new PulseAudioSystem();
pasys->qmWait.lock();
pasys->qwcWait.wait(&pasys->qmWait, 1000);
pasys->qmWait.unlock();
if (pasys->bPulseIsGood) {
airPulseAudio = new PulseAudioInputRegistrar();
aorPulseAudio = new PulseAudioOutputRegistrar();
} else {
airPulseAudio = NULL;
aorPulseAudio = NULL;
delete pasys;
pasys = NULL;
}
};
void destroy() {
delete airPulseAudio;
delete aorPulseAudio;
delete pasys;
pasys = NULL;
};
};
static PulseAudioInit pulseinit;
PulseAudioSystem::PulseAudioSystem() {
pasInput = pasOutput = pasSpeaker = NULL;
bSourceDone=bSinkDone=bServerDone = false;
iDelayCache = 0;
bPositionalCache = false;
bAttenuating = false;
iRemainingOperations = 0;
bPulseIsGood = false;
iSinkId = -1;
pam = pa_threaded_mainloop_new();
pa_mainloop_api *api = pa_threaded_mainloop_get_api(pam);
pa_proplist *proplist;
proplist = pa_proplist_new();
pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, "Mumble");
pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, "net.sourceforge.mumble.mumble");
pa_proplist_sets(proplist, PA_PROP_APPLICATION_ICON_NAME, "mumble");
pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, "game");
pacContext = pa_context_new_with_proplist(api, NULL, proplist);
pa_proplist_free(proplist);
pa_context_set_subscribe_callback(pacContext, subscribe_callback, this);
pa_context_set_state_callback(pacContext, context_state_callback, this);
pa_context_connect(pacContext, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
pade = api->defer_new(api, defer_event_callback, this);
api->defer_enable(pade, false);
pa_threaded_mainloop_start(pam);
bRunning = true;
}
PulseAudioSystem::~PulseAudioSystem() {
bRunning = false;
if (bAttenuating) {
qmWait.lock();
bAttenuating = false;
setVolumes();
bool success = qwcWait.wait(&qmWait, 1000);
if (! success) {
qWarning("PulseAudio: Shutdown timeout when attempting to restore volumes.");
}
qmWait.unlock();
}
pa_threaded_mainloop_stop(pam);
pa_context_disconnect(pacContext);
pa_context_unref(pacContext);
pa_threaded_mainloop_free(pam);
}
QString PulseAudioSystem::outputDevice() const {
QString odev = g.s.qsPulseAudioOutput;
if (odev.isEmpty()) {
odev = qsDefaultOutput;
}
if (!qhOutput.contains(odev)) {
odev = qsDefaultOutput;
}
return odev;
}
QString PulseAudioSystem::inputDevice() const {
QString idev = g.s.qsPulseAudioInput;
if (idev.isEmpty()) {
idev = qsDefaultInput;
}
if (!qhInput.contains(idev)) {
idev = qsDefaultInput;
}
return idev;
}
void PulseAudioSystem::wakeup() {
pa_mainloop_api *api = pa_threaded_mainloop_get_api(pam);
api->defer_enable(pade, true);
}
void PulseAudioSystem::wakeup_lock() {
pa_threaded_mainloop_lock(pam);
pa_mainloop_api *api = pa_threaded_mainloop_get_api(pam);
api->defer_enable(pade, true);
pa_threaded_mainloop_unlock(pam);
}
void PulseAudioSystem::defer_event_callback(pa_mainloop_api *a, pa_defer_event *e, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
pas->eventCallback(a, e);
}
void PulseAudioSystem::eventCallback(pa_mainloop_api *api, pa_defer_event *) {
api->defer_enable(pade, false);
if (! bSourceDone || ! bSinkDone || ! bServerDone)
return;
AudioInputPtr ai = g.ai;
AudioOutputPtr ao = g.ao;
AudioInput *raw_ai = ai.data();
AudioOutput *raw_ao = ao.data();
PulseAudioInput *pai = dynamic_cast<PulseAudioInput *>(raw_ai);
PulseAudioOutput *pao = dynamic_cast<PulseAudioOutput *>(raw_ao);
if (raw_ao) {
QString odev = outputDevice();
pa_stream_state ost = pasOutput ? pa_stream_get_state(pasOutput) : PA_STREAM_TERMINATED;
bool do_stop = false;
bool do_start = false;
if (! pao && (ost == PA_STREAM_READY)) {
do_stop = true;
} else if (pao) {
switch (ost) {
case PA_STREAM_TERMINATED: {
if (pasOutput)
pa_stream_unref(pasOutput);
pa_sample_spec pss = qhSpecMap.value(odev);
pa_channel_map pcm = qhChanMap.value(odev);
if ((pss.format != PA_SAMPLE_FLOAT32NE) && (pss.format != PA_SAMPLE_S16NE))
pss.format = PA_SAMPLE_FLOAT32NE;
if (pss.rate == 0)
pss.rate = SAMPLE_RATE;
if ((pss.channels == 0) || (! g.s.doPositionalAudio()))
pss.channels = 1;
pasOutput = pa_stream_new(pacContext, mumble_sink_input, &pss, (pss.channels == 1) ? NULL : &pcm);
pa_stream_set_state_callback(pasOutput, stream_callback, this);
pa_stream_set_write_callback(pasOutput, write_callback, this);
}
case PA_STREAM_UNCONNECTED:
do_start = true;
break;
case PA_STREAM_READY: {
if (g.s.iOutputDelay != iDelayCache) {
do_stop = true;
} else if (g.s.doPositionalAudio() != bPositionalCache) {
do_stop = true;
} else if (odev != qsOutputCache) {
do_stop = true;
}
break;
}
default:
break;
}
}
if (do_stop) {
qWarning("PulseAudio: Stopping output");
pa_stream_disconnect(pasOutput);
iSinkId = -1;
} else if (do_start) {
qWarning("PulseAudio: Starting output: %s", qPrintable(odev));
pa_buffer_attr buff;
const pa_sample_spec *pss = pa_stream_get_sample_spec(pasOutput);
const size_t sampleSize = (pss->format == PA_SAMPLE_FLOAT32NE) ? sizeof(float) : sizeof(short);
const unsigned int iBlockLen = ((pao->iFrameSize * pss->rate) / SAMPLE_RATE) * pss->channels * static_cast<unsigned int>(sampleSize);
buff.tlength = iBlockLen * (g.s.iOutputDelay+1);
buff.minreq = iBlockLen;
buff.maxlength = -1;
buff.prebuf = -1;
buff.fragsize = iBlockLen;
iDelayCache = g.s.iOutputDelay;
bPositionalCache = g.s.doPositionalAudio();
qsOutputCache = odev;
pa_stream_connect_playback(pasOutput, qPrintable(odev), &buff, PA_STREAM_ADJUST_LATENCY, NULL, NULL);
pa_context_get_sink_info_by_name(pacContext, qPrintable(odev), sink_info_callback, this);
}
}
if (raw_ai) {
QString idev = inputDevice();
pa_stream_state ist = pasInput ? pa_stream_get_state(pasInput) : PA_STREAM_TERMINATED;
bool do_stop = false;
bool do_start = false;
if (! pai && (ist == PA_STREAM_READY)) {
do_stop = true;
} else if (pai) {
switch (ist) {
case PA_STREAM_TERMINATED: {
if (pasInput)
pa_stream_unref(pasInput);
pa_sample_spec pss = qhSpecMap.value(idev);
if ((pss.format != PA_SAMPLE_FLOAT32NE) && (pss.format != PA_SAMPLE_S16NE))
pss.format = PA_SAMPLE_FLOAT32NE;
if (pss.rate == 0)
pss.rate = SAMPLE_RATE;
pss.channels = 1;
pasInput = pa_stream_new(pacContext, "Microphone", &pss, NULL);
pa_stream_set_state_callback(pasInput, stream_callback, this);
pa_stream_set_read_callback(pasInput, read_callback, this);
}
case PA_STREAM_UNCONNECTED:
do_start = true;
break;
case PA_STREAM_READY: {
if (idev != qsInputCache) {
do_stop = true;
}
break;
}
default:
break;
}
}
if (do_stop) {
qWarning("PulseAudio: Stopping input");
pa_stream_disconnect(pasInput);
} else if (do_start) {
qWarning("PulseAudio: Starting input %s",qPrintable(idev));
pa_buffer_attr buff;
const pa_sample_spec *pss = pa_stream_get_sample_spec(pasInput);
const size_t sampleSize = (pss->format == PA_SAMPLE_FLOAT32NE) ? sizeof(float) : sizeof(short);
const unsigned int iBlockLen = ((pai->iFrameSize * pss->rate) / SAMPLE_RATE) * pss->channels * static_cast<unsigned int>(sampleSize);
buff.tlength = iBlockLen;
buff.minreq = iBlockLen;
buff.maxlength = -1;
buff.prebuf = -1;
buff.fragsize = iBlockLen;
qsInputCache = idev;
pa_stream_connect_record(pasInput, qPrintable(idev), &buff, PA_STREAM_ADJUST_LATENCY);
}
}
if (raw_ai) {
QString odev = outputDevice();
QString edev = qhEchoMap.value(odev);
pa_stream_state est = pasSpeaker ? pa_stream_get_state(pasSpeaker) : PA_STREAM_TERMINATED;
bool do_stop = false;
bool do_start = false;
if ((! pai || ! g.s.doEcho()) && (est == PA_STREAM_READY)) {
do_stop = true;
} else if (pai && g.s.doEcho()) {
switch (est) {
case PA_STREAM_TERMINATED: {
if (pasSpeaker)
pa_stream_unref(pasSpeaker);
pa_sample_spec pss = qhSpecMap.value(edev);
pa_channel_map pcm = qhChanMap.value(edev);
if ((pss.format != PA_SAMPLE_FLOAT32NE) && (pss.format != PA_SAMPLE_S16NE))
pss.format = PA_SAMPLE_FLOAT32NE;
if (pss.rate == 0)
pss.rate = SAMPLE_RATE;
if ((pss.channels == 0) || (! g.s.bEchoMulti))
pss.channels = 1;
pasSpeaker = pa_stream_new(pacContext, mumble_echo, &pss, (pss.channels == 1) ? NULL : &pcm);
pa_stream_set_state_callback(pasSpeaker, stream_callback, this);
pa_stream_set_read_callback(pasSpeaker, read_callback, this);
}
case PA_STREAM_UNCONNECTED:
do_start = true;
break;
case PA_STREAM_READY: {
if (g.s.bEchoMulti != bEchoMultiCache) {
do_stop = true;
} else if (edev != qsEchoCache) {
do_stop = true;
}
break;
}
default:
break;
}
}
if (do_stop) {
qWarning("PulseAudio: Stopping echo");
pa_stream_disconnect(pasSpeaker);
} else if (do_start) {
qWarning("PulseAudio: Starting echo: %s", qPrintable(edev));
pa_buffer_attr buff;
const pa_sample_spec *pss = pa_stream_get_sample_spec(pasSpeaker);
const size_t sampleSize = (pss->format == PA_SAMPLE_FLOAT32NE) ? sizeof(float) : sizeof(short);
const unsigned int iBlockLen = ((pai->iFrameSize * pss->rate) / SAMPLE_RATE) * pss->channels * static_cast<unsigned int>(sampleSize);
buff.tlength = iBlockLen;
buff.minreq = iBlockLen;
buff.maxlength = -1;
buff.prebuf = -1;
buff.fragsize = iBlockLen;
bEchoMultiCache = g.s.bEchoMulti;
qsEchoCache = edev;
pa_stream_connect_record(pasSpeaker, qPrintable(edev), &buff, PA_STREAM_ADJUST_LATENCY);
}
}
}
void PulseAudioSystem::context_state_callback(pa_context *c, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
pas->contextCallback(c);
}
void PulseAudioSystem::subscribe_callback(pa_context *, pa_subscription_event_type evt, unsigned int, void *userdata) {
switch (evt & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
case PA_SUBSCRIPTION_EVENT_NEW:
case PA_SUBSCRIPTION_EVENT_REMOVE:
break;
default:
return;
}
switch (evt & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
case PA_SUBSCRIPTION_EVENT_SINK:
case PA_SUBSCRIPTION_EVENT_SOURCE:
break;
default:
return;
}
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
qWarning("PulseAudio: Sinks or inputs changed (inserted or removed sound card)");
pas->query();
}
void PulseAudioSystem::sink_callback(pa_context *, const pa_sink_info *i, int eol, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
if (!i || eol) {
pas->bSinkDone = true;
pas->wakeup();
return;
}
const QString name = QLatin1String(i->name);
pas->qhSpecMap.insert(name, i->sample_spec);
pas->qhChanMap.insert(name, i->channel_map);
pas->qhOutput.insert(name, QLatin1String(i->description));
pas->qhEchoMap.insert(name, QLatin1String(i->monitor_source_name));
}
void PulseAudioSystem::source_callback(pa_context *, const pa_source_info *i, int eol, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
if (!i || eol) {
pas->bSourceDone = true;
pas->wakeup();
return;
}
const QString name = QLatin1String(i->name);
pas->qhSpecMap.insert(name, i->sample_spec);
pas->qhChanMap.insert(name, i->channel_map);
pas->qhInput.insert(QLatin1String(i->name), QLatin1String(i->description));
}
void PulseAudioSystem::server_callback(pa_context *, const pa_server_info *i, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
pas->qsDefaultInput = QLatin1String(i->default_source_name);
pas->qsDefaultOutput = QLatin1String(i->default_sink_name);
pas->bServerDone = true;
pas->wakeup();
}
void PulseAudioSystem::sink_info_callback(pa_context *, const pa_sink_info *i, int eol, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
if (!i || eol) {
return;
}
pas->iSinkId = i->index;
}
void PulseAudioSystem::stream_callback(pa_stream *s, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
switch (pa_stream_get_state(s)) {
case PA_STREAM_FAILED:
qWarning("PulseAudio: Stream error: %s", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
break;
default:
break;
}
pas->wakeup();
}
void PulseAudioSystem::read_callback(pa_stream *s, size_t bytes, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
size_t length = bytes;
const void *data = NULL;
pa_stream_peek(s, &data, &length);
if (data == NULL && length > 0) {
qWarning("PulseAudio: pa_stream_peek reports no data at current read index.");
} else if (data == NULL && length == 0) {
qWarning("PulseAudio: pa_stream_peek reports empty memblockq.");
} else if (data == NULL || length == 0) {
qWarning("PulseAudio: invalid pa_stream_peek state encountered.");
return;
}
AudioInputPtr ai = g.ai;
PulseAudioInput *pai = dynamic_cast<PulseAudioInput *>(ai.data());
if (! pai) {
if (length > 0) {
pa_stream_drop(s);
}
pas->wakeup();
return;
}
const pa_sample_spec *pss = pa_stream_get_sample_spec(s);
if (s == pas->pasInput) {
if (!pa_sample_spec_equal(pss, &pai->pssMic)) {
pai->pssMic = *pss;
pai->iMicFreq = pss->rate;
pai->iMicChannels = pss->channels;
if (pss->format == PA_SAMPLE_FLOAT32NE)
pai->eMicFormat = PulseAudioInput::SampleFloat;
else
pai->eMicFormat = PulseAudioInput::SampleShort;
pai->initializeMixer();
}
if (data != NULL) {
pai->addMic(data, static_cast<unsigned int>(length) / pai->iMicSampleSize);
}
} else if (s == pas->pasSpeaker) {
if (!pa_sample_spec_equal(pss, &pai->pssEcho)) {
pai->pssEcho = *pss;
pai->iEchoFreq = pss->rate;
pai->iEchoChannels = pss->channels;
if (pss->format == PA_SAMPLE_FLOAT32NE)
pai->eEchoFormat = PulseAudioInput::SampleFloat;
else
pai->eEchoFormat = PulseAudioInput::SampleShort;
pai->initializeMixer();
}
if (data != NULL) {
pai->addEcho(data, static_cast<unsigned int>(length) / pai->iEchoSampleSize);
}
}
if (length > 0) {
pa_stream_drop(s);
}
}
void PulseAudioSystem::write_callback(pa_stream *s, size_t bytes, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
Q_ASSERT(s == pas->pasOutput);
AudioOutputPtr ao = g.ao;
PulseAudioOutput *pao = dynamic_cast<PulseAudioOutput *>(ao.data());
unsigned char buffer[bytes];
if (! pao) {
// Transitioning, but most likely transitions back, so just zero.
memset(buffer, 0, bytes);
pa_stream_write(s, buffer, bytes, NULL, 0, PA_SEEK_RELATIVE);
pas->wakeup();
return;
}
const pa_sample_spec *pss = pa_stream_get_sample_spec(s);
const pa_channel_map *pcm = pa_stream_get_channel_map(pas->pasOutput);
if (!pa_sample_spec_equal(pss, &pao->pss) || !pa_channel_map_equal(pcm, &pao->pcm)) {
pao->pss = *pss;
pao->pcm = *pcm;
if (pss->format == PA_SAMPLE_FLOAT32NE)
pao->eSampleFormat = PulseAudioOutput::SampleFloat;
else
pao->eSampleFormat = PulseAudioOutput::SampleShort;
pao->iMixerFreq = pss->rate;
pao->iChannels = pss->channels;
unsigned int chanmasks[pss->channels];
for (int i=0;i<pss->channels;++i) {
unsigned int cm = 0;
switch (pcm->map[i]) {
case PA_CHANNEL_POSITION_LEFT:
cm = SPEAKER_FRONT_LEFT;
break;
case PA_CHANNEL_POSITION_RIGHT:
cm = SPEAKER_FRONT_RIGHT;
break;
case PA_CHANNEL_POSITION_CENTER:
cm = SPEAKER_FRONT_CENTER;
break;
case PA_CHANNEL_POSITION_REAR_LEFT:
cm = SPEAKER_BACK_LEFT;
break;
case PA_CHANNEL_POSITION_REAR_RIGHT:
cm = SPEAKER_BACK_RIGHT;
break;
case PA_CHANNEL_POSITION_REAR_CENTER:
cm = SPEAKER_BACK_CENTER;
break;
case PA_CHANNEL_POSITION_LFE:
cm = SPEAKER_LOW_FREQUENCY;
break;
case PA_CHANNEL_POSITION_SIDE_LEFT:
cm = SPEAKER_SIDE_LEFT;
break;
case PA_CHANNEL_POSITION_SIDE_RIGHT:
cm = SPEAKER_SIDE_RIGHT;
break;
case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
cm = SPEAKER_FRONT_LEFT_OF_CENTER;
break;
case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
cm = SPEAKER_FRONT_RIGHT_OF_CENTER;
break;
default:
cm = 0;
break;
}
chanmasks[i] = cm;
}
pao->initializeMixer(chanmasks);
}
const unsigned int iSampleSize = pao->iSampleSize;
const unsigned int samples = static_cast<unsigned int>(bytes) / iSampleSize;
bool oldAttenuation = pas->bAttenuating;
// do we have some mixed output?
if (pao->mix(buffer, samples)) {
// attenuate if instructed to or it's in settings
pas->bAttenuating = (g.bAttenuateOthers || g.s.bAttenuateOthers);
} else {
memset(buffer, 0, bytes);
// attenuate if intructed to (self-activated)
pas->bAttenuating = g.bAttenuateOthers;
}
// if the attenuation state has changed
if (oldAttenuation != pas->bAttenuating) {
pas->setVolumes();
}
pa_stream_write(s, buffer, iSampleSize * samples, NULL, 0, PA_SEEK_RELATIVE);
}
void PulseAudioSystem::volume_sink_input_list_callback(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
if (eol == 0) {
// If we're using the default of "enable attenuation on all ouputs" and output from an application is loopbacked,
// both the loopback and the application will be attenuated leading to double attenuation.
if (!g.s.bOnlyAttenuateSameOutput && pas->iSinkId > -1 && !strcmp(i->driver, "module-loopback.c")) {
return;
}
// If we're not attenuating different sinks and the input is not on this sink, don't attenuate. Or,
// if the input is a loopback module and connected to Mumble's sink, also ignore it (loopbacks are used to connect
// sinks). An attenuated loopback means an indirect application attenuation.
if (g.s.bOnlyAttenuateSameOutput && pas->iSinkId > -1) {
if (int(i->sink) != pas->iSinkId || (int(i->sink) == pas->iSinkId && !strcmp(i->driver, "module-loopback.c") && !g.s.bAttenuateLoopbacks)) {
return;
}
}
// ensure we're not attenuating ourselves!
if (strcmp(i->name, mumble_sink_input) != 0) {
// create a new entry
PulseAttenuation patt;
patt.index = i->index;
patt.name = QLatin1String(i->name);
patt.stream_restore_id = QLatin1String(pa_proplist_gets(i->proplist, "module-stream-restore.id"));
patt.normal_volume = i->volume;
// calculate the attenuated volume
pa_volume_t adj = static_cast<pa_volume_t>(PA_VOLUME_NORM * g.s.fOtherVolume);
pa_sw_cvolume_multiply_scalar(&patt.attenuated_volume, &i->volume, adj);
// set it on the sink input
pa_operation_unref(pa_context_set_sink_input_volume(c, i->index, &patt.attenuated_volume, NULL, NULL));
// store it
pas->qhVolumes[i->index] = patt;
}
} else if (eol < 0) {
qWarning("PulseAudio: Sink input introspection error.");
}
}
void PulseAudioSystem::restore_sink_input_list_callback(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
if (eol == 0) {
// if we were tracking this specific sink previously
if (pas->qhVolumes.contains(i->index)) {
// and if it has the attenuated volume we applied to it
if (pa_cvolume_equal(&i->volume, &pas->qhVolumes[i->index].attenuated_volume) != 0) {
// mark it as matched
pas->qlMatchedSinks.append(i->index);
// reset the volume to normal
pas->iRemainingOperations++;
pa_operation_unref(pa_context_set_sink_input_volume(c, i->index, &pas->qhVolumes[i->index].normal_volume, restore_volume_success_callback, pas));
}
// otherwise, save for matching at the end of iteration
} else {
QString restore_id = QLatin1String(pa_proplist_gets(i->proplist, "module-stream-restore.id"));
PulseAttenuation patt;
patt.index = i->index;
patt.normal_volume = i->volume;
pas->qhUnmatchedSinks[restore_id] = patt;
}
} else if (eol < 0) {
qWarning("PulseAudio: Sink input introspection error.");
} else {
// build a list of missing streams by iterating our active list
QHash<uint32_t, PulseAttenuation>::const_iterator it;
for (it = pas->qhVolumes.constBegin(); it != pas->qhVolumes.constEnd(); ++it) {
// skip if previously matched
if (pas->qlMatchedSinks.contains(it.key())) {
continue;
}
// check if the restore id matches. the only case where this would
// happen is if the application was reopened during attenuation.
if (pas->qhUnmatchedSinks.contains(it.value().stream_restore_id)) {
PulseAttenuation active_sink = pas->qhUnmatchedSinks[it.value().stream_restore_id];
// if the volume wasn't changed from our attenuation
if (pa_cvolume_equal(&active_sink.normal_volume, &it.value().attenuated_volume) != 0) {
// reset the volume to normal
pas->iRemainingOperations++;
pa_operation_unref(pa_context_set_sink_input_volume(c, active_sink.index, &it.value().normal_volume, restore_volume_success_callback, pas));
}
continue;
}
// at this point, we don't know what happened to the sink. add
// it to a list to check the stream restore database for.
pas->qhMissingSinks[it.value().stream_restore_id] = it.value();
}
// clean up
pas->qlMatchedSinks.clear();
pas->qhUnmatchedSinks.clear();
pas->qhVolumes.clear();
// if we had missing sinks, check the stream restore database
// to see if we can find and update them.
if (pas->qhMissingSinks.count() > 0) {
pas->iRemainingOperations++;
pa_operation_unref(pa_ext_stream_restore_read(c, stream_restore_read_callback, pas));
}
// trigger the volume completion callback;
// necessary so that shutdown actions are called
restore_volume_success_callback(c, 1, pas);
}
}
void PulseAudioSystem::stream_restore_read_callback(pa_context *c, const pa_ext_stream_restore_info *i, int eol, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
if (eol == 0) {
QString name = QLatin1String(i->name);
// were we looking for this restoration?
if (pas->qhMissingSinks.contains(name)) {
// make sure it still has the volume we gave it
if (pa_cvolume_equal(&pas->qhMissingSinks[name].attenuated_volume, &i->volume) != 0) {
// update the stream restore record
pa_ext_stream_restore_info restore = *i;
restore.volume = pas->qhMissingSinks[name].normal_volume;
pas->iRemainingOperations++;
pa_operation_unref(pa_ext_stream_restore_write(c, PA_UPDATE_REPLACE, &restore, 1, 1, restore_volume_success_callback, pas));
}
pas->qhMissingSinks.remove(name);
}
} else if (eol < 0) {
qWarning("PulseAudio: Couldn't read stream restore database.");
pas->qhMissingSinks.clear();
} else {
// verify missing list is empty
if (pas->qhMissingSinks.count() > 0) {
qWarning("PulseAudio: Failed to match %d stream(s).", pas->qhMissingSinks.count());
pas->qhMissingSinks.clear();
}
// trigger the volume completion callback;
// necessary so that shutdown actions are called
restore_volume_success_callback(c, 1, pas);
}
}
void PulseAudioSystem::restore_volume_success_callback(pa_context *, int, void *userdata) {
PulseAudioSystem *pas = reinterpret_cast<PulseAudioSystem *>(userdata);
pas->iRemainingOperations--;
// if there are no more pending volume adjustments and we're shutting down,
// let the main thread know
if (! pas->bRunning && pas->iRemainingOperations == 0) {
pas->qwcWait.wakeAll();
}
}
void PulseAudioSystem::query() {
bSourceDone=bSinkDone=bServerDone = false;
qhInput.clear();
qhOutput.clear();
qhEchoMap.clear();
qhSpecMap.clear();
qhChanMap.clear();
qhInput.insert(QString(), tr("Default Input"));
qhOutput.insert(QString(), tr("Default Output"));
pa_operation_unref(pa_context_get_server_info(pacContext, server_callback, this));
pa_operation_unref(pa_context_get_sink_info_list(pacContext, sink_callback, this));
pa_operation_unref(pa_context_get_source_info_list(pacContext, source_callback, this));
wakeup();
}
void PulseAudioSystem::setVolumes() {
// set attenuation state and volumes
if (bAttenuating) {
// ensure the volume map is empty, otherwise it may be dangerous to change
if (qhVolumes.empty()) {
// set the new per-application volumes and store the old ones
pa_operation_unref(pa_context_get_sink_input_info_list(pacContext, volume_sink_input_list_callback, this));
}
// clear attenuation state and restore normal volumes
} else {
iRemainingOperations++;
pa_operation_unref(pa_context_get_sink_input_info_list(pacContext, restore_sink_input_list_callback, this));
}
}
void PulseAudioSystem::contextCallback(pa_context *c) {
Q_ASSERT(c == pacContext);
switch (pa_context_get_state(c)) {
case PA_CONTEXT_READY:
bPulseIsGood = true;
pa_operation_unref(pa_context_subscribe(pacContext, PA_SUBSCRIPTION_MASK_SOURCE, NULL, this));
pa_operation_unref(pa_context_subscribe(pacContext, PA_SUBSCRIPTION_MASK_SINK, NULL, this));
query();
break;
case PA_CONTEXT_TERMINATED:
qWarning("PulseAudio: Forcibly disconnected from PulseAudio");
break;
case PA_CONTEXT_FAILED:
qWarning("PulseAudio: Connection failure: %s", pa_strerror(pa_context_errno(c)));
break;
default:
return;
}
qmWait.lock();
qwcWait.wakeAll();
qmWait.unlock();
}
PulseAudioInputRegistrar::PulseAudioInputRegistrar() : AudioInputRegistrar(QLatin1String("PulseAudio"), 10) {
}
AudioInput *PulseAudioInputRegistrar::create() {
return new PulseAudioInput();
}
const QList<audioDevice> PulseAudioInputRegistrar::getDeviceChoices() {
QList<audioDevice> qlReturn;
QStringList qlInputDevs = pasys->qhInput.keys();
qSort(qlInputDevs);
if (qlInputDevs.contains(g.s.qsPulseAudioInput)) {
qlInputDevs.removeAll(g.s.qsPulseAudioInput);
qlInputDevs.prepend(g.s.qsPulseAudioInput);
}
foreach(const QString &dev, qlInputDevs) {
qlReturn << audioDevice(pasys->qhInput.value(dev), dev);
}
return qlReturn;
}
void PulseAudioInputRegistrar::setDeviceChoice(const QVariant &choice, Settings &s) {
s.qsPulseAudioInput = choice.toString();
}
bool PulseAudioInputRegistrar::canEcho(const QString &osys) const {
return (osys == name);
}
PulseAudioOutputRegistrar::PulseAudioOutputRegistrar() : AudioOutputRegistrar(QLatin1String("PulseAudio"), 10) {
}
AudioOutput *PulseAudioOutputRegistrar::create() {
return new PulseAudioOutput();
}
const QList<audioDevice> PulseAudioOutputRegistrar::getDeviceChoices() {
QList<audioDevice> qlReturn;
QStringList qlOutputDevs = pasys->qhOutput.keys();
qSort(qlOutputDevs);
if (qlOutputDevs.contains(g.s.qsPulseAudioOutput)) {
qlOutputDevs.removeAll(g.s.qsPulseAudioOutput);
qlOutputDevs.prepend(g.s.qsPulseAudioOutput);
}
foreach(const QString &dev, qlOutputDevs) {
qlReturn << audioDevice(pasys->qhOutput.value(dev), dev);
}
return qlReturn;
}
void PulseAudioOutputRegistrar::setDeviceChoice(const QVariant &choice, Settings &s) {
s.qsPulseAudioOutput = choice.toString();
}
bool PulseAudioOutputRegistrar::canMuteOthers() const {
return true;
}
PulseAudioInput::PulseAudioInput() {
memset(&pssMic, 0, sizeof(pssMic));
memset(&pssEcho, 0, sizeof(pssEcho));
bRunning = true;
if (pasys)
pasys->wakeup_lock();
}
PulseAudioInput::~PulseAudioInput() {
bRunning = false;
qmMutex.lock();
qwcWait.wakeAll();
qmMutex.unlock();
wait();
if (pasys)
pasys->wakeup_lock();
}
void PulseAudioInput::run() {
qmMutex.lock();
while (bRunning)
qwcWait.wait(&qmMutex);
qmMutex.unlock();
}
PulseAudioOutput::PulseAudioOutput() {
memset(&pss, 0, sizeof(pss));
memset(&pcm, 0, sizeof(pcm));
bRunning = true;
if (pasys)
pasys->wakeup_lock();
}
PulseAudioOutput::~PulseAudioOutput() {
bRunning = false;
qmMutex.lock();
qwcWait.wakeAll();
qmMutex.unlock();
wait();
if (pasys)
pasys->wakeup_lock();
}
void PulseAudioOutput::run() {
qmMutex.lock();
while (bRunning)
qwcWait.wait(&qmMutex);
qmMutex.unlock();
}
|