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