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