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