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