1 /**
2 * @file src/audio.c Audio stream
3 *
4 * Copyright (C) 2010 Creytiv.com
5 * \ref GenericAudioStream
6 */
7 #define _DEFAULT_SOURCE 1
8 #define _BSD_SOURCE 1
9 #include <string.h>
10 #include <stdlib.h>
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #ifdef HAVE_PTHREAD
15 #include <pthread.h>
16 #endif
17 #include <re.h>
18 #include <rem.h>
19 #include <baresip.h>
20 #include "core.h"
21
22
23 /** Magic number */
24 #define MAGIC 0x000a0d10
25 #include "magic.h"
26
27
28 /**
29 * \page GenericAudioStream Generic Audio Stream
30 *
31 * Implements a generic audio stream. The application can allocate multiple
32 * instances of a audio stream, mapping it to a particular SDP media line.
33 * The audio object has a DSP sound card sink and source, and an audio encoder
34 * and decoder. A particular audio object is mapped to a generic media
35 * stream object. Each audio channel has an optional audio filtering chain.
36 *
37 *<pre>
38 * write read
39 * | /|\
40 * \|/ |
41 * .------. .---------. .-------.
42 * |filter|<--| audio |--->|encoder|
43 * '------' | | |-------|
44 * | object |--->|decoder|
45 * '---------' '-------'
46 * | /|\
47 * | |
48 * \|/ |
49 * .------. .-----.
50 * |auplay| |ausrc|
51 * '------' '-----'
52 *</pre>
53 */
54
55 enum {
56 AUDIO_SAMPSZ = 3*1920 /* Max samples, 48000Hz 2ch at 60ms */
57 };
58
59
60 /**
61 * Audio transmit/encoder
62 *
63 *
64 \verbatim
65
66 Processing encoder pipeline:
67
68 . .-------. .-------. .--------. .--------. .--------.
69 | | | | | | | | | | |
70 |O-->| ausrc |-->| aubuf |-->| resamp |-->| aufilt |-->| encode |---> RTP
71 | | | | | | | | | | |
72 ' '-------' '-------' '--------' '--------' '--------'
73
74 \endverbatim
75 *
76 */
77 struct autx {
78 struct ausrc_st *ausrc; /**< Audio Source */
79 struct ausrc_prm ausrc_prm; /**< Audio Source parameters */
80 const struct aucodec *ac; /**< Current audio encoder */
81 struct auenc_state *enc; /**< Audio encoder state (optional) */
82 struct aubuf *aubuf; /**< Packetize outgoing stream */
83 size_t aubuf_maxsz; /**< Maximum aubuf size in [bytes] */
84 volatile bool aubuf_started; /**< Aubuf was started flag */
85 struct auresamp resamp; /**< Optional resampler for DSP */
86 struct list filtl; /**< Audio filters in encoding order */
87 struct mbuf *mb; /**< Buffer for outgoing RTP packets */
88 char device[64]; /**< Audio source device name */
89 void *sampv; /**< Sample buffer */
90 int16_t *sampv_rs; /**< Sample buffer for resampler */
91 uint32_t ptime; /**< Packet time for sending */
92 uint64_t ts_ext; /**< Ext. Timestamp for outgoing RTP */
93 uint32_t ts_base; /**< First timestamp sent */
94 uint32_t ts_tel; /**< Timestamp for Telephony Events */
95 size_t psize; /**< Packet size for sending */
96 bool marker; /**< Marker bit for outgoing RTP */
97 bool muted; /**< Audio source is muted */
98 int cur_key; /**< Currently transmitted event */
99 enum aufmt src_fmt; /**< Sample format for audio source */
100 enum aufmt enc_fmt;
101 bool need_conv; /**< Sample format conversion needed */
102
103 struct {
104 uint64_t aubuf_overrun;
105 uint64_t aubuf_underrun;
106 } stats;
107
108 #ifdef HAVE_PTHREAD
109 union {
110 struct {
111 pthread_t tid;/**< Audio transmit thread */
112 bool run; /**< Audio transmit thread running */
113 } thr;
114 } u;
115 #endif
116 };
117
118
119 /**
120 * Audio receive/decoder
121 *
122 \verbatim
123
124 Processing decoder pipeline:
125
126 .--------. .-------. .--------. .--------. .--------.
127 |\ | | | | | | | | | |
128 | |<--| auplay |<--| aubuf |<--| resamp |<--| aufilt |<--| decode |<--- RTP
129 |/ | | | | | | | | | |
130 '--------' '-------' '--------' '--------' '--------'
131
132 \endverbatim
133 */
134 struct aurx {
135 struct auplay_st *auplay; /**< Audio Player */
136 struct auplay_prm auplay_prm; /**< Audio Player parameters */
137 const struct aucodec *ac; /**< Current audio decoder */
138 struct audec_state *dec; /**< Audio decoder state (optional) */
139 struct aubuf *aubuf; /**< Incoming audio buffer */
140 size_t aubuf_maxsz; /**< Maximum aubuf size in [bytes] */
141 volatile bool aubuf_started; /**< Aubuf was started flag */
142 struct auresamp resamp; /**< Optional resampler for DSP */
143 struct list filtl; /**< Audio filters in decoding order */
144 char device[64]; /**< Audio player device name */
145 void *sampv; /**< Sample buffer */
146 int16_t *sampv_rs; /**< Sample buffer for resampler */
147 uint32_t ptime; /**< Packet time for receiving */
148 int pt; /**< Payload type for incoming RTP */
149 double level_last;
150 bool level_set;
151 enum aufmt play_fmt; /**< Sample format for audio playback*/
152 enum aufmt dec_fmt;
153 bool need_conv; /**< Sample format conversion needed */
154 struct timestamp_recv ts_recv;/**< Receive timestamp state */
155 uint64_t n_discard;
156
157 struct {
158 uint64_t aubuf_overrun;
159 uint64_t aubuf_underrun;
160 } stats;
161 };
162
163
164 /** Generic Audio stream */
165 struct audio {
166 MAGIC_DECL /**< Magic number for debugging */
167 struct autx tx; /**< Transmit */
168 struct aurx rx; /**< Receive */
169 struct stream *strm; /**< Generic media stream */
170 struct telev *telev; /**< Telephony events */
171 struct config_audio cfg; /**< Audio configuration */
172 bool started; /**< Stream is started flag */
173 bool level_enabled; /**< Audio level RTP ext. enabled */
174 unsigned extmap_aulevel; /**< ID Range 1-14 inclusive */
175 audio_event_h *eventh; /**< Event handler */
176 audio_err_h *errh; /**< Audio error handler */
177 void *arg; /**< Handler argument */
178 };
179
180
181 /* RFC 6464 */
182 static const char *uri_aulevel = "urn:ietf:params:rtp-hdrext:ssrc-audio-level";
183
184
audio_calc_seconds(uint64_t rtp_ts,uint32_t clock_rate)185 static double audio_calc_seconds(uint64_t rtp_ts, uint32_t clock_rate)
186 {
187 double timestamp;
188
189 /* convert from RTP clockrate to seconds */
190 timestamp = (double)rtp_ts / (double)clock_rate;
191
192 return timestamp;
193 }
194
195
autx_calc_seconds(const struct autx * autx)196 static double autx_calc_seconds(const struct autx *autx)
197 {
198 uint64_t dur;
199
200 if (!autx->ac)
201 return .0;
202
203 dur = autx->ts_ext - autx->ts_base;
204
205 return audio_calc_seconds(dur, autx->ac->crate);
206 }
207
208
aurx_calc_seconds(const struct aurx * aurx)209 static double aurx_calc_seconds(const struct aurx *aurx)
210 {
211 uint64_t dur;
212
213 if (!aurx->ac)
214 return .0;
215
216 dur = timestamp_duration(&aurx->ts_recv);
217
218 return audio_calc_seconds(dur, aurx->ac->crate);
219 }
220
221
stop_tx(struct autx * tx,struct audio * a)222 static void stop_tx(struct autx *tx, struct audio *a)
223 {
224 if (!tx || !a)
225 return;
226
227 switch (a->cfg.txmode) {
228
229 #ifdef HAVE_PTHREAD
230 case AUDIO_MODE_THREAD:
231 if (tx->u.thr.run) {
232 tx->u.thr.run = false;
233 pthread_join(tx->u.thr.tid, NULL);
234 }
235 break;
236 #endif
237 default:
238 break;
239 }
240
241 /* audio source must be stopped first */
242 tx->ausrc = mem_deref(tx->ausrc);
243 tx->aubuf = mem_deref(tx->aubuf);
244
245 list_flush(&tx->filtl);
246 }
247
248
stop_rx(struct aurx * rx)249 static void stop_rx(struct aurx *rx)
250 {
251 if (!rx)
252 return;
253
254 /* audio player must be stopped first */
255 rx->auplay = mem_deref(rx->auplay);
256 rx->aubuf = mem_deref(rx->aubuf);
257
258 list_flush(&rx->filtl);
259 }
260
261
audio_destructor(void * arg)262 static void audio_destructor(void *arg)
263 {
264 struct audio *a = arg;
265
266 stop_tx(&a->tx, a);
267 stop_rx(&a->rx);
268
269 mem_deref(a->tx.enc);
270 mem_deref(a->rx.dec);
271 mem_deref(a->tx.aubuf);
272 mem_deref(a->tx.mb);
273 mem_deref(a->tx.sampv);
274 mem_deref(a->rx.sampv);
275 mem_deref(a->rx.aubuf);
276 mem_deref(a->tx.sampv_rs);
277 mem_deref(a->rx.sampv_rs);
278
279 list_flush(&a->tx.filtl);
280 list_flush(&a->rx.filtl);
281
282 mem_deref(a->strm);
283 mem_deref(a->telev);
284 }
285
286
287 /**
288 * Calculate number of samples from sample rate, channels and packet time
289 *
290 * @param srate Sample rate in [Hz]
291 * @param channels Number of channels
292 * @param ptime Packet time in [ms]
293 *
294 * @return Number of samples
295 */
calc_nsamp(uint32_t srate,uint8_t channels,uint16_t ptime)296 static inline uint32_t calc_nsamp(uint32_t srate, uint8_t channels,
297 uint16_t ptime)
298 {
299 return srate * channels * ptime / 1000;
300 }
301
302
calc_ptime(size_t nsamp,uint32_t srate,uint8_t channels)303 static inline double calc_ptime(size_t nsamp, uint32_t srate, uint8_t channels)
304 {
305 double ptime;
306
307 ptime = 1000.0 * (double)nsamp / (double)(srate * channels);
308
309 return ptime;
310 }
311
312
313 /**
314 * Get the DSP samplerate for an audio-codec
315 */
get_srate(const struct aucodec * ac)316 static inline uint32_t get_srate(const struct aucodec *ac)
317 {
318 if (!ac)
319 return 0;
320
321 return ac->srate;
322 }
323
324
325 /**
326 * Get the DSP channels for an audio-codec (exception for MPA)
327 */
get_ch(const struct aucodec * ac)328 static inline uint32_t get_ch(const struct aucodec *ac)
329 {
330 if (!ac)
331 return 0;
332
333 return !str_casecmp(ac->name, "MPA") ? 2 : ac->ch;
334 }
335
336
get_framesize(const struct aucodec * ac,uint32_t ptime)337 static inline uint32_t get_framesize(const struct aucodec *ac,
338 uint32_t ptime)
339 {
340 if (!ac)
341 return 0;
342
343 return calc_nsamp(get_srate(ac), get_ch(ac), ptime);
344 }
345
346
aucodec_equal(const struct aucodec * a,const struct aucodec * b)347 static bool aucodec_equal(const struct aucodec *a, const struct aucodec *b)
348 {
349 if (!a || !b)
350 return false;
351
352 return get_srate(a) == get_srate(b) && get_ch(a) == get_ch(b);
353 }
354
355
add_audio_codec(struct audio * a,struct sdp_media * m,struct aucodec * ac)356 static int add_audio_codec(struct audio *a, struct sdp_media *m,
357 struct aucodec *ac)
358 {
359 if (!in_range(&a->cfg.srate, get_srate(ac))) {
360 debug("audio: skip %uHz codec (audio range %uHz - %uHz)\n",
361 get_srate(ac), a->cfg.srate.min, a->cfg.srate.max);
362 return 0;
363 }
364
365 if (!in_range(&a->cfg.channels, get_ch(ac))) {
366 debug("audio: skip codec with %uch (audio range %uch-%uch)\n",
367 get_ch(ac), a->cfg.channels.min, a->cfg.channels.max);
368 return 0;
369 }
370
371 if (ac->crate < 8000) {
372 warning("audio: illegal clock rate %u\n", ac->crate);
373 return EINVAL;
374 }
375
376 return sdp_format_add(NULL, m, false, ac->pt, ac->name, ac->crate,
377 ac->ch, ac->fmtp_ench, ac->fmtp_cmph, ac, false,
378 "%s", ac->fmtp);
379 }
380
381
append_rtpext(struct audio * au,struct mbuf * mb,int16_t * sampv,size_t sampc)382 static int append_rtpext(struct audio *au, struct mbuf *mb,
383 int16_t *sampv, size_t sampc)
384 {
385 uint8_t data[1];
386 double level;
387 int err;
388
389 /* audio level must be calculated from the audio samples that
390 * are actually sent on the network. */
391 level = aulevel_calc_dbov(sampv, sampc);
392
393 data[0] = (int)-level & 0x7f;
394
395 err = rtpext_encode(mb, au->extmap_aulevel, 1, data);
396 if (err) {
397 warning("audio: rtpext_encode failed (%m)\n", err);
398 return err;
399 }
400
401 return err;
402 }
403
404
405 /**
406 * Encoder audio and send via stream
407 *
408 * @note This function has REAL-TIME properties
409 *
410 * @param a Audio object
411 * @param tx Audio transmit object
412 * @param sampv Audio samples
413 * @param sampc Number of audio samples
414 */
encode_rtp_send(struct audio * a,struct autx * tx,int16_t * sampv,size_t sampc)415 static void encode_rtp_send(struct audio *a, struct autx *tx,
416 int16_t *sampv, size_t sampc)
417 {
418 size_t frame_size; /* number of samples per channel */
419 size_t sampc_rtp;
420 size_t len;
421 size_t ext_len = 0;
422 int err;
423
424 if (!tx->ac || !tx->ac->ench)
425 return;
426
427 tx->mb->pos = tx->mb->end = STREAM_PRESZ;
428
429 if (a->level_enabled) {
430
431 /* skip the extension header */
432 tx->mb->pos += RTPEXT_HDR_SIZE;
433
434 err = append_rtpext(a, tx->mb, sampv, sampc);
435 if (err)
436 return;
437
438 ext_len = tx->mb->pos - STREAM_PRESZ;
439
440 /* write the Extension header at the beginning */
441 tx->mb->pos = STREAM_PRESZ;
442
443 err = rtpext_hdr_encode(tx->mb, ext_len - RTPEXT_HDR_SIZE);
444 if (err)
445 return;
446
447 tx->mb->pos = STREAM_PRESZ + ext_len;
448 tx->mb->end = STREAM_PRESZ + ext_len;
449 }
450
451 len = mbuf_get_space(tx->mb);
452
453 err = tx->ac->ench(tx->enc, mbuf_buf(tx->mb), &len,
454 tx->enc_fmt, sampv, sampc);
455
456 if ((err & 0xffff0000) == 0x00010000) {
457 /* MPA needs some special treatment here */
458 tx->ts_ext = err & 0xffff;
459 err = 0;
460 }
461 else if (err) {
462 warning("audio: %s encode error: %d samples (%m)\n",
463 tx->ac->name, sampc, err);
464 goto out;
465 }
466
467 tx->mb->pos = STREAM_PRESZ;
468 tx->mb->end = STREAM_PRESZ + ext_len + len;
469
470 if (mbuf_get_left(tx->mb)) {
471
472 uint32_t rtp_ts = tx->ts_ext & 0xffffffff;
473
474 if (len) {
475 err = stream_send(a->strm, ext_len!=0, tx->marker, -1,
476 rtp_ts, tx->mb);
477 if (err)
478 goto out;
479 }
480 }
481
482 /* Convert from audio samplerate to RTP clockrate */
483 sampc_rtp = sampc * tx->ac->crate / tx->ac->srate;
484
485 /* The RTP clock rate used for generating the RTP timestamp is
486 * independent of the number of channels and the encoding
487 * However, MPA support variable packet durations. Thus, MPA
488 * should update the ts according to its current internal state.
489 */
490 frame_size = sampc_rtp / get_ch(tx->ac);
491
492 tx->ts_ext += (uint32_t)frame_size;
493
494 out:
495 tx->marker = false;
496 }
497
498
499 /*
500 * @note This function has REAL-TIME properties
501 */
poll_aubuf_tx(struct audio * a)502 static void poll_aubuf_tx(struct audio *a)
503 {
504 struct autx *tx = &a->tx;
505 int16_t *sampv = tx->sampv;
506 size_t sampc;
507 size_t sz;
508 size_t num_bytes;
509 struct le *le;
510 int err = 0;
511
512 sz = aufmt_sample_size(tx->src_fmt);
513 if (!sz)
514 return;
515
516 num_bytes = tx->psize;
517 sampc = tx->psize / sz;
518
519 /* timed read from audio-buffer */
520
521 if (tx->src_fmt == tx->enc_fmt) {
522
523 aubuf_read(tx->aubuf, (uint8_t *)tx->sampv, num_bytes);
524 }
525 else if (tx->enc_fmt == AUFMT_S16LE) {
526
527 /* Convert from ausrc format to 16-bit format */
528
529 void *tmp_sampv;
530
531 if (!tx->need_conv) {
532 info("audio: NOTE: source sample conversion"
533 " needed: %s --> %s\n",
534 aufmt_name(tx->src_fmt), aufmt_name(AUFMT_S16LE));
535 tx->need_conv = true;
536 }
537
538 tmp_sampv = mem_zalloc(num_bytes, NULL);
539 if (!tmp_sampv)
540 return;
541
542 aubuf_read(tx->aubuf, tmp_sampv, num_bytes);
543
544 auconv_to_s16(sampv, tx->src_fmt, tmp_sampv, sampc);
545
546 mem_deref(tmp_sampv);
547 }
548 else {
549 warning("audio: tx: invalid sample formats (%s -> %s)\n",
550 aufmt_name(tx->src_fmt),
551 aufmt_name(tx->enc_fmt));
552 }
553
554 /* optional resampler */
555 if (tx->resamp.resample) {
556 size_t sampc_rs = AUDIO_SAMPSZ;
557
558 if (tx->enc_fmt != AUFMT_S16LE) {
559 warning("audio: skipping resampler due to"
560 " incompatible format (%s)\n",
561 aufmt_name(tx->enc_fmt));
562 return;
563 }
564
565 err = auresamp(&tx->resamp,
566 tx->sampv_rs, &sampc_rs,
567 tx->sampv, sampc);
568 if (err)
569 return;
570
571 sampv = tx->sampv_rs;
572 sampc = sampc_rs;
573 }
574
575 if (tx->enc_fmt == AUFMT_S16LE) {
576
577 /* Process exactly one audio-frame in list order */
578 for (le = tx->filtl.head; le; le = le->next) {
579 struct aufilt_enc_st *st = le->data;
580
581 if (st->af && st->af->ench)
582 err |= st->af->ench(st, sampv, &sampc);
583 }
584 if (err) {
585 warning("audio: aufilter encode: %m\n", err);
586 }
587 }
588 else if (!list_isempty(&tx->filtl)) {
589 warning("audio: skipping audio-filters due to"
590 " incompatible format (%s)\n",
591 aufmt_name(tx->enc_fmt));
592 }
593
594 /* Encode and send */
595 encode_rtp_send(a, tx, sampv, sampc);
596 }
597
598
check_telev(struct audio * a,struct autx * tx)599 static void check_telev(struct audio *a, struct autx *tx)
600 {
601 const struct sdp_format *fmt;
602 bool marker = false;
603 int err;
604
605 tx->mb->pos = tx->mb->end = STREAM_PRESZ;
606
607 err = telev_poll(a->telev, &marker, tx->mb);
608 if (err)
609 return;
610
611 if (marker)
612 tx->ts_tel = (uint32_t)tx->ts_ext;
613
614 fmt = sdp_media_rformat(stream_sdpmedia(audio_strm(a)), telev_rtpfmt);
615 if (!fmt)
616 return;
617
618 tx->mb->pos = STREAM_PRESZ;
619 err = stream_send(a->strm, false, marker, fmt->pt, tx->ts_tel, tx->mb);
620 if (err) {
621 warning("audio: telev: stream_send %m\n", err);
622 }
623 }
624
625
626 /**
627 * Write samples to Audio Player.
628 *
629 * @note This function has REAL-TIME properties
630 *
631 * @note The application is responsible for filling in silence in
632 * the case of underrun
633 *
634 * @note This function may be called from any thread
635 *
636 * @note The sample format is set in rx->play_fmt
637 *
638 * @param buf Buffer to fill with audio samples
639 * @param sz Number of bytes in buffer
640 * @param arg Handler argument
641 */
auplay_write_handler(void * sampv,size_t sampc,void * arg)642 static void auplay_write_handler(void *sampv, size_t sampc, void *arg)
643 {
644 struct aurx *rx = arg;
645 size_t num_bytes = sampc * aufmt_sample_size(rx->play_fmt);
646
647 if (rx->aubuf_started && aubuf_cur_size(rx->aubuf) < num_bytes) {
648
649 ++rx->stats.aubuf_underrun;
650
651 debug("audio: rx aubuf underrun (total %llu)\n",
652 rx->stats.aubuf_underrun);
653 }
654
655 aubuf_read(rx->aubuf, sampv, num_bytes);
656 }
657
658
659 /**
660 * Read samples from Audio Source
661 *
662 * @note This function has REAL-TIME properties
663 *
664 * @note This function may be called from any thread
665 *
666 * @param buf Buffer with audio samples
667 * @param sz Number of bytes in buffer
668 * @param arg Handler argument
669 */
ausrc_read_handler(const void * sampv,size_t sampc,void * arg)670 static void ausrc_read_handler(const void *sampv, size_t sampc, void *arg)
671 {
672 struct audio *a = arg;
673 struct autx *tx = &a->tx;
674 size_t num_bytes = sampc * aufmt_sample_size(tx->src_fmt);
675
676 if (tx->muted)
677 memset((void *)sampv, 0, num_bytes);
678
679 if (aubuf_cur_size(tx->aubuf) >= tx->aubuf_maxsz) {
680
681 ++tx->stats.aubuf_overrun;
682
683 debug("audio: tx aubuf overrun (total %llu)\n",
684 tx->stats.aubuf_overrun);
685 }
686
687 (void)aubuf_write(tx->aubuf, sampv, num_bytes);
688
689 tx->aubuf_started = true;
690
691 if (a->cfg.txmode == AUDIO_MODE_POLL) {
692 unsigned i;
693
694 for (i=0; i<16; i++) {
695
696 if (aubuf_cur_size(tx->aubuf) < tx->psize)
697 break;
698
699 poll_aubuf_tx(a);
700 }
701 }
702
703 /* Exact timing: send Telephony-Events from here */
704 check_telev(a, tx);
705 }
706
707
ausrc_error_handler(int err,const char * str,void * arg)708 static void ausrc_error_handler(int err, const char *str, void *arg)
709 {
710 struct audio *a = arg;
711 MAGIC_CHECK(a);
712
713 if (a->errh)
714 a->errh(err, str, a->arg);
715 }
716
717
pt_handler(struct audio * a,uint8_t pt_old,uint8_t pt_new)718 static int pt_handler(struct audio *a, uint8_t pt_old, uint8_t pt_new)
719 {
720 const struct sdp_format *lc;
721
722 lc = sdp_media_lformat(stream_sdpmedia(a->strm), pt_new);
723 if (!lc)
724 return ENOENT;
725
726 if (pt_old != (uint8_t)-1) {
727 info("Audio decoder changed payload %u -> %u\n",
728 pt_old, pt_new);
729 }
730
731 a->rx.pt = pt_new;
732
733 return audio_decoder_set(a, lc->data, lc->pt, lc->params);
734 }
735
736
handle_telev(struct audio * a,struct mbuf * mb)737 static void handle_telev(struct audio *a, struct mbuf *mb)
738 {
739 int event, digit;
740 bool end;
741
742 if (telev_recv(a->telev, mb, &event, &end))
743 return;
744
745 digit = telev_code2digit(event);
746 if (digit >= 0 && a->eventh)
747 a->eventh(digit, end, a->arg);
748 }
749
750
aurx_stream_decode(struct aurx * rx,struct mbuf * mb)751 static int aurx_stream_decode(struct aurx *rx, struct mbuf *mb)
752 {
753 size_t sampc = AUDIO_SAMPSZ;
754 void *sampv;
755 struct le *le;
756 int err = 0;
757
758 /* No decoder set */
759 if (!rx->ac)
760 return 0;
761
762 if (mbuf_get_left(mb)) {
763
764 err = rx->ac->dech(rx->dec,
765 rx->dec_fmt, rx->sampv, &sampc,
766 mbuf_buf(mb), mbuf_get_left(mb));
767
768 }
769 else if (rx->ac->plch && rx->dec_fmt == AUFMT_S16LE) {
770 sampc = rx->ac->srate * rx->ac->ch * rx->ptime / 1000;
771
772 err = rx->ac->plch(rx->dec, rx->dec_fmt, rx->sampv, &sampc);
773 }
774 else {
775 /* no PLC in the codec, might be done in filters below */
776 sampc = 0;
777 }
778
779 if (err) {
780 warning("audio: %s codec decode %u bytes: %m\n",
781 rx->ac->name, mbuf_get_left(mb), err);
782 goto out;
783 }
784
785 if (rx->dec_fmt == AUFMT_S16LE) {
786 /* Process exactly one audio-frame in reverse list order */
787 for (le = rx->filtl.tail; le; le = le->prev) {
788 struct aufilt_dec_st *st = le->data;
789
790 if (st->af && st->af->dech)
791 err |= st->af->dech(st, rx->sampv, &sampc);
792 }
793 }
794 else if (!list_isempty(&rx->filtl)) {
795 warning("audio: skipping audio-filters due to"
796 " incompatible format (%s)\n",
797 aufmt_name(rx->dec_fmt));
798 }
799
800 if (!rx->aubuf)
801 goto out;
802
803 sampv = rx->sampv;
804
805 /* optional resampler */
806 if (rx->resamp.resample) {
807 size_t sampc_rs = AUDIO_SAMPSZ;
808
809 if (rx->dec_fmt != AUFMT_S16LE) {
810 warning("audio: skipping resampler due to"
811 " incompatible format (%s)\n",
812 aufmt_name(rx->dec_fmt));
813 return ENOTSUP;
814 }
815
816 err = auresamp(&rx->resamp,
817 rx->sampv_rs, &sampc_rs,
818 rx->sampv, sampc);
819 if (err)
820 return err;
821
822 sampv = rx->sampv_rs;
823 sampc = sampc_rs;
824 }
825
826 if (aubuf_cur_size(rx->aubuf) >= rx->aubuf_maxsz) {
827
828 ++rx->stats.aubuf_overrun;
829
830 debug("audio: rx aubuf overrun (total %llu)\n",
831 rx->stats.aubuf_overrun);
832 }
833
834 if (rx->play_fmt == rx->dec_fmt) {
835
836 size_t num_bytes = sampc * aufmt_sample_size(rx->play_fmt);
837
838 err = aubuf_write(rx->aubuf, sampv, num_bytes);
839 if (err)
840 goto out;
841 }
842 else if (rx->dec_fmt == AUFMT_S16LE) {
843
844 /* Convert from 16-bit to auplay format */
845 void *tmp_sampv;
846 size_t num_bytes = sampc * aufmt_sample_size(rx->play_fmt);
847
848 if (!rx->need_conv) {
849 info("audio: NOTE: playback sample conversion"
850 " needed: %s --> %s\n",
851 aufmt_name(AUFMT_S16LE),
852 aufmt_name(rx->play_fmt));
853 rx->need_conv = true;
854 }
855
856 tmp_sampv = mem_zalloc(num_bytes, NULL);
857 if (!tmp_sampv)
858 return ENOMEM;
859
860 auconv_from_s16(rx->play_fmt, tmp_sampv, sampv, sampc);
861
862 err = aubuf_write(rx->aubuf, tmp_sampv, num_bytes);
863
864 mem_deref(tmp_sampv);
865
866 if (err)
867 goto out;
868 }
869 else {
870 warning("audio: decode: invalid sample formats (%s -> %s)\n",
871 aufmt_name(rx->dec_fmt),
872 aufmt_name(rx->play_fmt));
873 }
874
875 rx->aubuf_started = true;
876
877 out:
878 return err;
879 }
880
881
882 /* Handle incoming stream data from the network */
stream_recv_handler(const struct rtp_header * hdr,struct rtpext * extv,size_t extc,struct mbuf * mb,void * arg)883 static void stream_recv_handler(const struct rtp_header *hdr,
884 struct rtpext *extv, size_t extc,
885 struct mbuf *mb, void *arg)
886 {
887 struct audio *a = arg;
888 struct aurx *rx = &a->rx;
889 bool discard = false;
890 size_t i;
891 int wrap;
892 int err;
893
894 if (!mb)
895 goto out;
896
897 /* Telephone event? */
898 if (hdr->pt != rx->pt) {
899 const struct sdp_format *fmt;
900
901 fmt = sdp_media_lformat(stream_sdpmedia(a->strm), hdr->pt);
902
903 if (fmt && !str_casecmp(fmt->name, "telephone-event")) {
904 handle_telev(a, mb);
905 return;
906 }
907 }
908
909 /* Comfort Noise (CN) as of RFC 3389 */
910 if (PT_CN == hdr->pt)
911 return;
912
913 /* Audio payload-type changed? */
914 /* XXX: this logic should be moved to stream.c */
915 if (hdr->pt != rx->pt) {
916
917 err = pt_handler(a, rx->pt, hdr->pt);
918 if (err)
919 return;
920 }
921
922 /* RFC 5285 -- A General Mechanism for RTP Header Extensions */
923 for (i=0; i<extc; i++) {
924
925 if (extv[i].id == a->extmap_aulevel) {
926
927 a->rx.level_last = -(double)(extv[i].data[0] & 0x7f);
928 a->rx.level_set = true;
929 }
930 else {
931 info("audio: rtp header ext ignored (id=%u)\n",
932 extv[i].id);
933 }
934 }
935
936 /* Save timestamp for incoming RTP packets */
937
938 if (rx->ts_recv.is_set) {
939
940 uint64_t ext_last, ext_now;
941
942 ext_last = calc_extended_timestamp(rx->ts_recv.num_wraps,
943 rx->ts_recv.last);
944
945 ext_now = calc_extended_timestamp(rx->ts_recv.num_wraps,
946 hdr->ts);
947
948 if (ext_now <= ext_last) {
949 uint64_t delta;
950
951 delta = ext_last - ext_now;
952
953 warning("audio: [time=%.3f]"
954 " discard old frame (%.3f seconds old)\n",
955 aurx_calc_seconds(rx),
956 audio_calc_seconds(delta, rx->ac->crate));
957
958 discard = true;
959 }
960 }
961 else {
962 rx->ts_recv.first = hdr->ts;
963 rx->ts_recv.last = hdr->ts;
964 rx->ts_recv.is_set = true;
965 }
966
967 wrap = timestamp_wrap(hdr->ts, rx->ts_recv.last);
968
969 switch (wrap) {
970
971 case -1:
972 warning("audio: rtp timestamp wraps backwards"
973 " (delta = %d) -- discard\n",
974 (int32_t)(rx->ts_recv.last - hdr->ts));
975 discard = true;
976 break;
977
978 case 0:
979 break;
980
981 case 1:
982 ++rx->ts_recv.num_wraps;
983 break;
984
985 default:
986 break;
987 }
988
989 rx->ts_recv.last = hdr->ts;
990
991 #if 0
992 re_printf("[time=%.3f] wrap=%d discard=%d\n",
993 aurx_calc_seconds(rx), wrap, discard);
994 #endif
995
996 if (discard) {
997 ++a->rx.n_discard;
998 return;
999 }
1000
1001 out:
1002 (void)aurx_stream_decode(&a->rx, mb);
1003 }
1004
1005
add_telev_codec(struct audio * a)1006 static int add_telev_codec(struct audio *a)
1007 {
1008 struct sdp_media *m = stream_sdpmedia(audio_strm(a));
1009 struct sdp_format *sf;
1010 int err;
1011
1012 /* Use payload-type 101 if available, for CiscoGW interop */
1013 err = sdp_format_add(&sf, m, false,
1014 (!sdp_media_lformat(m, 101)) ? "101" : NULL,
1015 telev_rtpfmt, TELEV_SRATE, 1, NULL,
1016 NULL, NULL, false, "0-15");
1017 if (err)
1018 return err;
1019
1020 return err;
1021 }
1022
1023
1024 /*
1025 * EBU ACIP (Audio Contribution over IP) Profile
1026 *
1027 * Ref: https://tech.ebu.ch/docs/tech/tech3368.pdf
1028 */
set_ebuacip_params(struct audio * au,uint32_t ptime)1029 static int set_ebuacip_params(struct audio *au, uint32_t ptime)
1030 {
1031 struct sdp_media *sdp = stream_sdpmedia(au->strm);
1032 const struct config_avt *avt = &au->strm->cfg;
1033 char str[64];
1034 int jbvalue = 0;
1035 int jb_id = 0;
1036 int err = 0;
1037
1038 /* set ebuacip version fixed value 0 for now. */
1039 err |= sdp_media_set_lattr(sdp, false, "ebuacip", "version %i", 0);
1040
1041 /* set jb option, only one in our case */
1042 err |= sdp_media_set_lattr(sdp, false, "ebuacip", "jb %i", jb_id);
1043
1044 /* define jb value in option */
1045 if (0 == conf_get_str(conf_cur(), "ebuacip_jb_type",str,sizeof(str))) {
1046
1047 if (0 == str_cmp(str, "auto")) {
1048
1049 err |= sdp_media_set_lattr(sdp, false,
1050 "ebuacip",
1051 "jbdef %i auto %d-%d",
1052 jb_id,
1053 avt->jbuf_del.min * ptime,
1054 avt->jbuf_del.max * ptime);
1055 }
1056 else if (0 == str_cmp(str, "fixed")) {
1057
1058 /* define jb value in option */
1059 jbvalue = avt->jbuf_del.max * ptime;
1060
1061 err |= sdp_media_set_lattr(sdp, false,
1062 "ebuacip",
1063 "jbdef %i fixed %d",
1064 jb_id, jbvalue);
1065 }
1066 }
1067
1068 /* set QOS recomendation use tos / 4 to set DSCP value */
1069 err |= sdp_media_set_lattr(sdp, false, "ebuacip", "qosrec %u",
1070 avt->rtp_tos / 4);
1071
1072 /* EBU ACIP FEC:: NOT SET IN BARESIP */
1073
1074 return err;
1075 }
1076
1077
audio_alloc(struct audio ** ap,const struct stream_param * stream_prm,const struct config * cfg,struct call * call,struct sdp_session * sdp_sess,int label,const struct mnat * mnat,struct mnat_sess * mnat_sess,const struct menc * menc,struct menc_sess * menc_sess,uint32_t ptime,const struct list * aucodecl,bool offerer,audio_event_h * eventh,audio_err_h * errh,void * arg)1078 int audio_alloc(struct audio **ap, const struct stream_param *stream_prm,
1079 const struct config *cfg,
1080 struct call *call, struct sdp_session *sdp_sess, int label,
1081 const struct mnat *mnat, struct mnat_sess *mnat_sess,
1082 const struct menc *menc, struct menc_sess *menc_sess,
1083 uint32_t ptime, const struct list *aucodecl, bool offerer,
1084 audio_event_h *eventh, audio_err_h *errh, void *arg)
1085 {
1086 struct audio *a;
1087 struct autx *tx;
1088 struct aurx *rx;
1089 struct le *le;
1090 int err;
1091 (void)offerer;
1092
1093 if (!ap || !cfg)
1094 return EINVAL;
1095
1096 a = mem_zalloc(sizeof(*a), audio_destructor);
1097 if (!a)
1098 return ENOMEM;
1099
1100 MAGIC_INIT(a);
1101
1102 a->cfg = cfg->audio;
1103 tx = &a->tx;
1104 rx = &a->rx;
1105
1106 tx->src_fmt = cfg->audio.src_fmt;
1107 rx->play_fmt = cfg->audio.play_fmt;
1108
1109 tx->enc_fmt = cfg->audio.enc_fmt;
1110 rx->dec_fmt = cfg->audio.dec_fmt;
1111
1112 err = stream_alloc(&a->strm, stream_prm, &cfg->avt, call, sdp_sess,
1113 "audio", label,
1114 mnat, mnat_sess, menc, menc_sess,
1115 call_localuri(call),
1116 stream_recv_handler, NULL, a);
1117 if (err)
1118 goto out;
1119
1120 if (cfg->avt.rtp_bw.max) {
1121 stream_set_bw(a->strm, AUDIO_BANDWIDTH);
1122 }
1123
1124 err = sdp_media_set_lattr(stream_sdpmedia(a->strm), true,
1125 "ptime", "%u", ptime);
1126 if (err)
1127 goto out;
1128
1129 if (cfg->audio.level && offerer) {
1130
1131 a->extmap_aulevel = 1;
1132
1133 err = sdp_media_set_lattr(stream_sdpmedia(a->strm), true,
1134 "extmap",
1135 "%u %s",
1136 a->extmap_aulevel, uri_aulevel);
1137 if (err)
1138 goto out;
1139 }
1140
1141 if (cfg->sdp.ebuacip) {
1142
1143 err = set_ebuacip_params(a, ptime);
1144 if (err)
1145 goto out;
1146 }
1147
1148 /* Audio codecs */
1149 for (le = list_head(aucodecl); le; le = le->next) {
1150 err = add_audio_codec(a, stream_sdpmedia(a->strm), le->data);
1151 if (err)
1152 goto out;
1153 }
1154
1155 tx->mb = mbuf_alloc(STREAM_PRESZ + 4096);
1156 tx->sampv = mem_zalloc(AUDIO_SAMPSZ * aufmt_sample_size(tx->enc_fmt),
1157 NULL);
1158
1159 rx->sampv = mem_zalloc(AUDIO_SAMPSZ * aufmt_sample_size(rx->dec_fmt),
1160 NULL);
1161 if (!tx->mb || !tx->sampv || !rx->sampv) {
1162 err = ENOMEM;
1163 goto out;
1164 }
1165
1166 err = telev_alloc(&a->telev, ptime);
1167 if (err)
1168 goto out;
1169
1170 err = add_telev_codec(a);
1171 if (err)
1172 goto out;
1173
1174 auresamp_init(&tx->resamp);
1175 str_ncpy(tx->device, a->cfg.src_dev, sizeof(tx->device));
1176 tx->ptime = ptime;
1177 tx->ts_ext = tx->ts_base = rand_u16();
1178 tx->marker = true;
1179
1180 auresamp_init(&rx->resamp);
1181 str_ncpy(rx->device, a->cfg.play_dev, sizeof(rx->device));
1182 rx->pt = -1;
1183 rx->ptime = ptime;
1184
1185 a->eventh = eventh;
1186 a->errh = errh;
1187 a->arg = arg;
1188
1189 out:
1190 if (err)
1191 mem_deref(a);
1192 else
1193 *ap = a;
1194
1195 return err;
1196 }
1197
1198
1199 #ifdef HAVE_PTHREAD
tx_thread(void * arg)1200 static void *tx_thread(void *arg)
1201 {
1202 struct audio *a = arg;
1203 struct autx *tx = &a->tx;
1204 uint64_t ts = 0;
1205
1206 while (a->tx.u.thr.run) {
1207
1208 uint64_t now;
1209
1210 sys_msleep(4);
1211
1212 if (!tx->aubuf_started)
1213 continue;
1214
1215 if (!a->tx.u.thr.run)
1216 break;
1217
1218 now = tmr_jiffies();
1219 if (!ts)
1220 ts = now;
1221
1222 if (ts > now)
1223 continue;
1224
1225 /* Now is the time to send */
1226
1227 if (aubuf_cur_size(tx->aubuf) >= tx->psize) {
1228
1229 poll_aubuf_tx(a);
1230 }
1231 else {
1232 ++tx->stats.aubuf_underrun;
1233
1234 debug("audio: thread: tx aubuf underrun"
1235 " (total %llu)\n", tx->stats.aubuf_underrun);
1236 }
1237
1238 ts += tx->ptime;
1239 }
1240
1241 return NULL;
1242 }
1243 #endif
1244
1245
aufilt_param_set(struct aufilt_prm * prm,const struct aucodec * ac,uint32_t ptime)1246 static void aufilt_param_set(struct aufilt_prm *prm,
1247 const struct aucodec *ac, uint32_t ptime)
1248 {
1249 if (!ac) {
1250 memset(prm, 0, sizeof(*prm));
1251 return;
1252 }
1253
1254 prm->srate = get_srate(ac);
1255 prm->ch = get_ch(ac);
1256 prm->ptime = ptime;
1257 }
1258
1259
autx_print_pipeline(struct re_printf * pf,const struct autx * autx)1260 static int autx_print_pipeline(struct re_printf *pf, const struct autx *autx)
1261 {
1262 struct le *le;
1263 int err;
1264
1265 if (!autx)
1266 return 0;
1267
1268 err = re_hprintf(pf, "audio tx pipeline: %10s",
1269 autx->ausrc ? autx->ausrc->as->name : "src");
1270
1271 for (le = list_head(&autx->filtl); le; le = le->next) {
1272 struct aufilt_enc_st *st = le->data;
1273
1274 if (st->af->ench)
1275 err |= re_hprintf(pf, " ---> %s", st->af->name);
1276 }
1277
1278 err |= re_hprintf(pf, " ---> %s\n",
1279 autx->ac ? autx->ac->name : "encoder");
1280
1281 return err;
1282 }
1283
1284
aurx_print_pipeline(struct re_printf * pf,const struct aurx * aurx)1285 static int aurx_print_pipeline(struct re_printf *pf, const struct aurx *aurx)
1286 {
1287 struct le *le;
1288 int err;
1289
1290 if (!aurx)
1291 return 0;
1292
1293 err = re_hprintf(pf, "audio rx pipeline: %10s",
1294 aurx->auplay ? aurx->auplay->ap->name : "play");
1295
1296 for (le = list_head(&aurx->filtl); le; le = le->next) {
1297 struct aufilt_dec_st *st = le->data;
1298
1299 if (st->af->dech)
1300 err |= re_hprintf(pf, " <--- %s", st->af->name);
1301 }
1302
1303 err |= re_hprintf(pf, " <--- %s\n",
1304 aurx->ac ? aurx->ac->name : "decoder");
1305
1306 return err;
1307 }
1308
1309
1310 /**
1311 * Setup the audio-filter chain
1312 *
1313 * must be called before auplay/ausrc-alloc
1314 *
1315 * @param a Audio object
1316 *
1317 * @return 0 if success, otherwise errorcode
1318 */
aufilt_setup(struct audio * a)1319 static int aufilt_setup(struct audio *a)
1320 {
1321 struct aufilt_prm encprm, decprm;
1322 struct autx *tx = &a->tx;
1323 struct aurx *rx = &a->rx;
1324 struct le *le;
1325 int err = 0;
1326
1327 /* wait until we have both Encoder and Decoder */
1328 if (!tx->ac || !rx->ac)
1329 return 0;
1330
1331 if (!list_isempty(&tx->filtl) || !list_isempty(&rx->filtl))
1332 return 0;
1333
1334 aufilt_param_set(&encprm, tx->ac, tx->ptime);
1335 aufilt_param_set(&decprm, rx->ac, rx->ptime);
1336
1337 /* Audio filters */
1338 for (le = list_head(baresip_aufiltl()); le; le = le->next) {
1339 struct aufilt *af = le->data;
1340 struct aufilt_enc_st *encst = NULL;
1341 struct aufilt_dec_st *decst = NULL;
1342 void *ctx = NULL;
1343
1344 if (af->encupdh) {
1345 err |= af->encupdh(&encst, &ctx, af, &encprm);
1346 if (err)
1347 break;
1348
1349 encst->af = af;
1350 list_append(&tx->filtl, &encst->le, encst);
1351 }
1352
1353 if (af->decupdh) {
1354 err |= af->decupdh(&decst, &ctx, af, &decprm);
1355 if (err)
1356 break;
1357
1358 decst->af = af;
1359 list_append(&rx->filtl, &decst->le, decst);
1360 }
1361
1362 if (err) {
1363 warning("audio: audio-filter '%s'"
1364 " update failed (%m)\n", af->name, err);
1365 break;
1366 }
1367 }
1368
1369 return 0;
1370 }
1371
1372
start_player(struct aurx * rx,struct audio * a)1373 static int start_player(struct aurx *rx, struct audio *a)
1374 {
1375 const struct aucodec *ac = rx->ac;
1376 uint32_t srate_dsp = get_srate(ac);
1377 uint32_t channels_dsp;
1378 bool resamp = false;
1379 int err;
1380
1381 if (!ac)
1382 return 0;
1383
1384 channels_dsp = get_ch(ac);
1385
1386 if (a->cfg.srate_play && a->cfg.srate_play != srate_dsp) {
1387 resamp = true;
1388 srate_dsp = a->cfg.srate_play;
1389 }
1390 if (a->cfg.channels_play && a->cfg.channels_play != channels_dsp) {
1391 resamp = true;
1392 channels_dsp = a->cfg.channels_play;
1393 }
1394
1395 /* Optional resampler, if configured */
1396 if (resamp && !rx->sampv_rs) {
1397
1398 info("audio: enable auplay resampler:"
1399 " %uHz/%uch --> %uHz/%uch\n",
1400 get_srate(ac), get_ch(ac), srate_dsp, channels_dsp);
1401
1402 rx->sampv_rs = mem_zalloc(AUDIO_SAMPSZ * 2, NULL);
1403 if (!rx->sampv_rs)
1404 return ENOMEM;
1405
1406 err = auresamp_setup(&rx->resamp,
1407 get_srate(ac), get_ch(ac),
1408 srate_dsp, channels_dsp);
1409 if (err) {
1410 warning("audio: could not setup auplay resampler"
1411 " (%m)\n", err);
1412 return err;
1413 }
1414 }
1415
1416 /* Start Audio Player */
1417 if (!rx->auplay && auplay_find(baresip_auplayl(), NULL)) {
1418
1419 struct auplay_prm prm;
1420
1421 prm.srate = srate_dsp;
1422 prm.ch = channels_dsp;
1423 prm.ptime = rx->ptime;
1424 prm.fmt = rx->play_fmt;
1425
1426 if (!rx->aubuf) {
1427 size_t psize;
1428 size_t sz = aufmt_sample_size(rx->play_fmt);
1429
1430 psize = sz * calc_nsamp(prm.srate, prm.ch, prm.ptime);
1431
1432 rx->aubuf_maxsz = psize * 8;
1433
1434 err = aubuf_alloc(&rx->aubuf, psize * 1,
1435 rx->aubuf_maxsz);
1436 if (err)
1437 return err;
1438 }
1439
1440 err = auplay_alloc(&rx->auplay, baresip_auplayl(),
1441 a->cfg.play_mod,
1442 &prm, rx->device,
1443 auplay_write_handler, rx);
1444 if (err) {
1445 warning("audio: start_player failed (%s.%s): %m\n",
1446 a->cfg.play_mod, rx->device, err);
1447 return err;
1448 }
1449
1450 rx->auplay_prm = prm;
1451
1452 info("audio: player started with sample format %s\n",
1453 aufmt_name(rx->play_fmt));
1454 }
1455
1456 return 0;
1457 }
1458
1459
start_source(struct autx * tx,struct audio * a)1460 static int start_source(struct autx *tx, struct audio *a)
1461 {
1462 const struct aucodec *ac = tx->ac;
1463 uint32_t srate_dsp = get_srate(ac);
1464 uint32_t channels_dsp;
1465 bool resamp = false;
1466 int err;
1467
1468 if (!ac)
1469 return 0;
1470
1471 channels_dsp = get_ch(ac);
1472
1473 if (a->cfg.srate_src && a->cfg.srate_src != srate_dsp) {
1474 resamp = true;
1475 srate_dsp = a->cfg.srate_src;
1476 }
1477 if (a->cfg.channels_src && a->cfg.channels_src != channels_dsp) {
1478 resamp = true;
1479 channels_dsp = a->cfg.channels_src;
1480 }
1481
1482 /* Optional resampler, if configured */
1483 if (resamp && !tx->sampv_rs) {
1484
1485 info("audio: enable ausrc resampler:"
1486 " %uHz/%uch <-- %uHz/%uch\n",
1487 get_srate(ac), get_ch(ac), srate_dsp, channels_dsp);
1488
1489 tx->sampv_rs = mem_zalloc(AUDIO_SAMPSZ * 2, NULL);
1490 if (!tx->sampv_rs)
1491 return ENOMEM;
1492
1493 err = auresamp_setup(&tx->resamp,
1494 srate_dsp, channels_dsp,
1495 get_srate(ac), get_ch(ac));
1496 if (err) {
1497 warning("audio: could not setup ausrc resampler"
1498 " (%m)\n", err);
1499 return err;
1500 }
1501 }
1502
1503 /* Start Audio Source */
1504 if (!tx->ausrc && ausrc_find(baresip_ausrcl(), NULL)) {
1505
1506 struct ausrc_prm prm;
1507 size_t sz;
1508
1509 prm.srate = srate_dsp;
1510 prm.ch = channels_dsp;
1511 prm.ptime = tx->ptime;
1512 prm.fmt = tx->src_fmt;
1513
1514 sz = aufmt_sample_size(tx->src_fmt);
1515
1516 tx->psize = sz * calc_nsamp(prm.srate, prm.ch, prm.ptime);
1517
1518 tx->aubuf_maxsz = tx->psize * 30;
1519
1520 if (!tx->aubuf) {
1521 err = aubuf_alloc(&tx->aubuf, tx->psize,
1522 tx->aubuf_maxsz);
1523 if (err)
1524 return err;
1525 }
1526
1527 err = ausrc_alloc(&tx->ausrc, baresip_ausrcl(),
1528 NULL, a->cfg.src_mod,
1529 &prm, tx->device,
1530 ausrc_read_handler, ausrc_error_handler, a);
1531 if (err) {
1532 warning("audio: start_source failed (%s.%s): %m\n",
1533 a->cfg.src_mod, tx->device, err);
1534 return err;
1535 }
1536
1537 switch (a->cfg.txmode) {
1538 #ifdef HAVE_PTHREAD
1539 case AUDIO_MODE_THREAD:
1540 if (!tx->u.thr.run) {
1541 tx->u.thr.run = true;
1542 err = pthread_create(&tx->u.thr.tid, NULL,
1543 tx_thread, a);
1544 if (err) {
1545 tx->u.thr.tid = false;
1546 return err;
1547 }
1548 }
1549 break;
1550 #endif
1551
1552 default:
1553 break;
1554 }
1555
1556 tx->ausrc_prm = prm;
1557
1558 info("audio: source started with sample format %s\n",
1559 aufmt_name(tx->src_fmt));
1560 }
1561
1562 return 0;
1563 }
1564
1565
1566 /**
1567 * Start the audio playback and recording
1568 *
1569 * @param a Audio object
1570 *
1571 * @return 0 if success, otherwise errorcode
1572 */
audio_start(struct audio * a)1573 int audio_start(struct audio *a)
1574 {
1575 int err;
1576
1577 if (!a)
1578 return EINVAL;
1579
1580 /* Audio filter */
1581 if (!list_isempty(baresip_aufiltl())) {
1582 err = aufilt_setup(a);
1583 if (err)
1584 return err;
1585 }
1586
1587 /* configurable order of play/src start */
1588 if (a->cfg.src_first) {
1589 err = start_source(&a->tx, a);
1590 err |= start_player(&a->rx, a);
1591 }
1592 else {
1593 err = start_player(&a->rx, a);
1594 err |= start_source(&a->tx, a);
1595 }
1596 if (err)
1597 return err;
1598
1599 if (a->tx.ac && a->rx.ac) {
1600
1601 if (!a->started) {
1602 info("%H%H",
1603 autx_print_pipeline, &a->tx,
1604 aurx_print_pipeline, &a->rx);
1605 }
1606
1607 a->started = true;
1608 }
1609
1610 return err;
1611 }
1612
1613
1614 /**
1615 * Stop the audio playback and recording
1616 *
1617 * @param a Audio object
1618 */
audio_stop(struct audio * a)1619 void audio_stop(struct audio *a)
1620 {
1621 if (!a)
1622 return;
1623
1624 stop_tx(&a->tx, a);
1625 stop_rx(&a->rx);
1626 }
1627
1628
audio_encoder_set(struct audio * a,const struct aucodec * ac,int pt_tx,const char * params)1629 int audio_encoder_set(struct audio *a, const struct aucodec *ac,
1630 int pt_tx, const char *params)
1631 {
1632 struct autx *tx;
1633 int err = 0;
1634 bool reset;
1635
1636 if (!a || !ac)
1637 return EINVAL;
1638
1639 tx = &a->tx;
1640
1641 reset = !aucodec_equal(ac, tx->ac);
1642
1643 if (ac != tx->ac) {
1644 info("audio: Set audio encoder: %s %uHz %dch\n",
1645 ac->name, get_srate(ac), get_ch(ac));
1646
1647 /* Audio source must be stopped first */
1648 if (reset) {
1649 tx->ausrc = mem_deref(tx->ausrc);
1650 }
1651
1652 tx->enc = mem_deref(tx->enc);
1653 tx->ac = ac;
1654 }
1655
1656 if (ac->encupdh) {
1657 struct auenc_param prm;
1658
1659 prm.ptime = tx->ptime;
1660 prm.bitrate = 0; /* auto */
1661
1662 err = ac->encupdh(&tx->enc, ac, &prm, params);
1663 if (err) {
1664 warning("audio: alloc encoder: %m\n", err);
1665 return err;
1666 }
1667 }
1668
1669 stream_set_srate(a->strm, ac->crate, ac->crate);
1670 stream_update_encoder(a->strm, pt_tx);
1671
1672 telev_set_srate(a->telev, ac->crate);
1673
1674 if (!tx->ausrc) {
1675 err |= audio_start(a);
1676 }
1677
1678 return err;
1679 }
1680
1681
audio_decoder_set(struct audio * a,const struct aucodec * ac,int pt_rx,const char * params)1682 int audio_decoder_set(struct audio *a, const struct aucodec *ac,
1683 int pt_rx, const char *params)
1684 {
1685 struct aurx *rx;
1686 bool reset = false;
1687 int err = 0;
1688
1689 if (!a || !ac)
1690 return EINVAL;
1691
1692 rx = &a->rx;
1693
1694 reset = !aucodec_equal(ac, rx->ac);
1695
1696 if (ac != rx->ac) {
1697
1698 info("audio: Set audio decoder: %s %uHz %dch\n",
1699 ac->name, get_srate(ac), get_ch(ac));
1700
1701 rx->pt = pt_rx;
1702 rx->ac = ac;
1703 rx->dec = mem_deref(rx->dec);
1704 }
1705
1706 if (ac->decupdh) {
1707 err = ac->decupdh(&rx->dec, ac, params);
1708 if (err) {
1709 warning("audio: alloc decoder: %m\n", err);
1710 return err;
1711 }
1712 }
1713
1714 stream_set_srate(a->strm, ac->crate, ac->crate);
1715
1716 if (reset) {
1717
1718 rx->auplay = mem_deref(rx->auplay);
1719
1720 /* Reset audio filter chain */
1721 list_flush(&rx->filtl);
1722
1723 err |= audio_start(a);
1724 }
1725
1726 return err;
1727 }
1728
1729
1730 /**
1731 * Use the next audio encoder in the local list of negotiated codecs
1732 *
1733 * @param audio Audio object
1734 */
audio_encoder_cycle(struct audio * audio)1735 void audio_encoder_cycle(struct audio *audio)
1736 {
1737 const struct sdp_format *rc = NULL;
1738
1739 if (!audio)
1740 return;
1741
1742 rc = sdp_media_format_cycle(stream_sdpmedia(audio_strm(audio)));
1743 if (!rc) {
1744 info("audio: encoder cycle: no remote codec found\n");
1745 return;
1746 }
1747
1748 (void)audio_encoder_set(audio, rc->data, rc->pt, rc->params);
1749 }
1750
1751
audio_strm(const struct audio * a)1752 struct stream *audio_strm(const struct audio *a)
1753 {
1754 return a ? a->strm : NULL;
1755 }
1756
1757
audio_send_digit(struct audio * a,char key)1758 int audio_send_digit(struct audio *a, char key)
1759 {
1760 int err = 0;
1761
1762 if (!a)
1763 return EINVAL;
1764
1765 if (key != KEYCODE_REL) {
1766 int event = telev_digit2code(key);
1767 info("audio: send DTMF digit: '%c'\n", key);
1768
1769 if (event == -1) {
1770 warning("audio: invalid DTMF digit (0x%02x)\n", key);
1771 return EINVAL;
1772 }
1773
1774 err = telev_send(a->telev, event, false);
1775 }
1776 else if (a->tx.cur_key && a->tx.cur_key != KEYCODE_REL) {
1777 /* Key release */
1778 info("audio: send DTMF digit end: '%c'\n", a->tx.cur_key);
1779 err = telev_send(a->telev,
1780 telev_digit2code(a->tx.cur_key), true);
1781 }
1782
1783 a->tx.cur_key = key;
1784
1785 return err;
1786 }
1787
1788
1789 /**
1790 * Mute the audio stream source (i.e. Microphone)
1791 *
1792 * @param a Audio stream
1793 * @param muted True to mute, false to un-mute
1794 */
audio_mute(struct audio * a,bool muted)1795 void audio_mute(struct audio *a, bool muted)
1796 {
1797 if (!a)
1798 return;
1799
1800 a->tx.muted = muted;
1801 }
1802
1803
1804 /**
1805 * Get the mute state of an audio source
1806 *
1807 * @param a Audio stream
1808 *
1809 * @return True if muted, otherwise false
1810 */
audio_ismuted(const struct audio * a)1811 bool audio_ismuted(const struct audio *a)
1812 {
1813 if (!a)
1814 return false;
1815
1816 return a->tx.muted;
1817 }
1818
1819
extmap_handler(const char * name,const char * value,void * arg)1820 static bool extmap_handler(const char *name, const char *value, void *arg)
1821 {
1822 struct audio *au = arg;
1823 struct sdp_extmap extmap;
1824 int err;
1825 (void)name;
1826
1827 err = sdp_extmap_decode(&extmap, value);
1828 if (err) {
1829 warning("audio: sdp_extmap_decode error (%m)\n", err);
1830 return false;
1831 }
1832
1833 if (0 == pl_strcasecmp(&extmap.name, uri_aulevel)) {
1834
1835 if (extmap.id < RTPEXT_ID_MIN || extmap.id > RTPEXT_ID_MAX) {
1836 warning("audio: extmap id out of range (%u)\n",
1837 extmap.id);
1838 return false;
1839 }
1840
1841 au->extmap_aulevel = extmap.id;
1842
1843 err = sdp_media_set_lattr(stream_sdpmedia(au->strm), true,
1844 "extmap",
1845 "%u %s",
1846 au->extmap_aulevel,
1847 uri_aulevel);
1848 if (err)
1849 return false;
1850
1851 au->level_enabled = true;
1852 info("audio: client-to-mixer audio levels enabled\n");
1853 }
1854
1855 return false;
1856 }
1857
1858
audio_sdp_attr_decode(struct audio * a)1859 void audio_sdp_attr_decode(struct audio *a)
1860 {
1861 const char *attr;
1862
1863 if (!a)
1864 return;
1865
1866 /* This is probably only meaningful for audio data, but
1867 may be used with other media types if it makes sense. */
1868 attr = sdp_media_rattr(stream_sdpmedia(a->strm), "ptime");
1869 if (attr) {
1870 struct autx *tx = &a->tx;
1871 uint32_t ptime_tx = atoi(attr);
1872
1873 if (ptime_tx && ptime_tx != a->tx.ptime) {
1874
1875 info("audio: peer changed ptime_tx %ums -> %ums\n",
1876 a->tx.ptime, ptime_tx);
1877
1878 tx->ptime = ptime_tx;
1879
1880 if (tx->ac) {
1881 tx->psize = 2 * get_framesize(tx->ac,
1882 ptime_tx);
1883 }
1884 }
1885 }
1886
1887 /* Client-to-Mixer Audio Level Indication */
1888 if (a->cfg.level) {
1889 sdp_media_rattr_apply(stream_sdpmedia(a->strm),
1890 "extmap",
1891 extmap_handler, a);
1892 }
1893 }
1894
1895
1896 /**
1897 * Get the last value of the audio level from incoming RTP packets
1898 *
1899 * @param au Audio object
1900 * @param levelp Pointer to where to write audio level value
1901 *
1902 * @return 0 if success, otherwise errorcode
1903 */
audio_level_get(const struct audio * au,double * levelp)1904 int audio_level_get(const struct audio *au, double *levelp)
1905 {
1906 if (!au)
1907 return EINVAL;
1908
1909 if (!au->level_enabled)
1910 return ENOTSUP;
1911
1912 if (!au->rx.level_set)
1913 return ENOENT;
1914
1915 if (levelp)
1916 *levelp = au->rx.level_last;
1917
1918 return 0;
1919 }
1920
1921
aucodec_print(struct re_printf * pf,const struct aucodec * ac)1922 static int aucodec_print(struct re_printf *pf, const struct aucodec *ac)
1923 {
1924 if (!ac)
1925 return 0;
1926
1927 return re_hprintf(pf, "%s %uHz/%dch",
1928 ac->name, get_srate(ac), get_ch(ac));
1929 }
1930
1931
audio_debug(struct re_printf * pf,const struct audio * a)1932 int audio_debug(struct re_printf *pf, const struct audio *a)
1933 {
1934 const struct autx *tx;
1935 const struct aurx *rx;
1936 size_t sztx, szrx;
1937 int err;
1938
1939 if (!a)
1940 return 0;
1941
1942 tx = &a->tx;
1943 rx = &a->rx;
1944
1945 sztx = aufmt_sample_size(tx->src_fmt);
1946 szrx = aufmt_sample_size(rx->play_fmt);
1947
1948 err = re_hprintf(pf, "\n--- Audio stream ---\n");
1949
1950 err |= re_hprintf(pf, " tx: encode: %H ptime=%ums %s\n",
1951 aucodec_print, tx->ac,
1952 tx->ptime,
1953 aufmt_name(tx->enc_fmt));
1954 err |= re_hprintf(pf, " aubuf: %H"
1955 " (cur %.2fms, max %.2fms, or %llu, ur %llu)\n",
1956 aubuf_debug, tx->aubuf,
1957 calc_ptime(aubuf_cur_size(tx->aubuf)/sztx,
1958 tx->ausrc_prm.srate,
1959 tx->ausrc_prm.ch),
1960 calc_ptime(tx->aubuf_maxsz/sztx,
1961 tx->ausrc_prm.srate,
1962 tx->ausrc_prm.ch),
1963 tx->stats.aubuf_overrun,
1964 tx->stats.aubuf_underrun);
1965 err |= re_hprintf(pf, " source: %s\n",
1966 aufmt_name(tx->src_fmt));
1967 err |= re_hprintf(pf, " time = %.3f sec\n",
1968 autx_calc_seconds(tx));
1969
1970 err |= re_hprintf(pf,
1971 " rx: decode: %H %s\n"
1972 " ptime=%ums pt=%d\n",
1973 aucodec_print, rx->ac, aufmt_name(rx->dec_fmt),
1974 rx->ptime, rx->pt);
1975 err |= re_hprintf(pf, " aubuf: %H"
1976 " (cur %.2fms, max %.2fms, or %llu, ur %llu)\n",
1977 aubuf_debug, rx->aubuf,
1978 calc_ptime(aubuf_cur_size(rx->aubuf)/szrx,
1979 rx->auplay_prm.srate,
1980 rx->auplay_prm.ch),
1981 calc_ptime(rx->aubuf_maxsz/szrx,
1982 rx->auplay_prm.srate,
1983 rx->auplay_prm.ch),
1984 rx->stats.aubuf_overrun,
1985 rx->stats.aubuf_underrun
1986 );
1987 err |= re_hprintf(pf, " player: %s\n", aufmt_name(rx->play_fmt));
1988 err |= re_hprintf(pf, " n_discard:%llu\n",
1989 rx->n_discard);
1990 if (rx->level_set) {
1991 err |= re_hprintf(pf, " level %.3f dBov\n",
1992 rx->level_last);
1993 }
1994 if (rx->ts_recv.is_set) {
1995 err |= re_hprintf(pf, " time = %.3f sec\n",
1996 aurx_calc_seconds(rx));
1997 }
1998 else {
1999 err |= re_hprintf(pf, " time = (not started)\n");
2000 }
2001
2002 err |= re_hprintf(pf,
2003 " %H"
2004 " %H",
2005 autx_print_pipeline, tx,
2006 aurx_print_pipeline, rx);
2007
2008 err |= stream_debug(pf, a->strm);
2009
2010 return err;
2011 }
2012
2013
audio_set_devicename(struct audio * a,const char * src,const char * play)2014 void audio_set_devicename(struct audio *a, const char *src, const char *play)
2015 {
2016 if (!a)
2017 return;
2018
2019 str_ncpy(a->tx.device, src, sizeof(a->tx.device));
2020 str_ncpy(a->rx.device, play, sizeof(a->rx.device));
2021 }
2022
2023
audio_set_source(struct audio * au,const char * mod,const char * device)2024 int audio_set_source(struct audio *au, const char *mod, const char *device)
2025 {
2026 struct autx *tx;
2027 int err;
2028
2029 if (!au)
2030 return EINVAL;
2031
2032 tx = &au->tx;
2033
2034 /* stop the audio device first */
2035 tx->ausrc = mem_deref(tx->ausrc);
2036
2037 err = ausrc_alloc(&tx->ausrc, baresip_ausrcl(),
2038 NULL, mod, &tx->ausrc_prm, device,
2039 ausrc_read_handler, ausrc_error_handler, au);
2040 if (err) {
2041 warning("audio: set_source failed (%s.%s): %m\n",
2042 mod, device, err);
2043 return err;
2044 }
2045
2046 return 0;
2047 }
2048
2049
audio_set_player(struct audio * au,const char * mod,const char * device)2050 int audio_set_player(struct audio *au, const char *mod, const char *device)
2051 {
2052 struct aurx *rx;
2053 int err;
2054
2055 if (!au)
2056 return EINVAL;
2057
2058 rx = &au->rx;
2059
2060 /* stop the audio device first */
2061 rx->auplay = mem_deref(rx->auplay);
2062
2063 err = auplay_alloc(&rx->auplay, baresip_auplayl(),
2064 mod, &rx->auplay_prm, device,
2065 auplay_write_handler, rx);
2066 if (err) {
2067 warning("audio: set_player failed (%s.%s): %m\n",
2068 mod, device, err);
2069 return err;
2070 }
2071
2072 return 0;
2073 }
2074
2075
2076 /*
2077 * Reference:
2078 *
2079 * https://www.avm.de/de/Extern/files/x-rtp/xrtpv32.pdf
2080 */
audio_print_rtpstat(struct re_printf * pf,const struct audio * a)2081 int audio_print_rtpstat(struct re_printf *pf, const struct audio *a)
2082 {
2083 const struct stream *s;
2084 const struct rtcp_stats *rtcp;
2085 int srate_tx = 8000;
2086 int srate_rx = 8000;
2087 int err;
2088
2089 if (!a)
2090 return 1;
2091
2092 s = a->strm;
2093 rtcp = &s->rtcp_stats;
2094
2095 if (!rtcp->tx.sent)
2096 return 1;
2097
2098 if (a->tx.ac)
2099 srate_tx = get_srate(a->tx.ac);
2100 if (a->rx.ac)
2101 srate_rx = get_srate(a->rx.ac);
2102
2103 err = re_hprintf(pf,
2104 "EX=BareSip;" /* Reporter Identifier */
2105 "CS=%d;" /* Call Setup in milliseconds */
2106 "CD=%d;" /* Call Duration in seconds */
2107 "PR=%u;PS=%u;" /* Packets RX, TX */
2108 "PL=%d,%d;" /* Packets Lost RX, TX */
2109 "PD=%d,%d;" /* Packets Discarded, RX, TX */
2110 "JI=%.1f,%.1f;" /* Jitter RX, TX in timestamp units */
2111 "IP=%J,%J" /* Local, Remote IPs */
2112 ,
2113 call_setup_duration(s->call) * 1000,
2114 call_duration(s->call),
2115
2116 s->metric_rx.n_packets,
2117 s->metric_tx.n_packets,
2118
2119 rtcp->rx.lost, rtcp->tx.lost,
2120
2121 s->metric_rx.n_err, s->metric_tx.n_err,
2122
2123 /* timestamp units (ie: 8 ts units = 1 ms @ 8KHZ) */
2124 1.0 * rtcp->rx.jit/1000 * (srate_rx/1000),
2125 1.0 * rtcp->tx.jit/1000 * (srate_tx/1000),
2126
2127 sdp_media_laddr(s->sdp),
2128 sdp_media_raddr(s->sdp)
2129 );
2130
2131 if (a->tx.ac) {
2132 err |= re_hprintf(pf, ";EN=%s/%d", a->tx.ac->name, srate_tx );
2133 }
2134 if (a->rx.ac) {
2135 err |= re_hprintf(pf, ";DE=%s/%d", a->rx.ac->name, srate_rx );
2136 }
2137
2138 return err;
2139 }
2140
2141
audio_set_bitrate(struct audio * au,uint32_t bitrate)2142 int audio_set_bitrate(struct audio *au, uint32_t bitrate)
2143 {
2144 struct autx *tx;
2145 const struct aucodec *ac;
2146 int err;
2147
2148 if (!au)
2149 return EINVAL;
2150
2151 tx = &au->tx;
2152
2153 ac = tx->ac;
2154
2155 info("audio: set bitrate for encoder '%s' to %u bits/s\n",
2156 ac ? ac->name : "?",
2157 bitrate);
2158
2159 if (ac) {
2160
2161 if (ac->encupdh) {
2162 struct auenc_param prm;
2163
2164 prm.ptime = tx->ptime;
2165 prm.bitrate = bitrate;
2166
2167 err = ac->encupdh(&tx->enc, ac, &prm, NULL);
2168 if (err) {
2169 warning("audio: encupdh error: %m\n", err);
2170 return err;
2171 }
2172 }
2173
2174 }
2175 else {
2176 info("audio: set_bitrate: no audio encoder\n");
2177 }
2178
2179 return 0;
2180 }
2181