-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathcodec.c
More file actions
1974 lines (1674 loc) · 63.4 KB
/
codec.c
File metadata and controls
1974 lines (1674 loc) · 63.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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
#include "codec.h"
#include <glib.h>
#include <assert.h>
#include <inttypes.h>
#include <sys/types.h>
#include <spandsp/telephony.h>
#include <spandsp/super_tone_rx.h>
#include <spandsp/logging.h>
#include <spandsp/dtmf.h>
#include "call.h"
#include "log.h"
#include "rtplib.h"
#include "codeclib.h"
#include "ssrc.h"
#include "rtcp.h"
#include "call_interfaces.h"
#include "dtmf.h"
#include "dtmflib.h"
static codec_handler_func handler_func_passthrough;
static struct rtp_payload_type *__rtp_payload_type_copy(const struct rtp_payload_type *pt);
static void __rtp_payload_type_dup(struct call *call, struct rtp_payload_type *pt);
static void __rtp_payload_type_add_name(GHashTable *, struct rtp_payload_type *pt);
static struct codec_handler codec_handler_stub = {
.source_pt.payload_type = -1,
.func = handler_func_passthrough,
.kernelize = 1,
};
static GList *__delete_x_codec(GList *link, GHashTable *codecs, GHashTable *codec_names, GQueue *codecs_prefs) {
struct rtp_payload_type *pt = link->data;
g_hash_table_remove(codecs, &pt->payload_type);
g_hash_table_remove(codec_names, &pt->encoding);
g_hash_table_remove(codec_names, &pt->encoding_with_params);
GList *next = link->next;
g_queue_delete_link(codecs_prefs, link);
payload_type_free(pt);
return next;
}
static GList *__delete_receiver_codec(struct call_media *receiver, GList *link) {
return __delete_x_codec(link, receiver->codecs_recv, receiver->codec_names_recv,
&receiver->codecs_prefs_recv);
}
#ifdef WITH_TRANSCODING
#include "resample.h"
#include "dtmf_rx_fillin.h"
struct codec_ssrc_handler {
struct ssrc_entry h; // must be first
struct codec_handler *handler;
decoder_t *decoder;
encoder_t *encoder;
format_t encoder_format;
int bitrate;
int ptime;
int bytes_per_packet;
unsigned long first_ts; // for output TS scaling
unsigned long ts_in; // for DTMF dupe detection
struct timeval first_send;
unsigned long first_send_ts;
GString *sample_buffer;
// DTMF DSP stuff
dtmf_rx_state_t *dtmf_dsp;
resample_t dtmf_resampler;
format_t dtmf_format;
uint64_t dtmf_ts, last_dtmf_event_ts;
GQueue dtmf_events;
struct dtmf_event dtmf_event;
uint64_t skip_pts;
int rtp_mark:1;
};
struct transcode_packet {
seq_packet_t p; // must be first
unsigned long ts;
str *payload;
struct codec_handler *handler;
int marker:1,
ignore_seq:1;
int (*func)(struct codec_ssrc_handler *, struct transcode_packet *, struct media_packet *);
int (*dup_func)(struct codec_ssrc_handler *, struct transcode_packet *, struct media_packet *);
struct rtp_header rtp;
};
static codec_handler_func handler_func_passthrough_ssrc;
static codec_handler_func handler_func_transcode;
static codec_handler_func handler_func_playback;
static codec_handler_func handler_func_inject_dtmf;
static codec_handler_func handler_func_dtmf;
static struct ssrc_entry *__ssrc_handler_transcode_new(void *p);
static struct ssrc_entry *__ssrc_handler_new(void *p);
static void __free_ssrc_handler(void *);
static void __transcode_packet_free(struct transcode_packet *);
static struct codec_handler codec_handler_stub_ssrc = {
.source_pt.payload_type = -1,
.func = handler_func_passthrough_ssrc,
.kernelize = 1,
};
static void __handler_shutdown(struct codec_handler *handler) {
free_ssrc_hash(&handler->ssrc_hash);
if (handler->ssrc_handler)
obj_put(&handler->ssrc_handler->h);
handler->ssrc_handler = NULL;
handler->kernelize = 0;
handler->transcoder = 0;
handler->dtmf_scaler = 0;
handler->output_handler = handler; // reset to default
handler->dtmf_payload_type = -1;
handler->pcm_dtmf_detect = 0;
}
static void __codec_handler_free(void *pp) {
struct codec_handler *h = pp;
__handler_shutdown(h);
g_slice_free1(sizeof(*h), h);
}
void codec_handler_free(struct codec_handler *handler) {
__codec_handler_free(handler);
}
static struct codec_handler *__handler_new(struct rtp_payload_type *pt) {
struct codec_handler *handler = g_slice_alloc0(sizeof(*handler));
handler->source_pt = *pt;
handler->output_handler = handler; // default
handler->dtmf_payload_type = -1;
return handler;
}
static void __make_passthrough(struct codec_handler *handler) {
__handler_shutdown(handler);
ilog(LOG_DEBUG, "Using passthrough handler for " STR_FORMAT,
STR_FMT(&handler->source_pt.encoding_with_params));
if (handler->source_pt.codec_def && handler->source_pt.codec_def->dtmf)
handler->func = handler_func_dtmf;
else {
handler->func = handler_func_passthrough;
handler->kernelize = 1;
}
handler->dest_pt = handler->source_pt;
handler->ssrc_hash = create_ssrc_hash_full(__ssrc_handler_new, handler);
}
static void __make_passthrough_ssrc(struct codec_handler *handler) {
__handler_shutdown(handler);
ilog(LOG_DEBUG, "Using passthrough handler with new SSRC for " STR_FORMAT,
STR_FMT(&handler->source_pt.encoding_with_params));
if (handler->source_pt.codec_def && handler->source_pt.codec_def->dtmf)
handler->func = handler_func_dtmf;
else {
handler->func = handler_func_passthrough_ssrc;
handler->kernelize = 1;
}
handler->dest_pt = handler->source_pt;
handler->ssrc_hash = create_ssrc_hash_full(__ssrc_handler_new, handler);
}
static void __make_transcoder(struct codec_handler *handler, struct rtp_payload_type *dest,
GHashTable *output_transcoders, int dtmf_payload_type, int pcm_dtmf_detect)
{
assert(handler->source_pt.codec_def != NULL);
assert(dest->codec_def != NULL);
// if we're just repacketising:
if (dtmf_payload_type == -1 && dest->codec_def && dest->codec_def->dtmf)
dtmf_payload_type = dest->payload_type;
// don't reset handler if it already matches what we want
if (!handler->transcoder)
goto reset;
if (rtp_payload_type_cmp(dest, &handler->dest_pt))
goto reset;
if (handler->func != handler_func_transcode)
goto reset;
ilog(LOG_DEBUG, "Leaving transcode context for " STR_FORMAT " -> " STR_FORMAT " intact",
STR_FMT(&handler->source_pt.encoding_with_params),
STR_FMT(&dest->encoding_with_params));
goto check_output;
reset:
__handler_shutdown(handler);
handler->dest_pt = *dest;
handler->func = handler_func_transcode;
handler->transcoder = 1;
if (dtmf_payload_type != -1)
handler->dtmf_payload_type = dtmf_payload_type;
handler->pcm_dtmf_detect = pcm_dtmf_detect ? 1 : 0;
// is this DTMF to DTMF?
if (dtmf_payload_type != -1 && handler->source_pt.codec_def->dtmf) {
ilog(LOG_DEBUG, "Created DTMF transcode context for " STR_FORMAT " -> PT %i",
STR_FMT(&handler->source_pt.encoding_with_params),
dtmf_payload_type);
handler->dtmf_scaler = 1;
}
else
ilog(LOG_DEBUG, "Created transcode context for " STR_FORMAT " -> " STR_FORMAT
" with DTMF output %i",
STR_FMT(&handler->source_pt.encoding_with_params),
STR_FMT(&dest->encoding_with_params), dtmf_payload_type);
handler->ssrc_hash = create_ssrc_hash_full(__ssrc_handler_transcode_new, handler);
check_output:;
// check if we have multiple decoders transcoding to the same output PT
struct codec_handler *output_handler = g_hash_table_lookup(output_transcoders,
GINT_TO_POINTER(dest->payload_type));
if (output_handler) {
ilog(LOG_DEBUG, "Using existing encoder context");
handler->output_handler = output_handler;
}
else {
g_hash_table_insert(output_transcoders, GINT_TO_POINTER(dest->payload_type), handler);
handler->output_handler = handler; // make sure we don't have a stale pointer
}
}
struct codec_handler *codec_handler_make_playback(struct rtp_payload_type *src_pt,
struct rtp_payload_type *dst_pt, unsigned long last_ts)
{
struct codec_handler *handler = __handler_new(src_pt);
handler->dest_pt = *dst_pt;
handler->func = handler_func_playback;
handler->ssrc_handler = (void *) __ssrc_handler_transcode_new(handler);
handler->ssrc_handler->first_ts = last_ts;
while (handler->ssrc_handler->first_ts == 0)
handler->ssrc_handler->first_ts = random();
handler->ssrc_handler->rtp_mark = 1;
ilog(LOG_DEBUG, "Created media playback context for " STR_FORMAT " -> " STR_FORMAT "",
STR_FMT(&src_pt->encoding_with_params),
STR_FMT(&dst_pt->encoding_with_params));
return handler;
}
static void __ensure_codec_def(struct rtp_payload_type *pt, struct call_media *media) {
if (pt->codec_def)
return;
pt->codec_def = codec_find(&pt->encoding, media->type_id);
if (!pt->codec_def)
return;
if (!pt->codec_def->support_encoding || !pt->codec_def->support_decoding)
pt->codec_def = NULL;
}
static GList *__delete_send_codec(struct call_media *sender, GList *link) {
return __delete_x_codec(link, sender->codecs_send, sender->codec_names_send,
&sender->codecs_prefs_send);
}
// only called from codec_handlers_update()
static void __make_passthrough_gsl(struct codec_handler *handler, GSList **handlers) {
__make_passthrough(handler);
*handlers = g_slist_prepend(*handlers, handler);
}
// only called from codec_handlers_update()
static void __dtmf_dsp_shutdown(struct call_media *sink, int payload_type) {
if (!sink->codec_handlers)
return;
for (GList *l = sink->codec_handlers_store.head; l; l = l->next) {
struct codec_handler *handler = l->data;
if (!handler->transcoder)
continue;
if (handler->dtmf_payload_type != payload_type)
continue;
if (handler->dtmf_scaler)
continue;
ilog(LOG_DEBUG, "Shutting down DTMF DSP for '" STR_FORMAT "' -> %i (not needed)",
STR_FMT(&handler->source_pt.encoding_with_params),
payload_type);
handler->dtmf_payload_type = -1;
}
}
static struct rtp_payload_type *__check_dest_codecs(struct call_media *receiver, struct call_media *sink,
const struct sdp_ng_flags *flags, GHashTable *dtmf_sinks, int *sink_transcoding)
{
struct rtp_payload_type *pref_dest_codec = NULL;
for (GList *l = sink->codecs_prefs_send.head; l; l = l->next) {
struct rtp_payload_type *pt = l->data;
__ensure_codec_def(pt, sink);
if (!pt->codec_def) // not supported, next
continue;
// fix up ptime
if (!pt->ptime)
pt->ptime = pt->codec_def->default_ptime;
if (sink->ptime)
pt->ptime = sink->ptime;
if (!pref_dest_codec && !pt->codec_def->supplemental) {
ilog(LOG_DEBUG, "Default sink codec is " STR_FORMAT, STR_FMT(&pt->encoding_with_params));
pref_dest_codec = pt;
}
// also check if this is a transcoding codec: if we can send a codec to the sink,
// but can't receive it on the receiver side, then it's transcoding. this is to check
// whether transcoding on the sink side is actually needed. if transcoding has been
// previously enabled on the sink, but no transcoding codecs are actually present,
// we can disable the transcoding engine.
if (MEDIA_ISSET(sink, TRANSCODE)) {
struct rtp_payload_type *recv_pt = g_hash_table_lookup(receiver->codecs_send,
&pt->payload_type);
if (!recv_pt || rtp_payload_type_cmp(pt, recv_pt)) {
*sink_transcoding = 1;
// can the sink receive RFC DTMF but the receiver can't send it?
if (pt->codec_def && pt->codec_def->dtmf) {
if (!g_hash_table_lookup(dtmf_sinks, GUINT_TO_POINTER(pt->clock_rate)))
g_hash_table_insert(dtmf_sinks, GUINT_TO_POINTER(pt->clock_rate),
pt);
}
}
}
else if (flags && (flags->always_transcode || flags->inject_dtmf)) {
// with always-transcode, we must keep track of potential output DTMF payload
// types as well
if (pt->codec_def && pt->codec_def->dtmf) {
if (!g_hash_table_lookup(dtmf_sinks, GUINT_TO_POINTER(pt->clock_rate)))
g_hash_table_insert(dtmf_sinks, GUINT_TO_POINTER(pt->clock_rate),
pt);
}
}
}
return pref_dest_codec;
}
static void __check_send_codecs(struct call_media *receiver, struct call_media *sink,
const struct sdp_ng_flags *flags, GHashTable *dtmf_sinks, int *sink_transcoding)
{
if (!MEDIA_ISSET(sink, TRANSCODE))
return;
for (GList *l = sink->codecs_prefs_recv.head; l; l = l->next) {
struct rtp_payload_type *pt = l->data;
struct rtp_payload_type *recv_pt = g_hash_table_lookup(receiver->codecs_send,
&pt->payload_type);
if (!recv_pt || rtp_payload_type_cmp(pt, recv_pt) || (flags && flags->inject_dtmf)) {
*sink_transcoding = 1;
// can the sink receive RFC DTMF but the receiver can't send it?
if (pt->codec_def && pt->codec_def->dtmf) {
if (!g_hash_table_lookup(dtmf_sinks, GUINT_TO_POINTER(pt->clock_rate)))
g_hash_table_insert(dtmf_sinks, GUINT_TO_POINTER(pt->clock_rate),
pt);
}
continue;
}
// even if the receiver can receive the same codec that the sink can
// send, we might still have it configured as a transcoder due to
// always-transcode in the offer
// XXX codec_handlers can be converted to g_direct_hash table
struct codec_handler *ch_recv =
g_hash_table_lookup(sink->codec_handlers, &recv_pt->payload_type);
if (!ch_recv)
continue;
if (ch_recv->transcoder) {
*sink_transcoding = 1;
break;
}
}
}
static int __dtmf_payload_type(GHashTable *dtmf_sinks, struct rtp_payload_type *pref_dest_codec) {
if (!g_hash_table_size(dtmf_sinks) || !pref_dest_codec)
return -1;
int dtmf_payload_type = -1;
// find the telephone-event codec entry with a matching clock rate
struct rtp_payload_type *pt = g_hash_table_lookup(dtmf_sinks,
GUINT_TO_POINTER(pref_dest_codec->clock_rate));
if (!pt)
ilog(LOG_INFO, "Not transcoding PCM DTMF tones to telephone-event packets as "
"no payload type with a matching clock rate for '" STR_FORMAT
"' was found", STR_FMT(&pref_dest_codec->encoding_with_params));
else {
dtmf_payload_type = pt->payload_type;
ilog(LOG_DEBUG, "Output DTMF payload type is %i", dtmf_payload_type);
}
return dtmf_payload_type;
}
static void __accept_transcode_codecs(struct call_media *receiver, struct call_media *sink) {
// if the other side is transcoding, we need to accept codecs that were
// originally offered (recv->send) if we support them, even if the
// response (sink->send) doesn't include them
GList *insert_pos = NULL;
for (GList *l = receiver->codecs_prefs_send.head; l; l = l->next) {
struct rtp_payload_type *pt = l->data;
__ensure_codec_def(pt, receiver);
if (!pt->codec_def)
continue;
if (g_hash_table_lookup(receiver->codecs_recv, &pt->payload_type)) {
// already present.
// to keep the order intact, we seek the list for the position
// of this codec entry. all newly added codecs must come after
// this entry.
if (!insert_pos)
insert_pos = receiver->codecs_prefs_recv.head;
while (insert_pos) {
if (!insert_pos->next)
break; // end of list - we insert everything after
struct rtp_payload_type *test_pt = insert_pos->data;
if (test_pt->payload_type == pt->payload_type)
break;
insert_pos = insert_pos->next;
}
continue;
}
ilog(LOG_DEBUG, "Accepting offered codec " STR_FORMAT " due to transcoding",
STR_FMT(&pt->encoding_with_params));
MEDIA_SET(receiver, TRANSCODE);
// we need a new pt entry
pt = __rtp_payload_type_copy(pt);
// this somewhat duplicates __rtp_payload_type_add_recv
g_hash_table_insert(receiver->codecs_recv, &pt->payload_type, pt);
__rtp_payload_type_add_name(receiver->codec_names_recv, pt);
if (!insert_pos) {
g_queue_push_head(&receiver->codecs_prefs_recv, pt);
insert_pos = receiver->codecs_prefs_recv.head;
}
else {
g_queue_insert_after(&receiver->codecs_prefs_recv, insert_pos, pt);
insert_pos = insert_pos->next;
}
}
}
static void __eliminate_rejected_codecs(struct call_media *receiver, struct call_media *sink,
const struct sdp_ng_flags *flags)
{
if (flags && flags->asymmetric_codecs)
return;
// in the other case (not transcoding), we can eliminate rejected codecs from our
// `send` list if the receiver cannot receive it.
for (GList *l = receiver->codecs_prefs_send.head; l;) {
struct rtp_payload_type *pt = l->data;
if (g_hash_table_lookup(receiver->codec_names_recv, &pt->encoding)) {
l = l->next;
continue;
}
ilog(LOG_DEBUG, "Eliminating asymmetric outbound codec " STR_FORMAT,
STR_FMT(&pt->encoding_with_params));
l = __delete_send_codec(receiver, l);
}
}
static void __check_dtmf_injector(const struct sdp_ng_flags *flags, struct call_media *receiver,
struct rtp_payload_type *pref_dest_codec, GHashTable *output_transcoders,
int dtmf_payload_type)
{
if (!flags || !flags->inject_dtmf)
return;
if (receiver->dtmf_injector) {
// is this still valid?
if (!rtp_payload_type_cmp(pref_dest_codec, &receiver->dtmf_injector->dest_pt))
return;
receiver->dtmf_injector = NULL;
}
// synthesise input rtp payload type
struct rtp_payload_type src_pt = {
.payload_type = -1,
.clock_rate = pref_dest_codec->clock_rate,
.channels = pref_dest_codec->channels,
};
str_init(&src_pt.encoding, "DTMF injector");
str_init(&src_pt.encoding_with_params, "DTMF injector");
const str tp_event = STR_CONST_INIT("telephone-event");
src_pt.codec_def = codec_find(&tp_event, MT_AUDIO);
if (!src_pt.codec_def) {
ilog(LOG_ERR, "RTP payload type 'telephone-event' is not defined");
return;
}
//receiver->dtmf_injector = codec_handler_make_playback(&src_pt, pref_dest_codec, 0);
//receiver->dtmf_injector->dtmf_payload_type = dtmf_payload_type;
receiver->dtmf_injector = __handler_new(&src_pt);
__make_transcoder(receiver->dtmf_injector, pref_dest_codec, output_transcoders, dtmf_payload_type, 0);
receiver->dtmf_injector->func = handler_func_inject_dtmf;
g_queue_push_tail(&receiver->codec_handlers_store, receiver->dtmf_injector);
}
// call must be locked in W
void codec_handlers_update(struct call_media *receiver, struct call_media *sink,
const struct sdp_ng_flags *flags)
{
if (!receiver->codec_handlers)
receiver->codec_handlers = g_hash_table_new(g_int_hash, g_int_equal);
MEDIA_CLEAR(receiver, TRANSCODE);
receiver->rtcp_handler = NULL;
GSList *passthrough_handlers = NULL;
// we go through the list of codecs that the receiver supports and compare it
// with the list of codecs supported by the sink. if the receiver supports
// a codec that the sink doesn't support, we must transcode.
//
// if we transcode, we transcode to the highest-preference supported codec
// that the sink specified. determine this first.
struct rtp_payload_type *pref_dest_codec = NULL;
int sink_transcoding = 0;
// keep track of telephone-event payload types. we hash them by clock rate
// in case there's several of them. the clock rates of the destination
// codec and the telephone-event codec must match.
GHashTable *dtmf_sinks = g_hash_table_new(g_direct_hash, g_direct_equal);
pref_dest_codec = __check_dest_codecs(receiver, sink, flags, dtmf_sinks, &sink_transcoding);
// similarly, if the sink can receive a codec that the receiver can't send, it's also transcoding
__check_send_codecs(receiver, sink, flags, dtmf_sinks, &sink_transcoding);
ilog(LOG_DEBUG, "%i DTMF sink entries", g_hash_table_size(dtmf_sinks));
int dtmf_payload_type = __dtmf_payload_type(dtmf_sinks, pref_dest_codec);
g_hash_table_destroy(dtmf_sinks);
// stop transcoding if we've determined that we don't need it
if (MEDIA_ISSET(sink, TRANSCODE) && !sink_transcoding) {
ilog(LOG_DEBUG, "Disabling transcoding engine (not needed)");
MEDIA_CLEAR(sink, TRANSCODE);
}
if (MEDIA_ISSET(sink, TRANSCODE))
__accept_transcode_codecs(receiver, sink);
else
__eliminate_rejected_codecs(receiver, sink, flags);
// if multiple input codecs transcode to the same output codec, we want to make sure
// that all the decoders output their media to the same encoder. we use the destination
// payload type to keep track of this.
GHashTable *output_transcoders = g_hash_table_new(g_direct_hash, g_direct_equal);
int transcode_dtmf = 0; // is one of our destination codecs DTMF?
// do we need to detect PCM DTMF tones?
int pcm_dtmf_detect = 0;
if ((MEDIA_ISSET(sink, TRANSCODE) || (flags && flags->always_transcode))
&& dtmf_payload_type != -1
&& !g_hash_table_lookup(receiver->codecs_send, &dtmf_payload_type))
pcm_dtmf_detect = 1;
for (GList *l = receiver->codecs_prefs_recv.head; l; ) {
struct rtp_payload_type *pt = l->data;
if (MEDIA_ISSET(sink, TRANSCODE)) {
// if the other side is transcoding, we may come across a receiver entry
// (recv->recv) that wasn't originally offered (recv->send). we must eliminate
// those
if (!g_hash_table_lookup(receiver->codecs_send, &pt->payload_type)) {
ilog(LOG_DEBUG, "Eliminating transcoded codec " STR_FORMAT,
STR_FMT(&pt->encoding_with_params));
l = __delete_receiver_codec(receiver, l);
continue;
}
}
// first, make sure we have a codec_handler struct for this
__ensure_codec_def(pt, receiver);
struct codec_handler *handler;
handler = g_hash_table_lookup(receiver->codec_handlers, &pt->payload_type);
if (handler) {
// make sure existing handler matches this PT
if (rtp_payload_type_cmp(pt, &handler->source_pt)) {
ilog(LOG_DEBUG, "Resetting codec handler for PT %u", pt->payload_type);
handler = NULL;
g_atomic_pointer_set(&receiver->codec_handler_cache, NULL);
g_hash_table_remove(receiver->codec_handlers, &pt->payload_type);
}
}
if (!handler) {
ilog(LOG_DEBUG, "Creating codec handler for " STR_FORMAT,
STR_FMT(&pt->encoding_with_params));
handler = __handler_new(pt);
g_hash_table_insert(receiver->codec_handlers, &handler->source_pt.payload_type,
handler);
g_queue_push_tail(&receiver->codec_handlers_store, handler);
}
// check our own support for this codec
if (!pt->codec_def) {
// not supported
__make_passthrough_gsl(handler, &passthrough_handlers);
goto next;
}
// figure out our ptime
if (!pt->ptime)
pt->ptime = pt->codec_def->default_ptime;
if (receiver->ptime)
pt->ptime = receiver->ptime;
// if the sink's codec preferences are unknown (empty), or there are
// no supported codecs to transcode to, then we have nothing
// to do. most likely this is an initial offer without a received answer.
// we default to forwarding without transcoding.
if (!pref_dest_codec) {
ilog(LOG_DEBUG, "No known/supported sink codec for " STR_FORMAT,
STR_FMT(&pt->encoding_with_params));
__make_passthrough_gsl(handler, &passthrough_handlers);
goto next;
}
struct rtp_payload_type *dest_pt; // transcode to this
GQueue *dest_codecs = NULL;
if (!flags || !flags->always_transcode) {
// we ignore output codec matches if we must transcode DTMF
if (dtmf_payload_type != -1)
;
else if (flags && flags->inject_dtmf)
;
else
dest_codecs = g_hash_table_lookup(sink->codec_names_send, &pt->encoding);
}
else if (flags->always_transcode) {
// with always-transcode, we still accept DTMF payloads if possible
if (pt->codec_def && pt->codec_def->supplemental)
dest_codecs = g_hash_table_lookup(sink->codec_names_send, &pt->encoding);
}
if (dest_codecs) {
// the sink supports this codec - check offered formats
dest_pt = NULL;
for (GList *k = dest_codecs->head; k; k = k->next) {
unsigned int dest_ptype = GPOINTER_TO_UINT(k->data);
dest_pt = g_hash_table_lookup(sink->codecs_send, &dest_ptype);
if (!dest_pt)
continue;
// XXX match up format parameters
break;
}
if (!dest_pt)
goto unsupported;
// in case of ptime mismatch, we transcode, but between the same codecs
if (dest_pt->ptime && pt->ptime
&& dest_pt->ptime != pt->ptime)
{
ilog(LOG_DEBUG, "Mismatched ptime between source and sink (%i <> %i), "
"enabling transcoding",
dest_pt->ptime, pt->ptime);
goto transcode;
}
// XXX check format parameters as well
ilog(LOG_DEBUG, "Sink supports codec " STR_FORMAT, STR_FMT(&pt->encoding_with_params));
__make_passthrough_gsl(handler, &passthrough_handlers);
if (pt->codec_def && pt->codec_def->dtmf)
__dtmf_dsp_shutdown(sink, pt->payload_type);
goto next;
}
unsupported:
// the sink does not support this codec -> transcode
ilog(LOG_DEBUG, "Sink does not support codec " STR_FORMAT, STR_FMT(&pt->encoding_with_params));
dest_pt = pref_dest_codec;
if (pt->codec_def->dtmf)
transcode_dtmf = 1;
transcode:;
// look up the reverse side of this payload type, which is the decoder to our
// encoder. if any codec options such as bitrate were set during an offer,
// they're in the decoder // PT. copy them to the encoder PT.
struct rtp_payload_type *reverse_pt = g_hash_table_lookup(sink->codecs_recv,
&dest_pt->payload_type);
if (reverse_pt) {
if (!dest_pt->bitrate)
dest_pt->bitrate = reverse_pt->bitrate;
}
MEDIA_SET(receiver, TRANSCODE);
__make_transcoder(handler, dest_pt, output_transcoders, dtmf_payload_type, pcm_dtmf_detect);
next:
l = l->next;
}
// if we've determined that we transcode, we must remove all unsupported codecs from
// the list, as we must expect to potentially receive media in that codec, which we
// then could not transcode.
if (MEDIA_ISSET(receiver, TRANSCODE)) {
ilog(LOG_INFO, "Enabling transcoding engine");
for (GList *l = receiver->codecs_prefs_recv.head; l; ) {
struct rtp_payload_type *pt = l->data;
if (pt->codec_def) {
// supported
l = l->next;
continue;
}
ilog(LOG_DEBUG, "Stripping unsupported codec " STR_FORMAT " due to active transcoding",
STR_FMT(&pt->encoding));
l = __delete_receiver_codec(receiver, l);
}
// we have to translate RTCP packets
receiver->rtcp_handler = rtcp_transcode_handler;
__check_dtmf_injector(flags, receiver, pref_dest_codec, output_transcoders, dtmf_payload_type);
// at least some payload types will be transcoded, which will result in SSRC
// change. for payload types which we don't actually transcode, we still
// must substitute the SSRC
while (passthrough_handlers) {
struct codec_handler *handler = passthrough_handlers->data;
// if the sink does not support DTMF but we can receive it, we must transcode
// DTMF event packets to PCM. this requires all codecs to be transcoded to the
// sink's preferred destination codec.
if ((!transcode_dtmf && dtmf_payload_type == -1) || !pref_dest_codec
|| !handler->source_pt.codec_def || !pref_dest_codec->codec_def)
__make_passthrough_ssrc(handler);
else
__make_transcoder(handler, pref_dest_codec, output_transcoders,
dtmf_payload_type, pcm_dtmf_detect);
passthrough_handlers = g_slist_delete_link(passthrough_handlers, passthrough_handlers);
}
}
while (passthrough_handlers) {
passthrough_handlers = g_slist_delete_link(passthrough_handlers, passthrough_handlers);
}
g_hash_table_destroy(output_transcoders);
}
#endif
// call must be locked in R
struct codec_handler *codec_handler_get(struct call_media *m, int payload_type) {
#ifdef WITH_TRANSCODING
struct codec_handler *h;
if (payload_type < 0)
goto out;
h = g_atomic_pointer_get(&m->codec_handler_cache);
if (G_LIKELY(G_LIKELY(h) && G_LIKELY(h->source_pt.payload_type == payload_type)))
return h;
if (G_UNLIKELY(!m->codec_handlers))
goto out;
h = g_hash_table_lookup(m->codec_handlers, &payload_type);
if (!h)
goto out;
g_atomic_pointer_set(&m->codec_handler_cache, h);
return h;
out:
if (MEDIA_ISSET(m, TRANSCODE))
return &codec_handler_stub_ssrc;
#endif
return &codec_handler_stub;
}
void codec_handlers_free(struct call_media *m) {
if (m->codec_handlers)
g_hash_table_destroy(m->codec_handlers);
m->codec_handlers = NULL;
m->codec_handler_cache = NULL;
#ifdef WITH_TRANSCODING
g_queue_clear_full(&m->codec_handlers_store, __codec_handler_free);
m->dtmf_injector = NULL;
#endif
}
void codec_add_raw_packet(struct media_packet *mp) {
struct codec_packet *p = g_slice_alloc0(sizeof(*p));
p->s = mp->raw;
p->free_func = NULL;
if (mp->rtp && mp->ssrc_out)
payload_tracker_add(&mp->ssrc_out->tracker, mp->rtp->m_pt & 0x7f);
g_queue_push_tail(&mp->packets_out, p);
}
static int handler_func_passthrough(struct codec_handler *h, struct media_packet *mp) {
if (mp->call->block_media || mp->media->monologue->block_media)
return 0;
codec_add_raw_packet(mp);
return 0;
}
#ifdef WITH_TRANSCODING
static int __handler_func_sequencer(struct media_packet *mp, struct transcode_packet *packet)
{
struct codec_handler *h = packet->handler;
if (G_UNLIKELY(!h->ssrc_hash)) {
if (!packet->func || !packet->handler || !packet->handler->ssrc_hash) {
h->func(h, mp);
return 0;
}
}
struct ssrc_ctx *ssrc_in = mp->ssrc_in;
struct ssrc_entry_call *ssrc_in_p = ssrc_in->parent;
struct ssrc_ctx *ssrc_out = mp->ssrc_out;
struct ssrc_entry_call *ssrc_out_p = ssrc_out->parent;
struct codec_ssrc_handler *ch = get_ssrc(ssrc_in_p->h.ssrc, h->ssrc_hash);
if (G_UNLIKELY(!ch))
return 0;
atomic64_inc(&ssrc_in->packets);
atomic64_add(&ssrc_in->octets, mp->payload.len);
packet->p.seq = ntohs(mp->rtp->seq_num);
packet->payload = str_dup(&mp->payload);
packet->ts = ntohl(mp->rtp->timestamp);
packet->marker = (mp->rtp->m_pt & 0x80) ? 1 : 0;
// how should we retrieve packets from the sequencer?
void *(*seq_next_packet)(packet_sequencer_t *) = packet_sequencer_next_packet;
if (packet->ignore_seq)
seq_next_packet = packet_sequencer_force_next_packet;
// we need a nested lock here - both input and output SSRC needs to be locked.
// we don't know the lock order, so try both, and keep trying until we succeed.
while (1) {
mutex_lock(&ssrc_in_p->h.lock);
if (ssrc_in_p == ssrc_out_p)
break;
if (!mutex_trylock(&ssrc_out_p->h.lock))
break;
mutex_unlock(&ssrc_in_p->h.lock);
mutex_lock(&ssrc_out_p->h.lock);
if (!mutex_trylock(&ssrc_in_p->h.lock))
break;
mutex_unlock(&ssrc_out_p->h.lock);
}
packet_sequencer_init(&ssrc_in_p->sequencer, (GDestroyNotify) __transcode_packet_free);
u_int16_t seq_ori = ssrc_in_p->sequencer.seq;
int seq_ret = packet_sequencer_insert(&ssrc_in_p->sequencer, &packet->p);
if (seq_ret < 0) {
// dupe
if (packet->dup_func)
packet->dup_func(ch, packet, mp);
else
ilog(LOG_DEBUG, "Ignoring duplicate RTP packet");
__transcode_packet_free(packet);
atomic64_inc(&ssrc_in->duplicates);
goto out;
}
// got a new packet, run decoder
while (1) {
packet = seq_next_packet(&ssrc_in_p->sequencer);
if (G_UNLIKELY(!packet))
break;
h = packet->handler;
obj_put(&ch->h);
ch = get_ssrc(ssrc_in_p->h.ssrc, h->ssrc_hash);
if (G_UNLIKELY(!ch))
goto next;
atomic64_set(&ssrc_in->packets_lost, ssrc_in_p->sequencer.lost_count);
atomic64_set(&ssrc_in->last_seq, ssrc_in_p->sequencer.ext_seq);
ilog(LOG_DEBUG, "Decoding RTP packet: seq %u, TS %lu",
packet->p.seq, packet->ts);
if (seq_ret == 1) {
// seq reset - update output seq. we keep our output seq clean
ssrc_out_p->seq_diff -= packet->p.seq - seq_ori;
seq_ret = 0;
}
// we might be working with a different packet now
mp->rtp = &packet->rtp;
if (packet->func(ch, packet, mp))
ilog(LOG_WARN, "Decoder error while processing RTP packet");
next:
__transcode_packet_free(packet);
}
out:
mutex_unlock(&ssrc_in_p->h.lock);
if (ssrc_in_p != ssrc_out_p)
mutex_unlock(&ssrc_out_p->h.lock);
obj_put(&ch->h);
return 0;
}
static void __output_rtp(struct media_packet *mp, struct codec_ssrc_handler *ch,
struct codec_handler *handler, // normally == ch->handler except for DTMF
char *buf, // malloc'd, room for rtp_header + filled-in payload
unsigned int payload_len,
unsigned long payload_ts,
int marker, int seq, int seq_inc, int payload_type)
{
struct rtp_header *rh = (void *) buf;
struct ssrc_ctx *ssrc_out = mp->ssrc_out;
struct ssrc_entry_call *ssrc_out_p = ssrc_out->parent;
// reconstruct RTP header
unsigned long ts = payload_ts;
ZERO(*rh);
rh->v_p_x_cc = 0x80;
if (payload_type == -1)
payload_type = handler->dest_pt.payload_type;
rh->m_pt = payload_type | (marker ? 0x80 : 0);
if (seq != -1)
rh->seq_num = htons(seq);
else
rh->seq_num = htons(ntohs(mp->rtp->seq_num) + (ssrc_out_p->seq_diff += seq_inc));
rh->timestamp = htonl(ts);
rh->ssrc = htonl(ssrc_out_p->h.ssrc);
// add to output queue
struct codec_packet *p = g_slice_alloc0(sizeof(*p));
p->s.s = buf;
p->s.len = payload_len + sizeof(struct rtp_header);
payload_tracker_add(&ssrc_out->tracker, handler->dest_pt.payload_type);
p->free_func = free;
p->ttq_entry.source = handler;
p->rtp = rh;
// this packet is dynamically allocated, so we're able to schedule it.
// determine scheduled time to send
if (ch->first_send.tv_sec && ch->encoder_format.clockrate) {
// scale first_send from first_send_ts to ts
p->ttq_entry.when = ch->first_send;
uint32_t ts_diff = (uint32_t) ts - (uint32_t) ch->first_send_ts; // allow for wrap-around
unsigned long long ts_diff_us =
(unsigned long long) ts_diff * 1000000 / ch->encoder_format.clockrate
* ch->handler->dest_pt.codec_def->clockrate_mult;
timeval_add_usec(&p->ttq_entry.when, ts_diff_us);
// how far in the future is this?
ts_diff_us = timeval_diff(&p->ttq_entry.when, &rtpe_now); // negative wrap-around to positive OK
if (ts_diff_us > 1000000) // more than one second, can't be right
ch->first_send.tv_sec = 0; // fix it up below
}
if (!ch->first_send.tv_sec) {
p->ttq_entry.when = ch->first_send = rtpe_now;
ch->first_send_ts = ts;
}
ilog(LOG_DEBUG, "Scheduling to send RTP packet (seq %u TS %lu) at %lu.%06lu",
ntohs(rh->seq_num),
ts,
(long unsigned) p->ttq_entry.when.tv_sec,
(long unsigned) p->ttq_entry.when.tv_usec);
g_queue_push_tail(&mp->packets_out, p);
atomic64_inc(&ssrc_out->packets);