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