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