Fix a bug where we were passing in address information that should remain unmodified...
[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
41 /*! Structure that represents an RTP session (instance) */
42 struct ast_rtp_instance {
43         /*! Engine that is handling this RTP instance */
44         struct ast_rtp_engine *engine;
45         /*! Data unique to the RTP engine */
46         void *data;
47         /*! RTP properties that have been set and their value */
48         int properties[AST_RTP_PROPERTY_MAX];
49         /*! Address that we are expecting RTP to come in to */
50         struct sockaddr_in local_address;
51         /*! Address that we are sending RTP to */
52         struct sockaddr_in remote_address;
53         /*! Instance that we are bridged to if doing remote or local bridging */
54         struct ast_rtp_instance *bridged;
55         /*! Payload and packetization information */
56         struct ast_rtp_codecs codecs;
57         /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
58         int timeout;
59         /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
60         int holdtimeout;
61         /*! DTMF mode in use */
62         enum ast_rtp_dtmf_mode dtmf_mode;
63 };
64
65 /*! List of RTP engines that are currently registered */
66 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
67
68 /*! List of RTP glues */
69 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
70
71 /*! The following array defines the MIME Media type (and subtype) for each
72    of our codecs, or RTP-specific data type. */
73 static const struct ast_rtp_mime_type {
74         struct ast_rtp_payload_type payload_type;
75         char *type;
76         char *subtype;
77         unsigned int sample_rate;
78 } ast_rtp_mime_types[] = {
79         {{1, AST_FORMAT_G723_1}, "audio", "G723", 8000},
80         {{1, AST_FORMAT_GSM}, "audio", "GSM", 8000},
81         {{1, AST_FORMAT_ULAW}, "audio", "PCMU", 8000},
82         {{1, AST_FORMAT_ULAW}, "audio", "G711U", 8000},
83         {{1, AST_FORMAT_ALAW}, "audio", "PCMA", 8000},
84         {{1, AST_FORMAT_ALAW}, "audio", "G711A", 8000},
85         {{1, AST_FORMAT_G726}, "audio", "G726-32", 8000},
86         {{1, AST_FORMAT_ADPCM}, "audio", "DVI4", 8000},
87         {{1, AST_FORMAT_SLINEAR}, "audio", "L16", 8000},
88         {{1, AST_FORMAT_LPC10}, "audio", "LPC", 8000},
89         {{1, AST_FORMAT_G729A}, "audio", "G729", 8000},
90         {{1, AST_FORMAT_G729A}, "audio", "G729A", 8000},
91         {{1, AST_FORMAT_G729A}, "audio", "G.729", 8000},
92         {{1, AST_FORMAT_SPEEX}, "audio", "speex", 8000},
93         {{1, AST_FORMAT_ILBC}, "audio", "iLBC", 8000},
94         /* this is the sample rate listed in the RTP profile for the G.722
95                       codec, *NOT* the actual sample rate of the media stream
96         */
97         {{1, AST_FORMAT_G722}, "audio", "G722", 8000},
98         {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32", 8000},
99         {{0, AST_RTP_DTMF}, "audio", "telephone-event", 8000},
100         {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event", 8000},
101         {{0, AST_RTP_CN}, "audio", "CN", 8000},
102         {{1, AST_FORMAT_JPEG}, "video", "JPEG", 90000},
103         {{1, AST_FORMAT_PNG}, "video", "PNG", 90000},
104         {{1, AST_FORMAT_H261}, "video", "H261", 90000},
105         {{1, AST_FORMAT_H263}, "video", "H263", 90000},
106         {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998", 90000},
107         {{1, AST_FORMAT_H264}, "video", "H264", 90000},
108         {{1, AST_FORMAT_MP4_VIDEO}, "video", "MP4V-ES", 90000},
109         {{1, AST_FORMAT_T140RED}, "text", "RED", 1000},
110         {{1, AST_FORMAT_T140}, "text", "T140", 1000},
111         {{1, AST_FORMAT_SIREN7}, "audio", "G7221", 16000},
112         {{1, AST_FORMAT_SIREN14}, "audio", "G7221", 32000},
113 };
114
115 /*!
116  * \brief Mapping between Asterisk codecs and rtp payload types
117  *
118  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
119  * also, our own choices for dynamic payload types.  This is our master
120  * table for transmission
121  *
122  * See http://www.iana.org/assignments/rtp-parameters for a list of
123  * assigned values
124  */
125 static const struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT] = {
126         [0] = {1, AST_FORMAT_ULAW},
127         #ifdef USE_DEPRECATED_G726
128         [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
129         #endif
130         [3] = {1, AST_FORMAT_GSM},
131         [4] = {1, AST_FORMAT_G723_1},
132         [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
133         [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
134         [7] = {1, AST_FORMAT_LPC10},
135         [8] = {1, AST_FORMAT_ALAW},
136         [9] = {1, AST_FORMAT_G722},
137         [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
138         [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
139         [13] = {0, AST_RTP_CN},
140         [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
141         [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
142         [18] = {1, AST_FORMAT_G729A},
143         [19] = {0, AST_RTP_CN},         /* Also used for CN */
144         [26] = {1, AST_FORMAT_JPEG},
145         [31] = {1, AST_FORMAT_H261},
146         [34] = {1, AST_FORMAT_H263},
147         [97] = {1, AST_FORMAT_ILBC},
148         [98] = {1, AST_FORMAT_H263_PLUS},
149         [99] = {1, AST_FORMAT_H264},
150         [101] = {0, AST_RTP_DTMF},
151         [102] = {1, AST_FORMAT_SIREN7},
152         [103] = {1, AST_FORMAT_H263_PLUS},
153         [104] = {1, AST_FORMAT_MP4_VIDEO},
154         [105] = {1, AST_FORMAT_T140RED},        /* Real time text chat (with redundancy encoding) */
155         [106] = {1, AST_FORMAT_T140},   /* Real time text chat */
156         [110] = {1, AST_FORMAT_SPEEX},
157         [111] = {1, AST_FORMAT_G726},
158         [112] = {1, AST_FORMAT_G726_AAL2},
159         [115] = {1, AST_FORMAT_SIREN14},
160         [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
161 };
162
163 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
164 {
165         struct ast_rtp_engine *current_engine;
166
167         /* Perform a sanity check on the engine structure to make sure it has the basics */
168         if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
169                 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
170                 return -1;
171         }
172
173         /* Link owner module to the RTP engine for reference counting purposes */
174         engine->mod = module;
175
176         AST_RWLIST_WRLOCK(&engines);
177
178         /* Ensure that no two modules with the same name are registered at the same time */
179         AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
180                 if (!strcmp(current_engine->name, engine->name)) {
181                         ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
182                         AST_RWLIST_UNLOCK(&engines);
183                         return -1;
184                 }
185         }
186
187         /* The engine survived our critique. Off to the list it goes to be used */
188         AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
189
190         AST_RWLIST_UNLOCK(&engines);
191
192         ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
193
194         return 0;
195 }
196
197 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
198 {
199         struct ast_rtp_engine *current_engine = NULL;
200
201         AST_RWLIST_WRLOCK(&engines);
202
203         if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
204                 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
205         }
206
207         AST_RWLIST_UNLOCK(&engines);
208
209         return current_engine ? 0 : -1;
210 }
211
212 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
213 {
214         struct ast_rtp_glue *current_glue = NULL;
215
216         if (ast_strlen_zero(glue->type)) {
217                 return -1;
218         }
219
220         glue->mod = module;
221
222         AST_RWLIST_WRLOCK(&glues);
223
224         AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
225                 if (!strcasecmp(current_glue->type, glue->type)) {
226                         ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
227                         AST_RWLIST_UNLOCK(&glues);
228                         return -1;
229                 }
230         }
231
232         AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
233
234         AST_RWLIST_UNLOCK(&glues);
235
236         ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
237
238         return 0;
239 }
240
241 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
242 {
243         struct ast_rtp_glue *current_glue = NULL;
244
245         AST_RWLIST_WRLOCK(&glues);
246
247         if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
248                 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
249         }
250
251         AST_RWLIST_UNLOCK(&glues);
252
253         return current_glue ? 0 : -1;
254 }
255
256 static void instance_destructor(void *obj)
257 {
258         struct ast_rtp_instance *instance = obj;
259
260         /* Pass us off to the engine to destroy */
261         if (instance->data && instance->engine->destroy(instance)) {
262                 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
263                 return;
264         }
265
266         /* Drop our engine reference */
267         ast_module_unref(instance->engine->mod);
268
269         ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
270 }
271
272 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
273 {
274         ao2_ref(instance, -1);
275
276         return 0;
277 }
278
279 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name, struct sched_context *sched, struct sockaddr_in *sin, void *data)
280 {
281         struct sockaddr_in address = { 0, };
282         struct ast_rtp_instance *instance = NULL;
283         struct ast_rtp_engine *engine = NULL;
284
285         AST_RWLIST_RDLOCK(&engines);
286
287         /* If an engine name was specified try to use it or otherwise use the first one registered */
288         if (!ast_strlen_zero(engine_name)) {
289                 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
290                         if (!strcmp(engine->name, engine_name)) {
291                                 break;
292                         }
293                 }
294         } else {
295                 engine = AST_RWLIST_FIRST(&engines);
296         }
297
298         /* If no engine was actually found bail out now */
299         if (!engine) {
300                 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
301                 AST_RWLIST_UNLOCK(&engines);
302                 return NULL;
303         }
304
305         /* Bump up the reference count before we return so the module can not be unloaded */
306         ast_module_ref(engine->mod);
307
308         AST_RWLIST_UNLOCK(&engines);
309
310         /* Allocate a new RTP instance */
311         if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
312                 ast_module_unref(engine->mod);
313                 return NULL;
314         }
315         instance->engine = engine;
316         instance->local_address.sin_family = AF_INET;
317         instance->local_address.sin_addr = sin->sin_addr;
318         instance->remote_address.sin_family = AF_INET;
319         address.sin_addr = sin->sin_addr;
320
321         ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
322
323         /* And pass it off to the engine to setup */
324         if (instance->engine->new(instance, sched, &address, data)) {
325                 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
326                 ao2_ref(instance, -1);
327                 return NULL;
328         }
329
330         ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
331
332         return instance;
333 }
334
335 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
336 {
337         instance->data = data;
338 }
339
340 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
341 {
342         return instance->data;
343 }
344
345 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
346 {
347         return instance->engine->write(instance, frame);
348 }
349
350 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
351 {
352         return instance->engine->read(instance, rtcp);
353 }
354
355 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
356 {
357         instance->local_address.sin_addr = address->sin_addr;
358         instance->local_address.sin_port = address->sin_port;
359         return 0;
360 }
361
362 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
363 {
364         instance->remote_address.sin_addr = address->sin_addr;
365         instance->remote_address.sin_port = address->sin_port;
366
367         /* moo */
368
369         if (instance->engine->remote_address_set) {
370                 instance->engine->remote_address_set(instance, &instance->remote_address);
371         }
372
373         return 0;
374 }
375
376 int ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
377 {
378         if ((address->sin_family != AF_INET) ||
379             (address->sin_port != instance->local_address.sin_port) ||
380             (address->sin_addr.s_addr != instance->local_address.sin_addr.s_addr)) {
381                 memcpy(address, &instance->local_address, sizeof(*address));
382                 return 1;
383         }
384
385         return 0;
386 }
387
388 int ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
389 {
390         if ((address->sin_family != AF_INET) ||
391             (address->sin_port != instance->remote_address.sin_port) ||
392             (address->sin_addr.s_addr != instance->remote_address.sin_addr.s_addr)) {
393                 memcpy(address, &instance->remote_address, sizeof(*address));
394                 return 1;
395         }
396
397         return 0;
398 }
399
400 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
401 {
402         if (instance->engine->extended_prop_set) {
403                 instance->engine->extended_prop_set(instance, property, value);
404         }
405 }
406
407 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
408 {
409         if (instance->engine->extended_prop_get) {
410                 return instance->engine->extended_prop_get(instance, property);
411         }
412
413         return NULL;
414 }
415
416 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
417 {
418         instance->properties[property] = value;
419
420         if (instance->engine->prop_set) {
421                 instance->engine->prop_set(instance, property, value);
422         }
423 }
424
425 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
426 {
427         return instance->properties[property];
428 }
429
430 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
431 {
432         return &instance->codecs;
433 }
434
435 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
436 {
437         int i;
438
439         for (i = 0; i < AST_RTP_MAX_PT; i++) {
440                 ast_debug(2, "Clearing payload %d on %p\n", i, codecs);
441                 codecs->payloads[i].asterisk_format = 0;
442                 codecs->payloads[i].code = 0;
443                 if (instance && instance->engine && instance->engine->payload_set) {
444                         instance->engine->payload_set(instance, i, 0, 0);
445                 }
446         }
447 }
448
449 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
450 {
451         int i;
452
453         for (i = 0; i < AST_RTP_MAX_PT; i++) {
454                 if (static_RTP_PT[i].code) {
455                         ast_debug(2, "Set default payload %d on %p\n", i, codecs);
456                         codecs->payloads[i].asterisk_format = static_RTP_PT[i].asterisk_format;
457                         codecs->payloads[i].code = static_RTP_PT[i].code;
458                         if (instance && instance->engine && instance->engine->payload_set) {
459                                 instance->engine->payload_set(instance, i, codecs->payloads[i].asterisk_format, codecs->payloads[i].code);
460                         }
461                 }
462         }
463 }
464
465 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
466 {
467         int i;
468
469         for (i = 0; i < AST_RTP_MAX_PT; i++) {
470                 if (src->payloads[i].code) {
471                         ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
472                         dest->payloads[i].asterisk_format = src->payloads[i].asterisk_format;
473                         dest->payloads[i].code = src->payloads[i].code;
474                         if (instance && instance->engine && instance->engine->payload_set) {
475                                 instance->engine->payload_set(instance, i, dest->payloads[i].asterisk_format, dest->payloads[i].code);
476                         }
477                 }
478         }
479 }
480
481 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
482 {
483         if (payload < 0 || payload > AST_RTP_MAX_PT || !static_RTP_PT[payload].code) {
484                 return;
485         }
486
487         codecs->payloads[payload].asterisk_format = static_RTP_PT[payload].asterisk_format;
488         codecs->payloads[payload].code = static_RTP_PT[payload].code;
489
490         ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
491
492         if (instance && instance->engine && instance->engine->payload_set) {
493                 instance->engine->payload_set(instance, payload, codecs->payloads[payload].asterisk_format, codecs->payloads[payload].code);
494         }
495 }
496
497 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
498                                  char *mimetype, char *mimesubtype,
499                                  enum ast_rtp_options options,
500                                  unsigned int sample_rate)
501 {
502         unsigned int i;
503         int found = 0;
504
505         if (pt < 0 || pt > AST_RTP_MAX_PT)
506                 return -1; /* bogus payload type */
507
508         for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
509                 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
510
511                 if (strcasecmp(mimesubtype, t->subtype)) {
512                         continue;
513                 }
514
515                 if (strcasecmp(mimetype, t->type)) {
516                         continue;
517                 }
518
519                 /* if both sample rates have been supplied, and they don't match,
520                                       then this not a match; if one has not been supplied, then the
521                                       rates are not compared */
522                 if (sample_rate && t->sample_rate &&
523                     (sample_rate != t->sample_rate)) {
524                         continue;
525                 }
526
527                 found = 1;
528                 codecs->payloads[pt] = t->payload_type;
529
530                 if ((t->payload_type.code == AST_FORMAT_G726) &&
531                                         t->payload_type.asterisk_format &&
532                     (options & AST_RTP_OPT_G726_NONSTANDARD)) {
533                         codecs->payloads[pt].code = AST_FORMAT_G726_AAL2;
534                 }
535
536                 if (instance && instance->engine && instance->engine->payload_set) {
537                         instance->engine->payload_set(instance, pt, codecs->payloads[i].asterisk_format, codecs->payloads[i].code);
538                 }
539
540                 break;
541         }
542
543         return (found ? 0 : -2);
544 }
545
546 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)
547 {
548         return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
549 }
550
551 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
552 {
553         if (payload < 0 || payload > AST_RTP_MAX_PT) {
554                 return;
555         }
556
557         ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
558
559         codecs->payloads[payload].asterisk_format = 0;
560         codecs->payloads[payload].code = 0;
561
562         if (instance && instance->engine && instance->engine->payload_set) {
563                 instance->engine->payload_set(instance, payload, 0, 0);
564         }
565 }
566
567 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
568 {
569         struct ast_rtp_payload_type result = { .asterisk_format = 0, };
570
571         if (payload < 0 || payload > AST_RTP_MAX_PT) {
572                 return result;
573         }
574
575         result.asterisk_format = codecs->payloads[payload].asterisk_format;
576         result.code = codecs->payloads[payload].code;
577
578         if (!result.code) {
579                 result = static_RTP_PT[payload];
580         }
581
582         return result;
583 }
584
585 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, int *astformats, int *nonastformats)
586 {
587         int i;
588
589         *astformats = *nonastformats = 0;
590
591         for (i = 0; i < AST_RTP_MAX_PT; i++) {
592                 if (codecs->payloads[i].code) {
593                         ast_debug(1, "Incorporating payload %d on %p\n", i, codecs);
594                 }
595                 if (codecs->payloads[i].asterisk_format) {
596                         *astformats |= codecs->payloads[i].code;
597                 } else {
598                         *nonastformats |= codecs->payloads[i].code;
599                 }
600         }
601 }
602
603 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, const int asterisk_format, const int code)
604 {
605         int i;
606
607         for (i = 0; i < AST_RTP_MAX_PT; i++) {
608                 if (codecs->payloads[i].asterisk_format == asterisk_format && codecs->payloads[i].code == code) {
609                         ast_debug(2, "Found code %d at payload %d on %p\n", code, i, codecs);
610                         return i;
611                 }
612         }
613
614         for (i = 0; i < AST_RTP_MAX_PT; i++) {
615                 if (static_RTP_PT[i].asterisk_format == asterisk_format && static_RTP_PT[i].code == code) {
616                         return i;
617                 }
618         }
619
620         return -1;
621 }
622
623 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, const int code, enum ast_rtp_options options)
624 {
625         int i;
626
627         for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); i++) {
628                 if (ast_rtp_mime_types[i].payload_type.code == code && ast_rtp_mime_types[i].payload_type.asterisk_format == asterisk_format) {
629                         if (asterisk_format && (code == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
630                                 return "G726-32";
631                         } else {
632                                 return ast_rtp_mime_types[i].subtype;
633                         }
634                 }
635         }
636
637         return "";
638 }
639
640 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, int code)
641 {
642         unsigned int i;
643
644         for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
645                 if ((ast_rtp_mime_types[i].payload_type.code == code) && (ast_rtp_mime_types[i].payload_type.asterisk_format == asterisk_format)) {
646                         return ast_rtp_mime_types[i].sample_rate;
647                 }
648         }
649
650         return 0;
651 }
652
653 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, const int capability, const int asterisk_format, enum ast_rtp_options options)
654 {
655         int format, found = 0;
656
657         if (!buf) {
658                 return NULL;
659         }
660
661         ast_str_append(&buf, 0, "0x%x (", capability);
662
663         for (format = 1; format < AST_RTP_MAX; format <<= 1) {
664                 if (capability & format) {
665                         const char *name = ast_rtp_lookup_mime_subtype2(asterisk_format, format, options);
666                         ast_str_append(&buf, 0, "%s|", name);
667                         found = 1;
668                 }
669         }
670
671         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
672
673         return ast_str_buffer(buf);
674 }
675
676 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
677 {
678         codecs->pref = *prefs;
679
680         if (instance && instance->engine->packetization_set) {
681                 instance->engine->packetization_set(instance, &instance->codecs.pref);
682         }
683 }
684
685 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
686 {
687         return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
688 }
689
690 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
691 {
692         return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
693 }
694
695 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
696 {
697         if (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) {
698                 return -1;
699         }
700
701         instance->dtmf_mode = dtmf_mode;
702
703         return 0;
704 }
705
706 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
707 {
708         return instance->dtmf_mode;
709 }
710
711 void ast_rtp_instance_new_source(struct ast_rtp_instance *instance)
712 {
713         if (instance->engine->new_source) {
714                 instance->engine->new_source(instance);
715         }
716 }
717
718 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
719 {
720         return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
721 }
722
723 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
724 {
725         if (instance->engine->stop) {
726                 instance->engine->stop(instance);
727         }
728 }
729
730 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
731 {
732         return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
733 }
734
735 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
736 {
737         struct ast_rtp_glue *glue = NULL;
738
739         AST_RWLIST_RDLOCK(&glues);
740
741         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
742                 if (!strcasecmp(glue->type, type)) {
743                         break;
744                 }
745         }
746
747         AST_RWLIST_UNLOCK(&glues);
748
749         return glue;
750 }
751
752 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)
753 {
754         enum ast_bridge_result res = AST_BRIDGE_FAILED;
755         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
756         struct ast_frame *fr = NULL;
757
758         /* Start locally bridging both instances */
759         if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
760                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
761                 ast_channel_unlock(c0);
762                 ast_channel_unlock(c1);
763                 return AST_BRIDGE_FAILED_NOWARN;
764         }
765         if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
766                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
767                 if (instance0->engine->local_bridge) {
768                         instance0->engine->local_bridge(instance0, NULL);
769                 }
770                 ast_channel_unlock(c0);
771                 ast_channel_unlock(c1);
772                 return AST_BRIDGE_FAILED_NOWARN;
773         }
774
775         ast_channel_unlock(c0);
776         ast_channel_unlock(c1);
777
778         instance0->bridged = instance1;
779         instance1->bridged = instance0;
780
781         ast_poll_channel_add(c0, c1);
782
783         /* Hop into a loop waiting for a frame from either channel */
784         cs[0] = c0;
785         cs[1] = c1;
786         cs[2] = NULL;
787         for (;;) {
788                 /* If the underlying formats have changed force this bridge to break */
789                 if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
790                         ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
791                         res = AST_BRIDGE_FAILED_NOWARN;
792                         break;
793                 }
794                 /* Check if anything changed */
795                 if ((c0->tech_pvt != pvt0) ||
796                     (c1->tech_pvt != pvt1) ||
797                     (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
798                     (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
799                         ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
800                         /* 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 */
801                         if ((c0->masq || c0->masqr) && (fr = ast_read(c0))) {
802                                 ast_frfree(fr);
803                         }
804                         if ((c1->masq || c1->masqr) && (fr = ast_read(c1))) {
805                                 ast_frfree(fr);
806                         }
807                         res = AST_BRIDGE_RETRY;
808                         break;
809                 }
810                 /* Wait on a channel to feed us a frame */
811                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
812                         if (!timeoutms) {
813                                 res = AST_BRIDGE_RETRY;
814                                 break;
815                         }
816                         ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
817                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
818                                 break;
819                         }
820                         continue;
821                 }
822                 /* Read in frame from channel */
823                 fr = ast_read(who);
824                 other = (who == c0) ? c1 : c0;
825                 /* Depending on the frame we may need to break out of our bridge */
826                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
827                             ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
828                             ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
829                         /* Record received frame and who */
830                         *fo = fr;
831                         *rc = who;
832                         ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
833                         res = AST_BRIDGE_COMPLETE;
834                         break;
835                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
836                         if ((fr->subclass == AST_CONTROL_HOLD) ||
837                             (fr->subclass == AST_CONTROL_UNHOLD) ||
838                             (fr->subclass == AST_CONTROL_VIDUPDATE) ||
839                             (fr->subclass == AST_CONTROL_T38) ||
840                             (fr->subclass == AST_CONTROL_SRCUPDATE)) {
841                                 /* If we are going on hold, then break callback mode and P2P bridging */
842                                 if (fr->subclass == AST_CONTROL_HOLD) {
843                                         if (instance0->engine->local_bridge) {
844                                                 instance0->engine->local_bridge(instance0, NULL);
845                                         }
846                                         if (instance1->engine->local_bridge) {
847                                                 instance1->engine->local_bridge(instance1, NULL);
848                                         }
849                                         instance0->bridged = NULL;
850                                         instance1->bridged = NULL;
851                                 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
852                                         if (instance0->engine->local_bridge) {
853                                                 instance0->engine->local_bridge(instance0, instance1);
854                                         }
855                                         if (instance1->engine->local_bridge) {
856                                                 instance1->engine->local_bridge(instance1, instance0);
857                                         }
858                                         instance0->bridged = instance1;
859                                         instance1->bridged = instance0;
860                                 }
861                                 ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
862                                 ast_frfree(fr);
863                         } else {
864                                 *fo = fr;
865                                 *rc = who;
866                                 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
867                                 res = AST_BRIDGE_COMPLETE;
868                                 break;
869                         }
870                 } else {
871                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
872                             (fr->frametype == AST_FRAME_DTMF_END) ||
873                             (fr->frametype == AST_FRAME_VOICE) ||
874                             (fr->frametype == AST_FRAME_VIDEO) ||
875                             (fr->frametype == AST_FRAME_IMAGE) ||
876                             (fr->frametype == AST_FRAME_HTML) ||
877                             (fr->frametype == AST_FRAME_MODEM) ||
878                             (fr->frametype == AST_FRAME_TEXT)) {
879                                 ast_write(other, fr);
880                         }
881
882                         ast_frfree(fr);
883                 }
884                 /* Swap priority */
885                 cs[2] = cs[0];
886                 cs[0] = cs[1];
887                 cs[1] = cs[2];
888         }
889
890         /* Stop locally bridging both instances */
891         if (instance0->engine->local_bridge) {
892                 instance0->engine->local_bridge(instance0, NULL);
893         }
894         if (instance1->engine->local_bridge) {
895                 instance1->engine->local_bridge(instance1, NULL);
896         }
897
898         instance0->bridged = NULL;
899         instance1->bridged = NULL;
900
901         ast_poll_channel_del(c0, c1);
902
903         return res;
904 }
905
906 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1,
907                                                  struct ast_rtp_instance *vinstance0, struct ast_rtp_instance *vinstance1, struct ast_rtp_instance *tinstance0,
908                                                  struct ast_rtp_instance *tinstance1, struct ast_rtp_glue *glue0, struct ast_rtp_glue *glue1, int codec0, int codec1, int timeoutms,
909                                                  int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
910 {
911         enum ast_bridge_result res = AST_BRIDGE_FAILED;
912         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
913         int oldcodec0 = codec0, oldcodec1 = codec1;
914         struct sockaddr_in ac1 = {0,}, vac1 = {0,}, tac1 = {0,}, ac0 = {0,}, vac0 = {0,}, tac0 = {0,};
915         struct sockaddr_in t1 = {0,}, vt1 = {0,}, tt1 = {0,}, t0 = {0,}, vt0 = {0,}, tt0 = {0,};
916         struct ast_frame *fr = NULL;
917
918         /* Test the first channel */
919         if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0))) {
920                 ast_rtp_instance_get_remote_address(instance1, &ac1);
921                 if (vinstance1) {
922                         ast_rtp_instance_get_remote_address(vinstance1, &vac1);
923                 }
924                 if (tinstance1) {
925                         ast_rtp_instance_get_remote_address(tinstance1, &tac1);
926                 }
927         } else {
928                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
929         }
930
931         /* Test the second channel */
932         if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0))) {
933                 ast_rtp_instance_get_remote_address(instance0, &ac0);
934                 if (vinstance0) {
935                         ast_rtp_instance_get_remote_address(instance0, &vac0);
936                 }
937                 if (tinstance0) {
938                         ast_rtp_instance_get_remote_address(instance0, &tac0);
939                 }
940         } else {
941                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
942         }
943
944         ast_channel_unlock(c0);
945         ast_channel_unlock(c1);
946
947         instance0->bridged = instance1;
948         instance1->bridged = instance0;
949
950         ast_poll_channel_add(c0, c1);
951
952         /* Go into a loop handling any stray frames that may come in */
953         cs[0] = c0;
954         cs[1] = c1;
955         cs[2] = NULL;
956         for (;;) {
957                 /* Check if anything changed */
958                 if ((c0->tech_pvt != pvt0) ||
959                     (c1->tech_pvt != pvt1) ||
960                     (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
961                     (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
962                         ast_debug(1, "Oooh, something is weird, backing out\n");
963                         res = AST_BRIDGE_RETRY;
964                         break;
965                 }
966
967                 /* Check if they have changed their address */
968                 ast_rtp_instance_get_remote_address(instance1, &t1);
969                 if (vinstance1) {
970                         ast_rtp_instance_get_remote_address(vinstance1, &vt1);
971                 }
972                 if (tinstance1) {
973                         ast_rtp_instance_get_remote_address(tinstance1, &tt1);
974                 }
975                 if (glue1->get_codec) {
976                         codec1 = glue1->get_codec(c1);
977                 }
978
979                 ast_rtp_instance_get_remote_address(instance0, &t0);
980                 if (vinstance0) {
981                         ast_rtp_instance_get_remote_address(vinstance0, &vt0);
982                 }
983                 if (tinstance0) {
984                         ast_rtp_instance_get_remote_address(tinstance0, &tt0);
985                 }
986                 if (glue0->get_codec) {
987                         codec0 = glue0->get_codec(c0);
988                 }
989
990                 if ((inaddrcmp(&t1, &ac1)) ||
991                     (vinstance1 && inaddrcmp(&vt1, &vac1)) ||
992                     (tinstance1 && inaddrcmp(&tt1, &tac1)) ||
993                     (codec1 != oldcodec1)) {
994                         ast_debug(1, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
995                                   c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
996                         ast_debug(1, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
997                                   c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
998                         ast_debug(1, "Oooh, '%s' changed end taddress to %s:%d (format %d)\n",
999                                   c1->name, ast_inet_ntoa(tt1.sin_addr), ntohs(tt1.sin_port), codec1);
1000                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1001                                   c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1002                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1003                                   c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1004                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1005                                   c1->name, ast_inet_ntoa(tac1.sin_addr), ntohs(tac1.sin_port), oldcodec1);
1006                         if (glue0->update_peer(c0, t1.sin_addr.s_addr ? instance1 : NULL, vt1.sin_addr.s_addr ? vinstance1 : NULL, tt1.sin_addr.s_addr ? tinstance1 : NULL, codec1, 0)) {
1007                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1008                         }
1009                         memcpy(&ac1, &t1, sizeof(ac1));
1010                         memcpy(&vac1, &vt1, sizeof(vac1));
1011                         memcpy(&tac1, &tt1, sizeof(tac1));
1012                         oldcodec1 = codec1;
1013                 }
1014                 if ((inaddrcmp(&t0, &ac0)) ||
1015                     (vinstance0 && inaddrcmp(&vt0, &vac0)) ||
1016                     (tinstance0 && inaddrcmp(&tt0, &tac0)) ||
1017                     (codec0 != oldcodec0)) {
1018                         ast_debug(1, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1019                                   c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
1020                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1021                                   c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1022                         if (glue1->update_peer(c1, t0.sin_addr.s_addr ? instance0 : NULL, vt0.sin_addr.s_addr ? vinstance0 : NULL, tt0.sin_addr.s_addr ? tinstance0 : NULL, codec0, 0)) {
1023                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1024                         }
1025                         memcpy(&ac0, &t0, sizeof(ac0));
1026                         memcpy(&vac0, &vt0, sizeof(vac0));
1027                         memcpy(&tac0, &tt0, sizeof(tac0));
1028                         oldcodec0 = codec0;
1029                 }
1030
1031                 /* Wait for frame to come in on the channels */
1032                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1033                         if (!timeoutms) {
1034                                 res = AST_BRIDGE_RETRY;
1035                                 break;
1036                         }
1037                         ast_debug(1, "Ooh, empty read...\n");
1038                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1039                                 break;
1040                         }
1041                         continue;
1042                 }
1043                 fr = ast_read(who);
1044                 other = (who == c0) ? c1 : c0;
1045                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1046                             (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1047                              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1048                         /* Break out of bridge */
1049                         *fo = fr;
1050                         *rc = who;
1051                         ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1052                         res = AST_BRIDGE_COMPLETE;
1053                         break;
1054                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1055                         if ((fr->subclass == AST_CONTROL_HOLD) ||
1056                             (fr->subclass == AST_CONTROL_UNHOLD) ||
1057                             (fr->subclass == AST_CONTROL_VIDUPDATE) ||
1058                             (fr->subclass == AST_CONTROL_T38) ||
1059                             (fr->subclass == AST_CONTROL_SRCUPDATE)) {
1060                                 if (fr->subclass == AST_CONTROL_HOLD) {
1061                                         /* If we someone went on hold we want the other side to reinvite back to us */
1062                                         if (who == c0) {
1063                                                 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1064                                         } else {
1065                                                 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1066                                         }
1067                                 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
1068                                         /* If they went off hold they should go back to being direct */
1069                                         if (who == c0) {
1070                                                 glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0);
1071                                         } else {
1072                                                 glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0);
1073                                         }
1074                                 }
1075                                 /* Update local address information */
1076                                 ast_rtp_instance_get_remote_address(instance0, &t0);
1077                                 memcpy(&ac0, &t0, sizeof(ac0));
1078                                 ast_rtp_instance_get_remote_address(instance1, &t1);
1079                                 memcpy(&ac1, &t1, sizeof(ac1));
1080                                 /* Update codec information */
1081                                 if (glue0->get_codec && c0->tech_pvt) {
1082                                         oldcodec0 = codec0 = glue0->get_codec(c0);
1083                                 }
1084                                 if (glue1->get_codec && c1->tech_pvt) {
1085                                         oldcodec1 = codec1 = glue1->get_codec(c1);
1086                                 }
1087                                 ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
1088                                 ast_frfree(fr);
1089                         } else {
1090                                 *fo = fr;
1091                                 *rc = who;
1092                                 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
1093                                 return AST_BRIDGE_COMPLETE;
1094                         }
1095                 } else {
1096                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1097                             (fr->frametype == AST_FRAME_DTMF_END) ||
1098                             (fr->frametype == AST_FRAME_VOICE) ||
1099                             (fr->frametype == AST_FRAME_VIDEO) ||
1100                             (fr->frametype == AST_FRAME_IMAGE) ||
1101                             (fr->frametype == AST_FRAME_HTML) ||
1102                             (fr->frametype == AST_FRAME_MODEM) ||
1103                             (fr->frametype == AST_FRAME_TEXT)) {
1104                                 ast_write(other, fr);
1105                         }
1106                         ast_frfree(fr);
1107                 }
1108                 /* Swap priority */
1109                 cs[2] = cs[0];
1110                 cs[0] = cs[1];
1111                 cs[1] = cs[2];
1112         }
1113
1114         if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1115                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
1116         }
1117         if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1118                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
1119         }
1120
1121         instance0->bridged = NULL;
1122         instance1->bridged = NULL;
1123
1124         ast_poll_channel_del(c0, c1);
1125
1126         return res;
1127 }
1128
1129 /*!
1130  * \brief Conditionally unref an rtp instance
1131  */
1132 static void unref_instance_cond(struct ast_rtp_instance **instance)
1133 {
1134         if (*instance) {
1135                 ao2_ref(*instance, -1);
1136                 *instance = NULL;
1137         }
1138 }
1139
1140 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)
1141 {
1142         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1143                         *vinstance0 = NULL, *vinstance1 = NULL,
1144                         *tinstance0 = NULL, *tinstance1 = NULL;
1145         struct ast_rtp_glue *glue0, *glue1;
1146         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1147         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1148         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1149         int codec0 = 0, codec1 = 0;
1150         int unlock_chans = 1;
1151
1152         /* Lock both channels so we can look for the glue that binds them together */
1153         ast_channel_lock(c0);
1154         while (ast_channel_trylock(c1)) {
1155                 ast_channel_unlock(c0);
1156                 usleep(1);
1157                 ast_channel_lock(c0);
1158         }
1159
1160         /* Ensure neither channel got hungup during lock avoidance */
1161         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1162                 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
1163                 goto done;
1164         }
1165
1166         /* Grab glue that binds each channel to something using the RTP engine */
1167         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1168                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1169                 goto done;
1170         }
1171
1172         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1173         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1174         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1175
1176         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1177         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1178         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1179
1180         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1181         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)) {
1182                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1183         }
1184         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)) {
1185                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1186         }
1187
1188         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1189         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1190                 res = AST_BRIDGE_FAILED_NOWARN;
1191                 goto done;
1192         }
1193
1194         /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1195         if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && instance0->properties[AST_RTP_PROPERTY_DTMF]) {
1196                 res = AST_BRIDGE_FAILED_NOWARN;
1197                 goto done;
1198         }
1199         if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && instance1->properties[AST_RTP_PROPERTY_DTMF]) {
1200                 res = AST_BRIDGE_FAILED_NOWARN;
1201                 goto done;
1202         }
1203
1204         /* 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 */
1205         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)))) {
1206                 res = AST_BRIDGE_FAILED_NOWARN;
1207                 goto done;
1208         }
1209
1210         /* Make sure that codecs match */
1211         codec0 = glue0->get_codec ? glue0->get_codec(c0) : 0;
1212         codec1 = glue1->get_codec ? glue1->get_codec(c1) : 0;
1213         if (codec0 && codec1 && !(codec0 & codec1)) {
1214                 ast_debug(1, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
1215                 res = AST_BRIDGE_FAILED_NOWARN;
1216                 goto done;
1217         }
1218
1219         /* Depending on the end result for bridging either do a local bridge or remote bridge */
1220         if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1221                 ast_verbose(VERBOSE_PREFIX_3 "Locally bridging %s and %s\n", c0->name, c1->name);
1222                 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
1223         } else {
1224                 ast_verbose(VERBOSE_PREFIX_3 "Remotely bridging %s and %s\n", c0->name, c1->name);
1225                 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1226                                 tinstance0, tinstance1, glue0, glue1, codec0, codec1, timeoutms, flags,
1227                                 fo, rc, c0->tech_pvt, c1->tech_pvt);
1228         }
1229
1230         unlock_chans = 0;
1231
1232 done:
1233         if (unlock_chans) {
1234                 ast_channel_unlock(c0);
1235                 ast_channel_unlock(c1);
1236         }
1237
1238         unref_instance_cond(&instance0);
1239         unref_instance_cond(&instance1);
1240         unref_instance_cond(&vinstance0);
1241         unref_instance_cond(&vinstance1);
1242         unref_instance_cond(&tinstance0);
1243         unref_instance_cond(&tinstance1);
1244
1245         return res;
1246 }
1247
1248 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1249 {
1250         return instance->bridged;
1251 }
1252
1253 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1254 {
1255         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1256                 *vinstance0 = NULL, *vinstance1 = NULL,
1257                 *tinstance0 = NULL, *tinstance1 = NULL;
1258         struct ast_rtp_glue *glue0, *glue1;
1259         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1260         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1261         int codec0 = 0, codec1 = 0;
1262         int res = 0;
1263
1264         /* Lock both channels so we can look for the glue that binds them together */
1265         ast_channel_lock(c0);
1266         while (ast_channel_trylock(c1)) {
1267                 ast_channel_unlock(c0);
1268                 usleep(1);
1269                 ast_channel_lock(c0);
1270         }
1271
1272         /* Grab glue that binds each channel to something using the RTP engine */
1273         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1274                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1275                 goto done;
1276         }
1277
1278         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1279         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1280         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1281
1282         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1283         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1284         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1285
1286         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1287         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)) {
1288                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1289         }
1290         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)) {
1291                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1292         }
1293         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(c0)) {
1294                 codec0 = glue0->get_codec(c0);
1295         }
1296         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(c1)) {
1297                 codec1 = glue1->get_codec(c1);
1298         }
1299
1300         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1301         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1302                 goto done;
1303         }
1304
1305         /* Make sure we have matching codecs */
1306         if (!(codec0 & codec1)) {
1307                 goto done;
1308         }
1309
1310         ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1311
1312         if (vinstance0 && vinstance1) {
1313                 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1314         }
1315         if (tinstance0 && tinstance1) {
1316                 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1317         }
1318
1319         res = 0;
1320
1321 done:
1322         ast_channel_unlock(c0);
1323         ast_channel_unlock(c1);
1324
1325         unref_instance_cond(&instance0);
1326         unref_instance_cond(&instance1);
1327         unref_instance_cond(&vinstance0);
1328         unref_instance_cond(&vinstance1);
1329         unref_instance_cond(&tinstance0);
1330         unref_instance_cond(&tinstance1);
1331
1332         if (!res) {
1333                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1334         }
1335 }
1336
1337 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1338 {
1339         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1340                         *vinstance0 = NULL, *vinstance1 = NULL,
1341                         *tinstance0 = NULL, *tinstance1 = NULL;
1342         struct ast_rtp_glue *glue0, *glue1;
1343         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1344         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1345         int codec0 = 0, codec1 = 0;
1346         int res = 0;
1347
1348         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1349         if (!c1) {
1350                 return -1;
1351         }
1352
1353         /* Lock both channels so we can look for the glue that binds them together */
1354         ast_channel_lock(c0);
1355         while (ast_channel_trylock(c1)) {
1356                 ast_channel_unlock(c0);
1357                 usleep(1);
1358                 ast_channel_lock(c0);
1359         }
1360
1361         /* Grab glue that binds each channel to something using the RTP engine */
1362         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1363                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1364                 goto done;
1365         }
1366
1367         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1368         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1369         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1370
1371         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1372         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1373         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1374
1375         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1376         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)) {
1377                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1378         }
1379         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)) {
1380                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1381         }
1382         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(c0)) {
1383                 codec0 = glue0->get_codec(c0);
1384         }
1385         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(c1)) {
1386                 codec1 = glue1->get_codec(c1);
1387         }
1388
1389         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1390         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1391                 goto done;
1392         }
1393
1394         /* Make sure we have matching codecs */
1395         if (!(codec0 & codec1)) {
1396                 goto done;
1397         }
1398
1399         /* Bridge media early */
1400         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0)) {
1401                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1402         }
1403
1404         res = 0;
1405
1406 done:
1407         ast_channel_unlock(c0);
1408         ast_channel_unlock(c1);
1409
1410         unref_instance_cond(&instance0);
1411         unref_instance_cond(&instance1);
1412         unref_instance_cond(&vinstance0);
1413         unref_instance_cond(&vinstance1);
1414         unref_instance_cond(&tinstance0);
1415         unref_instance_cond(&tinstance1);
1416
1417         if (!res) {
1418                 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1419         }
1420
1421         return res;
1422 }
1423
1424 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1425 {
1426         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1427 }
1428
1429 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1430 {
1431         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1432 }
1433
1434 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1435 {
1436         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1437 }
1438
1439 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1440 {
1441         struct ast_rtp_instance_stats stats = { 0, };
1442         enum ast_rtp_instance_stat stat;
1443
1444         /* Determine what statistics we will need to retrieve based on field passed in */
1445         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1446                 stat = AST_RTP_INSTANCE_STAT_ALL;
1447         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1448                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1449         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1450                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1451         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1452                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1453         } else {
1454                 return NULL;
1455         }
1456
1457         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1458         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1459                 return NULL;
1460         }
1461
1462         /* Now actually fill the buffer with the good information */
1463         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1464                 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%u;rxcount=%u;txjitter=%u;txcount=%u;rlp=%u;rtt=%u",
1465                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1466         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1467                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1468                          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));
1469         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1470                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1471                          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));
1472         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1473                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1474         }
1475
1476         return buf;
1477 }
1478
1479 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1480 {
1481         char quality_buf[AST_MAX_USER_FIELD], *quality;
1482         struct ast_channel *bridge = ast_bridged_channel(chan);
1483
1484         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1485                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1486                 if (bridge) {
1487                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1488                 }
1489         }
1490
1491         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1492                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1493                 if (bridge) {
1494                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1495                 }
1496         }
1497
1498         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1499                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1500                 if (bridge) {
1501                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1502                 }
1503         }
1504
1505         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1506                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1507                 if (bridge) {
1508                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1509                 }
1510         }
1511 }
1512
1513 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, int format)
1514 {
1515         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1516 }
1517
1518 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, int format)
1519 {
1520         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1521 }
1522
1523 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1524 {
1525         struct ast_rtp_glue *glue;
1526         struct ast_rtp_instance *peer_instance = NULL;
1527         int res = -1;
1528
1529         if (!instance->engine->make_compatible) {
1530                 return -1;
1531         }
1532
1533         ast_channel_lock(peer);
1534
1535         if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
1536                 ast_channel_unlock(peer);
1537                 return -1;
1538         }
1539
1540         glue->get_rtp_info(peer, &peer_instance);
1541
1542         if (!peer_instance || peer_instance->engine != instance->engine) {
1543                 ast_channel_unlock(peer);
1544                 ao2_ref(peer_instance, -1);
1545                 peer_instance = NULL;
1546                 return -1;
1547         }
1548
1549         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1550
1551         ast_channel_unlock(peer);
1552
1553         ao2_ref(peer_instance, -1);
1554         peer_instance = NULL;
1555
1556         return res;
1557 }
1558
1559 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1560 {
1561         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1562 }
1563
1564 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance, struct sockaddr_in *suggestion, const char *username)
1565 {
1566         if (instance->engine->stun_request) {
1567                 instance->engine->stun_request(instance, suggestion, username);
1568         }
1569 }
1570
1571 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1572 {
1573         instance->timeout = timeout;
1574 }
1575
1576 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1577 {
1578         instance->holdtimeout = timeout;
1579 }
1580
1581 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1582 {
1583         return instance->timeout;
1584 }
1585
1586 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1587 {
1588         return instance->holdtimeout;
1589 }