7907efba45d48b5f42b4758ebdbcb643f23321b3
[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 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <math.h>
35
36 #include "asterisk/channel.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/module.h"
39 #include "asterisk/rtp_engine.h"
40 #include "asterisk/manager.h"
41 #include "asterisk/options.h"
42 #include "asterisk/astobj2.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/netsock2.h"
46 #include "asterisk/_private.h"
47 #include "asterisk/framehook.h"
48
49 struct ast_srtp_res *res_srtp = NULL;
50 struct ast_srtp_policy_res *res_srtp_policy = NULL;
51
52 /*! Structure that represents an RTP session (instance) */
53 struct ast_rtp_instance {
54         /*! Engine that is handling this RTP instance */
55         struct ast_rtp_engine *engine;
56         /*! Data unique to the RTP engine */
57         void *data;
58         /*! RTP properties that have been set and their value */
59         int properties[AST_RTP_PROPERTY_MAX];
60         /*! Address that we are expecting RTP to come in to */
61         struct ast_sockaddr local_address;
62         /*! Address that we are sending RTP to */
63         struct ast_sockaddr remote_address;
64         /*! Alternate address that we are receiving RTP from */
65         struct ast_sockaddr alt_remote_address;
66         /*! Instance that we are bridged to if doing remote or local bridging */
67         struct ast_rtp_instance *bridged;
68         /*! Payload and packetization information */
69         struct ast_rtp_codecs codecs;
70         /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
71         int timeout;
72         /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
73         int holdtimeout;
74         /*! RTP keepalive interval */
75         int keepalive;
76         /*! Glue currently in use */
77         struct ast_rtp_glue *glue;
78         /*! Channel associated with the instance */
79         struct ast_channel *chan;
80         /*! SRTP info associated with the instance */
81         struct ast_srtp *srtp;
82 };
83
84 /*! List of RTP engines that are currently registered */
85 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
86
87 /*! List of RTP glues */
88 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
89
90 /*! The following array defines the MIME Media type (and subtype) for each
91    of our codecs, or RTP-specific data type. */
92 static struct ast_rtp_mime_type {
93         struct ast_rtp_payload_type payload_type;
94         char *type;
95         char *subtype;
96         unsigned int sample_rate;
97 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
98 static ast_rwlock_t mime_types_lock;
99 static int mime_types_len = 0;
100
101 /*!
102  * \brief Mapping between Asterisk codecs and rtp payload types
103  *
104  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
105  * also, our own choices for dynamic payload types.  This is our master
106  * table for transmission
107  *
108  * See http://www.iana.org/assignments/rtp-parameters for a list of
109  * assigned values
110  */
111 static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
112 static ast_rwlock_t static_RTP_PT_lock;
113
114 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
115 {
116         struct ast_rtp_engine *current_engine;
117
118         /* Perform a sanity check on the engine structure to make sure it has the basics */
119         if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
120                 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
121                 return -1;
122         }
123
124         /* Link owner module to the RTP engine for reference counting purposes */
125         engine->mod = module;
126
127         AST_RWLIST_WRLOCK(&engines);
128
129         /* Ensure that no two modules with the same name are registered at the same time */
130         AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
131                 if (!strcmp(current_engine->name, engine->name)) {
132                         ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
133                         AST_RWLIST_UNLOCK(&engines);
134                         return -1;
135                 }
136         }
137
138         /* The engine survived our critique. Off to the list it goes to be used */
139         AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
140
141         AST_RWLIST_UNLOCK(&engines);
142
143         ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
144
145         return 0;
146 }
147
148 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
149 {
150         struct ast_rtp_engine *current_engine = NULL;
151
152         AST_RWLIST_WRLOCK(&engines);
153
154         if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
155                 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
156         }
157
158         AST_RWLIST_UNLOCK(&engines);
159
160         return current_engine ? 0 : -1;
161 }
162
163 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
164 {
165         struct ast_rtp_glue *current_glue = NULL;
166
167         if (ast_strlen_zero(glue->type)) {
168                 return -1;
169         }
170
171         glue->mod = module;
172
173         AST_RWLIST_WRLOCK(&glues);
174
175         AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
176                 if (!strcasecmp(current_glue->type, glue->type)) {
177                         ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
178                         AST_RWLIST_UNLOCK(&glues);
179                         return -1;
180                 }
181         }
182
183         AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
184
185         AST_RWLIST_UNLOCK(&glues);
186
187         ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
188
189         return 0;
190 }
191
192 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
193 {
194         struct ast_rtp_glue *current_glue = NULL;
195
196         AST_RWLIST_WRLOCK(&glues);
197
198         if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
199                 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
200         }
201
202         AST_RWLIST_UNLOCK(&glues);
203
204         return current_glue ? 0 : -1;
205 }
206
207 static void instance_destructor(void *obj)
208 {
209         struct ast_rtp_instance *instance = obj;
210
211         /* Pass us off to the engine to destroy */
212         if (instance->data && instance->engine->destroy(instance)) {
213                 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
214                 return;
215         }
216
217         if (instance->srtp) {
218                 res_srtp->destroy(instance->srtp);
219         }
220
221         ast_rtp_codecs_payloads_destroy(&instance->codecs);
222
223         /* Drop our engine reference */
224         ast_module_unref(instance->engine->mod);
225
226         ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
227 }
228
229 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
230 {
231         ao2_ref(instance, -1);
232
233         return 0;
234 }
235
236 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
237                 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
238                 void *data)
239 {
240         struct ast_sockaddr address = {{0,}};
241         struct ast_rtp_instance *instance = NULL;
242         struct ast_rtp_engine *engine = NULL;
243
244         AST_RWLIST_RDLOCK(&engines);
245
246         /* If an engine name was specified try to use it or otherwise use the first one registered */
247         if (!ast_strlen_zero(engine_name)) {
248                 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
249                         if (!strcmp(engine->name, engine_name)) {
250                                 break;
251                         }
252                 }
253         } else {
254                 engine = AST_RWLIST_FIRST(&engines);
255         }
256
257         /* If no engine was actually found bail out now */
258         if (!engine) {
259                 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
260                 AST_RWLIST_UNLOCK(&engines);
261                 return NULL;
262         }
263
264         /* Bump up the reference count before we return so the module can not be unloaded */
265         ast_module_ref(engine->mod);
266
267         AST_RWLIST_UNLOCK(&engines);
268
269         /* Allocate a new RTP instance */
270         if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
271                 ast_module_unref(engine->mod);
272                 return NULL;
273         }
274         instance->engine = engine;
275         ast_sockaddr_copy(&instance->local_address, sa);
276         ast_sockaddr_copy(&address, sa);
277
278         if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
279                 ao2_ref(instance, -1);
280                 return NULL;
281         }
282
283         ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
284
285         /* And pass it off to the engine to setup */
286         if (instance->engine->new(instance, sched, &address, data)) {
287                 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
288                 ao2_ref(instance, -1);
289                 return NULL;
290         }
291
292         ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
293
294         return instance;
295 }
296
297 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
298 {
299         instance->data = data;
300 }
301
302 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
303 {
304         return instance->data;
305 }
306
307 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
308 {
309         return instance->engine->write(instance, frame);
310 }
311
312 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
313 {
314         return instance->engine->read(instance, rtcp);
315 }
316
317 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
318                 const struct ast_sockaddr *address)
319 {
320         ast_sockaddr_copy(&instance->local_address, address);
321         return 0;
322 }
323
324 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
325                 const struct ast_sockaddr *address)
326 {
327         ast_sockaddr_copy(&instance->remote_address, address);
328
329         /* moo */
330
331         if (instance->engine->remote_address_set) {
332                 instance->engine->remote_address_set(instance, &instance->remote_address);
333         }
334
335         return 0;
336 }
337
338 int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
339                 const struct ast_sockaddr *address)
340 {
341         ast_sockaddr_copy(&instance->alt_remote_address, address);
342
343         /* oink */
344
345         if (instance->engine->alt_remote_address_set) {
346                 instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
347         }
348
349         return 0;
350 }
351
352 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
353                 struct ast_sockaddr *address)
354 {
355         if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
356                 ast_sockaddr_copy(address, &instance->local_address);
357                 return 1;
358         }
359
360         return 0;
361 }
362
363 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
364                 struct ast_sockaddr *address)
365 {
366         ast_sockaddr_copy(address, &instance->local_address);
367 }
368
369 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
370                 struct ast_sockaddr *address)
371 {
372         if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
373                 ast_sockaddr_copy(address, &instance->remote_address);
374                 return 1;
375         }
376
377         return 0;
378 }
379
380 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
381                 struct ast_sockaddr *address)
382 {
383         ast_sockaddr_copy(address, &instance->remote_address);
384 }
385
386 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
387 {
388         if (instance->engine->extended_prop_set) {
389                 instance->engine->extended_prop_set(instance, property, value);
390         }
391 }
392
393 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
394 {
395         if (instance->engine->extended_prop_get) {
396                 return instance->engine->extended_prop_get(instance, property);
397         }
398
399         return NULL;
400 }
401
402 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
403 {
404         instance->properties[property] = value;
405
406         if (instance->engine->prop_set) {
407                 instance->engine->prop_set(instance, property, value);
408         }
409 }
410
411 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
412 {
413         return instance->properties[property];
414 }
415
416 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
417 {
418         return &instance->codecs;
419 }
420
421 static int rtp_payload_type_hash(const void *obj, const int flags)
422 {
423         const struct ast_rtp_payload_type *type = obj;
424         const int *rtp_code = obj;
425
426         return (flags & OBJ_KEY) ? *rtp_code : type->rtp_code;
427 }
428
429 static int rtp_payload_type_cmp(void *obj, void *arg, int flags)
430 {
431         struct ast_rtp_payload_type *type1 = obj, *type2 = arg;
432         const int *rtp_code = arg;
433
434         return (type1->rtp_code == (OBJ_KEY ? *rtp_code : type2->rtp_code)) ? CMP_MATCH | CMP_STOP : 0;
435 }
436
437 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
438 {
439         if (!(codecs->payloads = ao2_container_alloc(AST_RTP_MAX_PT, rtp_payload_type_hash, rtp_payload_type_cmp))) {
440                 return -1;
441         }
442
443         return 0;
444 }
445
446 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
447 {
448         ao2_cleanup(codecs->payloads);
449 }
450
451 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
452 {
453         ast_rtp_codecs_payloads_destroy(codecs);
454
455         if (instance && instance->engine && instance->engine->payload_set) {
456                 int i;
457                 for (i = 0; i < AST_RTP_MAX_PT; i++) {
458                         instance->engine->payload_set(instance, i, 0, NULL, 0);
459                 }
460         }
461
462         ast_rtp_codecs_payloads_initialize(codecs);
463 }
464
465 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
466 {
467         int i;
468
469         ast_rwlock_rdlock(&static_RTP_PT_lock);
470         for (i = 0; i < AST_RTP_MAX_PT; i++) {
471                 if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
472                         struct ast_rtp_payload_type *type;
473
474                         if (!(type = ao2_alloc(sizeof(*type), NULL))) {
475                                 /* Unfortunately if this occurs the payloads container will not contain all possible default payloads
476                                  * but we err on the side of doing what we can in the hopes that the extreme memory conditions which
477                                  * caused this to occur will go away.
478                                  */
479                                 continue;
480                         }
481
482                         type->asterisk_format = static_RTP_PT[i].asterisk_format;
483                         type->rtp_code = static_RTP_PT[i].rtp_code;
484                         ast_format_copy(&type->format, &static_RTP_PT[i].format);
485
486                         ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
487
488                         if (instance && instance->engine && instance->engine->payload_set) {
489                                 instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
490                         }
491
492                         ao2_ref(type, -1);
493                 }
494         }
495         ast_rwlock_unlock(&static_RTP_PT_lock);
496 }
497
498 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
499 {
500         int i;
501         struct ast_rtp_payload_type *type;
502
503         for (i = 0; i < AST_RTP_MAX_PT; i++) {
504                 struct ast_rtp_payload_type *new_type;
505
506                 if (!(type = ao2_find(src->payloads, &i, OBJ_KEY | OBJ_NOLOCK))) {
507                         continue;
508                 }
509
510                 if (!(new_type = ao2_alloc(sizeof(*new_type), NULL))) {
511                         continue;
512                 }
513
514                 ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
515
516                 *new_type = *type;
517
518                 ao2_link_flags(dest->payloads, new_type, OBJ_NOLOCK);
519
520                 ao2_ref(new_type, -1);
521
522                 if (instance && instance->engine && instance->engine->payload_set) {
523                         instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
524                 }
525
526                 ao2_ref(type, -1);
527         }
528 }
529
530 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
531 {
532         struct ast_rtp_payload_type *type;
533
534         ast_rwlock_rdlock(&static_RTP_PT_lock);
535         if (payload < 0 || payload >= AST_RTP_MAX_PT || !(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
536                 ast_rwlock_unlock(&static_RTP_PT_lock);
537                 return;
538         }
539
540         type->asterisk_format = static_RTP_PT[payload].asterisk_format;
541         type->rtp_code = static_RTP_PT[payload].rtp_code;
542         ast_format_copy(&type->format, &static_RTP_PT[payload].format);
543
544         ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
545
546         if (instance && instance->engine && instance->engine->payload_set) {
547                 instance->engine->payload_set(instance, payload, type->asterisk_format, &type->format, type->rtp_code);
548         }
549
550         ao2_ref(type, -1);
551
552         ast_rwlock_unlock(&static_RTP_PT_lock);
553 }
554
555 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
556                                  char *mimetype, char *mimesubtype,
557                                  enum ast_rtp_options options,
558                                  unsigned int sample_rate)
559 {
560         unsigned int i;
561         int found = 0;
562
563         if (pt < 0 || pt >= AST_RTP_MAX_PT)
564                 return -1; /* bogus payload type */
565
566         ast_rwlock_rdlock(&mime_types_lock);
567         for (i = 0; i < mime_types_len; ++i) {
568                 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
569                 struct ast_rtp_payload_type *type;
570
571                 if (strcasecmp(mimesubtype, t->subtype)) {
572                         continue;
573                 }
574
575                 if (strcasecmp(mimetype, t->type)) {
576                         continue;
577                 }
578
579                 /* if both sample rates have been supplied, and they don't match,
580                  * then this not a match; if one has not been supplied, then the
581                  * rates are not compared */
582                 if (sample_rate && t->sample_rate &&
583                     (sample_rate != t->sample_rate)) {
584                         continue;
585                 }
586
587                 found = 1;
588
589                 if (!(type = ao2_find(codecs->payloads, &pt, OBJ_KEY | OBJ_NOLOCK))) {
590                         if (!(type = ao2_alloc(sizeof(*type), NULL))) {
591                                 continue;
592                         }
593                         type->rtp_code = pt;
594                         ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
595                 }
596
597                 *type = t->payload_type;
598                 type->rtp_code = pt;
599
600                 if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
601                         ast_format_set(&type->format, AST_FORMAT_G726_AAL2, 0);
602                 }
603
604                 if (instance && instance->engine && instance->engine->payload_set) {
605                         instance->engine->payload_set(instance, pt, type->asterisk_format, &type->format, type->rtp_code);
606                 }
607
608                 ao2_ref(type, -1);
609
610                 break;
611         }
612         ast_rwlock_unlock(&mime_types_lock);
613
614         return (found ? 0 : -2);
615 }
616
617 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)
618 {
619         return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
620 }
621
622 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
623 {
624         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
625                 return;
626         }
627
628         ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
629
630         ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK | OBJ_NODATA | OBJ_UNLINK);
631
632         if (instance && instance->engine && instance->engine->payload_set) {
633                 instance->engine->payload_set(instance, payload, 0, NULL, 0);
634         }
635 }
636
637 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
638 {
639         struct ast_rtp_payload_type result = { .asterisk_format = 0, }, *type;
640
641         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
642                 return result;
643         }
644
645         if ((type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
646                 result = *type;
647                 ao2_ref(type, -1);
648         }
649
650         if (!result.rtp_code && !result.asterisk_format) {
651                 ast_rwlock_rdlock(&static_RTP_PT_lock);
652                 result = static_RTP_PT[payload];
653                 ast_rwlock_unlock(&static_RTP_PT_lock);
654         }
655
656         return result;
657 }
658
659
660 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
661 {
662         struct ast_rtp_payload_type *type;
663         struct ast_format *format;
664
665         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
666                 return NULL;
667         }
668
669         if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
670                 return NULL;
671         }
672
673         format = type->asterisk_format ? &type->format : NULL;
674
675         ao2_ref(type, -1);
676
677         return format;
678 }
679
680 static int rtp_payload_type_add_ast(void *obj, void *arg, int flags)
681 {
682         struct ast_rtp_payload_type *type = obj;
683         struct ast_format_cap *astformats = arg;
684
685         if (type->asterisk_format) {
686                 ast_format_cap_add(astformats, &type->format);
687         }
688
689         return 0;
690 }
691
692 static int rtp_payload_type_add_nonast(void *obj, void *arg, int flags)
693 {
694         struct ast_rtp_payload_type *type = obj;
695         int *nonastformats = arg;
696
697         if (!type->asterisk_format) {
698                 *nonastformats |= type->rtp_code;
699         }
700
701         return 0;
702 }
703
704 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
705 {
706         ast_format_cap_remove_all(astformats);
707         *nonastformats = 0;
708
709         ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_ast, astformats);
710         ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_nonast, nonastformats);
711 }
712
713 static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
714 {
715         struct ast_rtp_payload_type *type = obj;
716         struct ast_format *format = arg;
717
718         return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
719 }
720
721 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
722 {
723         struct ast_rtp_payload_type *type;
724         int i, res = -1;
725
726         if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
727                 res = type->rtp_code;
728                 ao2_ref(type, -1);
729                 return res;
730         } else if (!asterisk_format && (type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY))) {
731                 res = type->rtp_code;
732                 ao2_ref(type, -1);
733                 return res;
734         }
735
736         ast_rwlock_rdlock(&static_RTP_PT_lock);
737         for (i = 0; i < AST_RTP_MAX_PT; i++) {
738                 if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
739                         (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
740                         res = i;
741                         break;
742                 } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
743                         (static_RTP_PT[i].rtp_code == code)) {
744                         res = i;
745                         break;
746                 }
747         }
748         ast_rwlock_unlock(&static_RTP_PT_lock);
749
750         return res;
751 }
752
753 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
754 {
755         int i;
756         const char *res = "";
757
758         ast_rwlock_rdlock(&mime_types_lock);
759         for (i = 0; i < mime_types_len; i++) {
760                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
761                         (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
762                         if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
763                                 res = "G726-32";
764                                 break;
765                         } else {
766                                 res = ast_rtp_mime_types[i].subtype;
767                                 break;
768                         }
769                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
770                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
771
772                         res = ast_rtp_mime_types[i].subtype;
773                         break;
774                 }
775         }
776         ast_rwlock_unlock(&mime_types_lock);
777
778         return res;
779 }
780
781 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
782 {
783         unsigned int i;
784         unsigned int res = 0;
785
786         ast_rwlock_rdlock(&mime_types_lock);
787         for (i = 0; i < mime_types_len; ++i) {
788                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
789                         (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
790                         res = ast_rtp_mime_types[i].sample_rate;
791                         break;
792                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
793                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
794                         res = ast_rtp_mime_types[i].sample_rate;
795                         break;
796                 }
797         }
798         ast_rwlock_unlock(&mime_types_lock);
799
800         return res;
801 }
802
803 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)
804 {
805         int found = 0;
806         const char *name;
807         if (!buf) {
808                 return NULL;
809         }
810
811
812         if (asterisk_format) {
813                 struct ast_format tmp_fmt;
814                 ast_format_cap_iter_start(ast_format_capability);
815                 while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
816                         name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
817                         ast_str_append(&buf, 0, "%s|", name);
818                         found = 1;
819                 }
820                 ast_format_cap_iter_end(ast_format_capability);
821
822         } else {
823                 int x;
824                 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
825                 for (x = 1; x < AST_RTP_MAX; x <<= 1) {
826                         if (rtp_capability & x) {
827                                 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
828                                 ast_str_append(&buf, 0, "%s|", name);
829                                 found = 1;
830                         }
831                 }
832         }
833
834         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
835
836         return ast_str_buffer(buf);
837 }
838
839 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
840 {
841         codecs->pref = *prefs;
842
843         if (instance && instance->engine->packetization_set) {
844                 instance->engine->packetization_set(instance, &instance->codecs.pref);
845         }
846 }
847
848 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
849 {
850         return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
851 }
852
853 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
854 {
855         return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
856 }
857 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
858 {
859         return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
860 }
861
862 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
863 {
864         return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
865 }
866
867 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
868 {
869         return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
870 }
871
872 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
873 {
874         if (instance->engine->update_source) {
875                 instance->engine->update_source(instance);
876         }
877 }
878
879 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
880 {
881         if (instance->engine->change_source) {
882                 instance->engine->change_source(instance);
883         }
884 }
885
886 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
887 {
888         return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
889 }
890
891 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
892 {
893         if (instance->engine->stop) {
894                 instance->engine->stop(instance);
895         }
896 }
897
898 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
899 {
900         return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
901 }
902
903 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
904 {
905         struct ast_rtp_glue *glue = NULL;
906
907         AST_RWLIST_RDLOCK(&glues);
908
909         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
910                 if (!strcasecmp(glue->type, type)) {
911                         break;
912                 }
913         }
914
915         AST_RWLIST_UNLOCK(&glues);
916
917         return glue;
918 }
919
920 static enum ast_bridge_result local_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
921 {
922         enum ast_bridge_result res = AST_BRIDGE_FAILED;
923         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
924         struct ast_frame *fr = NULL;
925
926         /* Start locally bridging both instances */
927         if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
928                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
929                 ast_channel_unlock(c0);
930                 ast_channel_unlock(c1);
931                 return AST_BRIDGE_FAILED_NOWARN;
932         }
933         if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
934                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
935                 if (instance0->engine->local_bridge) {
936                         instance0->engine->local_bridge(instance0, NULL);
937                 }
938                 ast_channel_unlock(c0);
939                 ast_channel_unlock(c1);
940                 return AST_BRIDGE_FAILED_NOWARN;
941         }
942
943         ast_channel_unlock(c0);
944         ast_channel_unlock(c1);
945
946         instance0->bridged = instance1;
947         instance1->bridged = instance0;
948
949         ast_poll_channel_add(c0, c1);
950
951         /* Hop into a loop waiting for a frame from either channel */
952         cs[0] = c0;
953         cs[1] = c1;
954         cs[2] = NULL;
955         for (;;) {
956                 /* If the underlying formats have changed force this bridge to break */
957                 if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
958                         (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
959                         ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
960                         res = AST_BRIDGE_FAILED_NOWARN;
961                         break;
962                 }
963                 /* Check if anything changed */
964                 if ((ast_channel_tech_pvt(c0) != pvt0) ||
965                     (ast_channel_tech_pvt(c1) != pvt1) ||
966                     (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
967                     (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
968                     (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
969                         ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
970                         /* If a masquerade needs to happen we have to try to read in a frame so that it actually happens. Without this we risk being called again and going into a loop */
971                         if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
972                                 ast_frfree(fr);
973                         }
974                         if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
975                                 ast_frfree(fr);
976                         }
977                         res = AST_BRIDGE_RETRY;
978                         break;
979                 }
980                 /* Wait on a channel to feed us a frame */
981                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
982                         if (!timeoutms) {
983                                 res = AST_BRIDGE_RETRY;
984                                 break;
985                         }
986                         ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
987                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
988                                 break;
989                         }
990                         continue;
991                 }
992                 /* Read in frame from channel */
993                 fr = ast_read(who);
994                 other = (who == c0) ? c1 : c0;
995                 /* Depending on the frame we may need to break out of our bridge */
996                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
997                             ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
998                             ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
999                         /* Record received frame and who */
1000                         *fo = fr;
1001                         *rc = who;
1002                         ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
1003                         res = AST_BRIDGE_COMPLETE;
1004                         break;
1005                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1006                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1007                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1008                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1009                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1010                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
1011                             (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
1012                                 /* If we are going on hold, then break callback mode and P2P bridging */
1013                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1014                                         if (instance0->engine->local_bridge) {
1015                                                 instance0->engine->local_bridge(instance0, NULL);
1016                                         }
1017                                         if (instance1->engine->local_bridge) {
1018                                                 instance1->engine->local_bridge(instance1, NULL);
1019                                         }
1020                                         instance0->bridged = NULL;
1021                                         instance1->bridged = NULL;
1022                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
1023                                         if (instance0->engine->local_bridge) {
1024                                                 instance0->engine->local_bridge(instance0, instance1);
1025                                         }
1026                                         if (instance1->engine->local_bridge) {
1027                                                 instance1->engine->local_bridge(instance1, instance0);
1028                                         }
1029                                         instance0->bridged = instance1;
1030                                         instance1->bridged = instance0;
1031                                 }
1032                                 /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
1033                                 if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
1034                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1035                                 }
1036                                 ast_frfree(fr);
1037                         } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
1038                                 if (ast_channel_connected_line_sub(who, other, fr, 1) &&
1039                                         ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
1040                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1041                                 }
1042                                 ast_frfree(fr);
1043                         } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
1044                                 if (ast_channel_redirecting_sub(who, other, fr, 1) &&
1045                                         ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
1046                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1047                                 }
1048                                 ast_frfree(fr);
1049                         } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
1050                                 ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
1051                                 ast_frfree(fr);
1052                         } else {
1053                                 *fo = fr;
1054                                 *rc = who;
1055                                 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
1056                                 res = AST_BRIDGE_COMPLETE;
1057                                 break;
1058                         }
1059                 } else {
1060                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1061                             (fr->frametype == AST_FRAME_DTMF_END) ||
1062                             (fr->frametype == AST_FRAME_VOICE) ||
1063                             (fr->frametype == AST_FRAME_VIDEO) ||
1064                             (fr->frametype == AST_FRAME_IMAGE) ||
1065                             (fr->frametype == AST_FRAME_HTML) ||
1066                             (fr->frametype == AST_FRAME_MODEM) ||
1067                             (fr->frametype == AST_FRAME_TEXT)) {
1068                                 ast_write(other, fr);
1069                         }
1070
1071                         ast_frfree(fr);
1072                 }
1073                 /* Swap priority */
1074                 cs[2] = cs[0];
1075                 cs[0] = cs[1];
1076                 cs[1] = cs[2];
1077         }
1078
1079         /* Stop locally bridging both instances */
1080         if (instance0->engine->local_bridge) {
1081                 instance0->engine->local_bridge(instance0, NULL);
1082         }
1083         if (instance1->engine->local_bridge) {
1084                 instance1->engine->local_bridge(instance1, NULL);
1085         }
1086
1087         instance0->bridged = NULL;
1088         instance1->bridged = NULL;
1089
1090         ast_poll_channel_del(c0, c1);
1091
1092         return res;
1093 }
1094
1095 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
1096         struct ast_channel *c1,
1097         struct ast_rtp_instance *instance0,
1098         struct ast_rtp_instance *instance1,
1099         struct ast_rtp_instance *vinstance0,
1100         struct ast_rtp_instance *vinstance1,
1101         struct ast_rtp_instance *tinstance0,
1102         struct ast_rtp_instance *tinstance1,
1103         struct ast_rtp_glue *glue0,
1104         struct ast_rtp_glue *glue1,
1105         struct ast_format_cap *cap0,
1106         struct ast_format_cap *cap1,
1107         int timeoutms,
1108         int flags,
1109         struct ast_frame **fo,
1110         struct ast_channel **rc,
1111         void *pvt0,
1112         void *pvt1)
1113 {
1114         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1115         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
1116         struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
1117         struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
1118         struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
1119         struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
1120         struct ast_frame *fr = NULL;
1121
1122         if (!oldcap0 || !oldcap1) {
1123                 ast_channel_unlock(c0);
1124                 ast_channel_unlock(c1);
1125                 goto remote_bridge_cleanup;
1126         }
1127         /* Test the first channel */
1128         if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
1129                 ast_rtp_instance_get_remote_address(instance1, &ac1);
1130                 if (vinstance1) {
1131                         ast_rtp_instance_get_remote_address(vinstance1, &vac1);
1132                 }
1133                 if (tinstance1) {
1134                         ast_rtp_instance_get_remote_address(tinstance1, &tac1);
1135                 }
1136         } else {
1137                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1138         }
1139
1140         /* Test the second channel */
1141         if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
1142                 ast_rtp_instance_get_remote_address(instance0, &ac0);
1143                 if (vinstance0) {
1144                         ast_rtp_instance_get_remote_address(instance0, &vac0);
1145                 }
1146                 if (tinstance0) {
1147                         ast_rtp_instance_get_remote_address(instance0, &tac0);
1148                 }
1149         } else {
1150                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1151         }
1152
1153         ast_channel_unlock(c0);
1154         ast_channel_unlock(c1);
1155
1156         instance0->bridged = instance1;
1157         instance1->bridged = instance0;
1158
1159         ast_poll_channel_add(c0, c1);
1160
1161         /* Go into a loop handling any stray frames that may come in */
1162         cs[0] = c0;
1163         cs[1] = c1;
1164         cs[2] = NULL;
1165         for (;;) {
1166                 /* Check if anything changed */
1167                 if ((ast_channel_tech_pvt(c0) != pvt0) ||
1168                     (ast_channel_tech_pvt(c1) != pvt1) ||
1169                     (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
1170                     (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
1171                     (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
1172                         ast_debug(1, "Oooh, something is weird, backing out\n");
1173                         res = AST_BRIDGE_RETRY;
1174                         break;
1175                 }
1176
1177                 /* Check if they have changed their address */
1178                 ast_rtp_instance_get_remote_address(instance1, &t1);
1179                 if (vinstance1) {
1180                         ast_rtp_instance_get_remote_address(vinstance1, &vt1);
1181                 }
1182                 if (tinstance1) {
1183                         ast_rtp_instance_get_remote_address(tinstance1, &tt1);
1184                 }
1185                 if (glue1->get_codec) {
1186                         ast_format_cap_remove_all(cap1);
1187                         glue1->get_codec(c1, cap1);
1188                 }
1189
1190                 ast_rtp_instance_get_remote_address(instance0, &t0);
1191                 if (vinstance0) {
1192                         ast_rtp_instance_get_remote_address(vinstance0, &vt0);
1193                 }
1194                 if (tinstance0) {
1195                         ast_rtp_instance_get_remote_address(tinstance0, &tt0);
1196                 }
1197                 if (glue0->get_codec) {
1198                         ast_format_cap_remove_all(cap0);
1199                         glue0->get_codec(c0, cap0);
1200                 }
1201
1202                 if ((ast_sockaddr_cmp(&t1, &ac1)) ||
1203                     (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
1204                     (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
1205                     (!ast_format_cap_identical(cap1, oldcap1))) {
1206                         char tmp_buf[512] = { 0, };
1207                         ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1208                                   ast_channel_name(c1), ast_sockaddr_stringify(&t1),
1209                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1210                         ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
1211                                   ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
1212                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1213                         ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
1214                                   ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
1215                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1216                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1217                                   ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
1218                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1219                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1220                                   ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
1221                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1222                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1223                                   ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
1224                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1225                         if (glue0->update_peer(c0,
1226                                                ast_sockaddr_isnull(&t1)  ? NULL : instance1,
1227                                                ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
1228                                                ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
1229                                                cap1, 0)) {
1230                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1231                         }
1232                         ast_sockaddr_copy(&ac1, &t1);
1233                         ast_sockaddr_copy(&vac1, &vt1);
1234                         ast_sockaddr_copy(&tac1, &tt1);
1235                         ast_format_cap_copy(oldcap1, cap1);
1236                 }
1237                 if ((ast_sockaddr_cmp(&t0, &ac0)) ||
1238                     (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
1239                     (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
1240                     (!ast_format_cap_identical(cap0, oldcap0))) {
1241                         char tmp_buf[512] = { 0, };
1242                         ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1243                                   ast_channel_name(c0), ast_sockaddr_stringify(&t0),
1244                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
1245                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1246                                   ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
1247                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
1248                         if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
1249                                                 vt0.len ? vinstance0 : NULL,
1250                                                 tt0.len ? tinstance0 : NULL,
1251                                                 cap0, 0)) {
1252                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1253                         }
1254                         ast_sockaddr_copy(&ac0, &t0);
1255                         ast_sockaddr_copy(&vac0, &vt0);
1256                         ast_sockaddr_copy(&tac0, &tt0);
1257                         ast_format_cap_copy(oldcap0, cap0);
1258                 }
1259
1260                 /* Wait for frame to come in on the channels */
1261                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1262                         if (!timeoutms) {
1263                                 res = AST_BRIDGE_RETRY;
1264                                 break;
1265                         }
1266                         ast_debug(1, "Ooh, empty read...\n");
1267                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1268                                 break;
1269                         }
1270                         continue;
1271                 }
1272                 fr = ast_read(who);
1273                 other = (who == c0) ? c1 : c0;
1274                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1275                             (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1276                              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1277                         /* Break out of bridge */
1278                         *fo = fr;
1279                         *rc = who;
1280                         ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1281                         res = AST_BRIDGE_COMPLETE;
1282                         break;
1283                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1284                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1285                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1286                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1287                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1288                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
1289                                 (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
1290                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1291                                         /* If we someone went on hold we want the other side to reinvite back to us */
1292                                         if (who == c0) {
1293                                                 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1294                                         } else {
1295                                                 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1296                                         }
1297                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
1298                                         fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
1299                                         /* If they went off hold they should go back to being direct, or if we have
1300                                          * been told to force a peer update, go ahead and do it. */
1301                                         if (who == c0) {
1302                                                 glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
1303                                         } else {
1304                                                 glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
1305                                         }
1306                                 }
1307                                 /* Update local address information */
1308                                 ast_rtp_instance_get_remote_address(instance0, &t0);
1309                                 ast_sockaddr_copy(&ac0, &t0);
1310                                 ast_rtp_instance_get_remote_address(instance1, &t1);
1311                                 ast_sockaddr_copy(&ac1, &t1);
1312                                 /* Update codec information */
1313                                 if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
1314                                         ast_format_cap_remove_all(cap0);
1315                                         ast_format_cap_remove_all(oldcap0);
1316                                         glue0->get_codec(c0, cap0);
1317                                         ast_format_cap_append(oldcap0, cap0);
1318
1319                                 }
1320                                 if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
1321                                         ast_format_cap_remove_all(cap1);
1322                                         ast_format_cap_remove_all(oldcap1);
1323                                         glue1->get_codec(c1, cap1);
1324                                         ast_format_cap_append(oldcap1, cap1);
1325                                 }
1326                                 /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
1327                                 if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
1328                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1329                                 }
1330                                 ast_frfree(fr);
1331                         } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
1332                                 if (ast_channel_connected_line_sub(who, other, fr, 1) &&
1333                                         ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
1334                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1335                                 }
1336                                 ast_frfree(fr);
1337                         } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
1338                                 if (ast_channel_redirecting_sub(who, other, fr, 1) &&
1339                                         ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
1340                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1341                                 }
1342                                 ast_frfree(fr);
1343                         } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
1344                                 ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
1345                                 ast_frfree(fr);
1346                         } else {
1347                                 *fo = fr;
1348                                 *rc = who;
1349                                 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
1350                                 res = AST_BRIDGE_COMPLETE;
1351                                 goto remote_bridge_cleanup;
1352                         }
1353                 } else {
1354                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1355                             (fr->frametype == AST_FRAME_DTMF_END) ||
1356                             (fr->frametype == AST_FRAME_VOICE) ||
1357                             (fr->frametype == AST_FRAME_VIDEO) ||
1358                             (fr->frametype == AST_FRAME_IMAGE) ||
1359                             (fr->frametype == AST_FRAME_HTML) ||
1360                             (fr->frametype == AST_FRAME_MODEM) ||
1361                             (fr->frametype == AST_FRAME_TEXT)) {
1362                                 ast_write(other, fr);
1363                         }
1364                         ast_frfree(fr);
1365                 }
1366                 /* Swap priority */
1367                 cs[2] = cs[0];
1368                 cs[0] = cs[1];
1369                 cs[1] = cs[2];
1370         }
1371
1372         if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
1373                 ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
1374         } else if (ast_channel_tech_pvt(c0) != pvt0) {
1375                 ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1376         } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
1377                 ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1378         } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1379                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
1380         }
1381         if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
1382                 ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
1383         } else if (ast_channel_tech_pvt(c1) != pvt1) {
1384                 ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1385         } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
1386                 ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1387         } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1388                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
1389         }
1390
1391         instance0->bridged = NULL;
1392         instance1->bridged = NULL;
1393
1394         ast_poll_channel_del(c0, c1);
1395
1396 remote_bridge_cleanup:
1397         ast_format_cap_destroy(oldcap0);
1398         ast_format_cap_destroy(oldcap1);
1399
1400         return res;
1401 }
1402
1403 /*!
1404  * \brief Conditionally unref an rtp instance
1405  */
1406 static void unref_instance_cond(struct ast_rtp_instance **instance)
1407 {
1408         if (*instance) {
1409                 ao2_ref(*instance, -1);
1410                 *instance = NULL;
1411         }
1412 }
1413
1414 enum ast_bridge_result ast_rtp_instance_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
1415 {
1416         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1417                         *vinstance0 = NULL, *vinstance1 = NULL,
1418                         *tinstance0 = NULL, *tinstance1 = NULL;
1419         struct ast_rtp_glue *glue0, *glue1;
1420         struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
1421         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1422         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1423         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1424         enum ast_rtp_dtmf_mode dmode;
1425         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1426         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1427         int unlock_chans = 1;
1428
1429         if (!cap0 || !cap1) {
1430                 unlock_chans = 0;
1431                 goto done;
1432         }
1433
1434         /* Lock both channels so we can look for the glue that binds them together */
1435         ast_channel_lock(c0);
1436         while (ast_channel_trylock(c1)) {
1437                 ast_channel_unlock(c0);
1438                 usleep(1);
1439                 ast_channel_lock(c0);
1440         }
1441
1442         /* Ensure neither channel got hungup during lock avoidance */
1443         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1444                 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1445                 goto done;
1446         }
1447
1448         /* Grab glue that binds each channel to something using the RTP engine */
1449         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1450                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1451                 goto done;
1452         }
1453
1454         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1455         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1456
1457         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1458         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1459
1460         /* If the channels are of the same technology, they might have limitations on remote bridging */
1461         if (ast_channel_tech(c0) == ast_channel_tech(c1)) {
1462                 if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
1463                         if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, c1))) {
1464                                 /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
1465                                 audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1466                         }
1467                 }
1468                 if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
1469                         if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, c1))) {
1470                                 /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
1471                                 video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1472                         }
1473                 }
1474         }
1475
1476         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1477         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)) {
1478                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1479         }
1480         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)) {
1481                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1482         }
1483
1484         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1485         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1486                 res = AST_BRIDGE_FAILED_NOWARN;
1487                 goto done;
1488         }
1489
1490
1491         /* If address families differ, force a local bridge */
1492         ast_rtp_instance_get_remote_address(instance0, &addr1);
1493         ast_rtp_instance_get_remote_address(instance1, &addr2);
1494
1495         if (addr1.ss.ss_family != addr2.ss.ss_family ||
1496            (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
1497                 audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
1498                 audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1499         }
1500
1501         /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1502         dmode = ast_rtp_instance_dtmf_mode_get(instance0);
1503         if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
1504                 res = AST_BRIDGE_FAILED_NOWARN;
1505                 goto done;
1506         }
1507         dmode = ast_rtp_instance_dtmf_mode_get(instance1);
1508         if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
1509                 res = AST_BRIDGE_FAILED_NOWARN;
1510                 goto done;
1511         }
1512
1513         /* If we have gotten to a local bridge make sure that both sides have the same local bridge callback and that they are DTMF compatible */
1514         if ((audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) && ((instance0->engine->local_bridge != instance1->engine->local_bridge) || (instance0->engine->dtmf_compatible && !instance0->engine->dtmf_compatible(c0, instance0, c1, instance1)))) {
1515                 res = AST_BRIDGE_FAILED_NOWARN;
1516                 goto done;
1517         }
1518
1519         /* Make sure that codecs match */
1520         if (glue0->get_codec){
1521                 glue0->get_codec(c0, cap0);
1522         }
1523         if (glue1->get_codec) {
1524                 glue1->get_codec(c1, cap1);
1525         }
1526         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
1527                 char tmp0[256] = { 0, };
1528                 char tmp1[256] = { 0, };
1529                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
1530                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
1531                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
1532                 res = AST_BRIDGE_FAILED_NOWARN;
1533                 goto done;
1534         }
1535
1536         instance0->glue = glue0;
1537         instance1->glue = glue1;
1538         instance0->chan = c0;
1539         instance1->chan = c1;
1540
1541         /* Depending on the end result for bridging either do a local bridge or remote bridge */
1542         if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1543                 ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
1544                 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
1545         } else {
1546                 ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
1547                 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1548                                 tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
1549                                 fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
1550         }
1551
1552         instance0->glue = NULL;
1553         instance1->glue = NULL;
1554         instance0->chan = NULL;
1555         instance1->chan = NULL;
1556
1557         unlock_chans = 0;
1558
1559 done:
1560         if (unlock_chans) {
1561                 ast_channel_unlock(c0);
1562                 ast_channel_unlock(c1);
1563         }
1564         ast_format_cap_destroy(cap1);
1565         ast_format_cap_destroy(cap0);
1566
1567         unref_instance_cond(&instance0);
1568         unref_instance_cond(&instance1);
1569         unref_instance_cond(&vinstance0);
1570         unref_instance_cond(&vinstance1);
1571         unref_instance_cond(&tinstance0);
1572         unref_instance_cond(&tinstance1);
1573
1574         return res;
1575 }
1576
1577 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1578 {
1579         return instance->bridged;
1580 }
1581
1582 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1583 {
1584         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1585                 *vinstance0 = NULL, *vinstance1 = NULL,
1586                 *tinstance0 = NULL, *tinstance1 = NULL;
1587         struct ast_rtp_glue *glue0, *glue1;
1588         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1589         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1590         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1591         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1592
1593         /* Lock both channels so we can look for the glue that binds them together */
1594         ast_channel_lock_both(c0, c1);
1595
1596         if (!cap1 || !cap0) {
1597                 goto done;
1598         }
1599
1600         /* Grab glue that binds each channel to something using the RTP engine */
1601         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1602                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1603                 goto done;
1604         }
1605
1606         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1607         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1608
1609         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1610         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1611
1612         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1613         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)) {
1614                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1615         }
1616         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)) {
1617                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1618         }
1619         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) {
1620                 glue0->get_codec(c0, cap0);
1621         }
1622         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) {
1623                 glue1->get_codec(c1, cap1);
1624         }
1625
1626         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1627         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1628                 goto done;
1629         }
1630
1631         /* Make sure we have matching codecs */
1632         if (!ast_format_cap_has_joint(cap0, cap1)) {
1633                 goto done;
1634         }
1635
1636         ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1637
1638         if (vinstance0 && vinstance1) {
1639                 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1640         }
1641         if (tinstance0 && tinstance1) {
1642                 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1643         }
1644
1645         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1646                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
1647                         ast_channel_name(c0), ast_channel_name(c1));
1648         } else {
1649                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
1650                         ast_channel_name(c0), ast_channel_name(c1));
1651         }
1652
1653 done:
1654         ast_channel_unlock(c0);
1655         ast_channel_unlock(c1);
1656
1657         ast_format_cap_destroy(cap0);
1658         ast_format_cap_destroy(cap1);
1659
1660         unref_instance_cond(&instance0);
1661         unref_instance_cond(&instance1);
1662         unref_instance_cond(&vinstance0);
1663         unref_instance_cond(&vinstance1);
1664         unref_instance_cond(&tinstance0);
1665         unref_instance_cond(&tinstance1);
1666 }
1667
1668 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1669 {
1670         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1671                         *vinstance0 = NULL, *vinstance1 = NULL,
1672                         *tinstance0 = NULL, *tinstance1 = NULL;
1673         struct ast_rtp_glue *glue0, *glue1;
1674         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1675         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1676         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1677         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1678         int res = 0;
1679
1680         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1681         if (!c1) {
1682                 ast_format_cap_destroy(cap0);
1683                 ast_format_cap_destroy(cap1);
1684                 return -1;
1685         }
1686
1687         /* Lock both channels so we can look for the glue that binds them together */
1688         ast_channel_lock(c0);
1689         while (ast_channel_trylock(c1)) {
1690                 ast_channel_unlock(c0);
1691                 usleep(1);
1692                 ast_channel_lock(c0);
1693         }
1694
1695         if (!cap1 || !cap0) {
1696                 goto done;
1697         }
1698
1699         /* Grab glue that binds each channel to something using the RTP engine */
1700         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1701                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1702                 goto done;
1703         }
1704
1705         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1706         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1707
1708         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1709         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1710
1711         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1712         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)) {
1713                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1714         }
1715         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)) {
1716                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1717         }
1718         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) {
1719                 glue0->get_codec(c0, cap0);
1720         }
1721         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) {
1722                 glue1->get_codec(c1, cap1);
1723         }
1724
1725         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1726         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1727                 goto done;
1728         }
1729
1730         /* Make sure we have matching codecs */
1731         if (!ast_format_cap_has_joint(cap0, cap1)) {
1732                 goto done;
1733         }
1734
1735         /* Bridge media early */
1736         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1737                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1738         }
1739
1740         res = 0;
1741
1742 done:
1743         ast_channel_unlock(c0);
1744         ast_channel_unlock(c1);
1745
1746         ast_format_cap_destroy(cap0);
1747         ast_format_cap_destroy(cap1);
1748
1749         unref_instance_cond(&instance0);
1750         unref_instance_cond(&instance1);
1751         unref_instance_cond(&vinstance0);
1752         unref_instance_cond(&vinstance1);
1753         unref_instance_cond(&tinstance0);
1754         unref_instance_cond(&tinstance1);
1755
1756         if (!res) {
1757                 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1758         }
1759
1760         return res;
1761 }
1762
1763 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1764 {
1765         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1766 }
1767
1768 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1769 {
1770         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1771 }
1772
1773 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1774 {
1775         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1776 }
1777
1778 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1779 {
1780         struct ast_rtp_instance_stats stats = { 0, };
1781         enum ast_rtp_instance_stat stat;
1782
1783         /* Determine what statistics we will need to retrieve based on field passed in */
1784         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1785                 stat = AST_RTP_INSTANCE_STAT_ALL;
1786         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1787                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1788         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1789                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1790         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1791                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1792         } else {
1793                 return NULL;
1794         }
1795
1796         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1797         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1798                 return NULL;
1799         }
1800
1801         /* Now actually fill the buffer with the good information */
1802         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1803                 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1804                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1805         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1806                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1807                          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));
1808         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1809                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1810                          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));
1811         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1812                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1813         }
1814
1815         return buf;
1816 }
1817
1818 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1819 {
1820         char quality_buf[AST_MAX_USER_FIELD], *quality;
1821         struct ast_channel *bridge = ast_bridged_channel(chan);
1822
1823         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1824                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1825                 if (bridge) {
1826                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1827                 }
1828         }
1829
1830         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1831                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1832                 if (bridge) {
1833                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1834                 }
1835         }
1836
1837         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1838                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1839                 if (bridge) {
1840                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1841                 }
1842         }
1843
1844         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1845                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1846                 if (bridge) {
1847                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1848                 }
1849         }
1850 }
1851
1852 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1853 {
1854         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1855 }
1856
1857 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1858 {
1859         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1860 }
1861
1862 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1863 {
1864         struct ast_rtp_glue *glue;
1865         struct ast_rtp_instance *peer_instance = NULL;
1866         int res = -1;
1867
1868         if (!instance->engine->make_compatible) {
1869                 return -1;
1870         }
1871
1872         ast_channel_lock(peer);
1873
1874         if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
1875                 ast_channel_unlock(peer);
1876                 return -1;
1877         }
1878
1879         glue->get_rtp_info(peer, &peer_instance);
1880
1881         if (!peer_instance || peer_instance->engine != instance->engine) {
1882                 ast_channel_unlock(peer);
1883                 ao2_ref(peer_instance, -1);
1884                 peer_instance = NULL;
1885                 return -1;
1886         }
1887
1888         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1889
1890         ast_channel_unlock(peer);
1891
1892         ao2_ref(peer_instance, -1);
1893         peer_instance = NULL;
1894
1895         return res;
1896 }
1897
1898 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)
1899 {
1900         if (instance->engine->available_formats) {
1901                 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1902                 if (!ast_format_cap_is_empty(result)) {
1903                         return;
1904                 }
1905         }
1906
1907         ast_translate_available_formats(to_endpoint, to_asterisk, result);
1908 }
1909
1910 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1911 {
1912         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1913 }
1914
1915 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1916                                    struct ast_sockaddr *suggestion,
1917                                    const char *username)
1918 {
1919         if (instance->engine->stun_request) {
1920                 instance->engine->stun_request(instance, suggestion, username);
1921         }
1922 }
1923
1924 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1925 {
1926         instance->timeout = timeout;
1927 }
1928
1929 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1930 {
1931         instance->holdtimeout = timeout;
1932 }
1933
1934 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
1935 {
1936         instance->keepalive = interval;
1937 }
1938
1939 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1940 {
1941         return instance->timeout;
1942 }
1943
1944 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1945 {
1946         return instance->holdtimeout;
1947 }
1948
1949 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
1950 {
1951         return instance->keepalive;
1952 }
1953
1954 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1955 {
1956         return instance->engine;
1957 }
1958
1959 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1960 {
1961         return instance->glue;
1962 }
1963
1964 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
1965 {
1966         return instance->chan;
1967 }
1968
1969 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1970 {
1971         if (res_srtp || res_srtp_policy) {
1972                 return -1;
1973         }
1974         if (!srtp_res || !policy_res) {
1975                 return -1;
1976         }
1977
1978         res_srtp = srtp_res;
1979         res_srtp_policy = policy_res;
1980
1981         return 0;
1982 }
1983
1984 void ast_rtp_engine_unregister_srtp(void)
1985 {
1986         res_srtp = NULL;
1987         res_srtp_policy = NULL;
1988 }
1989
1990 int ast_rtp_engine_srtp_is_registered(void)
1991 {
1992         return res_srtp && res_srtp_policy;
1993 }
1994
1995 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
1996 {
1997         int res = 0;
1998
1999         if (!res_srtp) {
2000                 return -1;
2001         }
2002
2003         if (!instance->srtp) {
2004                 res = res_srtp->create(&instance->srtp, instance, remote_policy);
2005         } else {
2006                 res = res_srtp->replace(&instance->srtp, instance, remote_policy);
2007         }
2008         if (!res) {
2009                 res = res_srtp->add_stream(instance->srtp, local_policy);
2010         }
2011
2012         return res;
2013 }
2014
2015 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
2016 {
2017         return instance->srtp;
2018 }
2019
2020 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
2021 {
2022         if (instance->engine->sendcng) {
2023                 return instance->engine->sendcng(instance, level);
2024         }
2025
2026         return -1;
2027 }
2028
2029 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
2030 {
2031         return instance->engine->ice;
2032 }
2033
2034 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
2035 {
2036         int x = mime_types_len;
2037         if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
2038                 return;
2039         }
2040
2041         ast_rwlock_wrlock(&mime_types_lock);
2042         if (format) {
2043                 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
2044                 ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
2045         } else {
2046                 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
2047         }
2048         ast_rtp_mime_types[x].type = type;
2049         ast_rtp_mime_types[x].subtype = subtype;
2050         ast_rtp_mime_types[x].sample_rate = sample_rate;
2051         mime_types_len++;
2052         ast_rwlock_unlock(&mime_types_lock);
2053 }
2054
2055 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
2056 {
2057         int x;
2058         ast_rwlock_wrlock(&static_RTP_PT_lock);
2059         if (map < 0) {
2060                 /* find next available dynamic payload slot */
2061                 for (x = 96; x < 127; x++) {
2062                         if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
2063                                 map = x;
2064                                 break;
2065                         }
2066                 }
2067         }
2068
2069         if (map < 0) {
2070                 ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
2071                 ast_rwlock_unlock(&static_RTP_PT_lock);
2072                 return;
2073         }
2074
2075         if (format) {
2076                 static_RTP_PT[map].asterisk_format = 1;
2077                 ast_format_copy(&static_RTP_PT[map].format, format);
2078         } else {
2079                 static_RTP_PT[map].rtp_code = rtp_code;
2080         }
2081         ast_rwlock_unlock(&static_RTP_PT_lock);
2082 }
2083
2084 int ast_rtp_engine_load_format(const struct ast_format *format)
2085 {
2086         switch (format->id) {
2087         case AST_FORMAT_SILK:
2088                 set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
2089                 add_static_payload(-1, format, 0);
2090                 break;
2091         case AST_FORMAT_CELT:
2092                 set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
2093                 add_static_payload(-1, format, 0);
2094                 break;
2095         default:
2096                 break;
2097         }
2098
2099         return 0;
2100 }
2101
2102 int ast_rtp_engine_unload_format(const struct ast_format *format)
2103 {
2104         int x;
2105         int y = 0;
2106
2107         ast_rwlock_wrlock(&static_RTP_PT_lock);
2108         /* remove everything pertaining to this format id from the lists */
2109         for (x = 0; x < AST_RTP_MAX_PT; x++) {
2110                 if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
2111                         memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
2112                 }
2113         }
2114         ast_rwlock_unlock(&static_RTP_PT_lock);
2115
2116
2117         ast_rwlock_wrlock(&mime_types_lock);
2118         /* rebuild the list skipping the items matching this id */
2119         for (x = 0; x < mime_types_len; x++) {
2120                 if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
2121                         continue;
2122                 }
2123                 ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
2124                 y++;
2125         }
2126         mime_types_len = y;
2127         ast_rwlock_unlock(&mime_types_lock);
2128         return 0;
2129 }
2130
2131 int ast_rtp_engine_init()
2132 {
2133         struct ast_format tmpfmt;
2134
2135         ast_rwlock_init(&mime_types_lock);
2136         ast_rwlock_init(&static_RTP_PT_lock);
2137
2138         /* Define all the RTP mime types available */
2139         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
2140         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
2141         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
2142         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
2143         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
2144         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
2145         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
2146         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
2147         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
2148         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
2149         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
2150         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
2151         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
2152         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
2153         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
2154         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
2155         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0,  "audio", "speex", 16000);
2156         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0,  "audio", "speex", 32000);
2157         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
2158         /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
2159         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
2160         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
2161         set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
2162         set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
2163         set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
2164         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
2165         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
2166         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
2167         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
2168         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
2169         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
2170         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
2171         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
2172         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
2173         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
2174         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
2175         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
2176
2177         /* Define the static rtp payload mappings */
2178         add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
2179         #ifdef USE_DEPRECATED_G726
2180         add_static_payload(2, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
2181         #endif
2182         add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
2183         add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
2184         add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
2185         add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
2186         add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
2187         add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
2188         add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
2189         add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
2190         add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
2191         add_static_payload(13, NULL, AST_RTP_CN);
2192         add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
2193         add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
2194         add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
2195         add_static_payload(19, NULL, AST_RTP_CN);         /* Also used for CN */
2196         add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
2197         add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
2198         add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
2199         add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
2200         add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2201         add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
2202         add_static_payload(101, NULL, AST_RTP_DTMF);
2203         add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
2204         add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2205         add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
2206         add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0);   /* Real time text chat (with redundancy encoding) */
2207         add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0);     /* Real time text chat */
2208         add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
2209         add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
2210         add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
2211         add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
2212         add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
2213         add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
2214         add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
2215         add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
2216         add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
2217
2218         return 0;
2219 }