res/res_ari: Added ARI resource /ari/channels/{channelId}/rtp_statistics
[asterisk/asterisk.git] / main / rtp_engine.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Pluggable RTP Architecture
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28 ***/
29
30 /*** DOCUMENTATION
31         <managerEvent language="en_US" name="RTCPSent">
32                 <managerEventInstance class="EVENT_FLAG_REPORTING">
33                         <synopsis>Raised when an RTCP packet is sent.</synopsis>
34                         <syntax>
35                                 <channel_snapshot/>
36                                 <parameter name="SSRC">
37                                         <para>The SSRC identifier for our stream</para>
38                                 </parameter>
39                                 <parameter name="PT">
40                                         <para>The type of packet for this RTCP report.</para>
41                                         <enumlist>
42                                                 <enum name="200(SR)"/>
43                                                 <enum name="201(RR)"/>
44                                         </enumlist>
45                                 </parameter>
46                                 <parameter name="To">
47                                         <para>The address the report is sent to.</para>
48                                 </parameter>
49                                 <parameter name="ReportCount">
50                                         <para>The number of reports that were sent.</para>
51                                         <para>The report count determines the number of ReportX headers in
52                                         the message. The X for each set of report headers will range from 0 to
53                                         <literal>ReportCount - 1</literal>.</para>
54                                 </parameter>
55                                 <parameter name="SentNTP" required="false">
56                                         <para>The time the sender generated the report. Only valid when
57                                         PT is <literal>200(SR)</literal>.</para>
58                                 </parameter>
59                                 <parameter name="SentRTP" required="false">
60                                         <para>The sender's last RTP timestamp. Only valid when PT is
61                                         <literal>200(SR)</literal>.</para>
62                                 </parameter>
63                                 <parameter name="SentPackets" required="false">
64                                         <para>The number of packets the sender has sent. Only valid when PT
65                                         is <literal>200(SR)</literal>.</para>
66                                 </parameter>
67                                 <parameter name="SentOctets" required="false">
68                                         <para>The number of bytes the sender has sent. Only valid when PT is
69                                         <literal>200(SR)</literal>.</para>
70                                 </parameter>
71                                 <parameter name="ReportXSourceSSRC">
72                                         <para>The SSRC for the source of this report block.</para>
73                                 </parameter>
74                                 <parameter name="ReportXFractionLost">
75                                         <para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
76                                         lost since the previous SR or RR report was sent.</para>
77                                 </parameter>
78                                 <parameter name="ReportXCumulativeLost">
79                                         <para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
80                                         lost since the beginning of reception.</para>
81                                 </parameter>
82                                 <parameter name="ReportXHighestSequence">
83                                         <para>The highest sequence number received in an RTP data packet from
84                                         <literal>ReportXSourceSSRC</literal>.</para>
85                                 </parameter>
86                                 <parameter name="ReportXSequenceNumberCycles">
87                                         <para>The number of sequence number cycles seen for the RTP data
88                                         received from <literal>ReportXSourceSSRC</literal>.</para>
89                                 </parameter>
90                                 <parameter name="ReportXIAJitter">
91                                         <para>An estimate of the statistical variance of the RTP data packet
92                                         interarrival time, measured in timestamp units.</para>
93                                 </parameter>
94                                 <parameter name="ReportXLSR">
95                                         <para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
96                                         If no SR has been received from <literal>ReportXSourceSSRC</literal>,
97                                         then 0.</para>
98                                 </parameter>
99                                 <parameter name="ReportXDLSR">
100                                         <para>The delay, expressed in units of 1/65536 seconds, between
101                                         receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
102                                         and sending this report.</para>
103                                 </parameter>
104                         </syntax>
105                         <see-also>
106                                 <ref type="managerEvent">RTCPReceived</ref>
107                         </see-also>
108                 </managerEventInstance>
109         </managerEvent>
110         <managerEvent language="en_US" name="RTCPReceived">
111                 <managerEventInstance class="EVENT_FLAG_REPORTING">
112                         <synopsis>Raised when an RTCP packet is received.</synopsis>
113                         <syntax>
114                                 <channel_snapshot/>
115                                 <parameter name="SSRC">
116                                         <para>The SSRC identifier for the remote system</para>
117                                 </parameter>
118                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
119                                 <parameter name="From">
120                                         <para>The address the report was received from.</para>
121                                 </parameter>
122                                 <parameter name="RTT">
123                                         <para>Calculated Round-Trip Time in seconds</para>
124                                 </parameter>
125                                 <parameter name="ReportCount">
126                                         <para>The number of reports that were received.</para>
127                                         <para>The report count determines the number of ReportX headers in
128                                         the message. The X for each set of report headers will range from 0 to
129                                         <literal>ReportCount - 1</literal>.</para>
130                                 </parameter>
131                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
132                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
133                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
134                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
135                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
136                         </syntax>
137                         <see-also>
138                                 <ref type="managerEvent">RTCPSent</ref>
139                         </see-also>
140                 </managerEventInstance>
141         </managerEvent>
142  ***/
143
144 #include "asterisk.h"
145
146 #include <math.h>                       /* for sqrt, MAX */
147 #include <sched.h>                      /* for sched_yield */
148 #include <sys/time.h>                   /* for timeval */
149 #include <time.h>                       /* for time_t */
150
151 #include "asterisk/_private.h"          /* for ast_rtp_engine_init prototype */
152 #include "asterisk/astobj2.h"           /* for ao2_cleanup, ao2_ref, etc */
153 #include "asterisk/channel.h"           /* for ast_channel_name, etc */
154 #include "asterisk/codec.h"             /* for ast_codec_media_type2str, etc */
155 #include "asterisk/format.h"            /* for ast_format_cmp, etc */
156 #include "asterisk/format_cache.h"      /* for ast_format_adpcm, etc */
157 #include "asterisk/format_cap.h"        /* for ast_format_cap_alloc, etc */
158 #include "asterisk/json.h"              /* for ast_json_ref, etc */
159 #include "asterisk/linkedlists.h"       /* for ast_rtp_engine::<anonymous>, etc */
160 #include "asterisk/lock.h"              /* for ast_rwlock_unlock, etc */
161 #include "asterisk/logger.h"            /* for ast_log, ast_debug, etc */
162 #include "asterisk/manager.h"
163 #include "asterisk/module.h"            /* for ast_module_unref, etc */
164 #include "asterisk/netsock2.h"          /* for ast_sockaddr_copy, etc */
165 #include "asterisk/options.h"           /* for ast_option_rtpptdynamic */
166 #include "asterisk/pbx.h"               /* for pbx_builtin_setvar_helper */
167 #include "asterisk/res_srtp.h"          /* for ast_srtp_res */
168 #include "asterisk/rtp_engine.h"        /* for ast_rtp_codecs, etc */
169 #include "asterisk/stasis.h"            /* for stasis_message_data, etc */
170 #include "asterisk/stasis_channels.h"   /* for ast_channel_stage_snapshot, etc */
171 #include "asterisk/strings.h"           /* for ast_str_append, etc */
172 #include "asterisk/time.h"              /* for ast_tvdiff_ms, ast_tvnow */
173 #include "asterisk/translate.h"         /* for ast_translate_available_formats */
174 #include "asterisk/utils.h"             /* for ast_free, ast_strdup, etc */
175 #include "asterisk/vector.h"            /* for AST_VECTOR_GET, etc */
176
177 struct ast_srtp_res *res_srtp = NULL;
178 struct ast_srtp_policy_res *res_srtp_policy = NULL;
179
180 /*! Structure that contains extmap negotiation information */
181 struct rtp_extmap {
182         /*! The RTP extension */
183         enum ast_rtp_extension extension;
184         /*! The current negotiated direction */
185         enum ast_rtp_extension_direction direction;
186 };
187
188 /*! Structure that represents an RTP session (instance) */
189 struct ast_rtp_instance {
190         /*! Engine that is handling this RTP instance */
191         struct ast_rtp_engine *engine;
192         /*! Data unique to the RTP engine */
193         void *data;
194         /*! RTP properties that have been set and their value */
195         int properties[AST_RTP_PROPERTY_MAX];
196         /*! Address that we are expecting RTP to come in to */
197         struct ast_sockaddr local_address;
198         /*! The original source address */
199         struct ast_sockaddr requested_target_address;
200         /*! Address that we are sending RTP to */
201         struct ast_sockaddr incoming_source_address;
202         /*! Instance that we are bridged to if doing remote or local bridging */
203         struct ast_rtp_instance *bridged;
204         /*! Payload and packetization information */
205         struct ast_rtp_codecs codecs;
206         /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
207         int timeout;
208         /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
209         int holdtimeout;
210         /*! RTP keepalive interval */
211         int keepalive;
212         /*! Glue currently in use */
213         struct ast_rtp_glue *glue;
214         /*! SRTP info associated with the instance */
215         struct ast_srtp *srtp;
216         /*! SRTP info dedicated for RTCP associated with the instance */
217         struct ast_srtp *rtcp_srtp;
218         /*! Channel unique ID */
219         char channel_uniqueid[AST_MAX_UNIQUEID];
220         /*! Time of last packet sent */
221         time_t last_tx;
222         /*! Time of last packet received */
223         time_t last_rx;
224         /*! Enabled RTP extensions */
225         AST_VECTOR(, enum ast_rtp_extension_direction) extmap_enabled;
226         /*! Negotiated RTP extensions (using index based on extension) */
227         AST_VECTOR(, int) extmap_negotiated;
228         /*! Negotiated RTP extensions (using index based on unique id) */
229         AST_VECTOR(, struct rtp_extmap) extmap_unique_ids;
230 };
231
232 /*!
233  * \brief URIs for known RTP extensions
234  */
235 static const char * const rtp_extension_uris[AST_RTP_EXTENSION_MAX] = {
236         [AST_RTP_EXTENSION_UNSUPPORTED]         = "",
237         [AST_RTP_EXTENSION_ABS_SEND_TIME]       = "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time",
238 };
239
240 /*! List of RTP engines that are currently registered */
241 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
242
243 /*! List of RTP glues */
244 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
245
246 #define MAX_RTP_MIME_TYPES 128
247
248 /*! The following array defines the MIME Media type (and subtype) for each
249    of our codecs, or RTP-specific data type. */
250 static struct ast_rtp_mime_type {
251         /*! \brief A mapping object between the Asterisk codec and this RTP payload */
252         struct ast_rtp_payload_type payload_type;
253         /*! \brief The media type */
254         char type[16];
255         /*! \brief The format type */
256         char subtype[64];
257         /*! \brief Expected sample rate of the /c subtype */
258         unsigned int sample_rate;
259 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
260 static ast_rwlock_t mime_types_lock;
261 static int mime_types_len = 0;
262
263 /*!
264  * \brief Mapping between Asterisk codecs and rtp payload types
265  *
266  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
267  * also, our own choices for dynamic payload types.  This is our master
268  * table for transmission
269  *
270  * See http://www.iana.org/assignments/rtp-parameters for a list of
271  * assigned values
272  */
273 static struct ast_rtp_payload_type *static_RTP_PT[AST_RTP_MAX_PT];
274 static ast_rwlock_t static_RTP_PT_lock;
275
276 /*! \brief \ref stasis topic for RTP related messages */
277 static struct stasis_topic *rtp_topic;
278
279
280 /*!
281  * \brief Set given json object into target with name
282  *
283  * \param target Target json.
284  * \param name key of given object.
285  * \param obj Json value will be set.
286  */
287 #define SET_AST_JSON_OBJ(target, name, obj) ({                                  \
288         struct ast_json *j_tmp = obj;                                           \
289         if (j_tmp) {                                            \
290                 ast_json_object_set(target, name, j_tmp);                                               \
291         }                                               \
292 })
293
294 /*!
295  * \internal
296  * \brief Destructor for \c ast_rtp_payload_type
297  */
298 static void rtp_payload_type_dtor(void *obj)
299 {
300         struct ast_rtp_payload_type *payload = obj;
301
302         ao2_cleanup(payload->format);
303 }
304
305 static struct ast_rtp_payload_type *rtp_payload_type_alloc(struct ast_format *format,
306         int payload, int rtp_code, int primary_mapping)
307 {
308         struct ast_rtp_payload_type *type = ao2_alloc_options(
309                 sizeof(*type), rtp_payload_type_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
310
311         if (!type) {
312                 return NULL;
313         }
314
315         type->format = ao2_bump(format);
316         type->asterisk_format = type->format != NULL;
317         type->payload = payload;
318         type->rtp_code = rtp_code;
319         type->primary_mapping = primary_mapping;
320
321         return type;
322 }
323
324 struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
325 {
326         return rtp_payload_type_alloc(NULL, 0, 0, 0);
327 }
328
329 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
330 {
331         struct ast_rtp_engine *current_engine;
332
333         /* Perform a sanity check on the engine structure to make sure it has the basics */
334         if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
335                 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
336                 return -1;
337         }
338
339         /* Link owner module to the RTP engine for reference counting purposes */
340         engine->mod = module;
341
342         AST_RWLIST_WRLOCK(&engines);
343
344         /* Ensure that no two modules with the same name are registered at the same time */
345         AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
346                 if (!strcmp(current_engine->name, engine->name)) {
347                         ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
348                         AST_RWLIST_UNLOCK(&engines);
349                         return -1;
350                 }
351         }
352
353         /* The engine survived our critique. Off to the list it goes to be used */
354         AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
355
356         AST_RWLIST_UNLOCK(&engines);
357
358         ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
359
360         return 0;
361 }
362
363 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
364 {
365         struct ast_rtp_engine *current_engine = NULL;
366
367         AST_RWLIST_WRLOCK(&engines);
368
369         if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
370                 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
371         }
372
373         AST_RWLIST_UNLOCK(&engines);
374
375         return current_engine ? 0 : -1;
376 }
377
378 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
379 {
380         struct ast_rtp_glue *current_glue = NULL;
381
382         if (ast_strlen_zero(glue->type)) {
383                 return -1;
384         }
385
386         glue->mod = module;
387
388         AST_RWLIST_WRLOCK(&glues);
389
390         AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
391                 if (!strcasecmp(current_glue->type, glue->type)) {
392                         ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
393                         AST_RWLIST_UNLOCK(&glues);
394                         return -1;
395                 }
396         }
397
398         AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
399
400         AST_RWLIST_UNLOCK(&glues);
401
402         ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
403
404         return 0;
405 }
406
407 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
408 {
409         struct ast_rtp_glue *current_glue = NULL;
410
411         AST_RWLIST_WRLOCK(&glues);
412
413         if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
414                 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
415         }
416
417         AST_RWLIST_UNLOCK(&glues);
418
419         return current_glue ? 0 : -1;
420 }
421
422 static void instance_destructor(void *obj)
423 {
424         struct ast_rtp_instance *instance = obj;
425
426         /* Pass us off to the engine to destroy */
427         if (instance->data) {
428                 /*
429                  * Lock in case the RTP engine has other threads that
430                  * need synchronization with the destruction.
431                  */
432                 ao2_lock(instance);
433                 instance->engine->destroy(instance);
434                 ao2_unlock(instance);
435         }
436
437         if (instance->srtp) {
438                 res_srtp->destroy(instance->srtp);
439         }
440
441         if (instance->rtcp_srtp) {
442                 res_srtp->destroy(instance->rtcp_srtp);
443         }
444
445         ast_rtp_codecs_payloads_destroy(&instance->codecs);
446
447         AST_VECTOR_FREE(&instance->extmap_enabled);
448         AST_VECTOR_FREE(&instance->extmap_negotiated);
449         AST_VECTOR_FREE(&instance->extmap_unique_ids);
450
451         /* Drop our engine reference */
452         ast_module_unref(instance->engine->mod);
453
454         ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
455 }
456
457 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
458 {
459         ao2_ref(instance, -1);
460
461         return 0;
462 }
463
464 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
465                 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
466                 void *data)
467 {
468         struct ast_sockaddr address = {{0,}};
469         struct ast_rtp_instance *instance = NULL;
470         struct ast_rtp_engine *engine = NULL;
471         struct ast_module *mod_ref;
472
473         AST_RWLIST_RDLOCK(&engines);
474
475         /* If an engine name was specified try to use it or otherwise use the first one registered */
476         if (!ast_strlen_zero(engine_name)) {
477                 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
478                         if (!strcmp(engine->name, engine_name)) {
479                                 break;
480                         }
481                 }
482         } else {
483                 engine = AST_RWLIST_FIRST(&engines);
484         }
485
486         /* If no engine was actually found bail out now */
487         if (!engine) {
488                 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
489                 AST_RWLIST_UNLOCK(&engines);
490                 return NULL;
491         }
492
493         /* Bump up the reference count before we return so the module can not be unloaded */
494         mod_ref = ast_module_running_ref(engine->mod);
495
496         AST_RWLIST_UNLOCK(&engines);
497
498         if (!mod_ref) {
499                 /* BUGBUG: improve handling of this situation. */
500                 return NULL;
501         }
502
503         /* Allocate a new RTP instance */
504         if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
505                 ast_module_unref(engine->mod);
506                 return NULL;
507         }
508         instance->engine = engine;
509         ast_sockaddr_copy(&instance->local_address, sa);
510         ast_sockaddr_copy(&address, sa);
511
512         if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
513                 ao2_ref(instance, -1);
514                 return NULL;
515         }
516
517         /* Initialize RTP extension support */
518         if (AST_VECTOR_INIT(&instance->extmap_enabled, 0) ||
519                 AST_VECTOR_INIT(&instance->extmap_negotiated, 0) ||
520                 AST_VECTOR_INIT(&instance->extmap_unique_ids, 0)) {
521                 ao2_ref(instance, -1);
522                 return NULL;
523         }
524
525         ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
526
527         /*
528          * And pass it off to the engine to setup
529          *
530          * Lock in case the RTP engine has other threads that
531          * need synchronization with the construction.
532          */
533         ao2_lock(instance);
534         if (instance->engine->new(instance, sched, &address, data)) {
535                 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
536                 ao2_unlock(instance);
537                 ao2_ref(instance, -1);
538                 return NULL;
539         }
540         ao2_unlock(instance);
541
542         ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
543
544         return instance;
545 }
546
547 const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
548 {
549         return instance->channel_uniqueid;
550 }
551
552 void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
553 {
554         ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
555 }
556
557 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
558 {
559         instance->data = data;
560 }
561
562 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
563 {
564         return instance->data;
565 }
566
567 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
568 {
569         int res;
570
571         ao2_lock(instance);
572         res = instance->engine->write(instance, frame);
573         ao2_unlock(instance);
574         return res;
575 }
576
577 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
578 {
579         struct ast_frame *frame;
580
581         ao2_lock(instance);
582         frame = instance->engine->read(instance, rtcp);
583         ao2_unlock(instance);
584         return frame;
585 }
586
587 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
588                 const struct ast_sockaddr *address)
589 {
590         ao2_lock(instance);
591         ast_sockaddr_copy(&instance->local_address, address);
592         ao2_unlock(instance);
593         return 0;
594 }
595
596 static void rtp_instance_set_incoming_source_address_nolock(struct ast_rtp_instance *instance,
597         const struct ast_sockaddr *address)
598 {
599         ast_sockaddr_copy(&instance->incoming_source_address, address);
600         if (instance->engine->remote_address_set) {
601                 instance->engine->remote_address_set(instance, &instance->incoming_source_address);
602         }
603 }
604
605 int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
606         const struct ast_sockaddr *address)
607 {
608         ao2_lock(instance);
609         rtp_instance_set_incoming_source_address_nolock(instance, address);
610         ao2_unlock(instance);
611
612         return 0;
613 }
614
615 int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
616                                                   const struct ast_sockaddr *address)
617 {
618         ao2_lock(instance);
619
620         ast_sockaddr_copy(&instance->requested_target_address, address);
621         rtp_instance_set_incoming_source_address_nolock(instance, address);
622
623         ao2_unlock(instance);
624
625         return 0;
626 }
627
628 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
629                 struct ast_sockaddr *address)
630 {
631         ao2_lock(instance);
632         if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
633                 ast_sockaddr_copy(address, &instance->local_address);
634                 ao2_unlock(instance);
635                 return 1;
636         }
637         ao2_unlock(instance);
638
639         return 0;
640 }
641
642 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
643                 struct ast_sockaddr *address)
644 {
645         ao2_lock(instance);
646         ast_sockaddr_copy(address, &instance->local_address);
647         ao2_unlock(instance);
648 }
649
650 int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
651                 struct ast_sockaddr *address)
652 {
653         ao2_lock(instance);
654         if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
655                 ast_sockaddr_copy(address, &instance->requested_target_address);
656                 ao2_unlock(instance);
657                 return 1;
658         }
659         ao2_unlock(instance);
660
661         return 0;
662 }
663
664 void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
665                                                   struct ast_sockaddr *address)
666 {
667         ao2_lock(instance);
668         ast_sockaddr_copy(address, &instance->incoming_source_address);
669         ao2_unlock(instance);
670 }
671
672 void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
673                                                    struct ast_sockaddr *address)
674 {
675         ao2_lock(instance);
676         ast_sockaddr_copy(address, &instance->requested_target_address);
677         ao2_unlock(instance);
678 }
679
680 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
681 {
682         if (instance->engine->extended_prop_set) {
683                 ao2_lock(instance);
684                 instance->engine->extended_prop_set(instance, property, value);
685                 ao2_unlock(instance);
686         }
687 }
688
689 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
690 {
691         void *prop;
692
693         if (instance->engine->extended_prop_get) {
694                 ao2_lock(instance);
695                 prop = instance->engine->extended_prop_get(instance, property);
696                 ao2_unlock(instance);
697         } else {
698                 prop = NULL;
699         }
700
701         return prop;
702 }
703
704 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
705 {
706         ao2_lock(instance);
707         instance->properties[property] = value;
708
709         if (instance->engine->prop_set) {
710                 instance->engine->prop_set(instance, property, value);
711         }
712         ao2_unlock(instance);
713 }
714
715 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
716 {
717         int prop;
718
719         ao2_lock(instance);
720         prop = instance->properties[property];
721         ao2_unlock(instance);
722
723         return prop;
724 }
725
726 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
727 {
728         return &instance->codecs;
729 }
730
731 int ast_rtp_instance_extmap_enable(struct ast_rtp_instance *instance, int id, enum ast_rtp_extension extension,
732         enum ast_rtp_extension_direction direction)
733 {
734         struct rtp_extmap extmap = {
735                 .extension = extension,
736                 .direction = direction,
737         };
738
739         ao2_lock(instance);
740
741         if (!instance->engine->extension_enable || !instance->engine->extension_enable(instance, extension)) {
742                 ao2_unlock(instance);
743                 return 0;
744         }
745
746         /* We store enabled extensions separately so we can easily do negotiation */
747         if (AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, direction)) {
748                 ao2_unlock(instance);
749                 return -1;
750         }
751
752         if (id <= 0) {
753                 /* We find a free unique identifier for this extension by just appending it to the
754                  * vector of unique ids. The size of the vector will become its unique identifier.
755                  * As well when we are asking for information on the extensions it will be returned,
756                  * allowing it to be added to the SDP offer.
757                  */
758                 if (AST_VECTOR_APPEND(&instance->extmap_unique_ids, extmap)) {
759                         AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
760                         ao2_unlock(instance);
761                         return -1;
762                 }
763                 id = AST_VECTOR_SIZE(&instance->extmap_unique_ids);
764         } else {
765                 /* Otherwise we put it precisely where they want it */
766                 if (AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap)) {
767                         AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
768                         ao2_unlock(instance);
769                         return -1;
770                 }
771         }
772
773         /* Now that we have an id add the extension to here */
774         if (AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id)) {
775                 extmap.extension = AST_RTP_EXTENSION_UNSUPPORTED;
776                 extmap.direction = AST_RTP_EXTENSION_DIRECTION_NONE;
777                 AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
778                 AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap);
779                 ao2_unlock(instance);
780                 return -1;
781         }
782
783         ao2_unlock(instance);
784
785         return 0;
786 }
787
788 /*! \brief Helper function which negotiates two RTP extension directions to get our current direction */
789 static enum ast_rtp_extension_direction rtp_extmap_negotiate_direction(enum ast_rtp_extension_direction ours,
790         enum ast_rtp_extension_direction theirs)
791 {
792         if (theirs == AST_RTP_EXTENSION_DIRECTION_NONE || ours == AST_RTP_EXTENSION_DIRECTION_NONE) {
793                 /* This should not occur but if it does tolerate either side not having this extension
794                  * in use.
795                  */
796                 return AST_RTP_EXTENSION_DIRECTION_NONE;
797         } else if (theirs == AST_RTP_EXTENSION_DIRECTION_INACTIVE) {
798                 /* Inactive is always inactive on our side */
799                 return AST_RTP_EXTENSION_DIRECTION_INACTIVE;
800         } else if (theirs == AST_RTP_EXTENSION_DIRECTION_SENDRECV) {
801                 return ours;
802         } else if (theirs == AST_RTP_EXTENSION_DIRECTION_SENDONLY) {
803                 /* If they are send only then we become recvonly if we are configured as sendrecv or recvonly */
804                 if (ours == AST_RTP_EXTENSION_DIRECTION_SENDRECV || ours == AST_RTP_EXTENSION_DIRECTION_RECVONLY) {
805                         return AST_RTP_EXTENSION_DIRECTION_RECVONLY;
806                 }
807         } else if (theirs == AST_RTP_EXTENSION_DIRECTION_RECVONLY) {
808                 /* If they are recv only then we become sendonly if we are configured as sendrecv or sendonly */
809                 if (ours == AST_RTP_EXTENSION_DIRECTION_SENDRECV || ours == AST_RTP_EXTENSION_DIRECTION_SENDONLY) {
810                         return AST_RTP_EXTENSION_DIRECTION_SENDONLY;
811                 }
812         }
813
814         return AST_RTP_EXTENSION_DIRECTION_NONE;
815 }
816
817 int ast_rtp_instance_extmap_negotiate(struct ast_rtp_instance *instance, int id, enum ast_rtp_extension_direction direction,
818         const char *uri, const char *attributes)
819 {
820         /* 'attributes' is currently unused but exists in the API to ensure it does not need to be altered
821          * in the future in case we need to use it.
822          */
823         int idx;
824         enum ast_rtp_extension extension = AST_RTP_EXTENSION_UNSUPPORTED;
825
826         /* Per the RFC the identifier has to be 1 or above */
827         if (id < 1) {
828                 return -1;
829         }
830
831         /* Convert the provided URI to the internal representation */
832         for (idx = 0; idx < ARRAY_LEN(rtp_extension_uris); ++idx) {
833                 if (!strcasecmp(rtp_extension_uris[idx], uri)) {
834                         extension = idx;
835                         break;
836                 }
837         }
838
839         ao2_lock(instance);
840         /* We only accept the extension if it is enabled */
841         if (extension < AST_VECTOR_SIZE(&instance->extmap_enabled) &&
842                 AST_VECTOR_GET(&instance->extmap_enabled, extension) != AST_RTP_EXTENSION_DIRECTION_NONE) {
843                 struct rtp_extmap extmap = {
844                         .extension = extension,
845                         .direction = rtp_extmap_negotiate_direction(AST_VECTOR_GET(&instance->extmap_enabled, extension), direction),
846                 };
847
848                 /* If the direction negotiation failed then don't accept or use this extension */
849                 if (extmap.direction != AST_RTP_EXTENSION_DIRECTION_NONE) {
850                         if (extension != AST_RTP_EXTENSION_UNSUPPORTED) {
851                                 AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id);
852                         }
853                         AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap);
854                 }
855         }
856         ao2_unlock(instance);
857
858         return 0;
859 }
860
861 void ast_rtp_instance_extmap_clear(struct ast_rtp_instance *instance)
862 {
863         static const struct rtp_extmap extmap_none = {
864                 .extension = AST_RTP_EXTENSION_UNSUPPORTED,
865                 .direction = AST_RTP_EXTENSION_DIRECTION_NONE,
866         };
867         int idx;
868
869         ao2_lock(instance);
870
871         /* Clear both the known unique ids and the negotiated extensions as we are about to have
872          * new results set on us.
873          */
874         for (idx = 0; idx < AST_VECTOR_SIZE(&instance->extmap_unique_ids); ++idx) {
875                 AST_VECTOR_REPLACE(&instance->extmap_unique_ids, idx, extmap_none);
876         }
877
878         for (idx = 0; idx < AST_VECTOR_SIZE(&instance->extmap_negotiated); ++idx) {
879                 AST_VECTOR_REPLACE(&instance->extmap_negotiated, idx, -1);
880         }
881
882         ao2_unlock(instance);
883 }
884
885 int ast_rtp_instance_extmap_get_id(struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
886 {
887         int id = -1;
888
889         ao2_lock(instance);
890         if (extension < AST_VECTOR_SIZE(&instance->extmap_negotiated)) {
891                 id = AST_VECTOR_GET(&instance->extmap_negotiated, extension);
892         }
893         ao2_unlock(instance);
894
895         return id;
896 }
897
898 size_t ast_rtp_instance_extmap_count(struct ast_rtp_instance *instance)
899 {
900         size_t count;
901
902         ao2_lock(instance);
903         count = AST_VECTOR_SIZE(&instance->extmap_unique_ids);
904         ao2_unlock(instance);
905
906         return count;
907 }
908
909 enum ast_rtp_extension ast_rtp_instance_extmap_get_extension(struct ast_rtp_instance *instance, int id)
910 {
911         enum ast_rtp_extension extension = AST_RTP_EXTENSION_UNSUPPORTED;
912
913         ao2_lock(instance);
914
915         /* The local unique identifier starts at '1' so the highest unique identifier
916          * can be the actual size of the vector. We compensate (as it is 0 index based)
917          * by dropping it down to 1 to get the correct information.
918          */
919         if (0 < id && id <= AST_VECTOR_SIZE(&instance->extmap_unique_ids)) {
920                 struct rtp_extmap *extmap = AST_VECTOR_GET_ADDR(&instance->extmap_unique_ids, id - 1);
921
922                 extension = extmap->extension;
923         }
924         ao2_unlock(instance);
925
926         return extension;
927 }
928
929 enum ast_rtp_extension_direction ast_rtp_instance_extmap_get_direction(struct ast_rtp_instance *instance, int id)
930 {
931         enum ast_rtp_extension_direction direction = AST_RTP_EXTENSION_DIRECTION_NONE;
932
933         ao2_lock(instance);
934
935         if (0 < id && id <= AST_VECTOR_SIZE(&instance->extmap_unique_ids)) {
936                 struct rtp_extmap *extmap = AST_VECTOR_GET_ADDR(&instance->extmap_unique_ids, id - 1);
937
938                 direction = extmap->direction;
939         }
940         ao2_unlock(instance);
941
942         return direction;
943 }
944
945 const char *ast_rtp_instance_extmap_get_uri(struct ast_rtp_instance *instance, int id)
946 {
947         enum ast_rtp_extension extension = ast_rtp_instance_extmap_get_extension(instance, id);
948
949         if (extension == AST_RTP_EXTENSION_UNSUPPORTED ||
950                 (unsigned int)extension >= ARRAY_LEN(rtp_extension_uris)) {
951                 return NULL;
952         }
953
954         return rtp_extension_uris[extension];
955 }
956
957 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
958 {
959         int res;
960
961         codecs->framing = 0;
962         ast_rwlock_init(&codecs->codecs_lock);
963         res = AST_VECTOR_INIT(&codecs->payload_mapping_rx, AST_RTP_MAX_PT);
964         res |= AST_VECTOR_INIT(&codecs->payload_mapping_tx, AST_RTP_MAX_PT);
965         if (res) {
966                 AST_VECTOR_FREE(&codecs->payload_mapping_rx);
967                 AST_VECTOR_FREE(&codecs->payload_mapping_tx);
968         }
969
970         return res;
971 }
972
973 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
974 {
975         int idx;
976         struct ast_rtp_payload_type *type;
977
978         for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
979                 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
980                 ao2_t_cleanup(type, "destroying ast_rtp_codec rx mapping");
981         }
982         AST_VECTOR_FREE(&codecs->payload_mapping_rx);
983
984         for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
985                 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
986                 ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
987         }
988         AST_VECTOR_FREE(&codecs->payload_mapping_tx);
989
990         ast_rwlock_destroy(&codecs->codecs_lock);
991 }
992
993 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
994 {
995         ast_rtp_codecs_payloads_destroy(codecs);
996         ast_rtp_codecs_payloads_initialize(codecs);
997
998         if (instance && instance->engine && instance->engine->payload_set) {
999                 int i;
1000
1001                 ao2_lock(instance);
1002                 for (i = 0; i < AST_RTP_MAX_PT; i++) {
1003                         instance->engine->payload_set(instance, i, 0, NULL, 0);
1004                 }
1005                 ao2_unlock(instance);
1006         }
1007 }
1008
1009 /*!
1010  * \internal
1011  * \brief Clear the rx primary mapping flag on all other matching mappings.
1012  * \since 14.0.0
1013  *
1014  * \param codecs Codecs that need rx clearing.
1015  * \param to_match Payload type object to compare against.
1016  *
1017  * \note It is assumed that codecs is write locked before calling.
1018  *
1019  * \return Nothing
1020  */
1021 static void payload_mapping_rx_clear_primary(struct ast_rtp_codecs *codecs, struct ast_rtp_payload_type *to_match)
1022 {
1023         int idx;
1024         struct ast_rtp_payload_type *current;
1025         struct ast_rtp_payload_type *new_type;
1026         struct timeval now;
1027
1028         if (!to_match->primary_mapping) {
1029                 return;
1030         }
1031
1032         now = ast_tvnow();
1033         for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1034                 current = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1035
1036                 if (!current || current == to_match || !current->primary_mapping) {
1037                         continue;
1038                 }
1039                 if (current->asterisk_format && to_match->asterisk_format) {
1040                         if (ast_format_cmp(current->format, to_match->format) == AST_FORMAT_CMP_NOT_EQUAL) {
1041                                 continue;
1042                         }
1043                 } else if (!current->asterisk_format && !to_match->asterisk_format) {
1044                         if (current->rtp_code != to_match->rtp_code) {
1045                                 continue;
1046                         }
1047                 } else {
1048                         continue;
1049                 }
1050
1051                 /* Replace current with non-primary marked version */
1052                 new_type = ast_rtp_engine_alloc_payload_type();
1053                 if (!new_type) {
1054                         continue;
1055                 }
1056                 *new_type = *current;
1057                 new_type->primary_mapping = 0;
1058                 new_type->when_retired = now;
1059                 ao2_bump(new_type->format);
1060                 AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, idx, new_type);
1061                 ao2_ref(current, -1);
1062         }
1063 }
1064
1065 /*!
1066  * \internal
1067  * \brief Put the new_type into the rx payload type mapping.
1068  * \since 14.0.0
1069  *
1070  * \param codecs Codecs structure to put new_type into
1071  * \param payload type position to replace.
1072  * \param new_type RTP payload mapping object to store.
1073  *
1074  * \note It is assumed that codecs is write locked before calling.
1075  *
1076  * \return Nothing
1077  */
1078 static void rtp_codecs_payload_replace_rx(struct ast_rtp_codecs *codecs, int payload, struct ast_rtp_payload_type *new_type)
1079 {
1080         ao2_ref(new_type, +1);
1081         if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1082                 ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_rx, payload),
1083                         "cleaning up rx mapping vector element about to be replaced");
1084         }
1085         if (AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, payload, new_type)) {
1086                 ao2_ref(new_type, -1);
1087                 return;
1088         }
1089
1090         payload_mapping_rx_clear_primary(codecs, new_type);
1091 }
1092
1093 /*!
1094  * \internal
1095  * \brief Copy the rx payload type mapping to the destination.
1096  * \since 14.0.0
1097  *
1098  * \param src The source codecs structure
1099  * \param dest The destination codecs structure that the values from src will be copied to
1100  * \param instance Optionally the instance that the dst codecs structure belongs to
1101  *
1102  * \note It is assumed that src is at least read locked before calling.
1103  * \note It is assumed that dest is write locked before calling.
1104  *
1105  * \return Nothing
1106  */
1107 static void rtp_codecs_payloads_copy_rx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1108 {
1109         int idx;
1110         struct ast_rtp_payload_type *type;
1111
1112         for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_rx); ++idx) {
1113                 type = AST_VECTOR_GET(&src->payload_mapping_rx, idx);
1114                 if (!type) {
1115                         continue;
1116                 }
1117
1118                 ast_debug(2, "Copying rx payload mapping %d (%p) from %p to %p\n",
1119                         idx, type, src, dest);
1120                 rtp_codecs_payload_replace_rx(dest, idx, type);
1121
1122                 if (instance && instance->engine && instance->engine->payload_set) {
1123                         ao2_lock(instance);
1124                         instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1125                         ao2_unlock(instance);
1126                 }
1127         }
1128 }
1129
1130 /*!
1131  * \internal
1132  * \brief Determine if a type of payload is already present in mappings.
1133  * \since 14.0.0
1134  *
1135  * \param codecs Codecs to be checked for mappings.
1136  * \param to_match Payload type object to compare against.
1137  *
1138  * \note It is assumed that codecs is write locked before calling.
1139  *
1140  * \retval 0 not found
1141  * \retval 1 found
1142  */
1143 static int payload_mapping_tx_is_present(const struct ast_rtp_codecs *codecs, const struct ast_rtp_payload_type *to_match)
1144 {
1145         int idx;
1146         struct ast_rtp_payload_type *current;
1147
1148         for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1149                 current = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1150
1151                 if (!current) {
1152                         continue;
1153                 }
1154                 if (current == to_match) {
1155                         /* The exact object is already in the mapping. */
1156                         return 1;
1157                 }
1158                 if (current->asterisk_format && to_match->asterisk_format) {
1159                         if (ast_format_get_codec_id(current->format) != ast_format_get_codec_id(to_match->format)) {
1160                                 continue;
1161                         } else if (current->payload == to_match->payload) {
1162                                 return 0;
1163                         }
1164                 } else if (!current->asterisk_format && !to_match->asterisk_format) {
1165                         if (current->rtp_code != to_match->rtp_code) {
1166                                 continue;
1167                         }
1168                 } else {
1169                         continue;
1170                 }
1171
1172                 return 1;
1173         }
1174
1175         return 0;
1176 }
1177
1178 /*!
1179  * \internal
1180  * \brief Copy the tx payload type mapping to the destination.
1181  * \since 14.0.0
1182  *
1183  * \param src The source codecs structure
1184  * \param dest The destination codecs structure that the values from src will be copied to
1185  * \param instance Optionally the instance that the dst codecs structure belongs to
1186  *
1187  * \note It is assumed that src is at least read locked before calling.
1188  * \note It is assumed that dest is write locked before calling.
1189  *
1190  * \return Nothing
1191  */
1192 static void rtp_codecs_payloads_copy_tx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1193 {
1194         int idx;
1195         struct ast_rtp_payload_type *type;
1196
1197         for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
1198                 type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
1199                 if (!type) {
1200                         continue;
1201                 }
1202
1203                 ast_debug(2, "Copying tx payload mapping %d (%p) from %p to %p\n",
1204                         idx, type, src, dest);
1205                 ao2_ref(type, +1);
1206                 if (idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx)) {
1207                         ao2_t_cleanup(AST_VECTOR_GET(&dest->payload_mapping_tx, idx),
1208                                 "cleaning up tx mapping vector element about to be replaced");
1209                 }
1210                 if (AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, type)) {
1211                         ao2_ref(type, -1);
1212                         continue;
1213                 }
1214
1215                 if (instance && instance->engine && instance->engine->payload_set) {
1216                         ao2_lock(instance);
1217                         instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1218                         ao2_unlock(instance);
1219                 }
1220         }
1221 }
1222
1223 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1224 {
1225         int idx;
1226         struct ast_rtp_payload_type *type;
1227
1228         ast_rwlock_wrlock(&dest->codecs_lock);
1229
1230         /* Deadlock avoidance because of held write lock. */
1231         while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
1232                 ast_rwlock_unlock(&dest->codecs_lock);
1233                 sched_yield();
1234                 ast_rwlock_wrlock(&dest->codecs_lock);
1235         }
1236
1237         /*
1238          * This represents a completely new mapping of what the remote party is
1239          * expecting for payloads, so we clear out the entire tx payload mapping
1240          * vector and replace it.
1241          */
1242         for (idx = 0; idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx); ++idx) {
1243                 type = AST_VECTOR_GET(&dest->payload_mapping_tx, idx);
1244                 ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
1245                 AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, NULL);
1246         }
1247
1248         rtp_codecs_payloads_copy_rx(src, dest, instance);
1249         rtp_codecs_payloads_copy_tx(src, dest, instance);
1250         dest->framing = src->framing;
1251
1252         ast_rwlock_unlock(&src->codecs_lock);
1253         ast_rwlock_unlock(&dest->codecs_lock);
1254 }
1255
1256 void ast_rtp_codecs_payloads_xover(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1257 {
1258         int idx;
1259         struct ast_rtp_payload_type *type;
1260
1261         ast_rwlock_wrlock(&dest->codecs_lock);
1262         if (src != dest) {
1263                 /* Deadlock avoidance because of held write lock. */
1264                 while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
1265                         ast_rwlock_unlock(&dest->codecs_lock);
1266                         sched_yield();
1267                         ast_rwlock_wrlock(&dest->codecs_lock);
1268                 }
1269         }
1270
1271         /* Crossover copy payload type tx mapping to rx mapping. */
1272         for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
1273                 type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
1274                 if (!type) {
1275                         continue;
1276                 }
1277
1278                 /* All tx mapping elements should have the primary flag set. */
1279                 ast_assert(type->primary_mapping);
1280
1281                 ast_debug(2, "Crossover copying tx to rx payload mapping %d (%p) from %p to %p\n",
1282                         idx, type, src, dest);
1283                 rtp_codecs_payload_replace_rx(dest, idx, type);
1284
1285                 if (instance && instance->engine && instance->engine->payload_set) {
1286                         ao2_lock(instance);
1287                         instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1288                         ao2_unlock(instance);
1289                 }
1290         }
1291
1292         dest->framing = src->framing;
1293
1294         if (src != dest) {
1295                 ast_rwlock_unlock(&src->codecs_lock);
1296         }
1297         ast_rwlock_unlock(&dest->codecs_lock);
1298 }
1299
1300 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
1301 {
1302         struct ast_rtp_payload_type *new_type;
1303
1304         if (payload < 0 || payload >= AST_RTP_MAX_PT || payload > AST_RTP_PT_LAST_STATIC) {
1305                 return;
1306         }
1307
1308         ast_rwlock_rdlock(&static_RTP_PT_lock);
1309         new_type = ao2_bump(static_RTP_PT[payload]);
1310         ast_rwlock_unlock(&static_RTP_PT_lock);
1311         if (!new_type) {
1312                 ast_debug(1, "Don't have a default tx payload type %d format for m type on %p\n",
1313                         payload, codecs);
1314                 return;
1315         }
1316
1317         ast_debug(1, "Setting tx payload type %d based on m type on %p\n",
1318                 payload, codecs);
1319
1320         ast_rwlock_wrlock(&codecs->codecs_lock);
1321
1322         if (!payload_mapping_tx_is_present(codecs, new_type)) {
1323                 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1324                         ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload),
1325                                 "cleaning up replaced tx payload type");
1326                 }
1327
1328                 if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, new_type)) {
1329                         ao2_ref(new_type, -1);
1330                 } else if (instance && instance->engine && instance->engine->payload_set) {
1331                         ao2_lock(instance);
1332                         instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
1333                         ao2_unlock(instance);
1334                 }
1335         } else {
1336                 ao2_ref(new_type, -1);
1337         }
1338
1339         ast_rwlock_unlock(&codecs->codecs_lock);
1340 }
1341
1342 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
1343                                  char *mimetype, char *mimesubtype,
1344                                  enum ast_rtp_options options,
1345                                  unsigned int sample_rate)
1346 {
1347         unsigned int idx;
1348         int found = 0;
1349
1350         if (pt < 0 || pt >= AST_RTP_MAX_PT) {
1351                 return -1; /* bogus payload type */
1352         }
1353
1354         ast_rwlock_rdlock(&mime_types_lock);
1355         ast_rwlock_wrlock(&codecs->codecs_lock);
1356
1357         for (idx = 0; idx < mime_types_len; ++idx) {
1358                 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[idx];
1359                 struct ast_rtp_payload_type *new_type;
1360
1361                 if (strcasecmp(mimesubtype, t->subtype)) {
1362                         continue;
1363                 }
1364
1365                 if (strcasecmp(mimetype, t->type)) {
1366                         continue;
1367                 }
1368
1369                 /* if both sample rates have been supplied, and they don't match,
1370                  * then this not a match; if one has not been supplied, then the
1371                  * rates are not compared */
1372                 if (sample_rate && t->sample_rate &&
1373                     (sample_rate != t->sample_rate)) {
1374                         continue;
1375                 }
1376
1377                 found = 1;
1378
1379                 new_type = ast_rtp_engine_alloc_payload_type();
1380                 if (!new_type) {
1381                         continue;
1382                 }
1383
1384                 new_type->asterisk_format = t->payload_type.asterisk_format;
1385                 new_type->rtp_code = t->payload_type.rtp_code;
1386                 new_type->payload = pt;
1387                 new_type->primary_mapping = 1;
1388                 if (t->payload_type.asterisk_format
1389                         && ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL
1390                         && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
1391                         new_type->format = ast_format_g726_aal2;
1392                 } else {
1393                         new_type->format = t->payload_type.format;
1394                 }
1395
1396                 if (new_type->format) {
1397                         /* SDP parsing automatically increases the reference count */
1398                         new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
1399                 }
1400
1401                 if (!payload_mapping_tx_is_present(codecs, new_type)) {
1402                         if (pt < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1403                                 ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, pt),
1404                                         "cleaning up replaced tx payload type");
1405                         }
1406
1407                         if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, pt, new_type)) {
1408                                 ao2_ref(new_type, -1);
1409                         } else if (instance && instance->engine && instance->engine->payload_set) {
1410                                 ao2_lock(instance);
1411                                 instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
1412                                 ao2_unlock(instance);
1413                         }
1414                 } else {
1415                         ao2_ref(new_type, -1);
1416                 }
1417
1418                 break;
1419         }
1420
1421         ast_rwlock_unlock(&codecs->codecs_lock);
1422         ast_rwlock_unlock(&mime_types_lock);
1423
1424         return (found ? 0 : -2);
1425 }
1426
1427 int ast_rtp_codecs_payloads_set_rtpmap_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload, char *mimetype, char *mimesubtype, enum ast_rtp_options options)
1428 {
1429         return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
1430 }
1431
1432 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
1433 {
1434         struct ast_rtp_payload_type *type;
1435
1436         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1437                 return;
1438         }
1439
1440         ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
1441
1442         ast_rwlock_wrlock(&codecs->codecs_lock);
1443
1444         if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1445                 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1446                 ao2_cleanup(type);
1447                 AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, NULL);
1448         }
1449
1450         if (instance && instance->engine && instance->engine->payload_set) {
1451                 ao2_lock(instance);
1452                 instance->engine->payload_set(instance, payload, 0, NULL, 0);
1453                 ao2_unlock(instance);
1454         }
1455
1456         ast_rwlock_unlock(&codecs->codecs_lock);
1457 }
1458
1459 enum ast_media_type ast_rtp_codecs_get_stream_type(struct ast_rtp_codecs *codecs)
1460 {
1461         enum ast_media_type stream_type = AST_MEDIA_TYPE_UNKNOWN;
1462         int payload;
1463         struct ast_rtp_payload_type *type;
1464
1465         ast_rwlock_rdlock(&codecs->codecs_lock);
1466         for (payload = 0; payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++payload) {
1467                 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
1468                 if (type && type->asterisk_format) {
1469                         stream_type = ast_format_get_type(type->format);
1470                         break;
1471                 }
1472         }
1473         ast_rwlock_unlock(&codecs->codecs_lock);
1474
1475         return stream_type;
1476 }
1477
1478 struct ast_rtp_payload_type *ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
1479 {
1480         struct ast_rtp_payload_type *type = NULL;
1481
1482         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1483                 return NULL;
1484         }
1485
1486         ast_rwlock_rdlock(&codecs->codecs_lock);
1487         if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1488                 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
1489                 ao2_bump(type);
1490         }
1491         ast_rwlock_unlock(&codecs->codecs_lock);
1492
1493         if (!type && payload <= AST_RTP_PT_LAST_STATIC) {
1494                 ast_rwlock_rdlock(&static_RTP_PT_lock);
1495                 type = ao2_bump(static_RTP_PT[payload]);
1496                 ast_rwlock_unlock(&static_RTP_PT_lock);
1497         }
1498
1499         return type;
1500 }
1501
1502 int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int payload, struct ast_format *format)
1503 {
1504         struct ast_rtp_payload_type *type;
1505
1506         if (payload < 0 || payload >= AST_RTP_MAX_PT || !format) {
1507                 return -1;
1508         }
1509
1510         type = ast_rtp_engine_alloc_payload_type();
1511         if (!type) {
1512                 return -1;
1513         }
1514         ao2_ref(format, +1);
1515         type->format = format;
1516         type->asterisk_format = 1;
1517         type->payload = payload;
1518         type->primary_mapping = 1;
1519
1520         ast_rwlock_wrlock(&codecs->codecs_lock);
1521         if (!payload_mapping_tx_is_present(codecs, type)) {
1522                 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1523                         ao2_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload));
1524                 }
1525                 if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, type)) {
1526                         ao2_ref(type, -1);
1527                 }
1528         } else {
1529                 ao2_ref(type, -1);
1530         }
1531         ast_rwlock_unlock(&codecs->codecs_lock);
1532
1533         return 0;
1534 }
1535
1536 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
1537 {
1538         struct ast_rtp_payload_type *type;
1539         struct ast_format *format = NULL;
1540
1541         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1542                 return NULL;
1543         }
1544
1545         ast_rwlock_rdlock(&codecs->codecs_lock);
1546         if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1547                 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1548                 if (type && type->asterisk_format) {
1549                         format = ao2_bump(type->format);
1550                 }
1551         }
1552         ast_rwlock_unlock(&codecs->codecs_lock);
1553
1554         return format;
1555 }
1556
1557 void ast_rtp_codecs_set_framing(struct ast_rtp_codecs *codecs, unsigned int framing)
1558 {
1559         if (!framing) {
1560                 return;
1561         }
1562
1563         ast_rwlock_wrlock(&codecs->codecs_lock);
1564         codecs->framing = framing;
1565         ast_rwlock_unlock(&codecs->codecs_lock);
1566 }
1567
1568 unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
1569 {
1570         unsigned int framing;
1571
1572         ast_rwlock_rdlock(&codecs->codecs_lock);
1573         framing = codecs->framing;
1574         ast_rwlock_unlock(&codecs->codecs_lock);
1575
1576         return framing;
1577 }
1578
1579 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
1580 {
1581         int idx;
1582
1583         ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN);
1584         *nonastformats = 0;
1585
1586         ast_rwlock_rdlock(&codecs->codecs_lock);
1587
1588         for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1589                 struct ast_rtp_payload_type *type;
1590
1591                 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1592                 if (!type) {
1593                         continue;
1594                 }
1595
1596                 if (type->asterisk_format) {
1597                         ast_format_cap_append(astformats, type->format, 0);
1598                 } else {
1599                         *nonastformats |= type->rtp_code;
1600                 }
1601         }
1602         if (codecs->framing) {
1603                 ast_format_cap_set_framing(astformats, codecs->framing);
1604         }
1605
1606         ast_rwlock_unlock(&codecs->codecs_lock);
1607 }
1608
1609 /*!
1610  * \internal
1611  * \brief Find the static payload type mapping for the format.
1612  * \since 14.0.0
1613  *
1614  * \param asterisk_format Non-zero if the given Asterisk format is present
1615  * \param format Asterisk format to look for
1616  * \param code The non-Asterisk format code to look for
1617  *
1618  * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
1619  *
1620  * \retval Numerical payload type
1621  * \retval -1 if not found.
1622  */
1623 static int find_static_payload_type(int asterisk_format, const struct ast_format *format, int code)
1624 {
1625         int idx;
1626         int payload = -1;
1627
1628         if (!asterisk_format) {
1629                 for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
1630                         if (static_RTP_PT[idx]
1631                                 && !static_RTP_PT[idx]->asterisk_format
1632                                 && static_RTP_PT[idx]->rtp_code == code) {
1633                                 payload = idx;
1634                                 break;
1635                         }
1636                 }
1637         } else if (format) {
1638                 for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
1639                         if (static_RTP_PT[idx]
1640                                 && static_RTP_PT[idx]->asterisk_format
1641                                 && ast_format_cmp(format, static_RTP_PT[idx]->format)
1642                                         != AST_FORMAT_CMP_NOT_EQUAL) {
1643                                 payload = idx;
1644                                 break;
1645                         }
1646                 }
1647         }
1648
1649         return payload;
1650 }
1651
1652 /*!
1653  * \internal
1654  * \brief Find the first unused payload type in a given range
1655  *
1656  * \param codecs The codec structure to look in
1657  * \param start Starting index
1658  * \param end Ending index
1659  * \param ignore Skip these payloads types
1660  *
1661  * \note The static_RTP_PT_lock object must be locked before calling
1662  *
1663  * \retval Numerical payload type
1664  * \retval -1 if not found.
1665  */
1666 static int find_unused_payload_in_range(const struct ast_rtp_codecs *codecs,
1667                 int start, int end, struct ast_rtp_payload_type *ignore[])
1668 {
1669         int x;
1670
1671         for (x = start; x < end; ++x) {
1672                 struct ast_rtp_payload_type *type;
1673
1674                 if (ignore[x]) {
1675                         continue;
1676                 } else if (!codecs || x >= AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1677                         return x;
1678                 }
1679
1680                 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, x);
1681                 if (!type) {
1682                         return x;
1683                 }
1684         }
1685         return -1;
1686 }
1687
1688 /*!
1689  * \internal
1690  * \brief Find an unused payload type
1691  *
1692  * \param codecs Codecs structure to look in
1693  *
1694  * \note Both static_RTP_PT_lock and codecs (if given) must be at least
1695  *       read locked before calling.
1696  *
1697  * \retval Numerical payload type
1698  * \retval -1 if not found.
1699  */
1700 static int find_unused_payload(const struct ast_rtp_codecs *codecs)
1701 {
1702         int res;
1703
1704         /* find next available dynamic payload slot */
1705         res = find_unused_payload_in_range(
1706                 codecs, AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT, static_RTP_PT);
1707         if (res != -1) {
1708                 return res;
1709         }
1710
1711         if (ast_option_rtpusedynamic) {
1712                 /*
1713                  * We're using default values for some dynamic types. So if an unused
1714                  * slot was not found try again, but this time ignore the default
1715                  * values declared for dynamic types (except for 101 and 121) .
1716                  */
1717                 static struct ast_rtp_payload_type *ignore[AST_RTP_MAX_PT] = {0};
1718
1719                 ignore[101] = static_RTP_PT[101];
1720                 ignore[121] = static_RTP_PT[121];
1721
1722                 res = find_unused_payload_in_range(
1723                         codecs, AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT, ignore);
1724                 if (res != -1) {
1725                         return res;
1726                 }
1727         }
1728
1729         /* http://www.iana.org/assignments/rtp-parameters
1730          * RFC 3551, Section 3: "[...] applications which need to define more
1731          * than 32 dynamic payload types MAY bind codes below 96, in which case
1732          * it is RECOMMENDED that unassigned payload type numbers be used
1733          * first". Updated by RFC 5761, Section 4: "[...] values in the range
1734          * 64-95 MUST NOT be used [to avoid conflicts with RTCP]". Summaries:
1735          * https://tools.ietf.org/html/draft-roach-mmusic-unified-plan#section-3.2.1.2
1736          * https://tools.ietf.org/html/draft-wu-avtcore-dynamic-pt-usage#section-3
1737          */
1738         res = find_unused_payload_in_range(
1739                 codecs, MAX(ast_option_rtpptdynamic, AST_RTP_PT_LAST_STATIC + 1),
1740                 AST_RTP_PT_LAST_REASSIGN, static_RTP_PT);
1741         if (res != -1) {
1742                 return res;
1743         }
1744
1745         /* Yet, reusing mappings below AST_RTP_PT_LAST_STATIC (35) is not supported
1746          * in Asterisk because when Compact Headers are activated, no rtpmap is
1747          * send for those below 35. If you want to use 35 and below
1748          * A) do not use Compact Headers,
1749          * B) remove that code in chan_sip/res_pjsip, or
1750          * C) add a flag that this RTP Payload Type got reassigned dynamically
1751          *    and requires a rtpmap even with Compact Headers enabled.
1752          */
1753         res = find_unused_payload_in_range(
1754                 codecs, MAX(ast_option_rtpptdynamic, 20),
1755                 AST_RTP_PT_LAST_STATIC + 1, static_RTP_PT);
1756         if (res != -1) {
1757                 return res;
1758         }
1759
1760         return find_unused_payload_in_range(
1761                 codecs, MAX(ast_option_rtpptdynamic, 0),
1762                 20, static_RTP_PT);
1763 }
1764
1765 /*!
1766  * \internal
1767  * \brief Find the oldest non-primary dynamic rx payload type.
1768  * \since 14.0.0
1769  *
1770  * \param codecs Codecs structure to look in
1771  *
1772  * \note It is assumed that codecs is at least read locked before calling.
1773  *
1774  * \retval Numerical payload type
1775  * \retval -1 if not found.
1776  */
1777 static int rtp_codecs_find_non_primary_dynamic_rx(struct ast_rtp_codecs *codecs)
1778 {
1779         struct ast_rtp_payload_type *type;
1780         struct timeval oldest;
1781         int idx;
1782         int payload = -1;
1783
1784         idx = AST_RTP_PT_FIRST_DYNAMIC;
1785         for (; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1786                 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1787                 if (type
1788                         && !type->primary_mapping
1789                         && (payload == -1
1790                                 || ast_tvdiff_ms(type->when_retired, oldest) < 0)) {
1791                         oldest = type->when_retired;
1792                         payload = idx;
1793                 }
1794         }
1795         return payload;
1796 }
1797
1798 /*!
1799  * \internal
1800  * \brief Assign a payload type for the rx mapping.
1801  * \since 14.0.0
1802  *
1803  * \param codecs Codecs structure to look in
1804  * \param asterisk_format Non-zero if the given Asterisk format is present
1805  * \param format Asterisk format to look for
1806  * \param code The format to look for
1807  * \param explicit Require the provided code to be explicitly used
1808  *
1809  * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
1810  *
1811  * \retval Numerical payload type
1812  * \retval -1 if could not assign.
1813  */
1814 static int rtp_codecs_assign_payload_code_rx(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code, int explicit)
1815 {
1816         int payload = code;
1817         struct ast_rtp_payload_type *new_type;
1818
1819         if (!explicit) {
1820                 payload = find_static_payload_type(asterisk_format, format, code);
1821
1822                 if (payload < 0 && (!asterisk_format || !ast_option_rtpusedynamic)) {
1823                         return payload;
1824                 }
1825         }
1826
1827         new_type = rtp_payload_type_alloc(format, payload, code, 1);
1828         if (!new_type) {
1829                 return -1;
1830         }
1831
1832         ast_rwlock_wrlock(&codecs->codecs_lock);
1833         if (payload > -1 && (payload < AST_RTP_PT_FIRST_DYNAMIC
1834                 || AST_VECTOR_SIZE(&codecs->payload_mapping_rx) <= payload
1835                 || !AST_VECTOR_GET(&codecs->payload_mapping_rx, payload))) {
1836                 /*
1837                  * The payload type is a static assignment
1838                  * or our default dynamic position is available.
1839                  */
1840                 rtp_codecs_payload_replace_rx(codecs, payload, new_type);
1841         } else if (!explicit && (-1 < (payload = find_unused_payload(codecs))
1842                 || -1 < (payload = rtp_codecs_find_non_primary_dynamic_rx(codecs)))) {
1843                 /*
1844                  * We found the first available empty dynamic position
1845                  * or we found a mapping that should no longer be
1846                  * actively used.
1847                  */
1848                 new_type->payload = payload;
1849                 rtp_codecs_payload_replace_rx(codecs, payload, new_type);
1850         } else if (explicit) {
1851                 /*
1852                 * They explicitly requested this payload number be used but it couldn't be
1853                 */
1854                 payload = -1;
1855         } else {
1856                 /*
1857                  * There are no empty or non-primary dynamic positions
1858                  * left.  Sadness.
1859                  *
1860                  * I don't think this is really possible.
1861                  */
1862                 ast_log(LOG_WARNING, "No dynamic RTP payload type values available "
1863                         "for %s - %d!\n", format ? ast_format_get_name(format) : "", code);
1864         }
1865         ast_rwlock_unlock(&codecs->codecs_lock);
1866
1867         ao2_ref(new_type, -1);
1868
1869         return payload;
1870 }
1871
1872 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code)
1873 {
1874         struct ast_rtp_payload_type *type;
1875         int idx;
1876         int payload = -1;
1877
1878         ast_rwlock_rdlock(&static_RTP_PT_lock);
1879         if (!asterisk_format) {
1880                 ast_rwlock_rdlock(&codecs->codecs_lock);
1881                 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1882                         type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1883                         if (!type) {
1884                                 continue;
1885                         }
1886
1887                         if (!type->asterisk_format
1888                                 && type->primary_mapping
1889                                 && type->rtp_code == code) {
1890                                 payload = idx;
1891                                 break;
1892                         }
1893                 }
1894                 ast_rwlock_unlock(&codecs->codecs_lock);
1895         } else if (format) {
1896                 ast_rwlock_rdlock(&codecs->codecs_lock);
1897                 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1898                         type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1899                         if (!type) {
1900                                 continue;
1901                         }
1902
1903                         if (type->asterisk_format
1904                                 && type->primary_mapping
1905                                 && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
1906                                 payload = idx;
1907                                 break;
1908                         }
1909                 }
1910                 ast_rwlock_unlock(&codecs->codecs_lock);
1911         }
1912
1913         if (payload < 0) {
1914                 payload = rtp_codecs_assign_payload_code_rx(codecs, asterisk_format, format,
1915                         code, 0);
1916         }
1917         ast_rwlock_unlock(&static_RTP_PT_lock);
1918
1919         return payload;
1920 }
1921
1922 int ast_rtp_codecs_payload_set_rx(struct ast_rtp_codecs *codecs, int code, struct ast_format *format)
1923 {
1924         return rtp_codecs_assign_payload_code_rx(codecs, 1, format, code, 1);
1925 }
1926
1927 int ast_rtp_codecs_payload_code_tx(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
1928 {
1929         struct ast_rtp_payload_type *type;
1930         int idx;
1931         int payload = -1;
1932
1933         if (!asterisk_format) {
1934                 ast_rwlock_rdlock(&codecs->codecs_lock);
1935                 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1936                         type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1937                         if (!type) {
1938                                 continue;
1939                         }
1940
1941                         if (!type->asterisk_format
1942                                 && type->rtp_code == code) {
1943                                 payload = idx;
1944                                 break;
1945                         }
1946                 }
1947                 ast_rwlock_unlock(&codecs->codecs_lock);
1948         } else if (format) {
1949                 ast_rwlock_rdlock(&codecs->codecs_lock);
1950                 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1951                         type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1952                         if (!type) {
1953                                 continue;
1954                         }
1955
1956                         if (type->asterisk_format
1957                                 && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
1958                                 payload = idx;
1959                                 break;
1960                         }
1961                 }
1962                 ast_rwlock_unlock(&codecs->codecs_lock);
1963         }
1964
1965         if (payload < 0) {
1966                 ast_rwlock_rdlock(&static_RTP_PT_lock);
1967                 payload = find_static_payload_type(asterisk_format, format, code);
1968                 ast_rwlock_unlock(&static_RTP_PT_lock);
1969         }
1970
1971         return payload;
1972 }
1973
1974 int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int payload)
1975 {
1976         struct ast_rtp_payload_type *type;
1977         int res = -1;
1978
1979         ast_rwlock_rdlock(&codecs->codecs_lock);
1980         if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1981                 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1982                 if (type) {
1983                         res = payload;
1984                 }
1985         }
1986         ast_rwlock_unlock(&codecs->codecs_lock);
1987
1988         return res;
1989 }
1990
1991 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format,
1992         const struct ast_format *format, int code, enum ast_rtp_options options)
1993 {
1994         int i;
1995         const char *res = "";
1996
1997         ast_rwlock_rdlock(&mime_types_lock);
1998         for (i = 0; i < mime_types_len; i++) {
1999                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
2000                         (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
2001                         if ((ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) &&
2002                                         (options & AST_RTP_OPT_G726_NONSTANDARD)) {
2003                                 res = "G726-32";
2004                                 break;
2005                         } else {
2006                                 res = ast_rtp_mime_types[i].subtype;
2007                                 break;
2008                         }
2009                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
2010                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
2011
2012                         res = ast_rtp_mime_types[i].subtype;
2013                         break;
2014                 }
2015         }
2016         ast_rwlock_unlock(&mime_types_lock);
2017
2018         return res;
2019 }
2020
2021 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format,
2022         const struct ast_format *format, int code)
2023 {
2024         unsigned int i;
2025         unsigned int res = 0;
2026
2027         ast_rwlock_rdlock(&mime_types_lock);
2028         for (i = 0; i < mime_types_len; ++i) {
2029                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
2030                         (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
2031                         res = ast_rtp_mime_types[i].sample_rate;
2032                         break;
2033                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
2034                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
2035                         res = ast_rtp_mime_types[i].sample_rate;
2036                         break;
2037                 }
2038         }
2039         ast_rwlock_unlock(&mime_types_lock);
2040
2041         return res;
2042 }
2043
2044 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *ast_format_capability, int rtp_capability, const int asterisk_format, enum ast_rtp_options options)
2045 {
2046         int found = 0;
2047         const char *name;
2048         if (!buf) {
2049                 return NULL;
2050         }
2051
2052
2053         if (asterisk_format) {
2054                 int x;
2055                 struct ast_format *tmp_fmt;
2056                 for (x = 0; x < ast_format_cap_count(ast_format_capability); x++) {
2057                         tmp_fmt = ast_format_cap_get_format(ast_format_capability, x);
2058                         name = ast_rtp_lookup_mime_subtype2(asterisk_format, tmp_fmt, 0, options);
2059                         ao2_ref(tmp_fmt, -1);
2060                         ast_str_append(&buf, 0, "%s|", name);
2061                         found = 1;
2062                 }
2063         } else {
2064                 int x;
2065                 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
2066                 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
2067                         if (rtp_capability & x) {
2068                                 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
2069                                 ast_str_append(&buf, 0, "%s|", name);
2070                                 found = 1;
2071                         }
2072                 }
2073         }
2074
2075         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
2076
2077         return ast_str_buffer(buf);
2078 }
2079
2080 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
2081 {
2082         int res;
2083
2084         if (instance->engine->dtmf_begin) {
2085                 ao2_lock(instance);
2086                 res = instance->engine->dtmf_begin(instance, digit);
2087                 ao2_unlock(instance);
2088         } else {
2089                 res = -1;
2090         }
2091         return res;
2092 }
2093
2094 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
2095 {
2096         int res;
2097
2098         if (instance->engine->dtmf_end) {
2099                 ao2_lock(instance);
2100                 res = instance->engine->dtmf_end(instance, digit);
2101                 ao2_unlock(instance);
2102         } else {
2103                 res = -1;
2104         }
2105         return res;
2106 }
2107
2108 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
2109 {
2110         int res;
2111
2112         if (instance->engine->dtmf_end_with_duration) {
2113                 ao2_lock(instance);
2114                 res = instance->engine->dtmf_end_with_duration(instance, digit, duration);
2115                 ao2_unlock(instance);
2116         } else {
2117                 res = -1;
2118         }
2119         return res;
2120 }
2121
2122 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
2123 {
2124         int res;
2125
2126         if (instance->engine->dtmf_mode_set) {
2127                 ao2_lock(instance);
2128                 res = instance->engine->dtmf_mode_set(instance, dtmf_mode);
2129                 ao2_unlock(instance);
2130         } else {
2131                 res = -1;
2132         }
2133         return res;
2134 }
2135
2136 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
2137 {
2138         int res;
2139
2140         if (instance->engine->dtmf_mode_get) {
2141                 ao2_lock(instance);
2142                 res = instance->engine->dtmf_mode_get(instance);
2143                 ao2_unlock(instance);
2144         } else {
2145                 res = 0;
2146         }
2147         return res;
2148 }
2149
2150 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
2151 {
2152         if (instance->engine->update_source) {
2153                 ao2_lock(instance);
2154                 instance->engine->update_source(instance);
2155                 ao2_unlock(instance);
2156         }
2157 }
2158
2159 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
2160 {
2161         if (instance->engine->change_source) {
2162                 ao2_lock(instance);
2163                 instance->engine->change_source(instance);
2164                 ao2_unlock(instance);
2165         }
2166 }
2167
2168 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
2169 {
2170         int res;
2171
2172         if (instance->engine->qos) {
2173                 ao2_lock(instance);
2174                 res = instance->engine->qos(instance, tos, cos, desc);
2175                 ao2_unlock(instance);
2176         } else {
2177                 res = -1;
2178         }
2179         return res;
2180 }
2181
2182 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
2183 {
2184         if (instance->engine->stop) {
2185                 ao2_lock(instance);
2186                 instance->engine->stop(instance);
2187                 ao2_unlock(instance);
2188         }
2189 }
2190
2191 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
2192 {
2193         int res;
2194
2195         if (instance->engine->fd) {
2196                 ao2_lock(instance);
2197                 res = instance->engine->fd(instance, rtcp);
2198                 ao2_unlock(instance);
2199         } else {
2200                 res = -1;
2201         }
2202         return res;
2203 }
2204
2205 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
2206 {
2207         struct ast_rtp_glue *glue = NULL;
2208
2209         AST_RWLIST_RDLOCK(&glues);
2210
2211         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
2212                 if (!strcasecmp(glue->type, type)) {
2213                         break;
2214                 }
2215         }
2216
2217         AST_RWLIST_UNLOCK(&glues);
2218
2219         return glue;
2220 }
2221
2222 /*!
2223  * \brief Conditionally unref an rtp instance
2224  */
2225 static void unref_instance_cond(struct ast_rtp_instance **instance)
2226 {
2227         if (*instance) {
2228                 ao2_ref(*instance, -1);
2229                 *instance = NULL;
2230         }
2231 }
2232
2233 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
2234 {
2235         struct ast_rtp_instance *bridged;
2236
2237         ao2_lock(instance);
2238         bridged = instance->bridged;
2239         ao2_unlock(instance);
2240         return bridged;
2241 }
2242
2243 void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
2244 {
2245         ao2_lock(instance);
2246         instance->bridged = bridged;
2247         ao2_unlock(instance);
2248 }
2249
2250 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
2251 {
2252         struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
2253                 *vinstance_dst = NULL, *vinstance_src = NULL,
2254                 *tinstance_dst = NULL, *tinstance_src = NULL;
2255         struct ast_rtp_glue *glue_dst, *glue_src;
2256         enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
2257         enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
2258         struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2259         struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2260
2261         /* Lock both channels so we can look for the glue that binds them together */
2262         ast_channel_lock_both(c_dst, c_src);
2263
2264         if (!cap_src || !cap_dst) {
2265                 goto done;
2266         }
2267
2268         /* Grab glue that binds each channel to something using the RTP engine */
2269         if (!(glue_dst = ast_rtp_instance_get_glue(ast_channel_tech(c_dst)->type)) || !(glue_src = ast_rtp_instance_get_glue(ast_channel_tech(c_src)->type))) {
2270                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
2271                 goto done;
2272         }
2273
2274         audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
2275         video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
2276
2277         audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
2278         video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
2279
2280         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
2281         if (video_glue_dst_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE)) {
2282                 audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
2283         }
2284         if (video_glue_src_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE)) {
2285                 audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
2286         }
2287         if (audio_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_dst_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_dst->get_codec) {
2288                 glue_dst->get_codec(c_dst, cap_dst);
2289         }
2290         if (audio_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_src_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_src->get_codec) {
2291                 glue_src->get_codec(c_src, cap_src);
2292         }
2293
2294         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
2295         if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
2296                 goto done;
2297         }
2298
2299         /* Make sure we have matching codecs */
2300         if (!ast_format_cap_iscompatible(cap_dst, cap_src)) {
2301                 goto done;
2302         }
2303
2304         ast_rtp_codecs_payloads_xover(&instance_src->codecs, &instance_dst->codecs, instance_dst);
2305
2306         if (vinstance_dst && vinstance_src) {
2307                 ast_rtp_codecs_payloads_xover(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
2308         }
2309         if (tinstance_dst && tinstance_src) {
2310                 ast_rtp_codecs_payloads_xover(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
2311         }
2312
2313         if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
2314                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
2315                         ast_channel_name(c_dst), ast_channel_name(c_src));
2316         } else {
2317                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
2318                         ast_channel_name(c_dst), ast_channel_name(c_src));
2319         }
2320
2321 done:
2322         ast_channel_unlock(c_dst);
2323         ast_channel_unlock(c_src);
2324
2325         ao2_cleanup(cap_dst);
2326         ao2_cleanup(cap_src);
2327
2328         unref_instance_cond(&instance_dst);
2329         unref_instance_cond(&instance_src);
2330         unref_instance_cond(&vinstance_dst);
2331         unref_instance_cond(&vinstance_src);
2332         unref_instance_cond(&tinstance_dst);
2333         unref_instance_cond(&tinstance_src);
2334 }
2335
2336 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
2337 {
2338         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
2339                         *vinstance0 = NULL, *vinstance1 = NULL,
2340                         *tinstance0 = NULL, *tinstance1 = NULL;
2341         struct ast_rtp_glue *glue0, *glue1;
2342         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
2343         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
2344         struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2345         struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2346
2347         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
2348         if (!c1 || !cap1 || !cap0) {
2349                 ao2_cleanup(cap0);
2350                 ao2_cleanup(cap1);
2351                 return -1;
2352         }
2353
2354         /* Lock both channels so we can look for the glue that binds them together */
2355         ast_channel_lock_both(c0, c1);
2356
2357         /* Grab glue that binds each channel to something using the RTP engine */
2358         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
2359                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
2360                 goto done;
2361         }
2362
2363         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
2364         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
2365
2366         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
2367         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
2368
2369         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
2370         if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
2371                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
2372         }
2373         if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
2374                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
2375         }
2376         if (audio_glue0_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue0_res == AST_RTP_GLUE_RESULT_FORBID || video_glue0_res == AST_RTP_GLUE_RESULT_REMOTE) && glue0->get_codec) {
2377                 glue0->get_codec(c0, cap0);
2378         }
2379         if (audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue1_res == AST_RTP_GLUE_RESULT_FORBID || video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) && glue1->get_codec) {
2380                 glue1->get_codec(c1, cap1);
2381         }
2382
2383         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
2384         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
2385                 goto done;
2386         }
2387
2388         /* Make sure we have matching codecs */
2389         if (!ast_format_cap_iscompatible(cap0, cap1)) {
2390                 goto done;
2391         }
2392
2393         /* Bridge media early */
2394         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
2395                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
2396         }
2397
2398 done:
2399         ast_channel_unlock(c0);
2400         ast_channel_unlock(c1);
2401
2402         ao2_cleanup(cap0);
2403         ao2_cleanup(cap1);
2404
2405         unref_instance_cond(&instance0);
2406         unref_instance_cond(&instance1);
2407         unref_instance_cond(&vinstance0);
2408         unref_instance_cond(&vinstance1);
2409         unref_instance_cond(&tinstance0);
2410         unref_instance_cond(&tinstance1);
2411
2412         ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
2413
2414         return 0;
2415 }
2416
2417 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
2418 {
2419         int res;
2420
2421         if (instance->engine->red_init) {
2422                 ao2_lock(instance);
2423                 res = instance->engine->red_init(instance, buffer_time, payloads, generations);
2424                 ao2_unlock(instance);
2425         } else {
2426                 res = -1;
2427         }
2428         return res;
2429 }
2430
2431 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
2432 {
2433         int res;
2434
2435         if (instance->engine->red_buffer) {
2436                 ao2_lock(instance);
2437                 res = instance->engine->red_buffer(instance, frame);
2438                 ao2_unlock(instance);
2439         } else {
2440                 res = -1;
2441         }
2442         return res;
2443 }
2444
2445 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
2446 {
2447         int res;
2448
2449         if (instance->engine->get_stat) {
2450                 ao2_lock(instance);
2451                 res = instance->engine->get_stat(instance, stats, stat);
2452                 ao2_unlock(instance);
2453         } else {
2454                 res = -1;
2455         }
2456         return res;
2457 }
2458
2459 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
2460 {
2461         struct ast_rtp_instance_stats stats = { 0, };
2462         enum ast_rtp_instance_stat stat;
2463
2464         /* Determine what statistics we will need to retrieve based on field passed in */
2465         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
2466                 stat = AST_RTP_INSTANCE_STAT_ALL;
2467         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
2468                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
2469         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
2470                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
2471         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
2472                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
2473         } else {
2474                 return NULL;
2475         }
2476
2477         /* Attempt to actually retrieve the statistics we need to generate the quality string */
2478         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
2479                 return NULL;
2480         }
2481
2482         /* Now actually fill the buffer with the good information */
2483         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
2484                 snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
2485                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
2486         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
2487                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
2488                          stats.local_minjitter, stats.local_maxjitter, stats.local_normdevjitter, sqrt(stats.local_stdevjitter), stats.remote_minjitter, stats.remote_maxjitter, stats.remote_normdevjitter, sqrt(stats.remote_stdevjitter));
2489         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
2490                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
2491                          stats.local_minrxploss, stats.local_maxrxploss, stats.local_normdevrxploss, sqrt(stats.local_stdevrxploss), stats.remote_minrxploss, stats.remote_maxrxploss, stats.remote_normdevrxploss, sqrt(stats.remote_stdevrxploss));
2492         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
2493                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
2494         }
2495
2496         return buf;
2497 }
2498
2499 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
2500 {
2501         char quality_buf[AST_MAX_USER_FIELD];
2502         char *quality;
2503         struct ast_channel *bridge;
2504
2505         bridge = ast_channel_bridge_peer(chan);
2506         if (bridge) {
2507                 ast_channel_lock_both(chan, bridge);
2508                 ast_channel_stage_snapshot(bridge);
2509         } else {
2510                 ast_channel_lock(chan);
2511         }
2512         ast_channel_stage_snapshot(chan);
2513
2514         quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
2515                 quality_buf, sizeof(quality_buf));
2516         if (quality) {
2517                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
2518                 if (bridge) {
2519                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
2520                 }
2521         }
2522
2523         quality = ast_rtp_instance_get_quality(instance,
2524                 AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
2525         if (quality) {
2526                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
2527                 if (bridge) {
2528                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
2529                 }
2530         }
2531
2532         quality = ast_rtp_instance_get_quality(instance,
2533                 AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
2534         if (quality) {
2535                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
2536                 if (bridge) {
2537                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
2538                 }
2539         }
2540
2541         quality = ast_rtp_instance_get_quality(instance,
2542                 AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
2543         if (quality) {
2544                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
2545                 if (bridge) {
2546                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
2547                 }
2548         }
2549
2550         ast_channel_stage_snapshot_done(chan);
2551         ast_channel_unlock(chan);
2552         if (bridge) {
2553                 ast_channel_stage_snapshot_done(bridge);
2554                 ast_channel_unlock(bridge);
2555                 ast_channel_unref(bridge);
2556         }
2557 }
2558
2559 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
2560 {
2561         int res;
2562
2563         if (instance->engine->set_read_format) {
2564                 ao2_lock(instance);
2565                 res = instance->engine->set_read_format(instance, format);
2566                 ao2_unlock(instance);
2567         } else {
2568                 res = -1;
2569         }
2570         return res;
2571 }
2572
2573 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
2574 {
2575         int res;
2576
2577         if (instance->engine->set_read_format) {
2578                 ao2_lock(instance);
2579                 res = instance->engine->set_write_format(instance, format);
2580                 ao2_unlock(instance);
2581         } else {
2582                 res = -1;
2583         }
2584         return res;
2585 }
2586
2587 /* XXX Nothing calls this */
2588 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
2589 {
2590         struct ast_rtp_glue *glue;
2591         struct ast_rtp_instance *peer_instance = NULL;
2592         int res = -1;
2593
2594         if (!instance->engine->make_compatible) {
2595                 return -1;
2596         }
2597
2598         ast_channel_lock(peer);
2599
2600         if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
2601                 ast_channel_unlock(peer);
2602                 return -1;
2603         }
2604
2605         glue->get_rtp_info(peer, &peer_instance);
2606         if (!peer_instance) {
2607                 ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
2608                 ast_channel_unlock(peer);
2609                 return -1;
2610         }
2611         if (peer_instance->engine != instance->engine) {
2612                 ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
2613                 ast_channel_unlock(peer);
2614                 ao2_ref(peer_instance, -1);
2615                 return -1;
2616         }
2617
2618         /*
2619          * XXX Good thing nothing calls this function because we would need
2620          * deadlock avoidance to get the two instance locks.
2621          */
2622         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
2623
2624         ast_channel_unlock(peer);
2625
2626         ao2_ref(peer_instance, -1);
2627         peer_instance = NULL;
2628
2629         return res;
2630 }
2631
2632 void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
2633 {
2634         if (instance->engine->available_formats) {
2635                 ao2_lock(instance);
2636                 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
2637                 ao2_unlock(instance);
2638                 if (ast_format_cap_count(result)) {
2639                         return;
2640                 }
2641         }
2642
2643         ast_translate_available_formats(to_endpoint, to_asterisk, result);
2644 }
2645
2646 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
2647 {
2648         int res;
2649
2650         if (instance->engine->activate) {
2651                 ao2_lock(instance);
2652                 res = instance->engine->activate(instance);
2653                 ao2_unlock(instance);
2654         } else {
2655                 res = 0;
2656         }
2657         return res;
2658 }
2659
2660 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
2661                                    struct ast_sockaddr *suggestion,
2662                                    const char *username)
2663 {
2664         if (instance->engine->stun_request) {
2665                 instance->engine->stun_request(instance, suggestion, username);
2666         }
2667 }
2668
2669 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
2670 {
2671         instance->timeout = timeout;
2672 }
2673
2674 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
2675 {
2676         instance->holdtimeout = timeout;
2677 }
2678
2679 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
2680 {
2681         instance->keepalive = interval;
2682 }
2683
2684 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
2685 {
2686         return instance->timeout;
2687 }
2688
2689 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
2690 {
2691         return instance->holdtimeout;
2692 }
2693
2694 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
2695 {
2696         return instance->keepalive;
2697 }
2698
2699 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
2700 {
2701         return instance->engine;
2702 }
2703
2704 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
2705 {
2706         return instance->glue;
2707 }
2708
2709 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
2710 {
2711         if (res_srtp || res_srtp_policy) {
2712                 return -1;
2713         }
2714         if (!srtp_res || !policy_res) {
2715                 return -1;
2716         }
2717
2718         res_srtp = srtp_res;
2719         res_srtp_policy = policy_res;
2720
2721         return 0;
2722 }
2723
2724 void ast_rtp_engine_unregister_srtp(void)
2725 {
2726         res_srtp = NULL;
2727         res_srtp_policy = NULL;
2728 }
2729
2730 int ast_rtp_engine_srtp_is_registered(void)
2731 {
2732         return res_srtp && res_srtp_policy;
2733 }
2734
2735 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
2736 {
2737         int res = 0;
2738         struct ast_srtp **srtp;
2739
2740         if (!res_srtp) {
2741                 return -1;
2742         }
2743
2744
2745         srtp = rtcp ? &instance->rtcp_srtp : &instance->srtp;
2746
2747         if (!*srtp) {
2748                 res = res_srtp->create(srtp, instance, remote_policy);
2749         } else if (remote_policy) {
2750                 res = res_srtp->replace(srtp, instance, remote_policy);
2751         }
2752         if (!res) {
2753                 res = res_srtp->add_stream(*srtp, local_policy);
2754         }
2755
2756         return res;
2757 }
2758
2759 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
2760 {
2761         if (rtcp && instance->rtcp_srtp) {
2762                 return instance->rtcp_srtp;
2763         } else {
2764                 return instance->srtp;
2765         }
2766 }
2767
2768 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
2769 {
2770         int res;
2771
2772         if (instance->engine->sendcng) {
2773                 ao2_lock(instance);
2774                 res = instance->engine->sendcng(instance, level);
2775                 ao2_unlock(instance);
2776         } else {
2777                 res = -1;
2778         }
2779         return res;
2780 }
2781
2782 static void rtp_ice_wrap_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
2783 {
2784         ao2_lock(instance);
2785         instance->engine->ice->set_authentication(instance, ufrag, password);
2786         ao2_unlock(instance);
2787 }
2788
2789 static void rtp_ice_wrap_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
2790 {
2791         ao2_lock(instance);
2792         instance->engine->ice->add_remote_candidate(instance, candidate);
2793         ao2_unlock(instance);
2794 }
2795
2796 static void rtp_ice_wrap_start(struct ast_rtp_instance *instance)
2797 {
2798         ao2_lock(instance);
2799         instance->engine->ice->start(instance);
2800         ao2_unlock(instance);
2801 }
2802
2803 static void rtp_ice_wrap_stop(struct ast_rtp_instance *instance)
2804 {
2805         ao2_lock(instance);
2806         instance->engine->ice->stop(instance);
2807         ao2_unlock(instance);
2808 }
2809
2810 static const char *rtp_ice_wrap_get_ufrag(struct ast_rtp_instance *instance)
2811 {
2812         const char *ufrag;
2813
2814         ao2_lock(instance);
2815         ufrag = instance->engine->ice->get_ufrag(instance);
2816         ao2_unlock(instance);
2817         return ufrag;
2818 }
2819
2820 static const char *rtp_ice_wrap_get_password(struct ast_rtp_instance *instance)
2821 {
2822         const char *password;
2823
2824         ao2_lock(instance);
2825         password = instance->engine->ice->get_password(instance);
2826         ao2_unlock(instance);
2827         return password;
2828 }
2829
2830 static struct ao2_container *rtp_ice_wrap_get_local_candidates(struct ast_rtp_instance *instance)
2831 {
2832         struct ao2_container *local_candidates;
2833
2834         ao2_lock(instance);
2835         local_candidates = instance->engine->ice->get_local_candidates(instance);
2836         ao2_unlock(instance);
2837         return local_candidates;
2838 }
2839
2840 static void rtp_ice_wrap_ice_lite(struct ast_rtp_instance *instance)
2841 {
2842         ao2_lock(instance);
2843         instance->engine->ice->ice_lite(instance);
2844         ao2_unlock(instance);
2845 }
2846
2847 static void rtp_ice_wrap_set_role(struct ast_rtp_instance *instance,
2848         enum ast_rtp_ice_role role)
2849 {
2850         ao2_lock(instance);
2851         instance->engine->ice->set_role(instance, role);
2852         ao2_unlock(instance);
2853 }
2854
2855 static void rtp_ice_wrap_turn_request(struct ast_rtp_instance *instance,
2856         enum ast_rtp_ice_component_type component, enum ast_transport transport,
2857         const char *server, unsigned int port, const char *username, const char *password)
2858 {
2859         ao2_lock(instance);
2860         instance->engine->ice->turn_request(instance, component, transport, server, port,
2861                 username, password);
2862         ao2_unlock(instance);
2863 }
2864
2865 static void rtp_ice_wrap_change_components(struct ast_rtp_instance *instance,
2866         int num_components)
2867 {
2868         ao2_lock(instance);
2869         instance->engine->ice->change_components(instance, num_components);
2870         ao2_unlock(instance);
2871 }
2872
2873 static struct ast_rtp_engine_ice rtp_ice_wrappers = {
2874         .set_authentication = rtp_ice_wrap_set_authentication,
2875         .add_remote_candidate = rtp_ice_wrap_add_remote_candidate,
2876         .start = rtp_ice_wrap_start,
2877         .stop = rtp_ice_wrap_stop,
2878         .get_ufrag = rtp_ice_wrap_get_ufrag,
2879         .get_password = rtp_ice_wrap_get_password,
2880         .get_local_candidates = rtp_ice_wrap_get_local_candidates,
2881         .ice_lite = rtp_ice_wrap_ice_lite,
2882         .set_role = rtp_ice_wrap_set_role,
2883         .turn_request = rtp_ice_wrap_turn_request,
2884         .change_components = rtp_ice_wrap_change_components,
2885 };
2886
2887 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
2888 {
2889         if (instance->engine->ice) {
2890                 return &rtp_ice_wrappers;
2891         }
2892         /* ICE not available */
2893         return NULL;
2894 }
2895
2896 static int rtp_dtls_wrap_set_configuration(struct ast_rtp_instance *instance,
2897         const struct ast_rtp_dtls_cfg *dtls_cfg)
2898 {
2899         int set_configuration;
2900
2901         ao2_lock(instance);
2902         set_configuration = instance->engine->dtls->set_configuration(instance, dtls_cfg);
2903         ao2_unlock(instance);
2904         return set_configuration;
2905 }
2906
2907 static int rtp_dtls_wrap_active(struct ast_rtp_instance *instance)
2908 {
2909         int active;
2910
2911         ao2_lock(instance);
2912         active = instance->engine->dtls->active(instance);
2913         ao2_unlock(instance);
2914         return active;
2915 }
2916
2917 static void rtp_dtls_wrap_stop(struct ast_rtp_instance *instance)
2918 {
2919         ao2_lock(instance);
2920         instance->engine->dtls->stop(instance);
2921         ao2_unlock(instance);
2922 }
2923
2924 static void rtp_dtls_wrap_reset(struct ast_rtp_instance *instance)
2925 {
2926         ao2_lock(instance);
2927         instance->engine->dtls->reset(instance);
2928         ao2_unlock(instance);
2929 }
2930
2931 static enum ast_rtp_dtls_connection rtp_dtls_wrap_get_connection(struct ast_rtp_instance *instance)
2932 {
2933         enum ast_rtp_dtls_connection get_connection;
2934
2935         ao2_lock(instance);
2936         get_connection = instance->engine->dtls->get_connection(instance);
2937         ao2_unlock(instance);
2938         return get_connection;
2939 }
2940
2941 static enum ast_rtp_dtls_setup rtp_dtls_wrap_get_setup(struct ast_rtp_instance *instance)
2942 {
2943         enum ast_rtp_dtls_setup get_setup;
2944
2945         ao2_lock(instance);
2946         get_setup = instance->engine->dtls->get_setup(instance);
2947         ao2_unlock(instance);
2948         return get_setup;
2949 }
2950
2951 static void rtp_dtls_wrap_set_setup(struct ast_rtp_instance *instance,
2952         enum ast_rtp_dtls_setup setup)
2953 {
2954         ao2_lock(instance);
2955         instance->engine->dtls->set_setup(instance, setup);
2956         ao2_unlock(instance);
2957 }
2958
2959 static void rtp_dtls_wrap_set_fingerprint(struct ast_rtp_instance *instance,
2960         enum ast_rtp_dtls_hash hash, const char *fingerprint)
2961 {
2962         ao2_lock(instance);
2963         instance->engine->dtls->set_fingerprint(instance, hash, fingerprint);
2964         ao2_unlock(instance);
2965 }
2966
2967 static enum ast_rtp_dtls_hash rtp_dtls_wrap_get_fingerprint_hash(struct ast_rtp_instance *instance)
2968 {
2969         enum ast_rtp_dtls_hash get_fingerprint_hash;
2970
2971         ao2_lock(instance);
2972         get_fingerprint_hash = instance->engine->dtls->get_fingerprint_hash(instance);
2973         ao2_unlock(instance);
2974         return get_fingerprint_hash;
2975 }
2976
2977 static const char *rtp_dtls_wrap_get_fingerprint(struct ast_rtp_instance *instance)
2978 {
2979         const char *get_fingerprint;
2980
2981         ao2_lock(instance);
2982         get_fingerprint = instance->engine->dtls->get_fingerprint(instance);
2983         ao2_unlock(instance);
2984         return get_fingerprint;
2985 }
2986
2987 static struct ast_rtp_engine_dtls rtp_dtls_wrappers = {
2988         .set_configuration = rtp_dtls_wrap_set_configuration,
2989         .active = rtp_dtls_wrap_active,
2990         .stop = rtp_dtls_wrap_stop,
2991         .reset = rtp_dtls_wrap_reset,
2992         .get_connection = rtp_dtls_wrap_get_connection,
2993         .get_setup = rtp_dtls_wrap_get_setup,
2994         .set_setup = rtp_dtls_wrap_set_setup,
2995         .set_fingerprint = rtp_dtls_wrap_set_fingerprint,
2996         .get_fingerprint_hash = rtp_dtls_wrap_get_fingerprint_hash,
2997         .get_fingerprint = rtp_dtls_wrap_get_fingerprint,
2998 };
2999
3000 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
3001 {
3002         if (instance->engine->dtls) {
3003                 return &rtp_dtls_wrappers;
3004         }
3005         /* DTLS not available */
3006         return NULL;
3007 }
3008
3009 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
3010 {
3011         if (!strcasecmp(name, "dtlsenable")) {
3012                 dtls_cfg->enabled = ast_true(value) ? 1 : 0;
3013         } else if (!strcasecmp(name, "dtlsverify")) {
3014                 if (!strcasecmp(value, "yes")) {
3015                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
3016                 } else if (!strcasecmp(value, "fingerprint")) {
3017                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
3018                 } else if (!strcasecmp(value, "certificate")) {
3019                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
3020                 } else if (!strcasecmp(value, "no")) {
3021                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
3022                 } else {
3023                         return -1;
3024                 }
3025         } else if (!strcasecmp(name, "dtlsrekey")) {
3026                 if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
3027                         return -1;
3028                 }
3029         } else if (!strcasecmp(name, "dtlsautogeneratecert")) {
3030                 dtls_cfg->ephemeral_cert = ast_true(value) ? 1 : 0;
3031         } else if (!strcasecmp(name, "dtlscertfile")) {
3032                 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3033                         ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3034                         return -1;
3035                 }
3036                 ast_free(dtls_cfg->certfile);
3037                 dtls_cfg->certfile = ast_strdup(value);
3038         } else if (!strcasecmp(name, "dtlsprivatekey")) {
3039                 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3040                         ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3041                         return -1;
3042                 }
3043                 ast_free(dtls_cfg->pvtfile);
3044                 dtls_cfg->pvtfile = ast_strdup(value);
3045         } else if (!strcasecmp(name, "dtlscipher")) {
3046                 ast_free(dtls_cfg->cipher);
3047                 dtls_cfg->cipher = ast_strdup(value);
3048         } else if (!strcasecmp(name, "dtlscafile")) {
3049                 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3050                         ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3051                         return -1;
3052                 }
3053                 ast_free(dtls_cfg->cafile);
3054                 dtls_cfg->cafile = ast_strdup(value);
3055         } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
3056                 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3057                         ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3058                         return -1;
3059                 }
3060                 ast_free(dtls_cfg->capath);
3061                 dtls_cfg->capath = ast_strdup(value);
3062         } else if (!strcasecmp(name, "dtlssetup")) {
3063                 if (!strcasecmp(value, "active")) {
3064                         dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
3065                 } else if (!strcasecmp(value, "passive")) {
3066                         dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
3067                 } else if (!strcasecmp(value, "actpass")) {
3068                         dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
3069                 }
3070         } else if (!strcasecmp(name, "dtlsfingerprint")) {
3071                 if (!strcasecmp(value, "sha-256")) {
3072                         dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
3073                 } else if (!strcasecmp(value, "sha-1")) {
3074                         dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
3075                 }
3076         } else {
3077                 return -1;
3078         }
3079
3080         return 0;
3081 }
3082
3083 int ast_rtp_dtls_cfg_validate(struct ast_rtp_dtls_cfg *dtls_cfg)
3084 {
3085         if (dtls_cfg->ephemeral_cert) {
3086                 if (!ast_strlen_zero(dtls_cfg->certfile)) {
3087                         ast_log(LOG_ERROR, "You cannot request automatically generated certificates"
3088                                 " (dtls_auto_generate_cert) and also specify a certificate file"
3089                                 " (dtls_cert_file) at the same time\n");
3090                         return -1;
3091                 } else if (!ast_strlen_zero(dtls_cfg->pvtfile)
3092                                   || !ast_strlen_zero(dtls_cfg->cafile)
3093                                   || !ast_strlen_zero(dtls_cfg->capath)) {
3094                         ast_log(LOG_NOTICE, "dtls_pvt_file, dtls_cafile, and dtls_ca_path are"
3095                                 " ignored when dtls_auto_generate_cert is enabled\n");
3096                 }
3097         }
3098
3099         return 0;
3100 }
3101
3102 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
3103 {
3104         ast_rtp_dtls_cfg_free(dst_cfg);         /* Prevent a double-call leaking memory via ast_strdup */
3105
3106         dst_cfg->enabled = src_cfg->enabled;
3107         dst_cfg->verify = src_cfg->verify;
3108         dst_cfg->rekey = src_cfg->rekey;
3109         dst_cfg->suite = src_cfg->suite;
3110         dst_cfg->hash = src_cfg->hash;
3111         dst_cfg->ephemeral_cert = src_cfg->ephemeral_cert;
3112         dst_cfg->certfile = ast_strdup(src_cfg->certfile);
3113         dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
3114         dst_cfg->cipher = ast_strdup(src_cfg->cipher);
3115         dst_cfg->cafile = ast_strdup(src_cfg->cafile);
3116         dst_cfg->capath = ast_strdup(src_cfg->capath);
3117         dst_cfg->default_setup = src_cfg->default_setup;
3118 }
3119
3120 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
3121 {
3122         ast_free(dtls_cfg->certfile);
3123         dtls_cfg->certfile = NULL;
3124         ast_free(dtls_cfg->pvtfile);
3125         dtls_cfg->pvtfile = NULL;
3126         ast_free(dtls_cfg->cipher);
3127         dtls_cfg->cipher = NULL;
3128         ast_free(dtls_cfg->cafile);
3129         dtls_cfg->cafile = NULL;
3130         ast_free(dtls_cfg->capath);
3131         dtls_cfg->capath = NULL;
3132 }
3133
3134 /*! \internal
3135  * \brief Small helper routine that cleans up entry i in
3136  * \c ast_rtp_mime_types.
3137  */
3138 static void rtp_engine_mime_type_cleanup(int i)
3139 {
3140         ao2_cleanup(ast_rtp_mime_types[i].payload_type.format);
3141         memset(&ast_rtp_mime_types[i], 0, sizeof(struct ast_rtp_mime_type));
3142 }
3143
3144 static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
3145 {
3146         int x;
3147
3148         ast_rwlock_wrlock(&mime_types_lock);
3149
3150         x = mime_types_len;
3151         if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
3152                 ast_rwlock_unlock(&mime_types_lock);
3153                 return;
3154         }
3155
3156         /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
3157         memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));
3158         if (format) {
3159                 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
3160                 ast_rtp_mime_types[x].payload_type.format = ao2_bump(format);
3161         } else {
3162                 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
3163         }
3164         ast_copy_string(ast_rtp_mime_types[x].type, type, sizeof(ast_rtp_mime_types[x].type));
3165         ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
3166         ast_rtp_mime_types[x].sample_rate = sample_rate;
3167         mime_types_len++;
3168
3169         ast_rwlock_unlock(&mime_types_lock);
3170 }
3171
3172 static void add_static_payload(int payload, struct ast_format *format, int rtp_code)
3173 {
3174         struct ast_rtp_payload_type *type;
3175
3176         /*
3177          * ARRAY_LEN's result is cast to an int so 'map' is not autocast to a size_t,
3178          * which if negative would cause an assertion.
3179          */
3180         ast_assert(payload < (int)ARRAY_LEN(static_RTP_PT));
3181
3182         if (ast_option_rtpusedynamic && payload < 0) {
3183                 /*
3184                  * We're going to build dynamic payloads dynamically. An RTP code is
3185                  * required otherwise one will be dynamically allocated per instance.
3186                  */
3187                 return;
3188         }
3189
3190         /*
3191          * Either the given payload is truly a static type, or Asterisk is
3192          * globally storing the dynamic payloads in the static_RTP_PT object.
3193          */
3194         ast_rwlock_wrlock(&static_RTP_PT_lock);
3195
3196         if (payload < 0) {
3197                 /*
3198                  * This is a dynamic payload that will be stored globally,
3199                  * so find the next available empty slot.
3200                  */
3201                 payload = find_unused_payload(NULL);
3202                 if (payload < 0) {
3203                         ast_log(LOG_WARNING, "No dynamic RTP payload type values available "
3204                                 "for %s - %d!\n", format ? ast_format_get_name(format) : "", rtp_code);
3205                         ast_rwlock_unlock(&static_RTP_PT_lock);
3206                         return;
3207                 }
3208         }
3209
3210         type = rtp_payload_type_alloc(format, payload, rtp_code, 1);
3211         if (type) {
3212                 ao2_cleanup(static_RTP_PT[payload]);
3213                 static_RTP_PT[payload] = type;
3214         }
3215         ast_rwlock_unlock(&static_RTP_PT_lock);
3216 }
3217
3218 int ast_rtp_engine_load_format(struct ast_format *format)
3219 {
3220         char *codec_name = ast_strdupa(ast_format_get_codec_name(format));
3221
3222         codec_name = ast_str_to_upper(codec_name);
3223
3224         set_next_mime_type(format,
3225                 0,
3226                 ast_codec_media_type2str(ast_format_get_type(format)),
3227                 codec_name,
3228                 ast_format_get_sample_rate(format));
3229         add_static_payload(-1, format, 0);
3230
3231         return 0;
3232 }
3233
3234 int ast_rtp_engine_unload_format(struct ast_format *format)
3235 {
3236         int x;
3237         int y = 0;
3238
3239         ast_rwlock_wrlock(&static_RTP_PT_lock);
3240         /* remove everything pertaining to this format id from the lists */
3241         for (x = 0; x < AST_RTP_MAX_PT; x++) {
3242                 if (static_RTP_PT[x]
3243                         && ast_format_cmp(static_RTP_PT[x]->format, format) == AST_FORMAT_CMP_EQUAL) {
3244                         ao2_ref(static_RTP_PT[x], -1);
3245                         static_RTP_PT[x] = NULL;
3246                 }
3247         }
3248         ast_rwlock_unlock(&static_RTP_PT_lock);
3249
3250         ast_rwlock_wrlock(&mime_types_lock);
3251         /* rebuild the list skipping the items matching this id */
3252         for (x = 0; x < mime_types_len; x++) {
3253                 if (ast_format_cmp(ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
3254                         rtp_engine_mime_type_cleanup(x);
3255                         continue;
3256                 }
3257                 if (x != y) {
3258                         ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
3259                 }
3260                 y++;
3261         }
3262         mime_types_len = y;
3263         ast_rwlock_unlock(&mime_types_lock);
3264         return 0;
3265 }
3266
3267 /*!
3268  * \internal
3269  * \brief \ref stasis message payload for RTCP messages
3270  */
3271 struct rtcp_message_payload {
3272         struct ast_channel_snapshot *snapshot;  /*< The channel snapshot, if available */
3273         struct ast_rtp_rtcp_report *report;     /*< The RTCP report */
3274         struct ast_json *blob;                  /*< Extra JSON data to publish */
3275 };
3276
3277 static void rtcp_message_payload_dtor(void *obj)
3278 {
3279         struct rtcp_message_payload *payload = obj;
3280
3281         ao2_cleanup(payload->report);
3282         ao2_cleanup(payload->snapshot);
3283         ast_json_unref(payload->blob);
3284 }
3285
3286 static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
3287 {
3288         struct rtcp_message_payload *payload = stasis_message_data(msg);
3289         RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
3290         RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
3291         unsigned int ssrc = payload->report->ssrc;
3292         unsigned int type = payload->report->type;
3293         unsigned int report_count = payload->report->reception_report_count;
3294         int i;
3295