Asterisk media architecture conversion - no more format bitfields
[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, {.id = AST_FORMAT_G723_1}, 0}, "audio", "G723", 8000},
93         {{1, {.id = AST_FORMAT_GSM}, 0}, "audio", "GSM", 8000},
94         {{1, {.id = AST_FORMAT_ULAW}, 0}, "audio", "PCMU", 8000},
95         {{1, {.id = AST_FORMAT_ULAW}, 0}, "audio", "G711U", 8000},
96         {{1, {.id = AST_FORMAT_ALAW}, 0}, "audio", "PCMA", 8000},
97         {{1, {.id = AST_FORMAT_ALAW}, 0}, "audio", "G711A", 8000},
98         {{1, {.id = AST_FORMAT_G726}, 0}, "audio", "G726-32", 8000},
99         {{1, {.id = AST_FORMAT_ADPCM}, 0}, "audio", "DVI4", 8000},
100         {{1, {.id = AST_FORMAT_SLINEAR}, 0}, "audio", "L16", 8000},
101         {{1, {.id = AST_FORMAT_SLINEAR16}, 0}, "audio", "L16", 16000},
102         {{1, {.id = AST_FORMAT_LPC10}, 0}, "audio", "LPC", 8000},
103         {{1, {.id = AST_FORMAT_G729A}, 0}, "audio", "G729", 8000},
104         {{1, {.id = AST_FORMAT_G729A}, 0}, "audio", "G729A", 8000},
105         {{1, {.id = AST_FORMAT_G729A}, 0}, "audio", "G.729", 8000},
106         {{1, {.id = AST_FORMAT_SPEEX}, 0}, "audio", "speex", 8000},
107         {{1, {.id = AST_FORMAT_SPEEX16}, 0},  "audio", "speex", 16000},
108         {{1, {.id = AST_FORMAT_ILBC}, 0}, "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, {.id = AST_FORMAT_G722}, 0}, "audio", "G722", 8000},
113         {{1, {.id = AST_FORMAT_G726_AAL2}, 0}, "audio", "AAL2-G726-32", 8000},
114         {{0, {.id = 0}, AST_RTP_DTMF}, "audio", "telephone-event", 8000},
115         {{0, {.id = 0}, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event", 8000},
116         {{0, {.id = 0}, AST_RTP_CN}, "audio", "CN", 8000},
117         {{1, {.id = AST_FORMAT_JPEG}, 0}, "video", "JPEG", 90000},
118         {{1, {.id = AST_FORMAT_PNG}, 0}, "video", "PNG", 90000},
119         {{1, {.id = AST_FORMAT_H261}, 0}, "video", "H261", 90000},
120         {{1, {.id = AST_FORMAT_H263}, 0}, "video", "H263", 90000},
121         {{1, {.id = AST_FORMAT_H263_PLUS}, 0}, "video", "h263-1998", 90000},
122         {{1, {.id = AST_FORMAT_H264}, 0}, "video", "H264", 90000},
123         {{1, {.id = AST_FORMAT_MP4_VIDEO}, 0}, "video", "MP4V-ES", 90000},
124         {{1, {.id = AST_FORMAT_T140RED}, 0}, "text", "RED", 1000},
125         {{1, {.id = AST_FORMAT_T140}, 0}, "text", "T140", 1000},
126         {{1, {.id = AST_FORMAT_SIREN7}, 0}, "audio", "G7221", 16000},
127         {{1, {.id = AST_FORMAT_SIREN14}, 0}, "audio", "G7221", 32000},
128         {{1, {.id = AST_FORMAT_G719}, 0}, "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, {.id = AST_FORMAT_ULAW}, 0},
143         #ifdef USE_DEPRECATED_G726
144         [2] = {1, {.id = AST_FORMAT_G726}, 0},/* Technically this is G.721, but if Cisco can do it, so can we... */
145         #endif
146         [3] = {1, {.id = AST_FORMAT_GSM}, 0},
147         [4] = {1, {.id = AST_FORMAT_G723_1}, 0},
148         [5] = {1, {.id = AST_FORMAT_ADPCM}, 0},/* 8 kHz */
149         [6] = {1, {.id = AST_FORMAT_ADPCM}, 0}, /* 16 kHz */
150         [7] = {1, {.id = AST_FORMAT_LPC10}, 0},
151         [8] = {1, {.id = AST_FORMAT_ALAW}, 0},
152         [9] = {1, {.id = AST_FORMAT_G722}, 0},
153         [10] = {1, {.id = AST_FORMAT_SLINEAR}, 0}, /* 2 channels */
154         [11] = {1, {.id = AST_FORMAT_SLINEAR}, 0}, /* 1 channel */
155         [13] = {0, {.id = 0}, AST_RTP_CN},
156         [16] = {1, {.id = AST_FORMAT_ADPCM}, 0}, /* 11.025 kHz */
157         [17] = {1, {.id = AST_FORMAT_ADPCM}, 0}, /* 22.050 kHz */
158         [18] = {1, {.id = AST_FORMAT_G729A}, 0},
159         [19] = {0, {.id = 0}, AST_RTP_CN},         /* Also used for CN */
160         [26] = {1, {.id = AST_FORMAT_JPEG}, 0},
161         [31] = {1, {.id = AST_FORMAT_H261}, 0},
162         [34] = {1, {.id = AST_FORMAT_H263}, 0},
163         [97] = {1, {.id = AST_FORMAT_ILBC}, 0},
164         [98] = {1, {.id = AST_FORMAT_H263_PLUS}, 0},
165         [99] = {1, {.id = AST_FORMAT_H264}, 0},
166         [101] = {0, {.id = 0}, AST_RTP_DTMF},
167         [102] = {1, {.id = AST_FORMAT_SIREN7}, 0},
168         [103] = {1, {.id = AST_FORMAT_H263_PLUS}, 0},
169         [104] = {1, {.id = AST_FORMAT_MP4_VIDEO}, 0},
170         [105] = {1, {.id = AST_FORMAT_T140RED}, 0},   /* Real time text chat (with redundancy encoding) */
171         [106] = {1, {.id = AST_FORMAT_T140}, 0},      /* Real time text chat */
172         [110] = {1, {.id = AST_FORMAT_SPEEX}, 0},
173         [111] = {1, {.id = AST_FORMAT_G726}, 0},
174         [112] = {1, {.id = AST_FORMAT_G726_AAL2}, 0},
175         [115] = {1, {.id = AST_FORMAT_SIREN14}, 0},
176         [116] = {1, {.id = AST_FORMAT_G719}, 0},
177         [117] = {1, {.id = AST_FORMAT_SPEEX16}, 0},
178         [118] = {1, {.id = AST_FORMAT_SLINEAR16}, 0}, /* 16 Khz signed linear */
179         [121] = {0, {.id = 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 ast_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_and_cmp_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 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
425                 struct ast_sockaddr *address)
426 {
427         ast_sockaddr_copy(address, &instance->local_address);
428 }
429
430 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
431                 struct ast_sockaddr *address)
432 {
433         if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
434                 ast_sockaddr_copy(address, &instance->remote_address);
435                 return 1;
436         }
437
438         return 0;
439 }
440
441 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
442                 struct ast_sockaddr *address)
443 {
444         ast_sockaddr_copy(address, &instance->remote_address);
445 }
446
447 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
448 {
449         if (instance->engine->extended_prop_set) {
450                 instance->engine->extended_prop_set(instance, property, value);
451         }
452 }
453
454 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
455 {
456         if (instance->engine->extended_prop_get) {
457                 return instance->engine->extended_prop_get(instance, property);
458         }
459
460         return NULL;
461 }
462
463 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
464 {
465         instance->properties[property] = value;
466
467         if (instance->engine->prop_set) {
468                 instance->engine->prop_set(instance, property, value);
469         }
470 }
471
472 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
473 {
474         return instance->properties[property];
475 }
476
477 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
478 {
479         return &instance->codecs;
480 }
481
482 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
483 {
484         int i;
485
486         for (i = 0; i < AST_RTP_MAX_PT; i++) {
487                 codecs->payloads[i].asterisk_format = 0;
488                 codecs->payloads[i].rtp_code = 0;
489                 ast_format_clear(&codecs->payloads[i].format);
490                 if (instance && instance->engine && instance->engine->payload_set) {
491                         instance->engine->payload_set(instance, i, 0, NULL, 0);
492                 }
493         }
494 }
495
496 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
497 {
498         int i;
499
500         for (i = 0; i < AST_RTP_MAX_PT; i++) {
501                 if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
502
503                         codecs->payloads[i].asterisk_format = static_RTP_PT[i].asterisk_format;
504                         codecs->payloads[i].rtp_code = static_RTP_PT[i].rtp_code;
505                         ast_format_copy(&codecs->payloads[i].format, &static_RTP_PT[i].format);
506                         if (instance && instance->engine && instance->engine->payload_set) {
507                                 instance->engine->payload_set(instance, i, codecs->payloads[i].asterisk_format, &codecs->payloads[i].format, codecs->payloads[i].rtp_code);
508                         }
509                 }
510         }
511 }
512
513 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
514 {
515         int i;
516
517         for (i = 0; i < AST_RTP_MAX_PT; i++) {
518                 if (src->payloads[i].rtp_code || src->payloads[i].asterisk_format) {
519                         ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
520                         dest->payloads[i].asterisk_format = src->payloads[i].asterisk_format;
521                         dest->payloads[i].rtp_code = src->payloads[i].rtp_code;
522                         ast_format_copy(&dest->payloads[i].format, &src->payloads[i].format);
523                         if (instance && instance->engine && instance->engine->payload_set) {
524                                 instance->engine->payload_set(instance, i, dest->payloads[i].asterisk_format, &dest->payloads[i].format, dest->payloads[i].rtp_code);
525                         }
526                 }
527         }
528 }
529
530 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
531 {
532         if (payload < 0 || payload >= AST_RTP_MAX_PT || (!static_RTP_PT[payload].rtp_code && !static_RTP_PT[payload].asterisk_format)) {
533                 return;
534         }
535
536         codecs->payloads[payload].asterisk_format = static_RTP_PT[payload].asterisk_format;
537         codecs->payloads[payload].rtp_code = static_RTP_PT[payload].rtp_code;
538         ast_format_copy(&codecs->payloads[payload].format, &static_RTP_PT[payload].format);
539
540         ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
541
542         if (instance && instance->engine && instance->engine->payload_set) {
543                 instance->engine->payload_set(instance, payload, codecs->payloads[payload].asterisk_format, &codecs->payloads[payload].format, codecs->payloads[payload].rtp_code);
544         }
545 }
546
547 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
548                                  char *mimetype, char *mimesubtype,
549                                  enum ast_rtp_options options,
550                                  unsigned int sample_rate)
551 {
552         unsigned int i;
553         int found = 0;
554
555         if (pt < 0 || pt >= AST_RTP_MAX_PT)
556                 return -1; /* bogus payload type */
557
558         for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
559                 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
560
561                 if (strcasecmp(mimesubtype, t->subtype)) {
562                         continue;
563                 }
564
565                 if (strcasecmp(mimetype, t->type)) {
566                         continue;
567                 }
568
569                 /* if both sample rates have been supplied, and they don't match,
570                                       then this not a match; if one has not been supplied, then the
571                                       rates are not compared */
572                 if (sample_rate && t->sample_rate &&
573                     (sample_rate != t->sample_rate)) {
574                         continue;
575                 }
576
577                 found = 1;
578                 codecs->payloads[pt] = t->payload_type;
579
580                 if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
581                         ast_format_set(&codecs->payloads[pt].format, AST_FORMAT_G726_AAL2, 0);
582                 }
583
584                 if (instance && instance->engine && instance->engine->payload_set) {
585                         instance->engine->payload_set(instance, pt, codecs->payloads[i].asterisk_format, &codecs->payloads[i].format, codecs->payloads[i].rtp_code);
586                 }
587
588                 break;
589         }
590
591         return (found ? 0 : -2);
592 }
593
594 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)
595 {
596         return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
597 }
598
599 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
600 {
601         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
602                 return;
603         }
604
605         ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
606
607         codecs->payloads[payload].asterisk_format = 0;
608         codecs->payloads[payload].rtp_code = 0;
609         ast_format_clear(&codecs->payloads[payload].format);
610
611         if (instance && instance->engine && instance->engine->payload_set) {
612                 instance->engine->payload_set(instance, payload, 0, NULL, 0);
613         }
614 }
615
616 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
617 {
618         struct ast_rtp_payload_type result = { .asterisk_format = 0, };
619
620         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
621                 return result;
622         }
623
624         result.asterisk_format = codecs->payloads[payload].asterisk_format;
625         result.rtp_code = codecs->payloads[payload].rtp_code;
626         ast_format_copy(&result.format, &codecs->payloads[payload].format);
627
628         if (!result.rtp_code && !result.asterisk_format) {
629                 result = static_RTP_PT[payload];
630         }
631
632         return result;
633 }
634
635 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
636 {
637         int i;
638
639         ast_format_cap_remove_all(astformats);
640         *nonastformats = 0;
641
642         for (i = 0; i < AST_RTP_MAX_PT; i++) {
643                 if (codecs->payloads[i].rtp_code || codecs->payloads[i].asterisk_format) {
644                         ast_debug(1, "Incorporating payload %d on %p\n", i, codecs);
645                 }
646                 if (codecs->payloads[i].asterisk_format) {
647                         ast_format_cap_add(astformats, &codecs->payloads[i].format);
648                 } else {
649                         *nonastformats |= codecs->payloads[i].rtp_code;
650                 }
651         }
652 }
653
654 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
655 {
656         int i;
657
658         for (i = 0; i < AST_RTP_MAX_PT; i++) {
659                 if (codecs->payloads[i].asterisk_format && asterisk_format && format &&
660                         (ast_format_cmp(format, &codecs->payloads[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
661                         return i;
662                 } else if (!codecs->payloads[i].asterisk_format && !asterisk_format &&
663                         (codecs->payloads[i].rtp_code == code)) {
664                         return i;
665                 }
666         }
667
668         for (i = 0; i < AST_RTP_MAX_PT; i++) {
669                 if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
670                         (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
671                         return i;
672                 } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
673                         (static_RTP_PT[i].rtp_code == code)) {
674                         return i;
675                 }
676         }
677
678         return -1;
679 }
680
681 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
682 {
683         int i;
684
685         for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); i++) {
686                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
687                         (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
688                         if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
689                                 return "G726-32";
690                         } else {
691                                 return ast_rtp_mime_types[i].subtype;
692                         }
693                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
694                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
695
696                         return ast_rtp_mime_types[i].subtype;
697                 }
698         }
699
700         return "";
701 }
702
703 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
704 {
705         unsigned int i;
706
707         for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
708                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
709                         (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
710                         return ast_rtp_mime_types[i].sample_rate;
711                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
712                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
713                         return ast_rtp_mime_types[i].sample_rate;
714                 }
715         }
716
717         return 0;
718 }
719
720 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *ast_format_capability, int rtp_capability, const int asterisk_format, enum ast_rtp_options options)
721 {
722         int found = 0;
723         const char *name;
724         if (!buf) {
725                 return NULL;
726         }
727
728
729         if (asterisk_format) {
730                 struct ast_format tmp_fmt;
731                 ast_format_cap_iter_start(ast_format_capability);
732                 while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
733                         name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
734                         ast_str_append(&buf, 0, "%s|", name);
735                         found = 1;
736                 }
737                 ast_format_cap_iter_end(ast_format_capability);
738
739         } else {
740                 int x;
741                 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
742                 for (x = 1; x < AST_RTP_MAX; x <<= 1) {
743                         if (rtp_capability & x) {
744                                 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
745                                 ast_str_append(&buf, 0, "%s|", name);
746                                 found = 1;
747                         }
748                 }
749         }
750
751         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
752
753         return ast_str_buffer(buf);
754 }
755
756 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
757 {
758         codecs->pref = *prefs;
759
760         if (instance && instance->engine->packetization_set) {
761                 instance->engine->packetization_set(instance, &instance->codecs.pref);
762         }
763 }
764
765 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
766 {
767         return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
768 }
769
770 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
771 {
772         return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
773 }
774 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
775 {
776         return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
777 }
778
779 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
780 {
781         if (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) {
782                 return -1;
783         }
784
785         instance->dtmf_mode = dtmf_mode;
786
787         return 0;
788 }
789
790 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
791 {
792         return instance->dtmf_mode;
793 }
794
795 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
796 {
797         if (instance->engine->update_source) {
798                 instance->engine->update_source(instance);
799         }
800 }
801
802 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
803 {
804         if (instance->engine->change_source) {
805                 instance->engine->change_source(instance);
806         }
807 }
808
809 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
810 {
811         return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
812 }
813
814 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
815 {
816         if (instance->engine->stop) {
817                 instance->engine->stop(instance);
818         }
819 }
820
821 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
822 {
823         return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
824 }
825
826 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
827 {
828         struct ast_rtp_glue *glue = NULL;
829
830         AST_RWLIST_RDLOCK(&glues);
831
832         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
833                 if (!strcasecmp(glue->type, type)) {
834                         break;
835                 }
836         }
837
838         AST_RWLIST_UNLOCK(&glues);
839
840         return glue;
841 }
842
843 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)
844 {
845         enum ast_bridge_result res = AST_BRIDGE_FAILED;
846         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
847         struct ast_frame *fr = NULL;
848
849         /* Start locally bridging both instances */
850         if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
851                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
852                 ast_channel_unlock(c0);
853                 ast_channel_unlock(c1);
854                 return AST_BRIDGE_FAILED_NOWARN;
855         }
856         if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
857                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
858                 if (instance0->engine->local_bridge) {
859                         instance0->engine->local_bridge(instance0, NULL);
860                 }
861                 ast_channel_unlock(c0);
862                 ast_channel_unlock(c1);
863                 return AST_BRIDGE_FAILED_NOWARN;
864         }
865
866         ast_channel_unlock(c0);
867         ast_channel_unlock(c1);
868
869         instance0->bridged = instance1;
870         instance1->bridged = instance0;
871
872         ast_poll_channel_add(c0, c1);
873
874         /* Hop into a loop waiting for a frame from either channel */
875         cs[0] = c0;
876         cs[1] = c1;
877         cs[2] = NULL;
878         for (;;) {
879                 /* If the underlying formats have changed force this bridge to break */
880                 if ((ast_format_cmp(&c0->rawreadformat, &c1->rawwriteformat) == AST_FORMAT_CMP_NOT_EQUAL) ||
881                         (ast_format_cmp(&c1->rawreadformat, &c0->rawwriteformat) == AST_FORMAT_CMP_NOT_EQUAL)) {
882                         ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
883                         res = AST_BRIDGE_FAILED_NOWARN;
884                         break;
885                 }
886                 /* Check if anything changed */
887                 if ((c0->tech_pvt != pvt0) ||
888                     (c1->tech_pvt != pvt1) ||
889                     (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
890                     (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
891                         ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
892                         /* 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 */
893                         if ((c0->masq || c0->masqr) && (fr = ast_read(c0))) {
894                                 ast_frfree(fr);
895                         }
896                         if ((c1->masq || c1->masqr) && (fr = ast_read(c1))) {
897                                 ast_frfree(fr);
898                         }
899                         res = AST_BRIDGE_RETRY;
900                         break;
901                 }
902                 /* Wait on a channel to feed us a frame */
903                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
904                         if (!timeoutms) {
905                                 res = AST_BRIDGE_RETRY;
906                                 break;
907                         }
908                         ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
909                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
910                                 break;
911                         }
912                         continue;
913                 }
914                 /* Read in frame from channel */
915                 fr = ast_read(who);
916                 other = (who == c0) ? c1 : c0;
917                 /* Depending on the frame we may need to break out of our bridge */
918                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
919                             ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
920                             ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
921                         /* Record received frame and who */
922                         *fo = fr;
923                         *rc = who;
924                         ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
925                         res = AST_BRIDGE_COMPLETE;
926                         break;
927                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
928                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
929                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
930                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
931                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
932                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
933                                 /* If we are going on hold, then break callback mode and P2P bridging */
934                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
935                                         if (instance0->engine->local_bridge) {
936                                                 instance0->engine->local_bridge(instance0, NULL);
937                                         }
938                                         if (instance1->engine->local_bridge) {
939                                                 instance1->engine->local_bridge(instance1, NULL);
940                                         }
941                                         instance0->bridged = NULL;
942                                         instance1->bridged = NULL;
943                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
944                                         if (instance0->engine->local_bridge) {
945                                                 instance0->engine->local_bridge(instance0, instance1);
946                                         }
947                                         if (instance1->engine->local_bridge) {
948                                                 instance1->engine->local_bridge(instance1, instance0);
949                                         }
950                                         instance0->bridged = instance1;
951                                         instance1->bridged = instance0;
952                                 }
953                                 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
954                                 ast_frfree(fr);
955                         } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
956                                 if (ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
957                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
958                                 }
959                                 ast_frfree(fr);
960                         } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
961                                 if (ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
962                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
963                                 }
964                                 ast_frfree(fr);
965                         } else {
966                                 *fo = fr;
967                                 *rc = who;
968                                 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
969                                 res = AST_BRIDGE_COMPLETE;
970                                 break;
971                         }
972                 } else {
973                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
974                             (fr->frametype == AST_FRAME_DTMF_END) ||
975                             (fr->frametype == AST_FRAME_VOICE) ||
976                             (fr->frametype == AST_FRAME_VIDEO) ||
977                             (fr->frametype == AST_FRAME_IMAGE) ||
978                             (fr->frametype == AST_FRAME_HTML) ||
979                             (fr->frametype == AST_FRAME_MODEM) ||
980                             (fr->frametype == AST_FRAME_TEXT)) {
981                                 ast_write(other, fr);
982                         }
983
984                         ast_frfree(fr);
985                 }
986                 /* Swap priority */
987                 cs[2] = cs[0];
988                 cs[0] = cs[1];
989                 cs[1] = cs[2];
990         }
991
992         /* Stop locally bridging both instances */
993         if (instance0->engine->local_bridge) {
994                 instance0->engine->local_bridge(instance0, NULL);
995         }
996         if (instance1->engine->local_bridge) {
997                 instance1->engine->local_bridge(instance1, NULL);
998         }
999
1000         instance0->bridged = NULL;
1001         instance1->bridged = NULL;
1002
1003         ast_poll_channel_del(c0, c1);
1004
1005         return res;
1006 }
1007
1008 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
1009         struct ast_channel *c1,
1010         struct ast_rtp_instance *instance0,
1011         struct ast_rtp_instance *instance1,
1012         struct ast_rtp_instance *vinstance0,
1013         struct ast_rtp_instance *vinstance1,
1014         struct ast_rtp_instance *tinstance0,
1015         struct ast_rtp_instance *tinstance1,
1016         struct ast_rtp_glue *glue0,
1017         struct ast_rtp_glue *glue1,
1018         struct ast_format_cap *cap0,
1019         struct ast_format_cap *cap1,
1020         int timeoutms,
1021         int flags,
1022         struct ast_frame **fo,
1023         struct ast_channel **rc,
1024         void *pvt0,
1025         void *pvt1)
1026 {
1027         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1028         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
1029         struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
1030         struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
1031         struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
1032         struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
1033         struct ast_frame *fr = NULL;
1034
1035         if (!oldcap0 || !oldcap1) {
1036                 ast_channel_unlock(c0);
1037                 ast_channel_unlock(c1);
1038                 goto remote_bridge_cleanup;
1039         }
1040         /* Test the first channel */
1041         if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
1042                 ast_rtp_instance_get_remote_address(instance1, &ac1);
1043                 if (vinstance1) {
1044                         ast_rtp_instance_get_remote_address(vinstance1, &vac1);
1045                 }
1046                 if (tinstance1) {
1047                         ast_rtp_instance_get_remote_address(tinstance1, &tac1);
1048                 }
1049         } else {
1050                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1051         }
1052
1053         /* Test the second channel */
1054         if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
1055                 ast_rtp_instance_get_remote_address(instance0, &ac0);
1056                 if (vinstance0) {
1057                         ast_rtp_instance_get_remote_address(instance0, &vac0);
1058                 }
1059                 if (tinstance0) {
1060                         ast_rtp_instance_get_remote_address(instance0, &tac0);
1061                 }
1062         } else {
1063                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
1064         }
1065
1066         ast_channel_unlock(c0);
1067         ast_channel_unlock(c1);
1068
1069         instance0->bridged = instance1;
1070         instance1->bridged = instance0;
1071
1072         ast_poll_channel_add(c0, c1);
1073
1074         /* Go into a loop handling any stray frames that may come in */
1075         cs[0] = c0;
1076         cs[1] = c1;
1077         cs[2] = NULL;
1078         for (;;) {
1079                 /* Check if anything changed */
1080                 if ((c0->tech_pvt != pvt0) ||
1081                     (c1->tech_pvt != pvt1) ||
1082                     (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
1083                     (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
1084                         ast_debug(1, "Oooh, something is weird, backing out\n");
1085                         res = AST_BRIDGE_RETRY;
1086                         break;
1087                 }
1088
1089                 /* Check if they have changed their address */
1090                 ast_rtp_instance_get_remote_address(instance1, &t1);
1091                 if (vinstance1) {
1092                         ast_rtp_instance_get_remote_address(vinstance1, &vt1);
1093                 }
1094                 if (tinstance1) {
1095                         ast_rtp_instance_get_remote_address(tinstance1, &tt1);
1096                 }
1097                 if (glue1->get_codec) {
1098                         ast_format_cap_remove_all(cap1);
1099                         glue1->get_codec(c1, cap1);
1100                 }
1101
1102                 ast_rtp_instance_get_remote_address(instance0, &t0);
1103                 if (vinstance0) {
1104                         ast_rtp_instance_get_remote_address(vinstance0, &vt0);
1105                 }
1106                 if (tinstance0) {
1107                         ast_rtp_instance_get_remote_address(tinstance0, &tt0);
1108                 }
1109                 if (glue0->get_codec) {
1110                         ast_format_cap_remove_all(cap0);
1111                         glue0->get_codec(c0, cap0);
1112                 }
1113
1114                 if ((ast_sockaddr_cmp(&t1, &ac1)) ||
1115                     (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
1116                     (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
1117                     (!ast_format_cap_identical(cap1, oldcap1))) {
1118                         char tmp_buf[512] = { 0, };
1119                         ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1120                                   c1->name, ast_sockaddr_stringify(&t1),
1121                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1122                         ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
1123                                   c1->name, ast_sockaddr_stringify(&vt1),
1124                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1125                         ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
1126                                   c1->name, ast_sockaddr_stringify(&tt1),
1127                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1128                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1129                                   c1->name, ast_sockaddr_stringify(&ac1),
1130                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1131                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1132                                   c1->name, ast_sockaddr_stringify(&vac1),
1133                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1134                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1135                                   c1->name, ast_sockaddr_stringify(&tac1),
1136                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1137                         if (glue0->update_peer(c0,
1138                                                ast_sockaddr_isnull(&t1)  ? NULL : instance1,
1139                                                ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
1140                                                ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
1141                                                cap1, 0)) {
1142                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1143                         }
1144                         ast_sockaddr_copy(&ac1, &t1);
1145                         ast_sockaddr_copy(&vac1, &vt1);
1146                         ast_sockaddr_copy(&tac1, &tt1);
1147                         ast_format_cap_copy(oldcap1, cap1);
1148                 }
1149                 if ((ast_sockaddr_cmp(&t0, &ac0)) ||
1150                     (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
1151                     (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
1152                     (!ast_format_cap_identical(cap0, oldcap0))) {
1153                         char tmp_buf[512] = { 0, };
1154                         ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1155                                   c0->name, ast_sockaddr_stringify(&t0),
1156                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
1157                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1158                                   c0->name, ast_sockaddr_stringify(&ac0),
1159                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
1160                         if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
1161                                                 vt0.len ? vinstance0 : NULL,
1162                                                 tt0.len ? tinstance0 : NULL,
1163                                                 cap0, 0)) {
1164                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1165                         }
1166                         ast_sockaddr_copy(&ac0, &t0);
1167                         ast_sockaddr_copy(&vac0, &vt0);
1168                         ast_sockaddr_copy(&tac0, &tt0);
1169                         ast_format_cap_copy(oldcap0, cap0);
1170                 }
1171
1172                 /* Wait for frame to come in on the channels */
1173                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1174                         if (!timeoutms) {
1175                                 res = AST_BRIDGE_RETRY;
1176                                 break;
1177                         }
1178                         ast_debug(1, "Ooh, empty read...\n");
1179                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1180                                 break;
1181                         }
1182                         continue;
1183                 }
1184                 fr = ast_read(who);
1185                 other = (who == c0) ? c1 : c0;
1186                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1187                             (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1188                              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1189                         /* Break out of bridge */
1190                         *fo = fr;
1191                         *rc = who;
1192                         ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1193                         res = AST_BRIDGE_COMPLETE;
1194                         break;
1195                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1196                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1197                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1198                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1199                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1200                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
1201                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1202                                         /* If we someone went on hold we want the other side to reinvite back to us */
1203                                         if (who == c0) {
1204                                                 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1205                                         } else {
1206                                                 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1207                                         }
1208                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
1209                                         /* If they went off hold they should go back to being direct */
1210                                         if (who == c0) {
1211                                                 glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
1212                                         } else {
1213                                                 glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
1214                                         }
1215                                 }
1216                                 /* Update local address information */
1217                                 ast_rtp_instance_get_remote_address(instance0, &t0);
1218                                 ast_sockaddr_copy(&ac0, &t0);
1219                                 ast_rtp_instance_get_remote_address(instance1, &t1);
1220                                 ast_sockaddr_copy(&ac1, &t1);
1221                                 /* Update codec information */
1222                                 if (glue0->get_codec && c0->tech_pvt) {
1223                                         ast_format_cap_remove_all(cap0);
1224                                         ast_format_cap_remove_all(oldcap0);
1225                                         glue0->get_codec(c0, cap0);
1226                                         ast_format_cap_append(oldcap0, cap0);
1227
1228                                 }
1229                                 if (glue1->get_codec && c1->tech_pvt) {
1230                                         ast_format_cap_remove_all(cap1);
1231                                         ast_format_cap_remove_all(oldcap1);
1232                                         glue0->get_codec(c1, cap1);
1233                                         ast_format_cap_append(oldcap1, cap1);
1234                                 }
1235                                 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1236                                 ast_frfree(fr);
1237                         } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
1238                                 if (ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
1239                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1240                                 }
1241                                 ast_frfree(fr);
1242                         } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
1243                                 if (ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
1244                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1245                                 }
1246                                 ast_frfree(fr);
1247                         } else {
1248                                 *fo = fr;
1249                                 *rc = who;
1250                                 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
1251                                 res = AST_BRIDGE_COMPLETE;
1252                                 goto remote_bridge_cleanup;
1253                         }
1254                 } else {
1255                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1256                             (fr->frametype == AST_FRAME_DTMF_END) ||
1257                             (fr->frametype == AST_FRAME_VOICE) ||
1258                             (fr->frametype == AST_FRAME_VIDEO) ||
1259                             (fr->frametype == AST_FRAME_IMAGE) ||
1260                             (fr->frametype == AST_FRAME_HTML) ||
1261                             (fr->frametype == AST_FRAME_MODEM) ||
1262                             (fr->frametype == AST_FRAME_TEXT)) {
1263                                 ast_write(other, fr);
1264                         }
1265                         ast_frfree(fr);
1266                 }
1267                 /* Swap priority */
1268                 cs[2] = cs[0];
1269                 cs[0] = cs[1];
1270                 cs[1] = cs[2];
1271         }
1272
1273         if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1274                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
1275         }
1276         if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1277                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
1278         }
1279
1280         instance0->bridged = NULL;
1281         instance1->bridged = NULL;
1282
1283         ast_poll_channel_del(c0, c1);
1284
1285 remote_bridge_cleanup:
1286         ast_format_cap_destroy(oldcap0);
1287         ast_format_cap_destroy(oldcap1);
1288
1289         return res;
1290 }
1291
1292 /*!
1293  * \brief Conditionally unref an rtp instance
1294  */
1295 static void unref_instance_cond(struct ast_rtp_instance **instance)
1296 {
1297         if (*instance) {
1298                 ao2_ref(*instance, -1);
1299                 *instance = NULL;
1300         }
1301 }
1302
1303 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)
1304 {
1305         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1306                         *vinstance0 = NULL, *vinstance1 = NULL,
1307                         *tinstance0 = NULL, *tinstance1 = NULL;
1308         struct ast_rtp_glue *glue0, *glue1;
1309         struct ast_sockaddr addr1, addr2;
1310         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;
1311         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;
1312         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1313         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1314         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1315         int unlock_chans = 1;
1316
1317         if (!cap0 || !cap1) {
1318                 unlock_chans = 0;
1319                 goto done;
1320         }
1321
1322         /* Lock both channels so we can look for the glue that binds them together */
1323         ast_channel_lock(c0);
1324         while (ast_channel_trylock(c1)) {
1325                 ast_channel_unlock(c0);
1326                 usleep(1);
1327                 ast_channel_lock(c0);
1328         }
1329
1330         /* Ensure neither channel got hungup during lock avoidance */
1331         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1332                 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
1333                 goto done;
1334         }
1335
1336         /* Grab glue that binds each channel to something using the RTP engine */
1337         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1338                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1339                 goto done;
1340         }
1341
1342         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1343         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1344         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1345
1346         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1347         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1348         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1349
1350         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1351         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)) {
1352                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1353         }
1354         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)) {
1355                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1356         }
1357
1358         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1359         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1360                 res = AST_BRIDGE_FAILED_NOWARN;
1361                 goto done;
1362         }
1363
1364
1365         /* If address families differ, force a local bridge */
1366         ast_rtp_instance_get_remote_address(instance0, &addr1);
1367         ast_rtp_instance_get_remote_address(instance1, &addr2);
1368
1369         if (addr1.ss.ss_family != addr2.ss.ss_family ||
1370            (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
1371                 audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
1372                 audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1373         }
1374
1375         /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1376         if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && instance0->properties[AST_RTP_PROPERTY_DTMF]) {
1377                 res = AST_BRIDGE_FAILED_NOWARN;
1378                 goto done;
1379         }
1380         if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && instance1->properties[AST_RTP_PROPERTY_DTMF]) {
1381                 res = AST_BRIDGE_FAILED_NOWARN;
1382                 goto done;
1383         }
1384
1385         /* 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 */
1386         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)))) {
1387                 res = AST_BRIDGE_FAILED_NOWARN;
1388                 goto done;
1389         }
1390
1391         /* Make sure that codecs match */
1392         if (glue0->get_codec){
1393                 glue0->get_codec(c0, cap0);
1394         }
1395         if (glue1->get_codec) {
1396                 glue1->get_codec(c1, cap1);
1397         }
1398         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
1399                 char tmp0[256] = { 0, };
1400                 char tmp1[256] = { 0, };
1401                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
1402                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
1403                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
1404                 res = AST_BRIDGE_FAILED_NOWARN;
1405                 goto done;
1406         }
1407
1408         instance0->glue = glue0;
1409         instance1->glue = glue1;
1410         instance0->chan = c0;
1411         instance1->chan = c1;
1412
1413         /* Depending on the end result for bridging either do a local bridge or remote bridge */
1414         if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1415                 ast_verbose(VERBOSE_PREFIX_3 "Locally bridging %s and %s\n", c0->name, c1->name);
1416                 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
1417         } else {
1418                 ast_verbose(VERBOSE_PREFIX_3 "Remotely bridging %s and %s\n", c0->name, c1->name);
1419                 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1420                                 tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
1421                                 fo, rc, c0->tech_pvt, c1->tech_pvt);
1422         }
1423
1424         instance0->glue = NULL;
1425         instance1->glue = NULL;
1426         instance0->chan = NULL;
1427         instance1->chan = NULL;
1428
1429         unlock_chans = 0;
1430
1431 done:
1432         if (unlock_chans) {
1433                 ast_channel_unlock(c0);
1434                 ast_channel_unlock(c1);
1435         }
1436         ast_format_cap_destroy(cap1);
1437         ast_format_cap_destroy(cap0);
1438
1439         unref_instance_cond(&instance0);
1440         unref_instance_cond(&instance1);
1441         unref_instance_cond(&vinstance0);
1442         unref_instance_cond(&vinstance1);
1443         unref_instance_cond(&tinstance0);
1444         unref_instance_cond(&tinstance1);
1445
1446         return res;
1447 }
1448
1449 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1450 {
1451         return instance->bridged;
1452 }
1453
1454 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1455 {
1456         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1457                 *vinstance0 = NULL, *vinstance1 = NULL,
1458                 *tinstance0 = NULL, *tinstance1 = NULL;
1459         struct ast_rtp_glue *glue0, *glue1;
1460         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;
1461         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;
1462         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1463         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1464         int res = 0;
1465
1466         /* Lock both channels so we can look for the glue that binds them together */
1467         ast_channel_lock(c0);
1468         while (ast_channel_trylock(c1)) {
1469                 ast_channel_unlock(c0);
1470                 usleep(1);
1471                 ast_channel_lock(c0);
1472         }
1473
1474         if (!cap1 || !cap0) {
1475                 goto done;
1476         }
1477
1478         /* Grab glue that binds each channel to something using the RTP engine */
1479         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1480                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1481                 goto done;
1482         }
1483
1484         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1485         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1486         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1487
1488         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1489         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1490         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1491
1492         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1493         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)) {
1494                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1495         }
1496         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)) {
1497                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1498         }
1499         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) {
1500                 glue0->get_codec(c0, cap0);
1501         }
1502         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) {
1503                 glue1->get_codec(c1, cap1);
1504         }
1505
1506         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1507         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1508                 goto done;
1509         }
1510
1511         /* Make sure we have matching codecs */
1512         if (!ast_format_cap_has_joint(cap0, cap1)) {
1513                 goto done;
1514         }
1515
1516         ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1517
1518         if (vinstance0 && vinstance1) {
1519                 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1520         }
1521         if (tinstance0 && tinstance1) {
1522                 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1523         }
1524
1525         res = 0;
1526
1527 done:
1528         ast_channel_unlock(c0);
1529         ast_channel_unlock(c1);
1530
1531         ast_format_cap_destroy(cap0);
1532         ast_format_cap_destroy(cap1);
1533
1534         unref_instance_cond(&instance0);
1535         unref_instance_cond(&instance1);
1536         unref_instance_cond(&vinstance0);
1537         unref_instance_cond(&vinstance1);
1538         unref_instance_cond(&tinstance0);
1539         unref_instance_cond(&tinstance1);
1540
1541         if (!res) {
1542                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1543         }
1544 }
1545
1546 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1547 {
1548         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1549                         *vinstance0 = NULL, *vinstance1 = NULL,
1550                         *tinstance0 = NULL, *tinstance1 = NULL;
1551         struct ast_rtp_glue *glue0, *glue1;
1552         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;
1553         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;
1554         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1555         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1556         int res = 0;
1557
1558         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1559         if (!c1) {
1560                 ast_format_cap_destroy(cap0);
1561                 ast_format_cap_destroy(cap1);
1562                 return -1;
1563         }
1564
1565         /* Lock both channels so we can look for the glue that binds them together */
1566         ast_channel_lock(c0);
1567         while (ast_channel_trylock(c1)) {
1568                 ast_channel_unlock(c0);
1569                 usleep(1);
1570                 ast_channel_lock(c0);
1571         }
1572
1573         if (!cap1 || !cap0) {
1574                 goto done;
1575         }
1576
1577         /* Grab glue that binds each channel to something using the RTP engine */
1578         if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1579                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1580                 goto done;
1581         }
1582
1583         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1584         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1585         text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1586
1587         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1588         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1589         text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1590
1591         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1592         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)) {
1593                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1594         }
1595         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)) {
1596                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1597         }
1598         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) {
1599                 glue0->get_codec(c0, cap0);
1600         }
1601         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) {
1602                 glue1->get_codec(c1, cap1);
1603         }
1604
1605         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1606         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1607                 goto done;
1608         }
1609
1610         /* Make sure we have matching codecs */
1611         if (!ast_format_cap_has_joint(cap0, cap1)) {
1612                 goto done;
1613         }
1614
1615         /* Bridge media early */
1616         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1617                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1618         }
1619
1620         res = 0;
1621
1622 done:
1623         ast_channel_unlock(c0);
1624         ast_channel_unlock(c1);
1625
1626         ast_format_cap_destroy(cap0);
1627         ast_format_cap_destroy(cap1);
1628
1629         unref_instance_cond(&instance0);
1630         unref_instance_cond(&instance1);
1631         unref_instance_cond(&vinstance0);
1632         unref_instance_cond(&vinstance1);
1633         unref_instance_cond(&tinstance0);
1634         unref_instance_cond(&tinstance1);
1635
1636         if (!res) {
1637                 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1638         }
1639
1640         return res;
1641 }
1642
1643 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1644 {
1645         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1646 }
1647
1648 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1649 {
1650         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1651 }
1652
1653 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1654 {
1655         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1656 }
1657
1658 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1659 {
1660         struct ast_rtp_instance_stats stats = { 0, };
1661         enum ast_rtp_instance_stat stat;
1662
1663         /* Determine what statistics we will need to retrieve based on field passed in */
1664         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1665                 stat = AST_RTP_INSTANCE_STAT_ALL;
1666         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1667                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1668         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1669                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1670         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1671                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1672         } else {
1673                 return NULL;
1674         }
1675
1676         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1677         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1678                 return NULL;
1679         }
1680
1681         /* Now actually fill the buffer with the good information */
1682         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1683                 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1684                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1685         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1686                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1687                          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));
1688         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1689                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1690                          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));
1691         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1692                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1693         }
1694
1695         return buf;
1696 }
1697
1698 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1699 {
1700         char quality_buf[AST_MAX_USER_FIELD], *quality;
1701         struct ast_channel *bridge = ast_bridged_channel(chan);
1702
1703         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1704                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1705                 if (bridge) {
1706                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1707                 }
1708         }
1709
1710         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1711                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1712                 if (bridge) {
1713                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1714                 }
1715         }
1716
1717         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1718                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1719                 if (bridge) {
1720                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1721                 }
1722         }
1723
1724         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1725                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1726                 if (bridge) {
1727                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1728                 }
1729         }
1730 }
1731
1732 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1733 {
1734         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1735 }
1736
1737 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1738 {
1739         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1740 }
1741
1742 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1743 {
1744         struct ast_rtp_glue *glue;
1745         struct ast_rtp_instance *peer_instance = NULL;
1746         int res = -1;
1747
1748         if (!instance->engine->make_compatible) {
1749                 return -1;
1750         }
1751
1752         ast_channel_lock(peer);
1753
1754         if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
1755                 ast_channel_unlock(peer);
1756                 return -1;
1757         }
1758
1759         glue->get_rtp_info(peer, &peer_instance);
1760
1761         if (!peer_instance || peer_instance->engine != instance->engine) {
1762                 ast_channel_unlock(peer);
1763                 ao2_ref(peer_instance, -1);
1764                 peer_instance = NULL;
1765                 return -1;
1766         }
1767
1768         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1769
1770         ast_channel_unlock(peer);
1771
1772         ao2_ref(peer_instance, -1);
1773         peer_instance = NULL;
1774
1775         return res;
1776 }
1777
1778 void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
1779 {
1780         if (instance->engine->available_formats) {
1781                 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1782                 if (!ast_format_cap_is_empty(result)) {
1783                         return;
1784                 }
1785         }
1786
1787         ast_translate_available_formats(to_endpoint, to_asterisk, result);
1788 }
1789
1790 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1791 {
1792         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1793 }
1794
1795 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1796                                    struct ast_sockaddr *suggestion,
1797                                    const char *username)
1798 {
1799         if (instance->engine->stun_request) {
1800                 instance->engine->stun_request(instance, suggestion, username);
1801         }
1802 }
1803
1804 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1805 {
1806         instance->timeout = timeout;
1807 }
1808
1809 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1810 {
1811         instance->holdtimeout = timeout;
1812 }
1813
1814 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1815 {
1816         return instance->timeout;
1817 }
1818
1819 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1820 {
1821         return instance->holdtimeout;
1822 }
1823
1824 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1825 {
1826         return instance->engine;
1827 }
1828
1829 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1830 {
1831         return instance->glue;
1832 }
1833
1834 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
1835 {
1836         return instance->chan;
1837 }
1838
1839 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1840 {
1841         if (res_srtp || res_srtp_policy) {
1842                 return -1;
1843         }
1844         if (!srtp_res || !policy_res) {
1845                 return -1;
1846         }
1847
1848         res_srtp = srtp_res;
1849         res_srtp_policy = policy_res;
1850
1851         return 0;
1852 }
1853
1854 void ast_rtp_engine_unregister_srtp(void)
1855 {
1856         res_srtp = NULL;
1857         res_srtp_policy = NULL;
1858 }
1859
1860 int ast_rtp_engine_srtp_is_registered(void)
1861 {
1862         return res_srtp && res_srtp_policy;
1863 }
1864
1865 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *policy)
1866 {
1867         if (!res_srtp) {
1868                 return -1;
1869         }
1870
1871         if (!instance->srtp) {
1872                 return res_srtp->create(&instance->srtp, instance, policy);
1873         } else {
1874                 return res_srtp->add_stream(instance->srtp, policy);
1875         }
1876 }
1877
1878 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
1879 {
1880         return instance->srtp;
1881 }