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