forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtp_engine.c
4030 lines (3418 loc) · 122 KB
/
rtp_engine.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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2008, Digium, Inc.
*
* Joshua Colp <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Pluggable RTP Architecture
*
* \author Joshua Colp <[email protected]>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
/*** DOCUMENTATION
<managerEvent language="en_US" name="RTCPSent">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised when an RTCP packet is sent.</synopsis>
<syntax>
<channel_snapshot/>
<parameter name="SSRC">
<para>The SSRC identifier for our stream</para>
</parameter>
<parameter name="PT">
<para>The type of packet for this RTCP report.</para>
<enumlist>
<enum name="200(SR)"/>
<enum name="201(RR)"/>
</enumlist>
</parameter>
<parameter name="To">
<para>The address the report is sent to.</para>
</parameter>
<parameter name="ReportCount">
<para>The number of reports that were sent.</para>
<para>The report count determines the number of ReportX headers in
the message. The X for each set of report headers will range from 0 to
<literal>ReportCount - 1</literal>.</para>
</parameter>
<parameter name="SentNTP" required="false">
<para>The time the sender generated the report. Only valid when
PT is <literal>200(SR)</literal>.</para>
</parameter>
<parameter name="SentRTP" required="false">
<para>The sender's last RTP timestamp. Only valid when PT is
<literal>200(SR)</literal>.</para>
</parameter>
<parameter name="SentPackets" required="false">
<para>The number of packets the sender has sent. Only valid when PT
is <literal>200(SR)</literal>.</para>
</parameter>
<parameter name="SentOctets" required="false">
<para>The number of bytes the sender has sent. Only valid when PT is
<literal>200(SR)</literal>.</para>
</parameter>
<parameter name="ReportXSourceSSRC">
<para>The SSRC for the source of this report block.</para>
</parameter>
<parameter name="ReportXFractionLost">
<para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
lost since the previous SR or RR report was sent.</para>
</parameter>
<parameter name="ReportXCumulativeLost">
<para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
lost since the beginning of reception.</para>
</parameter>
<parameter name="ReportXHighestSequence">
<para>The highest sequence number received in an RTP data packet from
<literal>ReportXSourceSSRC</literal>.</para>
</parameter>
<parameter name="ReportXSequenceNumberCycles">
<para>The number of sequence number cycles seen for the RTP data
received from <literal>ReportXSourceSSRC</literal>.</para>
</parameter>
<parameter name="ReportXIAJitter">
<para>An estimate of the statistical variance of the RTP data packet
interarrival time, measured in timestamp units.</para>
</parameter>
<parameter name="ReportXLSR">
<para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
If no SR has been received from <literal>ReportXSourceSSRC</literal>,
then 0.</para>
</parameter>
<parameter name="ReportXDLSR">
<para>The delay, expressed in units of 1/65536 seconds, between
receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
and sending this report.</para>
</parameter>
</syntax>
<see-also>
<ref type="managerEvent">RTCPReceived</ref>
</see-also>
</managerEventInstance>
</managerEvent>
<managerEvent language="en_US" name="RTCPReceived">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised when an RTCP packet is received.</synopsis>
<syntax>
<channel_snapshot/>
<parameter name="SSRC">
<para>The SSRC identifier for the remote system</para>
</parameter>
<xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
<parameter name="From">
<para>The address the report was received from.</para>
</parameter>
<parameter name="RTT">
<para>Calculated Round-Trip Time in seconds</para>
</parameter>
<parameter name="ReportCount">
<para>The number of reports that were received.</para>
<para>The report count determines the number of ReportX headers in
the message. The X for each set of report headers will range from 0 to
<literal>ReportCount - 1</literal>.</para>
</parameter>
<xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
<xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
<xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
<xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
<xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
</syntax>
<see-also>
<ref type="managerEvent">RTCPSent</ref>
</see-also>
</managerEventInstance>
</managerEvent>
***/
#include "asterisk.h"
#include <math.h> /* for sqrt, MAX */
#include <sched.h> /* for sched_yield */
#include <sys/time.h> /* for timeval */
#include <time.h> /* for time_t */
#include "asterisk/_private.h" /* for ast_rtp_engine_init prototype */
#include "asterisk/astobj2.h" /* for ao2_cleanup, ao2_ref, etc */
#include "asterisk/channel.h" /* for ast_channel_name, etc */
#include "asterisk/codec.h" /* for ast_codec_media_type2str, etc */
#include "asterisk/format.h" /* for ast_format_cmp, etc */
#include "asterisk/format_cache.h" /* for ast_format_adpcm, etc */
#include "asterisk/format_cap.h" /* for ast_format_cap_alloc, etc */
#include "asterisk/json.h" /* for ast_json_ref, etc */
#include "asterisk/linkedlists.h" /* for ast_rtp_engine::<anonymous>, etc */
#include "asterisk/lock.h" /* for ast_rwlock_unlock, etc */
#include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
#include "asterisk/manager.h"
#include "asterisk/module.h" /* for ast_module_unref, etc */
#include "asterisk/netsock2.h" /* for ast_sockaddr_copy, etc */
#include "asterisk/options.h" /* for ast_option_rtpptdynamic */
#include "asterisk/pbx.h" /* for pbx_builtin_setvar_helper */
#include "asterisk/res_srtp.h" /* for ast_srtp_res */
#include "asterisk/rtp_engine.h" /* for ast_rtp_codecs, etc */
#include "asterisk/stasis.h" /* for stasis_message_data, etc */
#include "asterisk/stasis_channels.h" /* for ast_channel_stage_snapshot, etc */
#include "asterisk/strings.h" /* for ast_str_append, etc */
#include "asterisk/time.h" /* for ast_tvdiff_ms, ast_tvnow */
#include "asterisk/translate.h" /* for ast_translate_available_formats */
#include "asterisk/utils.h" /* for ast_free, ast_strdup, etc */
#include "asterisk/vector.h" /* for AST_VECTOR_GET, etc */
struct ast_srtp_res *res_srtp = NULL;
struct ast_srtp_policy_res *res_srtp_policy = NULL;
/*! Structure that contains extmap negotiation information */
struct rtp_extmap {
/*! The RTP extension */
enum ast_rtp_extension extension;
/*! The current negotiated direction */
enum ast_rtp_extension_direction direction;
};
/*! Structure that represents an RTP session (instance) */
struct ast_rtp_instance {
/*! Engine that is handling this RTP instance */
struct ast_rtp_engine *engine;
/*! Data unique to the RTP engine */
void *data;
/*! RTP properties that have been set and their value */
int properties[AST_RTP_PROPERTY_MAX];
/*! Address that we are expecting RTP to come in to */
struct ast_sockaddr local_address;
/*! The original source address */
struct ast_sockaddr requested_target_address;
/*! Address that we are sending RTP to */
struct ast_sockaddr incoming_source_address;
/*! Instance that we are bridged to if doing remote or local bridging */
struct ast_rtp_instance *bridged;
/*! Payload and packetization information */
struct ast_rtp_codecs codecs;
/*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
int timeout;
/*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
int holdtimeout;
/*! RTP keepalive interval */
int keepalive;
/*! Glue currently in use */
struct ast_rtp_glue *glue;
/*! SRTP info associated with the instance */
struct ast_srtp *srtp;
/*! SRTP info dedicated for RTCP associated with the instance */
struct ast_srtp *rtcp_srtp;
/*! Channel unique ID */
char channel_uniqueid[AST_MAX_UNIQUEID];
/*! Time of last packet sent */
time_t last_tx;
/*! Time of last packet received */
time_t last_rx;
/*! Enabled RTP extensions */
AST_VECTOR(, enum ast_rtp_extension_direction) extmap_enabled;
/*! Negotiated RTP extensions (using index based on extension) */
AST_VECTOR(, int) extmap_negotiated;
/*! Negotiated RTP extensions (using index based on unique id) */
AST_VECTOR(, struct rtp_extmap) extmap_unique_ids;
};
/*!
* \brief URIs for known RTP extensions
*/
static const char * const rtp_extension_uris[AST_RTP_EXTENSION_MAX] = {
[AST_RTP_EXTENSION_UNSUPPORTED] = "",
[AST_RTP_EXTENSION_ABS_SEND_TIME] = "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time",
[AST_RTP_EXTENSION_TRANSPORT_WIDE_CC] = "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
};
/*! List of RTP engines that are currently registered */
static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
/*! List of RTP glues */
static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
#define MAX_RTP_MIME_TYPES 128
/*! The following array defines the MIME Media type (and subtype) for each
of our codecs, or RTP-specific data type. */
static struct ast_rtp_mime_type {
/*! \brief A mapping object between the Asterisk codec and this RTP payload */
struct ast_rtp_payload_type payload_type;
/*! \brief The media type */
char type[16];
/*! \brief The format type */
char subtype[64];
/*! \brief Expected sample rate of the /c subtype */
unsigned int sample_rate;
} ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
static ast_rwlock_t mime_types_lock;
static int mime_types_len = 0;
/*!
* \brief Mapping between Asterisk codecs and rtp payload types
*
* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
* also, our own choices for dynamic payload types. This is our master
* table for transmission
*
* See http://www.iana.org/assignments/rtp-parameters for a list of
* assigned values
*/
static struct ast_rtp_payload_type *static_RTP_PT[AST_RTP_MAX_PT];
static ast_rwlock_t static_RTP_PT_lock;
/*! \brief \ref stasis topic for RTP related messages */
static struct stasis_topic *rtp_topic;
/*!
* \brief Set given json object into target with name
*
* \param target Target json.
* \param name key of given object.
* \param obj Json value will be set.
*/
#define SET_AST_JSON_OBJ(target, name, obj) ({ \
struct ast_json *j_tmp = obj; \
if (j_tmp) { \
ast_json_object_set(target, name, j_tmp); \
} \
})
/*!
* \internal
* \brief Destructor for \c ast_rtp_payload_type
*/
static void rtp_payload_type_dtor(void *obj)
{
struct ast_rtp_payload_type *payload = obj;
ao2_cleanup(payload->format);
}
static struct ast_rtp_payload_type *rtp_payload_type_alloc(struct ast_format *format,
int payload, int rtp_code, int primary_mapping)
{
struct ast_rtp_payload_type *type = ao2_alloc_options(
sizeof(*type), rtp_payload_type_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!type) {
return NULL;
}
type->format = ao2_bump(format);
type->asterisk_format = type->format != NULL;
type->payload = payload;
type->rtp_code = rtp_code;
type->primary_mapping = primary_mapping;
return type;
}
struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
{
return rtp_payload_type_alloc(NULL, 0, 0, 0);
}
int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
{
struct ast_rtp_engine *current_engine;
/* Perform a sanity check on the engine structure to make sure it has the basics */
if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
return -1;
}
/* Link owner module to the RTP engine for reference counting purposes */
engine->mod = module;
AST_RWLIST_WRLOCK(&engines);
/* Ensure that no two modules with the same name are registered at the same time */
AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
if (!strcmp(current_engine->name, engine->name)) {
ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
AST_RWLIST_UNLOCK(&engines);
return -1;
}
}
/* The engine survived our critique. Off to the list it goes to be used */
AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
AST_RWLIST_UNLOCK(&engines);
ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
return 0;
}
int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
{
struct ast_rtp_engine *current_engine = NULL;
AST_RWLIST_WRLOCK(&engines);
if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
}
AST_RWLIST_UNLOCK(&engines);
return current_engine ? 0 : -1;
}
int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
{
struct ast_rtp_glue *current_glue = NULL;
if (ast_strlen_zero(glue->type)) {
return -1;
}
glue->mod = module;
AST_RWLIST_WRLOCK(&glues);
AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
if (!strcasecmp(current_glue->type, glue->type)) {
ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
AST_RWLIST_UNLOCK(&glues);
return -1;
}
}
AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
AST_RWLIST_UNLOCK(&glues);
ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
return 0;
}
int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
{
struct ast_rtp_glue *current_glue = NULL;
AST_RWLIST_WRLOCK(&glues);
if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
}
AST_RWLIST_UNLOCK(&glues);
return current_glue ? 0 : -1;
}
static void instance_destructor(void *obj)
{
struct ast_rtp_instance *instance = obj;
/* Pass us off to the engine to destroy */
if (instance->data) {
/*
* Lock in case the RTP engine has other threads that
* need synchronization with the destruction.
*/
ao2_lock(instance);
instance->engine->destroy(instance);
ao2_unlock(instance);
}
if (instance->srtp) {
res_srtp->destroy(instance->srtp);
}
if (instance->rtcp_srtp) {
res_srtp->destroy(instance->rtcp_srtp);
}
ast_rtp_codecs_payloads_destroy(&instance->codecs);
AST_VECTOR_FREE(&instance->extmap_enabled);
AST_VECTOR_FREE(&instance->extmap_negotiated);
AST_VECTOR_FREE(&instance->extmap_unique_ids);
/* Drop our engine reference */
ast_module_unref(instance->engine->mod);
ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
}
int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
{
ao2_cleanup(instance);
return 0;
}
struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
struct ast_sched_context *sched, const struct ast_sockaddr *sa,
void *data)
{
struct ast_sockaddr address = {{0,}};
struct ast_rtp_instance *instance = NULL;
struct ast_rtp_engine *engine = NULL;
struct ast_module *mod_ref;
AST_RWLIST_RDLOCK(&engines);
/* If an engine name was specified try to use it or otherwise use the first one registered */
if (!ast_strlen_zero(engine_name)) {
AST_RWLIST_TRAVERSE(&engines, engine, entry) {
if (!strcmp(engine->name, engine_name)) {
break;
}
}
} else {
engine = AST_RWLIST_FIRST(&engines);
}
/* If no engine was actually found bail out now */
if (!engine) {
ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
AST_RWLIST_UNLOCK(&engines);
return NULL;
}
/* Bump up the reference count before we return so the module can not be unloaded */
mod_ref = ast_module_running_ref(engine->mod);
AST_RWLIST_UNLOCK(&engines);
if (!mod_ref) {
/* BUGBUG: improve handling of this situation. */
return NULL;
}
/* Allocate a new RTP instance */
if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
ast_module_unref(engine->mod);
return NULL;
}
instance->engine = engine;
ast_sockaddr_copy(&instance->local_address, sa);
ast_sockaddr_copy(&address, sa);
if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
ao2_ref(instance, -1);
return NULL;
}
/* Initialize RTP extension support */
if (AST_VECTOR_INIT(&instance->extmap_enabled, 0) ||
AST_VECTOR_INIT(&instance->extmap_negotiated, 0) ||
AST_VECTOR_INIT(&instance->extmap_unique_ids, 0)) {
ao2_ref(instance, -1);
return NULL;
}
ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
/*
* And pass it off to the engine to setup
*
* Lock in case the RTP engine has other threads that
* need synchronization with the construction.
*/
ao2_lock(instance);
if (instance->engine->new(instance, sched, &address, data)) {
ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
ao2_unlock(instance);
ao2_ref(instance, -1);
return NULL;
}
ao2_unlock(instance);
ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
return instance;
}
const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
{
return instance->channel_uniqueid;
}
void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
{
ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
}
void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
{
instance->data = data;
}
void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
{
return instance->data;
}
int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
{
int res;
ao2_lock(instance);
res = instance->engine->write(instance, frame);
ao2_unlock(instance);
return res;
}
struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
{
struct ast_frame *frame;
ao2_lock(instance);
frame = instance->engine->read(instance, rtcp);
ao2_unlock(instance);
return frame;
}
int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
const struct ast_sockaddr *address)
{
ao2_lock(instance);
ast_sockaddr_copy(&instance->local_address, address);
ao2_unlock(instance);
return 0;
}
static void rtp_instance_set_incoming_source_address_nolock(struct ast_rtp_instance *instance,
const struct ast_sockaddr *address)
{
ast_sockaddr_copy(&instance->incoming_source_address, address);
if (instance->engine->remote_address_set) {
instance->engine->remote_address_set(instance, &instance->incoming_source_address);
}
}
int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
const struct ast_sockaddr *address)
{
ao2_lock(instance);
rtp_instance_set_incoming_source_address_nolock(instance, address);
ao2_unlock(instance);
return 0;
}
int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
const struct ast_sockaddr *address)
{
ao2_lock(instance);
ast_sockaddr_copy(&instance->requested_target_address, address);
rtp_instance_set_incoming_source_address_nolock(instance, address);
ao2_unlock(instance);
return 0;
}
int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
struct ast_sockaddr *address)
{
ao2_lock(instance);
if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
ast_sockaddr_copy(address, &instance->local_address);
ao2_unlock(instance);
return 1;
}
ao2_unlock(instance);
return 0;
}
void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
struct ast_sockaddr *address)
{
ao2_lock(instance);
ast_sockaddr_copy(address, &instance->local_address);
ao2_unlock(instance);
}
int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
struct ast_sockaddr *address)
{
ao2_lock(instance);
if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
ast_sockaddr_copy(address, &instance->requested_target_address);
ao2_unlock(instance);
return 1;
}
ao2_unlock(instance);
return 0;
}
void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
struct ast_sockaddr *address)
{
ao2_lock(instance);
ast_sockaddr_copy(address, &instance->incoming_source_address);
ao2_unlock(instance);
}
void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
struct ast_sockaddr *address)
{
ao2_lock(instance);
ast_sockaddr_copy(address, &instance->requested_target_address);
ao2_unlock(instance);
}
void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
{
if (instance->engine->extended_prop_set) {
ao2_lock(instance);
instance->engine->extended_prop_set(instance, property, value);
ao2_unlock(instance);
}
}
void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
{
void *prop;
if (instance->engine->extended_prop_get) {
ao2_lock(instance);
prop = instance->engine->extended_prop_get(instance, property);
ao2_unlock(instance);
} else {
prop = NULL;
}
return prop;
}
void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
{
ao2_lock(instance);
instance->properties[property] = value;
if (instance->engine->prop_set) {
instance->engine->prop_set(instance, property, value);
}
ao2_unlock(instance);
}
int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
{
int prop;
ao2_lock(instance);
prop = instance->properties[property];
ao2_unlock(instance);
return prop;
}
struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
{
return &instance->codecs;
}
int ast_rtp_instance_extmap_enable(struct ast_rtp_instance *instance, int id, enum ast_rtp_extension extension,
enum ast_rtp_extension_direction direction)
{
struct rtp_extmap extmap = {
.extension = extension,
.direction = direction,
};
ao2_lock(instance);
if (!instance->engine->extension_enable || !instance->engine->extension_enable(instance, extension)) {
ao2_unlock(instance);
return 0;
}
/* We store enabled extensions separately so we can easily do negotiation */
if (AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, direction)) {
ao2_unlock(instance);
return -1;
}
if (id <= 0) {
/* We find a free unique identifier for this extension by just appending it to the
* vector of unique ids. The size of the vector will become its unique identifier.
* As well when we are asking for information on the extensions it will be returned,
* allowing it to be added to the SDP offer.
*/
if (AST_VECTOR_APPEND(&instance->extmap_unique_ids, extmap)) {
AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
ao2_unlock(instance);
return -1;
}
id = AST_VECTOR_SIZE(&instance->extmap_unique_ids);
} else {
/* Otherwise we put it precisely where they want it */
if (AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap)) {
AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
ao2_unlock(instance);
return -1;
}
}
/* Now that we have an id add the extension to here */
if (AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id)) {
extmap.extension = AST_RTP_EXTENSION_UNSUPPORTED;
extmap.direction = AST_RTP_EXTENSION_DIRECTION_NONE;
AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap);
ao2_unlock(instance);
return -1;
}
ao2_unlock(instance);
return 0;
}
/*! \brief Helper function which negotiates two RTP extension directions to get our current direction */
static enum ast_rtp_extension_direction rtp_extmap_negotiate_direction(enum ast_rtp_extension_direction ours,
enum ast_rtp_extension_direction theirs)
{
if (theirs == AST_RTP_EXTENSION_DIRECTION_NONE || ours == AST_RTP_EXTENSION_DIRECTION_NONE) {
/* This should not occur but if it does tolerate either side not having this extension
* in use.
*/
return AST_RTP_EXTENSION_DIRECTION_NONE;
} else if (theirs == AST_RTP_EXTENSION_DIRECTION_INACTIVE) {
/* Inactive is always inactive on our side */
return AST_RTP_EXTENSION_DIRECTION_INACTIVE;
} else if (theirs == AST_RTP_EXTENSION_DIRECTION_SENDRECV) {
return ours;
} else if (theirs == AST_RTP_EXTENSION_DIRECTION_SENDONLY) {
/* If they are send only then we become recvonly if we are configured as sendrecv or recvonly */
if (ours == AST_RTP_EXTENSION_DIRECTION_SENDRECV || ours == AST_RTP_EXTENSION_DIRECTION_RECVONLY) {
return AST_RTP_EXTENSION_DIRECTION_RECVONLY;
}
} else if (theirs == AST_RTP_EXTENSION_DIRECTION_RECVONLY) {
/* If they are recv only then we become sendonly if we are configured as sendrecv or sendonly */
if (ours == AST_RTP_EXTENSION_DIRECTION_SENDRECV || ours == AST_RTP_EXTENSION_DIRECTION_SENDONLY) {
return AST_RTP_EXTENSION_DIRECTION_SENDONLY;
}
}
return AST_RTP_EXTENSION_DIRECTION_NONE;
}
int ast_rtp_instance_extmap_negotiate(struct ast_rtp_instance *instance, int id, enum ast_rtp_extension_direction direction,
const char *uri, const char *attributes)
{
/* 'attributes' is currently unused but exists in the API to ensure it does not need to be altered
* in the future in case we need to use it.
*/
int idx;
enum ast_rtp_extension extension = AST_RTP_EXTENSION_UNSUPPORTED;
/* Per the RFC the identifier has to be 1 or above */
if (id < 1) {
return -1;
}
/* Convert the provided URI to the internal representation */
for (idx = 0; idx < ARRAY_LEN(rtp_extension_uris); ++idx) {
if (!strcasecmp(rtp_extension_uris[idx], uri)) {
extension = idx;
break;
}
}
ao2_lock(instance);
/* We only accept the extension if it is enabled */
if (extension < AST_VECTOR_SIZE(&instance->extmap_enabled) &&
AST_VECTOR_GET(&instance->extmap_enabled, extension) != AST_RTP_EXTENSION_DIRECTION_NONE) {
struct rtp_extmap extmap = {
.extension = extension,
.direction = rtp_extmap_negotiate_direction(AST_VECTOR_GET(&instance->extmap_enabled, extension), direction),
};
/* If the direction negotiation failed then don't accept or use this extension */
if (extmap.direction != AST_RTP_EXTENSION_DIRECTION_NONE) {
if (extension != AST_RTP_EXTENSION_UNSUPPORTED) {
AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id);
}
AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap);
}
}
ao2_unlock(instance);
return 0;
}
void ast_rtp_instance_extmap_clear(struct ast_rtp_instance *instance)
{
static const struct rtp_extmap extmap_none = {
.extension = AST_RTP_EXTENSION_UNSUPPORTED,
.direction = AST_RTP_EXTENSION_DIRECTION_NONE,
};
int idx;
ao2_lock(instance);
/* Clear both the known unique ids and the negotiated extensions as we are about to have
* new results set on us.
*/
for (idx = 0; idx < AST_VECTOR_SIZE(&instance->extmap_unique_ids); ++idx) {
AST_VECTOR_REPLACE(&instance->extmap_unique_ids, idx, extmap_none);
}
for (idx = 0; idx < AST_VECTOR_SIZE(&instance->extmap_negotiated); ++idx) {
AST_VECTOR_REPLACE(&instance->extmap_negotiated, idx, -1);
}
ao2_unlock(instance);
}
int ast_rtp_instance_extmap_get_id(struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
{
int id = -1;
ao2_lock(instance);
if (extension < AST_VECTOR_SIZE(&instance->extmap_negotiated)) {
id = AST_VECTOR_GET(&instance->extmap_negotiated, extension);
}
ao2_unlock(instance);
return id;
}
size_t ast_rtp_instance_extmap_count(struct ast_rtp_instance *instance)
{
size_t count;
ao2_lock(instance);
count = AST_VECTOR_SIZE(&instance->extmap_unique_ids);
ao2_unlock(instance);
return count;
}
enum ast_rtp_extension ast_rtp_instance_extmap_get_extension(struct ast_rtp_instance *instance, int id)
{
enum ast_rtp_extension extension = AST_RTP_EXTENSION_UNSUPPORTED;
ao2_lock(instance);
/* The local unique identifier starts at '1' so the highest unique identifier
* can be the actual size of the vector. We compensate (as it is 0 index based)
* by dropping it down to 1 to get the correct information.
*/
if (0 < id && id <= AST_VECTOR_SIZE(&instance->extmap_unique_ids)) {
struct rtp_extmap *extmap = AST_VECTOR_GET_ADDR(&instance->extmap_unique_ids, id - 1);
extension = extmap->extension;
}
ao2_unlock(instance);
return extension;
}
enum ast_rtp_extension_direction ast_rtp_instance_extmap_get_direction(struct ast_rtp_instance *instance, int id)
{
enum ast_rtp_extension_direction direction = AST_RTP_EXTENSION_DIRECTION_NONE;
ao2_lock(instance);
if (0 < id && id <= AST_VECTOR_SIZE(&instance->extmap_unique_ids)) {
struct rtp_extmap *extmap = AST_VECTOR_GET_ADDR(&instance->extmap_unique_ids, id - 1);
direction = extmap->direction;
}
ao2_unlock(instance);
return direction;
}
const char *ast_rtp_instance_extmap_get_uri(struct ast_rtp_instance *instance, int id)
{
enum ast_rtp_extension extension = ast_rtp_instance_extmap_get_extension(instance, id);
if (extension == AST_RTP_EXTENSION_UNSUPPORTED ||
(unsigned int)extension >= ARRAY_LEN(rtp_extension_uris)) {
return NULL;
}
return rtp_extension_uris[extension];
}
int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
{
int res;
codecs->framing = 0;
ast_rwlock_init(&codecs->codecs_lock);
res = AST_VECTOR_INIT(&codecs->payload_mapping_rx, AST_RTP_MAX_PT);
res |= AST_VECTOR_INIT(&codecs->payload_mapping_tx, AST_RTP_MAX_PT);
if (res) {
AST_VECTOR_FREE(&codecs->payload_mapping_rx);
AST_VECTOR_FREE(&codecs->payload_mapping_tx);
}
return res;
}
void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
{
int idx;
struct ast_rtp_payload_type *type;
for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
ao2_t_cleanup(type, "destroying ast_rtp_codec rx mapping");
}
AST_VECTOR_FREE(&codecs->payload_mapping_rx);
for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
}
AST_VECTOR_FREE(&codecs->payload_mapping_tx);
ast_rwlock_destroy(&codecs->codecs_lock);
}
void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
{
ast_rtp_codecs_payloads_destroy(codecs);
ast_rtp_codecs_payloads_initialize(codecs);
if (instance && instance->engine && instance->engine->payload_set) {
int i;