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