rtp_engine.c: Must protect mime_types_len with mime_types_lock.
[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                 </managerEventInstance>
106         </managerEvent>
107         <managerEvent language="en_US" name="RTCPReceived">
108                 <managerEventInstance class="EVENT_FLAG_REPORTING">
109                         <synopsis>Raised when an RTCP packet is received.</synopsis>
110                         <syntax>
111                                 <channel_snapshot/>
112                                 <parameter name="SSRC">
113                                         <para>The SSRC identifier for the remote system</para>
114                                 </parameter>
115                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
116                                 <parameter name="From">
117                                         <para>The address the report was received from.</para>
118                                 </parameter>
119                                 <parameter name="RTT">
120                                         <para>Calculated Round-Trip Time in seconds</para>
121                                 </parameter>
122                                 <parameter name="ReportCount">
123                                         <para>The number of reports that were received.</para>
124                                         <para>The report count determines the number of ReportX headers in
125                                         the message. The X for each set of report headers will range from 0 to
126                                         <literal>ReportCount - 1</literal>.</para>
127                                 </parameter>
128                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
129                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
130                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
131                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
132                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
133                         </syntax>
134                 </managerEventInstance>
135         </managerEvent>
136  ***/
137
138 #include "asterisk.h"
139
140 ASTERISK_REGISTER_FILE()
141
142 #include <math.h>
143
144 #include "asterisk/channel.h"
145 #include "asterisk/frame.h"
146 #include "asterisk/module.h"
147 #include "asterisk/rtp_engine.h"
148 #include "asterisk/manager.h"
149 #include "asterisk/options.h"
150 #include "asterisk/astobj2.h"
151 #include "asterisk/pbx.h"
152 #include "asterisk/translate.h"
153 #include "asterisk/netsock2.h"
154 #include "asterisk/_private.h"
155 #include "asterisk/framehook.h"
156 #include "asterisk/stasis.h"
157 #include "asterisk/json.h"
158 #include "asterisk/stasis_channels.h"
159
160 struct ast_srtp_res *res_srtp = NULL;
161 struct ast_srtp_policy_res *res_srtp_policy = NULL;
162
163 /*! Structure that represents an RTP session (instance) */
164 struct ast_rtp_instance {
165         /*! Engine that is handling this RTP instance */
166         struct ast_rtp_engine *engine;
167         /*! Data unique to the RTP engine */
168         void *data;
169         /*! RTP properties that have been set and their value */
170         int properties[AST_RTP_PROPERTY_MAX];
171         /*! Address that we are expecting RTP to come in to */
172         struct ast_sockaddr local_address;
173         /*! The original source address */
174         struct ast_sockaddr requested_target_address;
175         /*! Address that we are sending RTP to */
176         struct ast_sockaddr incoming_source_address;
177         /*! Instance that we are bridged to if doing remote or local bridging */
178         struct ast_rtp_instance *bridged;
179         /*! Payload and packetization information */
180         struct ast_rtp_codecs codecs;
181         /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
182         int timeout;
183         /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
184         int holdtimeout;
185         /*! RTP keepalive interval */
186         int keepalive;
187         /*! Glue currently in use */
188         struct ast_rtp_glue *glue;
189         /*! SRTP info associated with the instance */
190         struct ast_srtp *srtp;
191         /*! Channel unique ID */
192         char channel_uniqueid[AST_MAX_UNIQUEID];
193         /*! Time of last packet sent */
194         time_t last_tx;
195         /*! Time of last packet received */
196         time_t last_rx;
197 };
198
199 /*! List of RTP engines that are currently registered */
200 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
201
202 /*! List of RTP glues */
203 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
204
205 #define MAX_RTP_MIME_TYPES 128
206
207 /*! The following array defines the MIME Media type (and subtype) for each
208    of our codecs, or RTP-specific data type. */
209 static struct ast_rtp_mime_type {
210         /*! \brief A mapping object between the Asterisk codec and this RTP payload */
211         struct ast_rtp_payload_type payload_type;
212         /*! \brief The media type */
213         char type[16];
214         /*! \brief The format type */
215         char subtype[64];
216         /*! \brief Expected sample rate of the /c subtype */
217         unsigned int sample_rate;
218 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
219 static ast_rwlock_t mime_types_lock;
220 static int mime_types_len = 0;
221
222 /*!
223  * \brief Mapping between Asterisk codecs and rtp payload types
224  *
225  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
226  * also, our own choices for dynamic payload types.  This is our master
227  * table for transmission
228  *
229  * See http://www.iana.org/assignments/rtp-parameters for a list of
230  * assigned values
231  */
232 static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
233 static ast_rwlock_t static_RTP_PT_lock;
234
235 /*! \brief \ref stasis topic for RTP related messages */
236 static struct stasis_topic *rtp_topic;
237
238
239 /*!
240  * \internal
241  * \brief Destructor for \c ast_rtp_payload_type
242  */
243 static void rtp_payload_type_dtor(void *obj)
244 {
245         struct ast_rtp_payload_type *payload = obj;
246
247         ao2_cleanup(payload->format);
248 }
249
250 struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
251 {
252         struct ast_rtp_payload_type *payload;
253
254         payload = ao2_alloc_options(sizeof(*payload), rtp_payload_type_dtor,
255                 AO2_ALLOC_OPT_LOCK_NOLOCK);
256
257         return payload;
258 }
259
260 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
261 {
262         struct ast_rtp_engine *current_engine;
263
264         /* Perform a sanity check on the engine structure to make sure it has the basics */
265         if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
266                 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
267                 return -1;
268         }
269
270         /* Link owner module to the RTP engine for reference counting purposes */
271         engine->mod = module;
272
273         AST_RWLIST_WRLOCK(&engines);
274
275         /* Ensure that no two modules with the same name are registered at the same time */
276         AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
277                 if (!strcmp(current_engine->name, engine->name)) {
278                         ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
279                         AST_RWLIST_UNLOCK(&engines);
280                         return -1;
281                 }
282         }
283
284         /* The engine survived our critique. Off to the list it goes to be used */
285         AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
286
287         AST_RWLIST_UNLOCK(&engines);
288
289         ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
290
291         return 0;
292 }
293
294 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
295 {
296         struct ast_rtp_engine *current_engine = NULL;
297
298         AST_RWLIST_WRLOCK(&engines);
299
300         if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
301                 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
302         }
303
304         AST_RWLIST_UNLOCK(&engines);
305
306         return current_engine ? 0 : -1;
307 }
308
309 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
310 {
311         struct ast_rtp_glue *current_glue = NULL;
312
313         if (ast_strlen_zero(glue->type)) {
314                 return -1;
315         }
316
317         glue->mod = module;
318
319         AST_RWLIST_WRLOCK(&glues);
320
321         AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
322                 if (!strcasecmp(current_glue->type, glue->type)) {
323                         ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
324                         AST_RWLIST_UNLOCK(&glues);
325                         return -1;
326                 }
327         }
328
329         AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
330
331         AST_RWLIST_UNLOCK(&glues);
332
333         ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
334
335         return 0;
336 }
337
338 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
339 {
340         struct ast_rtp_glue *current_glue = NULL;
341
342         AST_RWLIST_WRLOCK(&glues);
343
344         if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
345                 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
346         }
347
348         AST_RWLIST_UNLOCK(&glues);
349
350         return current_glue ? 0 : -1;
351 }
352
353 static void instance_destructor(void *obj)
354 {
355         struct ast_rtp_instance *instance = obj;
356
357         /* Pass us off to the engine to destroy */
358         if (instance->data && instance->engine->destroy(instance)) {
359                 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
360                 return;
361         }
362
363         if (instance->srtp) {
364                 res_srtp->destroy(instance->srtp);
365         }
366
367         ast_rtp_codecs_payloads_destroy(&instance->codecs);
368
369         /* Drop our engine reference */
370         ast_module_unref(instance->engine->mod);
371
372         ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
373 }
374
375 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
376 {
377         ao2_ref(instance, -1);
378
379         return 0;
380 }
381
382 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
383                 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
384                 void *data)
385 {
386         struct ast_sockaddr address = {{0,}};
387         struct ast_rtp_instance *instance = NULL;
388         struct ast_rtp_engine *engine = NULL;
389
390         AST_RWLIST_RDLOCK(&engines);
391
392         /* If an engine name was specified try to use it or otherwise use the first one registered */
393         if (!ast_strlen_zero(engine_name)) {
394                 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
395                         if (!strcmp(engine->name, engine_name)) {
396                                 break;
397                         }
398                 }
399         } else {
400                 engine = AST_RWLIST_FIRST(&engines);
401         }
402
403         /* If no engine was actually found bail out now */
404         if (!engine) {
405                 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
406                 AST_RWLIST_UNLOCK(&engines);
407                 return NULL;
408         }
409
410         /* Bump up the reference count before we return so the module can not be unloaded */
411         ast_module_ref(engine->mod);
412
413         AST_RWLIST_UNLOCK(&engines);
414
415         /* Allocate a new RTP instance */
416         if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
417                 ast_module_unref(engine->mod);
418                 return NULL;
419         }
420         instance->engine = engine;
421         ast_sockaddr_copy(&instance->local_address, sa);
422         ast_sockaddr_copy(&address, sa);
423
424         if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
425                 ao2_ref(instance, -1);
426                 return NULL;
427         }
428
429         ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
430
431         /* And pass it off to the engine to setup */
432         if (instance->engine->new(instance, sched, &address, data)) {
433                 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
434                 ao2_ref(instance, -1);
435                 return NULL;
436         }
437
438         ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
439
440         return instance;
441 }
442
443 const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
444 {
445         return instance->channel_uniqueid;
446 }
447
448 void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
449 {
450         ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
451 }
452
453 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
454 {
455         instance->data = data;
456 }
457
458 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
459 {
460         return instance->data;
461 }
462
463 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
464 {
465         return instance->engine->write(instance, frame);
466 }
467
468 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
469 {
470         return instance->engine->read(instance, rtcp);
471 }
472
473 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
474                 const struct ast_sockaddr *address)
475 {
476         ast_sockaddr_copy(&instance->local_address, address);
477         return 0;
478 }
479
480 int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
481                                                  const struct ast_sockaddr *address)
482 {
483         ast_sockaddr_copy(&instance->incoming_source_address, address);
484
485         /* moo */
486
487         if (instance->engine->remote_address_set) {
488                 instance->engine->remote_address_set(instance, &instance->incoming_source_address);
489         }
490
491         return 0;
492 }
493
494 int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
495                                                   const struct ast_sockaddr *address)
496 {
497         ast_sockaddr_copy(&instance->requested_target_address, address);
498
499         return ast_rtp_instance_set_incoming_source_address(instance, address);
500 }
501
502 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
503                 struct ast_sockaddr *address)
504 {
505         if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
506                 ast_sockaddr_copy(address, &instance->local_address);
507                 return 1;
508         }
509
510         return 0;
511 }
512
513 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
514                 struct ast_sockaddr *address)
515 {
516         ast_sockaddr_copy(address, &instance->local_address);
517 }
518
519 int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
520                 struct ast_sockaddr *address)
521 {
522         if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
523                 ast_sockaddr_copy(address, &instance->requested_target_address);
524                 return 1;
525         }
526
527         return 0;
528 }
529
530 void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
531                                                   struct ast_sockaddr *address)
532 {
533         ast_sockaddr_copy(address, &instance->incoming_source_address);
534 }
535
536 void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
537                                                    struct ast_sockaddr *address)
538 {
539         ast_sockaddr_copy(address, &instance->requested_target_address);
540 }
541
542 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
543 {
544         if (instance->engine->extended_prop_set) {
545                 instance->engine->extended_prop_set(instance, property, value);
546         }
547 }
548
549 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
550 {
551         if (instance->engine->extended_prop_get) {
552                 return instance->engine->extended_prop_get(instance, property);
553         }
554
555         return NULL;
556 }
557
558 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
559 {
560         instance->properties[property] = value;
561
562         if (instance->engine->prop_set) {
563                 instance->engine->prop_set(instance, property, value);
564         }
565 }
566
567 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
568 {
569         return instance->properties[property];
570 }
571
572 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
573 {
574         return &instance->codecs;
575 }
576
577 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
578 {
579         int res;
580
581         codecs->framing = 0;
582         ast_rwlock_init(&codecs->codecs_lock);
583         res = AST_VECTOR_INIT(&codecs->payloads, AST_RTP_MAX_PT);
584
585         return res;
586 }
587
588 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
589 {
590         int i;
591
592         for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
593                 struct ast_rtp_payload_type *type;
594
595                 type = AST_VECTOR_GET(&codecs->payloads, i);
596                 ao2_t_cleanup(type, "destroying ast_rtp_codec");
597         }
598         AST_VECTOR_FREE(&codecs->payloads);
599
600         ast_rwlock_destroy(&codecs->codecs_lock);
601 }
602
603 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
604 {
605         ast_rtp_codecs_payloads_destroy(codecs);
606         ast_rtp_codecs_payloads_initialize(codecs);
607
608         if (instance && instance->engine && instance->engine->payload_set) {
609                 int i;
610                 for (i = 0; i < AST_RTP_MAX_PT; i++) {
611                         instance->engine->payload_set(instance, i, 0, NULL, 0);
612                 }
613         }
614 }
615
616 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
617 {
618         int i;
619
620         ast_rwlock_rdlock(&src->codecs_lock);
621         ast_rwlock_wrlock(&dest->codecs_lock);
622
623         for (i = 0; i < AST_VECTOR_SIZE(&src->payloads); i++) {
624                 struct ast_rtp_payload_type *type;
625
626                 type = AST_VECTOR_GET(&src->payloads, i);
627                 if (!type) {
628                         continue;
629                 }
630                 if (i < AST_VECTOR_SIZE(&dest->payloads)) {
631                         ao2_t_cleanup(AST_VECTOR_GET(&dest->payloads, i), "cleaning up vector element about to be replaced");
632                 }
633                 ast_debug(2, "Copying payload %d (%p) from %p to %p\n", i, type, src, dest);
634                 ao2_bump(type);
635                 AST_VECTOR_REPLACE(&dest->payloads, i, type);
636
637                 if (instance && instance->engine && instance->engine->payload_set) {
638                         instance->engine->payload_set(instance, i, type->asterisk_format, type->format, type->rtp_code);
639                 }
640         }
641         dest->framing = src->framing;
642         ast_rwlock_unlock(&dest->codecs_lock);
643         ast_rwlock_unlock(&src->codecs_lock);
644 }
645
646 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
647 {
648         struct ast_rtp_payload_type *new_type;
649
650         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
651                 return;
652         }
653
654         new_type = ast_rtp_engine_alloc_payload_type();
655         if (!new_type) {
656                 return;
657         }
658
659         ast_rwlock_rdlock(&static_RTP_PT_lock);
660         ast_rwlock_wrlock(&codecs->codecs_lock);
661         if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
662                 ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, payload), "cleaning up replaced payload type");
663         }
664
665         new_type->asterisk_format = static_RTP_PT[payload].asterisk_format;
666         new_type->rtp_code = static_RTP_PT[payload].rtp_code;
667         new_type->payload = payload;
668         new_type->format = ao2_bump(static_RTP_PT[payload].format);
669
670         ast_debug(1, "Setting payload %d (%p) based on m type on %p\n", payload, new_type, codecs);
671         AST_VECTOR_REPLACE(&codecs->payloads, payload, new_type);
672
673         if (instance && instance->engine && instance->engine->payload_set) {
674                 instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
675         }
676
677         ast_rwlock_unlock(&codecs->codecs_lock);
678         ast_rwlock_unlock(&static_RTP_PT_lock);
679 }
680
681 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
682                                  char *mimetype, char *mimesubtype,
683                                  enum ast_rtp_options options,
684                                  unsigned int sample_rate)
685 {
686         unsigned int i;
687         int found = 0;
688
689         if (pt < 0 || pt >= AST_RTP_MAX_PT) {
690                 return -1; /* bogus payload type */
691         }
692
693         ast_rwlock_rdlock(&mime_types_lock);
694         ast_rwlock_wrlock(&codecs->codecs_lock);
695         for (i = 0; i < mime_types_len; ++i) {
696                 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
697                 struct ast_rtp_payload_type *new_type;
698
699                 if (strcasecmp(mimesubtype, t->subtype)) {
700                         continue;
701                 }
702
703                 if (strcasecmp(mimetype, t->type)) {
704                         continue;
705                 }
706
707                 /* if both sample rates have been supplied, and they don't match,
708                  * then this not a match; if one has not been supplied, then the
709                  * rates are not compared */
710                 if (sample_rate && t->sample_rate &&
711                     (sample_rate != t->sample_rate)) {
712                         continue;
713                 }
714
715                 found = 1;
716
717                 new_type = ast_rtp_engine_alloc_payload_type();
718                 if (!new_type) {
719                         continue;
720                 }
721
722                 if (pt < AST_VECTOR_SIZE(&codecs->payloads)) {
723                         ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, pt), "cleaning up replaced payload type");
724                 }
725
726                 new_type->payload = pt;
727                 new_type->asterisk_format = t->payload_type.asterisk_format;
728                 new_type->rtp_code = t->payload_type.rtp_code;
729                 if ((ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) &&
730                                 t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
731                         new_type->format = ao2_bump(ast_format_g726_aal2);
732                 } else {
733                         new_type->format = ao2_bump(t->payload_type.format);
734                 }
735                 AST_VECTOR_REPLACE(&codecs->payloads, pt, new_type);
736
737                 if (instance && instance->engine && instance->engine->payload_set) {
738                         instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
739                 }
740
741                 break;
742         }
743         ast_rwlock_unlock(&codecs->codecs_lock);
744         ast_rwlock_unlock(&mime_types_lock);
745
746         return (found ? 0 : -2);
747 }
748
749 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)
750 {
751         return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
752 }
753
754 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
755 {
756         struct ast_rtp_payload_type *type;
757
758         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
759                 return;
760         }
761
762         ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
763
764         ast_rwlock_wrlock(&codecs->codecs_lock);
765         if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
766                 type = AST_VECTOR_GET(&codecs->payloads, payload);
767                 ao2_cleanup(type);
768                 AST_VECTOR_REPLACE(&codecs->payloads, payload, NULL);
769         }
770
771         if (instance && instance->engine && instance->engine->payload_set) {
772                 instance->engine->payload_set(instance, payload, 0, NULL, 0);
773         }
774
775         ast_rwlock_unlock(&codecs->codecs_lock);
776 }
777
778 struct ast_rtp_payload_type *ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
779 {
780         struct ast_rtp_payload_type *type = NULL;
781
782         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
783                 return NULL;
784         }
785
786         ast_rwlock_rdlock(&codecs->codecs_lock);
787         if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
788                 type = AST_VECTOR_GET(&codecs->payloads, payload);
789                 ao2_bump(type);
790         }
791         ast_rwlock_unlock(&codecs->codecs_lock);
792
793         if (!type) {
794                 type = ast_rtp_engine_alloc_payload_type();
795                 if (!type) {
796                         return NULL;
797                 }
798                 ast_rwlock_rdlock(&static_RTP_PT_lock);
799                 type->asterisk_format = static_RTP_PT[payload].asterisk_format;
800                 type->rtp_code = static_RTP_PT[payload].rtp_code;
801                 type->payload = payload;
802                 type->format = ao2_bump(static_RTP_PT[payload].format);
803                 ast_rwlock_unlock(&static_RTP_PT_lock);
804         }
805
806         return type;
807 }
808
809 int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int payload, struct ast_format *format)
810 {
811         struct ast_rtp_payload_type *type;
812
813         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
814                 return -1;
815         }
816
817         ast_rwlock_wrlock(&codecs->codecs_lock);
818         if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
819                 type = AST_VECTOR_GET(&codecs->payloads, payload);
820                 if (type && type->asterisk_format) {
821                         ao2_replace(type->format, format);
822                 }
823         }
824         ast_rwlock_unlock(&codecs->codecs_lock);
825
826         return 0;
827 }
828
829 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
830 {
831         struct ast_rtp_payload_type *type;
832         struct ast_format *format = NULL;
833
834         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
835                 return NULL;
836         }
837
838         ast_rwlock_rdlock(&codecs->codecs_lock);
839         if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
840                 type = AST_VECTOR_GET(&codecs->payloads, payload);
841                 if (type && type->asterisk_format) {
842                         format = ao2_bump(type->format);
843                 }
844         }
845         ast_rwlock_unlock(&codecs->codecs_lock);
846
847         return format;
848 }
849
850 void ast_rtp_codecs_set_framing(struct ast_rtp_codecs *codecs, unsigned int framing)
851 {
852         if (!framing) {
853                 return;
854         }
855
856         ast_rwlock_wrlock(&codecs->codecs_lock);
857         codecs->framing = framing;
858         ast_rwlock_unlock(&codecs->codecs_lock);
859 }
860
861 unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
862 {
863         unsigned int framing;
864
865         ast_rwlock_rdlock(&codecs->codecs_lock);
866         framing = codecs->framing;
867         ast_rwlock_unlock(&codecs->codecs_lock);
868
869         return framing;
870 }
871
872 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
873 {
874         int i;
875
876         ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN);
877         *nonastformats = 0;
878
879         ast_rwlock_rdlock(&codecs->codecs_lock);
880         for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
881                 struct ast_rtp_payload_type *type;
882
883                 type = AST_VECTOR_GET(&codecs->payloads, i);
884                 if (!type) {
885                         continue;
886                 }
887
888                 if (type->asterisk_format) {
889                         ast_format_cap_append(astformats, type->format, 0);
890                 } else {
891                         *nonastformats |= type->rtp_code;
892                 }
893         }
894
895         if (codecs->framing) {
896                 ast_format_cap_set_framing(astformats, codecs->framing);
897         }
898
899         ast_rwlock_unlock(&codecs->codecs_lock);
900 }
901
902 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
903 {
904         struct ast_rtp_payload_type *type;
905         int i;
906         int payload = -1;
907
908         ast_rwlock_rdlock(&codecs->codecs_lock);
909         for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
910                 type = AST_VECTOR_GET(&codecs->payloads, i);
911                 if (!type) {
912                         continue;
913                 }
914
915                 if ((asterisk_format && format && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL)
916                         || (!asterisk_format && type->rtp_code == code)) {
917                         payload = i;
918                         break;
919                 }
920         }
921         ast_rwlock_unlock(&codecs->codecs_lock);
922
923         if (payload < 0) {
924                 ast_rwlock_rdlock(&static_RTP_PT_lock);
925                 for (i = 0; i < AST_RTP_MAX_PT; i++) {
926                         if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
927                                 (ast_format_cmp(format, static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
928                                 payload = i;
929                                 break;
930                         } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
931                                 (static_RTP_PT[i].rtp_code == code)) {
932                                 payload = i;
933                                 break;
934                         }
935                 }
936                 ast_rwlock_unlock(&static_RTP_PT_lock);
937         }
938
939         return payload;
940 }
941
942 int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
943 {
944         struct ast_rtp_payload_type *type;
945         int res = -1;
946
947         ast_rwlock_rdlock(&codecs->codecs_lock);
948         if (code < AST_VECTOR_SIZE(&codecs->payloads)) {
949                 type = AST_VECTOR_GET(&codecs->payloads, code);
950                 if (type) {
951                         res = type->payload;
952                 }
953         }
954         ast_rwlock_unlock(&codecs->codecs_lock);
955
956         return res;
957 }
958
959 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
960 {
961         int i;
962         const char *res = "";
963
964         ast_rwlock_rdlock(&mime_types_lock);
965         for (i = 0; i < mime_types_len; i++) {
966                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
967                         (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
968                         if ((ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) &&
969                                         (options & AST_RTP_OPT_G726_NONSTANDARD)) {
970                                 res = "G726-32";
971                                 break;
972                         } else {
973                                 res = ast_rtp_mime_types[i].subtype;
974                                 break;
975                         }
976                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
977                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
978
979                         res = ast_rtp_mime_types[i].subtype;
980                         break;
981                 }
982         }
983         ast_rwlock_unlock(&mime_types_lock);
984
985         return res;
986 }
987
988 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
989 {
990         unsigned int i;
991         unsigned int res = 0;
992
993         ast_rwlock_rdlock(&mime_types_lock);
994         for (i = 0; i < mime_types_len; ++i) {
995                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
996                         (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
997                         res = ast_rtp_mime_types[i].sample_rate;
998                         break;
999                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
1000                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
1001                         res = ast_rtp_mime_types[i].sample_rate;
1002                         break;
1003                 }
1004         }
1005         ast_rwlock_unlock(&mime_types_lock);
1006
1007         return res;
1008 }
1009
1010 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)
1011 {
1012         int found = 0;
1013         const char *name;
1014         if (!buf) {
1015                 return NULL;
1016         }
1017
1018
1019         if (asterisk_format) {
1020                 int x;
1021                 struct ast_format *tmp_fmt;
1022                 for (x = 0; x < ast_format_cap_count(ast_format_capability); x++) {
1023                         tmp_fmt = ast_format_cap_get_format(ast_format_capability, x);
1024                         name = ast_rtp_lookup_mime_subtype2(asterisk_format, tmp_fmt, 0, options);
1025                         ao2_ref(tmp_fmt, -1);
1026                         ast_str_append(&buf, 0, "%s|", name);
1027                         found = 1;
1028                 }
1029         } else {
1030                 int x;
1031                 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
1032                 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
1033                         if (rtp_capability & x) {
1034                                 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
1035                                 ast_str_append(&buf, 0, "%s|", name);
1036                                 found = 1;
1037                         }
1038                 }
1039         }
1040
1041         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
1042
1043         return ast_str_buffer(buf);
1044 }
1045
1046 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
1047 {
1048         return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
1049 }
1050
1051 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
1052 {
1053         return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
1054 }
1055 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
1056 {
1057         return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
1058 }
1059
1060 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
1061 {
1062         return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
1063 }
1064
1065 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
1066 {
1067         return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
1068 }
1069
1070 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
1071 {
1072         if (instance->engine->update_source) {
1073                 instance->engine->update_source(instance);
1074         }
1075 }
1076
1077 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
1078 {
1079         if (instance->engine->change_source) {
1080                 instance->engine->change_source(instance);
1081         }
1082 }
1083
1084 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
1085 {
1086         return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
1087 }
1088
1089 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
1090 {
1091         if (instance->engine->stop) {
1092                 instance->engine->stop(instance);
1093         }
1094 }
1095
1096 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
1097 {
1098         return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
1099 }
1100
1101 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
1102 {
1103         struct ast_rtp_glue *glue = NULL;
1104
1105         AST_RWLIST_RDLOCK(&glues);
1106
1107         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
1108                 if (!strcasecmp(glue->type, type)) {
1109                         break;
1110                 }
1111         }
1112
1113         AST_RWLIST_UNLOCK(&glues);
1114
1115         return glue;
1116 }
1117
1118 /*!
1119  * \brief Conditionally unref an rtp instance
1120  */
1121 static void unref_instance_cond(struct ast_rtp_instance **instance)
1122 {
1123         if (*instance) {
1124                 ao2_ref(*instance, -1);
1125                 *instance = NULL;
1126         }
1127 }
1128
1129 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1130 {
1131         return instance->bridged;
1132 }
1133
1134 void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
1135 {
1136         instance->bridged = bridged;
1137 }
1138
1139 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
1140 {
1141         struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
1142                 *vinstance_dst = NULL, *vinstance_src = NULL,
1143                 *tinstance_dst = NULL, *tinstance_src = NULL;
1144         struct ast_rtp_glue *glue_dst, *glue_src;
1145         enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
1146         enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
1147         struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1148         struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1149
1150         /* Lock both channels so we can look for the glue that binds them together */
1151         ast_channel_lock_both(c_dst, c_src);
1152
1153         if (!cap_src || !cap_dst) {
1154                 goto done;
1155         }
1156
1157         /* Grab glue that binds each channel to something using the RTP engine */
1158         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))) {
1159                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
1160                 goto done;
1161         }
1162
1163         audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
1164         video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
1165
1166         audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
1167         video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
1168
1169         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1170         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)) {
1171                 audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
1172         }
1173         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)) {
1174                 audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
1175         }
1176         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) {
1177                 glue_dst->get_codec(c_dst, cap_dst);
1178         }
1179         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) {
1180                 glue_src->get_codec(c_src, cap_src);
1181         }
1182
1183         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1184         if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
1185                 goto done;
1186         }
1187
1188         /* Make sure we have matching codecs */
1189         if (!ast_format_cap_iscompatible(cap_dst, cap_src)) {
1190                 goto done;
1191         }
1192
1193         ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
1194
1195         if (vinstance_dst && vinstance_src) {
1196                 ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
1197         }
1198         if (tinstance_dst && tinstance_src) {
1199                 ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
1200         }
1201
1202         if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
1203                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
1204                         ast_channel_name(c_dst), ast_channel_name(c_src));
1205         } else {
1206                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
1207                         ast_channel_name(c_dst), ast_channel_name(c_src));
1208         }
1209
1210 done:
1211         ast_channel_unlock(c_dst);
1212         ast_channel_unlock(c_src);
1213
1214         ao2_cleanup(cap_dst);
1215         ao2_cleanup(cap_src);
1216
1217         unref_instance_cond(&instance_dst);
1218         unref_instance_cond(&instance_src);
1219         unref_instance_cond(&vinstance_dst);
1220         unref_instance_cond(&vinstance_src);
1221         unref_instance_cond(&tinstance_dst);
1222         unref_instance_cond(&tinstance_src);
1223 }
1224
1225 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1226 {
1227         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1228                         *vinstance0 = NULL, *vinstance1 = NULL,
1229                         *tinstance0 = NULL, *tinstance1 = NULL;
1230         struct ast_rtp_glue *glue0, *glue1;
1231         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1232         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1233         struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1234         struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1235
1236         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1237         if (!c1 || !cap1 || !cap0) {
1238                 ao2_cleanup(cap0);
1239                 ao2_cleanup(cap1);
1240                 return -1;
1241         }
1242
1243         /* Lock both channels so we can look for the glue that binds them together */
1244         ast_channel_lock_both(c0, c1);
1245
1246         /* Grab glue that binds each channel to something using the RTP engine */
1247         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1248                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1249                 goto done;
1250         }
1251
1252         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1253         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1254
1255         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1256         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1257
1258         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1259         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)) {
1260                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1261         }
1262         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)) {
1263                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1264         }
1265         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) {
1266                 glue0->get_codec(c0, cap0);
1267         }
1268         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) {
1269                 glue1->get_codec(c1, cap1);
1270         }
1271
1272         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1273         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1274                 goto done;
1275         }
1276
1277         /* Make sure we have matching codecs */
1278         if (!ast_format_cap_iscompatible(cap0, cap1)) {
1279                 goto done;
1280         }
1281
1282         /* Bridge media early */
1283         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1284                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1285         }
1286
1287 done:
1288         ast_channel_unlock(c0);
1289         ast_channel_unlock(c1);
1290
1291         ao2_cleanup(cap0);
1292         ao2_cleanup(cap1);
1293
1294         unref_instance_cond(&instance0);
1295         unref_instance_cond(&instance1);
1296         unref_instance_cond(&vinstance0);
1297         unref_instance_cond(&vinstance1);
1298         unref_instance_cond(&tinstance0);
1299         unref_instance_cond(&tinstance1);
1300
1301         ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1302
1303         return 0;
1304 }
1305
1306 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1307 {
1308         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1309 }
1310
1311 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1312 {
1313         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1314 }
1315
1316 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1317 {
1318         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1319 }
1320
1321 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1322 {
1323         struct ast_rtp_instance_stats stats = { 0, };
1324         enum ast_rtp_instance_stat stat;
1325
1326         /* Determine what statistics we will need to retrieve based on field passed in */
1327         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1328                 stat = AST_RTP_INSTANCE_STAT_ALL;
1329         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1330                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1331         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1332                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1333         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1334                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1335         } else {
1336                 return NULL;
1337         }
1338
1339         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1340         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1341                 return NULL;
1342         }
1343
1344         /* Now actually fill the buffer with the good information */
1345         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1346                 snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1347                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
1348         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1349                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1350                          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));
1351         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1352                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1353                          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));
1354         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1355                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1356         }
1357
1358         return buf;
1359 }
1360
1361 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1362 {
1363         char quality_buf[AST_MAX_USER_FIELD];
1364         char *quality;
1365         struct ast_channel *bridge = ast_channel_bridge_peer(chan);
1366
1367         ast_channel_lock(chan);
1368         ast_channel_stage_snapshot(chan);
1369         ast_channel_unlock(chan);
1370         if (bridge) {
1371                 ast_channel_lock(bridge);
1372                 ast_channel_stage_snapshot(bridge);
1373                 ast_channel_unlock(bridge);
1374         }
1375
1376         quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
1377                 quality_buf, sizeof(quality_buf));
1378         if (quality) {
1379                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1380                 if (bridge) {
1381                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1382                 }
1383         }
1384
1385         quality = ast_rtp_instance_get_quality(instance,
1386                 AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
1387         if (quality) {
1388                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1389                 if (bridge) {
1390                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1391                 }
1392         }
1393
1394         quality = ast_rtp_instance_get_quality(instance,
1395                 AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
1396         if (quality) {
1397                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1398                 if (bridge) {
1399                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1400                 }
1401         }
1402
1403         quality = ast_rtp_instance_get_quality(instance,
1404                 AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
1405         if (quality) {
1406                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1407                 if (bridge) {
1408                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1409                 }
1410         }
1411
1412         ast_channel_lock(chan);
1413         ast_channel_stage_snapshot_done(chan);
1414         ast_channel_unlock(chan);
1415         if (bridge) {
1416                 ast_channel_lock(bridge);
1417                 ast_channel_stage_snapshot_done(bridge);
1418                 ast_channel_unlock(bridge);
1419                 ast_channel_unref(bridge);
1420         }
1421 }
1422
1423 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1424 {
1425         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1426 }
1427
1428 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1429 {
1430         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1431 }
1432
1433 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1434 {
1435         struct ast_rtp_glue *glue;
1436         struct ast_rtp_instance *peer_instance = NULL;
1437         int res = -1;
1438
1439         if (!instance->engine->make_compatible) {
1440                 return -1;
1441         }
1442
1443         ast_channel_lock(peer);
1444
1445         if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
1446                 ast_channel_unlock(peer);
1447                 return -1;
1448         }
1449
1450         glue->get_rtp_info(peer, &peer_instance);
1451         if (!peer_instance) {
1452                 ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
1453                 ast_channel_unlock(peer);
1454                 return -1;
1455         }
1456         if (peer_instance->engine != instance->engine) {
1457                 ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
1458                 ast_channel_unlock(peer);
1459                 ao2_ref(peer_instance, -1);
1460                 return -1;
1461         }
1462
1463         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1464
1465         ast_channel_unlock(peer);
1466
1467         ao2_ref(peer_instance, -1);
1468         peer_instance = NULL;
1469
1470         return res;
1471 }
1472
1473 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)
1474 {
1475         if (instance->engine->available_formats) {
1476                 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1477                 if (ast_format_cap_count(result)) {
1478                         return;
1479                 }
1480         }
1481
1482         ast_translate_available_formats(to_endpoint, to_asterisk, result);
1483 }
1484
1485 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1486 {
1487         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1488 }
1489
1490 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1491                                    struct ast_sockaddr *suggestion,
1492                                    const char *username)
1493 {
1494         if (instance->engine->stun_request) {
1495                 instance->engine->stun_request(instance, suggestion, username);
1496         }
1497 }
1498
1499 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1500 {
1501         instance->timeout = timeout;
1502 }
1503
1504 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1505 {
1506         instance->holdtimeout = timeout;
1507 }
1508
1509 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
1510 {
1511         instance->keepalive = interval;
1512 }
1513
1514 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1515 {
1516         return instance->timeout;
1517 }
1518
1519 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1520 {
1521         return instance->holdtimeout;
1522 }
1523
1524 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
1525 {
1526         return instance->keepalive;
1527 }
1528
1529 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1530 {
1531         return instance->engine;
1532 }
1533
1534 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1535 {
1536         return instance->glue;
1537 }
1538
1539 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1540 {
1541         if (res_srtp || res_srtp_policy) {
1542                 return -1;
1543         }
1544         if (!srtp_res || !policy_res) {
1545                 return -1;
1546         }
1547
1548         res_srtp = srtp_res;
1549         res_srtp_policy = policy_res;
1550
1551         return 0;
1552 }
1553
1554 void ast_rtp_engine_unregister_srtp(void)
1555 {
1556         res_srtp = NULL;
1557         res_srtp_policy = NULL;
1558 }
1559
1560 int ast_rtp_engine_srtp_is_registered(void)
1561 {
1562         return res_srtp && res_srtp_policy;
1563 }
1564
1565 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
1566 {
1567         int res = 0;
1568
1569         if (!res_srtp) {
1570                 return -1;
1571         }
1572
1573         if (!instance->srtp) {
1574                 res = res_srtp->create(&instance->srtp, instance, remote_policy);
1575         } else {
1576                 res = res_srtp->replace(&instance->srtp, instance, remote_policy);
1577         }
1578         if (!res) {
1579                 res = res_srtp->add_stream(instance->srtp, local_policy);
1580         }
1581
1582         return res;
1583 }
1584
1585 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
1586 {
1587         return instance->srtp;
1588 }
1589
1590 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
1591 {
1592         if (instance->engine->sendcng) {
1593                 return instance->engine->sendcng(instance, level);
1594         }
1595
1596         return -1;
1597 }
1598
1599 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
1600 {
1601         return instance->engine->ice;
1602 }
1603
1604 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
1605 {
1606         return instance->engine->dtls;
1607 }
1608
1609 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
1610 {
1611         if (!strcasecmp(name, "dtlsenable")) {
1612                 dtls_cfg->enabled = ast_true(value) ? 1 : 0;
1613         } else if (!strcasecmp(name, "dtlsverify")) {
1614                 if (!strcasecmp(value, "yes")) {
1615                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
1616                 } else if (!strcasecmp(value, "fingerprint")) {
1617                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
1618                 } else if (!strcasecmp(value, "certificate")) {
1619                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
1620                 } else if (!strcasecmp(value, "no")) {
1621                         dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
1622                 } else {
1623                         return -1;
1624                 }
1625         } else if (!strcasecmp(name, "dtlsrekey")) {
1626                 if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
1627                         return -1;
1628                 }
1629         } else if (!strcasecmp(name, "dtlscertfile")) {
1630                 ast_free(dtls_cfg->certfile);
1631                 dtls_cfg->certfile = ast_strdup(value);
1632         } else if (!strcasecmp(name, "dtlsprivatekey")) {
1633                 ast_free(dtls_cfg->pvtfile);
1634                 dtls_cfg->pvtfile = ast_strdup(value);
1635         } else if (!strcasecmp(name, "dtlscipher")) {
1636                 ast_free(dtls_cfg->cipher);
1637                 dtls_cfg->cipher = ast_strdup(value);
1638         } else if (!strcasecmp(name, "dtlscafile")) {
1639                 ast_free(dtls_cfg->cafile);
1640                 dtls_cfg->cafile = ast_strdup(value);
1641         } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
1642                 ast_free(dtls_cfg->capath);
1643                 dtls_cfg->capath = ast_strdup(value);
1644         } else if (!strcasecmp(name, "dtlssetup")) {
1645                 if (!strcasecmp(value, "active")) {
1646                         dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
1647                 } else if (!strcasecmp(value, "passive")) {
1648                         dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1649                 } else if (!strcasecmp(value, "actpass")) {
1650                         dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
1651                 }
1652         } else if (!strcasecmp(name, "dtlsfingerprint")) {
1653                 if (!strcasecmp(value, "sha-256")) {
1654                         dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
1655                 } else if (!strcasecmp(value, "sha-1")) {
1656                         dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
1657                 }
1658         } else {
1659                 return -1;
1660         }
1661
1662         return 0;
1663 }
1664
1665 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
1666 {
1667         ast_rtp_dtls_cfg_free(dst_cfg);         /* Prevent a double-call leaking memory via ast_strdup */
1668
1669         dst_cfg->enabled = src_cfg->enabled;
1670         dst_cfg->verify = src_cfg->verify;
1671         dst_cfg->rekey = src_cfg->rekey;
1672         dst_cfg->suite = src_cfg->suite;
1673         dst_cfg->hash = src_cfg->hash;
1674         dst_cfg->certfile = ast_strdup(src_cfg->certfile);
1675         dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
1676         dst_cfg->cipher = ast_strdup(src_cfg->cipher);
1677         dst_cfg->cafile = ast_strdup(src_cfg->cafile);
1678         dst_cfg->capath = ast_strdup(src_cfg->capath);
1679         dst_cfg->default_setup = src_cfg->default_setup;
1680 }
1681
1682 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
1683 {
1684         ast_free(dtls_cfg->certfile);
1685         dtls_cfg->certfile = NULL;
1686         ast_free(dtls_cfg->pvtfile);
1687         dtls_cfg->pvtfile = NULL;
1688         ast_free(dtls_cfg->cipher);
1689         dtls_cfg->cipher = NULL;
1690         ast_free(dtls_cfg->cafile);
1691         dtls_cfg->cafile = NULL;
1692         ast_free(dtls_cfg->capath);
1693         dtls_cfg->capath = NULL;
1694 }
1695
1696 /*! \internal
1697  * \brief Small helper routine that cleans up entry i in
1698  * \c static_RTP_PT.
1699  */
1700 static void rtp_engine_static_RTP_PT_cleanup(int i)
1701 {
1702         ao2_cleanup(static_RTP_PT[i].format);
1703         memset(&static_RTP_PT[i], 0, sizeof(struct ast_rtp_payload_type));
1704 }
1705
1706 /*! \internal
1707  * \brief Small helper routine that cleans up entry i in
1708  * \c ast_rtp_mime_types.
1709  */
1710 static void rtp_engine_mime_type_cleanup(int i)
1711 {
1712         ao2_cleanup(ast_rtp_mime_types[i].payload_type.format);
1713         memset(&ast_rtp_mime_types[i], 0, sizeof(struct ast_rtp_mime_type));
1714 }
1715
1716 static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
1717 {
1718         int x;
1719
1720         ast_rwlock_wrlock(&mime_types_lock);
1721
1722         x = mime_types_len;
1723         if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
1724                 ast_rwlock_unlock(&mime_types_lock);
1725                 return;
1726         }
1727
1728         /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
1729         memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));    
1730         if (format) {
1731                 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
1732                 ast_rtp_mime_types[x].payload_type.format = ao2_bump(format);
1733         } else {
1734                 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
1735         }
1736         ast_copy_string(ast_rtp_mime_types[x].type, type, sizeof(ast_rtp_mime_types[x].type));
1737         ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
1738         ast_rtp_mime_types[x].sample_rate = sample_rate;
1739         mime_types_len++;
1740
1741         ast_rwlock_unlock(&mime_types_lock);
1742 }
1743
1744 static void add_static_payload(int map, struct ast_format *format, int rtp_code)
1745 {
1746         int x;
1747
1748         ast_assert(map < ARRAY_LEN(static_RTP_PT));
1749
1750         ast_rwlock_wrlock(&static_RTP_PT_lock);
1751         if (map < 0) {
1752                 /* find next available dynamic payload slot */
1753                 for (x = AST_RTP_PT_FIRST_DYNAMIC; x < AST_RTP_MAX_PT; ++x) {
1754                         if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
1755                                 map = x;
1756                                 break;
1757                         }
1758                 }
1759                 if (map < 0) {
1760                         ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n",
1761                                 ast_format_get_name(format));
1762                         ast_rwlock_unlock(&static_RTP_PT_lock);
1763                         return;
1764                 }
1765         }
1766
1767         if (format) {
1768                 static_RTP_PT[map].asterisk_format = 1;
1769                 static_RTP_PT[map].format = ao2_bump(format);
1770         } else {
1771                 static_RTP_PT[map].rtp_code = rtp_code;
1772         }
1773         ast_rwlock_unlock(&static_RTP_PT_lock);
1774 }
1775
1776 int ast_rtp_engine_load_format(struct ast_format *format)
1777 {
1778         char *codec_name = ast_strdupa(ast_format_get_name(format));
1779
1780         codec_name = ast_str_to_upper(codec_name);
1781
1782         set_next_mime_type(format,
1783                 0,
1784                 ast_codec_media_type2str(ast_format_get_type(format)),
1785                 codec_name,
1786                 ast_format_get_sample_rate(format));
1787         add_static_payload(-1, format, 0);
1788
1789         return 0;
1790 }
1791
1792 int ast_rtp_engine_unload_format(struct ast_format *format)
1793 {
1794         int x;
1795         int y = 0;
1796
1797         ast_rwlock_wrlock(&static_RTP_PT_lock);
1798         /* remove everything pertaining to this format id from the lists */
1799         for (x = 0; x < AST_RTP_MAX_PT; x++) {
1800                 if (ast_format_cmp(static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
1801                         rtp_engine_static_RTP_PT_cleanup(x);
1802                 }
1803         }
1804         ast_rwlock_unlock(&static_RTP_PT_lock);
1805
1806         ast_rwlock_wrlock(&mime_types_lock);
1807         /* rebuild the list skipping the items matching this id */
1808         for (x = 0; x < mime_types_len; x++) {
1809                 if (ast_format_cmp(ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
1810                         rtp_engine_mime_type_cleanup(x);
1811                         continue;
1812                 }
1813                 if (x != y) {
1814                         ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
1815                 }
1816                 y++;
1817         }
1818         mime_types_len = y;
1819         ast_rwlock_unlock(&mime_types_lock);
1820         return 0;
1821 }
1822
1823 /*!
1824  * \internal
1825  * \brief \ref stasis message payload for RTCP messages
1826  */
1827 struct rtcp_message_payload {
1828         struct ast_channel_snapshot *snapshot;  /*< The channel snapshot, if available */
1829         struct ast_rtp_rtcp_report *report;     /*< The RTCP report */
1830         struct ast_json *blob;                  /*< Extra JSON data to publish */
1831 };
1832
1833 static void rtcp_message_payload_dtor(void *obj)
1834 {
1835         struct rtcp_message_payload *payload = obj;
1836
1837         ao2_cleanup(payload->report);
1838         ao2_cleanup(payload->snapshot);
1839         ast_json_unref(payload->blob);
1840 }
1841
1842 static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
1843 {
1844         struct rtcp_message_payload *payload = stasis_message_data(msg);
1845         RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
1846         RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
1847         unsigned int ssrc = payload->report->ssrc;
1848         unsigned int type = payload->report->type;
1849         unsigned int report_count = payload->report->reception_report_count;
1850         int i;
1851
1852         if (!packet_string) {
1853                 return NULL;
1854         }
1855
1856         if (payload->snapshot) {
1857                 channel_string = ast_manager_build_channel_state_string(payload->snapshot);
1858                 if (!channel_string) {
1859                         return NULL;
1860                 }
1861         }
1862
1863         if (payload->blob) {
1864                 /* Optional data */
1865                 struct ast_json *to = ast_json_object_get(payload->blob, "to");
1866                 struct ast_json *from = ast_json_object_get(payload->blob, "from");
1867                 struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
1868                 if (to) {
1869                         ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
1870                 }
1871                 if (from) {
1872                         ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
1873                 }
1874                 if (rtt) {
1875                         ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
1876                 }
1877         }
1878
1879         ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
1880         ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
1881         ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
1882         if (type == AST_RTP_RTCP_SR) {
1883                 ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
1884                         (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
1885                         (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec * 4096);
1886                 ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
1887                                 payload->report->sender_information.rtp_timestamp);
1888                 ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
1889                                 payload->report->sender_information.packet_count);
1890                 ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
1891                                 payload->report->sender_information.octet_count);
1892         }
1893
1894         for (i = 0; i < report_count; i++) {
1895                 RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
1896
1897                 if (!payload->report->report_block[i]) {
1898                         break;
1899                 }
1900
1901                 report_string = ast_str_create(256);
1902                 if (!report_string) {
1903                         return NULL;
1904                 }
1905
1906                 ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
1907                                 i, payload->report->report_block[i]->source_ssrc);
1908                 ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
1909                                 i, payload->report->report_block[i]->lost_count.fraction);
1910                 ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
1911                                 i, payload->report->report_block[i]->lost_count.packets);
1912                 ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
1913                                 i, payload->report->report_block[i]->highest_seq_no & 0xffff);
1914                 ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
1915                                 i, payload->report->report_block[i]->highest_seq_no >> 16);
1916                 ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
1917                                 i, payload->report->report_block[i]->ia_jitter);
1918                 ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
1919                                 i, payload->report->report_block[i]->lsr);
1920                 ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
1921                                 i, ((double)payload->report->report_block[i]->dlsr) / 65536);
1922                 ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
1923         }
1924
1925         return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
1926                 stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
1927                 "%s%s",
1928                 AS_OR(channel_string, ""),
1929                 ast_str_buffer(packet_string));
1930 }
1931
1932 static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
1933         const struct stasis_message_sanitizer *sanitize)
1934 {
1935         struct rtcp_message_payload *payload = stasis_message_data(msg);
1936         RAII_VAR(struct ast_json *, json_rtcp_report, NULL, ast_json_unref);
1937         RAII_VAR(struct ast_json *, json_rtcp_report_blocks, NULL, ast_json_unref);
1938         RAII_VAR(struct ast_json *, json_rtcp_sender_info, NULL, ast_json_unref);
1939         RAII_VAR(struct ast_json *, json_channel, NULL, ast_json_unref);
1940         int i;
1941
1942         json_rtcp_report_blocks = ast_json_array_create();
1943         if (!json_rtcp_report_blocks) {
1944                 return NULL;
1945         }
1946
1947         for (i = 0; i < payload->report->reception_report_count && payload->report->report_block[i]; i++) {
1948                 struct ast_json *json_report_block;
1949                 char str_lsr[32];
1950                 snprintf(str_lsr, sizeof(str_lsr), "%u", payload->report->report_block[i]->lsr);
1951                 json_report_block = ast_json_pack("{s: i, s: i, s: i, s: i, s: i, s: s, s: i}",
1952                                 "source_ssrc", payload->report->report_block[i]->source_ssrc,
1953                                 "fraction_lost", payload->report->report_block[i]->lost_count.fraction,
1954                                 "packets_lost", payload->report->report_block[i]->lost_count.packets,
1955                                 "highest_seq_no", payload->report->report_block[i]->highest_seq_no,
1956                                 "ia_jitter", payload->report->report_block[i]->ia_jitter,
1957                                 "lsr", str_lsr,
1958                                 "dlsr", payload->report->report_block[i]->dlsr);
1959                 if (!json_report_block) {
1960                         return NULL;
1961                 }
1962
1963                 if (ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
1964                         return NULL;
1965                 }
1966         }
1967
1968         if (payload->report->type == AST_RTP_RTCP_SR) {
1969                 char sec[32];
1970                 char usec[32];
1971                 snprintf(sec, sizeof(sec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec);
1972                 snprintf(usec, sizeof(usec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
1973                 json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: i, s: i, s: i}",
1974                                 "ntp_timestamp_sec", sec,
1975                                 "ntp_timestamp_usec", usec,
1976                                 "rtp_timestamp", payload->report->sender_information.rtp_timestamp,
1977                                 "packets", payload->report->sender_information.packet_count,
1978                                 "octets", payload->report->sender_information.octet_count);
1979                 if (!json_rtcp_sender_info) {
1980                         return NULL;
1981                 }
1982         }
1983
1984         json_rtcp_report = ast_json_pack("{s: i, s: i, s: i, s: O, s: O}",
1985                         "ssrc", payload->report->ssrc,
1986                         "type", payload->report->type,
1987                         "report_count", payload->report->reception_report_count,
1988                         "sender_information", json_rtcp_sender_info ? json_rtcp_sender_info : ast_json_null(),
1989                         "report_blocks", json_rtcp_report_blocks);
1990         if (!json_rtcp_report) {
1991                 return NULL;
1992         }
1993
1994         if (payload->snapshot) {
1995                 json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
1996                 if (!json_channel) {
1997                         return NULL;
1998                 }
1999         }
2000
2001         return ast_json_pack("{s: O, s: O, s: O}",
2002                 "channel", payload->snapshot ? json_channel : ast_json_null(),
2003                 "rtcp_report", json_rtcp_report,
2004                 "blob", payload->blob);
2005 }
2006
2007 static void rtp_rtcp_report_dtor(void *obj)
2008 {
2009         int i;
2010         struct ast_rtp_rtcp_report *rtcp_report = obj;
2011
2012         for (i = 0; i < rtcp_report->reception_report_count; i++) {
2013                 ast_free(rtcp_report->report_block[i]);
2014         }
2015 }
2016
2017 struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
2018 {
2019         struct ast_rtp_rtcp_report *rtcp_report;
2020
2021         /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
2022         rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
2023                 rtp_rtcp_report_dtor);
2024
2025         return rtcp_report;
2026 }
2027
2028 void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
2029                 struct stasis_message_type *message_type,
2030                 struct ast_rtp_rtcp_report *report,
2031                 struct ast_json *blob)
2032 {
2033         RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
2034         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
2035
2036         if (!message_type) {
2037                 return;
2038         }
2039
2040         payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
2041         if (!payload || !report) {
2042                 return;
2043         }
2044
2045         if (!ast_strlen_zero(rtp->channel_uniqueid)) {
2046                 payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
2047         }
2048         if (blob) {
2049                 payload->blob = blob;
2050                 ast_json_ref(blob);
2051         }
2052         ao2_ref(report, +1);
2053         payload->report = report;
2054
2055         message = stasis_message_create(message_type, payload);
2056         if (!message) {
2057                 return;
2058         }
2059
2060         stasis_publish(ast_rtp_topic(), message);
2061 }
2062
2063 /*!
2064  * @{ \brief Define RTCP/RTP message types.
2065  */
2066 STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
2067                 .to_ami = rtcp_report_to_ami,
2068                 .to_json = rtcp_report_to_json,);
2069 STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
2070                 .to_ami = rtcp_report_to_ami,
2071                 .to_json = rtcp_report_to_json,);
2072 /*! @} */
2073
2074 struct stasis_topic *ast_rtp_topic(void)
2075 {
2076         return rtp_topic;
2077 }
2078
2079 static void rtp_engine_shutdown(void)
2080 {
2081         int x;
2082
2083         ao2_cleanup(rtp_topic);
2084         rtp_topic = NULL;
2085         STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
2086         STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
2087
2088         ast_rwlock_wrlock(&static_RTP_PT_lock);
2089         for (x = 0; x < AST_RTP_MAX_PT; x++) {
2090                 if (static_RTP_PT[x].format) {
2091                         rtp_engine_static_RTP_PT_cleanup(x);
2092                 }
2093         }
2094         ast_rwlock_unlock(&static_RTP_PT_lock);
2095
2096         ast_rwlock_wrlock(&mime_types_lock);
2097         for (x = 0; x < mime_types_len; x++) {
2098                 if (ast_rtp_mime_types[x].payload_type.format) {
2099                         rtp_engine_mime_type_cleanup(x);
2100                 }
2101         }
2102         mime_types_len = 0;
2103         ast_rwlock_unlock(&mime_types_lock);
2104 }
2105
2106 int ast_rtp_engine_init(void)
2107 {
2108         ast_rwlock_init(&mime_types_lock);
2109         ast_rwlock_init(&static_RTP_PT_lock);
2110
2111         rtp_topic = stasis_topic_create("rtp_topic");
2112         if (!rtp_topic) {
2113                 return -1;
2114         }
2115         STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
2116         STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
2117         ast_register_cleanup(rtp_engine_shutdown);
2118
2119         /* Define all the RTP mime types available */
2120         set_next_mime_type(ast_format_g723, 0, "audio", "G723", 8000);
2121         set_next_mime_type(ast_format_gsm, 0, "audio", "GSM", 8000);
2122         set_next_mime_type(ast_format_ulaw, 0, "audio", "PCMU", 8000);
2123         set_next_mime_type(ast_format_ulaw, 0, "audio", "G711U", 8000);
2124         set_next_mime_type(ast_format_alaw, 0, "audio", "PCMA", 8000);
2125         set_next_mime_type(ast_format_alaw, 0, "audio", "G711A", 8000);
2126         set_next_mime_type(ast_format_g726, 0, "audio", "G726-32", 8000);
2127         set_next_mime_type(ast_format_adpcm, 0, "audio", "DVI4", 8000);
2128         set_next_mime_type(ast_format_slin, 0, "audio", "L16", 8000);
2129         set_next_mime_type(ast_format_slin16, 0, "audio", "L16", 16000);
2130         set_next_mime_type(ast_format_slin16, 0, "audio", "L16-256", 16000);
2131         set_next_mime_type(ast_format_slin12, 0, "audio", "L16", 12000);
2132         set_next_mime_type(ast_format_slin24, 0, "audio", "L16", 24000);
2133         set_next_mime_type(ast_format_slin32, 0, "audio", "L16", 32000);
2134         set_next_mime_type(ast_format_slin44, 0, "audio", "L16", 44000);
2135         set_next_mime_type(ast_format_slin48, 0, "audio", "L16", 48000);
2136         set_next_mime_type(ast_format_slin96, 0, "audio", "L16", 96000);
2137         set_next_mime_type(ast_format_slin192, 0, "audio", "L16", 192000);
2138         set_next_mime_type(ast_format_lpc10, 0, "audio", "LPC", 8000);
2139         set_next_mime_type(ast_format_g729, 0, "audio", "G729", 8000);
2140         set_next_mime_type(ast_format_g729, 0, "audio", "G729A", 8000);
2141         set_next_mime_type(ast_format_g729, 0, "audio", "G.729", 8000);
2142         set_next_mime_type(ast_format_speex, 0, "audio", "speex", 8000);
2143         set_next_mime_type(ast_format_speex16, 0,  "audio", "speex", 16000);
2144         set_next_mime_type(ast_format_speex32, 0,  "audio", "speex", 32000);
2145         set_next_mime_type(ast_format_ilbc, 0, "audio", "iLBC", 8000);
2146         /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
2147         set_next_mime_type(ast_format_g722, 0, "audio", "G722", 8000);
2148         set_next_mime_type(ast_format_g726_aal2, 0, "audio", "AAL2-G726-32", 8000);
2149         set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
2150         set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
2151         set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
2152         set_next_mime_type(ast_format_jpeg, 0, "video", "JPEG", 90000);
2153         set_next_mime_type(ast_format_png, 0, "video", "PNG", 90000);
2154         set_next_mime_type(ast_format_h261, 0, "video", "H261", 90000);
2155         set_next_mime_type(ast_format_h263, 0, "video", "H263", 90000);
2156         set_next_mime_type(ast_format_h263p, 0, "video", "h263-1998", 90000);
2157         set_next_mime_type(ast_format_h264, 0, "video", "H264", 90000);
2158         set_next_mime_type(ast_format_mp4, 0, "video", "MP4V-ES", 90000);
2159         set_next_mime_type(ast_format_t140_red, 0, "text", "RED", 1000);
2160         set_next_mime_type(ast_format_t140, 0, "text", "T140", 1000);
2161         set_next_mime_type(ast_format_siren7, 0, "audio", "G7221", 16000);
2162         set_next_mime_type(ast_format_siren14, 0, "audio", "G7221", 32000);
2163         set_next_mime_type(ast_format_g719, 0, "audio", "G719", 48000);
2164         /* Opus and VP8 */
2165         set_next_mime_type(ast_format_opus, 0,  "audio", "opus", 48000);
2166         set_next_mime_type(ast_format_vp8, 0,  "video", "VP8", 90000);
2167
2168         /* Define the static rtp payload mappings */
2169         add_static_payload(0, ast_format_ulaw, 0);
2170         #ifdef USE_DEPRECATED_G726
2171         add_static_payload(2, ast_format_g726, 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
2172         #endif
2173         add_static_payload(3, ast_format_gsm, 0);
2174         add_static_payload(4, ast_format_g723, 0);
2175         add_static_payload(5, ast_format_adpcm, 0);/* 8 kHz */
2176         add_static_payload(6, ast_format_adpcm, 0); /* 16 kHz */
2177         add_static_payload(7, ast_format_lpc10, 0);
2178         add_static_payload(8, ast_format_alaw, 0);
2179         add_static_payload(9, ast_format_g722, 0);
2180         add_static_payload(10, ast_format_slin, 0); /* 2 channels */
2181         add_static_payload(11, ast_format_slin, 0); /* 1 channel */
2182         add_static_payload(13, NULL, AST_RTP_CN);
2183         add_static_payload(16, ast_format_adpcm, 0); /* 11.025 kHz */
2184         add_static_payload(17, ast_format_adpcm, 0); /* 22.050 kHz */
2185         add_static_payload(18, ast_format_g729, 0);
2186         add_static_payload(19, NULL, AST_RTP_CN);         /* Also used for CN */
2187         add_static_payload(26, ast_format_jpeg, 0);
2188         add_static_payload(31, ast_format_h261, 0);
2189         add_static_payload(34, ast_format_h263, 0);
2190         add_static_payload(97, ast_format_ilbc, 0);
2191         add_static_payload(98, ast_format_h263p, 0);
2192         add_static_payload(99, ast_format_h264, 0);
2193         add_static_payload(101, NULL, AST_RTP_DTMF);
2194         add_static_payload(102, ast_format_siren7, 0);
2195         add_static_payload(103, ast_format_h263p, 0);
2196         add_static_payload(104, ast_format_mp4, 0);
2197         add_static_payload(105, ast_format_t140_red, 0);   /* Real time text chat (with redundancy encoding) */
2198         add_static_payload(106, ast_format_t140, 0);     /* Real time text chat */
2199         add_static_payload(110, ast_format_speex, 0);
2200         add_static_payload(111, ast_format_g726, 0);
2201         add_static_payload(112, ast_format_g726_aal2, 0);
2202         add_static_payload(115, ast_format_siren14, 0);
2203         add_static_payload(116, ast_format_g719, 0);
2204         add_static_payload(117, ast_format_speex16, 0);
2205         add_static_payload(118, ast_format_slin16, 0); /* 16 Khz signed linear */
2206         add_static_payload(119, ast_format_speex32, 0);
2207         add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
2208         add_static_payload(122, ast_format_slin12, 0);
2209         add_static_payload(123, ast_format_slin24, 0);
2210         add_static_payload(124, ast_format_slin32, 0);
2211         add_static_payload(125, ast_format_slin44, 0);
2212         add_static_payload(126, ast_format_slin48, 0);
2213         add_static_payload(127, ast_format_slin96, 0);
2214         /* payload types above 127 are not valid */
2215         add_static_payload(96, ast_format_slin192, 0);
2216         /* Opus and VP8 */
2217         add_static_payload(100, ast_format_vp8, 0);
2218         add_static_payload(107, ast_format_opus, 0);
2219
2220         return 0;
2221 }
2222
2223 time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
2224 {
2225         return rtp->last_tx;
2226 }
2227
2228 void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
2229 {
2230         rtp->last_tx = time;
2231 }
2232
2233 time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp)
2234 {
2235         return rtp->last_rx;
2236 }
2237
2238 void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
2239 {
2240         rtp->last_rx = time;
2241 }