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