-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibasound_module_pcm_cdsp.c
1531 lines (1360 loc) · 47.7 KB
/
libasound_module_pcm_cdsp.c
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CamillaDSP ALSA "IO" Plugin (Maybe O plugin as it is output only)
//
// Based on bluealsa-pcm.c from the project bluez-alsa
// which is Copyright (c) 2016-2020 Arkadiusz Bokowy
//
// This project is licensed under the terms of the MIT license.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/eventfd.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <stdint.h>
#include <limits.h>
#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>
#include "strrep.h"
#define DEBUG 1
#define error(fmt, ...) \
do { if(DEBUG > 0){fprintf(stderr,"CDSP Plugin ERROR: ");\
fprintf(stderr,((fmt)), ##__VA_ARGS__);} } while (0)
#define warn(fmt, ...) \
do { if(DEBUG > 1){fprintf(stderr,"CDSP Plugin WARN: ");\
fprintf(stderr,((fmt)), ##__VA_ARGS__);} } while (0)
#define info(fmt, ...) \
do { if(DEBUG > 2){fprintf(stderr,"CDSP Plugin INFO: ");\
fprintf(stderr,((fmt)), ##__VA_ARGS__);} } while (0)
#define debug(fmt, ...) \
do { if(DEBUG > 3){fprintf(stderr,"CDSP Plugin DEBUG: ");\
fprintf(stderr,((fmt)), ##__VA_ARGS__);} } while (0)
#define excessive(fmt, ...) \
do { if(DEBUG > 4){fprintf(stderr,((fmt)), ##__VA_ARGS__);} } while (0)
#define CDSP_PAUSE_STATE_RUNNING 0
#define CDSP_PAUSE_STATE_PAUSED (1 << 0)
#define CDSP_PAUSE_STATE_PENDING (1 << 1)
// Cleanup callback casting wrapper for the brevity's sake.
#define PTHREAD_CLEANUP(f) ((void (*)(void *))(void (*)(void))(f))
// Thread routing callback casting wrapper.
#define PTHREAD_ROUTINE(f) ((void *(*)(void *))(f))
//
// Get system monotonic time-stamp.
//
// Why try for RAW? We're trying to simulate an accurate clock. Let
// ntp correct the rate.
//
// @param ts Address to the timespec structure where the time-stamp will
// be stored.
// @return On success this function returns 0. Otherwise, -1 is returned
// and errno is set to indicate the error.
//#ifdef CLOCK_MONOTONIC_RAW
//# define gettimestamp(ts) clock_gettime(CLOCK_MONOTONIC_RAW, ts)
//#else
# define gettimestamp(ts) clock_gettime(CLOCK_MONOTONIC, ts)
//#endif
// Calculate time difference for two time points.
//
// @param ts1 Address to the timespec structure providing t1 time point.
// @param ts2 Address to the timespec structure providing t2 time point.
// @param ts Address to the timespec structure where the absolute time
// difference will be stored.
// @return This function returns an integer less than, equal to, or greater
// than zero, if t2 time point is found to be, respectively, less than,
// equal to, or greater than the t1 time point.*/
int difftimespec( const struct timespec *ts1, const struct timespec *ts2,
struct timespec *ts) {
const struct timespec _ts1 = *ts1;
const struct timespec _ts2 = *ts2;
if (_ts1.tv_sec == _ts2.tv_sec) {
ts->tv_sec = 0;
ts->tv_nsec = labs(_ts2.tv_nsec - _ts1.tv_nsec);
return _ts2.tv_nsec > _ts1.tv_nsec ? 1 : -ts->tv_nsec;
}
if (_ts1.tv_sec < _ts2.tv_sec) {
if (_ts1.tv_nsec <= _ts2.tv_nsec) {
ts->tv_sec = _ts2.tv_sec - _ts1.tv_sec;
ts->tv_nsec = _ts2.tv_nsec - _ts1.tv_nsec;
}
else {
ts->tv_sec = _ts2.tv_sec - 1 - _ts1.tv_sec;
ts->tv_nsec = _ts2.tv_nsec + 1000000000 - _ts1.tv_nsec;
}
return 1;
}
if (_ts1.tv_nsec >= _ts2.tv_nsec) {
ts->tv_sec = _ts1.tv_sec - _ts2.tv_sec;
ts->tv_nsec = _ts1.tv_nsec - _ts2.tv_nsec;
}
else {
ts->tv_sec = _ts1.tv_sec - 1 - _ts2.tv_sec;
ts->tv_nsec = _ts1.tv_nsec + 1000000000 - _ts2.tv_nsec;
}
return -1;
}
typedef struct {
snd_pcm_ioplug_t io;
// IO thread and application thread sync
pthread_mutex_t mutex;
// Pipe to talk to CamillaDSP
int cdsp_pcm_fd;
// event file descriptor
int event_fd;
// virtual hardware - ring buffer
char *io_hw_buffer;
// The IO thread is responsible for maintaining the hardware pointer
// (pcm->io_hw_ptr), the application is responsible for the application
// pointer (io->appl_ptr). These are both volatile as they are both
// written in one thread and read in the other.
volatile snd_pcm_uframes_t io_hw_ptr;
// A signed value for the status return to the IO plugin pointer call
volatile int io_status;
snd_pcm_uframes_t io_hw_boundary;
// Permit the application to modify the frequency of poll() events.
volatile snd_pcm_uframes_t io_avail_min;
pthread_t io_thread;
bool io_started;
// ALSA operates on frames, we on bytes
size_t frame_size;
struct timespec delay_ts;
snd_pcm_uframes_t delay_hw_ptr;
unsigned int delay_pcm_nread;
// In the capture mode, delay_running indicates that frames are being
// transfered to the FIFO by the server. In playback mode it indicates
// that the IO thread is transferring frames to the FIFO.
bool delay_running;
// delay accumulated just before pausing
snd_pcm_sframes_t delay_paused;
// maximum delay in FIFO
snd_pcm_sframes_t delay_fifo_size;
// synchronize threads to begin/end pause
pthread_cond_t pause_cond;
unsigned int pause_state;
// Process id of forked CamillaDSP
pid_t cpid;
// Path to CamillaDSP executable
char *cpath;
// Location of CamillaDSP input YAML template
char *config_in;
// Alternatively to providing config_in provide a program
// that will generate config_out
char *config_cmd;
// And yet another alternative use CamillaDSP's new internal
// subsitution
long config_cdsp;
// Gain and mute startup file
char *vol_file;
// Arguments to execv
// cargs[0] = "camilladsp" => Process name
// cargs[1] = config_out => Location of CamillaDSP output YAML configuration
// cargs[2+] = Additional arguments passed through .asoundrc
// If config_cdsp cargs will also be used to hold hw_params
// If vol_file is set cargs will also be used to hold mute and gain
// Make the array a bit bigger to allow them
size_t n_cargs;
char *cargs[120];
// Search / Replace string tokens - let people use whatever format
// they want.
char *format_token;
char *rate_token;
char *channels_token;
char *ext_samp_token;
// Extra samples parameter to pass to CamillaDSP if the config_in template
// is used instead of config_cmd
// ext_samp_44100 and ext_samp_4800 allow rate matched expansion of the
// extra samples. They will be multiplied by {rate}/44100 or rate/{48000}
// if {rate} is an integer multiple of 44100 or 48000 respectively
long ext_samp;
long ext_samp_44100;
long ext_samp_48000;
// Suppress a spurious warning on the first call to revents for the
// event triggered during prepare. Some programs need that event to
// start so it's not actually an overcall.
bool first_revent;
// A command to run at init - turn on audio system for example
char *start_cmd;
// A command to run just before the CamillaDSP process is ended
// A good time to update the gain and mute values for example
char *camilla_exit_cmd;
} cdsp_t;
#if SND_LIB_VERSION < 0x010106
//
// Get the available frames.
//
// This function is available in alsa-lib since version 1.1.6. For older
// alsa-lib versions we need to provide our own implementation.
static snd_pcm_uframes_t snd_pcm_ioplug_hw_avail(
const snd_pcm_ioplug_t * const io, const snd_pcm_uframes_t hw_ptr,
const snd_pcm_uframes_t appl_ptr) {
cdsp_t *pcm = io->private_data;
snd_pcm_sframes_t diff;
if (io->stream == SND_PCM_STREAM_PLAYBACK)
diff = appl_ptr - hw_ptr;
else
diff = io->buffer_size - hw_ptr + appl_ptr;
if (diff < 0)
diff += pcm->io_hw_boundary;
return diff <= io->buffer_size ? (snd_pcm_uframes_t) diff : 0;
}
#endif
// Helper function for closing PCM transport.
static int close_transport(cdsp_t *pcm) {
int rv = 0;
pthread_mutex_lock(&pcm->mutex);
if (pcm->cdsp_pcm_fd != -1) {
rv |= close(pcm->cdsp_pcm_fd);
pcm->cdsp_pcm_fd = -1;
}
pthread_mutex_unlock(&pcm->mutex);
return rv;
}
// Helper function for IO thread termination.
static void io_thread_cleanup(cdsp_t *pcm) {
debug("IO thread cleanup\n");
pcm->io_started = false;
}
// Helper function for IO thread delay calculation.
static void io_thread_update_delay(cdsp_t *pcm, snd_pcm_uframes_t hw_ptr) {
struct timespec now;
unsigned int nread = 0;
gettimestamp(&now);
// Get the number of bytes still in the pipe to cdsp
ioctl(pcm->cdsp_pcm_fd, FIONREAD, &nread);
pthread_mutex_lock(&pcm->mutex);
// stash current time and levels
pcm->delay_ts = now;
pcm->delay_pcm_nread = nread;
if (pcm->io_status < 0) {
pcm->delay_hw_ptr = 0;
if (pcm->io.stream == SND_PCM_STREAM_PLAYBACK)
pcm->delay_running = false;
}
else {
pcm->delay_hw_ptr = hw_ptr;
if (pcm->io.stream == SND_PCM_STREAM_PLAYBACK)
pcm->delay_running = true;
}
pthread_mutex_unlock(&pcm->mutex);
}
// IO thread, which facilitates ring buffer.
static void *io_thread(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
pthread_cleanup_push(PTHREAD_CLEANUP(io_thread_cleanup), pcm);
int xrun = 0;
sigset_t sigset;
sigemptyset(&sigset);
// Block signal, which will be used for pause/resume actions.
sigaddset(&sigset, SIGIO);
// Block SIGPIPE, so we could receive EPIPE while writing to the pipe
// whose reading end has been closed. This will allow clean playback
// termination.
sigaddset(&sigset, SIGPIPE);
if ((errno = pthread_sigmask(SIG_BLOCK, &sigset, NULL)) != 0) {
SNDERR("Thread signal mask error: %s", strerror(errno));
goto fail;
}
// We update pcm->io_hw_ptr (i.e. the value seen by ioplug) only when
// a period has been completed. We use a temporary copy during the
// transfer procedure.
snd_pcm_uframes_t io_hw_ptr = pcm->io_hw_ptr;
debug("Starting IO loop: %d\n", pcm->cdsp_pcm_fd);
for (;;) {
if (pcm->pause_state & CDSP_PAUSE_STATE_PENDING ||
pcm->io_status < 0) {
debug("Pausing IO thread\n");
pthread_mutex_lock(&pcm->mutex);
pcm->pause_state = CDSP_PAUSE_STATE_PAUSED;
pthread_cond_signal(&pcm->pause_cond);
pthread_mutex_unlock(&pcm->mutex);
int tmp;
sigwait(&sigset, &tmp);
pthread_mutex_lock(&pcm->mutex);
pcm->pause_state = CDSP_PAUSE_STATE_RUNNING;
pthread_mutex_unlock(&pcm->mutex);
debug("IO thread resumed\n");
if (pcm->io_status < 0)
continue;
if (pcm->cdsp_pcm_fd == -1) {
error("FAILING BECAUSE PIPE GONE\n");
goto fail;
}
}
// There are 2 reasons why the number of available frames may be
// zero: XRUN or drained final samples; we set the io_status to
// -1 to indicate we have no work to do.
snd_pcm_uframes_t avail;
if ((avail = snd_pcm_ioplug_hw_avail(io, io_hw_ptr, io->appl_ptr)) == 0) {
if(io->state == SND_PCM_STATE_DRAINING) {
// Draining is complete. Signal that to the ioplug code so it will
// drop the pcm.
pcm->io_status = -1;
io_thread_update_delay(pcm, 0);
eventfd_write(pcm->event_fd, 1);
continue;
} else {
warn("IO Thread out of data.\n");
// Running and no data is available. The internal alsa buffer is
// empty. This isn't a problem until a period has passed though
// at which point we have an underrun condition.
// Sleep in 1/4 period intervals to wait for data to catch up
// Add 1 extra sample to the period to allow for clock differences
// and rounding errors
uint64_t quarter_period_ns =
(1000000000 / 4) * (io->period_size+1) / io->rate;
struct timespec ts;
ts.tv_sec = quarter_period_ns / 1000000000;
ts.tv_nsec = quarter_period_ns - 1000000000 * ts.tv_sec;
debug("Sleep Time: %ld %ld %lu %d\n", ts.tv_sec, ts.tv_nsec,io->period_size, io->rate);
nanosleep(&ts, NULL);
xrun++;
if(xrun > 4) {
// We've gone longer than a period with no data.
// The player isn't providing data fast enough.
error("XRUN OCCURRED!\n");
// Signal XRUN to the ioplug code
pcm->io_status = -1;
io_thread_update_delay(pcm, 0);
eventfd_write(pcm->event_fd, 1);
}
continue;
}
}
// Data available - reset the xrun counter
xrun = 0;
pcm->io_status = 0;
// current offset of the head pointer in the IO buffer
snd_pcm_uframes_t offset = io_hw_ptr % io->buffer_size;
// Transfer at most 1 period of frames each iteration
snd_pcm_uframes_t frames = io->period_size;
// ... but do not try to transfer more frames than are available in
// the ring buffer!
if (frames > avail) {
frames = avail;
}
// Sometimes alsa chooses a buffer size that isn't an integer multiple
// of the period size. In that case don't read past the end of the
// buffer.
if (io->buffer_size - offset < frames) {
frames = io->buffer_size - offset;
}
// IO operation size in bytes
size_t len = frames * pcm->frame_size;
char *head = pcm->io_hw_buffer + offset * pcm->frame_size;
// Increment the HW pointer (with boundary wrap)
io_hw_ptr += frames;
if (io_hw_ptr >= pcm->io_hw_boundary)
io_hw_ptr -= pcm->io_hw_boundary;
ssize_t ret = 0;
struct timespec tstart,tstop,twrite;
gettimestamp(&tstart);
// Perform atomic write - see the explanation above.
do {
if ((ret = write(pcm->cdsp_pcm_fd, head, len)) == -1) {
if (errno == EINTR)
continue;
if (errno != EPIPE)
SNDERR("PCM FIFO write error: %s", strerror(errno));
goto fail;
}
head += ret;
len -= ret;
} while (len != 0);
io_thread_update_delay(pcm, io_hw_ptr);
// Things tend to run a little smoother if writes take at least
// some time. So slow down when the pipe was empty enough that the
// write was basically instant.
gettimestamp(&tstop);
difftimespec(&tstart, &tstop, &twrite);
double sampletime = (double)frames/(double)io->rate;
double writetime = (double)twrite.tv_sec + (double)twrite.tv_nsec/1e9;
double excess = sampletime - writetime;
if(excess > 0) {
excess *= 0.5;
tstop.tv_sec = (time_t)excess;
excess -= tstop.tv_sec;
tstop.tv_nsec = (long)(excess*1e9);
nanosleep(&tstop, NULL);
}
excessive("Frames = %lu = %lf secs, Write Time = %lf\n", frames, sampletime, writetime);
// Make the new HW pointer value visible to the ioplug.
pcm->io_hw_ptr = io_hw_ptr;
// Wake application thread if enough space/frames are available
// to write avail_min frames. Note that just as we can't read
// past the end of the hardware buffer the app can't write past
// it so the metric is distance from the end of the buffer.
offset = io_hw_ptr % io->buffer_size;
if(io->buffer_size - offset >= pcm->io_avail_min)
eventfd_write(pcm->event_fd, 1);
}
fail:
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
pthread_cleanup_pop(1);
close_transport(pcm);
eventfd_write(pcm->event_fd, 0xDEAD0000);
pthread_cond_signal(&pcm->pause_cond);
return NULL;
}
static int start_camilla(cdsp_t *pcm) {
char sformat[20];
switch (pcm->io.format) {
case SND_PCM_FORMAT_S16_LE:
snprintf(sformat, sizeof(sformat), "S16LE");
break;
case SND_PCM_FORMAT_S24_LE:
snprintf(sformat, sizeof(sformat), "S24LE");
break;
case SND_PCM_FORMAT_S24_3LE:
snprintf(sformat, sizeof(sformat), "S24LE3");
break;
case SND_PCM_FORMAT_S32_LE:
snprintf(sformat, sizeof(sformat), "S32LE");
break;
case SND_PCM_FORMAT_FLOAT_LE:
snprintf(sformat, sizeof(sformat), "FLOAT32LE");
break;
case SND_PCM_FORMAT_FLOAT64_LE:
snprintf(sformat, sizeof(sformat), "FLOAT64LE");
break;
default:
// Shouldn't get here
SNDERR("Unsupported Format: %s\n", snd_pcm_format_name(pcm->io.format));
return -EINVAL;
}
char schannels[10]; // The number of channels should fit in 9 digits
snprintf(schannels, sizeof(schannels), "%u", pcm->io.channels);
char srate[10]; // The rate should fit in 9 digits too
snprintf(srate, sizeof(srate), "%u", pcm->io.rate);
char sextrasamples[20]; // Some use really long audio chains
long extrasamples = -1;
// We multiply the ext_samp by the ratio of sample rate to
// one of the two common audio rates if the sample rate is an
// integer multiple
if((pcm->ext_samp_44100 >= 0) && ((pcm->io.rate % 44100) == 0)) {
extrasamples = pcm->ext_samp_44100*(pcm->io.rate/44100);
} else if((pcm->ext_samp_48000 >= 0) && ((pcm->io.rate % 48000) == 0)) {
extrasamples = pcm->ext_samp_48000*(pcm->io.rate/48000);
} else if(pcm->ext_samp >= 0) {
extrasamples = pcm->ext_samp;
}
if(extrasamples >= 0) {
snprintf(sextrasamples, sizeof(sextrasamples), "%ld", extrasamples);
}
// Create the pipe to send data to camilla
int fd[2];
if(pipe(fd)) {
return -ENODEV;
}
// Fork to launch camilla
pcm->cpid = fork();
if(pcm->cpid < 0) {
return -ENODEV;
}
if(pcm->cpid == 0) {
// Child process
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
debug("cpath: %s\n", pcm->cpath);
debug("config_in: %s\n", pcm->config_in);
debug("config_out: %s\n", pcm->cargs[1]);
debug("config_cmd: %s\n", pcm->config_cmd);
debug("config_cdsp: %ld\n", pcm->config_cdsp);
debug("vol_file: %s\n", pcm->vol_file);
debug("cargs:");
#if DEBUG > 3
for(size_t ca = 2; ca < pcm->n_cargs; ca++) {
fprintf(stderr," %s", pcm->cargs[ca]);
}
fprintf(stderr,"\n");
#endif
double gain = 0;
// Use mute < 0 as the flag for gain and mute being set.
int mute = -1;
size_t extra_cargs = 0;
if(pcm->vol_file) {
FILE *volfile = fopen(pcm->vol_file, "r");
if(!volfile) {
SNDERR("Error reading input volume file %s\n", pcm->vol_file);
return -EINVAL;
}
if(fscanf(volfile, "%lf %d", &gain, &mute) != 2) {
SNDERR("Error reading input volume file %s\n", pcm->vol_file);
return -EINVAL;
}
debug("Read Volume File: Gain = %lf, Mute = %d\n", gain, mute);
fclose(volfile);
}
if(pcm->config_in) {
debug("format_token: %s\n", pcm->format_token);
debug("rate_token: %s\n", pcm->rate_token);
debug("channels_token: %s\n", pcm->channels_token);
debug("ext_samp_token: %s\n", pcm->ext_samp_token);
FILE *cfgin = fopen(pcm->config_in, "r");
if(!cfgin) {
SNDERR("Error reading input config file %s\n", pcm->config_in);
return -EINVAL;
}
FILE *cfgout = fopen(pcm->cargs[1], "w");
if(!cfgout) {
SNDERR("Error writing output config file %s\n", pcm->cargs[1]);
return -EINVAL;
}
char buf[1000];
char *obuf;
while(fgets(buf, sizeof(buf), cfgin)) {
obuf = strrep(buf, pcm->format_token, sformat);
obuf = strrep(obuf, pcm->rate_token, srate);
obuf = strrep(obuf, pcm->channels_token, schannels);
if(extrasamples >= 0) {
obuf = strrep(obuf, pcm->ext_samp_token, sextrasamples);
}
fprintf(cfgout,"%s",obuf);
}
fclose(cfgin);
fclose(cfgout);
} else if(pcm->config_cmd) {
char command[1000];
// Call the config_cmd with the hw params to do whatever
// camilla configuration is desired
// Command will be called with arguments "format rate channels"
snprintf(command, 1000, "%s %s %d %d\n", pcm->config_cmd,
sformat, pcm->io.rate, pcm->io.channels);
debug("Calling config_cmd %s\n", command);
int err = system(command);
if(err != 0) {
SNDERR("Error executing config_cmd %s\n", pcm->config_cmd);
if(err > 0) return -err;
return err;
}
} else {
// Pass the hw_params as arguments directly to CamillaDSP
char farg[] = "-f";
pcm->cargs[pcm->n_cargs] = farg;
pcm->cargs[pcm->n_cargs+1] = sformat;
extra_cargs += 2;
char rarg[] = "-r";
pcm->cargs[pcm->n_cargs+2] = rarg;
pcm->cargs[pcm->n_cargs+3] = srate;
extra_cargs += 2;
char narg[] = "-n";
pcm->cargs[pcm->n_cargs+4] = narg;
pcm->cargs[pcm->n_cargs+5] = schannels;
extra_cargs += 2;
char earg[] = "-e";
if(extrasamples >= 0) {
pcm->cargs[pcm->n_cargs+6] = earg;
pcm->cargs[pcm->n_cargs+7] = sextrasamples;
extra_cargs += 2;
} else {
pcm->cargs[pcm->n_cargs+6] = 0;
pcm->cargs[pcm->n_cargs+7] = 0;
// Don't advance extra_cargs pointer as we might add
// gain and mute
}
}
if(mute >= 0) {
char sgain[30]; // Big enough for argument and value
snprintf(sgain, sizeof(sgain), "-g%lf", gain);
pcm->cargs[pcm->n_cargs+extra_cargs] = sgain;
if(mute > 0) {
char marg[] = "-m";
pcm->cargs[pcm->n_cargs+extra_cargs+1] = marg;
pcm->cargs[pcm->n_cargs+extra_cargs+2] = 0;
} else {
pcm->cargs[pcm->n_cargs+extra_cargs+1] = 0;
}
}
execv(pcm->cpath, pcm->cargs);
// Shouldn't get here
SNDERR("Failed to execute CamillaDSP");
return -ENODEV;
} else {
// Parent process
close(fd[0]);
pcm->cdsp_pcm_fd = fd[1];
}
return 0;
}
static int cdsp_start(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
debug("Starting\n");
// If the IO thread is already started, skip thread creation. Otherwise,
// we might end up with a bunch of IO threads reading or writing to the
// same FIFO simultaneously. Instead, just send resume signal. */
if (pcm->io_started) {
pthread_kill(pcm->io_thread, SIGIO);
return 0;
}
// Initialize delay calculation - capture reception begins immediately,
// playback transmission begins only when first period has been written
// by the application.
pcm->delay_running = io->stream == SND_PCM_STREAM_CAPTURE ? true : false;
gettimestamp(&pcm->delay_ts);
// start the IO thread
pcm->io_started = true;
if ((errno = pthread_create(&pcm->io_thread, NULL,
PTHREAD_ROUTINE(io_thread), io)) != 0) {
error("Couldn't create IO thread: %s\n", strerror(errno));
pcm->io_started = false;
return -errno;
}
pthread_setname_np(pcm->io_thread, "pcm-io");
return 0;
}
static int cdsp_stop(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
debug("Stopping\n");
if (pcm->io_started) {
pcm->io_started = false;
pthread_cancel(pcm->io_thread);
}
pthread_join(pcm->io_thread, NULL);
pcm->delay_running = false;
pcm->delay_pcm_nread = 0;
// Bug in ioplug - if pcm->io_status == -1 then it reports state
// SND_PCM_STATE_XRUN instead of SND_PCM_STATE_SETUP after PCM
// was stopped.
// However -1 should be set if it is due to draining
if(io->state != SND_PCM_STATE_DRAINING) {
pcm->io_status = 0;
}
// Applications that call poll() after snd_pcm_drain() will be blocked
// forever unless we generate a poll() event here.
eventfd_write(pcm->event_fd, 1);
return 0;
}
static snd_pcm_sframes_t cdsp_pointer(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
// Any error returned here is translated to -EPIPE, SND_PCM_STATE_XRUN,
// by ioplug; and that prevents snd_pcm_readi() and snd_pcm_writei()
// from returning -ENODEV to the application on device disconnection.
// Instead, when the device is disconnected, we update the PCM state
// directly here but we do not return an error code. This ensures that
// ioplug does not undo that state change. Both snd_pcm_readi() and
// snd_pcm_writei() return -ENODEV when the PCM state is
// SND_PCM_STATE_DISCONNECTED after their internal call to
// snd_pcm_avail_update(), which will be the case when we set it here.
if (pcm->cdsp_pcm_fd == -1)
snd_pcm_ioplug_set_state(io, SND_PCM_STATE_DISCONNECTED);
#ifndef SND_PCM_IOPLUG_FLAG_BOUNDARY_WA
if (pcm->io_status >= 0)
return pcm->io_hw_ptr % io->buffer_size;
#endif
if (pcm->io_status >= 0)
return pcm->io_hw_ptr;
return pcm->io_status;
}
static void free_cdsp(cdsp_t **pcm) {
if ((*pcm)->event_fd != -1)
close((*pcm)->event_fd);
if((*pcm)->cpath)
free((void *)(*pcm)->cpath);
if((*pcm)->config_in)
free((void *)(*pcm)->config_in);
for(size_t f = 0; f < (*pcm)->n_cargs; f++) {
if((*pcm)->cargs[f] != 0) {
free((void *)(*pcm)->cargs[f]);
}
}
if((*pcm)->config_cmd)
free((void *)(*pcm)->config_cmd);
if((*pcm)->vol_file)
free((void *)(*pcm)->vol_file);
if((*pcm)->format_token)
free((void *)(*pcm)->format_token);
if((*pcm)->rate_token)
free((void *)(*pcm)->rate_token);
if((*pcm)->channels_token)
free((void *)(*pcm)->channels_token);
if((*pcm)->ext_samp_token)
free((void *)(*pcm)->ext_samp_token);
if((*pcm)->start_cmd)
free((void *)(*pcm)->start_cmd);
if((*pcm)->camilla_exit_cmd)
free((void *)(*pcm)->camilla_exit_cmd);
pthread_mutex_destroy(&(*pcm)->mutex);
pthread_cond_destroy(&(*pcm)->pause_cond);
free((void *)*pcm);
}
static int cdsp_close(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
debug("Closing\n");
free_cdsp(&pcm);
return 0;
}
static int cdsp_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params __attribute__((unused))) {
cdsp_t *pcm = io->private_data;
info("Initializing hw_params: %s %d %d\n",
snd_pcm_format_name(io->format), io->rate, io->channels);
pcm->frame_size = (snd_pcm_format_physical_width(io->format)*io->channels)/8;
// Start CamillaDSP in a forked process
start_camilla(pcm);
// By default, the size of the pipe buffer is set to a too large value for
// our purpose. On modern Linux system it is 65536 bytes. Large buffer in
// the playback mode might contribute to an unnecessary audio delay. Since
// it is possible to modify the size of this buffer we will set is to some
// low value, but big enough to prevent audio tearing. Note, that the size
// will be rounded up to the page size (typically 4096 bytes).
pcm->delay_fifo_size =
fcntl(pcm->cdsp_pcm_fd, F_SETPIPE_SZ, 2048) / pcm->frame_size;
info("FIFO buffer size: %ld frames\n", pcm->delay_fifo_size);
/* ALSA default for avail min is one period. */
pcm->io_avail_min = io->period_size;
info("Selected HW buffer: %ld periods x %ld bytes %c= %ld bytes\n",
io->buffer_size / io->period_size, pcm->frame_size * io->period_size,
io->period_size * (io->buffer_size / io->period_size) == io->buffer_size ? '=' : '<',
io->buffer_size * pcm->frame_size);
return 0;
}
static int cdsp_hw_free(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
debug("Freeing HW\n");
int err = 0;
if(pcm->camilla_exit_cmd) {
debug("Calling camilla_exit_cmd: %s\n", pcm->camilla_exit_cmd);
// Call the camilla_exit_cmd
err = system(pcm->camilla_exit_cmd);
if(err != 0) {
SNDERR("Error executing camilla_exit_cmd %s\n", pcm->camilla_exit_cmd);
}
}
debug("Stopping Camilla\n");
if (close_transport(pcm) == -1)
return -errno;
if(pcm->cpid != -1) {
// Wait on CamillaDSP to finish. It needs to free the ALSA
// device before another copy is started.
waitpid(pcm->cpid, NULL, 0);
pcm->cpid = -1;
}
if(err > 0) return -err;
return err;
}
// A check that get_boundary still works
static snd_pcm_uframes_t calc_boundary_size(snd_pcm_ioplug_t *io) {
snd_pcm_uframes_t boundary;
boundary = io->buffer_size;
while (boundary * 2 <= LONG_MAX - io->buffer_size)
boundary *= 2;
return boundary;
}
static int cdsp_sw_params(snd_pcm_ioplug_t *io, snd_pcm_sw_params_t *params) {
cdsp_t *pcm = io->private_data;
debug("Initializing SW\n");
snd_pcm_sw_params_get_boundary(params, &pcm->io_hw_boundary);
assert(pcm->io_hw_boundary == calc_boundary_size(io));
// We would get avail_min here but alsa has hidden it from the plugin
// So we'll just have to ignore the player's request and stick to
// period_size
return 0;
}
static int cdsp_prepare(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
// if PCM FIFO is not opened, report it right away
if (pcm->cdsp_pcm_fd == -1)
return -ENODEV;
// initialize ring buffer and status
pcm->io_hw_ptr = 0;
pcm->io_status = 0;
// The ioplug allocates and configures its channel area buffer when the
// HW parameters are fixed, but after calling cdsp_hw_params(). So,
// this is the earliest opportunity for us to safely cache the ring
// buffer start address.
const snd_pcm_channel_area_t *areas = snd_pcm_ioplug_mmap_areas(io);
pcm->io_hw_buffer = (char *)areas->addr + areas->first / 8;
// Indicate that our PCM is ready for IO, even though is is not 100%
// true - the IO thread may not be running yet. Applications using
// snd_pcm_sw_params_set_start_threshold() require the PCM to be usable
// as soon as it has been prepared.
pcm->first_revent = true;
eventfd_write(pcm->event_fd, 1);
debug("Prepared\n");
return 0;
}
static int cdsp_drain(snd_pcm_ioplug_t *io) {
debug("Draining\n");
cdsp_t *pcm = io->private_data;
// Wait for the playback thread to empty the ring buffer
while(pcm->io_status >= 0)
usleep(10);
// We cannot recover from an error here. By returning zero we ensure that
// ioplug stops the pcm. Returning an error code would be interpreted by
// ioplug as an incomplete drain and would it leave the pcm running.
return 0;
}
// Calculate overall PCM delay.
//
// Exact calculation of the PCM delay is very hard, if not impossible. For
// the sake of simplicity we will make few assumptions and approximations.
// In general, the delay of this plugin is proportional to the number of
// bytes queued in the FIFO buffer. Of course CamillaDSP may add consdirable
// additional delay which is not accounted for in this estimation.
static snd_pcm_sframes_t cdsp_calculate_delay(snd_pcm_ioplug_t *io) {
cdsp_t *pcm = io->private_data;
snd_pcm_sframes_t delay = 0;
struct timespec now;
pthread_mutex_lock(&pcm->mutex);
gettimestamp(&now);
struct timespec diff;
difftimespec(&now, &pcm->delay_ts, &diff);
// the maximum number of frames that can have been
// produced/consumed by the server since pcm->delay_ts
unsigned int tframes =
//(diff.tv_sec * 1000 + diff.tv_nsec / 1000000) * io->rate / 1000;
(unsigned int)(((double)diff.tv_sec + ((double)diff.tv_nsec)/1e9) * io->rate);
// the number of frames that were in the FIFO at pcm->delay_ts
snd_pcm_uframes_t fifo_delay = pcm->delay_pcm_nread / pcm->frame_size;
delay = fifo_delay;
// The buffer_delay is the number of frames that were in the buffer at
// pcm->delay_ts, adjusted the number written by the application since
// then.
snd_pcm_sframes_t buffer_delay = 0;
if (io->state != SND_PCM_STATE_XRUN)
buffer_delay = snd_pcm_ioplug_hw_avail(io, pcm->delay_hw_ptr, io->appl_ptr);
delay += buffer_delay;
if (pcm->delay_running) {
// Adjust the total delay by the number of frames consumed.
if ((delay -= tframes) < 0) delay = 0;
}
pthread_mutex_unlock(&pcm->mutex);
return delay;
}
static int cdsp_pause(snd_pcm_ioplug_t *io, int enable) {
cdsp_t *pcm = io->private_data;
if (enable == 1) {
// Synchronize the IO thread with an application thread to ensure that
// the server will not be paused while we are processing a transfer.
pthread_mutex_lock(&pcm->mutex);
pcm->pause_state |= CDSP_PAUSE_STATE_PENDING;
while (!(pcm->pause_state & CDSP_PAUSE_STATE_PAUSED)
&& pcm->cdsp_pcm_fd != -1) {
pthread_cond_wait(&pcm->pause_cond, &pcm->mutex);
}
pthread_mutex_unlock(&pcm->mutex);
}
if (enable == 0) {
pcm->first_revent = true;
pthread_kill(pcm->io_thread, SIGIO);
} else {
// store current delay value
pcm->delay_paused = cdsp_calculate_delay(io);
}
// Even though PCM transport is paused, our IO thread is still running. If
// the implementer relies on the PCM file descriptor readiness, we have to
// bump our internal event trigger. Otherwise, client might stuck forever
// in the poll/select system call.
eventfd_write(pcm->event_fd, 1);
return 0;
}
static void cdsp_dump(snd_pcm_ioplug_t *io, snd_output_t *out) {
cdsp_t *pcm = io->private_data;
snd_output_printf(out, "CamillaDSP Plugin\n");
snd_output_printf(out, "c_path: %s\n", pcm->cpath);
snd_output_printf(out, "config_out: %s\n", pcm->cargs[1]);
if(pcm->config_in)
snd_output_printf(out, "config_in: %s\n", pcm->config_in);
if(pcm->config_cmd)
snd_output_printf(out, "config_cmd: %s\n", pcm->config_cmd);
snd_output_printf(out, "vol_file: %s\n", pcm->vol_file);
// alsa-lib commits the PCM setup only if cdsp_hw_params() returned
// success, so we only dump the ALSA PCM parameters if CamillaDSP was
// started.
if (pcm->cpid >= 0) {
snd_output_printf(out, "Its setup is:\n");
snd_pcm_dump_setup(io->pcm, out);
}