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