Debian Codegraph @

main/rtp_engine.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2008, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
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 <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 /*** DOCUMENTATION
31 <managerEvent language="en_US" name="RTCPSent">
32 <managerEventInstance class="EVENT_FLAG_REPORTING">
33 <since>
34 <version>12.0.0</version>
35 </since>
36 <synopsis>Raised when an RTCP packet is sent.</synopsis>
37 <syntax>
38 <channel_snapshot/>
39 <parameter name="SSRC">
40 <para>The SSRC identifier for our stream</para>
41 </parameter>
42 <parameter name="PT">
43 <para>The type of packet for this RTCP report.</para>
44 <enumlist>
45 <enum name="200(SR)"/>
46 <enum name="201(RR)"/>
47 </enumlist>
48 </parameter>
49 <parameter name="To">
50 <para>The address the report is sent to.</para>
51 </parameter>
52 <parameter name="ReportCount">
53 <para>The number of reports that were sent.</para>
54 <para>The report count determines the number of ReportX headers in
55 the message. The X for each set of report headers will range from 0 to
56 <literal>ReportCount - 1</literal>.</para>
57 </parameter>
58 <parameter name="SentNTP" required="false">
59 <para>The time the sender generated the report. Only valid when
60 PT is <literal>200(SR)</literal>.</para>
61 </parameter>
62 <parameter name="SentRTP" required="false">
63 <para>The sender's last RTP timestamp. Only valid when PT is
64 <literal>200(SR)</literal>.</para>
65 </parameter>
66 <parameter name="SentPackets" required="false">
67 <para>The number of packets the sender has sent. Only valid when PT
68 is <literal>200(SR)</literal>.</para>
69 </parameter>
70 <parameter name="SentOctets" required="false">
71 <para>The number of bytes the sender has sent. Only valid when PT is
72 <literal>200(SR)</literal>.</para>
73 </parameter>
74 <parameter name="ReportXSourceSSRC">
75 <para>The SSRC for the source of this report block.</para>
76 </parameter>
77 <parameter name="ReportXFractionLost">
78 <para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
79 lost since the previous SR or RR report was sent.</para>
80 </parameter>
81 <parameter name="ReportXCumulativeLost">
82 <para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
83 lost since the beginning of reception.</para>
84 </parameter>
85 <parameter name="ReportXHighestSequence">
86 <para>The highest sequence number received in an RTP data packet from
87 <literal>ReportXSourceSSRC</literal>.</para>
88 </parameter>
89 <parameter name="ReportXSequenceNumberCycles">
90 <para>The number of sequence number cycles seen for the RTP data
91 received from <literal>ReportXSourceSSRC</literal>.</para>
92 </parameter>
93 <parameter name="ReportXIAJitter">
94 <para>An estimate of the statistical variance of the RTP data packet
95 interarrival time, measured in timestamp units.</para>
96 </parameter>
97 <parameter name="ReportXLSR">
98 <para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
99 If no SR has been received from <literal>ReportXSourceSSRC</literal>,
100 then 0.</para>
101 </parameter>
102 <parameter name="ReportXDLSR">
103 <para>The delay, expressed in units of 1/65536 seconds, between
104 receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
105 and sending this report.</para>
106 </parameter>
107 </syntax>
108 <see-also>
109 <ref type="managerEvent">RTCPReceived</ref>
110 </see-also>
111 </managerEventInstance>
112 </managerEvent>
113 <managerEvent language="en_US" name="RTCPReceived">
114 <managerEventInstance class="EVENT_FLAG_REPORTING">
115 <since>
116 <version>12.0.0</version>
117 </since>
118 <synopsis>Raised when an RTCP packet is received.</synopsis>
119 <syntax>
120 <channel_snapshot/>
121 <parameter name="SSRC">
122 <para>The SSRC identifier for the remote system</para>
123 </parameter>
124 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
125 <parameter name="From">
126 <para>The address the report was received from.</para>
127 </parameter>
128 <parameter name="RTT">
129 <para>Calculated Round-Trip Time in seconds</para>
130 </parameter>
131 <parameter name="ReportCount">
132 <para>The number of reports that were received.</para>
133 <para>The report count determines the number of ReportX headers in
134 the message. The X for each set of report headers will range from 0 to
135 <literal>ReportCount - 1</literal>.</para>
136 </parameter>
137 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
138 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
139 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
140 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
141 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
142 </syntax>
143 <see-also>
144 <ref type="managerEvent">RTCPSent</ref>
145 </see-also>
146 </managerEventInstance>
147 </managerEvent>
148 ***/
149
150 #include "asterisk.h"
151
152 #include <sched.h> /* for sched_yield */
153 #include <sys/time.h> /* for timeval */
154 #include <time.h> /* for time_t */
155
156 #include "asterisk/_private.h" /* for ast_rtp_engine_init prototype */
157 #include "asterisk/astobj2.h" /* for ao2_cleanup, ao2_ref, etc */
158 #include "asterisk/channel.h" /* for ast_channel_name, etc */
159 #include "asterisk/codec.h" /* for ast_codec_media_type2str, etc */
160 #include "asterisk/format.h" /* for ast_format_cmp, etc */
161 #include "asterisk/format_cache.h" /* for ast_format_adpcm, etc */
162 #include "asterisk/format_cap.h" /* for ast_format_cap_alloc, etc */
163 #include "asterisk/json.h" /* for ast_json_ref, etc */
164 #include "asterisk/linkedlists.h" /* for ast_rtp_engine::<anonymous>, etc */
165 #include "asterisk/lock.h" /* for ast_rwlock_unlock, etc */
166 #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
167 #include "asterisk/manager.h"
168 #include "asterisk/module.h" /* for ast_module_unref, etc */
169 #include "asterisk/netsock2.h" /* for ast_sockaddr_copy, etc */
170 #include "asterisk/options.h" /* for ast_option_rtpptdynamic */
171 #include "asterisk/pbx.h" /* for pbx_builtin_setvar_helper */
172 #include "asterisk/res_srtp.h" /* for ast_srtp_res */
173 #include "asterisk/rtp_engine.h" /* for ast_rtp_codecs, etc */
174 #include "asterisk/stasis.h" /* for stasis_message_data, etc */
175 #include "asterisk/stasis_channels.h" /* for ast_channel_stage_snapshot, etc */
176 #include "asterisk/strings.h" /* for ast_str_append, etc */
177 #include "asterisk/time.h" /* for ast_tvdiff_ms, ast_tvnow */
178 #include "asterisk/translate.h" /* for ast_translate_available_formats */
179 #include "asterisk/utils.h" /* for ast_free, ast_strdup, etc */
180 #include "asterisk/vector.h" /* for AST_VECTOR_GET, etc */
181
182 struct ast_srtp_res *res_srtp = NULL;
183 struct ast_srtp_policy_res *res_srtp_policy = NULL;
184
185 /*! Structure that contains extmap negotiation information */
186 struct rtp_extmap {
187 /*! The RTP extension */
188 enum ast_rtp_extension extension;
189 /*! The current negotiated direction */
190 enum ast_rtp_extension_direction direction;
191 };
192
193 /*! Structure that represents an RTP session (instance) */
194 struct ast_rtp_instance {
195 /*! Engine that is handling this RTP instance */
196 struct ast_rtp_engine *engine;
197 /*! Data unique to the RTP engine */
198 void *data;
199 /*! RTP properties that have been set and their value */
200 int properties[AST_RTP_PROPERTY_MAX];
201 /*! Address that we are expecting RTP to come in to */
202 struct ast_sockaddr local_address;
203 /*! The original source address */
204 struct ast_sockaddr requested_target_address;
205 /*! Address that we are sending RTP to */
206 struct ast_sockaddr incoming_source_address;
207 /*! Instance that we are bridged to if doing remote or local bridging */
208 struct ast_rtp_instance *bridged;
209 /*! Payload and packetization information */
210 struct ast_rtp_codecs codecs;
211 /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
212 int timeout;
213 /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
214 int holdtimeout;
215 /*! RTP keepalive interval */
216 int keepalive;
217 /*! Glue currently in use */
218 struct ast_rtp_glue *glue;
219 /*! SRTP info associated with the instance */
220 struct ast_srtp *srtp;
221 /*! SRTP info dedicated for RTCP associated with the instance */
222 struct ast_srtp *rtcp_srtp;
223 /*! Channel unique ID */
224 char channel_uniqueid[AST_MAX_UNIQUEID];
225 /*! Time of last packet sent */
226 time_t last_tx;
227 /*! Time of last packet received */
228 time_t last_rx;
229 /*! Enabled RTP extensions */
230 AST_VECTOR(, enum ast_rtp_extension_direction) extmap_enabled;
231 /*! Negotiated RTP extensions (using index based on extension) */
232 AST_VECTOR(, int) extmap_negotiated;
233 /*! Negotiated RTP extensions (using index based on unique id) */
234 AST_VECTOR(, struct rtp_extmap) extmap_unique_ids;
235 /*! Per-instance RTP port range start (0 means use global) */
236 unsigned int rtp_port_start;
237 /*! Per-instance RTP port range end (0 means use global) */
238 unsigned int rtp_port_end;
239 };
240
241 /*!
242 * \brief URIs for known RTP extensions
243 */
244 static const char * const rtp_extension_uris[AST_RTP_EXTENSION_MAX] = {
245 [AST_RTP_EXTENSION_UNSUPPORTED] = "",
246 [AST_RTP_EXTENSION_ABS_SEND_TIME] = "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time",
247 [AST_RTP_EXTENSION_TRANSPORT_WIDE_CC] = "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
248 };
249
250 /*! List of RTP engines that are currently registered */
251 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
252
253 /*! List of RTP glues */
254 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
255
256 #define MAX_RTP_MIME_TYPES 128
257
258 /*! The following array defines the MIME Media type (and subtype) for each
259 of our codecs, or RTP-specific data type. */
260 static struct ast_rtp_mime_type {
261 /*! \brief A mapping object between the Asterisk codec and this RTP payload */
262 struct ast_rtp_payload_type payload_type;
263 /*! \brief The media type */
264 char type[16];
265 /*! \brief The format type */
266 char subtype[64];
267 /*! \brief Expected sample rate of the /c subtype */
268 unsigned int sample_rate;
269 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
270 static ast_rwlock_t mime_types_lock;
271 static int mime_types_len = 0;
272
273 /*!
274 * \brief Mapping between Asterisk codecs and rtp payload types
275 *
276 * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
277 * also, our own choices for dynamic payload types. This is our master
278 * table for transmission
279 *
280 * See http://www.iana.org/assignments/rtp-parameters for a list of
281 * assigned values
282 */
283 static struct ast_rtp_payload_type *static_RTP_PT[AST_RTP_MAX_PT];
284 static ast_rwlock_t static_RTP_PT_lock;
285
286 /*! \brief \ref stasis topic for RTP related messages */
287 static struct stasis_topic *rtp_topic;
288
289
290 /*!
291 * \brief Set given json object into target with name
292 *
293 * \param target Target json.
294 * \param name key of given object.
295 * \param obj Json value will be set.
296 */
297 #define SET_AST_JSON_OBJ(target, name, obj) ({ \
298 struct ast_json *j_tmp = obj; \
299 if (j_tmp) { \
300 ast_json_object_set(target, name, j_tmp); \
301 } \
302 })
303
304 /*!
305 * \internal
306 * \brief Destructor for \c ast_rtp_payload_type
307 */
308 static void rtp_payload_type_dtor(void *obj)
309 {
310 struct ast_rtp_payload_type *payload = obj;
311
312 ao2_cleanup(payload->format);
313 }
314
315 static struct ast_rtp_payload_type *rtp_payload_type_alloc(struct ast_format *format,
316 int payload, int rtp_code, int primary_mapping, unsigned int sample_rate)
317 {
318 struct ast_rtp_payload_type *type = ao2_alloc_options(
319 sizeof(*type), rtp_payload_type_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
320
321 if (!type) {
322 return NULL;
323 }
324
325 type->format = ao2_bump(format);
326 type->asterisk_format = type->format != NULL;
327 type->payload = payload;
328 type->rtp_code = rtp_code;
329 type->primary_mapping = primary_mapping;
330 type->sample_rate = sample_rate;
331
332 return type;
333 }
334
335 struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
336 {
337 return rtp_payload_type_alloc(NULL, 0, 0, 0, 0);
338 }
339
340 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
341 {
342 struct ast_rtp_engine *current_engine;
343
344 /* Perform a sanity check on the engine structure to make sure it has the basics */
345 if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
346 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
347 return -1;
348 }
349
350 /* Link owner module to the RTP engine for reference counting purposes */
351 engine->mod = module;
352
353 AST_RWLIST_WRLOCK(&engines);
354
355 /* Ensure that no two modules with the same name are registered at the same time */
356 AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
357 if (!strcmp(current_engine->name, engine->name)) {
358 ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
359 AST_RWLIST_UNLOCK(&engines);
360 return -1;
361 }
362 }
363
364 /* The engine survived our critique. Off to the list it goes to be used */
365 AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
366
367 AST_RWLIST_UNLOCK(&engines);
368
369 ast_verb(5, "Registered RTP engine '%s'\n", engine->name);
370
371 return 0;
372 }
373
374 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
375 {
376 struct ast_rtp_engine *current_engine = NULL;
377
378 AST_RWLIST_WRLOCK(&engines);
379
380 if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
381 ast_verb(5, "Unregistered RTP engine '%s'\n", engine->name);
382 }
383
384 AST_RWLIST_UNLOCK(&engines);
385
386 return current_engine ? 0 : -1;
387 }
388
389 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
390 {
391 struct ast_rtp_glue *current_glue = NULL;
392
393 if (ast_strlen_zero(glue->type)) {
394 return -1;
395 }
396
397 glue->mod = module;
398
399 AST_RWLIST_WRLOCK(&glues);
400
401 AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
402 if (!strcasecmp(current_glue->type, glue->type)) {
403 ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
404 AST_RWLIST_UNLOCK(&glues);
405 return -1;
406 }
407 }
408
409 AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
410
411 AST_RWLIST_UNLOCK(&glues);
412
413 ast_verb(5, "Registered RTP glue '%s'\n", glue->type);
414
415 return 0;
416 }
417
418 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
419 {
420 struct ast_rtp_glue *current_glue = NULL;
421
422 AST_RWLIST_WRLOCK(&glues);
423
424 if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
425 ast_verb(5, "Unregistered RTP glue '%s'\n", glue->type);
426 }
427
428 AST_RWLIST_UNLOCK(&glues);
429
430 return current_glue ? 0 : -1;
431 }
432
433 static void instance_destructor(void *obj)
434 {
435 struct ast_rtp_instance *instance = obj;
436
437 /* Pass us off to the engine to destroy */
438 if (instance->data) {
439 /*
440 * Lock in case the RTP engine has other threads that
441 * need synchronization with the destruction.
442 */
443 ao2_lock(instance);
444 instance->engine->destroy(instance);
445 ao2_unlock(instance);
446 }
447
448 if (instance->srtp) {
449 res_srtp->destroy(instance->srtp);
450 }
451
452 if (instance->rtcp_srtp) {
453 res_srtp->destroy(instance->rtcp_srtp);
454 }
455
456 ast_rtp_codecs_payloads_destroy(&instance->codecs);
457
458 AST_VECTOR_FREE(&instance->extmap_enabled);
459 AST_VECTOR_FREE(&instance->extmap_negotiated);
460 AST_VECTOR_FREE(&instance->extmap_unique_ids);
461
462 /* Drop our engine reference */
463 ast_module_unref(instance->engine->mod);
464
465 ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
466 }
467
468 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
469 {
470 if (!instance) {
471 return 0;
472 }
473 if (ast_debug_rtp_is_allowed) {
474 char buffer[4][512];
475 ast_debug_rtp(1, "%s:\n"
476 " RTT: %s\n"
477 " Loss: %s\n"
478 " Jitter: %s\n"
479 " MES: %s\n",
480 instance->channel_uniqueid,
481 ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT,
482 buffer[0], sizeof(buffer[0])),
483 ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS,
484 buffer[1], sizeof(buffer[1])),
485 ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER,
486 buffer[2], sizeof(buffer[2])),
487 ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_MES,
488 buffer[3], sizeof(buffer[3]))
489 );
490 }
491
492 ao2_cleanup(instance);
493
494 return 0;
495 }
496
497 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
498 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
499 void *data)
500 {
501 return ast_rtp_instance_new_with_options(
502 engine_name, sched, sa, data, NULL);
503 }
504
505 struct ast_rtp_instance *ast_rtp_instance_new_with_options(const char *engine_name,
506 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
507 void *data, const struct ast_rtp_instance_options *options)
508 {
509 struct ast_sockaddr address = {{0,}};
510 struct ast_rtp_instance *instance = NULL;
511 struct ast_rtp_engine *engine = NULL;
512 struct ast_module *mod_ref;
513 unsigned int port_start = options ? options->port_start : 0;
514 unsigned int port_end = options ? options->port_end : 0;
515
516 AST_RWLIST_RDLOCK(&engines);
517
518 /* If an engine name was specified try to use it or otherwise use the first one registered */
519 if (!ast_strlen_zero(engine_name)) {
520 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
521 if (!strcmp(engine->name, engine_name)) {
522 break;
523 }
524 }
525 } else {
526 engine = AST_RWLIST_FIRST(&engines);
527 }
528
529 /* If no engine was actually found bail out now */
530 if (!engine) {
531 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
532 AST_RWLIST_UNLOCK(&engines);
533 return NULL;
534 }
535
536 /* Bump up the reference count before we return so the module can not be unloaded */
537 mod_ref = ast_module_running_ref(engine->mod);
538
539 AST_RWLIST_UNLOCK(&engines);
540
541 if (!mod_ref) {
542 /* BUGBUG: improve handling of this situation. */
543 return NULL;
544 }
545
546 /* Allocate a new RTP instance */
547 if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
548 ast_module_unref(engine->mod);
549 return NULL;
550 }
551 instance->engine = engine;
552 ast_sockaddr_copy(&instance->local_address, sa);
553 ast_sockaddr_copy(&address, sa);
554
555 /* Set the per-instance port range before the engine allocates the transport */
556 instance->rtp_port_start = port_start;
557 instance->rtp_port_end = port_end;
558
559 if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
560 ao2_ref(instance, -1);
561 return NULL;
562 }
563
564 /* Initialize RTP extension support */
565 if (AST_VECTOR_INIT(&instance->extmap_enabled, 0) ||
566 AST_VECTOR_INIT(&instance->extmap_negotiated, 0) ||
567 AST_VECTOR_INIT(&instance->extmap_unique_ids, 0)) {
568 ao2_ref(instance, -1);
569 return NULL;
570 }
571
572 if (port_start && port_end) {
573 ast_debug(1, "Using engine '%s' for RTP instance '%p' with port range %d-%d\n",
574 engine->name, instance, port_start, port_end);
575 } else {
576 ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
577 }
578
579 /*
580 * And pass it off to the engine to setup
581 *
582 * Lock in case the RTP engine has other threads that
583 * need synchronization with the construction.
584 */
585 ao2_lock(instance);
586 if (instance->engine->new(instance, sched, &address, data)) {
587 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
588 ao2_unlock(instance);
589 ao2_ref(instance, -1);
590 return NULL;
591 }
592 ao2_unlock(instance);
593
594 ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
595
596 return instance;
597 }
598
599 const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
600 {
601 return instance->channel_uniqueid;
602 }
603
604 void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
605 {
606 ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
607 }
608
609 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
610 {
611 instance->data = data;
612 }
613
614 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
615 {
616 return instance->data;
617 }
618
619 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
620 {
621 int res;
622
623 ao2_lock(instance);
624 res = instance->engine->write(instance, frame);
625 ao2_unlock(instance);
626 return res;
627 }
628
629 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
630 {
631 struct ast_frame *frame;
632
633 ao2_lock(instance);
634 frame = instance->engine->read(instance, rtcp);
635 ao2_unlock(instance);
636 return frame;
637 }
638
639 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
640 const struct ast_sockaddr *address)
641 {
642 ao2_lock(instance);
643 ast_sockaddr_copy(&instance->local_address, address);
644 ao2_unlock(instance);
645 return 0;
646 }
647
648 static void rtp_instance_set_incoming_source_address_nolock(struct ast_rtp_instance *instance,
649 const struct ast_sockaddr *address)
650 {
651 ast_sockaddr_copy(&instance->incoming_source_address, address);
652 if (instance->engine->remote_address_set) {
653 instance->engine->remote_address_set(instance, &instance->incoming_source_address);
654 }
655 }
656
657 int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
658 const struct ast_sockaddr *address)
659 {
660 ao2_lock(instance);
661 rtp_instance_set_incoming_source_address_nolock(instance, address);
662 ao2_unlock(instance);
663
664 return 0;
665 }
666
667 int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
668 const struct ast_sockaddr *address)
669 {
670 ao2_lock(instance);
671
672 ast_sockaddr_copy(&instance->requested_target_address, address);
673 rtp_instance_set_incoming_source_address_nolock(instance, address);
674
675 ao2_unlock(instance);
676
677 return 0;
678 }
679
680 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
681 struct ast_sockaddr *address)
682 {
683 ao2_lock(instance);
684 if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
685 ast_sockaddr_copy(address, &instance->local_address);
686 ao2_unlock(instance);
687 return 1;
688 }
689 ao2_unlock(instance);
690
691 return 0;
692 }
693
694 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
695 struct ast_sockaddr *address)
696 {
697 ao2_lock(instance);
698 ast_sockaddr_copy(address, &instance->local_address);
699 ao2_unlock(instance);
700 }
701
702 int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
703 struct ast_sockaddr *address)
704 {
705 ao2_lock(instance);
706 if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
707 ast_sockaddr_copy(address, &instance->requested_target_address);
708 ao2_unlock(instance);
709 return 1;
710 }
711 ao2_unlock(instance);
712
713 return 0;
714 }
715
716 void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
717 struct ast_sockaddr *address)
718 {
719 ao2_lock(instance);
720 ast_sockaddr_copy(address, &instance->incoming_source_address);
721 ao2_unlock(instance);
722 }
723
724 void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
725 struct ast_sockaddr *address)
726 {
727 ao2_lock(instance);
728 ast_sockaddr_copy(address, &instance->requested_target_address);
729 ao2_unlock(instance);
730 }
731
732 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
733 {
734 if (instance->engine->extended_prop_set) {
735 ao2_lock(instance);
736 instance->engine->extended_prop_set(instance, property, value);
737 ao2_unlock(instance);
738 }
739 }
740
741 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
742 {
743 void *prop;
744
745 if (instance->engine->extended_prop_get) {
746 ao2_lock(instance);
747 prop = instance->engine->extended_prop_get(instance, property);
748 ao2_unlock(instance);
749 } else {
750 prop = NULL;
751 }
752
753 return prop;
754 }
755
756 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
757 {
758 ao2_lock(instance);
759 instance->properties[property] = value;
760
761 if (instance->engine->prop_set) {
762 instance->engine->prop_set(instance, property, value);
763 }
764 ao2_unlock(instance);
765 }
766
767 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
768 {
769 int prop;
770
771 ao2_lock(instance);
772 prop = instance->properties[property];
773 ao2_unlock(instance);
774
775 return prop;
776 }
777
778 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
779 {
780 return &instance->codecs;
781 }
782
783 int ast_rtp_instance_extmap_enable(struct ast_rtp_instance *instance, int id, enum ast_rtp_extension extension,
784 enum ast_rtp_extension_direction direction)
785 {
786 struct rtp_extmap extmap = {
787 .extension = extension,
788 .direction = direction,
789 };
790
791 ao2_lock(instance);
792
793 if (!instance->engine->extension_enable || !instance->engine->extension_enable(instance, extension)) {
794 ao2_unlock(instance);
795 return 0;
796 }
797
798 /* We store enabled extensions separately so we can easily do negotiation */
799 if (AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, direction)) {
800 ao2_unlock(instance);
801 return -1;
802 }
803
804 if (id <= 0) {
805 /* We find a free unique identifier for this extension by just appending it to the
806 * vector of unique ids. The size of the vector will become its unique identifier.
807 * As well when we are asking for information on the extensions it will be returned,
808 * allowing it to be added to the SDP offer.
809 */
810 if (AST_VECTOR_APPEND(&instance->extmap_unique_ids, extmap)) {
811 AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
812 ao2_unlock(instance);
813 return -1;
814 }
815 id = AST_VECTOR_SIZE(&instance->extmap_unique_ids);
816 } else {
817 /* Otherwise we put it precisely where they want it */
818 if (AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap)) {
819 AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
820 ao2_unlock(instance);
821 return -1;
822 }
823 }
824
825 /* Now that we have an id add the extension to here */
826 if (AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id)) {
827 extmap.extension = AST_RTP_EXTENSION_UNSUPPORTED;
828 extmap.direction = AST_RTP_EXTENSION_DIRECTION_NONE;
829 AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
830 AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap);
831 ao2_unlock(instance);
832 return -1;
833 }
834
835 ao2_unlock(instance);
836
837 return 0;
838 }
839
840 /*! \brief Helper function which negotiates two RTP extension directions to get our current direction */
841 static enum ast_rtp_extension_direction rtp_extmap_negotiate_direction(enum ast_rtp_extension_direction ours,
842 enum ast_rtp_extension_direction theirs)
843 {
844 if (theirs == AST_RTP_EXTENSION_DIRECTION_NONE || ours == AST_RTP_EXTENSION_DIRECTION_NONE) {
845 /* This should not occur but if it does tolerate either side not having this extension
846 * in use.
847 */
848 return AST_RTP_EXTENSION_DIRECTION_NONE;
849 } else if (theirs == AST_RTP_EXTENSION_DIRECTION_INACTIVE) {
850 /* Inactive is always inactive on our side */
851 return AST_RTP_EXTENSION_DIRECTION_INACTIVE;
852 } else if (theirs == AST_RTP_EXTENSION_DIRECTION_SENDRECV) {
853 return ours;
854 } else if (theirs == AST_RTP_EXTENSION_DIRECTION_SENDONLY) {
855 /* If they are send only then we become recvonly if we are configured as sendrecv or recvonly */
856 if (ours == AST_RTP_EXTENSION_DIRECTION_SENDRECV || ours == AST_RTP_EXTENSION_DIRECTION_RECVONLY) {
857 return AST_RTP_EXTENSION_DIRECTION_RECVONLY;
858 }
859 } else if (theirs == AST_RTP_EXTENSION_DIRECTION_RECVONLY) {
860 /* If they are recv only then we become sendonly if we are configured as sendrecv or sendonly */
861 if (ours == AST_RTP_EXTENSION_DIRECTION_SENDRECV || ours == AST_RTP_EXTENSION_DIRECTION_SENDONLY) {
862 return AST_RTP_EXTENSION_DIRECTION_SENDONLY;
863 }
864 }
865
866 return AST_RTP_EXTENSION_DIRECTION_NONE;
867 }
868
869 int ast_rtp_instance_extmap_negotiate(struct ast_rtp_instance *instance, int id, enum ast_rtp_extension_direction direction,
870 const char *uri, const char *attributes)
871 {
872 /* 'attributes' is currently unused but exists in the API to ensure it does not need to be altered
873 * in the future in case we need to use it.
874 */
875 int idx;
876 enum ast_rtp_extension extension = AST_RTP_EXTENSION_UNSUPPORTED;
877
878 /* Per the RFC the identifier has to be 1 or above */
879 if (id < 1) {
880 return -1;
881 }
882
883 /* Convert the provided URI to the internal representation */
884 for (idx = 0; idx < ARRAY_LEN(rtp_extension_uris); ++idx) {
885 if (!strcasecmp(rtp_extension_uris[idx], uri)) {
886 extension = idx;
887 break;
888 }
889 }
890
891 ao2_lock(instance);
892 /* We only accept the extension if it is enabled */
893 if (extension < AST_VECTOR_SIZE(&instance->extmap_enabled) &&
894 AST_VECTOR_GET(&instance->extmap_enabled, extension) != AST_RTP_EXTENSION_DIRECTION_NONE) {
895 struct rtp_extmap extmap = {
896 .extension = extension,
897 .direction = rtp_extmap_negotiate_direction(AST_VECTOR_GET(&instance->extmap_enabled, extension), direction),
898 };
899
900 /* If the direction negotiation failed then don't accept or use this extension */
901 if (extmap.direction != AST_RTP_EXTENSION_DIRECTION_NONE) {
902 if (extension != AST_RTP_EXTENSION_UNSUPPORTED) {
903 AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id);
904 }
905 AST_VECTOR_REPLACE(&instance->extmap_unique_ids, id - 1, extmap);
906 }
907 }
908 ao2_unlock(instance);
909
910 return 0;
911 }
912
913 void ast_rtp_instance_extmap_clear(struct ast_rtp_instance *instance)
914 {
915 static const struct rtp_extmap extmap_none = {
916 .extension = AST_RTP_EXTENSION_UNSUPPORTED,
917 .direction = AST_RTP_EXTENSION_DIRECTION_NONE,
918 };
919 int idx;
920
921 ao2_lock(instance);
922
923 /* Clear both the known unique ids and the negotiated extensions as we are about to have
924 * new results set on us.
925 */
926 for (idx = 0; idx < AST_VECTOR_SIZE(&instance->extmap_unique_ids); ++idx) {
927 AST_VECTOR_REPLACE(&instance->extmap_unique_ids, idx, extmap_none);
928 }
929
930 for (idx = 0; idx < AST_VECTOR_SIZE(&instance->extmap_negotiated); ++idx) {
931 AST_VECTOR_REPLACE(&instance->extmap_negotiated, idx, -1);
932 }
933
934 ao2_unlock(instance);
935 }
936
937 int ast_rtp_instance_extmap_get_id(struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
938 {
939 int id = -1;
940
941 ao2_lock(instance);
942 if (extension < AST_VECTOR_SIZE(&instance->extmap_negotiated)) {
943 id = AST_VECTOR_GET(&instance->extmap_negotiated, extension);
944 }
945 ao2_unlock(instance);
946
947 return id;
948 }
949
950 size_t ast_rtp_instance_extmap_count(struct ast_rtp_instance *instance)
951 {
952 size_t count;
953
954 ao2_lock(instance);
955 count = AST_VECTOR_SIZE(&instance->extmap_unique_ids);
956 ao2_unlock(instance);
957
958 return count;
959 }
960
961 enum ast_rtp_extension ast_rtp_instance_extmap_get_extension(struct ast_rtp_instance *instance, int id)
962 {
963 enum ast_rtp_extension extension = AST_RTP_EXTENSION_UNSUPPORTED;
964
965 ao2_lock(instance);
966
967 /* The local unique identifier starts at '1' so the highest unique identifier
968 * can be the actual size of the vector. We compensate (as it is 0 index based)
969 * by dropping it down to 1 to get the correct information.
970 */
971 if (0 < id && id <= AST_VECTOR_SIZE(&instance->extmap_unique_ids)) {
972 struct rtp_extmap *extmap = AST_VECTOR_GET_ADDR(&instance->extmap_unique_ids, id - 1);
973
974 extension = extmap->extension;
975 }
976 ao2_unlock(instance);
977
978 return extension;
979 }
980
981 enum ast_rtp_extension_direction ast_rtp_instance_extmap_get_direction(struct ast_rtp_instance *instance, int id)
982 {
983 enum ast_rtp_extension_direction direction = AST_RTP_EXTENSION_DIRECTION_NONE;
984
985 ao2_lock(instance);
986
987 if (0 < id && id <= AST_VECTOR_SIZE(&instance->extmap_unique_ids)) {
988 struct rtp_extmap *extmap = AST_VECTOR_GET_ADDR(&instance->extmap_unique_ids, id - 1);
989
990 direction = extmap->direction;
991 }
992 ao2_unlock(instance);
993
994 return direction;
995 }
996
997 const char *ast_rtp_instance_extmap_get_uri(struct ast_rtp_instance *instance, int id)
998 {
999 enum ast_rtp_extension extension = ast_rtp_instance_extmap_get_extension(instance, id);
1000
1001 if (extension == AST_RTP_EXTENSION_UNSUPPORTED ||
1002 (unsigned int)extension >= ARRAY_LEN(rtp_extension_uris)) {
1003 return NULL;
1004 }
1005
1006 return rtp_extension_uris[extension];
1007 }
1008
1009 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
1010 {
1011 int res;
1012
1013 codecs->framing = 0;
1014 ast_rwlock_init(&codecs->codecs_lock);
1015 res = AST_VECTOR_INIT(&codecs->payload_mapping_rx, AST_RTP_MAX_PT);
1016 res |= AST_VECTOR_INIT(&codecs->payload_mapping_tx, AST_RTP_MAX_PT);
1017 if (res) {
1018 AST_VECTOR_FREE(&codecs->payload_mapping_rx);
1019 AST_VECTOR_FREE(&codecs->payload_mapping_tx);
1020 }
1021
1022 return res;
1023 }
1024
1025 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
1026 {
1027 int idx;
1028 struct ast_rtp_payload_type *type;
1029
1030 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1031 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1032 ao2_t_cleanup(type, "destroying ast_rtp_codec rx mapping");
1033 }
1034 AST_VECTOR_FREE(&codecs->payload_mapping_rx);
1035
1036 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1037 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1038 ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
1039 }
1040 AST_VECTOR_FREE(&codecs->payload_mapping_tx);
1041
1042 ao2_t_cleanup(codecs->preferred_format, "destroying ast_rtp_codec preferred format");
1043 codecs->preferred_format = NULL;
1044
1045 ast_rwlock_destroy(&codecs->codecs_lock);
1046 }
1047
1048 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
1049 {
1050 ast_rtp_codecs_payloads_destroy(codecs);
1051 ast_rtp_codecs_payloads_initialize(codecs);
1052
1053 if (instance && instance->engine && instance->engine->payload_set) {
1054 int i;
1055
1056 ao2_lock(instance);
1057 for (i = 0; i < AST_RTP_MAX_PT; i++) {
1058 instance->engine->payload_set(instance, i, 0, NULL, 0);
1059 }
1060 ao2_unlock(instance);
1061 }
1062 }
1063
1064 /*!
1065 * \internal
1066 * \brief Clear the rx primary mapping flag on all other matching mappings.
1067 * \since 14.0.0
1068 *
1069 * \param codecs Codecs that need rx clearing.
1070 * \param to_match Payload type object to compare against.
1071 *
1072 * \note It is assumed that codecs is write locked before calling.
1073 */
1074 static void payload_mapping_rx_clear_primary(struct ast_rtp_codecs *codecs, struct ast_rtp_payload_type *to_match)
1075 {
1076 int idx;
1077 struct ast_rtp_payload_type *current;
1078 struct ast_rtp_payload_type *new_type;
1079 struct timeval now;
1080
1081 if (!to_match->primary_mapping) {
1082 return;
1083 }
1084
1085 now = ast_tvnow();
1086 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1087 current = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1088
1089 if (!current || current == to_match || !current->primary_mapping) {
1090 continue;
1091 }
1092 if (current->asterisk_format && to_match->asterisk_format) {
1093 if (ast_format_cmp(current->format, to_match->format) == AST_FORMAT_CMP_NOT_EQUAL) {
1094 continue;
1095 }
1096 } else if (!current->asterisk_format && !to_match->asterisk_format) {
1097 if (current->rtp_code != to_match->rtp_code) {
1098 continue;
1099 }
1100 } else {
1101 continue;
1102 }
1103
1104 /* Replace current with non-primary marked version */
1105 new_type = ast_rtp_engine_alloc_payload_type();
1106 if (!new_type) {
1107 continue;
1108 }
1109 *new_type = *current;
1110 new_type->primary_mapping = 0;
1111 new_type->when_retired = now;
1112 ao2_bump(new_type->format);
1113 AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, idx, new_type);
1114 ao2_ref(current, -1);
1115 }
1116 }
1117
1118 /*!
1119 * \internal
1120 * \brief Put the new_type into the rx payload type mapping.
1121 * \since 21.0.0
1122 *
1123 * \param codecs Codecs structure to put new_type into
1124 * \param payload type position to replace.
1125 * \param new_type RTP payload mapping object to store.
1126 * \param replace Clear the primary flag
1127 *
1128 * \note It is assumed that codecs is write locked before calling.
1129 */
1130 static void rtp_codecs_payload_set_rx(struct ast_rtp_codecs *codecs, int payload, struct ast_rtp_payload_type *new_type, int replace)
1131 {
1132 ao2_ref(new_type, +1);
1133 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1134 ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_rx, payload),
1135 "cleaning up rx mapping vector element about to be replaced");
1136 }
1137 if (AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, payload, new_type)) {
1138 ao2_ref(new_type, -1);
1139 return;
1140 }
1141
1142 if (replace) {
1143 payload_mapping_rx_clear_primary(codecs, new_type);
1144 }
1145 }
1146
1147 /*!
1148 * \internal
1149 * \brief Put the new_type into the rx payload type mapping.
1150 * \since 14.0.0
1151 *
1152 * \param codecs Codecs structure to put new_type into
1153 * \param payload type position to replace.
1154 * \param new_type RTP payload mapping object to store.
1155 *
1156 * \note It is assumed that codecs is write locked before calling.
1157 */
1158 static void rtp_codecs_payload_replace_rx(struct ast_rtp_codecs *codecs, int payload, struct ast_rtp_payload_type *new_type) {
1159 rtp_codecs_payload_set_rx(codecs, payload, new_type, 1);
1160 }
1161
1162
1163 /*!
1164 * \internal
1165 * \brief Copy the rx payload type mapping to the destination.
1166 * \since 14.0.0
1167 *
1168 * \param src The source codecs structure
1169 * \param dest The destination codecs structure that the values from src will be copied to
1170 * \param instance Optionally the instance that the dst codecs structure belongs to
1171 *
1172 * \note It is assumed that src is at least read locked before calling.
1173 * \note It is assumed that dest is write locked before calling.
1174 */
1175 static void rtp_codecs_payloads_copy_rx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1176 {
1177 int idx;
1178 struct ast_rtp_payload_type *type;
1179
1180 for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_rx); ++idx) {
1181 type = AST_VECTOR_GET(&src->payload_mapping_rx, idx);
1182 if (!type) {
1183 continue;
1184 }
1185
1186 ast_debug(2, "Copying rx payload mapping %d (%p) from %p to %p\n",
1187 idx, type, src, dest);
1188 rtp_codecs_payload_replace_rx(dest, idx, type);
1189
1190 if (instance && instance->engine && instance->engine->payload_set) {
1191 ao2_lock(instance);
1192 instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1193 ao2_unlock(instance);
1194 }
1195 }
1196 }
1197
1198 /*!
1199 * \internal
1200 * \brief Determine if a type of payload is already present in mappings.
1201 * \since 14.0.0
1202 *
1203 * \param codecs Codecs to be checked for mappings.
1204 * \param to_match Payload type object to compare against.
1205 *
1206 * \note It is assumed that codecs is write locked before calling.
1207 *
1208 * \retval 0 not found
1209 * \retval 1 found
1210 */
1211 static int payload_mapping_tx_is_present(const struct ast_rtp_codecs *codecs, const struct ast_rtp_payload_type *to_match)
1212 {
1213 int idx;
1214 struct ast_rtp_payload_type *current;
1215
1216 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1217 current = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1218
1219 if (!current) {
1220 continue;
1221 }
1222 if (current == to_match) {
1223 /* The exact object is already in the mapping. */
1224 return 1;
1225 }
1226 if (current->asterisk_format && to_match->asterisk_format) {
1227 if (ast_format_get_codec_id(current->format) != ast_format_get_codec_id(to_match->format)) {
1228 continue;
1229 } else if (current->payload == to_match->payload) {
1230 return 0;
1231 }
1232 } else if (!current->asterisk_format && !to_match->asterisk_format) {
1233 if (current->rtp_code != to_match->rtp_code) {
1234 continue;
1235 } else if (to_match->rtp_code == AST_RTP_DTMF && current->sample_rate != to_match->sample_rate) {
1236 /* it is possible for multiple DTMF types to exist with different sample rates */
1237 continue;
1238 }
1239 } else {
1240 continue;
1241 }
1242
1243 return 1;
1244 }
1245
1246 return 0;
1247 }
1248
1249 int ast_rtp_payload_mapping_tx_is_present(struct ast_rtp_codecs *codecs, const struct ast_rtp_payload_type *to_match) {
1250 int ret = 0;
1251 if (codecs && to_match) {
1252 ast_rwlock_rdlock(&codecs->codecs_lock);
1253 ret = payload_mapping_tx_is_present(codecs, to_match);
1254 ast_rwlock_unlock(&codecs->codecs_lock);
1255 }
1256 return ret;
1257 }
1258
1259 /*!
1260 * \internal
1261 * \brief Copy the tx payload type mapping to the destination.
1262 * \since 14.0.0
1263 *
1264 * \param src The source codecs structure
1265 * \param dest The destination codecs structure that the values from src will be copied to
1266 * \param instance Optionally the instance that the dst codecs structure belongs to
1267 *
1268 * \note It is assumed that src is at least read locked before calling.
1269 * \note It is assumed that dest is write locked before calling.
1270 */
1271 static void rtp_codecs_payloads_copy_tx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1272 {
1273 int idx;
1274 struct ast_rtp_payload_type *type;
1275
1276 for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
1277 type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
1278 if (!type) {
1279 continue;
1280 }
1281
1282 ast_debug(2, "Copying tx payload mapping %d (%p) from %p to %p\n",
1283 idx, type, src, dest);
1284 ao2_ref(type, +1);
1285 if (idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx)) {
1286 ao2_t_cleanup(AST_VECTOR_GET(&dest->payload_mapping_tx, idx),
1287 "cleaning up tx mapping vector element about to be replaced");
1288 }
1289 if (AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, type)) {
1290 ao2_ref(type, -1);
1291 continue;
1292 }
1293
1294 if (instance && instance->engine && instance->engine->payload_set) {
1295 ao2_lock(instance);
1296 instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1297 ao2_unlock(instance);
1298 }
1299 }
1300 }
1301
1302 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1303 {
1304 int idx;
1305 struct ast_rtp_payload_type *type;
1306
1307 ast_rwlock_wrlock(&dest->codecs_lock);
1308
1309 /* Deadlock avoidance because of held write lock. */
1310 while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
1311 ast_rwlock_unlock(&dest->codecs_lock);
1312 sched_yield();
1313 ast_rwlock_wrlock(&dest->codecs_lock);
1314 }
1315
1316 /*
1317 * This represents a completely new mapping of what the remote party is
1318 * expecting for payloads, so we clear out the entire tx payload mapping
1319 * vector and replace it.
1320 */
1321 for (idx = 0; idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx); ++idx) {
1322 type = AST_VECTOR_GET(&dest->payload_mapping_tx, idx);
1323 ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
1324 AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, NULL);
1325 }
1326
1327 rtp_codecs_payloads_copy_rx(src, dest, instance);
1328 rtp_codecs_payloads_copy_tx(src, dest, instance);
1329 dest->framing = src->framing;
1330 ao2_replace(dest->preferred_format, src->preferred_format);
1331 dest->preferred_dtmf_rate = src->preferred_dtmf_rate;
1332 dest->preferred_dtmf_pt = src->preferred_dtmf_pt;
1333
1334 ast_rwlock_unlock(&src->codecs_lock);
1335 ast_rwlock_unlock(&dest->codecs_lock);
1336 }
1337
1338 void ast_rtp_codecs_payloads_xover(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1339 {
1340 int idx;
1341 struct ast_rtp_payload_type *type;
1342
1343 ast_rwlock_wrlock(&dest->codecs_lock);
1344 if (src != dest) {
1345 /* Deadlock avoidance because of held write lock. */
1346 while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
1347 ast_rwlock_unlock(&dest->codecs_lock);
1348 sched_yield();
1349 ast_rwlock_wrlock(&dest->codecs_lock);
1350 }
1351 }
1352
1353 /* Crossover copy payload type tx mapping to rx mapping. */
1354 for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
1355 type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
1356 if (!type) {
1357 continue;
1358 }
1359
1360 /* All tx mapping elements should have the primary flag set. */
1361 ast_assert(type->primary_mapping);
1362
1363 ast_debug(2, "Crossover copying tx to rx payload mapping %d (%p) from %p to %p\n",
1364 idx, type, src, dest);
1365 rtp_codecs_payload_replace_rx(dest, idx, type);
1366
1367 if (instance && instance->engine && instance->engine->payload_set) {
1368 ao2_lock(instance);
1369 instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1370 ao2_unlock(instance);
1371 }
1372 }
1373
1374 dest->framing = src->framing;
1375 ao2_replace(dest->preferred_format, src->preferred_format);
1376 dest->preferred_dtmf_rate = src->preferred_dtmf_rate;
1377 dest->preferred_dtmf_pt = src->preferred_dtmf_pt;
1378
1379 if (src != dest) {
1380 ast_rwlock_unlock(&src->codecs_lock);
1381 }
1382 ast_rwlock_unlock(&dest->codecs_lock);
1383 }
1384
1385 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
1386 {
1387 struct ast_rtp_payload_type *new_type;
1388
1389 if (payload < 0 || payload >= AST_RTP_MAX_PT || payload > AST_RTP_PT_LAST_STATIC) {
1390 return;
1391 }
1392
1393 ast_rwlock_rdlock(&static_RTP_PT_lock);
1394 new_type = ao2_bump(static_RTP_PT[payload]);
1395 ast_rwlock_unlock(&static_RTP_PT_lock);
1396 if (!new_type) {
1397 ast_debug(1, "Don't have a default tx payload type %d format for m type on %p\n",
1398 payload, codecs);
1399 return;
1400 }
1401
1402 ast_debug(1, "Setting tx payload type %d based on m type on %p\n",
1403 payload, codecs);
1404
1405 ast_rwlock_wrlock(&codecs->codecs_lock);
1406
1407 if (!payload_mapping_tx_is_present(codecs, new_type)) {
1408 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1409 ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload),
1410 "cleaning up replaced tx payload type");
1411 }
1412
1413 if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, new_type)) {
1414 ao2_ref(new_type, -1);
1415 } else if (instance && instance->engine && instance->engine->payload_set) {
1416 ao2_lock(instance);
1417 instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
1418 ao2_unlock(instance);
1419 }
1420 } else {
1421 ao2_ref(new_type, -1);
1422 }
1423
1424 ast_rwlock_unlock(&codecs->codecs_lock);
1425 }
1426
1427 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
1428 char *mimetype, char *mimesubtype,
1429 enum ast_rtp_options options,
1430 unsigned int sample_rate)
1431 {
1432 unsigned int idx;
1433 int found = 0;
1434
1435 if (pt < 0 || pt >= AST_RTP_MAX_PT) {
1436 return -1; /* bogus payload type */
1437 }
1438
1439 ast_rwlock_rdlock(&mime_types_lock);
1440 ast_rwlock_wrlock(&codecs->codecs_lock);
1441
1442 for (idx = 0; idx < mime_types_len; ++idx) {
1443 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[idx];
1444 struct ast_rtp_payload_type *new_type;
1445
1446 if (strcasecmp(mimesubtype, t->subtype)) {
1447 continue;
1448 }
1449
1450 if (strcasecmp(mimetype, t->type)) {
1451 continue;
1452 }
1453
1454 /* if both sample rates have been supplied, and they don't match,
1455 * then this not a match; if one has not been supplied, then the
1456 * rates are not compared */
1457 if (sample_rate && t->sample_rate &&
1458 (sample_rate != t->sample_rate)) {
1459 continue;
1460 }
1461
1462 found = 1;
1463
1464 new_type = ast_rtp_engine_alloc_payload_type();
1465 if (!new_type) {
1466 continue;
1467 }
1468
1469 new_type->asterisk_format = t->payload_type.asterisk_format;
1470 new_type->rtp_code = t->payload_type.rtp_code;
1471 new_type->payload = pt;
1472 new_type->primary_mapping = 1;
1473 new_type->sample_rate = sample_rate;
1474 if (t->payload_type.asterisk_format
1475 && ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL
1476 && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
1477 new_type->format = ast_format_g726_aal2;
1478 } else {
1479 new_type->format = t->payload_type.format;
1480 }
1481
1482 if (new_type->format) {
1483 /* SDP parsing automatically increases the reference count */
1484 new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
1485 }
1486
1487 if (!payload_mapping_tx_is_present(codecs, new_type)) {
1488 if (pt < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1489 ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, pt),
1490 "cleaning up replaced tx payload type");
1491 }
1492
1493 if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, pt, new_type)) {
1494 ao2_ref(new_type, -1);
1495 } else if (instance && instance->engine && instance->engine->payload_set) {
1496 ao2_lock(instance);
1497 instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
1498 ao2_unlock(instance);
1499 }
1500 } else {
1501 ao2_ref(new_type, -1);
1502 }
1503
1504 break;
1505 }
1506
1507 ast_rwlock_unlock(&codecs->codecs_lock);
1508 ast_rwlock_unlock(&mime_types_lock);
1509
1510 return (found ? 0 : -2);
1511 }
1512
1513 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)
1514 {
1515 return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
1516 }
1517
1518 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
1519 {
1520 struct ast_rtp_payload_type *type;
1521
1522 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1523 return;
1524 }
1525
1526 ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
1527
1528 ast_rwlock_wrlock(&codecs->codecs_lock);
1529
1530 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1531 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1532 /*
1533 * Remove the preferred format if we are unsetting its container.
1534 *
1535 * There can be empty slots in payload_mapping_tx corresponding to
1536 * dynamic payload types that haven't been seen before so we need
1537 * to check for NULL before attempting to use 'type' in the call to
1538 * ast_format_cmp.
1539 */
1540 if (type) {
1541 if (ast_format_cmp(type->format, codecs->preferred_format) == AST_FORMAT_CMP_EQUAL) {
1542 ao2_replace(codecs->preferred_format, NULL);
1543 }
1544 ao2_ref(type, -1);
1545 AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, NULL);
1546 }
1547 }
1548
1549 if (instance && instance->engine && instance->engine->payload_set) {
1550 ao2_lock(instance);
1551 instance->engine->payload_set(instance, payload, 0, NULL, 0);
1552 ao2_unlock(instance);
1553 }
1554
1555 ast_rwlock_unlock(&codecs->codecs_lock);
1556 }
1557
1558 enum ast_media_type ast_rtp_codecs_get_stream_type(struct ast_rtp_codecs *codecs)
1559 {
1560 enum ast_media_type stream_type = AST_MEDIA_TYPE_UNKNOWN;
1561 int payload;
1562 struct ast_rtp_payload_type *type;
1563
1564 ast_rwlock_rdlock(&codecs->codecs_lock);
1565 for (payload = 0; payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++payload) {
1566 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
1567 if (type && type->asterisk_format) {
1568 stream_type = ast_format_get_type(type->format);
1569 break;
1570 }
1571 }
1572 ast_rwlock_unlock(&codecs->codecs_lock);
1573
1574 return stream_type;
1575 }
1576
1577 struct ast_rtp_payload_type *ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
1578 {
1579 struct ast_rtp_payload_type *type = NULL;
1580
1581 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1582 return NULL;
1583 }
1584
1585 ast_rwlock_rdlock(&codecs->codecs_lock);
1586 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1587 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
1588 ao2_bump(type);
1589 }
1590 ast_rwlock_unlock(&codecs->codecs_lock);
1591
1592 if (!type && payload <= AST_RTP_PT_LAST_STATIC) {
1593 ast_rwlock_rdlock(&static_RTP_PT_lock);
1594 type = ao2_bump(static_RTP_PT[payload]);
1595 ast_rwlock_unlock(&static_RTP_PT_lock);
1596 }
1597
1598 return type;
1599 }
1600
1601 struct ast_format *ast_rtp_codecs_get_preferred_format(struct ast_rtp_codecs *codecs)
1602 {
1603 struct ast_format *format;
1604 ast_rwlock_rdlock(&codecs->codecs_lock);
1605 format = ao2_bump(codecs->preferred_format);
1606 ast_rwlock_unlock(&codecs->codecs_lock);
1607 return format;
1608 }
1609
1610 int ast_rtp_codecs_set_preferred_format(struct ast_rtp_codecs *codecs, struct ast_format *format)
1611 {
1612 ast_rwlock_wrlock(&codecs->codecs_lock);
1613 ao2_replace(codecs->preferred_format, format);
1614 ast_rwlock_unlock(&codecs->codecs_lock);
1615 return 0;
1616 }
1617
1618 int ast_rtp_codecs_get_preferred_dtmf_format_pt(struct ast_rtp_codecs *codecs)
1619 {
1620 int pt = -1;
1621 ast_rwlock_rdlock(&codecs->codecs_lock);
1622 pt = codecs->preferred_dtmf_pt;
1623 ast_rwlock_unlock(&codecs->codecs_lock);
1624 return pt;
1625 }
1626
1627 int ast_rtp_codecs_get_preferred_dtmf_format_rate(struct ast_rtp_codecs *codecs)
1628 {
1629 int rate = -1;
1630 ast_rwlock_rdlock(&codecs->codecs_lock);
1631 rate = codecs->preferred_dtmf_rate;
1632 ast_rwlock_unlock(&codecs->codecs_lock);
1633 return rate;
1634 }
1635
1636 int ast_rtp_codecs_set_preferred_dtmf_format(struct ast_rtp_codecs *codecs, int pt, int rate)
1637 {
1638 ast_rwlock_wrlock(&codecs->codecs_lock);
1639 codecs->preferred_dtmf_pt = pt;
1640 codecs->preferred_dtmf_rate = rate;
1641 ast_rwlock_unlock(&codecs->codecs_lock);
1642 return 0;
1643 }
1644
1645 int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int payload, struct ast_format *format)
1646 {
1647 struct ast_rtp_payload_type *type;
1648
1649 if (payload < 0 || payload >= AST_RTP_MAX_PT || !format) {
1650 return -1;
1651 }
1652
1653 type = ast_rtp_engine_alloc_payload_type();
1654 if (!type) {
1655 return -1;
1656 }
1657 ao2_ref(format, +1);
1658 type->format = format;
1659 type->asterisk_format = 1;
1660 type->payload = payload;
1661 type->primary_mapping = 1;
1662
1663 ast_rwlock_wrlock(&codecs->codecs_lock);
1664 if (!payload_mapping_tx_is_present(codecs, type)) {
1665 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1666 ao2_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload));
1667 }
1668 if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, type)) {
1669 ao2_ref(type, -1);
1670 }
1671 } else {
1672 ao2_ref(type, -1);
1673 }
1674 ast_rwlock_unlock(&codecs->codecs_lock);
1675
1676 return 0;
1677 }
1678
1679 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
1680 {
1681 struct ast_rtp_payload_type *type;
1682 struct ast_format *format = NULL;
1683
1684 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1685 return NULL;
1686 }
1687
1688 ast_rwlock_rdlock(&codecs->codecs_lock);
1689 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1690 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1691 if (type && type->asterisk_format) {
1692 format = ao2_bump(type->format);
1693 }
1694 }
1695 ast_rwlock_unlock(&codecs->codecs_lock);
1696
1697 return format;
1698 }
1699
1700 void ast_rtp_codecs_set_framing(struct ast_rtp_codecs *codecs, unsigned int framing)
1701 {
1702 if (!framing) {
1703 return;
1704 }
1705
1706 ast_rwlock_wrlock(&codecs->codecs_lock);
1707 codecs->framing = framing;
1708 ast_rwlock_unlock(&codecs->codecs_lock);
1709 }
1710
1711 unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
1712 {
1713 unsigned int framing;
1714
1715 ast_rwlock_rdlock(&codecs->codecs_lock);
1716 framing = codecs->framing;
1717 ast_rwlock_unlock(&codecs->codecs_lock);
1718
1719 return framing;
1720 }
1721
1722 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
1723 {
1724 int idx;
1725
1726 ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN);
1727 *nonastformats = 0;
1728
1729 ast_rwlock_rdlock(&codecs->codecs_lock);
1730
1731 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1732 struct ast_rtp_payload_type *type;
1733
1734 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1735 if (!type) {
1736 continue;
1737 }
1738
1739 if (type->asterisk_format) {
1740 ast_format_cap_append(astformats, type->format, 0);
1741 } else {
1742 *nonastformats |= type->rtp_code;
1743 }
1744 }
1745 if (codecs->framing) {
1746 ast_format_cap_set_framing(astformats, codecs->framing);
1747 }
1748
1749 ast_rwlock_unlock(&codecs->codecs_lock);
1750 }
1751
1752 /*!
1753 * \internal
1754 * \brief Find the static payload type mapping for the format.
1755 * \since 14.0.0
1756 *
1757 * \param asterisk_format Non-zero if the given Asterisk format is present
1758 * \param format Asterisk format to look for
1759 * \param code The non-Asterisk format code to look for
1760 *
1761 * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
1762 *
1763 * \return Numerical payload type
1764 * \retval -1 if not found.
1765 */
1766 static int find_static_payload_type(int asterisk_format, const struct ast_format *format, int code)
1767 {
1768 int idx;
1769 int payload = -1;
1770
1771 if (!asterisk_format) {
1772 for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
1773 if (static_RTP_PT[idx]
1774 && !static_RTP_PT[idx]->asterisk_format
1775 && static_RTP_PT[idx]->rtp_code == code) {
1776 payload = idx;
1777 break;
1778 }
1779 }
1780 } else if (format) {
1781 for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
1782 if (static_RTP_PT[idx]
1783 && static_RTP_PT[idx]->asterisk_format
1784 && ast_format_cmp(format, static_RTP_PT[idx]->format)
1785 != AST_FORMAT_CMP_NOT_EQUAL) {
1786 payload = idx;
1787 break;
1788 }
1789 }
1790 }
1791
1792 return payload;
1793 }
1794
1795 /*!
1796 * \internal
1797 * \brief Find the first unused payload type in a given range
1798 *
1799 * \param codecs The codec structure to look in
1800 * \param start Starting index
1801 * \param end Ending index
1802 * \param ignore Skip these payloads types
1803 *
1804 * \note The static_RTP_PT_lock object must be locked before calling
1805 *
1806 * \return Numerical payload type
1807 * \retval -1 if not found.
1808 */
1809 static int find_unused_payload_in_range(const struct ast_rtp_codecs *codecs,
1810 int start, int end, struct ast_rtp_payload_type *ignore[])
1811 {
1812 int x;
1813
1814 for (x = start; x < end; ++x) {
1815 struct ast_rtp_payload_type *type;
1816
1817 if (ignore[x]) {
1818 continue;
1819 } else if (!codecs || x >= AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1820 return x;
1821 }
1822
1823 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, x);
1824 if (!type) {
1825 return x;
1826 }
1827 }
1828 return -1;
1829 }
1830
1831 /*!
1832 * \internal
1833 * \brief Find an unused payload type
1834 *
1835 * \param codecs Codecs structure to look in
1836 *
1837 * \note Both static_RTP_PT_lock and codecs (if given) must be at least
1838 * read locked before calling.
1839 *
1840 * \return Numerical payload type
1841 * \retval -1 if not found.
1842 */
1843 static int find_unused_payload(const struct ast_rtp_codecs *codecs)
1844 {
1845 int res;
1846
1847 /* find next available dynamic payload slot */
1848 res = find_unused_payload_in_range(
1849 codecs, AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT, static_RTP_PT);
1850 if (res != -1) {
1851 return res;
1852 }
1853
1854 if (ast_option_rtpusedynamic) {
1855 /*
1856 * We're using default values for some dynamic types. So if an unused
1857 * slot was not found try again, but this time ignore the default
1858 * values declared for dynamic types (except for 101 and 121) .
1859 */
1860 static struct ast_rtp_payload_type *ignore[AST_RTP_MAX_PT] = {0};
1861
1862 ignore[101] = static_RTP_PT[101];
1863 ignore[121] = static_RTP_PT[121];
1864
1865 res = find_unused_payload_in_range(
1866 codecs, AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT, ignore);
1867 if (res != -1) {
1868 return res;
1869 }
1870 }
1871
1872 /* http://www.iana.org/assignments/rtp-parameters
1873 * RFC 3551, Section 3: "[...] applications which need to define more
1874 * than 32 dynamic payload types MAY bind codes below 96, in which case
1875 * it is RECOMMENDED that unassigned payload type numbers be used
1876 * first". Updated by RFC 5761, Section 4: "[...] values in the range
1877 * 64-95 MUST NOT be used [to avoid conflicts with RTCP]". Summaries:
1878 * https://tools.ietf.org/html/draft-roach-mmusic-unified-plan#section-3.2.1.2
1879 * https://tools.ietf.org/html/draft-wu-avtcore-dynamic-pt-usage#section-3
1880 */
1881 res = find_unused_payload_in_range(
1882 codecs, MAX(ast_option_rtpptdynamic, AST_RTP_PT_LAST_STATIC + 1),
1883 AST_RTP_PT_LAST_REASSIGN, static_RTP_PT);
1884 if (res != -1) {
1885 return res;
1886 }
1887
1888 /* Yet, reusing mappings below AST_RTP_PT_LAST_STATIC (35) is not supported
1889 * in Asterisk because when Compact Headers are activated, no rtpmap is
1890 * send for those below 35. If you want to use 35 and below
1891 * A) do not use Compact Headers,
1892 * B) remove that code in res_pjsip, or
1893 * C) add a flag that this RTP Payload Type got reassigned dynamically
1894 * and requires a rtpmap even with Compact Headers enabled.
1895 */
1896 res = find_unused_payload_in_range(
1897 codecs, MAX(ast_option_rtpptdynamic, 20),
1898 AST_RTP_PT_LAST_STATIC + 1, static_RTP_PT);
1899 if (res != -1) {
1900 return res;
1901 }
1902
1903 return find_unused_payload_in_range(
1904 codecs, MAX(ast_option_rtpptdynamic, 0),
1905 20, static_RTP_PT);
1906 }
1907
1908 /*!
1909 * \internal
1910 * \brief Find the oldest non-primary dynamic rx payload type.
1911 * \since 14.0.0
1912 *
1913 * \param codecs Codecs structure to look in
1914 *
1915 * \note It is assumed that codecs is at least read locked before calling.
1916 *
1917 * \return Numerical payload type
1918 * \retval -1 if not found.
1919 */
1920 static int rtp_codecs_find_non_primary_dynamic_rx(struct ast_rtp_codecs *codecs)
1921 {
1922 struct ast_rtp_payload_type *type;
1923 struct timeval oldest;
1924 int idx;
1925 int payload = -1;
1926
1927 idx = AST_RTP_PT_FIRST_DYNAMIC;
1928 for (; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1929 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1930 if (type
1931 && !type->primary_mapping
1932 && (payload == -1
1933 || ast_tvdiff_ms(type->when_retired, oldest) < 0)) {
1934 oldest = type->when_retired;
1935 payload = idx;
1936 }
1937 }
1938 return payload;
1939 }
1940
1941 /*!
1942 * \internal
1943 * \brief Assign a payload type for the rx mapping.
1944 * \since 14.0.0
1945 *
1946 * \param codecs Codecs structure to look in
1947 * \param asterisk_format Non-zero if the given Asterisk format is present
1948 * \param format Asterisk format to look for
1949 * \param code The format to look for
1950 * \param explicit Require the provided code to be explicitly used
1951 *
1952 * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
1953 *
1954 * \return Numerical payload type
1955 * \retval -1 if could not assign.
1956 */
1957 static int rtp_codecs_assign_payload_code_rx(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code, int explicit, unsigned int sample_rate)
1958 {
1959 int payload = code, i;
1960 struct ast_rtp_payload_type *new_type;
1961 static struct ast_rtp_payload_type *ignore[AST_RTP_MAX_PT] = {0};
1962
1963 if (!explicit) {
1964 payload = find_static_payload_type(asterisk_format, format, code);
1965
1966 if (payload < 0 && (!asterisk_format || !ast_option_rtpusedynamic)) {
1967 return payload;
1968 }
1969 }
1970
1971 new_type = rtp_payload_type_alloc(format, payload, code, 1, sample_rate);
1972 if (!new_type) {
1973 return -1;
1974 }
1975
1976 ast_rwlock_wrlock(&codecs->codecs_lock);
1977
1978 /* Go through the existing mapping to create an ignore list. */
1979 for (i = 0; i < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); i++) {
1980 if (AST_VECTOR_GET(&codecs->payload_mapping_rx, i)) {
1981 ignore[i] = static_RTP_PT[i];
1982 }
1983 }
1984
1985
1986 if (payload > -1 && (payload < AST_RTP_PT_FIRST_DYNAMIC
1987 || AST_VECTOR_SIZE(&codecs->payload_mapping_rx) <= payload
1988 || !AST_VECTOR_GET(&codecs->payload_mapping_rx, payload))) {
1989
1990 /*
1991 * The payload type is a static assignment
1992 * or our default dynamic position is available.
1993 */
1994 rtp_codecs_payload_replace_rx(codecs, payload, new_type);
1995 } else if (payload > -1 && !explicit
1996 /* We can either call this with the full list or the current rx list. The former
1997 * (static_RTP_PT) results in payload types skipping statically 'used' slots so you
1998 * get 101, 113...
1999 * With the latter (the built ignore list) you get what's expected 101, 102, 103 under
2000 * most circumstances, but this results in static types being replaced. Probably fine
2001 * because we preclude the current list.
2002 */
2003 && (-1 < (payload = find_unused_payload_in_range(codecs, payload, AST_RTP_MAX_PT, ignore)))) {
2004 /*
2005 * Our dynamic position is currently in use.
2006 * Try for the numerically next free one before trying
2007 * across the full range. This keeps the payload id's
2008 * in the best numerical order we can through the free
2009 * types.
2010 */
2011 new_type->payload = payload;
2012 /*
2013 * In this case, consider this the primary mapping for
2014 * the payload type so don't clear it. Set not replace.
2015 */
2016 rtp_codecs_payload_set_rx(codecs, payload, new_type, 0);
2017 } else if (!explicit && (-1 < (payload = find_unused_payload(codecs))
2018 || -1 < (payload = rtp_codecs_find_non_primary_dynamic_rx(codecs)))) {
2019 /*
2020 * We found the first available empty dynamic position
2021 * or we found a mapping that should no longer be
2022 * actively used.
2023 */
2024 new_type->payload = payload;
2025 rtp_codecs_payload_replace_rx(codecs, payload, new_type);
2026 } else if (explicit) {
2027 /*
2028 * They explicitly requested this payload number be used but it couldn't be
2029 */
2030 payload = -1;
2031 } else {
2032 /*
2033 * There are no empty or non-primary dynamic positions
2034 * left. Sadness.
2035 *
2036 * I don't think this is really possible.
2037 */
2038 ast_log(LOG_WARNING, "No dynamic RTP payload type values available "
2039 "for %s - %d!\n", format ? ast_format_get_name(format) : "", code);
2040 }
2041 ast_rwlock_unlock(&codecs->codecs_lock);
2042
2043 ao2_ref(new_type, -1);
2044
2045 return payload;
2046 }
2047
2048 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code)
2049 {
2050 return ast_rtp_codecs_payload_code_sample_rate(codecs, asterisk_format, format, code, 0);
2051 }
2052
2053 int ast_rtp_codecs_payload_code_sample_rate(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code, unsigned int sample_rate)
2054 {
2055 struct ast_rtp_payload_type *type;
2056 int idx;
2057 int payload = -1;
2058
2059 ast_rwlock_rdlock(&static_RTP_PT_lock);
2060 if (!asterisk_format) {
2061 ast_rwlock_rdlock(&codecs->codecs_lock);
2062 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
2063 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
2064 if (!type) {
2065 continue;
2066 }
2067
2068 if (!type->asterisk_format
2069 && type->primary_mapping
2070 && type->rtp_code == code
2071 && (sample_rate == 0 || type->sample_rate == sample_rate)) {
2072 payload = idx;
2073 break;
2074 }
2075 }
2076 ast_rwlock_unlock(&codecs->codecs_lock);
2077 } else if (format) {
2078 ast_rwlock_rdlock(&codecs->codecs_lock);
2079 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
2080 type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
2081 if (!type) {
2082 continue;
2083 }
2084
2085 if (type->asterisk_format
2086 && type->primary_mapping
2087 && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
2088 payload = idx;
2089 break;
2090 }
2091 }
2092 ast_rwlock_unlock(&codecs->codecs_lock);
2093 }
2094
2095 if (payload < 0) {
2096 payload = rtp_codecs_assign_payload_code_rx(codecs, asterisk_format, format,
2097 code, 0, sample_rate);
2098 }
2099 ast_rwlock_unlock(&static_RTP_PT_lock);
2100
2101 return payload;
2102 }
2103
2104 int ast_rtp_codecs_payload_set_rx(struct ast_rtp_codecs *codecs, int code, struct ast_format *format)
2105 {
2106 return rtp_codecs_assign_payload_code_rx(codecs, 1, format, code, 1, 0);
2107 }
2108
2109 int ast_rtp_codecs_payload_set_rx_sample_rate(struct ast_rtp_codecs *codecs, int code, struct ast_format *format, unsigned int sample_rate)
2110 {
2111 return rtp_codecs_assign_payload_code_rx(codecs, 1, format, code, 0, sample_rate);
2112 }
2113
2114 int ast_rtp_codecs_payload_code_tx_sample_rate(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code, unsigned int sample_rate)
2115 {
2116 struct ast_rtp_payload_type *type;
2117 int idx;
2118 int payload = -1;
2119
2120 if (!asterisk_format) {
2121 ast_rwlock_rdlock(&codecs->codecs_lock);
2122 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
2123 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
2124 if (!type) {
2125 continue;
2126 }
2127
2128 if (!type->asterisk_format
2129 && type->rtp_code == code
2130 /* Multiple DTMF types share an rtp code but have different sample rates. To ensure we have the right
2131 type we therefore need the sample rate as well as the format and code. Other types have a fixed
2132 sample rate so this is not needed. For those pass in a sample rate of 0 or use ast_rtp_codecs_payload_code_tx. */
2133 && (sample_rate == 0 || type->sample_rate == sample_rate)) {
2134 payload = idx;
2135 break;
2136 }
2137 }
2138 ast_rwlock_unlock(&codecs->codecs_lock);
2139 } else if (format) {
2140 ast_rwlock_rdlock(&codecs->codecs_lock);
2141 for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
2142 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
2143 if (!type) {
2144 continue;
2145 }
2146
2147 if (type->asterisk_format
2148 && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
2149 payload = idx;
2150 break;
2151 }
2152 }
2153 ast_rwlock_unlock(&codecs->codecs_lock);
2154 }
2155
2156 if (payload < 0) {
2157 ast_rwlock_rdlock(&static_RTP_PT_lock);
2158 payload = find_static_payload_type(asterisk_format, format, code);
2159 ast_rwlock_unlock(&static_RTP_PT_lock);
2160
2161 /*
2162 * Comfort noise is NOT used as an SDP negotiated format within Asterisk;
2163 * instead, it is used when it is not negotiated. This special case allows
2164 * its payload to be returned when not negotiated, allowing keep alive to
2165 * function as expected.
2166 */
2167 if (payload == 13 && code == AST_RTP_CN) {
2168 return payload;
2169 }
2170
2171 ast_rwlock_rdlock(&codecs->codecs_lock);
2172 if (payload >= 0 && payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)){
2173 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
2174 if (!type || (sample_rate != 0 && type->sample_rate != sample_rate)) {
2175 /* Don't use the type if we can't find it or it doesn't match the supplied sample_rate */
2176 payload = -1;
2177 }
2178 }
2179 ast_rwlock_unlock(&codecs->codecs_lock);
2180 }
2181
2182 return payload;
2183 }
2184
2185 int ast_rtp_codecs_payload_code_tx(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
2186 {
2187 return ast_rtp_codecs_payload_code_tx_sample_rate(codecs, asterisk_format, format, code, 0);
2188 }
2189
2190 int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int payload)
2191 {
2192 struct ast_rtp_payload_type *type;
2193 int res = -1;
2194
2195 ast_rwlock_rdlock(&codecs->codecs_lock);
2196 if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
2197 type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
2198 if (type) {
2199 res = payload;
2200 }
2201 }
2202 ast_rwlock_unlock(&codecs->codecs_lock);
2203
2204 return res;
2205 }
2206
2207 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format,
2208 const struct ast_format *format, int code, enum ast_rtp_options options)
2209 {
2210 int i;
2211 const char *res = "";
2212
2213 ast_rwlock_rdlock(&mime_types_lock);
2214 for (i = 0; i < mime_types_len; i++) {
2215 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
2216 (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
2217 if ((ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) &&
2218 (options & AST_RTP_OPT_G726_NONSTANDARD)) {
2219 res = "G726-32";
2220 break;
2221 } else {
2222 res = ast_rtp_mime_types[i].subtype;
2223 break;
2224 }
2225 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
2226 ast_rtp_mime_types[i].payload_type.rtp_code == code) {
2227
2228 res = ast_rtp_mime_types[i].subtype;
2229 break;
2230 }
2231 }
2232 ast_rwlock_unlock(&mime_types_lock);
2233
2234 return res;
2235 }
2236
2237 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format,
2238 const struct ast_format *format, int code)
2239 {
2240 unsigned int i;
2241 unsigned int res = 0;
2242
2243 ast_rwlock_rdlock(&mime_types_lock);
2244 for (i = 0; i < mime_types_len; ++i) {
2245 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
2246 (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
2247 res = ast_rtp_mime_types[i].sample_rate;
2248 break;
2249 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
2250 ast_rtp_mime_types[i].payload_type.rtp_code == code) {
2251 res = ast_rtp_mime_types[i].sample_rate;
2252 break;
2253 }
2254 }
2255 ast_rwlock_unlock(&mime_types_lock);
2256
2257 return res;
2258 }
2259
2260 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)
2261 {
2262 int found = 0;
2263 const char *name;
2264 if (!buf) {
2265 return NULL;
2266 }
2267
2268
2269 if (asterisk_format) {
2270 int x;
2271 struct ast_format *tmp_fmt;
2272 for (x = 0; x < ast_format_cap_count(ast_format_capability); x++) {
2273 tmp_fmt = ast_format_cap_get_format(ast_format_capability, x);
2274 name = ast_rtp_lookup_mime_subtype2(asterisk_format, tmp_fmt, 0, options);
2275 ao2_ref(tmp_fmt, -1);
2276 ast_str_append(&buf, 0, "%s|", name);
2277 found = 1;
2278 }
2279 } else {
2280 int x;
2281 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
2282 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
2283 if (rtp_capability & x) {
2284 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
2285 ast_str_append(&buf, 0, "%s|", name);
2286 found = 1;
2287 }
2288 }
2289 }
2290
2291 ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
2292
2293 return ast_str_buffer(buf);
2294 }
2295
2296 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
2297 {
2298 int res;
2299
2300 if (instance->engine->dtmf_begin) {
2301 ao2_lock(instance);
2302 res = instance->engine->dtmf_begin(instance, digit);
2303 ao2_unlock(instance);
2304 } else {
2305 res = -1;
2306 }
2307 return res;
2308 }
2309
2310 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
2311 {
2312 int res;
2313
2314 if (instance->engine->dtmf_end) {
2315 ao2_lock(instance);
2316 res = instance->engine->dtmf_end(instance, digit);
2317 ao2_unlock(instance);
2318 } else {
2319 res = -1;
2320 }
2321 return res;
2322 }
2323
2324 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
2325 {
2326 int res;
2327
2328 if (instance->engine->dtmf_end_with_duration) {
2329 ao2_lock(instance);
2330 res = instance->engine->dtmf_end_with_duration(instance, digit, duration);
2331 ao2_unlock(instance);
2332 } else {
2333 res = -1;
2334 }
2335 return res;
2336 }
2337
2338 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
2339 {
2340 int res;
2341
2342 if (instance->engine->dtmf_mode_set) {
2343 ao2_lock(instance);
2344 res = instance->engine->dtmf_mode_set(instance, dtmf_mode);
2345 ao2_unlock(instance);
2346 } else {
2347 res = -1;
2348 }
2349 return res;
2350 }
2351
2352 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
2353 {
2354 int res;
2355
2356 if (instance->engine->dtmf_mode_get) {
2357 ao2_lock(instance);
2358 res = instance->engine->dtmf_mode_get(instance);
2359 ao2_unlock(instance);
2360 } else {
2361 res = 0;
2362 }
2363 return res;
2364 }
2365
2366 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
2367 {
2368 if (instance->engine->update_source) {
2369 ao2_lock(instance);
2370 instance->engine->update_source(instance);
2371 ao2_unlock(instance);
2372 }
2373 }
2374
2375 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
2376 {
2377 if (instance->engine->change_source) {
2378 ao2_lock(instance);
2379 instance->engine->change_source(instance);
2380 ao2_unlock(instance);
2381 }
2382 }
2383
2384 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
2385 {
2386 int res;
2387
2388 if (instance->engine->qos) {
2389 ao2_lock(instance);
2390 res = instance->engine->qos(instance, tos, cos, desc);
2391 ao2_unlock(instance);
2392 } else {
2393 res = -1;
2394 }
2395 return res;
2396 }
2397
2398 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
2399 {
2400 if (instance->engine->stop) {
2401 ao2_lock(instance);
2402 instance->engine->stop(instance);
2403 ao2_unlock(instance);
2404 }
2405 }
2406
2407 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
2408 {
2409 int res;
2410
2411 if (instance->engine->fd) {
2412 ao2_lock(instance);
2413 res = instance->engine->fd(instance, rtcp);
2414 ao2_unlock(instance);
2415 } else {
2416 res = -1;
2417 }
2418 return res;
2419 }
2420
2421 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
2422 {
2423 struct ast_rtp_glue *glue = NULL;
2424
2425 AST_RWLIST_RDLOCK(&glues);
2426
2427 AST_RWLIST_TRAVERSE(&glues, glue, entry) {
2428 if (!strcasecmp(glue->type, type)) {
2429 break;
2430 }
2431 }
2432
2433 AST_RWLIST_UNLOCK(&glues);
2434
2435 return glue;
2436 }
2437
2438 /*!
2439 * \brief Conditionally unref an rtp instance
2440 */
2441 static void unref_instance_cond(struct ast_rtp_instance **instance)
2442 {
2443 if (*instance) {
2444 ao2_ref(*instance, -1);
2445 *instance = NULL;
2446 }
2447 }
2448
2449 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
2450 {
2451 struct ast_rtp_instance *bridged;
2452
2453 ao2_lock(instance);
2454 bridged = instance->bridged;
2455 ao2_unlock(instance);
2456 return bridged;
2457 }
2458
2459 void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
2460 {
2461 ao2_lock(instance);
2462 instance->bridged = bridged;
2463 ao2_unlock(instance);
2464 }
2465
2466 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
2467 {
2468 struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
2469 *vinstance_dst = NULL, *vinstance_src = NULL,
2470 *tinstance_dst = NULL, *tinstance_src = NULL;
2471 struct ast_rtp_glue *glue_dst, *glue_src;
2472 enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
2473 enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
2474 struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2475 struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2476
2477 /* Lock both channels so we can look for the glue that binds them together */
2478 ast_channel_lock_both(c_dst, c_src);
2479
2480 if (!cap_src || !cap_dst) {
2481 goto done;
2482 }
2483
2484 /* Grab glue that binds each channel to something using the RTP engine */
2485 if (!(glue_dst = ast_rtp_instance_get_glue(ast_channel_tech(c_dst)->type)) || !(glue_src = ast_rtp_instance_get_glue(ast_channel_tech(c_src)->type))) {
2486 ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
2487 goto done;
2488 }
2489
2490 audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
2491 video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
2492
2493 audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
2494 video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
2495
2496 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
2497 if (video_glue_dst_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE)) {
2498 audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
2499 }
2500 if (video_glue_src_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE)) {
2501 audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
2502 }
2503 if (audio_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_dst_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_dst->get_codec) {
2504 glue_dst->get_codec(c_dst, cap_dst);
2505 }
2506 if (audio_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_src_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_src->get_codec) {
2507 glue_src->get_codec(c_src, cap_src);
2508 }
2509
2510 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
2511 if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
2512 goto done;
2513 }
2514
2515 /* Make sure we have matching codecs */
2516 if (!ast_format_cap_iscompatible(cap_dst, cap_src)) {
2517 goto done;
2518 }
2519
2520 ast_rtp_codecs_payloads_xover(&instance_src->codecs, &instance_dst->codecs, instance_dst);
2521
2522 if (vinstance_dst && vinstance_src) {
2523 ast_rtp_codecs_payloads_xover(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
2524 }
2525 if (tinstance_dst && tinstance_src) {
2526 ast_rtp_codecs_payloads_xover(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
2527 }
2528
2529 if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
2530 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
2531 ast_channel_name(c_dst), ast_channel_name(c_src));
2532 } else {
2533 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
2534 ast_channel_name(c_dst), ast_channel_name(c_src));
2535 }
2536
2537 done:
2538 ast_channel_unlock(c_dst);
2539 ast_channel_unlock(c_src);
2540
2541 ao2_cleanup(cap_dst);
2542 ao2_cleanup(cap_src);
2543
2544 unref_instance_cond(&instance_dst);
2545 unref_instance_cond(&instance_src);
2546 unref_instance_cond(&vinstance_dst);
2547 unref_instance_cond(&vinstance_src);
2548 unref_instance_cond(&tinstance_dst);
2549 unref_instance_cond(&tinstance_src);
2550 }
2551
2552 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
2553 {
2554 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
2555 *vinstance0 = NULL, *vinstance1 = NULL,
2556 *tinstance0 = NULL, *tinstance1 = NULL;
2557 struct ast_rtp_glue *glue0, *glue1;
2558 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
2559 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
2560 struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2561 struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2562
2563 /* If there is no second channel just immediately bail out, we are of no use in that scenario */
2564 if (!c1 || !cap1 || !cap0) {
2565 ao2_cleanup(cap0);
2566 ao2_cleanup(cap1);
2567 return -1;
2568 }
2569
2570 /* Lock both channels so we can look for the glue that binds them together */
2571 ast_channel_lock_both(c0, c1);
2572
2573 /* Grab glue that binds each channel to something using the RTP engine */
2574 if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
2575 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
2576 goto done;
2577 }
2578
2579 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
2580 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
2581
2582 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
2583 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
2584
2585 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
2586 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)) {
2587 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
2588 }
2589 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)) {
2590 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
2591 }
2592 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) {
2593 glue0->get_codec(c0, cap0);
2594 }
2595 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) {
2596 glue1->get_codec(c1, cap1);
2597 }
2598
2599 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
2600 if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
2601 goto done;
2602 }
2603
2604 /* Make sure we have matching codecs */
2605 if (!ast_format_cap_iscompatible(cap0, cap1)) {
2606 goto done;
2607 }
2608
2609 /* Bridge media early */
2610 if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
2611 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
2612 }
2613
2614 done:
2615 ast_channel_unlock(c0);
2616 ast_channel_unlock(c1);
2617
2618 ao2_cleanup(cap0);
2619 ao2_cleanup(cap1);
2620
2621 unref_instance_cond(&instance0);
2622 unref_instance_cond(&instance1);
2623 unref_instance_cond(&vinstance0);
2624 unref_instance_cond(&vinstance1);
2625 unref_instance_cond(&tinstance0);
2626 unref_instance_cond(&tinstance1);
2627
2628 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
2629
2630 return 0;
2631 }
2632
2633 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
2634 {
2635 int res;
2636
2637 if (instance->engine->red_init) {
2638 ao2_lock(instance);
2639 res = instance->engine->red_init(instance, buffer_time, payloads, generations);
2640 ao2_unlock(instance);
2641 } else {
2642 res = -1;
2643 }
2644 return res;
2645 }
2646
2647 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
2648 {
2649 int res;
2650
2651 if (instance->engine->red_buffer) {
2652 ao2_lock(instance);
2653 res = instance->engine->red_buffer(instance, frame);
2654 ao2_unlock(instance);
2655 } else {
2656 res = -1;
2657 }
2658 return res;
2659 }
2660
2661 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
2662 {
2663 int res;
2664
2665 if (!instance || !instance->engine || !stats) {
2666 return -1;
2667 }
2668
2669 if (instance->engine->get_stat) {
2670 ao2_lock(instance);
2671 res = instance->engine->get_stat(instance, stats, stat);
2672 ao2_unlock(instance);
2673 } else {
2674 res = -1;
2675 }
2676 return res;
2677 }
2678
2679 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
2680 {
2681 struct ast_rtp_instance_stats stats = { 0, };
2682 enum ast_rtp_instance_stat stat;
2683
2684 /* Determine what statistics we will need to retrieve based on field passed in */
2685 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
2686 stat = AST_RTP_INSTANCE_STAT_ALL;
2687 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
2688 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
2689 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
2690 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
2691 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
2692 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
2693 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_MES) {
2694 stat = AST_RTP_INSTANCE_STAT_COMBINED_MES;
2695 } else {
2696 return NULL;
2697 }
2698
2699 /* Attempt to actually retrieve the statistics we need to generate the quality string */
2700 if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
2701 return NULL;
2702 }
2703
2704 /* Now actually fill the buffer with the good information */
2705 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
2706 snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;"
2707 "txjitter=%f;txcount=%u;rlp=%u;rtt=%f;rxmes=%f;txmes=%f",
2708 stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter,
2709 stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt,
2710 stats.rxmes, stats.txmes);
2711 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
2712 snprintf(buf, size, "minrxjitter=%010.6f;maxrxjitter=%010.6f;avgrxjitter=%010.6f;stdevrxjitter=%010.6f;mintxjitter=%010.6f;maxtxjitter=%010.6f;avgtxjitter=%010.6f;stdevtxjitter=%010.6f;",
2713 stats.local_minjitter, stats.local_maxjitter, stats.local_normdevjitter, stats.local_stdevjitter, stats.remote_minjitter, stats.remote_maxjitter, stats.remote_normdevjitter, stats.remote_stdevjitter);
2714 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
2715 snprintf(buf, size, " minrxlost=%010.6f; maxrxlost=%010.6f; avgrxlost=%010.6f; stdevrxlost=%010.6f; mintxlost=%010.6f; maxtxlost=%010.6f; avgtxlost=%010.6f; stdevtxlost=%010.6f;",
2716 stats.local_minrxploss, stats.local_maxrxploss, stats.local_normdevrxploss, stats.local_stdevrxploss, stats.remote_minrxploss, stats.remote_maxrxploss, stats.remote_normdevrxploss, stats.remote_stdevrxploss);
2717 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
2718 snprintf(buf, size, " minrtt=%010.6f; maxrtt=%010.6f; avgrtt=%010.6f; stdevrtt=%010.6f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
2719 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_MES) {
2720 snprintf(buf, size, " minrxmes=%010.6f; maxrxmes=%010.6f; avgrxmes=%010.6f; stdevrxmes=%010.6f; mintxmes=%010.6f; maxtxmes=%010.6f; avgtxmes=%010.6f; stdevtxmes=%010.6f;",
2721 stats.local_minmes, stats.local_maxmes,
2722 stats.local_normdevmes, stats.local_stdevmes,
2723 stats.remote_minmes, stats.remote_maxmes,
2724 stats.remote_normdevmes, stats.remote_stdevmes);
2725 }
2726
2727 return buf;
2728 }
2729
2730 #define SET_STATS_VAR_HELPER(var_prefix, field) \
2731 ({ \
2732 value = ast_rtp_instance_get_quality(instance, field, quality_buf, sizeof(quality_buf)); \
2733 if (value) { \
2734 if (!chanvars || ast_strlen_zero(ast_var_find(chanvars, var_prefix))) { \
2735 pbx_builtin_setvar_helper(chan, var_prefix, value); \
2736 chanchanges++; \
2737 } \
2738 if (bridge) { \
2739 if (!bridgevars || ast_strlen_zero(ast_var_find(bridgevars, var_prefix "BRIDGED"))) { \
2740 pbx_builtin_setvar_helper(bridge, var_prefix "BRIDGED", value); \
2741 bridgechanges++; \
2742 } \
2743 } \
2744 } \
2745 })
2746
2747 /*!
2748 * \internal
2749 *
2750 * \warning Absolutely _NO_ channel locks should be held before calling this function.
2751 * If the channel is in a bridge, ast_rtp_instance_set_stats_vars() will
2752 * attempt to lock the bridge peer as well as this channel. This can cause
2753 * a lock inversion if we already have this channel locked and another
2754 * thread tries to set bridge variables on the peer because it will have
2755 * locked the peer first, then this channel. For this reason, we must
2756 * NOT have the channel locked when we call ast_rtp_instance_set_stats_vars().
2757 */
2758 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
2759 {
2760 char quality_buf[AST_MAX_USER_FIELD];
2761 char *value;
2762 struct ast_channel *bridge;
2763 struct varshead *chanvars = ast_channel_varshead(chan);
2764 struct varshead *bridgevars = NULL;
2765 int chanchanges = 0;
2766 int bridgechanges = 0;
2767
2768 bridge = ast_channel_bridge_peer(chan);
2769 if (bridge) {
2770 bridgevars = ast_channel_varshead(bridge);
2771 ast_channel_lock_both(chan, bridge);
2772 ast_channel_stage_snapshot(bridge);
2773 } else {
2774 ast_channel_lock(chan);
2775 }
2776 ast_channel_stage_snapshot(chan);
2777
2778 SET_STATS_VAR_HELPER("RTPAUDIOQOS", AST_RTP_INSTANCE_STAT_FIELD_QUALITY);
2779
2780 SET_STATS_VAR_HELPER("RTPAUDIOQOSJITTER", AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER);
2781
2782 SET_STATS_VAR_HELPER("RTPAUDIOQOSLOSS", AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS);
2783
2784 SET_STATS_VAR_HELPER("RTPAUDIOQOSRTT", AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT);
2785
2786 SET_STATS_VAR_HELPER("RTPAUDIOQOSMES", AST_RTP_INSTANCE_STAT_FIELD_QUALITY_MES);
2787
2788 if (chanchanges) {
2789 ast_channel_stage_snapshot_done(chan);
2790 } else {
2791 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE);
2792 }
2793 ast_channel_unlock(chan);
2794 if (bridge) {
2795 if (bridgechanges) {
2796 ast_channel_stage_snapshot_done(bridge);
2797 } else {
2798 ast_clear_flag(ast_channel_flags(bridge), AST_FLAG_SNAPSHOT_STAGE);
2799 }
2800 ast_channel_unlock(bridge);
2801 ast_channel_unref(bridge);
2802 }
2803 }
2804
2805 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
2806 {
2807 int res;
2808
2809 if (instance->engine->set_read_format) {
2810 ao2_lock(instance);
2811 res = instance->engine->set_read_format(instance, format);
2812 ao2_unlock(instance);
2813 } else {
2814 res = -1;
2815 }
2816 return res;
2817 }
2818
2819 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
2820 {
2821 int res;
2822
2823 if (instance->engine->set_read_format) {
2824 ao2_lock(instance);
2825 res = instance->engine->set_write_format(instance, format);
2826 ao2_unlock(instance);
2827 } else {
2828 res = -1;
2829 }
2830 return res;
2831 }
2832
2833 /* XXX Nothing calls this */
2834 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
2835 {
2836 struct ast_rtp_glue *glue;
2837 struct ast_rtp_instance *peer_instance = NULL;
2838 int res = -1;
2839
2840 if (!instance->engine->make_compatible) {
2841 return -1;
2842 }
2843
2844 ast_channel_lock(peer);
2845
2846 if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
2847 ast_channel_unlock(peer);
2848 return -1;
2849 }
2850
2851 glue->get_rtp_info(peer, &peer_instance);
2852 if (!peer_instance) {
2853 ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
2854 ast_channel_unlock(peer);
2855 return -1;
2856 }
2857 if (peer_instance->engine != instance->engine) {
2858 ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
2859 ast_channel_unlock(peer);
2860 ao2_ref(peer_instance, -1);
2861 return -1;
2862 }
2863
2864 /*
2865 * XXX Good thing nothing calls this function because we would need
2866 * deadlock avoidance to get the two instance locks.
2867 */
2868 res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
2869
2870 ast_channel_unlock(peer);
2871
2872 ao2_ref(peer_instance, -1);
2873 peer_instance = NULL;
2874
2875 return res;
2876 }
2877
2878 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)
2879 {
2880 if (instance->engine->available_formats) {
2881 ao2_lock(instance);
2882 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
2883 ao2_unlock(instance);
2884 if (ast_format_cap_count(result)) {
2885 return;
2886 }
2887 }
2888
2889 ast_translate_available_formats(to_endpoint, to_asterisk, result);
2890 }
2891
2892 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
2893 {
2894 int res;
2895
2896 if (instance->engine->activate) {
2897 ao2_lock(instance);
2898 res = instance->engine->activate(instance);
2899 ao2_unlock(instance);
2900 } else {
2901 res = 0;
2902 }
2903 return res;
2904 }
2905
2906 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
2907 struct ast_sockaddr *suggestion,
2908 const char *username)
2909 {
2910 if (instance->engine->stun_request) {
2911 instance->engine->stun_request(instance, suggestion, username);
2912 }
2913 }
2914
2915 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
2916 {
2917 instance->timeout = timeout;
2918 }
2919
2920 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
2921 {
2922 instance->holdtimeout = timeout;
2923 }
2924
2925 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
2926 {
2927 instance->keepalive = interval;
2928 }
2929
2930 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
2931 {
2932 return instance->timeout;
2933 }
2934
2935 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
2936 {
2937 return instance->holdtimeout;
2938 }
2939
2940 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
2941 {
2942 return instance->keepalive;
2943 }
2944
2945 unsigned int ast_rtp_instance_get_port_start(struct ast_rtp_instance *instance)
2946 {
2947 return instance->rtp_port_start;
2948 }
2949
2950 unsigned int ast_rtp_instance_get_port_end(struct ast_rtp_instance *instance)
2951 {
2952 return instance->rtp_port_end;
2953 }
2954
2955 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
2956 {
2957 return instance->engine;
2958 }
2959
2960 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
2961 {
2962 return instance->glue;
2963 }
2964
2965 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
2966 {
2967 if (res_srtp || res_srtp_policy) {
2968 return -1;
2969 }
2970 if (!srtp_res || !policy_res) {
2971 return -1;
2972 }
2973
2974 res_srtp = srtp_res;
2975 res_srtp_policy = policy_res;
2976
2977 return 0;
2978 }
2979
2980 void ast_rtp_engine_unregister_srtp(void)
2981 {
2982 res_srtp = NULL;
2983 res_srtp_policy = NULL;
2984 }
2985
2986 int ast_rtp_engine_srtp_is_registered(void)
2987 {
2988 return res_srtp && res_srtp_policy;
2989 }
2990
2991 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
2992 {
2993 int res = 0;
2994 struct ast_srtp **srtp;
2995
2996 if (!res_srtp) {
2997 return -1;
2998 }
2999
3000 ao2_lock(instance);
3001
3002 srtp = rtcp ? &instance->rtcp_srtp : &instance->srtp;
3003
3004 if (!*srtp) {
3005 res = res_srtp->create(srtp, instance, remote_policy);
3006 } else if (remote_policy) {
3007 res = res_srtp->replace(srtp, instance, remote_policy);
3008 }
3009 if (!res) {
3010 res = res_srtp->add_stream(*srtp, local_policy);
3011 }
3012
3013 ao2_unlock(instance);
3014
3015 return res;
3016 }
3017
3018 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
3019 {
3020 if (rtcp && instance->rtcp_srtp) {
3021 return instance->rtcp_srtp;
3022 } else {
3023 return instance->srtp;
3024 }
3025 }
3026
3027 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
3028 {
3029 int res;
3030
3031 if (instance->engine->sendcng) {
3032 ao2_lock(instance);
3033 res = instance->engine->sendcng(instance, level);
3034 ao2_unlock(instance);
3035 } else {
3036 res = -1;
3037 }
3038 return res;
3039 }
3040
3041 static void rtp_ice_wrap_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
3042 {
3043 ao2_lock(instance);
3044 instance->engine->ice->set_authentication(instance, ufrag, password);
3045 ao2_unlock(instance);
3046 }
3047
3048 static void rtp_ice_wrap_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
3049 {
3050 ao2_lock(instance);
3051 instance->engine->ice->add_remote_candidate(instance, candidate);
3052 ao2_unlock(instance);
3053 }
3054
3055 static void rtp_ice_wrap_start(struct ast_rtp_instance *instance)
3056 {
3057 ao2_lock(instance);
3058 instance->engine->ice->start(instance);
3059 ao2_unlock(instance);
3060 }
3061
3062 static void rtp_ice_wrap_stop(struct ast_rtp_instance *instance)
3063 {
3064 ao2_lock(instance);
3065 instance->engine->ice->stop(instance);
3066 ao2_unlock(instance);
3067 }
3068
3069 static const char *rtp_ice_wrap_get_ufrag(struct ast_rtp_instance *instance)
3070 {
3071 const char *ufrag;
3072
3073 ao2_lock(instance);
3074 ufrag = instance->engine->ice->get_ufrag(instance);
3075 ao2_unlock(instance);
3076 return ufrag;
3077 }
3078
3079 static const char *rtp_ice_wrap_get_password(struct ast_rtp_instance *instance)
3080 {
3081 const char *password;
3082
3083 ao2_lock(instance);
3084 password = instance->engine->ice->get_password(instance);
3085 ao2_unlock(instance);
3086 return password;
3087 }
3088
3089 static struct ao2_container *rtp_ice_wrap_get_local_candidates(struct ast_rtp_instance *instance)
3090 {
3091 struct ao2_container *local_candidates;
3092
3093 ao2_lock(instance);
3094 local_candidates = instance->engine->ice->get_local_candidates(instance);
3095 ao2_unlock(instance);
3096 return local_candidates;
3097 }
3098
3099 static void rtp_ice_wrap_ice_lite(struct ast_rtp_instance *instance)
3100 {
3101 ao2_lock(instance);
3102 instance->engine->ice->ice_lite(instance);
3103 ao2_unlock(instance);
3104 }
3105
3106 static void rtp_ice_wrap_set_role(struct ast_rtp_instance *instance,
3107 enum ast_rtp_ice_role role)
3108 {
3109 ao2_lock(instance);
3110 instance->engine->ice->set_role(instance, role);
3111 ao2_unlock(instance);
3112 }
3113
3114 static void rtp_ice_wrap_turn_request(struct ast_rtp_instance *instance,
3115 enum ast_rtp_ice_component_type component, enum ast_transport transport,
3116 const char *server, unsigned int port, const char *username, const char *password)
3117 {
3118 ao2_lock(instance);
3119 instance->engine->ice->turn_request(instance, component, transport, server, port,
3120 username, password);
3121 ao2_unlock(instance);
3122 }
3123
3124 static void rtp_ice_wrap_change_components(struct ast_rtp_instance *instance,
3125 int num_components)
3126 {
3127 ao2_lock(instance);
3128 instance->engine->ice->change_components(instance, num_components);
3129 ao2_unlock(instance);
3130 }
3131
3132 static struct ast_rtp_engine_ice rtp_ice_wrappers = {
3133 .set_authentication = rtp_ice_wrap_set_authentication,
3134 .add_remote_candidate = rtp_ice_wrap_add_remote_candidate,
3135 .start = rtp_ice_wrap_start,
3136 .stop = rtp_ice_wrap_stop,
3137 .get_ufrag = rtp_ice_wrap_get_ufrag,
3138 .get_password = rtp_ice_wrap_get_password,
3139 .get_local_candidates = rtp_ice_wrap_get_local_candidates,
3140 .ice_lite = rtp_ice_wrap_ice_lite,
3141 .set_role = rtp_ice_wrap_set_role,
3142 .turn_request = rtp_ice_wrap_turn_request,
3143 .change_components = rtp_ice_wrap_change_components,
3144 };
3145
3146 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
3147 {
3148 if (instance->engine->ice) {
3149 return &rtp_ice_wrappers;
3150 }
3151 /* ICE not available */
3152 return NULL;
3153 }
3154
3155 #ifdef TEST_FRAMEWORK
3156 struct ast_rtp_engine_test *ast_rtp_instance_get_test(struct ast_rtp_instance *instance)
3157 {
3158 return instance->engine->test;
3159 }
3160 #endif
3161
3162 static int rtp_dtls_wrap_set_configuration(struct ast_rtp_instance *instance,
3163 const struct ast_rtp_dtls_cfg *dtls_cfg)
3164 {
3165 int set_configuration;
3166
3167 ao2_lock(instance);
3168 set_configuration = instance->engine->dtls->set_configuration(instance, dtls_cfg);
3169 ao2_unlock(instance);
3170 return set_configuration;
3171 }
3172
3173 static int rtp_dtls_wrap_active(struct ast_rtp_instance *instance)
3174 {
3175 int active;
3176
3177 ao2_lock(instance);
3178 active = instance->engine->dtls->active(instance);
3179 ao2_unlock(instance);
3180 return active;
3181 }
3182
3183 static void rtp_dtls_wrap_stop(struct ast_rtp_instance *instance)
3184 {
3185 ao2_lock(instance);
3186 instance->engine->dtls->stop(instance);
3187 ao2_unlock(instance);
3188 }
3189
3190 static void rtp_dtls_wrap_reset(struct ast_rtp_instance *instance)
3191 {
3192 ao2_lock(instance);
3193 instance->engine->dtls->reset(instance);
3194 ao2_unlock(instance);
3195 }
3196
3197 static enum ast_rtp_dtls_connection rtp_dtls_wrap_get_connection(struct ast_rtp_instance *instance)
3198 {
3199 enum ast_rtp_dtls_connection get_connection;
3200
3201 ao2_lock(instance);
3202 get_connection = instance->engine->dtls->get_connection(instance);
3203 ao2_unlock(instance);
3204 return get_connection;
3205 }
3206
3207 static enum ast_rtp_dtls_setup rtp_dtls_wrap_get_setup(struct ast_rtp_instance *instance)
3208 {
3209 enum ast_rtp_dtls_setup get_setup;
3210
3211 ao2_lock(instance);
3212 get_setup = instance->engine->dtls->get_setup(instance);
3213 ao2_unlock(instance);
3214 return get_setup;
3215 }
3216
3217 static void rtp_dtls_wrap_set_setup(struct ast_rtp_instance *instance,
3218 enum ast_rtp_dtls_setup setup)
3219 {
3220 ao2_lock(instance);
3221 instance->engine->dtls->set_setup(instance, setup);
3222 ao2_unlock(instance);
3223 }
3224
3225 static void rtp_dtls_wrap_set_fingerprint(struct ast_rtp_instance *instance,
3226 enum ast_rtp_dtls_hash hash, const char *fingerprint)
3227 {
3228 ao2_lock(instance);
3229 instance->engine->dtls->set_fingerprint(instance, hash, fingerprint);
3230 ao2_unlock(instance);
3231 }
3232
3233 static enum ast_rtp_dtls_hash rtp_dtls_wrap_get_fingerprint_hash(struct ast_rtp_instance *instance)
3234 {
3235 enum ast_rtp_dtls_hash get_fingerprint_hash;
3236
3237 ao2_lock(instance);
3238 get_fingerprint_hash = instance->engine->dtls->get_fingerprint_hash(instance);
3239 ao2_unlock(instance);
3240 return get_fingerprint_hash;
3241 }
3242
3243 static const char *rtp_dtls_wrap_get_fingerprint(struct ast_rtp_instance *instance)
3244 {
3245 const char *get_fingerprint;
3246
3247 ao2_lock(instance);
3248 get_fingerprint = instance->engine->dtls->get_fingerprint(instance);
3249 ao2_unlock(instance);
3250 return get_fingerprint;
3251 }
3252
3253 static struct ast_rtp_engine_dtls rtp_dtls_wrappers = {
3254 .set_configuration = rtp_dtls_wrap_set_configuration,
3255 .active = rtp_dtls_wrap_active,
3256 .stop = rtp_dtls_wrap_stop,
3257 .reset = rtp_dtls_wrap_reset,
3258 .get_connection = rtp_dtls_wrap_get_connection,
3259 .get_setup = rtp_dtls_wrap_get_setup,
3260 .set_setup = rtp_dtls_wrap_set_setup,
3261 .set_fingerprint = rtp_dtls_wrap_set_fingerprint,
3262 .get_fingerprint_hash = rtp_dtls_wrap_get_fingerprint_hash,
3263 .get_fingerprint = rtp_dtls_wrap_get_fingerprint,
3264 };
3265
3266 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
3267 {
3268 if (instance->engine->dtls) {
3269 return &rtp_dtls_wrappers;
3270 }
3271 /* DTLS not available */
3272 return NULL;
3273 }
3274
3275 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
3276 {
3277 if (!strcasecmp(name, "dtlsenable")) {
3278 dtls_cfg->enabled = ast_true(value) ? 1 : 0;
3279 } else if (!strcasecmp(name, "dtlsverify")) {
3280 if (!strcasecmp(value, "yes")) {
3281 dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
3282 } else if (!strcasecmp(value, "fingerprint")) {
3283 dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
3284 } else if (!strcasecmp(value, "certificate")) {
3285 dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
3286 } else if (!strcasecmp(value, "no")) {
3287 dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
3288 } else {
3289 return -1;
3290 }
3291 } else if (!strcasecmp(name, "dtlsrekey")) {
3292 if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
3293 return -1;
3294 }
3295 } else if (!strcasecmp(name, "dtlsautogeneratecert")) {
3296 dtls_cfg->ephemeral_cert = ast_true(value) ? 1 : 0;
3297 } else if (!strcasecmp(name, "dtlscertfile")) {
3298 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3299 ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3300 return -1;
3301 }
3302 ast_free(dtls_cfg->certfile);
3303 dtls_cfg->certfile = ast_strdup(value);
3304 } else if (!strcasecmp(name, "dtlsprivatekey")) {
3305 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3306 ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3307 return -1;
3308 }
3309 ast_free(dtls_cfg->pvtfile);
3310 dtls_cfg->pvtfile = ast_strdup(value);
3311 } else if (!strcasecmp(name, "dtlscipher")) {
3312 ast_free(dtls_cfg->cipher);
3313 dtls_cfg->cipher = ast_strdup(value);
3314 } else if (!strcasecmp(name, "dtlscafile")) {
3315 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3316 ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3317 return -1;
3318 }
3319 ast_free(dtls_cfg->cafile);
3320 dtls_cfg->cafile = ast_strdup(value);
3321 } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
3322 if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3323 ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3324 return -1;
3325 }
3326 ast_free(dtls_cfg->capath);
3327 dtls_cfg->capath = ast_strdup(value);
3328 } else if (!strcasecmp(name, "dtlssetup")) {
3329 if (!strcasecmp(value, "active")) {
3330 dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
3331 } else if (!strcasecmp(value, "passive")) {
3332 dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
3333 } else if (!strcasecmp(value, "actpass")) {
3334 dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
3335 }
3336 } else if (!strcasecmp(name, "dtlsfingerprint")) {
3337 if (!strcasecmp(value, "sha-256")) {
3338 dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
3339 } else if (!strcasecmp(value, "sha-1")) {
3340 dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
3341 }
3342 } else {
3343 return -1;
3344 }
3345
3346 return 0;
3347 }
3348
3349 int ast_rtp_dtls_cfg_validate(struct ast_rtp_dtls_cfg *dtls_cfg)
3350 {
3351 if (dtls_cfg->ephemeral_cert) {
3352 if (!ast_strlen_zero(dtls_cfg->certfile)) {
3353 ast_log(LOG_ERROR, "You cannot request automatically generated certificates"
3354 " (dtls_auto_generate_cert) and also specify a certificate file"
3355 " (dtls_cert_file) at the same time\n");
3356 return -1;
3357 } else if (!ast_strlen_zero(dtls_cfg->pvtfile)
3358 || !ast_strlen_zero(dtls_cfg->cafile)
3359 || !ast_strlen_zero(dtls_cfg->capath)) {
3360 ast_log(LOG_NOTICE, "dtls_pvt_file, dtls_cafile, and dtls_ca_path are"
3361 " ignored when dtls_auto_generate_cert is enabled\n");
3362 }
3363 }
3364
3365 return 0;
3366 }
3367
3368 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
3369 {
3370 ast_rtp_dtls_cfg_free(dst_cfg); /* Prevent a double-call leaking memory via ast_strdup */
3371
3372 dst_cfg->enabled = src_cfg->enabled;
3373 dst_cfg->verify = src_cfg->verify;
3374 dst_cfg->rekey = src_cfg->rekey;
3375 dst_cfg->suite = src_cfg->suite;
3376 dst_cfg->hash = src_cfg->hash;
3377 dst_cfg->ephemeral_cert = src_cfg->ephemeral_cert;
3378 dst_cfg->certfile = ast_strdup(src_cfg->certfile);
3379 dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
3380 dst_cfg->cipher = ast_strdup(src_cfg->cipher);
3381 dst_cfg->cafile = ast_strdup(src_cfg->cafile);
3382 dst_cfg->capath = ast_strdup(src_cfg->capath);
3383 dst_cfg->default_setup = src_cfg->default_setup;
3384 }
3385
3386 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
3387 {
3388 ast_free(dtls_cfg->certfile);
3389 dtls_cfg->certfile = NULL;
3390 ast_free(dtls_cfg->pvtfile);
3391 dtls_cfg->pvtfile = NULL;
3392 ast_free(dtls_cfg->cipher);
3393 dtls_cfg->cipher = NULL;
3394 ast_free(dtls_cfg->cafile);
3395 dtls_cfg->cafile = NULL;
3396 ast_free(dtls_cfg->capath);
3397 dtls_cfg->capath = NULL;
3398 }
3399
3400 /*! \internal
3401 * \brief Small helper routine that cleans up entry i in
3402 * \c ast_rtp_mime_types.
3403 */
3404 static void rtp_engine_mime_type_cleanup(int i)
3405 {
3406 ao2_cleanup(ast_rtp_mime_types[i].payload_type.format);
3407 memset(&ast_rtp_mime_types[i], 0, sizeof(struct ast_rtp_mime_type));
3408 }
3409
3410 static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
3411 {
3412 int x;
3413
3414 ast_rwlock_wrlock(&mime_types_lock);
3415
3416 x = mime_types_len;
3417 if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
3418 ast_rwlock_unlock(&mime_types_lock);
3419 return;
3420 }
3421
3422 /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
3423 memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));
3424 if (format) {
3425 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
3426 ast_rtp_mime_types[x].payload_type.format = ao2_bump(format);
3427 } else {
3428 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
3429 }
3430 ast_copy_string(ast_rtp_mime_types[x].type, type, sizeof(ast_rtp_mime_types[x].type));
3431 ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
3432 ast_rtp_mime_types[x].sample_rate = sample_rate;
3433 mime_types_len++;
3434
3435 ast_rwlock_unlock(&mime_types_lock);
3436 }
3437
3438 static void add_static_payload(int payload, struct ast_format *format, int rtp_code)
3439 {
3440 struct ast_rtp_payload_type *type;
3441
3442 /*
3443 * ARRAY_LEN's result is cast to an int so 'map' is not autocast to a size_t,
3444 * which if negative would cause an assertion.
3445 */
3446 ast_assert(payload < (int)ARRAY_LEN(static_RTP_PT));
3447
3448 if (ast_option_rtpusedynamic && payload < 0) {
3449 /*
3450 * We're going to build dynamic payloads dynamically. An RTP code is
3451 * required otherwise one will be dynamically allocated per instance.
3452 */
3453 return;
3454 }
3455
3456 /*
3457 * Either the given payload is truly a static type, or Asterisk is
3458 * globally storing the dynamic payloads in the static_RTP_PT object.
3459 */
3460 ast_rwlock_wrlock(&static_RTP_PT_lock);
3461
3462 if (payload < 0) {
3463 /*
3464 * This is a dynamic payload that will be stored globally,
3465 * so find the next available empty slot.
3466 */
3467 payload = find_unused_payload(NULL);
3468 if (payload < 0) {
3469 ast_log(LOG_WARNING, "No dynamic RTP payload type values available "
3470 "for %s - %d!\n", format ? ast_format_get_name(format) : "", rtp_code);
3471 ast_rwlock_unlock(&static_RTP_PT_lock);
3472 return;
3473 }
3474 }
3475
3476 type = rtp_payload_type_alloc(format, payload, rtp_code, 1, 0);
3477 if (type) {
3478 ao2_cleanup(static_RTP_PT[payload]);
3479 static_RTP_PT[payload] = type;
3480 }
3481 ast_rwlock_unlock(&static_RTP_PT_lock);
3482 }
3483
3484 int ast_rtp_engine_load_format(struct ast_format *format)
3485 {
3486 char *codec_name = ast_strdupa(ast_format_get_codec_name(format));
3487
3488 codec_name = ast_str_to_upper(codec_name);
3489
3490 set_next_mime_type(format,
3491 0,
3492 ast_codec_media_type2str(ast_format_get_type(format)),
3493 codec_name,
3494 ast_format_get_sample_rate(format));
3495 add_static_payload(-1, format, 0);
3496
3497 return 0;
3498 }
3499
3500 int ast_rtp_engine_unload_format(struct ast_format *format)
3501 {
3502 int x;
3503 int y = 0;
3504
3505 ast_rwlock_wrlock(&static_RTP_PT_lock);
3506 /* remove everything pertaining to this format id from the lists */
3507 for (x = 0; x < AST_RTP_MAX_PT; x++) {
3508 if (static_RTP_PT[x]
3509 && ast_format_cmp(static_RTP_PT[x]->format, format) == AST_FORMAT_CMP_EQUAL) {
3510 ao2_ref(static_RTP_PT[x], -1);
3511 static_RTP_PT[x] = NULL;
3512 }
3513 }
3514 ast_rwlock_unlock(&static_RTP_PT_lock);
3515
3516 ast_rwlock_wrlock(&mime_types_lock);
3517 /* rebuild the list skipping the items matching this id */
3518 for (x = 0; x < mime_types_len; x++) {
3519 if (ast_format_cmp(ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
3520 rtp_engine_mime_type_cleanup(x);
3521 continue;
3522 }
3523 if (x != y) {
3524 ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
3525 }
3526 y++;
3527 }
3528 mime_types_len = y;
3529 ast_rwlock_unlock(&mime_types_lock);
3530 return 0;
3531 }
3532
3533 /*!
3534 * \internal
3535 * \brief \ref stasis message payload for RTCP messages
3536 */
3537 struct rtcp_message_payload {
3538 struct ast_channel_snapshot *snapshot; /*< The channel snapshot, if available */
3539 struct ast_rtp_rtcp_report *report; /*< The RTCP report */
3540 struct ast_json *blob; /*< Extra JSON data to publish */
3541 };
3542
3543 static void rtcp_message_payload_dtor(void *obj)
3544 {
3545 struct rtcp_message_payload *payload = obj;
3546
3547 ao2_cleanup(payload->report);
3548 ao2_cleanup(payload->snapshot);
3549 ast_json_unref(payload->blob);
3550 }
3551
3552 static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
3553 {
3554 struct rtcp_message_payload *payload = stasis_message_data(msg);
3555 RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
3556 RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
3557 unsigned int ssrc = payload->report->ssrc;
3558 unsigned int type = payload->report->type;
3559 unsigned int report_count = payload->report->reception_report_count;
3560 int i;
3561
3562 if (!packet_string) {
3563 return NULL;
3564 }
3565
3566 if (payload->snapshot) {
3567 channel_string = ast_manager_build_channel_state_string(payload->snapshot);
3568 if (!channel_string) {
3569 return NULL;
3570 }
3571 }
3572
3573 if (payload->blob) {
3574 /* Optional data */
3575 struct ast_json *to = ast_json_object_get(payload->blob, "to");
3576 struct ast_json *from = ast_json_object_get(payload->blob, "from");
3577 struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
3578 struct ast_json *mes = ast_json_object_get(payload->blob, "mes");
3579 if (to) {
3580 ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
3581 }
3582 if (from) {
3583 ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
3584 }
3585 if (rtt) {
3586 ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
3587 }
3588 if (mes) {
3589 ast_str_append(&packet_string, 0, "MES: %4.1f\r\n", ast_json_real_get(mes));
3590 }
3591 }
3592
3593 ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
3594 ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
3595 ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
3596 if (type == AST_RTP_RTCP_SR) {
3597 ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
3598 (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
3599 (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
3600 ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
3601 payload->report->sender_information.rtp_timestamp);
3602 ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
3603 payload->report->sender_information.packet_count);
3604 ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
3605 payload->report->sender_information.octet_count);
3606 }
3607
3608 for (i = 0; i < report_count; i++) {
3609 RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
3610
3611 if (!payload->report->report_block[i]) {
3612 break;
3613 }
3614
3615 report_string = ast_str_create(256);
3616 if (!report_string) {
3617 return NULL;
3618 }
3619
3620 ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
3621 i, payload->report->report_block[i]->source_ssrc);
3622 ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
3623 i, payload->report->report_block[i]->lost_count.fraction);
3624 ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
3625 i, payload->report->report_block[i]->lost_count.packets);
3626 ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
3627 i, payload->report->report_block[i]->highest_seq_no & 0xffff);
3628 ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
3629 i, payload->report->report_block[i]->highest_seq_no >> 16);
3630 ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
3631 i, payload->report->report_block[i]->ia_jitter);
3632 ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
3633 i, payload->report->report_block[i]->lsr);
3634 ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
3635 i, ((double)payload->report->report_block[i]->dlsr) / 65536);
3636 ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
3637 }
3638
3639 return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
3640 stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
3641 "%s%s",
3642 AS_OR(channel_string, ""),
3643 ast_str_buffer(packet_string));
3644 }
3645
3646 static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
3647 const struct stasis_message_sanitizer *sanitize)
3648 {
3649 struct rtcp_message_payload *payload = stasis_message_data(msg);
3650 struct ast_json *json_rtcp_report = NULL;
3651 struct ast_json *json_rtcp_report_blocks;
3652 struct ast_json *json_rtcp_sender_info = NULL;
3653 struct ast_json *json_channel = NULL;
3654 int i;
3655
3656 json_rtcp_report_blocks = ast_json_array_create();
3657 if (!json_rtcp_report_blocks) {
3658 return NULL;
3659 }
3660
3661 for (i = 0; i < payload->report->reception_report_count && payload->report->report_block[i]; i++) {
3662 struct ast_json *json_report_block;
3663 char str_lsr[32];
3664
3665 snprintf(str_lsr, sizeof(str_lsr), "%u", payload->report->report_block[i]->lsr);
3666 json_report_block = ast_json_pack("{s: I, s: I, s: I, s: I, s: I, s: s, s: I}",
3667 "source_ssrc", (ast_json_int_t)payload->report->report_block[i]->source_ssrc,
3668 "fraction_lost", (ast_json_int_t)payload->report->report_block[i]->lost_count.fraction,
3669 "packets_lost", (ast_json_int_t)payload->report->report_block[i]->lost_count.packets,
3670 "highest_seq_no", (ast_json_int_t)payload->report->report_block[i]->highest_seq_no,
3671 "ia_jitter", (ast_json_int_t)payload->report->report_block[i]->ia_jitter,
3672 "lsr", str_lsr,
3673 "dlsr", (ast_json_int_t)payload->report->report_block[i]->dlsr);
3674 if (!json_report_block
3675 || ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
3676 ast_json_unref(json_rtcp_report_blocks);
3677 return NULL;
3678 }
3679 }
3680
3681 if (payload->report->type == AST_RTP_RTCP_SR) {
3682 char sec[32];
3683 char usec[32];
3684
3685 snprintf(sec, sizeof(sec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec);
3686 snprintf(usec, sizeof(usec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
3687 json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: I, s: I, s: I}",
3688 "ntp_timestamp_sec", sec,
3689 "ntp_timestamp_usec", usec,
3690 "rtp_timestamp", (ast_json_int_t)payload->report->sender_information.rtp_timestamp,
3691 "packets", (ast_json_int_t)payload->report->sender_information.packet_count,
3692 "octets", (ast_json_int_t)payload->report->sender_information.octet_count);
3693 if (!json_rtcp_sender_info) {
3694 ast_json_unref(json_rtcp_report_blocks);
3695 return NULL;
3696 }
3697 }
3698
3699 json_rtcp_report = ast_json_pack("{s: I, s: I, s: i, s: o, s: o}",
3700 "ssrc", (ast_json_int_t)payload->report->ssrc,
3701 "type", (ast_json_int_t)payload->report->type,
3702 "report_count", payload->report->reception_report_count,
3703 "sender_information", json_rtcp_sender_info ?: ast_json_null(),
3704 "report_blocks", json_rtcp_report_blocks);
3705 if (!json_rtcp_report) {
3706 return NULL;
3707 }
3708
3709 if (payload->snapshot) {
3710 json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
3711 if (!json_channel) {
3712 ast_json_unref(json_rtcp_report);
3713 return NULL;
3714 }
3715 }
3716
3717 return ast_json_pack("{s: s, s: o?, s: o, s: O?}",
3718 "type", stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
3719 "channel", json_channel,
3720 "rtcp_report", json_rtcp_report,
3721 "blob", payload->blob);
3722 }
3723
3724 static void rtp_rtcp_report_dtor(void *obj)
3725 {
3726 int i;
3727 struct ast_rtp_rtcp_report *rtcp_report = obj;
3728
3729 for (i = 0; i < rtcp_report->reception_report_count; i++) {
3730 ast_free(rtcp_report->report_block[i]);
3731 }
3732 }
3733
3734 struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
3735 {
3736 struct ast_rtp_rtcp_report *rtcp_report;
3737
3738 /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
3739 rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
3740 rtp_rtcp_report_dtor);
3741
3742 return rtcp_report;
3743 }
3744
3745 void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
3746 struct stasis_message_type *message_type,
3747 struct ast_rtp_rtcp_report *report,
3748 struct ast_json *blob)
3749 {
3750 RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
3751 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
3752
3753 if (!message_type) {
3754 return;
3755 }
3756
3757 payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
3758 if (!payload || !report) {
3759 return;
3760 }
3761
3762 if (!ast_strlen_zero(rtp->channel_uniqueid)) {
3763 payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
3764 }
3765 if (blob) {
3766 payload->blob = blob;
3767 ast_json_ref(blob);
3768 }
3769 ao2_ref(report, +1);
3770 payload->report = report;
3771
3772 message = stasis_message_create(message_type, payload);
3773 if (!message) {
3774 return;
3775 }
3776
3777 stasis_publish(ast_rtp_topic(), message);
3778 }
3779
3780 /*!
3781 * @{ \brief Define RTCP/RTP message types.
3782 */
3783 STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
3784 .to_ami = rtcp_report_to_ami,
3785 .to_json = rtcp_report_to_json,);
3786 STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
3787 .to_ami = rtcp_report_to_ami,
3788 .to_json = rtcp_report_to_json,);
3789 /*! @} */
3790
3791 struct stasis_topic *ast_rtp_topic(void)
3792 {
3793 return rtp_topic;
3794 }
3795
3796 static uintmax_t debug_category_rtp_id;
3797
3798 uintmax_t ast_debug_category_rtp_id(void)
3799 {
3800 return debug_category_rtp_id;
3801 }
3802
3803 static uintmax_t debug_category_rtp_packet_id;
3804
3805 uintmax_t ast_debug_category_rtp_packet_id(void)
3806 {
3807 return debug_category_rtp_packet_id;
3808 }
3809
3810 static uintmax_t debug_category_rtcp_id;
3811
3812 uintmax_t ast_debug_category_rtcp_id(void)
3813 {
3814 return debug_category_rtcp_id;
3815 }
3816
3817 static uintmax_t debug_category_rtcp_packet_id;
3818
3819 uintmax_t ast_debug_category_rtcp_packet_id(void)
3820 {
3821 return debug_category_rtcp_packet_id;
3822 }
3823
3824 static uintmax_t debug_category_dtls_id;
3825
3826 uintmax_t ast_debug_category_dtls_id(void)
3827 {
3828 return debug_category_dtls_id;
3829 }
3830
3831 static uintmax_t debug_category_dtls_packet_id;
3832
3833 uintmax_t ast_debug_category_dtls_packet_id(void)
3834 {
3835 return debug_category_dtls_packet_id;
3836 }
3837
3838 static uintmax_t debug_category_ice_id;
3839
3840 uintmax_t ast_debug_category_ice_id(void)
3841 {
3842 return debug_category_ice_id;
3843 }
3844
3845 /*!
3846 * \internal
3847 * \brief Shutdown the RTP engine
3848 * This function will not get called if any module fails to unload.
3849 */
3850 static void rtp_engine_shutdown(void)
3851 {
3852 int x;
3853
3854 ao2_cleanup(rtp_topic);
3855 rtp_topic = NULL;
3856 STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
3857 STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
3858
3859 ast_rwlock_wrlock(&static_RTP_PT_lock);
3860 for (x = 0; x < AST_RTP_MAX_PT; x++) {
3861 ao2_cleanup(static_RTP_PT[x]);
3862 static_RTP_PT[x] = NULL;
3863 }
3864 ast_rwlock_unlock(&static_RTP_PT_lock);
3865
3866 ast_rwlock_wrlock(&mime_types_lock);
3867 for (x = 0; x < mime_types_len; x++) {
3868 if (ast_rtp_mime_types[x].payload_type.format) {
3869 rtp_engine_mime_type_cleanup(x);
3870 }
3871 }
3872 mime_types_len = 0;
3873 ast_rwlock_unlock(&mime_types_lock);
3874 }
3875
3876 /*!
3877 * \internal
3878 * \brief Unregister the debug categories
3879 * This function will always get called even if any module fails to unload.
3880 */
3881 static void rtp_engine_atexit(void)
3882 {
3883 ast_debug_category_unregister(AST_LOG_CATEGORY_ICE);
3884
3885 ast_debug_category_unregister(AST_LOG_CATEGORY_DTLS_PACKET);
3886 ast_debug_category_unregister(AST_LOG_CATEGORY_DTLS);
3887
3888 ast_debug_category_unregister(AST_LOG_CATEGORY_RTCP_PACKET);
3889 ast_debug_category_unregister(AST_LOG_CATEGORY_RTCP);
3890
3891 ast_debug_category_unregister(AST_LOG_CATEGORY_RTP_PACKET);
3892 ast_debug_category_unregister(AST_LOG_CATEGORY_RTP);
3893 }
3894
3895 int ast_rtp_engine_init(void)
3896 {
3897 ast_rwlock_init(&mime_types_lock);
3898 ast_rwlock_init(&static_RTP_PT_lock);
3899
3900 rtp_topic = stasis_topic_create("rtp:all");
3901 if (!rtp_topic) {
3902 return -1;
3903 }
3904 STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
3905 STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
3906 ast_register_cleanup(rtp_engine_shutdown);
3907
3908 /* Define all the RTP mime types available */
3909 set_next_mime_type(ast_format_g723, 0, "audio", "G723", 8000);
3910 set_next_mime_type(ast_format_gsm, 0, "audio", "GSM", 8000);
3911 set_next_mime_type(ast_format_ulaw, 0, "audio", "PCMU", 8000);
3912 set_next_mime_type(ast_format_ulaw, 0, "audio", "G711U", 8000);
3913 set_next_mime_type(ast_format_alaw, 0, "audio", "PCMA", 8000);
3914 set_next_mime_type(ast_format_alaw, 0, "audio", "G711A", 8000);
3915 set_next_mime_type(ast_format_g726, 0, "audio", "G726-32", 8000);
3916 set_next_mime_type(ast_format_adpcm, 0, "audio", "DVI4", 8000);
3917 set_next_mime_type(ast_format_slin, 0, "audio", "L16", 8000);
3918 set_next_mime_type(ast_format_slin16, 0, "audio", "L16", 16000);
3919 set_next_mime_type(ast_format_slin16, 0, "audio", "L16-256", 16000);
3920 set_next_mime_type(ast_format_slin12, 0, "audio", "L16", 12000);
3921 set_next_mime_type(ast_format_slin24, 0, "audio", "L16", 24000);
3922 set_next_mime_type(ast_format_slin32, 0, "audio", "L16", 32000);
3923 set_next_mime_type(ast_format_slin44, 0, "audio", "L16", 44100);
3924 set_next_mime_type(ast_format_slin48, 0, "audio", "L16", 48000);
3925 set_next_mime_type(ast_format_slin96, 0, "audio", "L16", 96000);
3926 set_next_mime_type(ast_format_slin192, 0, "audio", "L16", 192000);
3927 set_next_mime_type(ast_format_lpc10, 0, "audio", "LPC", 8000);
3928 set_next_mime_type(ast_format_g729, 0, "audio", "G729", 8000);
3929 set_next_mime_type(ast_format_g729, 0, "audio", "G729A", 8000);
3930 set_next_mime_type(ast_format_g729, 0, "audio", "G.729", 8000);
3931 set_next_mime_type(ast_format_speex, 0, "audio", "speex", 8000);
3932 set_next_mime_type(ast_format_speex16, 0, "audio", "speex", 16000);
3933 set_next_mime_type(ast_format_speex32, 0, "audio", "speex", 32000);
3934 set_next_mime_type(ast_format_ilbc, 0, "audio", "iLBC", 8000);
3935 /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
3936 set_next_mime_type(ast_format_g722, 0, "audio", "G722", 8000);
3937 set_next_mime_type(ast_format_g726_aal2, 0, "audio", "AAL2-G726-32", 8000);
3938 /* we need all possible dtmf/bitrate combinations or ast_rtp_codecs_payloads_set_rtpmap_type_rate will not examine it */
3939 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
3940 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 16000);
3941 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 24000);
3942 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 32000);
3943 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 48000);
3944 set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
3945 set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
3946 set_next_mime_type(ast_format_jpeg, 0, "video", "JPEG", 90000);
3947 set_next_mime_type(ast_format_png, 0, "video", "PNG", 90000);
3948 set_next_mime_type(ast_format_h261, 0, "video", "H261", 90000);
3949 set_next_mime_type(ast_format_h263, 0, "video", "H263", 90000);
3950 set_next_mime_type(ast_format_h263p, 0, "video", "h263-1998", 90000);
3951 set_next_mime_type(ast_format_h264, 0, "video", "H264", 90000);
3952 set_next_mime_type(ast_format_h265, 0, "video", "H265", 90000);
3953 set_next_mime_type(ast_format_mp4, 0, "video", "MP4V-ES", 90000);
3954 set_next_mime_type(ast_format_t140_red, 0, "text", "RED", 1000);
3955 set_next_mime_type(ast_format_t140, 0, "text", "T140", 1000);
3956 set_next_mime_type(ast_format_siren7, 0, "audio", "G7221", 16000);
3957 set_next_mime_type(ast_format_siren14, 0, "audio", "G7221", 32000);
3958 set_next_mime_type(ast_format_g719, 0, "audio", "G719", 48000);
3959 /* Opus, VP8, and VP9 */
3960 set_next_mime_type(ast_format_opus, 0, "audio", "opus", 48000);
3961 set_next_mime_type(ast_format_vp8, 0, "video", "VP8", 90000);
3962 set_next_mime_type(ast_format_vp9, 0, "video", "VP9", 90000);
3963
•3964 set_next_mime_type(ast_format_amr, 0, "audio", "AMR", 8000);
3965 set_next_mime_type(ast_format_amrwb, 0, "audio", "AMR-WB", 16000);
3966
3967 /* Define the static rtp payload mappings */
3968 add_static_payload(0, ast_format_ulaw, 0);
3969 #ifdef USE_DEPRECATED_G726
3970 add_static_payload(2, ast_format_g726, 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
3971 #endif
3972 add_static_payload(3, ast_format_gsm, 0);
3973 add_static_payload(4, ast_format_g723, 0);
3974 add_static_payload(5, ast_format_adpcm, 0);/* 8 kHz */
3975 add_static_payload(6, ast_format_adpcm, 0); /* 16 kHz */
3976 add_static_payload(7, ast_format_lpc10, 0);
3977 add_static_payload(8, ast_format_alaw, 0);
3978 add_static_payload(9, ast_format_g722, 0);
3979 add_static_payload(10, ast_format_slin, 0); /* 2 channels */
3980 add_static_payload(11, ast_format_slin, 0); /* 1 channel */
3981 add_static_payload(13, NULL, AST_RTP_CN);
3982 add_static_payload(16, ast_format_adpcm, 0); /* 11.025 kHz */
3983 add_static_payload(17, ast_format_adpcm, 0); /* 22.050 kHz */
3984 add_static_payload(18, ast_format_g729, 0);
3985 add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
3986 add_static_payload(26, ast_format_jpeg, 0);
3987 add_static_payload(31, ast_format_h261, 0);
3988 add_static_payload(34, ast_format_h263, 0);
3989
3990 /*
3991 * Dynamic payload types - Even when dynamically assigning them we'll fall
3992 * back to using the statically declared values as the default number.
3993 */
3994 add_static_payload(96, ast_format_slin192, 0);
3995 add_static_payload(97, ast_format_ilbc, 0);
3996
3997 add_static_payload(99, ast_format_h264, 0);
3998 add_static_payload(100, ast_format_vp8, 0);
3999 add_static_payload(101, NULL, AST_RTP_DTMF);
4000 add_static_payload(102, ast_format_siren7, 0);
4001 add_static_payload(103, ast_format_h263p, 0);
4002 add_static_payload(104, ast_format_mp4, 0);
4003 add_static_payload(105, ast_format_t140_red, 0); /* Real time text chat (with redundancy encoding) */
4004 add_static_payload(106, ast_format_t140, 0); /* Real time text chat */
4005 add_static_payload(107, ast_format_opus, 0);
4006 add_static_payload(108, ast_format_vp9, 0);
4007 add_static_payload(109, ast_format_h265, 0);
4008
4009 add_static_payload(110, ast_format_speex, 0);
•4010
4011 add_static_payload(-1, ast_format_amr, 0);
4012 add_static_payload(-1, ast_format_amrwb, 0);
4013 add_static_payload(111, ast_format_g726, 0);
4014 add_static_payload(112, ast_format_g726_aal2, 0);
4015
4016 add_static_payload(115, ast_format_siren14, 0);
4017 add_static_payload(116, ast_format_g719, 0);
4018 add_static_payload(117, ast_format_speex16, 0);
4019 add_static_payload(118, ast_format_slin16, 0); /* 16 Khz signed linear */
4020 add_static_payload(119, ast_format_speex32, 0);
4021
4022 add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
4023 add_static_payload(122, ast_format_slin12, 0);
4024 add_static_payload(123, ast_format_slin24, 0);
4025 add_static_payload(124, ast_format_slin32, 0);
4026 add_static_payload(125, ast_format_slin44, 0);
4027 add_static_payload(126, ast_format_slin48, 0);
4028 add_static_payload(127, ast_format_slin96, 0);
4029 /* payload types above 127 are not valid */
4030
4031 debug_category_rtp_id = ast_debug_category_register(AST_LOG_CATEGORY_RTP);
4032 debug_category_rtp_packet_id = ast_debug_category_register(AST_LOG_CATEGORY_RTP_PACKET);
4033 debug_category_rtcp_id = ast_debug_category_register(AST_LOG_CATEGORY_RTCP);
4034 debug_category_rtcp_packet_id = ast_debug_category_register(AST_LOG_CATEGORY_RTCP_PACKET);
4035 debug_category_dtls_id = ast_debug_category_register(AST_LOG_CATEGORY_DTLS);
4036 debug_category_dtls_packet_id = ast_debug_category_register(AST_LOG_CATEGORY_DTLS_PACKET);
4037 debug_category_ice_id = ast_debug_category_register(AST_LOG_CATEGORY_ICE);
4038
4039 /*
4040 * Normnally a core module should call ast_register_cleanup,
4041 * which doesn't run if any module fails to unload. This
4042 * prevents resources being pulled out from under a running
4043 * module and possibly causing a segfault. In this case however,
4044 * the only thing we're cleaning up are the registrations of the
4045 * debug categories.
4046 */
4047 ast_register_atexit(rtp_engine_atexit);
4048
4049 return 0;
4050 }
4051
4052 time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
4053 {
4054 return rtp->last_tx;
4055 }
4056
4057 void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
4058 {
4059 rtp->last_tx = time;
4060 }
4061
4062 time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp)
4063 {
4064 return rtp->last_rx;
4065 }
4066
4067 void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
4068 {
4069 rtp->last_rx = time;
4070 }
4071
4072 unsigned int ast_rtp_instance_get_ssrc(struct ast_rtp_instance *rtp)
4073 {
4074 unsigned int ssrc = 0;
4075
4076 ao2_lock(rtp);
4077 if (rtp->engine->ssrc_get) {
4078 ssrc = rtp->engine->ssrc_get(rtp);
4079 }
4080 ao2_unlock(rtp);
4081
4082 return ssrc;
4083 }
4084
4085 const char *ast_rtp_instance_get_cname(struct ast_rtp_instance *rtp)
4086 {
4087 const char *cname = "";
4088
4089 ao2_lock(rtp);
4090 if (rtp->engine->cname_get) {
4091 cname = rtp->engine->cname_get(rtp);
4092 }
4093 ao2_unlock(rtp);
4094
4095 return cname;
4096 }
4097
4098 int ast_rtp_instance_bundle(struct ast_rtp_instance *child, struct ast_rtp_instance *parent)
4099 {
4100 int res = -1;
4101
4102 if (parent && (child->engine != parent->engine)) {
4103 return -1;
4104 }
4105
4106 ao2_lock(child);
4107 if (child->engine->bundle) {
4108 res = child->engine->bundle(child, parent);
4109 }
4110 ao2_unlock(child);
4111
4112 return res;
4113 }
4114
4115 void ast_rtp_instance_set_remote_ssrc(struct ast_rtp_instance *rtp, unsigned int ssrc)
4116 {
4117 ao2_lock(rtp);
4118 if (rtp->engine->set_remote_ssrc) {
4119 rtp->engine->set_remote_ssrc(rtp, ssrc);
4120 }
4121 ao2_unlock(rtp);
4122 }
4123
4124 void ast_rtp_instance_set_stream_num(struct ast_rtp_instance *rtp, int stream_num)
4125 {
4126 ao2_lock(rtp);
4127 if (rtp->engine->set_stream_num) {
4128 rtp->engine->set_stream_num(rtp, stream_num);
4129 }
4130 ao2_unlock(rtp);
4131 }
4132
4133 #ifdef TEST_FRAMEWORK
4134 size_t ast_rtp_instance_get_recv_buffer_max(struct ast_rtp_instance *instance)
4135 {
4136 size_t res;
4137 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4138
4139 if (!test) {
4140 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4141 return 0;
4142 }
4143
4144 ao2_lock(instance);
4145 res = test->recv_buffer_max(instance);
4146 ao2_unlock(instance);
4147
4148 return res;
4149 }
4150
4151 size_t ast_rtp_instance_get_recv_buffer_count(struct ast_rtp_instance *instance)
4152 {
4153 size_t res;
4154 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4155
4156 if (!test) {
4157 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4158 return 0;
4159 }
4160
4161 ao2_lock(instance);
4162 res = test->recv_buffer_count(instance);
4163 ao2_unlock(instance);
4164
4165 return res;
4166 }
4167
4168 size_t ast_rtp_instance_get_send_buffer_count(struct ast_rtp_instance *instance)
4169 {
4170 size_t res;
4171 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4172
4173 if (!test) {
4174 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4175 return 0;
4176 }
4177
4178 ao2_lock(instance);
4179 res = test->send_buffer_count(instance);
4180 ao2_unlock(instance);
4181
4182 return res;
4183 }
4184
4185 void ast_rtp_instance_set_schedid(struct ast_rtp_instance *instance, int id)
4186 {
4187 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4188
4189 if (!test) {
4190 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4191 return;
4192 }
4193
4194 ao2_lock(instance);
4195 test->set_schedid(instance, id);
4196 ao2_unlock(instance);
4197 }
4198
4199 void ast_rtp_instance_drop_packets(struct ast_rtp_instance *instance, int num)
4200 {
4201 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4202
4203 if (!test) {
4204 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4205 return;
4206 }
4207
4208 test->packets_to_drop = num;
4209 }
4210
4211 void ast_rtp_instance_queue_report(struct ast_rtp_instance *instance)
4212 {
4213 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4214
4215 if (!test) {
4216 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4217 return;
4218 }
4219
4220 test->send_report = 1;
4221 }
4222
4223 int ast_rtp_instance_get_sdes_received(struct ast_rtp_instance *instance)
4224 {
4225 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4226
4227 if (!test) {
4228 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4229 return 0;
4230 }
4231
4232 return test->sdes_received;
4233 }
4234
4235 void ast_rtp_instance_reset_test_engine(struct ast_rtp_instance *instance)
4236 {
4237 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
4238
4239 if (!test) {
4240 ast_log(LOG_ERROR, "There is no test engine set up!\n");
4241 return;
4242 }
4243
4244 test->packets_to_drop = 0;
4245 test->send_report = 0;
4246 test->sdes_received = 0;
4247 }
4248 #endif
4249
4250 struct ast_json *ast_rtp_convert_stats_json(const struct ast_rtp_instance_stats *stats)
4251 {
4252 struct ast_json *j_res;
4253 int ret;
4254
4255 j_res = ast_json_object_create();
4256 if (!j_res) {
4257 return NULL;
4258 }
4259
4260 /* set mandatory items */
4261 ret = ast_json_object_set(j_res, "txcount", ast_json_integer_create(stats->txcount));
4262 ret |= ast_json_object_set(j_res, "rxcount", ast_json_integer_create(stats->rxcount));
4263
4264 ret |= ast_json_object_set(j_res, "txploss", ast_json_integer_create(stats->txploss));
4265 ret |= ast_json_object_set(j_res, "rxploss", ast_json_integer_create(stats->rxploss));
4266
4267 ret |= ast_json_object_set(j_res, "local_ssrc", ast_json_integer_create(stats->local_ssrc));
4268 ret |= ast_json_object_set(j_res, "remote_ssrc", ast_json_integer_create(stats->remote_ssrc));
4269
4270 ret |= ast_json_object_set(j_res, "txoctetcount", ast_json_integer_create(stats->txoctetcount));
4271 ret |= ast_json_object_set(j_res, "rxoctetcount", ast_json_integer_create(stats->rxoctetcount));
4272
4273 ret |= ast_json_object_set(j_res, "channel_uniqueid", ast_json_string_create(stats->channel_uniqueid));
4274 if (ret) {
4275 ast_log(LOG_WARNING, "Could not create rtp statistics info. channel: %s\n", stats->channel_uniqueid);
4276 ast_json_unref(j_res);
4277 return NULL;
4278 }
4279
4280 /* set other items */
4281 SET_AST_JSON_OBJ(j_res, "txjitter", ast_json_real_create(stats->txjitter));
4282 SET_AST_JSON_OBJ(j_res, "rxjitter", ast_json_real_create(stats->rxjitter));
4283
4284 SET_AST_JSON_OBJ(j_res, "remote_maxjitter", ast_json_real_create(stats->remote_maxjitter));
4285 SET_AST_JSON_OBJ(j_res, "remote_minjitter", ast_json_real_create(stats->remote_minjitter));
4286 SET_AST_JSON_OBJ(j_res, "remote_normdevjitter", ast_json_real_create(stats->remote_normdevjitter));
4287 SET_AST_JSON_OBJ(j_res, "remote_stdevjitter", ast_json_real_create(stats->remote_stdevjitter));
4288
4289 SET_AST_JSON_OBJ(j_res, "local_maxjitter", ast_json_real_create(stats->local_maxjitter));
4290 SET_AST_JSON_OBJ(j_res, "local_minjitter", ast_json_real_create(stats->local_minjitter));
4291 SET_AST_JSON_OBJ(j_res, "local_normdevjitter", ast_json_real_create(stats->local_normdevjitter));
4292 SET_AST_JSON_OBJ(j_res, "local_stdevjitter", ast_json_real_create(stats->local_stdevjitter));
4293
4294 SET_AST_JSON_OBJ(j_res, "remote_maxrxploss", ast_json_real_create(stats->remote_maxrxploss));
4295 SET_AST_JSON_OBJ(j_res, "remote_minrxploss", ast_json_real_create(stats->remote_minrxploss));
4296 SET_AST_JSON_OBJ(j_res, "remote_normdevrxploss", ast_json_real_create(stats->remote_normdevrxploss));
4297 SET_AST_JSON_OBJ(j_res, "remote_stdevrxploss", ast_json_real_create(stats->remote_stdevrxploss));
4298
4299 SET_AST_JSON_OBJ(j_res, "local_maxrxploss", ast_json_real_create(stats->local_maxrxploss));
4300 SET_AST_JSON_OBJ(j_res, "local_minrxploss", ast_json_real_create(stats->local_minrxploss));
4301 SET_AST_JSON_OBJ(j_res, "local_normdevrxploss", ast_json_real_create(stats->local_normdevrxploss));
4302 SET_AST_JSON_OBJ(j_res, "local_stdevrxploss", ast_json_real_create(stats->local_stdevrxploss));
4303
4304 SET_AST_JSON_OBJ(j_res, "rtt", ast_json_real_create(stats->rtt));
4305 SET_AST_JSON_OBJ(j_res, "maxrtt", ast_json_real_create(stats->maxrtt));
4306 SET_AST_JSON_OBJ(j_res, "minrtt", ast_json_real_create(stats->minrtt));
4307 SET_AST_JSON_OBJ(j_res, "normdevrtt", ast_json_real_create(stats->normdevrtt));
4308 SET_AST_JSON_OBJ(j_res, "stdevrtt", ast_json_real_create(stats->stdevrtt));
4309
4310 SET_AST_JSON_OBJ(j_res, "txmes", ast_json_integer_create(stats->txmes));
4311 SET_AST_JSON_OBJ(j_res, "rxmes", ast_json_integer_create(stats->rxmes));
4312
4313 SET_AST_JSON_OBJ(j_res, "remote_maxmes", ast_json_real_create(stats->remote_maxmes));
4314 SET_AST_JSON_OBJ(j_res, "remote_minmes", ast_json_real_create(stats->remote_minmes));
4315 SET_AST_JSON_OBJ(j_res, "remote_normdevmes", ast_json_real_create(stats->remote_normdevmes));
4316 SET_AST_JSON_OBJ(j_res, "remote_stdevmes", ast_json_real_create(stats->remote_stdevmes));
4317
4318 SET_AST_JSON_OBJ(j_res, "local_maxmes", ast_json_real_create(stats->local_maxmes));
4319 SET_AST_JSON_OBJ(j_res, "local_minmes", ast_json_real_create(stats->local_minmes));
4320 SET_AST_JSON_OBJ(j_res, "local_normdevmes", ast_json_real_create(stats->local_normdevmes));
4321 SET_AST_JSON_OBJ(j_res, "local_stdevmes", ast_json_real_create(stats->local_stdevmes));
4322
4323 return j_res;
4324 }
4325
4326 struct ast_json *ast_rtp_instance_get_stats_all_json(struct ast_rtp_instance *instance)
4327 {
4328 struct ast_rtp_instance_stats stats = {0,};
4329
4330 if(ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
4331 return NULL;
4332 }
4333
4334 return ast_rtp_convert_stats_json(&stats);
4335 }
4336
4337 int ast_rtp_get_rate(const struct ast_format *format)
4338 {
4339 /* For those wondering: due to a fluke in RFC publication, G.722 is advertised
4340 * as having a sample rate of 8kHz, while implementations must know that its
4341 * real rate is 16kHz. Seriously.
4342 */
4343 return (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) ? 8000 : (int)ast_format_get_sample_rate(format);
4344 }