Two other trunk build fixes (reported by seanbright on #asterisk-dev)
[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, format_t *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 format_t 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 format_t 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, format_t 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 format_t capability, const int asterisk_format, enum ast_rtp_options options)
672 {
673         format_t format;
674         int found = 0;
675
676         if (!buf) {
677                 return NULL;
678         }
679
680         ast_str_append(&buf, 0, "0x%llx (", (unsigned long long) capability);
681
682         for (format = 1; format < AST_RTP_MAX; format <<= 1) {
683                 if (capability & format) {
684                         const char *name = ast_rtp_lookup_mime_subtype2(asterisk_format, format, options);
685                         ast_str_append(&buf, 0, "%s|", name);
686                         found = 1;
687                 }
688         }
689
690         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
691
692         return ast_str_buffer(buf);
693 }
694
695 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
696 {
697         codecs->pref = *prefs;
698
699         if (instance && instance->engine->packetization_set) {
700                 instance->engine->packetization_set(instance, &instance->codecs.pref);
701         }
702 }
703
704 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
705 {
706         return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
707 }
708
709 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
710 {
711         return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
712 }
713
714 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
715 {
716         if (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) {
717                 return -1;
718         }
719
720         instance->dtmf_mode = dtmf_mode;
721
722         return 0;
723 }
724
725 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
726 {
727         return instance->dtmf_mode;
728 }
729
730 void ast_rtp_instance_new_source(struct ast_rtp_instance *instance)
731 {
732         if (instance->engine->new_source) {
733                 instance->engine->new_source(instance);
734         }
735 }
736
737 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
738 {
739         return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
740 }
741
742 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
743 {
744         if (instance->engine->stop) {
745                 instance->engine->stop(instance);
746         }
747 }
748
749 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
750 {
751         return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
752 }
753
754 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
755 {
756         struct ast_rtp_glue *glue = NULL;
757
758         AST_RWLIST_RDLOCK(&glues);
759
760         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
761                 if (!strcasecmp(glue->type, type)) {
762                         break;
763                 }
764         }
765
766         AST_RWLIST_UNLOCK(&glues);
767
768         return glue;
769 }
770
771 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)
772 {
773         enum ast_bridge_result res = AST_BRIDGE_FAILED;
774         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
775         struct ast_frame *fr = NULL;
776
777         /* Start locally bridging both instances */
778         if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
779                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
780                 ast_channel_unlock(c0);
781                 ast_channel_unlock(c1);
782                 return AST_BRIDGE_FAILED_NOWARN;
783         }
784         if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
785                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
786                 if (instance0->engine->local_bridge) {
787                         instance0->engine->local_bridge(instance0, NULL);
788                 }
789                 ast_channel_unlock(c0);
790                 ast_channel_unlock(c1);
791                 return AST_BRIDGE_FAILED_NOWARN;
792         }
793
794         ast_channel_unlock(c0);
795         ast_channel_unlock(c1);
796
797         instance0->bridged = instance1;
798         instance1->bridged = instance0;
799
800         ast_poll_channel_add(c0, c1);
801
802         /* Hop into a loop waiting for a frame from either channel */
803         cs[0] = c0;
804         cs[1] = c1;
805         cs[2] = NULL;
806         for (;;) {
807                 /* If the underlying formats have changed force this bridge to break */
808                 if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
809                         ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
810                         res = AST_BRIDGE_FAILED_NOWARN;
811                         break;
812                 }
813                 /* Check if anything changed */
814                 if ((c0->tech_pvt != pvt0) ||
815                     (c1->tech_pvt != pvt1) ||
816                     (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
817                     (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
818                         ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
819                         /* 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 */
820                         if ((c0->masq || c0->masqr) && (fr = ast_read(c0))) {
821                                 ast_frfree(fr);
822                         }
823                         if ((c1->masq || c1->masqr) && (fr = ast_read(c1))) {
824                                 ast_frfree(fr);
825                         }
826                         res = AST_BRIDGE_RETRY;
827                         break;
828                 }
829                 /* Wait on a channel to feed us a frame */
830                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
831                         if (!timeoutms) {
832                                 res = AST_BRIDGE_RETRY;
833                                 break;
834                         }
835                         ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
836                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
837                                 break;
838                         }
839                         continue;
840                 }
841                 /* Read in frame from channel */
842                 fr = ast_read(who);
843                 other = (who == c0) ? c1 : c0;
844                 /* Depending on the frame we may need to break out of our bridge */
845                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
846                             ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
847                             ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
848                         /* Record received frame and who */
849                         *fo = fr;
850                         *rc = who;
851                         ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
852                         res = AST_BRIDGE_COMPLETE;
853                         break;
854                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
855                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
856                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
857                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
858                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
859                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
860                                 /* If we are going on hold, then break callback mode and P2P bridging */
861                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
862                                         if (instance0->engine->local_bridge) {
863                                                 instance0->engine->local_bridge(instance0, NULL);
864                                         }
865                                         if (instance1->engine->local_bridge) {
866                                                 instance1->engine->local_bridge(instance1, NULL);
867                                         }
868                                         instance0->bridged = NULL;
869                                         instance1->bridged = NULL;
870                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
871                                         if (instance0->engine->local_bridge) {
872                                                 instance0->engine->local_bridge(instance0, instance1);
873                                         }
874                                         if (instance1->engine->local_bridge) {
875                                                 instance1->engine->local_bridge(instance1, instance0);
876                                         }
877                                         instance0->bridged = instance1;
878                                         instance1->bridged = instance0;
879                                 }
880                                 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
881                                 ast_frfree(fr);
882                         } else {
883                                 *fo = fr;
884                                 *rc = who;
885                                 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
886                                 res = AST_BRIDGE_COMPLETE;
887                                 break;
888                         }
889                 } else {
890                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
891                             (fr->frametype == AST_FRAME_DTMF_END) ||
892                             (fr->frametype == AST_FRAME_VOICE) ||
893                             (fr->frametype == AST_FRAME_VIDEO) ||
894                             (fr->frametype == AST_FRAME_IMAGE) ||
895                             (fr->frametype == AST_FRAME_HTML) ||
896                             (fr->frametype == AST_FRAME_MODEM) ||
897                             (fr->frametype == AST_FRAME_TEXT)) {
898                                 ast_write(other, fr);
899                         }
900
901                         ast_frfree(fr);
902                 }
903                 /* Swap priority */
904                 cs[2] = cs[0];
905                 cs[0] = cs[1];
906                 cs[1] = cs[2];
907         }
908
909         /* Stop locally bridging both instances */
910         if (instance0->engine->local_bridge) {
911                 instance0->engine->local_bridge(instance0, NULL);
912         }
913         if (instance1->engine->local_bridge) {
914                 instance1->engine->local_bridge(instance1, NULL);
915         }
916
917         instance0->bridged = NULL;
918         instance1->bridged = NULL;
919
920         ast_poll_channel_del(c0, c1);
921
922         return res;
923 }
924
925 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,
926                                                  struct ast_rtp_instance *vinstance0, struct ast_rtp_instance *vinstance1, struct ast_rtp_instance *tinstance0,
927                                                  struct ast_rtp_instance *tinstance1, struct ast_rtp_glue *glue0, struct ast_rtp_glue *glue1, format_t codec0, format_t codec1, int timeoutms,
928                                                  int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
929 {
930         enum ast_bridge_result res = AST_BRIDGE_FAILED;
931         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
932         format_t oldcodec0 = codec0, oldcodec1 = codec1;
933         struct sockaddr_in ac1 = {0,}, vac1 = {0,}, tac1 = {0,}, ac0 = {0,}, vac0 = {0,}, tac0 = {0,};
934         struct sockaddr_in t1 = {0,}, vt1 = {0,}, tt1 = {0,}, t0 = {0,}, vt0 = {0,}, tt0 = {0,};
935         struct ast_frame *fr = NULL;
936
937         /* Test the first channel */
938         if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0))) {
939                 ast_rtp_instance_get_remote_address(instance1, &ac1);
940                 if (vinstance1) {
941                         ast_rtp_instance_get_remote_address(vinstance1, &vac1);
942                 }
943                 if (tinstance1) {
944                         ast_rtp_instance_get_remote_address(tinstance1, &tac1);
945                 }
946         } else {
947                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
948         }
949
950         /* Test the second channel */
951         if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0))) {
952                 ast_rtp_instance_get_remote_address(instance0, &ac0);
953                 if (vinstance0) {
954                         ast_rtp_instance_get_remote_address(instance0, &vac0);
955                 }
956                 if (tinstance0) {
957                         ast_rtp_instance_get_remote_address(instance0, &tac0);
958                 }
959         } else {
960                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
961         }
962
963         ast_channel_unlock(c0);
964         ast_channel_unlock(c1);
965
966         instance0->bridged = instance1;
967         instance1->bridged = instance0;
968
969         ast_poll_channel_add(c0, c1);
970
971         /* Go into a loop handling any stray frames that may come in */
972         cs[0] = c0;
973         cs[1] = c1;
974         cs[2] = NULL;
975         for (;;) {
976                 /* Check if anything changed */
977                 if ((c0->tech_pvt != pvt0) ||
978                     (c1->tech_pvt != pvt1) ||
979                     (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
980                     (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
981                         ast_debug(1, "Oooh, something is weird, backing out\n");
982                         res = AST_BRIDGE_RETRY;
983                         break;
984                 }
985
986                 /* Check if they have changed their address */
987                 ast_rtp_instance_get_remote_address(instance1, &t1);
988                 if (vinstance1) {
989                         ast_rtp_instance_get_remote_address(vinstance1, &vt1);
990                 }
991                 if (tinstance1) {
992                         ast_rtp_instance_get_remote_address(tinstance1, &tt1);
993                 }
994                 if (glue1->get_codec) {
995                         codec1 = glue1->get_codec(c1);
996                 }
997
998                 ast_rtp_instance_get_remote_address(instance0, &t0);
999                 if (vinstance0) {
1000                         ast_rtp_instance_get_remote_address(vinstance0, &vt0);
1001                 }
1002                 if (tinstance0) {
1003                         ast_rtp_instance_get_remote_address(tinstance0, &tt0);
1004                 }
1005                 if (glue0->get_codec) {
1006                         codec0 = glue0->get_codec(c0);
1007                 }
1008
1009                 if ((inaddrcmp(&t1, &ac1)) ||
1010                     (vinstance1 && inaddrcmp(&vt1, &vac1)) ||
1011                     (tinstance1 && inaddrcmp(&tt1, &tac1)) ||
1012                     (codec1 != oldcodec1)) {
1013                         ast_debug(1, "Oooh, '%s' changed end address to %s:%d (format %s)\n",
1014                                   c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), ast_getformatname(codec1));
1015                         ast_debug(1, "Oooh, '%s' changed end vaddress to %s:%d (format %s)\n",
1016                                   c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), ast_getformatname(codec1));
1017                         ast_debug(1, "Oooh, '%s' changed end taddress to %s:%d (format %s)\n",
1018                                   c1->name, ast_inet_ntoa(tt1.sin_addr), ntohs(tt1.sin_port), ast_getformatname(codec1));
1019                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %s)\n",
1020                                   c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), ast_getformatname(oldcodec1));
1021                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %s)\n",
1022                                   c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), ast_getformatname(oldcodec1));
1023                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %s)\n",
1024                                   c1->name, ast_inet_ntoa(tac1.sin_addr), ntohs(tac1.sin_port), ast_getformatname(oldcodec1));
1025                         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)) {
1026                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1027                         }
1028                         memcpy(&ac1, &t1, sizeof(ac1));
1029                         memcpy(&vac1, &vt1, sizeof(vac1));
1030                         memcpy(&tac1, &tt1, sizeof(tac1));
1031                         oldcodec1 = codec1;
1032                 }
1033                 if ((inaddrcmp(&t0, &ac0)) ||
1034                     (vinstance0 && inaddrcmp(&vt0, &vac0)) ||
1035                     (tinstance0 && inaddrcmp(&tt0, &tac0)) ||
1036                     (codec0 != oldcodec0)) {
1037                         ast_debug(1, "Oooh, '%s' changed end address to %s:%d (format %s)\n",
1038                                   c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), ast_getformatname(codec0));
1039                         ast_debug(1, "Oooh, '%s' was %s:%d/(format %s)\n",
1040                                   c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), ast_getformatname(oldcodec0));
1041                         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)) {
1042                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1043                         }
1044                         memcpy(&ac0, &t0, sizeof(ac0));
1045                         memcpy(&vac0, &vt0, sizeof(vac0));
1046                         memcpy(&tac0, &tt0, sizeof(tac0));
1047                         oldcodec0 = codec0;
1048                 }
1049
1050                 /* Wait for frame to come in on the channels */
1051                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1052                         if (!timeoutms) {
1053                                 res = AST_BRIDGE_RETRY;
1054                                 break;
1055                         }
1056                         ast_debug(1, "Ooh, empty read...\n");
1057                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1058                                 break;
1059                         }
1060                         continue;
1061                 }
1062                 fr = ast_read(who);
1063                 other = (who == c0) ? c1 : c0;
1064                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1065                             (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1066                              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1067                         /* Break out of bridge */
1068                         *fo = fr;
1069                         *rc = who;
1070                         ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1071                         res = AST_BRIDGE_COMPLETE;
1072                         break;
1073                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1074                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1075                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1076                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1077                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1078                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
1079                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1080                                         /* If we someone went on hold we want the other side to reinvite back to us */
1081                                         if (who == c0) {
1082                                                 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1083                                         } else {
1084                                                 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1085                                         }
1086                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
1087                                         /* If they went off hold they should go back to being direct */
1088                                         if (who == c0) {
1089                                                 glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0);
1090                                         } else {
1091                                                 glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0);
1092                                         }
1093                                 }
1094                                 /* Update local address information */
1095                                 ast_rtp_instance_get_remote_address(instance0, &t0);
1096                                 memcpy(&ac0, &t0, sizeof(ac0));
1097                                 ast_rtp_instance_get_remote_address(instance1, &t1);
1098                                 memcpy(&ac1, &t1, sizeof(ac1));
1099                                 /* Update codec information */
1100                                 if (glue0->get_codec && c0->tech_pvt) {
1101                                         oldcodec0 = codec0 = glue0->get_codec(c0);
1102                                 }
1103                                 if (glue1->get_codec && c1->tech_pvt) {
1104                                         oldcodec1 = codec1 = glue1->get_codec(c1);
1105                                 }
1106                                 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1107                                 ast_frfree(fr);
1108                         } else {
1109                                 *fo = fr;
1110                                 *rc = who;
1111                                 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
1112                                 return AST_BRIDGE_COMPLETE;
1113                         }
1114                 } else {
1115                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1116                             (fr->frametype == AST_FRAME_DTMF_END) ||
1117                             (fr->frametype == AST_FRAME_VOICE) ||
1118                             (fr->frametype == AST_FRAME_VIDEO) ||
1119                             (fr->frametype == AST_FRAME_IMAGE) ||
1120                             (fr->frametype == AST_FRAME_HTML) ||
1121                             (fr->frametype == AST_FRAME_MODEM) ||
1122                             (fr->frametype == AST_FRAME_TEXT)) {
1123                                 ast_write(other, fr);
1124                         }
1125                         ast_frfree(fr);
1126                 }
1127                 /* Swap priority */
1128                 cs[2] = cs[0];
1129                 cs[0] = cs[1];
1130                 cs[1] = cs[2];
1131         }
1132
1133         if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1134                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
1135         }
1136         if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1137                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
1138         }
1139
1140         instance0->bridged = NULL;
1141         instance1->bridged = NULL;
1142
1143         ast_poll_channel_del(c0, c1);
1144
1145         return res;
1146 }
1147
1148 /*!
1149  * \brief Conditionally unref an rtp instance
1150  */
1151 static void unref_instance_cond(struct ast_rtp_instance **instance)
1152 {
1153         if (*instance) {
1154                 ao2_ref(*instance, -1);
1155                 *instance = NULL;
1156         }
1157 }
1158
1159 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)
1160 {
1161         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1162                         *vinstance0 = NULL, *vinstance1 = NULL,
1163                         *tinstance0 = NULL, *tinstance1 = NULL;
1164         struct ast_rtp_glue *glue0, *glue1;
1165         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;
1166         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;
1167         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1168         format_t codec0 = 0, codec1 = 0;
1169         int unlock_chans = 1;
1170
1171         /* Lock both channels so we can look for the glue that binds them together */
1172         ast_channel_lock(c0);
1173         while (ast_channel_trylock(c1)) {
1174                 ast_channel_unlock(c0);
1175                 usleep(1);
1176                 ast_channel_lock(c0);
1177         }
1178
1179         /* Ensure neither channel got hungup during lock avoidance */
1180         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1181                 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
1182                 goto done;
1183         }
1184
1185         /* Grab glue that binds each channel to something using the RTP engine */
1186         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1187                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1188                 goto done;
1189         }
1190
1191         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1192         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1193         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1194
1195         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1196         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1197         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1198
1199         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1200         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)) {
1201                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1202         }
1203         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)) {
1204                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1205         }
1206
1207         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1208         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1209                 res = AST_BRIDGE_FAILED_NOWARN;
1210                 goto done;
1211         }
1212
1213         /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1214         if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && instance0->properties[AST_RTP_PROPERTY_DTMF]) {
1215                 res = AST_BRIDGE_FAILED_NOWARN;
1216                 goto done;
1217         }
1218         if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && instance1->properties[AST_RTP_PROPERTY_DTMF]) {
1219                 res = AST_BRIDGE_FAILED_NOWARN;
1220                 goto done;
1221         }
1222
1223         /* 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 */
1224         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)))) {
1225                 res = AST_BRIDGE_FAILED_NOWARN;
1226                 goto done;
1227         }
1228
1229         /* Make sure that codecs match */
1230         codec0 = glue0->get_codec ? glue0->get_codec(c0) : 0;
1231         codec1 = glue1->get_codec ? glue1->get_codec(c1) : 0;
1232         if (codec0 && codec1 && !(codec0 & codec1)) {
1233                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n", ast_getformatname(codec0), ast_getformatname(codec1));
1234                 res = AST_BRIDGE_FAILED_NOWARN;
1235                 goto done;
1236         }
1237
1238         instance0->glue = glue0;
1239         instance1->glue = glue1;
1240         instance0->chan = c0;
1241         instance1->chan = c1;
1242
1243         /* Depending on the end result for bridging either do a local bridge or remote bridge */
1244         if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1245                 ast_verbose(VERBOSE_PREFIX_3 "Locally bridging %s and %s\n", c0->name, c1->name);
1246                 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
1247         } else {
1248                 ast_verbose(VERBOSE_PREFIX_3 "Remotely bridging %s and %s\n", c0->name, c1->name);
1249                 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1250                                 tinstance0, tinstance1, glue0, glue1, codec0, codec1, timeoutms, flags,
1251                                 fo, rc, c0->tech_pvt, c1->tech_pvt);
1252         }
1253
1254         instance0->glue = NULL;
1255         instance1->glue = NULL;
1256         instance0->chan = NULL;
1257         instance1->chan = NULL;
1258
1259         unlock_chans = 0;
1260
1261 done:
1262         if (unlock_chans) {
1263                 ast_channel_unlock(c0);
1264                 ast_channel_unlock(c1);
1265         }
1266
1267         unref_instance_cond(&instance0);
1268         unref_instance_cond(&instance1);
1269         unref_instance_cond(&vinstance0);
1270         unref_instance_cond(&vinstance1);
1271         unref_instance_cond(&tinstance0);
1272         unref_instance_cond(&tinstance1);
1273
1274         return res;
1275 }
1276
1277 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1278 {
1279         return instance->bridged;
1280 }
1281
1282 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1283 {
1284         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1285                 *vinstance0 = NULL, *vinstance1 = NULL,
1286                 *tinstance0 = NULL, *tinstance1 = NULL;
1287         struct ast_rtp_glue *glue0, *glue1;
1288         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;
1289         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;
1290         format_t codec0 = 0, codec1 = 0;
1291         int res = 0;
1292
1293         /* Lock both channels so we can look for the glue that binds them together */
1294         ast_channel_lock(c0);
1295         while (ast_channel_trylock(c1)) {
1296                 ast_channel_unlock(c0);
1297                 usleep(1);
1298                 ast_channel_lock(c0);
1299         }
1300
1301         /* Grab glue that binds each channel to something using the RTP engine */
1302         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1303                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1304                 goto done;
1305         }
1306
1307         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1308         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1309         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1310
1311         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1312         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1313         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1314
1315         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1316         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)) {
1317                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1318         }
1319         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)) {
1320                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1321         }
1322         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)) {
1323                 codec0 = glue0->get_codec(c0);
1324         }
1325         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)) {
1326                 codec1 = glue1->get_codec(c1);
1327         }
1328
1329         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1330         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1331                 goto done;
1332         }
1333
1334         /* Make sure we have matching codecs */
1335         if (!(codec0 & codec1)) {
1336                 goto done;
1337         }
1338
1339         ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1340
1341         if (vinstance0 && vinstance1) {
1342                 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1343         }
1344         if (tinstance0 && tinstance1) {
1345                 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1346         }
1347
1348         res = 0;
1349
1350 done:
1351         ast_channel_unlock(c0);
1352         ast_channel_unlock(c1);
1353
1354         unref_instance_cond(&instance0);
1355         unref_instance_cond(&instance1);
1356         unref_instance_cond(&vinstance0);
1357         unref_instance_cond(&vinstance1);
1358         unref_instance_cond(&tinstance0);
1359         unref_instance_cond(&tinstance1);
1360
1361         if (!res) {
1362                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1363         }
1364 }
1365
1366 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1367 {
1368         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1369                         *vinstance0 = NULL, *vinstance1 = NULL,
1370                         *tinstance0 = NULL, *tinstance1 = NULL;
1371         struct ast_rtp_glue *glue0, *glue1;
1372         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;
1373         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;
1374         format_t codec0 = 0, codec1 = 0;
1375         int res = 0;
1376
1377         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1378         if (!c1) {
1379                 return -1;
1380         }
1381
1382         /* Lock both channels so we can look for the glue that binds them together */
1383         ast_channel_lock(c0);
1384         while (ast_channel_trylock(c1)) {
1385                 ast_channel_unlock(c0);
1386                 usleep(1);
1387                 ast_channel_lock(c0);
1388         }
1389
1390         /* Grab glue that binds each channel to something using the RTP engine */
1391         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1392                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1393                 goto done;
1394         }
1395
1396         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1397         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1398         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1399
1400         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1401         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1402         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1403
1404         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1405         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)) {
1406                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1407         }
1408         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)) {
1409                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1410         }
1411         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)) {
1412                 codec0 = glue0->get_codec(c0);
1413         }
1414         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)) {
1415                 codec1 = glue1->get_codec(c1);
1416         }
1417
1418         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1419         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1420                 goto done;
1421         }
1422
1423         /* Make sure we have matching codecs */
1424         if (!(codec0 & codec1)) {
1425                 goto done;
1426         }
1427
1428         /* Bridge media early */
1429         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0)) {
1430                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1431         }
1432
1433         res = 0;
1434
1435 done:
1436         ast_channel_unlock(c0);
1437         ast_channel_unlock(c1);
1438
1439         unref_instance_cond(&instance0);
1440         unref_instance_cond(&instance1);
1441         unref_instance_cond(&vinstance0);
1442         unref_instance_cond(&vinstance1);
1443         unref_instance_cond(&tinstance0);
1444         unref_instance_cond(&tinstance1);
1445
1446         if (!res) {
1447                 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1448         }
1449
1450         return res;
1451 }
1452
1453 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1454 {
1455         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1456 }
1457
1458 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1459 {
1460         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1461 }
1462
1463 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1464 {
1465         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1466 }
1467
1468 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1469 {
1470         struct ast_rtp_instance_stats stats = { 0, };
1471         enum ast_rtp_instance_stat stat;
1472
1473         /* Determine what statistics we will need to retrieve based on field passed in */
1474         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1475                 stat = AST_RTP_INSTANCE_STAT_ALL;
1476         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1477                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1478         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1479                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1480         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1481                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1482         } else {
1483                 return NULL;
1484         }
1485
1486         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1487         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1488                 return NULL;
1489         }
1490
1491         /* Now actually fill the buffer with the good information */
1492         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1493                 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%u;rxcount=%u;txjitter=%u;txcount=%u;rlp=%u;rtt=%u",
1494                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1495         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1496                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1497                          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));
1498         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1499                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1500                          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));
1501         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1502                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1503         }
1504
1505         return buf;
1506 }
1507
1508 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1509 {
1510         char quality_buf[AST_MAX_USER_FIELD], *quality;
1511         struct ast_channel *bridge = ast_bridged_channel(chan);
1512
1513         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1514                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1515                 if (bridge) {
1516                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1517                 }
1518         }
1519
1520         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1521                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1522                 if (bridge) {
1523                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1524                 }
1525         }
1526
1527         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1528                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1529                 if (bridge) {
1530                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1531                 }
1532         }
1533
1534         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1535                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1536                 if (bridge) {
1537                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1538                 }
1539         }
1540 }
1541
1542 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, format_t format)
1543 {
1544         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1545 }
1546
1547 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, format_t format)
1548 {
1549         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1550 }
1551
1552 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1553 {
1554         struct ast_rtp_glue *glue;
1555         struct ast_rtp_instance *peer_instance = NULL;
1556         int res = -1;
1557
1558         if (!instance->engine->make_compatible) {
1559                 return -1;
1560         }
1561
1562         ast_channel_lock(peer);
1563
1564         if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
1565                 ast_channel_unlock(peer);
1566                 return -1;
1567         }
1568
1569         glue->get_rtp_info(peer, &peer_instance);
1570
1571         if (!peer_instance || peer_instance->engine != instance->engine) {
1572                 ast_channel_unlock(peer);
1573                 ao2_ref(peer_instance, -1);
1574                 peer_instance = NULL;
1575                 return -1;
1576         }
1577
1578         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1579
1580         ast_channel_unlock(peer);
1581
1582         ao2_ref(peer_instance, -1);
1583         peer_instance = NULL;
1584
1585         return res;
1586 }
1587
1588 format_t ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, int to_endpoint, int to_asterisk)
1589 {
1590         format_t formats;
1591
1592         if (instance->engine->available_formats && (formats = instance->engine->available_formats(instance, to_endpoint, to_asterisk))) {
1593                 return formats;
1594         }
1595
1596         return ast_translate_available_formats(to_endpoint, to_asterisk);
1597 }
1598
1599 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1600 {
1601         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1602 }
1603
1604 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance, struct sockaddr_in *suggestion, const char *username)
1605 {
1606         if (instance->engine->stun_request) {
1607                 instance->engine->stun_request(instance, suggestion, username);
1608         }
1609 }
1610
1611 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1612 {
1613         instance->timeout = timeout;
1614 }
1615
1616 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1617 {
1618         instance->holdtimeout = timeout;
1619 }
1620
1621 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1622 {
1623         return instance->timeout;
1624 }
1625
1626 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1627 {
1628         return instance->holdtimeout;
1629 }
1630
1631 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1632 {
1633         return instance->engine;
1634 }
1635
1636 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1637 {
1638         return instance->glue;
1639 }
1640
1641 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
1642 {
1643         return instance->chan;
1644 }