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