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