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