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