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