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