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