Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio...
[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 (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1238                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
1239         }
1240         if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1241                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
1242         }
1243
1244         instance0->bridged = NULL;
1245         instance1->bridged = NULL;
1246
1247         ast_poll_channel_del(c0, c1);
1248
1249 remote_bridge_cleanup:
1250         ast_format_cap_destroy(oldcap0);
1251         ast_format_cap_destroy(oldcap1);
1252
1253         return res;
1254 }
1255
1256 /*!
1257  * \brief Conditionally unref an rtp instance
1258  */
1259 static void unref_instance_cond(struct ast_rtp_instance **instance)
1260 {
1261         if (*instance) {
1262                 ao2_ref(*instance, -1);
1263                 *instance = NULL;
1264         }
1265 }
1266
1267 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)
1268 {
1269         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1270                         *vinstance0 = NULL, *vinstance1 = NULL,
1271                         *tinstance0 = NULL, *tinstance1 = NULL;
1272         struct ast_rtp_glue *glue0, *glue1;
1273         struct ast_sockaddr addr1, addr2;
1274         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1275         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1276         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1277         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1278         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1279         int unlock_chans = 1;
1280
1281         if (!cap0 || !cap1) {
1282                 unlock_chans = 0;
1283                 goto done;
1284         }
1285
1286         /* Lock both channels so we can look for the glue that binds them together */
1287         ast_channel_lock(c0);
1288         while (ast_channel_trylock(c1)) {
1289                 ast_channel_unlock(c0);
1290                 usleep(1);
1291                 ast_channel_lock(c0);
1292         }
1293
1294         /* Ensure neither channel got hungup during lock avoidance */
1295         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1296                 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
1297                 goto done;
1298         }
1299
1300         /* Grab glue that binds each channel to something using the RTP engine */
1301         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1302                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1303                 goto done;
1304         }
1305
1306         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1307         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1308         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1309
1310         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1311         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1312         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1313
1314         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1315         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)) {
1316                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1317         }
1318         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)) {
1319                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1320         }
1321
1322         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1323         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1324                 res = AST_BRIDGE_FAILED_NOWARN;
1325                 goto done;
1326         }
1327
1328
1329         /* If address families differ, force a local bridge */
1330         ast_rtp_instance_get_remote_address(instance0, &addr1);
1331         ast_rtp_instance_get_remote_address(instance1, &addr2);
1332
1333         if (addr1.ss.ss_family != addr2.ss.ss_family ||
1334            (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
1335                 audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
1336                 audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1337         }
1338
1339         /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1340         if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && instance0->properties[AST_RTP_PROPERTY_DTMF]) {
1341                 res = AST_BRIDGE_FAILED_NOWARN;
1342                 goto done;
1343         }
1344         if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && instance1->properties[AST_RTP_PROPERTY_DTMF]) {
1345                 res = AST_BRIDGE_FAILED_NOWARN;
1346                 goto done;
1347         }
1348
1349         /* 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 */
1350         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)))) {
1351                 res = AST_BRIDGE_FAILED_NOWARN;
1352                 goto done;
1353         }
1354
1355         /* Make sure that codecs match */
1356         if (glue0->get_codec){
1357                 glue0->get_codec(c0, cap0);
1358         }
1359         if (glue1->get_codec) {
1360                 glue1->get_codec(c1, cap1);
1361         }
1362         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
1363                 char tmp0[256] = { 0, };
1364                 char tmp1[256] = { 0, };
1365                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
1366                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
1367                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
1368                 res = AST_BRIDGE_FAILED_NOWARN;
1369                 goto done;
1370         }
1371
1372         instance0->glue = glue0;
1373         instance1->glue = glue1;
1374         instance0->chan = c0;
1375         instance1->chan = c1;
1376
1377         /* Depending on the end result for bridging either do a local bridge or remote bridge */
1378         if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1379                 ast_verbose(VERBOSE_PREFIX_3 "Locally bridging %s and %s\n", c0->name, c1->name);
1380                 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
1381         } else {
1382                 ast_verbose(VERBOSE_PREFIX_3 "Remotely bridging %s and %s\n", c0->name, c1->name);
1383                 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1384                                 tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
1385                                 fo, rc, c0->tech_pvt, c1->tech_pvt);
1386         }
1387
1388         instance0->glue = NULL;
1389         instance1->glue = NULL;
1390         instance0->chan = NULL;
1391         instance1->chan = NULL;
1392
1393         unlock_chans = 0;
1394
1395 done:
1396         if (unlock_chans) {
1397                 ast_channel_unlock(c0);
1398                 ast_channel_unlock(c1);
1399         }
1400         ast_format_cap_destroy(cap1);
1401         ast_format_cap_destroy(cap0);
1402
1403         unref_instance_cond(&instance0);
1404         unref_instance_cond(&instance1);
1405         unref_instance_cond(&vinstance0);
1406         unref_instance_cond(&vinstance1);
1407         unref_instance_cond(&tinstance0);
1408         unref_instance_cond(&tinstance1);
1409
1410         return res;
1411 }
1412
1413 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1414 {
1415         return instance->bridged;
1416 }
1417
1418 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1419 {
1420         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1421                 *vinstance0 = NULL, *vinstance1 = NULL,
1422                 *tinstance0 = NULL, *tinstance1 = NULL;
1423         struct ast_rtp_glue *glue0, *glue1;
1424         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1425         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1426         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1427         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1428         int res = 0;
1429
1430         /* Lock both channels so we can look for the glue that binds them together */
1431         ast_channel_lock(c0);
1432         while (ast_channel_trylock(c1)) {
1433                 ast_channel_unlock(c0);
1434                 usleep(1);
1435                 ast_channel_lock(c0);
1436         }
1437
1438         if (!cap1 || !cap0) {
1439                 goto done;
1440         }
1441
1442         /* Grab glue that binds each channel to something using the RTP engine */
1443         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1444                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1445                 goto done;
1446         }
1447
1448         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1449         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1450         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1451
1452         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1453         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1454         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1455
1456         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1457         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)) {
1458                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1459         }
1460         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)) {
1461                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1462         }
1463         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) {
1464                 glue0->get_codec(c0, cap0);
1465         }
1466         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) {
1467                 glue1->get_codec(c1, cap1);
1468         }
1469
1470         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1471         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1472                 goto done;
1473         }
1474
1475         /* Make sure we have matching codecs */
1476         if (!ast_format_cap_has_joint(cap0, cap1)) {
1477                 goto done;
1478         }
1479
1480         ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1481
1482         if (vinstance0 && vinstance1) {
1483                 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1484         }
1485         if (tinstance0 && tinstance1) {
1486                 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1487         }
1488
1489         res = 0;
1490
1491 done:
1492         ast_channel_unlock(c0);
1493         ast_channel_unlock(c1);
1494
1495         ast_format_cap_destroy(cap0);
1496         ast_format_cap_destroy(cap1);
1497
1498         unref_instance_cond(&instance0);
1499         unref_instance_cond(&instance1);
1500         unref_instance_cond(&vinstance0);
1501         unref_instance_cond(&vinstance1);
1502         unref_instance_cond(&tinstance0);
1503         unref_instance_cond(&tinstance1);
1504
1505         if (!res) {
1506                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1507         }
1508 }
1509
1510 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1511 {
1512         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1513                         *vinstance0 = NULL, *vinstance1 = NULL,
1514                         *tinstance0 = NULL, *tinstance1 = NULL;
1515         struct ast_rtp_glue *glue0, *glue1;
1516         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1517         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1518         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1519         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1520         int res = 0;
1521
1522         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1523         if (!c1) {
1524                 ast_format_cap_destroy(cap0);
1525                 ast_format_cap_destroy(cap1);
1526                 return -1;
1527         }
1528
1529         /* Lock both channels so we can look for the glue that binds them together */
1530         ast_channel_lock(c0);
1531         while (ast_channel_trylock(c1)) {
1532                 ast_channel_unlock(c0);
1533                 usleep(1);
1534                 ast_channel_lock(c0);
1535         }
1536
1537         if (!cap1 || !cap0) {
1538                 goto done;
1539         }
1540
1541         /* Grab glue that binds each channel to something using the RTP engine */
1542         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1543                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1544                 goto done;
1545         }
1546
1547         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1548         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1549         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1550
1551         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1552         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1553         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1554
1555         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1556         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)) {
1557                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1558         }
1559         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)) {
1560                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1561         }
1562         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) {
1563                 glue0->get_codec(c0, cap0);
1564         }
1565         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) {
1566                 glue1->get_codec(c1, cap1);
1567         }
1568
1569         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1570         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1571                 goto done;
1572         }
1573
1574         /* Make sure we have matching codecs */
1575         if (!ast_format_cap_has_joint(cap0, cap1)) {
1576                 goto done;
1577         }
1578
1579         /* Bridge media early */
1580         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1581                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1582         }
1583
1584         res = 0;
1585
1586 done:
1587         ast_channel_unlock(c0);
1588         ast_channel_unlock(c1);
1589
1590         ast_format_cap_destroy(cap0);
1591         ast_format_cap_destroy(cap1);
1592
1593         unref_instance_cond(&instance0);
1594         unref_instance_cond(&instance1);
1595         unref_instance_cond(&vinstance0);
1596         unref_instance_cond(&vinstance1);
1597         unref_instance_cond(&tinstance0);
1598         unref_instance_cond(&tinstance1);
1599
1600         if (!res) {
1601                 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1602         }
1603
1604         return res;
1605 }
1606
1607 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1608 {
1609         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1610 }
1611
1612 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1613 {
1614         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1615 }
1616
1617 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1618 {
1619         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1620 }
1621
1622 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1623 {
1624         struct ast_rtp_instance_stats stats = { 0, };
1625         enum ast_rtp_instance_stat stat;
1626
1627         /* Determine what statistics we will need to retrieve based on field passed in */
1628         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1629                 stat = AST_RTP_INSTANCE_STAT_ALL;
1630         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1631                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1632         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1633                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1634         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1635                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1636         } else {
1637                 return NULL;
1638         }
1639
1640         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1641         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1642                 return NULL;
1643         }
1644
1645         /* Now actually fill the buffer with the good information */
1646         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1647                 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1648                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1649         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1650                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1651                          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));
1652         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1653                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1654                          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));
1655         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1656                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1657         }
1658
1659         return buf;
1660 }
1661
1662 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1663 {
1664         char quality_buf[AST_MAX_USER_FIELD], *quality;
1665         struct ast_channel *bridge = ast_bridged_channel(chan);
1666
1667         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1668                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1669                 if (bridge) {
1670                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1671                 }
1672         }
1673
1674         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1675                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1676                 if (bridge) {
1677                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1678                 }
1679         }
1680
1681         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1682                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1683                 if (bridge) {
1684                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1685                 }
1686         }
1687
1688         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1689                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1690                 if (bridge) {
1691                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1692                 }
1693         }
1694 }
1695
1696 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1697 {
1698         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1699 }
1700
1701 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1702 {
1703         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1704 }
1705
1706 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1707 {
1708         struct ast_rtp_glue *glue;
1709         struct ast_rtp_instance *peer_instance = NULL;
1710         int res = -1;
1711
1712         if (!instance->engine->make_compatible) {
1713                 return -1;
1714         }
1715
1716         ast_channel_lock(peer);
1717
1718         if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
1719                 ast_channel_unlock(peer);
1720                 return -1;
1721         }
1722
1723         glue->get_rtp_info(peer, &peer_instance);
1724
1725         if (!peer_instance || peer_instance->engine != instance->engine) {
1726                 ast_channel_unlock(peer);
1727                 ao2_ref(peer_instance, -1);
1728                 peer_instance = NULL;
1729                 return -1;
1730         }
1731
1732         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1733
1734         ast_channel_unlock(peer);
1735
1736         ao2_ref(peer_instance, -1);
1737         peer_instance = NULL;
1738
1739         return res;
1740 }
1741
1742 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)
1743 {
1744         if (instance->engine->available_formats) {
1745                 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1746                 if (!ast_format_cap_is_empty(result)) {
1747                         return;
1748                 }
1749         }
1750
1751         ast_translate_available_formats(to_endpoint, to_asterisk, result);
1752 }
1753
1754 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1755 {
1756         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1757 }
1758
1759 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1760                                    struct ast_sockaddr *suggestion,
1761                                    const char *username)
1762 {
1763         if (instance->engine->stun_request) {
1764                 instance->engine->stun_request(instance, suggestion, username);
1765         }
1766 }
1767
1768 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1769 {
1770         instance->timeout = timeout;
1771 }
1772
1773 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1774 {
1775         instance->holdtimeout = timeout;
1776 }
1777
1778 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1779 {
1780         return instance->timeout;
1781 }
1782
1783 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1784 {
1785         return instance->holdtimeout;
1786 }
1787
1788 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1789 {
1790         return instance->engine;
1791 }
1792
1793 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1794 {
1795         return instance->glue;
1796 }
1797
1798 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
1799 {
1800         return instance->chan;
1801 }
1802
1803 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1804 {
1805         if (res_srtp || res_srtp_policy) {
1806                 return -1;
1807         }
1808         if (!srtp_res || !policy_res) {
1809                 return -1;
1810         }
1811
1812         res_srtp = srtp_res;
1813         res_srtp_policy = policy_res;
1814
1815         return 0;
1816 }
1817
1818 void ast_rtp_engine_unregister_srtp(void)
1819 {
1820         res_srtp = NULL;
1821         res_srtp_policy = NULL;
1822 }
1823
1824 int ast_rtp_engine_srtp_is_registered(void)
1825 {
1826         return res_srtp && res_srtp_policy;
1827 }
1828
1829 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *policy)
1830 {
1831         if (!res_srtp) {
1832                 return -1;
1833         }
1834
1835         if (!instance->srtp) {
1836                 return res_srtp->create(&instance->srtp, instance, policy);
1837         } else {
1838                 return res_srtp->add_stream(instance->srtp, policy);
1839         }
1840 }
1841
1842 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
1843 {
1844         return instance->srtp;
1845 }
1846
1847 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
1848 {
1849         int x = mime_types_len;
1850         if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
1851                 return;
1852         }
1853
1854         ast_rwlock_wrlock(&mime_types_lock);
1855         if (format) {
1856                 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
1857                 ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
1858         } else {
1859                 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
1860         }
1861         ast_rtp_mime_types[x].type = type;
1862         ast_rtp_mime_types[x].subtype = subtype;
1863         ast_rtp_mime_types[x].sample_rate = sample_rate;
1864         mime_types_len++;
1865         ast_rwlock_unlock(&mime_types_lock);
1866 }
1867
1868 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
1869 {
1870         int x;
1871         ast_rwlock_wrlock(&static_RTP_PT_lock);
1872         if (map < 0) {
1873                 /* find next available dynamic payload slot */
1874                 for (x = 96; x < 127; x++) {
1875                         if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
1876                                 map = x;
1877                                 break;
1878                         }
1879                 }
1880         }
1881
1882         if (map < 0) {
1883                 ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
1884                 ast_rwlock_unlock(&static_RTP_PT_lock);
1885                 return;
1886         }
1887
1888         if (format) {
1889                 static_RTP_PT[map].asterisk_format = 1;
1890                 ast_format_copy(&static_RTP_PT[map].format, format);
1891         } else {
1892                 static_RTP_PT[map].rtp_code = rtp_code;
1893         }
1894         ast_rwlock_unlock(&static_RTP_PT_lock);
1895 }
1896
1897 int ast_rtp_engine_load_format(const struct ast_format *format)
1898 {
1899         switch (format->id) {
1900         case AST_FORMAT_SILK:
1901                 set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
1902                 add_static_payload(-1, format, 0);
1903                 break;
1904         default:
1905                 break;
1906         }
1907
1908         return 0;
1909 }
1910
1911 int ast_rtp_engine_unload_format(const struct ast_format *format)
1912 {
1913         int x;
1914         int y = 0;
1915
1916         ast_rwlock_wrlock(&static_RTP_PT_lock);
1917         /* remove everything pertaining to this format id from the lists */
1918         for (x = 0; x < AST_RTP_MAX_PT; x++) {
1919                 if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
1920                         memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
1921                 }
1922         }
1923         ast_rwlock_unlock(&static_RTP_PT_lock);
1924
1925
1926         ast_rwlock_wrlock(&mime_types_lock);
1927         /* rebuild the list skipping the items matching this id */
1928         for (x = 0; x < mime_types_len; x++) {
1929                 if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
1930                         continue;
1931                 }
1932                 ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
1933                 y++;
1934         }
1935         mime_types_len = y;
1936         ast_rwlock_unlock(&mime_types_lock);
1937         return 0;
1938 }
1939
1940 int ast_rtp_engine_init()
1941 {
1942         struct ast_format tmpfmt;
1943
1944         ast_rwlock_init(&mime_types_lock);
1945         ast_rwlock_init(&static_RTP_PT_lock);
1946
1947         /* Define all the RTP mime types available */
1948         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
1949         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
1950         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
1951         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
1952         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
1953         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
1954         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
1955         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
1956         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
1957         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
1958         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
1959         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
1960         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
1961         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
1962         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
1963         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0,  "audio", "speex", 16000);
1964         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0,  "audio", "speex", 32000);
1965         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
1966         /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
1967         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
1968         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
1969         set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
1970         set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
1971         set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
1972         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
1973         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
1974         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
1975         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
1976         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
1977         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
1978         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
1979         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
1980         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
1981         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
1982         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
1983         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
1984
1985         /* Define the static rtp payload mappings */
1986         add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
1987         #ifdef USE_DEPRECATED_G726
1988         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... */
1989         #endif
1990         add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
1991         add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
1992         add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
1993         add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
1994         add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
1995         add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
1996         add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
1997         add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
1998         add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
1999         add_static_payload(13, NULL, AST_RTP_CN);
2000         add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
2001         add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
2002         add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
2003         add_static_payload(19, NULL, AST_RTP_CN);         /* Also used for CN */
2004         add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
2005         add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
2006         add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
2007         add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
2008         add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2009         add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
2010         add_static_payload(101, NULL, AST_RTP_DTMF);
2011         add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
2012         add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2013         add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
2014         add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0);   /* Real time text chat (with redundancy encoding) */
2015         add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0);     /* Real time text chat */
2016         add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
2017         add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
2018         add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
2019         add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
2020         add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
2021         add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
2022         add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
2023         add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
2024         add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
2025
2026         return 0;
2027 }