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
|
/** @file
*
* VBox PulseAudio backend
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_AUDIO
#include <VBox/log.h>
#include <iprt/mem.h>
#include "pulse_mangling.h"
#include "pulse_stubs.h"
#include <pulse/pulseaudio.h>
#include "vl_vbox.h"
#include "audio.h"
#define AUDIO_CAP "pulse"
#include "audio_int.h"
#include <stdio.h>
#define MAX_LOG_REL_ERRORS 32
/*
* We use a g_pMainLoop in a separate thread g_pContext. We have to call functions for
* manipulating objects either from callback functions or we have to protect
* these functions by pa_threaded_mainloop_lock() / pa_threaded_mainloop_unlock().
*/
static struct pa_threaded_mainloop *g_pMainLoop;
static struct pa_context *g_pContext;
typedef struct PulseVoice
{
/** not accessed from within this context */
union
{
HWVoiceOut In;
HWVoiceIn Out;
} hw;
/** DAC buffer */
void *pPCMBuf;
/** Pulse stream */
pa_stream *pStream;
/** Pulse sample format and attribute specification */
pa_sample_spec SampleSpec;
/** Pulse playback and buffer metrics */
pa_buffer_attr BufAttr;
int fOpSuccess;
/** number of logged errors */
unsigned cErrors;
/** Pulse record peek buffer */
const uint8_t *pu8PeekBuf;
size_t cbPeekBuf;
size_t offPeekBuf;
pa_operation *pDrainOp;
} PulseVoice;
/* The desired buffer length in milliseconds. Will be the target total stream
* latency on newer version of pulse. Apparent latency can be less (or more.)
*/
static struct
{
int buffer_msecs_out;
int buffer_msecs_in;
} conf
=
{
INIT_FIELD (.buffer_msecs_out = ) 100,
INIT_FIELD (.buffer_msecs_in = ) 100,
};
static pa_sample_format_t aud_to_pulsefmt (audfmt_e fmt)
{
switch (fmt)
{
case AUD_FMT_U8:
return PA_SAMPLE_U8;
case AUD_FMT_S16:
return PA_SAMPLE_S16LE;
#ifdef PA_SAMPLE_S32LE
case AUD_FMT_S32:
return PA_SAMPLE_S32LE;
#endif
default:
dolog ("Bad audio format %d\n", fmt);
return PA_SAMPLE_U8;
}
}
static int pulse_to_audfmt (pa_sample_format_t pulsefmt, audfmt_e *fmt, int *endianess)
{
switch (pulsefmt)
{
case PA_SAMPLE_U8:
*endianess = 0;
*fmt = AUD_FMT_U8;
break;
case PA_SAMPLE_S16LE:
*fmt = AUD_FMT_S16;
*endianess = 0;
break;
case PA_SAMPLE_S16BE:
*fmt = AUD_FMT_S16;
*endianess = 1;
break;
#ifdef PA_SAMPLE_S32LE
case PA_SAMPLE_S32LE:
*fmt = AUD_FMT_S32;
*endianess = 0;
break;
#endif
#ifdef PA_SAMPLE_S32BE
case PA_SAMPLE_S32BE:
*fmt = AUD_FMT_S32;
*endianess = 1;
break;
#endif
default:
return -1;
}
return 0;
}
static void stream_success_callback(pa_stream *pStream, int fSuccess, void *userdata)
{
PulseVoice *pPulse = (PulseVoice *)userdata;
pPulse->fOpSuccess = fSuccess;
if (!fSuccess)
{
if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
{
int rc = pa_context_errno(g_pContext);
pPulse->cErrors++;
LogRel(("Pulse: Failed stream operation: %s\n", pa_strerror(rc)));
}
}
pa_threaded_mainloop_signal(g_pMainLoop, 0);
}
/**
* Synchronously wait until an operation completed.
*/
static int pulse_wait_for_operation (pa_operation *op)
{
if (op)
{
while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
pa_threaded_mainloop_wait(g_pMainLoop);
pa_operation_unref(op);
}
return 1;
}
/**
* Context status changed.
*/
static void context_state_callback(pa_context *pContext, void *userdata)
{
PulseVoice *pPulse = (PulseVoice *)userdata;
switch (pa_context_get_state(pContext))
{
case PA_CONTEXT_READY:
case PA_CONTEXT_TERMINATED:
pa_threaded_mainloop_signal(g_pMainLoop, 0);
break;
case PA_CONTEXT_FAILED:
LogRel(("Pulse: Audio input/output stopped!\n"));
if (pPulse)
pPulse->cErrors = MAX_LOG_REL_ERRORS;
pa_threaded_mainloop_signal(g_pMainLoop, 0);
break;
default:
break;
}
}
/**
* Stream status changed.
*/
static void stream_state_callback(pa_stream *pStream, void *userdata)
{
switch (pa_stream_get_state(pStream))
{
case PA_STREAM_READY:
case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED:
pa_threaded_mainloop_signal(g_pMainLoop, 0);
break;
default:
break;
}
}
/**
* Callback called when our pa_stream_drain operation was completed.
*/
static void stream_drain_callback(pa_stream *pStream, int fSuccess, void *userdata)
{
PulseVoice *pPulse = (PulseVoice *)userdata;
pPulse->fOpSuccess = fSuccess;
if (!fSuccess)
{
if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
{
int rc = pa_context_errno(g_pContext);
pPulse->cErrors++;
LogRel(("Pulse: Failed stream operation: %s\n", pa_strerror(rc)));
}
}
else
pa_operation_unref(pa_stream_cork(pStream, 1, stream_success_callback, userdata));
pa_operation_unref(pPulse->pDrainOp);
pPulse->pDrainOp = NULL;
}
static int pulse_open (int fIn, pa_stream **ppStream, pa_sample_spec *pSampleSpec,
pa_buffer_attr *pBufAttr)
{
const pa_buffer_attr *pBufAttrObtained;
pa_stream *pStream = NULL;
char achPCMName[64];
pa_stream_flags_t flags = 0;
const char *stream_name = audio_get_stream_name();
RTStrPrintf(achPCMName, sizeof(achPCMName), "%.32s%s%s%s",
stream_name ? stream_name : "",
stream_name ? " (" : "",
fIn ? "pcm_in" : "pcm_out",
stream_name ? ")" : "");
LogRel(("Pulse: open %s rate=%dHz channels=%d format=%s\n",
fIn ? "PCM_IN" : "PCM_OUT", pSampleSpec->rate, pSampleSpec->channels,
pa_sample_format_to_string(pSampleSpec->format)));
if (!pa_sample_spec_valid(pSampleSpec))
{
LogRel(("Pulse: Unsupported sample specification\n"));
goto fail;
}
pa_threaded_mainloop_lock(g_pMainLoop);
if (!(pStream = pa_stream_new(g_pContext, achPCMName, pSampleSpec, /*channel_map=*/NULL)))
{
LogRel(("Pulse: Cannot create stream %s\n", achPCMName));
goto unlock_and_fail;
}
pa_stream_set_state_callback(pStream, stream_state_callback, NULL);
#if PA_API_VERSION >= 12
/* XXX */
flags |= PA_STREAM_ADJUST_LATENCY;
#endif
#if 0
/* not applicable as we don't use pa_stream_get_latency() and pa_stream_get_time() */
flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
#endif
/* no input/output right away after the stream was started */
flags |= PA_STREAM_START_CORKED;
if (fIn)
{
LogRel(("Pulse: Requested record buffer attributes: maxlength=%d fragsize=%d\n",
pBufAttr->maxlength, pBufAttr->fragsize));
if (pa_stream_connect_record(pStream, /*dev=*/NULL, pBufAttr, flags) < 0)
{
LogRel(("Pulse: Cannot connect record stream: %s\n",
pa_strerror(pa_context_errno(g_pContext))));
goto disconnect_unlock_and_fail;
}
}
else
{
LogRel(("Pulse: Requested playback buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
if (pa_stream_connect_playback(pStream, /*dev=*/NULL, pBufAttr, flags,
/*cvolume=*/NULL, /*sync_stream=*/NULL) < 0)
{
LogRel(("Pulse: Cannot connect playback stream: %s\n",
pa_strerror(pa_context_errno(g_pContext))));
goto disconnect_unlock_and_fail;
}
}
/* Wait until the stream is ready */
for (;;)
{
pa_stream_state_t sstate;
pa_threaded_mainloop_wait(g_pMainLoop);
sstate = pa_stream_get_state(pStream);
if (sstate == PA_STREAM_READY)
break;
else if (sstate == PA_STREAM_FAILED || sstate == PA_STREAM_TERMINATED)
{
LogRel(("Pulse: Failed to initialize stream (state %d)\n", sstate));
goto disconnect_unlock_and_fail;
}
}
pBufAttrObtained = pa_stream_get_buffer_attr(pStream);
memcpy(pBufAttr, pBufAttrObtained, sizeof(pa_buffer_attr));
if (fIn)
{
LogRel(("Pulse: Obtained record buffer attributes: maxlength=%d fragsize=%d\n",
pBufAttr->maxlength, pBufAttr->fragsize));
}
else
{
LogRel(("Pulse: Obtained playback buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
}
pa_threaded_mainloop_unlock(g_pMainLoop);
*ppStream = pStream;
return 0;
disconnect_unlock_and_fail:
pa_stream_disconnect(pStream);
unlock_and_fail:
pa_threaded_mainloop_unlock(g_pMainLoop);
fail:
if (pStream)
pa_stream_unref(pStream);
*ppStream = NULL;
return -1;
}
static int pulse_init_out (HWVoiceOut *hw, audsettings_t *as)
{
PulseVoice *pPulse = (PulseVoice *) hw;
audsettings_t obt_as;
int cbBuf;
pPulse->pDrainOp = NULL;
pPulse->SampleSpec.format = aud_to_pulsefmt (as->fmt);
pPulse->SampleSpec.rate = as->freq;
pPulse->SampleSpec.channels = as->nchannels;
/* Note that setting maxlength to -1 does not work on PulseAudio servers
* older than 0.9.10. So use the suggested value of 3/2 of tlength */
pPulse->BufAttr.tlength = (pa_bytes_per_second(&pPulse->SampleSpec)
* conf.buffer_msecs_out) / 1000;
pPulse->BufAttr.maxlength = (pPulse->BufAttr.tlength * 3) / 2;
pPulse->BufAttr.prebuf = -1; /* Same as tlength */
pPulse->BufAttr.minreq = -1; /* Pulse should set something sensible for minreq on it's own */
/* Notice that the struct BufAttr is updated to the obtained values after this call */
if (pulse_open (0, &pPulse->pStream, &pPulse->SampleSpec, &pPulse->BufAttr))
return -1;
if (pulse_to_audfmt (pPulse->SampleSpec.format, &obt_as.fmt, &obt_as.endianness))
{
LogRel(("Pulse: Cannot find audio format %d\n", pPulse->SampleSpec.format));
return -1;
}
obt_as.freq = pPulse->SampleSpec.rate;
obt_as.nchannels = pPulse->SampleSpec.channels;
audio_pcm_init_info (&hw->info, &obt_as);
cbBuf = audio_MIN(pPulse->BufAttr.tlength * 2, pPulse->BufAttr.maxlength);
pPulse->pPCMBuf = RTMemAllocZ(cbBuf);
if (!pPulse->pPCMBuf)
{
LogRel(("Pulse: Could not allocate DAC buffer of %d bytes\n", cbBuf));
return -1;
}
/* Convert from bytes to frames (aka samples) */
hw->samples = cbBuf >> hw->info.shift;
return 0;
}
static void pulse_fini_out (HWVoiceOut *hw)
{
PulseVoice *pPulse = (PulseVoice *)hw;
if (pPulse->pStream)
{
pa_threaded_mainloop_lock(g_pMainLoop);
pa_stream_disconnect(pPulse->pStream);
pa_stream_unref(pPulse->pStream);
pa_threaded_mainloop_unlock(g_pMainLoop);
pPulse->pStream = NULL;
}
if (pPulse->pPCMBuf)
{
RTMemFree (pPulse->pPCMBuf);
pPulse->pPCMBuf = NULL;
}
}
static int pulse_run_out (HWVoiceOut *hw)
{
PulseVoice *pPulse = (PulseVoice *) hw;
int cFramesLive;
int cFramesWritten = 0;
int csSamples;
int cFramesToWrite;
int cFramesAvail;
size_t cbAvail;
size_t cbToWrite;
uint8_t *pu8Dst;
st_sample_t *psSrc;
cFramesLive = audio_pcm_hw_get_live_out (hw);
if (!cFramesLive)
return 0;
pa_threaded_mainloop_lock(g_pMainLoop);
cbAvail = pa_stream_writable_size (pPulse->pStream);
if (cbAvail == (size_t)-1)
{
if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
{
int rc = pa_context_errno(g_pContext);
pPulse->cErrors++;
LogRel(("Pulse: Failed to determine the writable size: %s\n",
pa_strerror(rc)));
}
goto unlock_and_exit;
}
cFramesAvail = cbAvail >> hw->info.shift; /* bytes => samples */
cFramesWritten = audio_MIN (cFramesLive, cFramesAvail);
csSamples = cFramesWritten;
while (csSamples)
{
/* split request at the end of our samples buffer */
cFramesToWrite = audio_MIN (csSamples, hw->samples - hw->rpos);
cbToWrite = cFramesToWrite << hw->info.shift;
psSrc = hw->mix_buf + hw->rpos;
pu8Dst = advance (pPulse->pPCMBuf, hw->rpos << hw->info.shift);
hw->clip (pu8Dst, psSrc, cFramesToWrite);
if (pa_stream_write (pPulse->pStream, pu8Dst, cbToWrite,
/*cleanup_callback=*/NULL, 0, PA_SEEK_RELATIVE) < 0)
{
LogRel(("Pulse: Failed to write %d samples: %s\n",
cFramesToWrite, pa_strerror(pa_context_errno(g_pContext))));
break;
}
hw->rpos = (hw->rpos + cFramesToWrite) % hw->samples;
csSamples -= cFramesToWrite;
}
unlock_and_exit:
pa_threaded_mainloop_unlock(g_pMainLoop);
return cFramesWritten;
}
static int pulse_write (SWVoiceOut *sw, void *buf, int len)
{
return audio_pcm_sw_write (sw, buf, len);
}
static int pulse_ctl_out (HWVoiceOut *hw, int cmd, ...)
{
PulseVoice *pPulse = (PulseVoice *) hw;
switch (cmd)
{
case VOICE_ENABLE:
/* Start audio output. */
pa_threaded_mainloop_lock(g_pMainLoop);
if ( pPulse->pDrainOp
&& pa_operation_get_state(pPulse->pDrainOp) != PA_OPERATION_DONE)
{
pa_operation_cancel(pPulse->pDrainOp);
pa_operation_unref(pPulse->pDrainOp);
pPulse->pDrainOp = NULL;
}
else
{
/* should return immediately */
pulse_wait_for_operation(pa_stream_cork(pPulse->pStream, 0,
stream_success_callback, pPulse));
}
pa_threaded_mainloop_unlock(g_pMainLoop);
break;
case VOICE_DISABLE:
/* Pause audio output (the Pause bit of the AC97 x_CR register is set).
* Note that we must return immediately from here! */
pa_threaded_mainloop_lock(g_pMainLoop);
if (!pPulse->pDrainOp)
{
/* should return immediately */
pulse_wait_for_operation(pa_stream_trigger(pPulse->pStream,
stream_success_callback, pPulse));
pPulse->pDrainOp = pa_stream_drain(pPulse->pStream,
stream_drain_callback, pPulse);
}
pa_threaded_mainloop_unlock(g_pMainLoop);
break;
default:
return -1;
}
return 0;
}
static int pulse_init_in (HWVoiceIn *hw, audsettings_t *as)
{
PulseVoice *pPulse = (PulseVoice *) hw;
audsettings_t obt_as;
pPulse->SampleSpec.format = aud_to_pulsefmt (as->fmt);
pPulse->SampleSpec.rate = as->freq;
pPulse->SampleSpec.channels = as->nchannels;
/* XXX check these values */
pPulse->BufAttr.fragsize = (pa_bytes_per_second(&pPulse->SampleSpec)
* conf.buffer_msecs_in) / 1000;
pPulse->BufAttr.maxlength = (pPulse->BufAttr.fragsize * 3) / 2;
/* Other members of pa_buffer_attr are ignored for record streams */
if (pulse_open (1, &pPulse->pStream, &pPulse->SampleSpec, &pPulse->BufAttr))
return -1;
if (pulse_to_audfmt (pPulse->SampleSpec.format, &obt_as.fmt, &obt_as.endianness))
{
LogRel(("Pulse: Cannot find audio format %d\n", pPulse->SampleSpec.format));
return -1;
}
obt_as.freq = pPulse->SampleSpec.rate;
obt_as.nchannels = pPulse->SampleSpec.channels;
audio_pcm_init_info (&hw->info, &obt_as);
hw->samples = audio_MIN(pPulse->BufAttr.fragsize * 10, pPulse->BufAttr.maxlength)
>> hw->info.shift;
pPulse->pu8PeekBuf = NULL;
return 0;
}
static void pulse_fini_in (HWVoiceIn *hw)
{
PulseVoice *pPulse = (PulseVoice *)hw;
if (pPulse->pStream)
{
pa_threaded_mainloop_lock(g_pMainLoop);
pa_stream_disconnect(pPulse->pStream);
pa_stream_unref(pPulse->pStream);
pa_threaded_mainloop_unlock(g_pMainLoop);
pPulse->pStream = NULL;
}
}
static int pulse_run_in (HWVoiceIn *hw)
{
PulseVoice *pPulse = (PulseVoice *) hw;
const int hwshift = hw->info.shift;
int cFramesRead = 0; /* total frames which have been read this call */
int cFramesAvail; /* total frames available from pulse at start of call */
int cFramesToRead; /* the largest amount we want/can get this call */
int cFramesToPeek; /* the largest amount we want/can get this peek */
/* We should only call pa_stream_readable_size() once and trust the first value */
pa_threaded_mainloop_lock(g_pMainLoop);
cFramesAvail = pa_stream_readable_size(pPulse->pStream) >> hwshift;
pa_threaded_mainloop_unlock(g_pMainLoop);
if (cFramesAvail == -1)
{
if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
{
int rc = pa_context_errno(g_pContext);
pPulse->cErrors++;
LogRel(("Pulse: Failed to determine the readable size: %s\n",
pa_strerror(rc)));
}
return 0;
}
/* If the buffer was not dropped last call, add what remains */
if (pPulse->pu8PeekBuf)
cFramesAvail += (pPulse->cbPeekBuf - pPulse->offPeekBuf) >> hwshift;
cFramesToRead = audio_MIN(cFramesAvail, hw->samples - audio_pcm_hw_get_live_in(hw));
for (; cFramesToRead; cFramesToRead -= cFramesToPeek)
{
/* If there is no data, do another peek */
if (!pPulse->pu8PeekBuf)
{
pa_threaded_mainloop_lock(g_pMainLoop);
pa_stream_peek(pPulse->pStream, (const void**)&pPulse->pu8PeekBuf, &pPulse->cbPeekBuf);
pa_threaded_mainloop_unlock(g_pMainLoop);
pPulse->offPeekBuf = 0;
if ( !pPulse->pu8PeekBuf
|| !pPulse->cbPeekBuf)
break;
}
cFramesToPeek = audio_MIN((signed)( pPulse->cbPeekBuf
- pPulse->offPeekBuf) >> hwshift,
cFramesToRead);
/* Check for wrapping around the buffer end */
if (hw->wpos + cFramesToPeek > hw->samples)
{
int cFramesDelta = hw->samples - hw->wpos;
hw->conv(hw->conv_buf + hw->wpos,
pPulse->pu8PeekBuf + pPulse->offPeekBuf,
cFramesDelta,
&nominal_volume);
hw->conv(hw->conv_buf,
pPulse->pu8PeekBuf + pPulse->offPeekBuf + (cFramesDelta << hwshift),
cFramesToPeek - cFramesDelta,
&nominal_volume);
}
else
{
hw->conv(hw->conv_buf + hw->wpos,
pPulse->pu8PeekBuf + pPulse->offPeekBuf,
cFramesToPeek,
&nominal_volume);
}
cFramesRead += cFramesToPeek;
hw->wpos = (hw->wpos + cFramesToPeek) % hw->samples;
pPulse->offPeekBuf += cFramesToPeek << hwshift;
/* If the buffer is done, drop it */
if (pPulse->offPeekBuf == pPulse->cbPeekBuf)
{
pa_threaded_mainloop_lock(g_pMainLoop);
pa_stream_drop(pPulse->pStream);
pa_threaded_mainloop_unlock(g_pMainLoop);
pPulse->pu8PeekBuf = NULL;
}
}
return cFramesRead;
}
static int pulse_read (SWVoiceIn *sw, void *buf, int size)
{
return audio_pcm_sw_read (sw, buf, size);
}
static int pulse_ctl_in (HWVoiceIn *hw, int cmd, ...)
{
PulseVoice *pPulse = (PulseVoice *)hw;
switch (cmd)
{
case VOICE_ENABLE:
pa_threaded_mainloop_lock(g_pMainLoop);
/* should return immediately */
pulse_wait_for_operation(pa_stream_cork(pPulse->pStream, 0,
stream_success_callback, pPulse));
pa_threaded_mainloop_unlock(g_pMainLoop);
break;
case VOICE_DISABLE:
pa_threaded_mainloop_lock(g_pMainLoop);
if (pPulse->pu8PeekBuf)
{
pa_stream_drop(pPulse->pStream);
pPulse->pu8PeekBuf = NULL;
}
/* should return immediately */
pulse_wait_for_operation(pa_stream_cork(pPulse->pStream, 1,
stream_success_callback, pPulse));
pa_threaded_mainloop_unlock(g_pMainLoop);
break;
default:
return -1;
}
return 0;
}
static void *pulse_audio_init (void)
{
int rc;
rc = audioLoadPulseLib();
if (RT_FAILURE(rc))
{
LogRel(("Pulse: Failed to load the PulseAudio shared library! Error %Rrc\n", rc));
return NULL;
}
if (!(g_pMainLoop = pa_threaded_mainloop_new()))
{
LogRel(("Pulse: Failed to allocate main loop: %s\n",
pa_strerror(pa_context_errno(g_pContext))));
goto fail;
}
if (!(g_pContext = pa_context_new(pa_threaded_mainloop_get_api(g_pMainLoop), "VBox")))
{
LogRel(("Pulse: Failed to allocate context: %s\n",
pa_strerror(pa_context_errno(g_pContext))));
goto fail;
}
if (pa_threaded_mainloop_start(g_pMainLoop) < 0)
{
LogRel(("Pulse: Failed to start threaded mainloop: %s\n",
pa_strerror(pa_context_errno(g_pContext))));
goto fail;
}
pa_context_set_state_callback(g_pContext, context_state_callback, NULL);
pa_threaded_mainloop_lock(g_pMainLoop);
if (pa_context_connect(g_pContext, /*server=*/NULL, 0, NULL) < 0)
{
LogRel(("Pulse: Failed to connect to server: %s\n",
pa_strerror(pa_context_errno(g_pContext))));
goto unlock_and_fail;
}
/* Wait until the g_pContext is ready */
for (;;)
{
pa_context_state_t cstate;
pa_threaded_mainloop_wait(g_pMainLoop);
cstate = pa_context_get_state(g_pContext);
if (cstate == PA_CONTEXT_READY)
break;
else if (cstate == PA_CONTEXT_TERMINATED || cstate == PA_CONTEXT_FAILED)
{
LogRel(("Pulse: Failed to initialize context (state %d)\n", cstate));
goto unlock_and_fail;
}
}
pa_threaded_mainloop_unlock(g_pMainLoop);
return &conf;
unlock_and_fail:
if (g_pMainLoop)
pa_threaded_mainloop_unlock(g_pMainLoop);
fail:
if (g_pMainLoop)
pa_threaded_mainloop_stop(g_pMainLoop);
if (g_pContext)
{
pa_context_disconnect(g_pContext);
pa_context_unref(g_pContext);
g_pContext = NULL;
}
if (g_pMainLoop)
{
pa_threaded_mainloop_free(g_pMainLoop);
g_pMainLoop = NULL;
}
return NULL;
}
static void pulse_audio_fini (void *opaque)
{
if (g_pMainLoop)
pa_threaded_mainloop_stop(g_pMainLoop);
if (g_pContext)
{
pa_context_disconnect(g_pContext);
pa_context_unref(g_pContext);
g_pContext = NULL;
}
if (g_pMainLoop)
{
pa_threaded_mainloop_free(g_pMainLoop);
g_pMainLoop = NULL;
}
(void) opaque;
}
static struct audio_option pulse_options[] =
{
{"DAC_MS", AUD_OPT_INT, &conf.buffer_msecs_out,
"DAC period size in milliseconds", NULL, 0},
{"ADC_MS", AUD_OPT_INT, &conf.buffer_msecs_in,
"ADC period size in milliseconds", NULL, 0},
{NULL, 0, NULL, NULL, NULL, 0}
};
static struct audio_pcm_ops pulse_pcm_ops =
{
pulse_init_out,
pulse_fini_out,
pulse_run_out,
pulse_write,
pulse_ctl_out,
pulse_init_in,
pulse_fini_in,
pulse_run_in,
pulse_read,
pulse_ctl_in
};
struct audio_driver pulse_audio_driver =
{
INIT_FIELD (name = ) "pulse",
INIT_FIELD (descr = ) "PulseAudio http://www.pulseaudio.org",
INIT_FIELD (options = ) pulse_options,
INIT_FIELD (init = ) pulse_audio_init,
INIT_FIELD (fini = ) pulse_audio_fini,
INIT_FIELD (pcm_ops = ) &pulse_pcm_ops,
INIT_FIELD (can_be_default = ) 1,
INIT_FIELD (max_voices_out = ) INT_MAX,
INIT_FIELD (max_voices_in = ) INT_MAX,
INIT_FIELD (voice_size_out = ) sizeof (PulseVoice),
INIT_FIELD (voice_size_in = ) sizeof (PulseVoice)
};
|