Create the payload type if it does not exist when setting information based on the...
[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 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <math.h>
35
36 #include "asterisk/channel.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/module.h"
39 #include "asterisk/rtp_engine.h"
40 #include "asterisk/manager.h"
41 #include "asterisk/options.h"
42 #include "asterisk/astobj2.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/netsock2.h"
46 #include "asterisk/_private.h"
47 #include "asterisk/framehook.h"
48
49 struct ast_srtp_res *res_srtp = NULL;
50 struct ast_srtp_policy_res *res_srtp_policy = NULL;
51
52 /*! Structure that represents an RTP session (instance) */
53 struct ast_rtp_instance {
54         /*! Engine that is handling this RTP instance */
55         struct ast_rtp_engine *engine;
56         /*! Data unique to the RTP engine */
57         void *data;
58         /*! RTP properties that have been set and their value */
59         int properties[AST_RTP_PROPERTY_MAX];
60         /*! Address that we are expecting RTP to come in to */
61         struct ast_sockaddr local_address;
62         /*! Address that we are sending RTP to */
63         struct ast_sockaddr remote_address;
64         /*! Alternate address that we are receiving RTP from */
65         struct ast_sockaddr alt_remote_address;
66         /*! Instance that we are bridged to if doing remote or local bridging */
67         struct ast_rtp_instance *bridged;
68         /*! Payload and packetization information */
69         struct ast_rtp_codecs codecs;
70         /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
71         int timeout;
72         /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
73         int holdtimeout;
74         /*! RTP keepalive interval */
75         int keepalive;
76         /*! Glue currently in use */
77         struct ast_rtp_glue *glue;
78         /*! Channel associated with the instance */
79         struct ast_channel *chan;
80         /*! SRTP info associated with the instance */
81         struct ast_srtp *srtp;
82 };
83
84 /*! List of RTP engines that are currently registered */
85 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
86
87 /*! List of RTP glues */
88 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
89
90 /*! The following array defines the MIME Media type (and subtype) for each
91    of our codecs, or RTP-specific data type. */
92 static struct ast_rtp_mime_type {
93         struct ast_rtp_payload_type payload_type;
94         char *type;
95         char *subtype;
96         unsigned int sample_rate;
97 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
98 static ast_rwlock_t mime_types_lock;
99 static int mime_types_len = 0;
100
101 /*!
102  * \brief Mapping between Asterisk codecs and rtp payload types
103  *
104  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
105  * also, our own choices for dynamic payload types.  This is our master
106  * table for transmission
107  *
108  * See http://www.iana.org/assignments/rtp-parameters for a list of
109  * assigned values
110  */
111 static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
112 static ast_rwlock_t static_RTP_PT_lock;
113
114 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
115 {
116         struct ast_rtp_engine *current_engine;
117
118         /* Perform a sanity check on the engine structure to make sure it has the basics */
119         if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
120                 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
121                 return -1;
122         }
123
124         /* Link owner module to the RTP engine for reference counting purposes */
125         engine->mod = module;
126
127         AST_RWLIST_WRLOCK(&engines);
128
129         /* Ensure that no two modules with the same name are registered at the same time */
130         AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
131                 if (!strcmp(current_engine->name, engine->name)) {
132                         ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
133                         AST_RWLIST_UNLOCK(&engines);
134                         return -1;
135                 }
136         }
137
138         /* The engine survived our critique. Off to the list it goes to be used */
139         AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
140
141         AST_RWLIST_UNLOCK(&engines);
142
143         ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
144
145         return 0;
146 }
147
148 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
149 {
150         struct ast_rtp_engine *current_engine = NULL;
151
152         AST_RWLIST_WRLOCK(&engines);
153
154         if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
155                 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
156         }
157
158         AST_RWLIST_UNLOCK(&engines);
159
160         return current_engine ? 0 : -1;
161 }
162
163 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
164 {
165         struct ast_rtp_glue *current_glue = NULL;
166
167         if (ast_strlen_zero(glue->type)) {
168                 return -1;
169         }
170
171         glue->mod = module;
172
173         AST_RWLIST_WRLOCK(&glues);
174
175         AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
176                 if (!strcasecmp(current_glue->type, glue->type)) {
177                         ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
178                         AST_RWLIST_UNLOCK(&glues);
179                         return -1;
180                 }
181         }
182
183         AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
184
185         AST_RWLIST_UNLOCK(&glues);
186
187         ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
188
189         return 0;
190 }
191
192 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
193 {
194         struct ast_rtp_glue *current_glue = NULL;
195
196         AST_RWLIST_WRLOCK(&glues);
197
198         if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
199                 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
200         }
201
202         AST_RWLIST_UNLOCK(&glues);
203
204         return current_glue ? 0 : -1;
205 }
206
207 static void instance_destructor(void *obj)
208 {
209         struct ast_rtp_instance *instance = obj;
210
211         /* Pass us off to the engine to destroy */
212         if (instance->data && instance->engine->destroy(instance)) {
213                 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
214                 return;
215         }
216
217         if (instance->srtp) {
218                 res_srtp->destroy(instance->srtp);
219         }
220
221         ast_rtp_codecs_payloads_destroy(&instance->codecs);
222
223         /* Drop our engine reference */
224         ast_module_unref(instance->engine->mod);
225
226         ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
227 }
228
229 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
230 {
231         ao2_ref(instance, -1);
232
233         return 0;
234 }
235
236 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
237                 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
238                 void *data)
239 {
240         struct ast_sockaddr address = {{0,}};
241         struct ast_rtp_instance *instance = NULL;
242         struct ast_rtp_engine *engine = NULL;
243
244         AST_RWLIST_RDLOCK(&engines);
245
246         /* If an engine name was specified try to use it or otherwise use the first one registered */
247         if (!ast_strlen_zero(engine_name)) {
248                 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
249                         if (!strcmp(engine->name, engine_name)) {
250                                 break;
251                         }
252                 }
253         } else {
254                 engine = AST_RWLIST_FIRST(&engines);
255         }
256
257         /* If no engine was actually found bail out now */
258         if (!engine) {
259                 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
260                 AST_RWLIST_UNLOCK(&engines);
261                 return NULL;
262         }
263
264         /* Bump up the reference count before we return so the module can not be unloaded */
265         ast_module_ref(engine->mod);
266
267         AST_RWLIST_UNLOCK(&engines);
268
269         /* Allocate a new RTP instance */
270         if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
271                 ast_module_unref(engine->mod);
272                 return NULL;
273         }
274         instance->engine = engine;
275         ast_sockaddr_copy(&instance->local_address, sa);
276         ast_sockaddr_copy(&address, sa);
277
278         if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
279                 ao2_ref(instance, -1);
280                 return NULL;
281         }
282
283         ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
284
285         /* And pass it off to the engine to setup */
286         if (instance->engine->new(instance, sched, &address, data)) {
287                 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
288                 ao2_ref(instance, -1);
289                 return NULL;
290         }
291
292         ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
293
294         return instance;
295 }
296
297 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
298 {
299         instance->data = data;
300 }
301
302 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
303 {
304         return instance->data;
305 }
306
307 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
308 {
309         return instance->engine->write(instance, frame);
310 }
311
312 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
313 {
314         return instance->engine->read(instance, rtcp);
315 }
316
317 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
318                 const struct ast_sockaddr *address)
319 {
320         ast_sockaddr_copy(&instance->local_address, address);
321         return 0;
322 }
323
324 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
325                 const struct ast_sockaddr *address)
326 {
327         ast_sockaddr_copy(&instance->remote_address, address);
328
329         /* moo */
330
331         if (instance->engine->remote_address_set) {
332                 instance->engine->remote_address_set(instance, &instance->remote_address);
333         }
334
335         return 0;
336 }
337
338 int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
339                 const struct ast_sockaddr *address)
340 {
341         ast_sockaddr_copy(&instance->alt_remote_address, address);
342
343         /* oink */
344
345         if (instance->engine->alt_remote_address_set) {
346                 instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
347         }
348
349         return 0;
350 }
351
352 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
353                 struct ast_sockaddr *address)
354 {
355         if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
356                 ast_sockaddr_copy(address, &instance->local_address);
357                 return 1;
358         }
359
360         return 0;
361 }
362
363 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
364                 struct ast_sockaddr *address)
365 {
366         ast_sockaddr_copy(address, &instance->local_address);
367 }
368
369 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
370                 struct ast_sockaddr *address)
371 {
372         if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
373                 ast_sockaddr_copy(address, &instance->remote_address);
374                 return 1;
375         }
376
377         return 0;
378 }
379
380 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
381                 struct ast_sockaddr *address)
382 {
383         ast_sockaddr_copy(address, &instance->remote_address);
384 }
385
386 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
387 {
388         if (instance->engine->extended_prop_set) {
389                 instance->engine->extended_prop_set(instance, property, value);
390         }
391 }
392
393 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
394 {
395         if (instance->engine->extended_prop_get) {
396                 return instance->engine->extended_prop_get(instance, property);
397         }
398
399         return NULL;
400 }
401
402 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
403 {
404         instance->properties[property] = value;
405
406         if (instance->engine->prop_set) {
407                 instance->engine->prop_set(instance, property, value);
408         }
409 }
410
411 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
412 {
413         return instance->properties[property];
414 }
415
416 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
417 {
418         return &instance->codecs;
419 }
420
421 static int rtp_payload_type_hash(const void *obj, const int flags)
422 {
423         const struct ast_rtp_payload_type *type = obj;
424         const int *payload = obj;
425
426         return (flags & OBJ_KEY) ? *payload : type->payload;
427 }
428
429 static int rtp_payload_type_cmp(void *obj, void *arg, int flags)
430 {
431         struct ast_rtp_payload_type *type1 = obj, *type2 = arg;
432         const int *payload = arg;
433
434         return (type1->payload == (OBJ_KEY ? *payload : type2->payload)) ? CMP_MATCH | CMP_STOP : 0;
435 }
436
437 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
438 {
439         if (!(codecs->payloads = ao2_container_alloc(AST_RTP_MAX_PT, rtp_payload_type_hash, rtp_payload_type_cmp))) {
440                 return -1;
441         }
442
443         return 0;
444 }
445
446 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
447 {
448         ao2_cleanup(codecs->payloads);
449 }
450
451 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
452 {
453         ast_rtp_codecs_payloads_destroy(codecs);
454
455         if (instance && instance->engine && instance->engine->payload_set) {
456                 int i;
457                 for (i = 0; i < AST_RTP_MAX_PT; i++) {
458                         instance->engine->payload_set(instance, i, 0, NULL, 0);
459                 }
460         }
461
462         ast_rtp_codecs_payloads_initialize(codecs);
463 }
464
465 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
466 {
467         int i;
468
469         ast_rwlock_rdlock(&static_RTP_PT_lock);
470         for (i = 0; i < AST_RTP_MAX_PT; i++) {
471                 if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
472                         struct ast_rtp_payload_type *type;
473
474                         if (!(type = ao2_alloc(sizeof(*type), NULL))) {
475                                 /* Unfortunately if this occurs the payloads container will not contain all possible default payloads
476                                  * but we err on the side of doing what we can in the hopes that the extreme memory conditions which
477                                  * caused this to occur will go away.
478                                  */
479                                 continue;
480                         }
481
482                         type->payload = i;
483                         type->asterisk_format = static_RTP_PT[i].asterisk_format;
484                         type->rtp_code = static_RTP_PT[i].rtp_code;
485                         ast_format_copy(&type->format, &static_RTP_PT[i].format);
486
487                         ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
488
489                         if (instance && instance->engine && instance->engine->payload_set) {
490                                 instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
491                         }
492
493                         ao2_ref(type, -1);
494                 }
495         }
496         ast_rwlock_unlock(&static_RTP_PT_lock);
497 }
498
499 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
500 {
501         int i;
502         struct ast_rtp_payload_type *type;
503
504         for (i = 0; i < AST_RTP_MAX_PT; i++) {
505                 struct ast_rtp_payload_type *new_type;
506
507                 if (!(type = ao2_find(src->payloads, &i, OBJ_KEY | OBJ_NOLOCK))) {
508                         continue;
509                 }
510
511                 if (!(new_type = ao2_alloc(sizeof(*new_type), NULL))) {
512                         continue;
513                 }
514
515                 ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
516
517                 new_type->payload = i;
518                 *new_type = *type;
519
520                 ao2_link_flags(dest->payloads, new_type, OBJ_NOLOCK);
521
522                 ao2_ref(new_type, -1);
523
524                 if (instance && instance->engine && instance->engine->payload_set) {
525                         instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
526                 }
527
528                 ao2_ref(type, -1);
529         }
530 }
531
532 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
533 {
534         struct ast_rtp_payload_type *type;
535
536         ast_rwlock_rdlock(&static_RTP_PT_lock);
537
538         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
539                 ast_rwlock_unlock(&static_RTP_PT_lock);
540                 return;
541         }
542
543         if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
544                 if (!(type = ao2_alloc(sizeof(*type), NULL))) {
545                         ast_rwlock_unlock(&static_RTP_PT_lock);
546                         return;
547                 }
548                 type->payload = payload;
549                 ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
550         }
551
552         type->asterisk_format = static_RTP_PT[payload].asterisk_format;
553         type->rtp_code = static_RTP_PT[payload].rtp_code;
554         type->payload = payload;
555         ast_format_copy(&type->format, &static_RTP_PT[payload].format);
556
557         ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
558
559         if (instance && instance->engine && instance->engine->payload_set) {
560                 instance->engine->payload_set(instance, payload, type->asterisk_format, &type->format, type->rtp_code);
561         }
562
563         ao2_ref(type, -1);
564
565         ast_rwlock_unlock(&static_RTP_PT_lock);
566 }
567
568 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
569                                  char *mimetype, char *mimesubtype,
570                                  enum ast_rtp_options options,
571                                  unsigned int sample_rate)
572 {
573         unsigned int i;
574         int found = 0;
575
576         if (pt < 0 || pt >= AST_RTP_MAX_PT)
577                 return -1; /* bogus payload type */
578
579         ast_rwlock_rdlock(&mime_types_lock);
580         for (i = 0; i < mime_types_len; ++i) {
581                 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
582                 struct ast_rtp_payload_type *type;
583
584                 if (strcasecmp(mimesubtype, t->subtype)) {
585                         continue;
586                 }
587
588                 if (strcasecmp(mimetype, t->type)) {
589                         continue;
590                 }
591
592                 /* if both sample rates have been supplied, and they don't match,
593                  * then this not a match; if one has not been supplied, then the
594                  * rates are not compared */
595                 if (sample_rate && t->sample_rate &&
596                     (sample_rate != t->sample_rate)) {
597                         continue;
598                 }
599
600                 found = 1;
601
602                 if (!(type = ao2_find(codecs->payloads, &pt, OBJ_KEY | OBJ_NOLOCK))) {
603                         if (!(type = ao2_alloc(sizeof(*type), NULL))) {
604                                 continue;
605                         }
606                         type->payload = pt;
607                         ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
608                 }
609
610                 *type = t->payload_type;
611                 type->payload = pt;
612
613                 if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
614                         ast_format_set(&type->format, AST_FORMAT_G726_AAL2, 0);
615                 }
616
617                 if (instance && instance->engine && instance->engine->payload_set) {
618                         instance->engine->payload_set(instance, pt, type->asterisk_format, &type->format, type->rtp_code);
619                 }
620
621                 ao2_ref(type, -1);
622
623                 break;
624         }
625         ast_rwlock_unlock(&mime_types_lock);
626
627         return (found ? 0 : -2);
628 }
629
630 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)
631 {
632         return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
633 }
634
635 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
636 {
637         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
638                 return;
639         }
640
641         ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
642
643         ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK | OBJ_NODATA | OBJ_UNLINK);
644
645         if (instance && instance->engine && instance->engine->payload_set) {
646                 instance->engine->payload_set(instance, payload, 0, NULL, 0);
647         }
648 }
649
650 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
651 {
652         struct ast_rtp_payload_type result = { .asterisk_format = 0, }, *type;
653
654         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
655                 return result;
656         }
657
658         if ((type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
659                 result = *type;
660                 ao2_ref(type, -1);
661         }
662
663         if (!result.rtp_code && !result.asterisk_format) {
664                 ast_rwlock_rdlock(&static_RTP_PT_lock);
665                 result = static_RTP_PT[payload];
666                 ast_rwlock_unlock(&static_RTP_PT_lock);
667         }
668
669         return result;
670 }
671
672
673 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
674 {
675         struct ast_rtp_payload_type *type;
676         struct ast_format *format;
677
678         if (payload < 0 || payload >= AST_RTP_MAX_PT) {
679                 return NULL;
680         }
681
682         if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
683                 return NULL;
684         }
685
686         format = type->asterisk_format ? &type->format : NULL;
687
688         ao2_ref(type, -1);
689
690         return format;
691 }
692
693 static int rtp_payload_type_add_ast(void *obj, void *arg, int flags)
694 {
695         struct ast_rtp_payload_type *type = obj;
696         struct ast_format_cap *astformats = arg;
697
698         if (type->asterisk_format) {
699                 ast_format_cap_add(astformats, &type->format);
700         }
701
702         return 0;
703 }
704
705 static int rtp_payload_type_add_nonast(void *obj, void *arg, int flags)
706 {
707         struct ast_rtp_payload_type *type = obj;
708         int *nonastformats = arg;
709
710         if (!type->asterisk_format) {
711                 *nonastformats |= type->rtp_code;
712         }
713
714         return 0;
715 }
716
717 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
718 {
719         ast_format_cap_remove_all(astformats);
720         *nonastformats = 0;
721
722         ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_ast, astformats);
723         ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_nonast, nonastformats);
724 }
725
726 static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
727 {
728         struct ast_rtp_payload_type *type = obj;
729         struct ast_format *format = arg;
730
731         return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
732 }
733
734 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
735 {
736         struct ast_rtp_payload_type *type;
737         int i, res = -1;
738
739         if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
740                 res = type->payload;
741                 ao2_ref(type, -1);
742                 return res;
743         } else if (!asterisk_format && (type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY))) {
744                 res = type->payload;
745                 ao2_ref(type, -1);
746                 return res;
747         }
748
749         ast_rwlock_rdlock(&static_RTP_PT_lock);
750         for (i = 0; i < AST_RTP_MAX_PT; i++) {
751                 if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
752                         (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
753                         res = i;
754                         break;
755                 } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
756                         (static_RTP_PT[i].rtp_code == code)) {
757                         res = i;
758                         break;
759                 }
760         }
761         ast_rwlock_unlock(&static_RTP_PT_lock);
762
763         return res;
764 }
765
766 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
767 {
768         int i;
769         const char *res = "";
770
771         ast_rwlock_rdlock(&mime_types_lock);
772         for (i = 0; i < mime_types_len; i++) {
773                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
774                         (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
775                         if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
776                                 res = "G726-32";
777                                 break;
778                         } else {
779                                 res = ast_rtp_mime_types[i].subtype;
780                                 break;
781                         }
782                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
783                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
784
785                         res = ast_rtp_mime_types[i].subtype;
786                         break;
787                 }
788         }
789         ast_rwlock_unlock(&mime_types_lock);
790
791         return res;
792 }
793
794 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
795 {
796         unsigned int i;
797         unsigned int res = 0;
798
799         ast_rwlock_rdlock(&mime_types_lock);
800         for (i = 0; i < mime_types_len; ++i) {
801                 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
802                         (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
803                         res = ast_rtp_mime_types[i].sample_rate;
804                         break;
805                 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
806                         ast_rtp_mime_types[i].payload_type.rtp_code == code) {
807                         res = ast_rtp_mime_types[i].sample_rate;
808                         break;
809                 }
810         }
811         ast_rwlock_unlock(&mime_types_lock);
812
813         return res;
814 }
815
816 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *ast_format_capability, int rtp_capability, const int asterisk_format, enum ast_rtp_options options)
817 {
818         int found = 0;
819         const char *name;
820         if (!buf) {
821                 return NULL;
822         }
823
824
825         if (asterisk_format) {
826                 struct ast_format tmp_fmt;
827                 ast_format_cap_iter_start(ast_format_capability);
828                 while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
829                         name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
830                         ast_str_append(&buf, 0, "%s|", name);
831                         found = 1;
832                 }
833                 ast_format_cap_iter_end(ast_format_capability);
834
835         } else {
836                 int x;
837                 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
838                 for (x = 1; x < AST_RTP_MAX; x <<= 1) {
839                         if (rtp_capability & x) {
840                                 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
841                                 ast_str_append(&buf, 0, "%s|", name);
842                                 found = 1;
843                         }
844                 }
845         }
846
847         ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
848
849         return ast_str_buffer(buf);
850 }
851
852 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
853 {
854         codecs->pref = *prefs;
855
856         if (instance && instance->engine->packetization_set) {
857                 instance->engine->packetization_set(instance, &instance->codecs.pref);
858         }
859 }
860
861 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
862 {
863         return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
864 }
865
866 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
867 {
868         return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
869 }
870 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
871 {
872         return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
873 }
874
875 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
876 {
877         return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
878 }
879
880 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
881 {
882         return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
883 }
884
885 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
886 {
887         if (instance->engine->update_source) {
888                 instance->engine->update_source(instance);
889         }
890 }
891
892 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
893 {
894         if (instance->engine->change_source) {
895                 instance->engine->change_source(instance);
896         }
897 }
898
899 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
900 {
901         return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
902 }
903
904 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
905 {
906         if (instance->engine->stop) {
907                 instance->engine->stop(instance);
908         }
909 }
910
911 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
912 {
913         return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
914 }
915
916 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
917 {
918         struct ast_rtp_glue *glue = NULL;
919
920         AST_RWLIST_RDLOCK(&glues);
921
922         AST_RWLIST_TRAVERSE(&glues, glue, entry) {
923                 if (!strcasecmp(glue->type, type)) {
924                         break;
925                 }
926         }
927
928         AST_RWLIST_UNLOCK(&glues);
929
930         return glue;
931 }
932
933 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)
934 {
935         enum ast_bridge_result res = AST_BRIDGE_FAILED;
936         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
937         struct ast_frame *fr = NULL;
938
939         /* Start locally bridging both instances */
940         if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
941                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
942                 ast_channel_unlock(c0);
943                 ast_channel_unlock(c1);
944                 return AST_BRIDGE_FAILED_NOWARN;
945         }
946         if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
947                 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
948                 if (instance0->engine->local_bridge) {
949                         instance0->engine->local_bridge(instance0, NULL);
950                 }
951                 ast_channel_unlock(c0);
952                 ast_channel_unlock(c1);
953                 return AST_BRIDGE_FAILED_NOWARN;
954         }
955
956         ast_channel_unlock(c0);
957         ast_channel_unlock(c1);
958
959         instance0->bridged = instance1;
960         instance1->bridged = instance0;
961
962         ast_poll_channel_add(c0, c1);
963
964         /* Hop into a loop waiting for a frame from either channel */
965         cs[0] = c0;
966         cs[1] = c1;
967         cs[2] = NULL;
968         for (;;) {
969                 /* If the underlying formats have changed force this bridge to break */
970                 if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
971                         (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
972                         ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
973                         res = AST_BRIDGE_FAILED_NOWARN;
974                         break;
975                 }
976                 /* Check if anything changed */
977                 if ((ast_channel_tech_pvt(c0) != pvt0) ||
978                     (ast_channel_tech_pvt(c1) != pvt1) ||
979                     (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
980                     (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
981                     (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
982                         ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
983                         /* 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 */
984                         if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
985                                 ast_frfree(fr);
986                         }
987                         if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
988                                 ast_frfree(fr);
989                         }
990                         res = AST_BRIDGE_RETRY;
991                         break;
992                 }
993                 /* Wait on a channel to feed us a frame */
994                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
995                         if (!timeoutms) {
996                                 res = AST_BRIDGE_RETRY;
997                                 break;
998                         }
999                         ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
1000                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1001                                 break;
1002                         }
1003                         continue;
1004                 }
1005                 /* Read in frame from channel */
1006                 fr = ast_read(who);
1007                 other = (who == c0) ? c1 : c0;
1008                 /* Depending on the frame we may need to break out of our bridge */
1009                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1010                             ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
1011                             ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
1012                         /* Record received frame and who */
1013                         *fo = fr;
1014                         *rc = who;
1015                         ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
1016                         res = AST_BRIDGE_COMPLETE;
1017                         break;
1018                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1019                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1020                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1021                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1022                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1023                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
1024                             (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
1025                                 /* If we are going on hold, then break callback mode and P2P bridging */
1026                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1027                                         if (instance0->engine->local_bridge) {
1028                                                 instance0->engine->local_bridge(instance0, NULL);
1029                                         }
1030                                         if (instance1->engine->local_bridge) {
1031                                                 instance1->engine->local_bridge(instance1, NULL);
1032                                         }
1033                                         instance0->bridged = NULL;
1034                                         instance1->bridged = NULL;
1035                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
1036                                         if (instance0->engine->local_bridge) {
1037                                                 instance0->engine->local_bridge(instance0, instance1);
1038                                         }
1039                                         if (instance1->engine->local_bridge) {
1040                                                 instance1->engine->local_bridge(instance1, instance0);
1041                                         }
1042                                         instance0->bridged = instance1;
1043                                         instance1->bridged = instance0;
1044                                 }
1045                                 /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
1046                                 if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
1047                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1048                                 }
1049                                 ast_frfree(fr);
1050                         } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
1051                                 if (ast_channel_connected_line_sub(who, other, fr, 1) &&
1052                                         ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
1053                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1054                                 }
1055                                 ast_frfree(fr);
1056                         } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
1057                                 if (ast_channel_redirecting_sub(who, other, fr, 1) &&
1058                                         ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
1059                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1060                                 }
1061                                 ast_frfree(fr);
1062                         } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
1063                                 ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
1064                                 ast_frfree(fr);
1065                         } else {
1066                                 *fo = fr;
1067                                 *rc = who;
1068                                 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
1069                                 res = AST_BRIDGE_COMPLETE;
1070                                 break;
1071                         }
1072                 } else {
1073                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1074                             (fr->frametype == AST_FRAME_DTMF_END) ||
1075                             (fr->frametype == AST_FRAME_VOICE) ||
1076                             (fr->frametype == AST_FRAME_VIDEO) ||
1077                             (fr->frametype == AST_FRAME_IMAGE) ||
1078                             (fr->frametype == AST_FRAME_HTML) ||
1079                             (fr->frametype == AST_FRAME_MODEM) ||
1080                             (fr->frametype == AST_FRAME_TEXT)) {
1081                                 ast_write(other, fr);
1082                         }
1083
1084                         ast_frfree(fr);
1085                 }
1086                 /* Swap priority */
1087                 cs[2] = cs[0];
1088                 cs[0] = cs[1];
1089                 cs[1] = cs[2];
1090         }
1091
1092         /* Stop locally bridging both instances */
1093         if (instance0->engine->local_bridge) {
1094                 instance0->engine->local_bridge(instance0, NULL);
1095         }
1096         if (instance1->engine->local_bridge) {
1097                 instance1->engine->local_bridge(instance1, NULL);
1098         }
1099
1100         instance0->bridged = NULL;
1101         instance1->bridged = NULL;
1102
1103         ast_poll_channel_del(c0, c1);
1104
1105         return res;
1106 }
1107
1108 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
1109         struct ast_channel *c1,
1110         struct ast_rtp_instance *instance0,
1111         struct ast_rtp_instance *instance1,
1112         struct ast_rtp_instance *vinstance0,
1113         struct ast_rtp_instance *vinstance1,
1114         struct ast_rtp_instance *tinstance0,
1115         struct ast_rtp_instance *tinstance1,
1116         struct ast_rtp_glue *glue0,
1117         struct ast_rtp_glue *glue1,
1118         struct ast_format_cap *cap0,
1119         struct ast_format_cap *cap1,
1120         int timeoutms,
1121         int flags,
1122         struct ast_frame **fo,
1123         struct ast_channel **rc,
1124         void *pvt0,
1125         void *pvt1)
1126 {
1127         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1128         struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
1129         struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
1130         struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
1131         struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
1132         struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
1133         struct ast_frame *fr = NULL;
1134
1135         if (!oldcap0 || !oldcap1) {
1136                 ast_channel_unlock(c0);
1137                 ast_channel_unlock(c1);
1138                 goto remote_bridge_cleanup;
1139         }
1140         /* Test the first channel */
1141         if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
1142                 ast_rtp_instance_get_remote_address(instance1, &ac1);
1143                 if (vinstance1) {
1144                         ast_rtp_instance_get_remote_address(vinstance1, &vac1);
1145                 }
1146                 if (tinstance1) {
1147                         ast_rtp_instance_get_remote_address(tinstance1, &tac1);
1148                 }
1149         } else {
1150                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1151         }
1152
1153         /* Test the second channel */
1154         if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
1155                 ast_rtp_instance_get_remote_address(instance0, &ac0);
1156                 if (vinstance0) {
1157                         ast_rtp_instance_get_remote_address(instance0, &vac0);
1158                 }
1159                 if (tinstance0) {
1160                         ast_rtp_instance_get_remote_address(instance0, &tac0);
1161                 }
1162         } else {
1163                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1164         }
1165
1166         ast_channel_unlock(c0);
1167         ast_channel_unlock(c1);
1168
1169         instance0->bridged = instance1;
1170         instance1->bridged = instance0;
1171
1172         ast_poll_channel_add(c0, c1);
1173
1174         /* Go into a loop handling any stray frames that may come in */
1175         cs[0] = c0;
1176         cs[1] = c1;
1177         cs[2] = NULL;
1178         for (;;) {
1179                 /* Check if anything changed */
1180                 if ((ast_channel_tech_pvt(c0) != pvt0) ||
1181                     (ast_channel_tech_pvt(c1) != pvt1) ||
1182                     (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
1183                     (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
1184                     (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
1185                         ast_debug(1, "Oooh, something is weird, backing out\n");
1186                         res = AST_BRIDGE_RETRY;
1187                         break;
1188                 }
1189
1190                 /* Check if they have changed their address */
1191                 ast_rtp_instance_get_remote_address(instance1, &t1);
1192                 if (vinstance1) {
1193                         ast_rtp_instance_get_remote_address(vinstance1, &vt1);
1194                 }
1195                 if (tinstance1) {
1196                         ast_rtp_instance_get_remote_address(tinstance1, &tt1);
1197                 }
1198                 if (glue1->get_codec) {
1199                         ast_format_cap_remove_all(cap1);
1200                         glue1->get_codec(c1, cap1);
1201                 }
1202
1203                 ast_rtp_instance_get_remote_address(instance0, &t0);
1204                 if (vinstance0) {
1205                         ast_rtp_instance_get_remote_address(vinstance0, &vt0);
1206                 }
1207                 if (tinstance0) {
1208                         ast_rtp_instance_get_remote_address(tinstance0, &tt0);
1209                 }
1210                 if (glue0->get_codec) {
1211                         ast_format_cap_remove_all(cap0);
1212                         glue0->get_codec(c0, cap0);
1213                 }
1214
1215                 if ((ast_sockaddr_cmp(&t1, &ac1)) ||
1216                     (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
1217                     (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
1218                     (!ast_format_cap_identical(cap1, oldcap1))) {
1219                         char tmp_buf[512] = { 0, };
1220                         ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1221                                   ast_channel_name(c1), ast_sockaddr_stringify(&t1),
1222                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1223                         ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
1224                                   ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
1225                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1226                         ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
1227                                   ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
1228                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1229                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1230                                   ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
1231                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1232                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1233                                   ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
1234                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1235                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1236                                   ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
1237                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1238                         if (glue0->update_peer(c0,
1239                                                ast_sockaddr_isnull(&t1)  ? NULL : instance1,
1240                                                ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
1241                                                ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
1242                                                cap1, 0)) {
1243                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1244                         }
1245                         ast_sockaddr_copy(&ac1, &t1);
1246                         ast_sockaddr_copy(&vac1, &vt1);
1247                         ast_sockaddr_copy(&tac1, &tt1);
1248                         ast_format_cap_copy(oldcap1, cap1);
1249                 }
1250                 if ((ast_sockaddr_cmp(&t0, &ac0)) ||
1251                     (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
1252                     (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
1253                     (!ast_format_cap_identical(cap0, oldcap0))) {
1254                         char tmp_buf[512] = { 0, };
1255                         ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1256                                   ast_channel_name(c0), ast_sockaddr_stringify(&t0),
1257                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
1258                         ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1259                                   ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
1260                                   ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
1261                         if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
1262                                                 vt0.len ? vinstance0 : NULL,
1263                                                 tt0.len ? tinstance0 : NULL,
1264                                                 cap0, 0)) {
1265                                 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1266                         }
1267                         ast_sockaddr_copy(&ac0, &t0);
1268                         ast_sockaddr_copy(&vac0, &vt0);
1269                         ast_sockaddr_copy(&tac0, &tt0);
1270                         ast_format_cap_copy(oldcap0, cap0);
1271                 }
1272
1273                 /* Wait for frame to come in on the channels */
1274                 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1275                         if (!timeoutms) {
1276                                 res = AST_BRIDGE_RETRY;
1277                                 break;
1278                         }
1279                         ast_debug(1, "Ooh, empty read...\n");
1280                         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1281                                 break;
1282                         }
1283                         continue;
1284                 }
1285                 fr = ast_read(who);
1286                 other = (who == c0) ? c1 : c0;
1287                 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1288                             (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1289                              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1290                         /* Break out of bridge */
1291                         *fo = fr;
1292                         *rc = who;
1293                         ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1294                         res = AST_BRIDGE_COMPLETE;
1295                         break;
1296                 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1297                         if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1298                             (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1299                             (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1300                             (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1301                             (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
1302                                 (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
1303                                 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1304                                         /* If we someone went on hold we want the other side to reinvite back to us */
1305                                         if (who == c0) {
1306                                                 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1307                                         } else {
1308                                                 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1309                                         }
1310                                 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
1311                                         fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
1312                                         /* If they went off hold they should go back to being direct, or if we have
1313                                          * been told to force a peer update, go ahead and do it. */
1314                                         if (who == c0) {
1315                                                 glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
1316                                         } else {
1317                                                 glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
1318                                         }
1319                                 }
1320                                 /* Update local address information */
1321                                 ast_rtp_instance_get_remote_address(instance0, &t0);
1322                                 ast_sockaddr_copy(&ac0, &t0);
1323                                 ast_rtp_instance_get_remote_address(instance1, &t1);
1324                                 ast_sockaddr_copy(&ac1, &t1);
1325                                 /* Update codec information */
1326                                 if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
1327                                         ast_format_cap_remove_all(cap0);
1328                                         ast_format_cap_remove_all(oldcap0);
1329                                         glue0->get_codec(c0, cap0);
1330                                         ast_format_cap_append(oldcap0, cap0);
1331
1332                                 }
1333                                 if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
1334                                         ast_format_cap_remove_all(cap1);
1335                                         ast_format_cap_remove_all(oldcap1);
1336                                         glue1->get_codec(c1, cap1);
1337                                         ast_format_cap_append(oldcap1, cap1);
1338                                 }
1339                                 /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
1340                                 if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
1341                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1342                                 }
1343                                 ast_frfree(fr);
1344                         } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
1345                                 if (ast_channel_connected_line_sub(who, other, fr, 1) &&
1346                                         ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
1347                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1348                                 }
1349                                 ast_frfree(fr);
1350                         } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
1351                                 if (ast_channel_redirecting_sub(who, other, fr, 1) &&
1352                                         ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
1353                                         ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1354                                 }
1355                                 ast_frfree(fr);
1356                         } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
1357                                 ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
1358                                 ast_frfree(fr);
1359                         } else {
1360                                 *fo = fr;
1361                                 *rc = who;
1362                                 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
1363                                 res = AST_BRIDGE_COMPLETE;
1364                                 goto remote_bridge_cleanup;
1365                         }
1366                 } else {
1367                         if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1368                             (fr->frametype == AST_FRAME_DTMF_END) ||
1369                             (fr->frametype == AST_FRAME_VOICE) ||
1370                             (fr->frametype == AST_FRAME_VIDEO) ||
1371                             (fr->frametype == AST_FRAME_IMAGE) ||
1372                             (fr->frametype == AST_FRAME_HTML) ||
1373                             (fr->frametype == AST_FRAME_MODEM) ||
1374                             (fr->frametype == AST_FRAME_TEXT)) {
1375                                 ast_write(other, fr);
1376                         }
1377                         ast_frfree(fr);
1378                 }
1379                 /* Swap priority */
1380                 cs[2] = cs[0];
1381                 cs[0] = cs[1];
1382                 cs[1] = cs[2];
1383         }
1384
1385         if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
1386                 ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
1387         } else if (ast_channel_tech_pvt(c0) != pvt0) {
1388                 ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1389         } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
1390                 ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1391         } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1392                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
1393         }
1394         if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
1395                 ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
1396         } else if (ast_channel_tech_pvt(c1) != pvt1) {
1397                 ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1398         } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
1399                 ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1400         } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1401                 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
1402         }
1403
1404         instance0->bridged = NULL;
1405         instance1->bridged = NULL;
1406
1407         ast_poll_channel_del(c0, c1);
1408
1409 remote_bridge_cleanup:
1410         ast_format_cap_destroy(oldcap0);
1411         ast_format_cap_destroy(oldcap1);
1412
1413         return res;
1414 }
1415
1416 /*!
1417  * \brief Conditionally unref an rtp instance
1418  */
1419 static void unref_instance_cond(struct ast_rtp_instance **instance)
1420 {
1421         if (*instance) {
1422                 ao2_ref(*instance, -1);
1423                 *instance = NULL;
1424         }
1425 }
1426
1427 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)
1428 {
1429         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1430                         *vinstance0 = NULL, *vinstance1 = NULL,
1431                         *tinstance0 = NULL, *tinstance1 = NULL;
1432         struct ast_rtp_glue *glue0, *glue1;
1433         struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
1434         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1435         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1436         enum ast_bridge_result res = AST_BRIDGE_FAILED;
1437         enum ast_rtp_dtmf_mode dmode;
1438         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1439         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1440         int unlock_chans = 1;
1441
1442         if (!cap0 || !cap1) {
1443                 unlock_chans = 0;
1444                 goto done;
1445         }
1446
1447         /* Lock both channels so we can look for the glue that binds them together */
1448         ast_channel_lock(c0);
1449         while (ast_channel_trylock(c1)) {
1450                 ast_channel_unlock(c0);
1451                 usleep(1);
1452                 ast_channel_lock(c0);
1453         }
1454
1455         /* Ensure neither channel got hungup during lock avoidance */
1456         if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1457                 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1458                 goto done;
1459         }
1460
1461         /* Grab glue that binds each channel to something using the RTP engine */
1462         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1463                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1464                 goto done;
1465         }
1466
1467         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1468         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1469
1470         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1471         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1472
1473         /* If the channels are of the same technology, they might have limitations on remote bridging */
1474         if (ast_channel_tech(c0) == ast_channel_tech(c1)) {
1475                 if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
1476                         if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, c1))) {
1477                                 /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
1478                                 audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1479                         }
1480                 }
1481                 if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
1482                         if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, c1))) {
1483                                 /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
1484                                 video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1485                         }
1486                 }
1487         }
1488
1489         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1490         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)) {
1491                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1492         }
1493         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)) {
1494                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1495         }
1496
1497         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1498         if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1499                 res = AST_BRIDGE_FAILED_NOWARN;
1500                 goto done;
1501         }
1502
1503
1504         /* If address families differ, force a local bridge */
1505         ast_rtp_instance_get_remote_address(instance0, &addr1);
1506         ast_rtp_instance_get_remote_address(instance1, &addr2);
1507
1508         if (addr1.ss.ss_family != addr2.ss.ss_family ||
1509            (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
1510                 audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
1511                 audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1512         }
1513
1514         /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1515         dmode = ast_rtp_instance_dtmf_mode_get(instance0);
1516         if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
1517                 res = AST_BRIDGE_FAILED_NOWARN;
1518                 goto done;
1519         }
1520         dmode = ast_rtp_instance_dtmf_mode_get(instance1);
1521         if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
1522                 res = AST_BRIDGE_FAILED_NOWARN;
1523                 goto done;
1524         }
1525
1526         /* 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 */
1527         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)))) {
1528                 res = AST_BRIDGE_FAILED_NOWARN;
1529                 goto done;
1530         }
1531
1532         /* Make sure that codecs match */
1533         if (glue0->get_codec){
1534                 glue0->get_codec(c0, cap0);
1535         }
1536         if (glue1->get_codec) {
1537                 glue1->get_codec(c1, cap1);
1538         }
1539         if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
1540                 char tmp0[256] = { 0, };
1541                 char tmp1[256] = { 0, };
1542                 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
1543                         ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
1544                         ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
1545                 res = AST_BRIDGE_FAILED_NOWARN;
1546                 goto done;
1547         }
1548
1549         instance0->glue = glue0;
1550         instance1->glue = glue1;
1551         instance0->chan = c0;
1552         instance1->chan = c1;
1553
1554         /* Depending on the end result for bridging either do a local bridge or remote bridge */
1555         if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1556                 ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
1557                 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
1558         } else {
1559                 ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
1560                 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1561                                 tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
1562                                 fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
1563         }
1564
1565         instance0->glue = NULL;
1566         instance1->glue = NULL;
1567         instance0->chan = NULL;
1568         instance1->chan = NULL;
1569
1570         unlock_chans = 0;
1571
1572 done:
1573         if (unlock_chans) {
1574                 ast_channel_unlock(c0);
1575                 ast_channel_unlock(c1);
1576         }
1577         ast_format_cap_destroy(cap1);
1578         ast_format_cap_destroy(cap0);
1579
1580         unref_instance_cond(&instance0);
1581         unref_instance_cond(&instance1);
1582         unref_instance_cond(&vinstance0);
1583         unref_instance_cond(&vinstance1);
1584         unref_instance_cond(&tinstance0);
1585         unref_instance_cond(&tinstance1);
1586
1587         return res;
1588 }
1589
1590 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1591 {
1592         return instance->bridged;
1593 }
1594
1595 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1596 {
1597         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1598                 *vinstance0 = NULL, *vinstance1 = NULL,
1599                 *tinstance0 = NULL, *tinstance1 = NULL;
1600         struct ast_rtp_glue *glue0, *glue1;
1601         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1602         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1603         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1604         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1605
1606         /* Lock both channels so we can look for the glue that binds them together */
1607         ast_channel_lock_both(c0, c1);
1608
1609         if (!cap1 || !cap0) {
1610                 goto done;
1611         }
1612
1613         /* Grab glue that binds each channel to something using the RTP engine */
1614         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1615                 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1616                 goto done;
1617         }
1618
1619         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1620         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1621
1622         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1623         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1624
1625         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1626         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)) {
1627                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1628         }
1629         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)) {
1630                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1631         }
1632         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) {
1633                 glue0->get_codec(c0, cap0);
1634         }
1635         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) {
1636                 glue1->get_codec(c1, cap1);
1637         }
1638
1639         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1640         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1641                 goto done;
1642         }
1643
1644         /* Make sure we have matching codecs */
1645         if (!ast_format_cap_has_joint(cap0, cap1)) {
1646                 goto done;
1647         }
1648
1649         ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1650
1651         if (vinstance0 && vinstance1) {
1652                 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1653         }
1654         if (tinstance0 && tinstance1) {
1655                 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1656         }
1657
1658         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1659                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
1660                         ast_channel_name(c0), ast_channel_name(c1));
1661         } else {
1662                 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
1663                         ast_channel_name(c0), ast_channel_name(c1));
1664         }
1665
1666 done:
1667         ast_channel_unlock(c0);
1668         ast_channel_unlock(c1);
1669
1670         ast_format_cap_destroy(cap0);
1671         ast_format_cap_destroy(cap1);
1672
1673         unref_instance_cond(&instance0);
1674         unref_instance_cond(&instance1);
1675         unref_instance_cond(&vinstance0);
1676         unref_instance_cond(&vinstance1);
1677         unref_instance_cond(&tinstance0);
1678         unref_instance_cond(&tinstance1);
1679 }
1680
1681 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1682 {
1683         struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1684                         *vinstance0 = NULL, *vinstance1 = NULL,
1685                         *tinstance0 = NULL, *tinstance1 = NULL;
1686         struct ast_rtp_glue *glue0, *glue1;
1687         enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1688         enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1689         struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1690         struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1691         int res = 0;
1692
1693         /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1694         if (!c1) {
1695                 ast_format_cap_destroy(cap0);
1696                 ast_format_cap_destroy(cap1);
1697                 return -1;
1698         }
1699
1700         /* Lock both channels so we can look for the glue that binds them together */
1701         ast_channel_lock(c0);
1702         while (ast_channel_trylock(c1)) {
1703                 ast_channel_unlock(c0);
1704                 usleep(1);
1705                 ast_channel_lock(c0);
1706         }
1707
1708         if (!cap1 || !cap0) {
1709                 goto done;
1710         }
1711
1712         /* Grab glue that binds each channel to something using the RTP engine */
1713         if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1714                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1715                 goto done;
1716         }
1717
1718         audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1719         video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1720
1721         audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1722         video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1723
1724         /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1725         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)) {
1726                 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1727         }
1728         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)) {
1729                 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1730         }
1731         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) {
1732                 glue0->get_codec(c0, cap0);
1733         }
1734         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) {
1735                 glue1->get_codec(c1, cap1);
1736         }
1737
1738         /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1739         if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1740                 goto done;
1741         }
1742
1743         /* Make sure we have matching codecs */
1744         if (!ast_format_cap_has_joint(cap0, cap1)) {
1745                 goto done;
1746         }
1747
1748         /* Bridge media early */
1749         if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1750                 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1751         }
1752
1753         res = 0;
1754
1755 done:
1756         ast_channel_unlock(c0);
1757         ast_channel_unlock(c1);
1758
1759         ast_format_cap_destroy(cap0);
1760         ast_format_cap_destroy(cap1);
1761
1762         unref_instance_cond(&instance0);
1763         unref_instance_cond(&instance1);
1764         unref_instance_cond(&vinstance0);
1765         unref_instance_cond(&vinstance1);
1766         unref_instance_cond(&tinstance0);
1767         unref_instance_cond(&tinstance1);
1768
1769         if (!res) {
1770                 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1771         }
1772
1773         return res;
1774 }
1775
1776 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1777 {
1778         return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1779 }
1780
1781 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1782 {
1783         return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1784 }
1785
1786 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1787 {
1788         return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1789 }
1790
1791 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1792 {
1793         struct ast_rtp_instance_stats stats = { 0, };
1794         enum ast_rtp_instance_stat stat;
1795
1796         /* Determine what statistics we will need to retrieve based on field passed in */
1797         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1798                 stat = AST_RTP_INSTANCE_STAT_ALL;
1799         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1800                 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1801         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1802                 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1803         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1804                 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1805         } else {
1806                 return NULL;
1807         }
1808
1809         /* Attempt to actually retrieve the statistics we need to generate the quality string */
1810         if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1811                 return NULL;
1812         }
1813
1814         /* Now actually fill the buffer with the good information */
1815         if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1816                 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1817                          stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1818         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1819                 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1820                          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));
1821         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1822                 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1823                          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));
1824         } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1825                 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1826         }
1827
1828         return buf;
1829 }
1830
1831 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1832 {
1833         char quality_buf[AST_MAX_USER_FIELD], *quality;
1834         struct ast_channel *bridge = ast_bridged_channel(chan);
1835
1836         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1837                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1838                 if (bridge) {
1839                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1840                 }
1841         }
1842
1843         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1844                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1845                 if (bridge) {
1846                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1847                 }
1848         }
1849
1850         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1851                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1852                 if (bridge) {
1853                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1854                 }
1855         }
1856
1857         if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1858                 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1859                 if (bridge) {
1860                         pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1861                 }
1862         }
1863 }
1864
1865 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1866 {
1867         return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1868 }
1869
1870 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1871 {
1872         return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1873 }
1874
1875 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1876 {
1877         struct ast_rtp_glue *glue;
1878         struct ast_rtp_instance *peer_instance = NULL;
1879         int res = -1;
1880
1881         if (!instance->engine->make_compatible) {
1882                 return -1;
1883         }
1884
1885         ast_channel_lock(peer);
1886
1887         if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
1888                 ast_channel_unlock(peer);
1889                 return -1;
1890         }
1891
1892         glue->get_rtp_info(peer, &peer_instance);
1893
1894         if (!peer_instance || peer_instance->engine != instance->engine) {
1895                 ast_channel_unlock(peer);
1896                 ao2_ref(peer_instance, -1);
1897                 peer_instance = NULL;
1898                 return -1;
1899         }
1900
1901         res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1902
1903         ast_channel_unlock(peer);
1904
1905         ao2_ref(peer_instance, -1);
1906         peer_instance = NULL;
1907
1908         return res;
1909 }
1910
1911 void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
1912 {
1913         if (instance->engine->available_formats) {
1914                 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1915                 if (!ast_format_cap_is_empty(result)) {
1916                         return;
1917                 }
1918         }
1919
1920         ast_translate_available_formats(to_endpoint, to_asterisk, result);
1921 }
1922
1923 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1924 {
1925         return instance->engine->activate ? instance->engine->activate(instance) : 0;
1926 }
1927
1928 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1929                                    struct ast_sockaddr *suggestion,
1930                                    const char *username)
1931 {
1932         if (instance->engine->stun_request) {
1933                 instance->engine->stun_request(instance, suggestion, username);
1934         }
1935 }
1936
1937 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1938 {
1939         instance->timeout = timeout;
1940 }
1941
1942 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1943 {
1944         instance->holdtimeout = timeout;
1945 }
1946
1947 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
1948 {
1949         instance->keepalive = interval;
1950 }
1951
1952 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1953 {
1954         return instance->timeout;
1955 }
1956
1957 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1958 {
1959         return instance->holdtimeout;
1960 }
1961
1962 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
1963 {
1964         return instance->keepalive;
1965 }
1966
1967 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1968 {
1969         return instance->engine;
1970 }
1971
1972 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1973 {
1974         return instance->glue;
1975 }
1976
1977 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
1978 {
1979         return instance->chan;
1980 }
1981
1982 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1983 {
1984         if (res_srtp || res_srtp_policy) {
1985                 return -1;
1986         }
1987         if (!srtp_res || !policy_res) {
1988                 return -1;
1989         }
1990
1991         res_srtp = srtp_res;
1992         res_srtp_policy = policy_res;
1993
1994         return 0;
1995 }
1996
1997 void ast_rtp_engine_unregister_srtp(void)
1998 {
1999         res_srtp = NULL;
2000         res_srtp_policy = NULL;
2001 }
2002
2003 int ast_rtp_engine_srtp_is_registered(void)
2004 {
2005         return res_srtp && res_srtp_policy;
2006 }
2007
2008 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
2009 {
2010         int res = 0;
2011
2012         if (!res_srtp) {
2013                 return -1;
2014         }
2015
2016         if (!instance->srtp) {
2017                 res = res_srtp->create(&instance->srtp, instance, remote_policy);
2018         } else {
2019                 res = res_srtp->replace(&instance->srtp, instance, remote_policy);
2020         }
2021         if (!res) {
2022                 res = res_srtp->add_stream(instance->srtp, local_policy);
2023         }
2024
2025         return res;
2026 }
2027
2028 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
2029 {
2030         return instance->srtp;
2031 }
2032
2033 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
2034 {
2035         if (instance->engine->sendcng) {
2036                 return instance->engine->sendcng(instance, level);
2037         }
2038
2039         return -1;
2040 }
2041
2042 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
2043 {
2044         return instance->engine->ice;
2045 }
2046
2047 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
2048 {
2049         int x = mime_types_len;
2050         if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
2051                 return;
2052         }
2053
2054         ast_rwlock_wrlock(&mime_types_lock);
2055         if (format) {
2056                 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
2057                 ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
2058         } else {
2059                 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
2060         }
2061         ast_rtp_mime_types[x].type = type;
2062         ast_rtp_mime_types[x].subtype = subtype;
2063         ast_rtp_mime_types[x].sample_rate = sample_rate;
2064         mime_types_len++;
2065         ast_rwlock_unlock(&mime_types_lock);
2066 }
2067
2068 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
2069 {
2070         int x;
2071         ast_rwlock_wrlock(&static_RTP_PT_lock);
2072         if (map < 0) {
2073                 /* find next available dynamic payload slot */
2074                 for (x = 96; x < 127; x++) {
2075                         if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
2076                                 map = x;
2077                                 break;
2078                         }
2079                 }
2080         }
2081
2082         if (map < 0) {
2083                 ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
2084                 ast_rwlock_unlock(&static_RTP_PT_lock);
2085                 return;
2086         }
2087
2088         if (format) {
2089                 static_RTP_PT[map].asterisk_format = 1;
2090                 ast_format_copy(&static_RTP_PT[map].format, format);
2091         } else {
2092                 static_RTP_PT[map].rtp_code = rtp_code;
2093         }
2094         ast_rwlock_unlock(&static_RTP_PT_lock);
2095 }
2096
2097 int ast_rtp_engine_load_format(const struct ast_format *format)
2098 {
2099         switch (format->id) {
2100         case AST_FORMAT_SILK:
2101                 set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
2102                 add_static_payload(-1, format, 0);
2103                 break;
2104         case AST_FORMAT_CELT:
2105                 set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
2106                 add_static_payload(-1, format, 0);
2107                 break;
2108         default:
2109                 break;
2110         }
2111
2112         return 0;
2113 }
2114
2115 int ast_rtp_engine_unload_format(const struct ast_format *format)
2116 {
2117         int x;
2118         int y = 0;
2119
2120         ast_rwlock_wrlock(&static_RTP_PT_lock);
2121         /* remove everything pertaining to this format id from the lists */
2122         for (x = 0; x < AST_RTP_MAX_PT; x++) {
2123                 if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
2124                         memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
2125                 }
2126         }
2127         ast_rwlock_unlock(&static_RTP_PT_lock);
2128
2129
2130         ast_rwlock_wrlock(&mime_types_lock);
2131         /* rebuild the list skipping the items matching this id */
2132         for (x = 0; x < mime_types_len; x++) {
2133                 if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
2134                         continue;
2135                 }
2136                 ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
2137                 y++;
2138         }
2139         mime_types_len = y;
2140         ast_rwlock_unlock(&mime_types_lock);
2141         return 0;
2142 }
2143
2144 int ast_rtp_engine_init()
2145 {
2146         struct ast_format tmpfmt;
2147
2148         ast_rwlock_init(&mime_types_lock);
2149         ast_rwlock_init(&static_RTP_PT_lock);
2150
2151         /* Define all the RTP mime types available */
2152         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
2153         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
2154         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
2155         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
2156         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
2157         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
2158         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
2159         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
2160         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
2161         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
2162         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
2163         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
2164         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
2165         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
2166         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
2167         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
2168         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0,  "audio", "speex", 16000);
2169         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0,  "audio", "speex", 32000);
2170         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
2171         /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
2172         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
2173         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
2174         set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
2175         set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
2176         set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
2177         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
2178         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
2179         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
2180         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
2181         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
2182         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
2183         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
2184         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
2185         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
2186         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
2187         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
2188         set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
2189
2190         /* Define the static rtp payload mappings */
2191         add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
2192         #ifdef USE_DEPRECATED_G726
2193         add_static_payload(2, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
2194         #endif
2195         add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
2196         add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
2197         add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
2198         add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
2199         add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
2200         add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
2201         add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
2202         add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
2203         add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
2204         add_static_payload(13, NULL, AST_RTP_CN);
2205         add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
2206         add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
2207         add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
2208         add_static_payload(19, NULL, AST_RTP_CN);         /* Also used for CN */
2209         add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
2210         add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
2211         add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
2212         add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
2213         add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2214         add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
2215         add_static_payload(101, NULL, AST_RTP_DTMF);
2216         add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
2217         add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2218         add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
2219         add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0);   /* Real time text chat (with redundancy encoding) */
2220         add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0);     /* Real time text chat */
2221         add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
2222         add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
2223         add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
2224         add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
2225         add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
2226         add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
2227         add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
2228         add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
2229         add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
2230
2231         return 0;
2232 }