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