Asterisk - The Open Source Telephony Project  GIT-master-44aef04
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 */
195  int properties[AST_RTP_PROPERTY_MAX];
196  /*! Address that we are expecting RTP to come in to */
197  struct ast_sockaddr local_address;
198  /*! The original source address */
199  struct ast_sockaddr requested_target_address;
200  /*! Address that we are sending RTP to */
201  struct ast_sockaddr incoming_source_address;
202  /*! Instance that we are bridged to if doing remote or local bridging */
204  /*! Payload and packetization information */
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 */
219  char channel_uniqueid[AST_MAX_UNIQUEID];
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 */
225  AST_VECTOR(, enum ast_rtp_extension_direction) extmap_enabled;
226  /*! Negotiated RTP extensions (using index based on extension) */
227  AST_VECTOR(, int) extmap_negotiated;
228  /*! Negotiated RTP extensions (using index based on unique id) */
229  AST_VECTOR(, struct rtp_extmap) extmap_unique_ids;
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 */
253  struct ast_rtp_payload_type payload_type;
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;
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);
449  AST_VECTOR_FREE(&instance->extmap_negotiated);
450  AST_VECTOR_FREE(&instance->extmap_unique_ids);
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);
511  ast_sockaddr_copy(&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);
592  ast_sockaddr_copy(&instance->local_address, address);
593  ao2_unlock(instance);
594  return 0;
595 }
596 
598  const struct ast_sockaddr *address)
599 {
600  ast_sockaddr_copy(&instance->incoming_source_address, address);
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 
621  ast_sockaddr_copy(&instance->requested_target_address, address);
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) {
634  ast_sockaddr_copy(address, &instance->local_address);
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);
647  ast_sockaddr_copy(address, &instance->local_address);
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) {
656  ast_sockaddr_copy(address, &instance->requested_target_address);
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);
669  ast_sockaddr_copy(address, &instance->incoming_source_address);
670  ao2_unlock(instance);
671 }
672 
674  struct ast_sockaddr *address)
675 {
676  ao2_lock(instance);
677  ast_sockaddr_copy(address, &instance->requested_target_address);
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 */
748  if (AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, direction)) {
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)) {
760  AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
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)) {
768  AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
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)) {
778  AST_VECTOR_REPLACE(&instance->extmap_enabled, extension, AST_RTP_EXTENSION_DIRECTION_NONE);
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) &&
843  AST_VECTOR_GET(&instance->extmap_enabled, extension) != AST_RTP_EXTENSION_DIRECTION_NONE) {
844  struct rtp_extmap extmap = {
845  .extension = extension,
846  .direction = rtp_extmap_negotiate_direction(AST_VECTOR_GET(&instance->extmap_enabled, extension), direction),
847  };
848 
849  /* If the direction negotiation failed then don't accept or use this extension */
851  if (extension != AST_RTP_EXTENSION_UNSUPPORTED) {
852  AST_VECTOR_REPLACE(&instance->extmap_negotiated, extension, id);
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 
950  if (extension == AST_RTP_EXTENSION_UNSUPPORTED ||
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 
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  *
1020  * \return Nothing
1021  */
1023 {
1024  int idx;
1025  struct ast_rtp_payload_type *current;
1027  struct timeval now;
1028 
1029  if (!to_match->primary_mapping) {
1030  return;
1031  }
1032 
1033  now = ast_tvnow();
1034  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1035  current = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1036 
1037  if (!current || current == to_match || !current->primary_mapping) {
1038  continue;
1039  }
1040  if (current->asterisk_format && to_match->asterisk_format) {
1041  if (ast_format_cmp(current->format, to_match->format) == AST_FORMAT_CMP_NOT_EQUAL) {
1042  continue;
1043  }
1044  } else if (!current->asterisk_format && !to_match->asterisk_format) {
1045  if (current->rtp_code != to_match->rtp_code) {
1046  continue;
1047  }
1048  } else {
1049  continue;
1050  }
1051 
1052  /* Replace current with non-primary marked version */
1053  new_type = ast_rtp_engine_alloc_payload_type();
1054  if (!new_type) {
1055  continue;
1056  }
1057  *new_type = *current;
1058  new_type->primary_mapping = 0;
1059  new_type->when_retired = now;
1060  ao2_bump(new_type->format);
1061  AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, idx, new_type);
1062  ao2_ref(current, -1);
1063  }
1064 }
1065 
1066 /*!
1067  * \internal
1068  * \brief Put the new_type into the rx payload type mapping.
1069  * \since 14.0.0
1070  *
1071  * \param codecs Codecs structure to put new_type into
1072  * \param payload type position to replace.
1073  * \param new_type RTP payload mapping object to store.
1074  *
1075  * \note It is assumed that codecs is write locked before calling.
1076  *
1077  * \return Nothing
1078  */
1080 {
1081  ao2_ref(new_type, +1);
1082  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1083  ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_rx, payload),
1084  "cleaning up rx mapping vector element about to be replaced");
1085  }
1086  if (AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, payload, new_type)) {
1087  ao2_ref(new_type, -1);
1088  return;
1089  }
1090 
1091  payload_mapping_rx_clear_primary(codecs, new_type);
1092 }
1093 
1094 /*!
1095  * \internal
1096  * \brief Copy the rx payload type mapping to the destination.
1097  * \since 14.0.0
1098  *
1099  * \param src The source codecs structure
1100  * \param dest The destination codecs structure that the values from src will be copied to
1101  * \param instance Optionally the instance that the dst codecs structure belongs to
1102  *
1103  * \note It is assumed that src is at least read locked before calling.
1104  * \note It is assumed that dest is write locked before calling.
1105  *
1106  * \return Nothing
1107  */
1108 static void rtp_codecs_payloads_copy_rx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1109 {
1110  int idx;
1111  struct ast_rtp_payload_type *type;
1112 
1113  for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_rx); ++idx) {
1114  type = AST_VECTOR_GET(&src->payload_mapping_rx, idx);
1115  if (!type) {
1116  continue;
1117  }
1118 
1119  ast_debug(2, "Copying rx payload mapping %d (%p) from %p to %p\n",
1120  idx, type, src, dest);
1121  rtp_codecs_payload_replace_rx(dest, idx, type);
1122 
1123  if (instance && instance->engine && instance->engine->payload_set) {
1124  ao2_lock(instance);
1125  instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1126  ao2_unlock(instance);
1127  }
1128  }
1129 }
1130 
1131 /*!
1132  * \internal
1133  * \brief Determine if a type of payload is already present in mappings.
1134  * \since 14.0.0
1135  *
1136  * \param codecs Codecs to be checked for mappings.
1137  * \param to_match Payload type object to compare against.
1138  *
1139  * \note It is assumed that codecs is write locked before calling.
1140  *
1141  * \retval 0 not found
1142  * \retval 1 found
1143  */
1144 static int payload_mapping_tx_is_present(const struct ast_rtp_codecs *codecs, const struct ast_rtp_payload_type *to_match)
1145 {
1146  int idx;
1147  struct ast_rtp_payload_type *current;
1148 
1149  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1150  current = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1151 
1152  if (!current) {
1153  continue;
1154  }
1155  if (current == to_match) {
1156  /* The exact object is already in the mapping. */
1157  return 1;
1158  }
1159  if (current->asterisk_format && to_match->asterisk_format) {
1160  if (ast_format_get_codec_id(current->format) != ast_format_get_codec_id(to_match->format)) {
1161  continue;
1162  } else if (current->payload == to_match->payload) {
1163  return 0;
1164  }
1165  } else if (!current->asterisk_format && !to_match->asterisk_format) {
1166  if (current->rtp_code != to_match->rtp_code) {
1167  continue;
1168  }
1169  } else {
1170  continue;
1171  }
1172 
1173  return 1;
1174  }
1175 
1176  return 0;
1177 }
1178 
1179 /*!
1180  * \internal
1181  * \brief Copy the tx payload type mapping to the destination.
1182  * \since 14.0.0
1183  *
1184  * \param src The source codecs structure
1185  * \param dest The destination codecs structure that the values from src will be copied to
1186  * \param instance Optionally the instance that the dst codecs structure belongs to
1187  *
1188  * \note It is assumed that src is at least read locked before calling.
1189  * \note It is assumed that dest is write locked before calling.
1190  *
1191  * \return Nothing
1192  */
1193 static void rtp_codecs_payloads_copy_tx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1194 {
1195  int idx;
1196  struct ast_rtp_payload_type *type;
1197 
1198  for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
1199  type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
1200  if (!type) {
1201  continue;
1202  }
1203 
1204  ast_debug(2, "Copying tx payload mapping %d (%p) from %p to %p\n",
1205  idx, type, src, dest);
1206  ao2_ref(type, +1);
1207  if (idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx)) {
1208  ao2_t_cleanup(AST_VECTOR_GET(&dest->payload_mapping_tx, idx),
1209  "cleaning up tx mapping vector element about to be replaced");
1210  }
1211  if (AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, type)) {
1212  ao2_ref(type, -1);
1213  continue;
1214  }
1215 
1216  if (instance && instance->engine && instance->engine->payload_set) {
1217  ao2_lock(instance);
1218  instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1219  ao2_unlock(instance);
1220  }
1221  }
1222 }
1223 
1224 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1225 {
1226  int idx;
1227  struct ast_rtp_payload_type *type;
1228 
1230 
1231  /* Deadlock avoidance because of held write lock. */
1232  while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
1234  sched_yield();
1236  }
1237 
1238  /*
1239  * This represents a completely new mapping of what the remote party is
1240  * expecting for payloads, so we clear out the entire tx payload mapping
1241  * vector and replace it.
1242  */
1243  for (idx = 0; idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx); ++idx) {
1244  type = AST_VECTOR_GET(&dest->payload_mapping_tx, idx);
1245  ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
1246  AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, NULL);
1247  }
1248 
1249  rtp_codecs_payloads_copy_rx(src, dest, instance);
1250  rtp_codecs_payloads_copy_tx(src, dest, instance);
1251  dest->framing = src->framing;
1252 
1255 }
1256 
1257 void ast_rtp_codecs_payloads_xover(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
1258 {
1259  int idx;
1260  struct ast_rtp_payload_type *type;
1261 
1263  if (src != dest) {
1264  /* Deadlock avoidance because of held write lock. */
1265  while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
1267  sched_yield();
1269  }
1270  }
1271 
1272  /* Crossover copy payload type tx mapping to rx mapping. */
1273  for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
1274  type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
1275  if (!type) {
1276  continue;
1277  }
1278 
1279  /* All tx mapping elements should have the primary flag set. */
1280  ast_assert(type->primary_mapping);
1281 
1282  ast_debug(2, "Crossover copying tx to rx payload mapping %d (%p) from %p to %p\n",
1283  idx, type, src, dest);
1284  rtp_codecs_payload_replace_rx(dest, idx, type);
1285 
1286  if (instance && instance->engine && instance->engine->payload_set) {
1287  ao2_lock(instance);
1288  instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
1289  ao2_unlock(instance);
1290  }
1291  }
1292 
1293  dest->framing = src->framing;
1294 
1295  if (src != dest) {
1297  }
1299 }
1300 
1302 {
1304 
1305  if (payload < 0 || payload >= AST_RTP_MAX_PT || payload > AST_RTP_PT_LAST_STATIC) {
1306  return;
1307  }
1308 
1309  ast_rwlock_rdlock(&static_RTP_PT_lock);
1310  new_type = ao2_bump(static_RTP_PT[payload]);
1311  ast_rwlock_unlock(&static_RTP_PT_lock);
1312  if (!new_type) {
1313  ast_debug(1, "Don't have a default tx payload type %d format for m type on %p\n",
1314  payload, codecs);
1315  return;
1316  }
1317 
1318  ast_debug(1, "Setting tx payload type %d based on m type on %p\n",
1319  payload, codecs);
1320 
1321  ast_rwlock_wrlock(&codecs->codecs_lock);
1322 
1323  if (!payload_mapping_tx_is_present(codecs, new_type)) {
1324  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1325  ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload),
1326  "cleaning up replaced tx payload type");
1327  }
1328 
1329  if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, new_type)) {
1330  ao2_ref(new_type, -1);
1331  } else if (instance && instance->engine && instance->engine->payload_set) {
1332  ao2_lock(instance);
1333  instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
1334  ao2_unlock(instance);
1335  }
1336  } else {
1337  ao2_ref(new_type, -1);
1338  }
1339 
1340  ast_rwlock_unlock(&codecs->codecs_lock);
1341 }
1342 
1344  char *mimetype, char *mimesubtype,
1345  enum ast_rtp_options options,
1346  unsigned int sample_rate)
1347 {
1348  unsigned int idx;
1349  int found = 0;
1350 
1351  if (pt < 0 || pt >= AST_RTP_MAX_PT) {
1352  return -1; /* bogus payload type */
1353  }
1354 
1355  ast_rwlock_rdlock(&mime_types_lock);
1356  ast_rwlock_wrlock(&codecs->codecs_lock);
1357 
1358  for (idx = 0; idx < mime_types_len; ++idx) {
1359  const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[idx];
1361 
1362  if (strcasecmp(mimesubtype, t->subtype)) {
1363  continue;
1364  }
1365 
1366  if (strcasecmp(mimetype, t->type)) {
1367  continue;
1368  }
1369 
1370  /* if both sample rates have been supplied, and they don't match,
1371  * then this not a match; if one has not been supplied, then the
1372  * rates are not compared */
1373  if (sample_rate && t->sample_rate &&
1374  (sample_rate != t->sample_rate)) {
1375  continue;
1376  }
1377 
1378  found = 1;
1379 
1380  new_type = ast_rtp_engine_alloc_payload_type();
1381  if (!new_type) {
1382  continue;
1383  }
1384 
1386  new_type->rtp_code = t->payload_type.rtp_code;
1387  new_type->payload = pt;
1388  new_type->primary_mapping = 1;
1391  && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
1392  new_type->format = ast_format_g726_aal2;
1393  } else {
1394  new_type->format = t->payload_type.format;
1395  }
1396 
1397  if (new_type->format) {
1398  /* SDP parsing automatically increases the reference count */
1399  new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
1400  }
1401 
1402  if (!payload_mapping_tx_is_present(codecs, new_type)) {
1403  if (pt < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1404  ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, pt),
1405  "cleaning up replaced tx payload type");
1406  }
1407 
1408  if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, pt, new_type)) {
1409  ao2_ref(new_type, -1);
1410  } else if (instance && instance->engine && instance->engine->payload_set) {
1411  ao2_lock(instance);
1412  instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
1413  ao2_unlock(instance);
1414  }
1415  } else {
1416  ao2_ref(new_type, -1);
1417  }
1418 
1419  break;
1420  }
1421 
1422  ast_rwlock_unlock(&codecs->codecs_lock);
1423  ast_rwlock_unlock(&mime_types_lock);
1424 
1425  return (found ? 0 : -2);
1426 }
1427 
1428 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)
1429 {
1430  return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
1431 }
1432 
1434 {
1435  struct ast_rtp_payload_type *type;
1436 
1437  if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1438  return;
1439  }
1440 
1441  ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
1442 
1443  ast_rwlock_wrlock(&codecs->codecs_lock);
1444 
1445  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1446  type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1447  ao2_cleanup(type);
1448  AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, NULL);
1449  }
1450 
1451  if (instance && instance->engine && instance->engine->payload_set) {
1452  ao2_lock(instance);
1453  instance->engine->payload_set(instance, payload, 0, NULL, 0);
1454  ao2_unlock(instance);
1455  }
1456 
1457  ast_rwlock_unlock(&codecs->codecs_lock);
1458 }
1459 
1461 {
1462  enum ast_media_type stream_type = AST_MEDIA_TYPE_UNKNOWN;
1463  int payload;
1464  struct ast_rtp_payload_type *type;
1465 
1466  ast_rwlock_rdlock(&codecs->codecs_lock);
1467  for (payload = 0; payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++payload) {
1468  type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
1469  if (type && type->asterisk_format) {
1470  stream_type = ast_format_get_type(type->format);
1471  break;
1472  }
1473  }
1474  ast_rwlock_unlock(&codecs->codecs_lock);
1475 
1476  return stream_type;
1477 }
1478 
1480 {
1481  struct ast_rtp_payload_type *type = NULL;
1482 
1483  if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1484  return NULL;
1485  }
1486 
1487  ast_rwlock_rdlock(&codecs->codecs_lock);
1488  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1489  type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
1490  ao2_bump(type);
1491  }
1492  ast_rwlock_unlock(&codecs->codecs_lock);
1493 
1494  if (!type && payload <= AST_RTP_PT_LAST_STATIC) {
1495  ast_rwlock_rdlock(&static_RTP_PT_lock);
1496  type = ao2_bump(static_RTP_PT[payload]);
1497  ast_rwlock_unlock(&static_RTP_PT_lock);
1498  }
1499 
1500  return type;
1501 }
1502 
1504 {
1505  struct ast_rtp_payload_type *type;
1506 
1507  if (payload < 0 || payload >= AST_RTP_MAX_PT || !format) {
1508  return -1;
1509  }
1510 
1512  if (!type) {
1513  return -1;
1514  }
1515  ao2_ref(format, +1);
1516  type->format = format;
1517  type->asterisk_format = 1;
1518  type->payload = payload;
1519  type->primary_mapping = 1;
1520 
1521  ast_rwlock_wrlock(&codecs->codecs_lock);
1522  if (!payload_mapping_tx_is_present(codecs, type)) {
1523  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1524  ao2_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload));
1525  }
1526  if (AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, type)) {
1527  ao2_ref(type, -1);
1528  }
1529  } else {
1530  ao2_ref(type, -1);
1531  }
1532  ast_rwlock_unlock(&codecs->codecs_lock);
1533 
1534  return 0;
1535 }
1536 
1538 {
1539  struct ast_rtp_payload_type *type;
1540  struct ast_format *format = NULL;
1541 
1542  if (payload < 0 || payload >= AST_RTP_MAX_PT) {
1543  return NULL;
1544  }
1545 
1546  ast_rwlock_rdlock(&codecs->codecs_lock);
1547  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1548  type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1549  if (type && type->asterisk_format) {
1550  format = ao2_bump(type->format);
1551  }
1552  }
1553  ast_rwlock_unlock(&codecs->codecs_lock);
1554 
1555  return format;
1556 }
1557 
1558 void ast_rtp_codecs_set_framing(struct ast_rtp_codecs *codecs, unsigned int framing)
1559 {
1560  if (!framing) {
1561  return;
1562  }
1563 
1564  ast_rwlock_wrlock(&codecs->codecs_lock);
1565  codecs->framing = framing;
1566  ast_rwlock_unlock(&codecs->codecs_lock);
1567 }
1568 
1570 {
1571  unsigned int framing;
1572 
1573  ast_rwlock_rdlock(&codecs->codecs_lock);
1574  framing = codecs->framing;
1575  ast_rwlock_unlock(&codecs->codecs_lock);
1576 
1577  return framing;
1578 }
1579 
1580 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
1581 {
1582  int idx;
1583 
1585  *nonastformats = 0;
1586 
1587  ast_rwlock_rdlock(&codecs->codecs_lock);
1588 
1589  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1590  struct ast_rtp_payload_type *type;
1591 
1592  type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1593  if (!type) {
1594  continue;
1595  }
1596 
1597  if (type->asterisk_format) {
1598  ast_format_cap_append(astformats, type->format, 0);
1599  } else {
1600  *nonastformats |= type->rtp_code;
1601  }
1602  }
1603  if (codecs->framing) {
1604  ast_format_cap_set_framing(astformats, codecs->framing);
1605  }
1606 
1607  ast_rwlock_unlock(&codecs->codecs_lock);
1608 }
1609 
1610 /*!
1611  * \internal
1612  * \brief Find the static payload type mapping for the format.
1613  * \since 14.0.0
1614  *
1615  * \param asterisk_format Non-zero if the given Asterisk format is present
1616  * \param format Asterisk format to look for
1617  * \param code The non-Asterisk format code to look for
1618  *
1619  * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
1620  *
1621  * \retval Numerical payload type
1622  * \retval -1 if not found.
1623  */
1624 static int find_static_payload_type(int asterisk_format, const struct ast_format *format, int code)
1625 {
1626  int idx;
1627  int payload = -1;
1628 
1629  if (!asterisk_format) {
1630  for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
1631  if (static_RTP_PT[idx]
1632  && !static_RTP_PT[idx]->asterisk_format
1633  && static_RTP_PT[idx]->rtp_code == code) {
1634  payload = idx;
1635  break;
1636  }
1637  }
1638  } else if (format) {
1639  for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
1640  if (static_RTP_PT[idx]
1641  && static_RTP_PT[idx]->asterisk_format
1642  && ast_format_cmp(format, static_RTP_PT[idx]->format)
1644  payload = idx;
1645  break;
1646  }
1647  }
1648  }
1649 
1650  return payload;
1651 }
1652 
1653 /*!
1654  * \internal
1655  * \brief Find the first unused payload type in a given range
1656  *
1657  * \param codecs The codec structure to look in
1658  * \param start Starting index
1659  * \param end Ending index
1660  * \param ignore Skip these payloads types
1661  *
1662  * \note The static_RTP_PT_lock object must be locked before calling
1663  *
1664  * \retval Numerical payload type
1665  * \retval -1 if not found.
1666  */
1668  int start, int end, struct ast_rtp_payload_type *ignore[])
1669 {
1670  int x;
1671 
1672  for (x = start; x < end; ++x) {
1673  struct ast_rtp_payload_type *type;
1674 
1675  if (ignore[x]) {
1676  continue;
1677  } else if (!codecs || x >= AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
1678  return x;
1679  }
1680 
1681  type = AST_VECTOR_GET(&codecs->payload_mapping_rx, x);
1682  if (!type) {
1683  return x;
1684  }
1685  }
1686  return -1;
1687 }
1688 
1689 /*!
1690  * \internal
1691  * \brief Find an unused payload type
1692  *
1693  * \param codecs Codecs structure to look in
1694  *
1695  * \note Both static_RTP_PT_lock and codecs (if given) must be at least
1696  * read locked before calling.
1697  *
1698  * \retval Numerical payload type
1699  * \retval -1 if not found.
1700  */
1701 static int find_unused_payload(const struct ast_rtp_codecs *codecs)
1702 {
1703  int res;
1704 
1705  /* find next available dynamic payload slot */
1707  codecs, AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT, static_RTP_PT);
1708  if (res != -1) {
1709  return res;
1710  }
1711 
1713  /*
1714  * We're using default values for some dynamic types. So if an unused
1715  * slot was not found try again, but this time ignore the default
1716  * values declared for dynamic types (except for 101 and 121) .
1717  */
1718  static struct ast_rtp_payload_type *ignore[AST_RTP_MAX_PT] = {0};
1719 
1720  ignore[101] = static_RTP_PT[101];
1721  ignore[121] = static_RTP_PT[121];
1722 
1724  codecs, AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT, ignore);
1725  if (res != -1) {
1726  return res;
1727  }
1728  }
1729 
1730  /* http://www.iana.org/assignments/rtp-parameters
1731  * RFC 3551, Section 3: "[...] applications which need to define more
1732  * than 32 dynamic payload types MAY bind codes below 96, in which case
1733  * it is RECOMMENDED that unassigned payload type numbers be used
1734  * first". Updated by RFC 5761, Section 4: "[...] values in the range
1735  * 64-95 MUST NOT be used [to avoid conflicts with RTCP]". Summaries:
1736  * https://tools.ietf.org/html/draft-roach-mmusic-unified-plan#section-3.2.1.2
1737  * https://tools.ietf.org/html/draft-wu-avtcore-dynamic-pt-usage#section-3
1738  */
1741  AST_RTP_PT_LAST_REASSIGN, static_RTP_PT);
1742  if (res != -1) {
1743  return res;
1744  }
1745 
1746  /* Yet, reusing mappings below AST_RTP_PT_LAST_STATIC (35) is not supported
1747  * in Asterisk because when Compact Headers are activated, no rtpmap is
1748  * send for those below 35. If you want to use 35 and below
1749  * A) do not use Compact Headers,
1750  * B) remove that code in chan_sip/res_pjsip, or
1751  * C) add a flag that this RTP Payload Type got reassigned dynamically
1752  * and requires a rtpmap even with Compact Headers enabled.
1753  */
1755  codecs, MAX(ast_option_rtpptdynamic, 20),
1756  AST_RTP_PT_LAST_STATIC + 1, static_RTP_PT);
1757  if (res != -1) {
1758  return res;
1759  }
1760 
1762  codecs, MAX(ast_option_rtpptdynamic, 0),
1763  20, static_RTP_PT);
1764 }
1765 
1766 /*!
1767  * \internal
1768  * \brief Find the oldest non-primary dynamic rx payload type.
1769  * \since 14.0.0
1770  *
1771  * \param codecs Codecs structure to look in
1772  *
1773  * \note It is assumed that codecs is at least read locked before calling.
1774  *
1775  * \retval Numerical payload type
1776  * \retval -1 if not found.
1777  */
1779 {
1780  struct ast_rtp_payload_type *type;
1781  struct timeval oldest;
1782  int idx;
1783  int payload = -1;
1784 
1786  for (; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1787  type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1788  if (type
1789  && !type->primary_mapping
1790  && (payload == -1
1791  || ast_tvdiff_ms(type->when_retired, oldest) < 0)) {
1792  oldest = type->when_retired;
1793  payload = idx;
1794  }
1795  }
1796  return payload;
1797 }
1798 
1799 /*!
1800  * \internal
1801  * \brief Assign a payload type for the rx mapping.
1802  * \since 14.0.0
1803  *
1804  * \param codecs Codecs structure to look in
1805  * \param asterisk_format Non-zero if the given Asterisk format is present
1806  * \param format Asterisk format to look for
1807  * \param code The format to look for
1808  * \param explicit Require the provided code to be explicitly used
1809  *
1810  * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
1811  *
1812  * \retval Numerical payload type
1813  * \retval -1 if could not assign.
1814  */
1815 static int rtp_codecs_assign_payload_code_rx(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code, int explicit)
1816 {
1817  int payload = code;
1819 
1820  if (!explicit) {
1821  payload = find_static_payload_type(asterisk_format, format, code);
1822 
1823  if (payload < 0 && (!asterisk_format || !ast_option_rtpusedynamic)) {
1824  return payload;
1825  }
1826  }
1827 
1828  new_type = rtp_payload_type_alloc(format, payload, code, 1);
1829  if (!new_type) {
1830  return -1;
1831  }
1832 
1833  ast_rwlock_wrlock(&codecs->codecs_lock);
1834  if (payload > -1 && (payload < AST_RTP_PT_FIRST_DYNAMIC
1835  || AST_VECTOR_SIZE(&codecs->payload_mapping_rx) <= payload
1836  || !AST_VECTOR_GET(&codecs->payload_mapping_rx, payload))) {
1837  /*
1838  * The payload type is a static assignment
1839  * or our default dynamic position is available.
1840  */
1841  rtp_codecs_payload_replace_rx(codecs, payload, new_type);
1842  } else if (!explicit && (-1 < (payload = find_unused_payload(codecs))
1843  || -1 < (payload = rtp_codecs_find_non_primary_dynamic_rx(codecs)))) {
1844  /*
1845  * We found the first available empty dynamic position
1846  * or we found a mapping that should no longer be
1847  * actively used.
1848  */
1849  new_type->payload = payload;
1850  rtp_codecs_payload_replace_rx(codecs, payload, new_type);
1851  } else if (explicit) {
1852  /*
1853  * They explicitly requested this payload number be used but it couldn't be
1854  */
1855  payload = -1;
1856  } else {
1857  /*
1858  * There are no empty or non-primary dynamic positions
1859  * left. Sadness.
1860  *
1861  * I don't think this is really possible.
1862  */
1863  ast_log(LOG_WARNING, "No dynamic RTP payload type values available "
1864  "for %s - %d!\n", format ? ast_format_get_name(format) : "", code);
1865  }
1866  ast_rwlock_unlock(&codecs->codecs_lock);
1867 
1868  ao2_ref(new_type, -1);
1869 
1870  return payload;
1871 }
1872 
1874 {
1875  struct ast_rtp_payload_type *type;
1876  int idx;
1877  int payload = -1;
1878 
1879  ast_rwlock_rdlock(&static_RTP_PT_lock);
1880  if (!asterisk_format) {
1881  ast_rwlock_rdlock(&codecs->codecs_lock);
1882  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1883  type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1884  if (!type) {
1885  continue;
1886  }
1887 
1888  if (!type->asterisk_format
1889  && type->primary_mapping
1890  && type->rtp_code == code) {
1891  payload = idx;
1892  break;
1893  }
1894  }
1895  ast_rwlock_unlock(&codecs->codecs_lock);
1896  } else if (format) {
1897  ast_rwlock_rdlock(&codecs->codecs_lock);
1898  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
1899  type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
1900  if (!type) {
1901  continue;
1902  }
1903 
1904  if (type->asterisk_format
1905  && type->primary_mapping
1906  && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
1907  payload = idx;
1908  break;
1909  }
1910  }
1911  ast_rwlock_unlock(&codecs->codecs_lock);
1912  }
1913 
1914  if (payload < 0) {
1915  payload = rtp_codecs_assign_payload_code_rx(codecs, asterisk_format, format,
1916  code, 0);
1917  }
1918  ast_rwlock_unlock(&static_RTP_PT_lock);
1919 
1920  return payload;
1921 }
1922 
1924 {
1925  return rtp_codecs_assign_payload_code_rx(codecs, 1, format, code, 1);
1926 }
1927 
1929 {
1930  struct ast_rtp_payload_type *type;
1931  int idx;
1932  int payload = -1;
1933 
1934  if (!asterisk_format) {
1935  ast_rwlock_rdlock(&codecs->codecs_lock);
1936  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1937  type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1938  if (!type) {
1939  continue;
1940  }
1941 
1942  if (!type->asterisk_format
1943  && type->rtp_code == code) {
1944  payload = idx;
1945  break;
1946  }
1947  }
1948  ast_rwlock_unlock(&codecs->codecs_lock);
1949  } else if (format) {
1950  ast_rwlock_rdlock(&codecs->codecs_lock);
1951  for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
1952  type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
1953  if (!type) {
1954  continue;
1955  }
1956 
1957  if (type->asterisk_format
1958  && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
1959  payload = idx;
1960  break;
1961  }
1962  }
1963  ast_rwlock_unlock(&codecs->codecs_lock);
1964  }
1965 
1966  if (payload < 0) {
1967  ast_rwlock_rdlock(&static_RTP_PT_lock);
1968  payload = find_static_payload_type(asterisk_format, format, code);
1969  ast_rwlock_unlock(&static_RTP_PT_lock);
1970  }
1971 
1972  return payload;
1973 }
1974 
1976 {
1977  struct ast_rtp_payload_type *type;
1978  int res = -1;
1979 
1980  ast_rwlock_rdlock(&codecs->codecs_lock);
1981  if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
1982  type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
1983  if (type) {
1984  res = payload;
1985  }
1986  }
1987  ast_rwlock_unlock(&codecs->codecs_lock);
1988 
1989  return res;
1990 }
1991 
1993  const struct ast_format *format, int code, enum ast_rtp_options options)
1994 {
1995  int i;
1996  const char *res = "";
1997 
1998  ast_rwlock_rdlock(&mime_types_lock);
1999  for (i = 0; i < mime_types_len; i++) {
2000  if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
2001  (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
2003  (options & AST_RTP_OPT_G726_NONSTANDARD)) {
2004  res = "G726-32";
2005  break;
2006  } else {
2007  res = ast_rtp_mime_types[i].subtype;
2008  break;
2009  }
2010  } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
2012 
2013  res = ast_rtp_mime_types[i].subtype;
2014  break;
2015  }
2016  }
2017  ast_rwlock_unlock(&mime_types_lock);
2018 
2019  return res;
2020 }
2021 
2023  const struct ast_format *format, int code)
2024 {
2025  unsigned int i;
2026  unsigned int res = 0;
2027 
2028  ast_rwlock_rdlock(&mime_types_lock);
2029  for (i = 0; i < mime_types_len; ++i) {
2030  if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
2031  (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
2033  break;
2034  } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
2037  break;
2038  }
2039  }
2040  ast_rwlock_unlock(&mime_types_lock);
2041 
2042  return res;
2043 }
2044 
2045 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)
2046 {
2047  int found = 0;
2048  const char *name;
2049  if (!buf) {
2050  return NULL;
2051  }
2052 
2053 
2054  if (asterisk_format) {
2055  int x;
2056  struct ast_format *tmp_fmt;
2057  for (x = 0; x < ast_format_cap_count(ast_format_capability); x++) {
2058  tmp_fmt = ast_format_cap_get_format(ast_format_capability, x);
2059  name = ast_rtp_lookup_mime_subtype2(asterisk_format, tmp_fmt, 0, options);
2060  ao2_ref(tmp_fmt, -1);
2061  ast_str_append(&buf, 0, "%s|", name);
2062  found = 1;
2063  }
2064  } else {
2065  int x;
2066  ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
2067  for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
2068  if (rtp_capability & x) {
2069  name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
2070  ast_str_append(&buf, 0, "%s|", name);
2071  found = 1;
2072  }
2073  }
2074  }
2075 
2076  ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
2077 
2078  return ast_str_buffer(buf);
2079 }
2080 
2082 {
2083  int res;
2084 
2085  if (instance->engine->dtmf_begin) {
2086  ao2_lock(instance);
2087  res = instance->engine->dtmf_begin(instance, digit);
2088  ao2_unlock(instance);
2089  } else {
2090  res = -1;
2091  }
2092  return res;
2093 }
2094 
2096 {
2097  int res;
2098 
2099  if (instance->engine->dtmf_end) {
2100  ao2_lock(instance);
2101  res = instance->engine->dtmf_end(instance, digit);
2102  ao2_unlock(instance);
2103  } else {
2104  res = -1;
2105  }
2106  return res;
2107 }
2108 
2109 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
2110 {
2111  int res;
2112 
2113  if (instance->engine->dtmf_end_with_duration) {
2114  ao2_lock(instance);
2115  res = instance->engine->dtmf_end_with_duration(instance, digit, duration);
2116  ao2_unlock(instance);
2117  } else {
2118  res = -1;
2119  }
2120  return res;
2121 }
2122 
2124 {
2125  int res;
2126 
2127  if (instance->engine->dtmf_mode_set) {
2128  ao2_lock(instance);
2129  res = instance->engine->dtmf_mode_set(instance, dtmf_mode);
2130  ao2_unlock(instance);
2131  } else {
2132  res = -1;
2133  }
2134  return res;
2135 }
2136 
2138 {
2139  int res;
2140 
2141  if (instance->engine->dtmf_mode_get) {
2142  ao2_lock(instance);
2143  res = instance->engine->dtmf_mode_get(instance);
2144  ao2_unlock(instance);
2145  } else {
2146  res = 0;
2147  }
2148  return res;
2149 }
2150 
2152 {
2153  if (instance->engine->update_source) {
2154  ao2_lock(instance);
2155  instance->engine->update_source(instance);
2156  ao2_unlock(instance);
2157  }
2158 }
2159 
2161 {
2162  if (instance->engine->change_source) {
2163  ao2_lock(instance);
2164  instance->engine->change_source(instance);
2165  ao2_unlock(instance);
2166  }
2167 }
2168 
2169 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
2170 {
2171  int res;
2172 
2173  if (instance->engine->qos) {
2174  ao2_lock(instance);
2175  res = instance->engine->qos(instance, tos, cos, desc);
2176  ao2_unlock(instance);
2177  } else {
2178  res = -1;
2179  }
2180  return res;
2181 }
2182 
2184 {
2185  if (instance->engine->stop) {
2186  ao2_lock(instance);
2187  instance->engine->stop(instance);
2188  ao2_unlock(instance);
2189  }
2190 }
2191 
2192 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
2193 {
2194  int res;
2195 
2196  if (instance->engine->fd) {
2197  ao2_lock(instance);
2198  res = instance->engine->fd(instance, rtcp);
2199  ao2_unlock(instance);
2200  } else {
2201  res = -1;
2202  }
2203  return res;
2204 }
2205 
2207 {
2208  struct ast_rtp_glue *glue = NULL;
2209 
2211 
2212  AST_RWLIST_TRAVERSE(&glues, glue, entry) {
2213  if (!strcasecmp(glue->type, type)) {
2214  break;
2215  }
2216  }
2217 
2219 
2220  return glue;
2221 }
2222 
2223 /*!
2224  * \brief Conditionally unref an rtp instance
2225  */
2226 static void unref_instance_cond(struct ast_rtp_instance **instance)
2227 {
2228  if (*instance) {
2229  ao2_ref(*instance, -1);
2230  *instance = NULL;
2231  }
2232 }
2233 
2235 {
2236  struct ast_rtp_instance *bridged;
2237 
2238  ao2_lock(instance);
2239  bridged = instance->bridged;
2240  ao2_unlock(instance);
2241  return bridged;
2242 }
2243 
2245 {
2246  ao2_lock(instance);
2247  instance->bridged = bridged;
2248  ao2_unlock(instance);
2249 }
2250 
2252 {
2253  struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
2254  *vinstance_dst = NULL, *vinstance_src = NULL,
2255  *tinstance_dst = NULL, *tinstance_src = NULL;
2256  struct ast_rtp_glue *glue_dst, *glue_src;
2257  enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
2258  enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
2261 
2262  /* Lock both channels so we can look for the glue that binds them together */
2263  ast_channel_lock_both(c_dst, c_src);
2264 
2265  if (!cap_src || !cap_dst) {
2266  goto done;
2267  }
2268 
2269  /* Grab glue that binds each channel to something using the RTP engine */
2270  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))) {
2271  ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
2272  goto done;
2273  }
2274 
2275  audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
2276  video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
2277 
2278  audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
2279  video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
2280 
2281  /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
2282  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)) {
2283  audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
2284  }
2285  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)) {
2286  audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
2287  }
2288  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) {
2289  glue_dst->get_codec(c_dst, cap_dst);
2290  }
2291  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) {
2292  glue_src->get_codec(c_src, cap_src);
2293  }
2294 
2295  /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
2296  if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
2297  goto done;
2298  }
2299 
2300  /* Make sure we have matching codecs */
2301  if (!ast_format_cap_iscompatible(cap_dst, cap_src)) {
2302  goto done;
2303  }
2304 
2305  ast_rtp_codecs_payloads_xover(&instance_src->codecs, &instance_dst->codecs, instance_dst);
2306 
2307  if (vinstance_dst && vinstance_src) {
2308  ast_rtp_codecs_payloads_xover(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
2309  }
2310  if (tinstance_dst && tinstance_src) {
2311  ast_rtp_codecs_payloads_xover(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
2312  }
2313 
2314  if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
2315  ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
2316  ast_channel_name(c_dst), ast_channel_name(c_src));
2317  } else {
2318  ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
2319  ast_channel_name(c_dst), ast_channel_name(c_src));
2320  }
2321 
2322 done:
2323  ast_channel_unlock(c_dst);
2324  ast_channel_unlock(c_src);
2325 
2326  ao2_cleanup(cap_dst);
2327  ao2_cleanup(cap_src);
2328 
2329  unref_instance_cond(&instance_dst);
2330  unref_instance_cond(&instance_src);
2331  unref_instance_cond(&vinstance_dst);
2332  unref_instance_cond(&vinstance_src);
2333  unref_instance_cond(&tinstance_dst);
2334  unref_instance_cond(&tinstance_src);
2335 }
2336 
2338 {
2339  struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
2340  *vinstance0 = NULL, *vinstance1 = NULL,
2341  *tinstance0 = NULL, *tinstance1 = NULL;
2342  struct ast_rtp_glue *glue0, *glue1;
2343  enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
2344  enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
2347 
2348  /* If there is no second channel just immediately bail out, we are of no use in that scenario */
2349  if (!c1 || !cap1 || !cap0) {
2350  ao2_cleanup(cap0);
2351  ao2_cleanup(cap1);
2352  return -1;
2353  }
2354 
2355  /* Lock both channels so we can look for the glue that binds them together */
2356  ast_channel_lock_both(c0, c1);
2357 
2358  /* Grab glue that binds each channel to something using the RTP engine */
2360  ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
2361  goto done;
2362  }
2363 
2364  audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
2365  video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
2366 
2367  audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
2368  video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
2369 
2370  /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
2371  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)) {
2372  audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
2373  }
2374  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)) {
2375  audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
2376  }
2377  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) {
2378  glue0->get_codec(c0, cap0);
2379  }
2380  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) {
2381  glue1->get_codec(c1, cap1);
2382  }
2383 
2384  /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
2385  if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
2386  goto done;
2387  }
2388 
2389  /* Make sure we have matching codecs */
2390  if (!ast_format_cap_iscompatible(cap0, cap1)) {
2391  goto done;
2392  }
2393 
2394  /* Bridge media early */
2395  if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
2396  ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
2397  }
2398 
2399 done:
2400  ast_channel_unlock(c0);
2401  ast_channel_unlock(c1);
2402 
2403  ao2_cleanup(cap0);
2404  ao2_cleanup(cap1);
2405 
2406  unref_instance_cond(&instance0);
2407  unref_instance_cond(&instance1);
2408  unref_instance_cond(&vinstance0);
2409  unref_instance_cond(&vinstance1);
2410  unref_instance_cond(&tinstance0);
2411  unref_instance_cond(&tinstance1);
2412 
2413  ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
2414 
2415  return 0;
2416 }
2417 
2418 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
2419 {
2420  int res;
2421 
2422  if (instance->engine->red_init) {
2423  ao2_lock(instance);
2424  res = instance->engine->red_init(instance, buffer_time, payloads, generations);
2425  ao2_unlock(instance);
2426  } else {
2427  res = -1;
2428  }
2429  return res;
2430 }
2431 
2432 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
2433 {
2434  int res;
2435 
2436  if (instance->engine->red_buffer) {
2437  ao2_lock(instance);
2438  res = instance->engine->red_buffer(instance, frame);
2439  ao2_unlock(instance);
2440  } else {
2441  res = -1;
2442  }
2443  return res;
2444 }
2445 
2447 {
2448  int res;
2449 
2450  if (instance->engine->get_stat) {
2451  ao2_lock(instance);
2452  res = instance->engine->get_stat(instance, stats, stat);
2453  ao2_unlock(instance);
2454  } else {
2455  res = -1;
2456  }
2457  return res;
2458 }
2459 
2460 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
2461 {
2462  struct ast_rtp_instance_stats stats = { 0, };
2463  enum ast_rtp_instance_stat stat;
2464 
2465  /* Determine what statistics we will need to retrieve based on field passed in */
2466  if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
2468  } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
2470  } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
2472  } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
2474  } else {
2475  return NULL;
2476  }
2477 
2478  /* Attempt to actually retrieve the statistics we need to generate the quality string */
2479  if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
2480  return NULL;
2481  }
2482 
2483  /* Now actually fill the buffer with the good information */
2484  if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
2485  snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
2486  stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
2487  } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
2488  snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
2490  } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
2491  snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
2493  } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
2494  snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
2495  }
2496 
2497  return buf;
2498 }
2499 
2501 {
2502  char quality_buf[AST_MAX_USER_FIELD];
2503  char *quality;
2504  struct ast_channel *bridge;
2505 
2506  bridge = ast_channel_bridge_peer(chan);
2507  if (bridge) {
2508  ast_channel_lock_both(chan, bridge);
2510  } else {
2511  ast_channel_lock(chan);
2512  }
2514 
2516  quality_buf, sizeof(quality_buf));
2517  if (quality) {
2518  pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
2519  if (bridge) {
2520  pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
2521  }
2522  }
2523 
2524  quality = ast_rtp_instance_get_quality(instance,
2525  AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
2526  if (quality) {
2527  pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
2528  if (bridge) {
2529  pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
2530  }
2531  }
2532 
2533  quality = ast_rtp_instance_get_quality(instance,
2534  AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
2535  if (quality) {
2536  pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
2537  if (bridge) {
2538  pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
2539  }
2540  }
2541 
2542  quality = ast_rtp_instance_get_quality(instance,
2543  AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
2544  if (quality) {
2545  pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
2546  if (bridge) {
2547  pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
2548  }
2549  }
2550 
2552  ast_channel_unlock(chan);
2553  if (bridge) {
2555  ast_channel_unlock(bridge);
2556  ast_channel_unref(bridge);
2557  }
2558 }
2559 
2561 {
2562  int res;
2563 
2564  if (instance->engine->set_read_format) {
2565  ao2_lock(instance);
2566  res = instance->engine->set_read_format(instance, format);
2567  ao2_unlock(instance);
2568  } else {
2569  res = -1;
2570  }
2571  return res;
2572 }
2573 
2575 {
2576  int res;
2577 
2578  if (instance->engine->set_read_format) {
2579  ao2_lock(instance);
2580  res = instance->engine->set_write_format(instance, format);
2581  ao2_unlock(instance);
2582  } else {
2583  res = -1;
2584  }
2585  return res;
2586 }
2587 
2588 /* XXX Nothing calls this */
2589 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
2590 {
2591  struct ast_rtp_glue *glue;
2592  struct ast_rtp_instance *peer_instance = NULL;
2593  int res = -1;
2594 
2595  if (!instance->engine->make_compatible) {
2596  return -1;
2597  }
2598 
2599  ast_channel_lock(peer);
2600 
2601  if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
2602  ast_channel_unlock(peer);
2603  return -1;
2604  }
2605 
2606  glue->get_rtp_info(peer, &peer_instance);
2607  if (!peer_instance) {
2608  ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
2609  ast_channel_unlock(peer);
2610  return -1;
2611  }
2612  if (peer_instance->engine != instance->engine) {
2613  ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
2614  ast_channel_unlock(peer);
2615  ao2_ref(peer_instance, -1);
2616  return -1;
2617  }
2618 
2619  /*
2620  * XXX Good thing nothing calls this function because we would need
2621  * deadlock avoidance to get the two instance locks.
2622  */
2623  res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
2624 
2625  ast_channel_unlock(peer);
2626 
2627  ao2_ref(peer_instance, -1);
2628  peer_instance = NULL;
2629 
2630  return res;
2631 }
2632 
2633 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)
2634 {
2635  if (instance->engine->available_formats) {
2636  ao2_lock(instance);
2637  instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
2638  ao2_unlock(instance);
2639  if (ast_format_cap_count(result)) {
2640  return;
2641  }
2642  }
2643 
2644  ast_translate_available_formats(to_endpoint, to_asterisk, result);
2645 }
2646 
2648 {
2649  int res;
2650 
2651  if (instance->engine->activate) {
2652  ao2_lock(instance);
2653  res = instance->engine->activate(instance);
2654  ao2_unlock(instance);
2655  } else {
2656  res = 0;
2657  }
2658  return res;
2659 }
2660 
2662  struct ast_sockaddr *suggestion,
2663  const char *username)
2664 {
2665  if (instance->engine->stun_request) {
2666  instance->engine->stun_request(instance, suggestion, username);
2667  }
2668 }
2669 
2671 {
2672  instance->timeout = timeout;
2673 }
2674 
2676 {
2677  instance->holdtimeout = timeout;
2678 }
2679 
2680 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
2681 {
2682  instance->keepalive = interval;
2683 }
2684 
2686 {
2687  return instance->timeout;
2688 }
2689 
2691 {
2692  return instance->holdtimeout;
2693 }
2694 
2696 {
2697  return instance->keepalive;
2698 }
2699 
2701 {
2702  return instance->engine;
2703 }
2704 
2706 {
2707  return instance->glue;
2708 }
2709 
2711 {
2712  if (res_srtp || res_srtp_policy) {
2713  return -1;
2714  }
2715  if (!srtp_res || !policy_res) {
2716  return -1;
2717  }
2718 
2719  res_srtp = srtp_res;
2720  res_srtp_policy = policy_res;
2721 
2722  return 0;
2723 }
2724 
2726 {
2727  res_srtp = NULL;
2728  res_srtp_policy = NULL;
2729 }
2730 
2732 {
2733  return res_srtp && res_srtp_policy;
2734 }
2735 
2736 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)
2737 {
2738  int res = 0;
2739  struct ast_srtp **srtp;
2740 
2741  if (!res_srtp) {
2742  return -1;
2743  }
2744 
2745  ao2_lock(instance);
2746 
2747  srtp = rtcp ? &instance->rtcp_srtp : &instance->srtp;
2748 
2749  if (!*srtp) {
2750  res = res_srtp->create(srtp, instance, remote_policy);
2751  } else if (remote_policy) {
2752  res = res_srtp->replace(srtp, instance, remote_policy);
2753  }
2754  if (!res) {
2755  res = res_srtp->add_stream(*srtp, local_policy);
2756  }
2757 
2758  ao2_unlock(instance);
2759 
2760  return res;
2761 }
2762 
2763 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
2764 {
2765  if (rtcp && instance->rtcp_srtp) {
2766  return instance->rtcp_srtp;
2767  } else {
2768  return instance->srtp;
2769  }
2770 }
2771 
2772 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
2773 {
2774  int res;
2775 
2776  if (instance->engine->sendcng) {
2777  ao2_lock(instance);
2778  res = instance->engine->sendcng(instance, level);
2779  ao2_unlock(instance);
2780  } else {
2781  res = -1;
2782  }
2783  return res;
2784 }
2785 
2786 static void rtp_ice_wrap_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
2787 {
2788  ao2_lock(instance);
2789  instance->engine->ice->set_authentication(instance, ufrag, password);
2790  ao2_unlock(instance);
2791 }
2792 
2793 static void rtp_ice_wrap_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
2794 {
2795  ao2_lock(instance);
2796  instance->engine->ice->add_remote_candidate(instance, candidate);
2797  ao2_unlock(instance);
2798 }
2799 
2800 static void rtp_ice_wrap_start(struct ast_rtp_instance *instance)
2801 {
2802  ao2_lock(instance);
2803  instance->engine->ice->start(instance);
2804  ao2_unlock(instance);
2805 }
2806 
2807 static void rtp_ice_wrap_stop(struct ast_rtp_instance *instance)
2808 {
2809  ao2_lock(instance);
2810  instance->engine->ice->stop(instance);
2811  ao2_unlock(instance);
2812 }
2813 
2814 static const char *rtp_ice_wrap_get_ufrag(struct ast_rtp_instance *instance)
2815 {
2816  const char *ufrag;
2817 
2818  ao2_lock(instance);
2819  ufrag = instance->engine->ice->get_ufrag(instance);
2820  ao2_unlock(instance);
2821  return ufrag;
2822 }
2823 
2824 static const char *rtp_ice_wrap_get_password(struct ast_rtp_instance *instance)
2825 {
2826  const char *password;
2827 
2828  ao2_lock(instance);
2829  password = instance->engine->ice->get_password(instance);
2830  ao2_unlock(instance);
2831  return password;
2832 }
2833 
2835 {
2836  struct ao2_container *local_candidates;
2837 
2838  ao2_lock(instance);
2839  local_candidates = instance->engine->ice->get_local_candidates(instance);
2840  ao2_unlock(instance);
2841  return local_candidates;
2842 }
2843 
2844 static void rtp_ice_wrap_ice_lite(struct ast_rtp_instance *instance)
2845 {
2846  ao2_lock(instance);
2847  instance->engine->ice->ice_lite(instance);
2848  ao2_unlock(instance);
2849 }
2850 
2851 static void rtp_ice_wrap_set_role(struct ast_rtp_instance *instance,
2852  enum ast_rtp_ice_role role)
2853 {
2854  ao2_lock(instance);
2855  instance->engine->ice->set_role(instance, role);
2856  ao2_unlock(instance);
2857 }
2858 
2859 static void rtp_ice_wrap_turn_request(struct ast_rtp_instance *instance,
2860  enum ast_rtp_ice_component_type component, enum ast_transport transport,
2861  const char *server, unsigned int port, const char *username, const char *password)
2862 {
2863  ao2_lock(instance);
2864  instance->engine->ice->turn_request(instance, component, transport, server, port,
2865  username, password);
2866  ao2_unlock(instance);
2867 }
2868 
2870  int num_components)
2871 {
2872  ao2_lock(instance);
2873  instance->engine->ice->change_components(instance, num_components);
2874  ao2_unlock(instance);
2875 }
2876 
2879  .add_remote_candidate = rtp_ice_wrap_add_remote_candidate,
2880  .start = rtp_ice_wrap_start,
2881  .stop = rtp_ice_wrap_stop,
2882  .get_ufrag = rtp_ice_wrap_get_ufrag,
2883  .get_password = rtp_ice_wrap_get_password,
2884  .get_local_candidates = rtp_ice_wrap_get_local_candidates,
2885  .ice_lite = rtp_ice_wrap_ice_lite,
2886  .set_role = rtp_ice_wrap_set_role,
2887  .turn_request = rtp_ice_wrap_turn_request,
2888  .change_components = rtp_ice_wrap_change_components,
2889 };
2890 
2892 {
2893  if (instance->engine->ice) {
2894  return &rtp_ice_wrappers;
2895  }
2896  /* ICE not available */
2897  return NULL;
2898 }
2899 
2900 #ifdef TEST_FRAMEWORK
2901 struct ast_rtp_engine_test *ast_rtp_instance_get_test(struct ast_rtp_instance *instance)
2902 {
2903  return instance->engine->test;
2904 }
2905 #endif
2906 
2908  const struct ast_rtp_dtls_cfg *dtls_cfg)
2909 {
2910  int set_configuration;
2911 
2912  ao2_lock(instance);
2913  set_configuration = instance->engine->dtls->set_configuration(instance, dtls_cfg);
2914  ao2_unlock(instance);
2915  return set_configuration;
2916 }
2917 
2918 static int rtp_dtls_wrap_active(struct ast_rtp_instance *instance)
2919 {
2920  int active;
2921 
2922  ao2_lock(instance);
2923  active = instance->engine->dtls->active(instance);
2924  ao2_unlock(instance);
2925  return active;
2926 }
2927 
2928 static void rtp_dtls_wrap_stop(struct ast_rtp_instance *instance)
2929 {
2930  ao2_lock(instance);
2931  instance->engine->dtls->stop(instance);
2932  ao2_unlock(instance);
2933 }
2934 
2935 static void rtp_dtls_wrap_reset(struct ast_rtp_instance *instance)
2936 {
2937  ao2_lock(instance);
2938  instance->engine->dtls->reset(instance);
2939  ao2_unlock(instance);
2940 }
2941 
2943 {
2944  enum ast_rtp_dtls_connection get_connection;
2945 
2946  ao2_lock(instance);
2947  get_connection = instance->engine->dtls->get_connection(instance);
2948  ao2_unlock(instance);
2949  return get_connection;
2950 }
2951 
2953 {
2954  enum ast_rtp_dtls_setup get_setup;
2955 
2956  ao2_lock(instance);
2957  get_setup = instance->engine->dtls->get_setup(instance);
2958  ao2_unlock(instance);
2959  return get_setup;
2960 }
2961 
2962 static void rtp_dtls_wrap_set_setup(struct ast_rtp_instance *instance,
2963  enum ast_rtp_dtls_setup setup)
2964 {
2965  ao2_lock(instance);
2966  instance->engine->dtls->set_setup(instance, setup);
2967  ao2_unlock(instance);
2968 }
2969 
2971  enum ast_rtp_dtls_hash hash, const char *fingerprint)
2972 {
2973  ao2_lock(instance);
2974  instance->engine->dtls->set_fingerprint(instance, hash, fingerprint);
2975  ao2_unlock(instance);
2976 }
2977 
2979 {
2980  enum ast_rtp_dtls_hash get_fingerprint_hash;
2981 
2982  ao2_lock(instance);
2983  get_fingerprint_hash = instance->engine->dtls->get_fingerprint_hash(instance);
2984  ao2_unlock(instance);
2985  return get_fingerprint_hash;
2986 }
2987 
2988 static const char *rtp_dtls_wrap_get_fingerprint(struct ast_rtp_instance *instance)
2989 {
2990  const char *get_fingerprint;
2991 
2992  ao2_lock(instance);
2993  get_fingerprint = instance->engine->dtls->get_fingerprint(instance);
2994  ao2_unlock(instance);
2995  return get_fingerprint;
2996 }
2997 
3000  .active = rtp_dtls_wrap_active,
3001  .stop = rtp_dtls_wrap_stop,
3002  .reset = rtp_dtls_wrap_reset,
3003  .get_connection = rtp_dtls_wrap_get_connection,
3004  .get_setup = rtp_dtls_wrap_get_setup,
3005  .set_setup = rtp_dtls_wrap_set_setup,
3006  .set_fingerprint = rtp_dtls_wrap_set_fingerprint,
3007  .get_fingerprint_hash = rtp_dtls_wrap_get_fingerprint_hash,
3008  .get_fingerprint = rtp_dtls_wrap_get_fingerprint,
3009 };
3010 
3012 {
3013  if (instance->engine->dtls) {
3014  return &rtp_dtls_wrappers;
3015  }
3016  /* DTLS not available */
3017  return NULL;
3018 }
3019 
3020 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
3021 {
3022  if (!strcasecmp(name, "dtlsenable")) {
3023  dtls_cfg->enabled = ast_true(value) ? 1 : 0;
3024  } else if (!strcasecmp(name, "dtlsverify")) {
3025  if (!strcasecmp(value, "yes")) {
3027  } else if (!strcasecmp(value, "fingerprint")) {
3029  } else if (!strcasecmp(value, "certificate")) {
3031  } else if (!strcasecmp(value, "no")) {
3032  dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
3033  } else {
3034  return -1;
3035  }
3036  } else if (!strcasecmp(name, "dtlsrekey")) {
3037  if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
3038  return -1;
3039  }
3040  } else if (!strcasecmp(name, "dtlsautogeneratecert")) {
3041  dtls_cfg->ephemeral_cert = ast_true(value) ? 1 : 0;
3042  } else if (!strcasecmp(name, "dtlscertfile")) {
3043  if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3044  ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3045  return -1;
3046  }
3047  ast_free(dtls_cfg->certfile);
3048  dtls_cfg->certfile = ast_strdup(value);
3049  } else if (!strcasecmp(name, "dtlsprivatekey")) {
3050  if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3051  ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3052  return -1;
3053  }
3054  ast_free(dtls_cfg->pvtfile);
3055  dtls_cfg->pvtfile = ast_strdup(value);
3056  } else if (!strcasecmp(name, "dtlscipher")) {
3057  ast_free(dtls_cfg->cipher);
3058  dtls_cfg->cipher = ast_strdup(value);
3059  } else if (!strcasecmp(name, "dtlscafile")) {
3060  if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3061  ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3062  return -1;
3063  }
3064  ast_free(dtls_cfg->cafile);
3065  dtls_cfg->cafile = ast_strdup(value);
3066  } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
3067  if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
3068  ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
3069  return -1;
3070  }
3071  ast_free(dtls_cfg->capath);
3072  dtls_cfg->capath = ast_strdup(value);
3073  } else if (!strcasecmp(name, "dtlssetup")) {
3074  if (!strcasecmp(value, "active")) {
3076  } else if (!strcasecmp(value, "passive")) {
3078  } else if (!strcasecmp(value, "actpass")) {
3080  }
3081  } else if (!strcasecmp(name, "dtlsfingerprint")) {
3082  if (!strcasecmp(value, "sha-256")) {
3083  dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
3084  } else if (!strcasecmp(value, "sha-1")) {
3085  dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
3086  }
3087  } else {
3088  return -1;
3089  }
3090 
3091  return 0;
3092 }
3093 
3095 {
3096  if (dtls_cfg->ephemeral_cert) {
3097  if (!ast_strlen_zero(dtls_cfg->certfile)) {
3098  ast_log(LOG_ERROR, "You cannot request automatically generated certificates"
3099  " (dtls_auto_generate_cert) and also specify a certificate file"
3100  " (dtls_cert_file) at the same time\n");
3101  return -1;
3102  } else if (!ast_strlen_zero(dtls_cfg->pvtfile)
3103  || !ast_strlen_zero(dtls_cfg->cafile)
3104  || !ast_strlen_zero(dtls_cfg->capath)) {
3105  ast_log(LOG_NOTICE, "dtls_pvt_file, dtls_cafile, and dtls_ca_path are"
3106  " ignored when dtls_auto_generate_cert is enabled\n");
3107  }
3108  }
3109 
3110  return 0;
3111 }
3112 
3113 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
3114 {
3115  ast_rtp_dtls_cfg_free(dst_cfg); /* Prevent a double-call leaking memory via ast_strdup */
3116 
3117  dst_cfg->enabled = src_cfg->enabled;
3118  dst_cfg->verify = src_cfg->verify;
3119  dst_cfg->rekey = src_cfg->rekey;
3120  dst_cfg->suite = src_cfg->suite;
3121  dst_cfg->hash = src_cfg->hash;
3122  dst_cfg->ephemeral_cert = src_cfg->ephemeral_cert;
3123  dst_cfg->certfile = ast_strdup(src_cfg->certfile);
3124  dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
3125  dst_cfg->cipher = ast_strdup(src_cfg->cipher);
3126  dst_cfg->cafile = ast_strdup(src_cfg->cafile);
3127  dst_cfg->capath = ast_strdup(src_cfg->capath);
3128  dst_cfg->default_setup = src_cfg->default_setup;
3129 }
3130 
3132 {
3133  ast_free(dtls_cfg->certfile);
3134  dtls_cfg->certfile = NULL;
3135  ast_free(dtls_cfg->pvtfile);
3136  dtls_cfg->pvtfile = NULL;
3137  ast_free(dtls_cfg->cipher);
3138  dtls_cfg->cipher = NULL;
3139  ast_free(dtls_cfg->cafile);
3140  dtls_cfg->cafile = NULL;
3141  ast_free(dtls_cfg->capath);
3142  dtls_cfg->capath = NULL;
3143 }
3144 
3145 /*! \internal
3146  * \brief Small helper routine that cleans up entry i in
3147  * \c ast_rtp_mime_types.
3148  */
3150 {
3151  ao2_cleanup(ast_rtp_mime_types[i].payload_type.format);
3152  memset(&ast_rtp_mime_types[i], 0, sizeof(struct ast_rtp_mime_type));
3153 }
3154 
3155 static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
3156 {
3157  int x;
3158 
3159  ast_rwlock_wrlock(&mime_types_lock);
3160 
3161  x = mime_types_len;
3162  if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
3163  ast_rwlock_unlock(&mime_types_lock);
3164  return;
3165  }
3166 
3167  /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
3168  memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));
3169  if (format) {
3172  } else {
3174  }
3175  ast_copy_string(ast_rtp_mime_types[x].type, type, sizeof(ast_rtp_mime_types[x].type));
3176  ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
3177  ast_rtp_mime_types[x].sample_rate = sample_rate;
3178  mime_types_len++;
3179 
3180  ast_rwlock_unlock(&mime_types_lock);
3181 }
3182 
3183 static void add_static_payload(int payload, struct ast_format *format, int rtp_code)
3184 {
3185  struct ast_rtp_payload_type *type;
3186 
3187  /*
3188  * ARRAY_LEN's result is cast to an int so 'map' is not autocast to a size_t,
3189  * which if negative would cause an assertion.
3190  */
3191  ast_assert(payload < (int)ARRAY_LEN(static_RTP_PT));
3192 
3193  if (ast_option_rtpusedynamic && payload < 0) {
3194  /*
3195  * We're going to build dynamic payloads dynamically. An RTP code is
3196  * required otherwise one will be dynamically allocated per instance.
3197  */
3198  return;
3199  }
3200 
3201  /*
3202  * Either the given payload is truly a static type, or Asterisk is
3203  * globally storing the dynamic payloads in the static_RTP_PT object.
3204  */
3205  ast_rwlock_wrlock(&static_RTP_PT_lock);
3206 
3207  if (payload < 0) {
3208  /*
3209  * This is a dynamic payload that will be stored globally,
3210  * so find the next available empty slot.
3211  */
3212  payload = find_unused_payload(NULL);
3213  if (payload < 0) {
3214  ast_log(LOG_WARNING, "No dynamic RTP payload type values available "
3215  "for %s - %d!\n", format ? ast_format_get_name(format) : "", rtp_code);
3216  ast_rwlock_unlock(&static_RTP_PT_lock);
3217  return;
3218  }
3219  }
3220 
3221  type = rtp_payload_type_alloc(format, payload, rtp_code, 1);
3222  if (type) {
3223  ao2_cleanup(static_RTP_PT[payload]);
3224  static_RTP_PT[payload] = type;
3225  }
3226  ast_rwlock_unlock(&static_RTP_PT_lock);
3227 }
3228 
3230 {
3231  char *codec_name = ast_strdupa(ast_format_get_codec_name(format));
3232 
3233  codec_name = ast_str_to_upper(codec_name);
3234 
3235  set_next_mime_type(format,
3236  0,
3238  codec_name,
3239  ast_format_get_sample_rate(format));
3240  add_static_payload(-1, format, 0);
3241 
3242  return 0;
3243 }
3244 
3246 {
3247  int x;
3248  int y = 0;
3249 
3250  ast_rwlock_wrlock(&static_RTP_PT_lock);
3251  /* remove everything pertaining to this format id from the lists */
3252  for (x = 0; x < AST_RTP_MAX_PT; x++) {
3253  if (static_RTP_PT[x]
3254  && ast_format_cmp(static_RTP_PT[x]->format, format) == AST_FORMAT_CMP_EQUAL) {
3255  ao2_ref(static_RTP_PT[x], -1);
3256  static_RTP_PT[x] = NULL;
3257  }
3258  }
3259  ast_rwlock_unlock(&static_RTP_PT_lock);
3260 
3261  ast_rwlock_wrlock(&mime_types_lock);
3262  /* rebuild the list skipping the items matching this id */
3263  for (x = 0; x < mime_types_len; x++) {
3264  if (ast_format_cmp(ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
3266  continue;
3267  }
3268  if (x != y) {
3270  }
3271  y++;
3272  }
3273  mime_types_len = y;
3274  ast_rwlock_unlock(&mime_types_lock);
3275  return 0;
3276 }
3277 
3278 /*!
3279  * \internal
3280  * \brief \ref stasis message payload for RTCP messages
3281  */
3283  struct ast_channel_snapshot *snapshot; /*< The channel snapshot, if available */
3284  struct ast_rtp_rtcp_report *report; /*< The RTCP report */
3285  struct ast_json *blob; /*< Extra JSON data to publish */
3286 };
3287 
3288 static void rtcp_message_payload_dtor(void *obj)
3289 {
3290  struct rtcp_message_payload *payload = obj;
3291 
3292  ao2_cleanup(payload->report);
3293  ao2_cleanup(payload->snapshot);
3294  ast_json_unref(payload->blob);
3295 }
3296 
3298 {
3299  struct rtcp_message_payload *payload = stasis_message_data(msg);
3300  RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
3301  RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
3302  unsigned int ssrc = payload->report->ssrc;
3303  unsigned int type = payload->report->type;
3304  unsigned int report_count = payload->report->reception_report_count;
3305  int i;
3306 
3307  if (!packet_string) {
3308  return NULL;
3309  }
3310 
3311  if (payload->snapshot) {
3312  channel_string = ast_manager_build_channel_state_string(payload->snapshot);
3313  if (!channel_string) {
3314  return NULL;
3315  }
3316  }
3317 
3318  if (payload->blob) {
3319  /* Optional data */
3320  struct ast_json *to = ast_json_object_get(payload->blob, "to");
3321  struct ast_json *from = ast_json_object_get(payload->blob, "from");
3322  struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
3323  if (to) {
3324  ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
3325  }
3326  if (from) {
3327  ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
3328  }
3329  if (rtt) {
3330  ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
3331  }
3332  }
3333 
3334  ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
3335  ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
3336  ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
3337  if (type == AST_RTP_RTCP_SR) {
3338  ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
3339  (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
3340  (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
3341  ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
3343  ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
3345  ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
3347  }
3348 
3349  for (i = 0; i < report_count; i++) {
3350  RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
3351 
3352  if (!payload->report->report_block[i]) {
3353  break;
3354  }
3355 
3356  report_string = ast_str_create(256);
3357  if (!report_string) {
3358  return NULL;
3359  }
3360 
3361  ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
3362  i, payload->report->report_block[i]->source_ssrc);
3363  ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
3364  i, payload->report->report_block[i]->lost_count.fraction);
3365  ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
3366  i, payload->report->report_block[i]->lost_count.packets);
3367  ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
3368  i, payload->report->report_block[i]->highest_seq_no & 0xffff);
3369  ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
3370  i, payload->report->report_block[i]->highest_seq_no >> 16);
3371  ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
3372  i, payload->report->report_block[i]->ia_jitter);
3373  ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
3374  i, payload->report->report_block[i]->lsr);
3375  ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
3376  i, ((double)payload->report->report_block[i]->dlsr) / 65536);
3377  ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
3378  }
3379 
3381  stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
3382  "%s%s",
3383  AS_OR(channel_string, ""),
3384  ast_str_buffer(packet_string));
3385 }
3386 
3387 static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
3388  const struct stasis_message_sanitizer *sanitize)
3389 {
3390  struct rtcp_message_payload *payload = stasis_message_data(msg);
3391  struct ast_json *json_rtcp_report = NULL;
3392  struct ast_json *json_rtcp_report_blocks;
3393  struct ast_json *json_rtcp_sender_info = NULL;
3394  struct ast_json *json_channel = NULL;
3395  int i;
3396 
3397  json_rtcp_report_blocks = ast_json_array_create();
3398  if (!json_rtcp_report_blocks) {
3399  return NULL;
3400  }
3401 
3402  for (i = 0; i < payload->report->reception_report_count && payload->report->report_block[i]; i++) {
3403  struct ast_json *json_report_block;
3404  char str_lsr[32];
3405 
3406  snprintf(str_lsr, sizeof(str_lsr), "%u", payload->report->report_block[i]->lsr);
3407  json_report_block = ast_json_pack("{s: I, s: I, s: I, s: I, s: I, s: s, s: I}",
3408  "source_ssrc", (ast_json_int_t)payload->report->report_block[i]->source_ssrc,
3409  "fraction_lost", (ast_json_int_t)payload->report->report_block[i]->lost_count.fraction,
3410  "packets_lost", (ast_json_int_t)payload->report->report_block[i]->lost_count.packets,
3411  "highest_seq_no", (ast_json_int_t)payload->report->report_block[i]->highest_seq_no,
3412  "ia_jitter", (ast_json_int_t)payload->report->report_block[i]->ia_jitter,
3413  "lsr", str_lsr,
3414  "dlsr", (ast_json_int_t)payload->report->report_block[i]->dlsr);
3415  if (!json_report_block
3416  || ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
3417  ast_json_unref(json_rtcp_report_blocks);
3418  return NULL;
3419  }
3420  }
3421 
3422  if (payload->report->type == AST_RTP_RTCP_SR) {
3423  char sec[32];
3424  char usec[32];
3425 
3426  snprintf(sec, sizeof(sec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec);
3427  snprintf(usec, sizeof(usec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
3428  json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: I, s: I, s: I}",
3429  "ntp_timestamp_sec", sec,
3430  "ntp_timestamp_usec", usec,
3431  "rtp_timestamp", (ast_json_int_t)payload->report->sender_information.rtp_timestamp,
3432  "packets", (ast_json_int_t)payload->report->sender_information.packet_count,
3433  "octets", (ast_json_int_t)payload->report->sender_information.octet_count);
3434  if (!json_rtcp_sender_info) {
3435  ast_json_unref(json_rtcp_report_blocks);
3436  return NULL;
3437  }
3438  }
3439 
3440  json_rtcp_report = ast_json_pack("{s: I, s: I, s: i, s: o, s: o}",
3441  "ssrc", (ast_json_int_t)payload->report->ssrc,
3442  "type", (ast_json_int_t)payload->report->type,
3443  "report_count", payload->report->reception_report_count,
3444  "sender_information", json_rtcp_sender_info ?: ast_json_null(),
3445  "report_blocks", json_rtcp_report_blocks);
3446  if (!json_rtcp_report) {
3447  return NULL;
3448  }
3449 
3450  if (payload->snapshot) {
3451  json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
3452  if (!json_channel) {
3453  ast_json_unref(json_rtcp_report);
3454  return NULL;
3455  }
3456  }
3457 
3458  return ast_json_pack("{s: o?, s: o, s: O?}",
3459  "channel", json_channel,
3460  "rtcp_report", json_rtcp_report,
3461  "blob", payload->blob);
3462 }
3463 
3464 static void rtp_rtcp_report_dtor(void *obj)
3465 {
3466  int i;
3467  struct ast_rtp_rtcp_report *rtcp_report = obj;
3468 
3469  for (i = 0; i < rtcp_report->reception_report_count; i++) {
3470  ast_free(rtcp_report->report_block[i]);
3471  }
3472 }
3473 
3474 struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
3475 {
3476  struct ast_rtp_rtcp_report *rtcp_report;
3477 
3478  /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
3479  rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
3481 
3482  return rtcp_report;
3483 }
3484 
3486  struct stasis_message_type *message_type,
3487  struct ast_rtp_rtcp_report *report,
3488  struct ast_json *blob)
3489 {
3490  RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
3492 
3493  if (!message_type) {
3494  return;
3495  }
3496 
3497  payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
3498  if (!payload || !report) {
3499  return;
3500  }
3501 
3502  if (!ast_strlen_zero(rtp->channel_uniqueid)) {
3503  payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
3504  }
3505  if (blob) {
3506  payload->blob = blob;
3507  ast_json_ref(blob);
3508  }
3509  ao2_ref(report, +1);
3510  payload->report = report;
3511 
3512  message = stasis_message_create(message_type, payload);
3513  if (!message) {
3514  return;
3515  }
3516 
3518 }
3519 
3520 /*!
3521  * @{ \brief Define RTCP/RTP message types.
3522  */
3525  .to_json = rtcp_report_to_json,);
3528  .to_json = rtcp_report_to_json,);
3529 /*! @} */
3530 
3532 {
3533  return rtp_topic;
3534 }
3535 
3536 static uintmax_t debug_category_rtp_id;
3537 
3539 {
3540  return debug_category_rtp_id;
3541 }
3542 
3544 
3546 {
3548 }
3549 
3550 static uintmax_t debug_category_rtcp_id;
3551 
3553 {
3554  return debug_category_rtcp_id;
3555 }
3556 
3558 
3560 {
3562 }
3563 
3564 static uintmax_t debug_category_dtls_id;
3565 
3567 {
3568  return debug_category_dtls_id;
3569 }
3570 
3572 
3574 {
3576 }
3577 
3578 static uintmax_t debug_category_ice_id;
3579 
3581 {
3582  return debug_category_ice_id;
3583 }
3584 
3585 static void rtp_engine_shutdown(void)
3586 {
3587  int x;
3588 
3589  ao2_cleanup(rtp_topic);
3590  rtp_topic = NULL;
3593 
3594  ast_rwlock_wrlock(&static_RTP_PT_lock);
3595  for (x = 0; x < AST_RTP_MAX_PT; x++) {
3596  ao2_cleanup(static_RTP_PT[x]);
3597  static_RTP_PT[x] = NULL;
3598  }
3599  ast_rwlock_unlock(&static_RTP_PT_lock);
3600 
3601  ast_rwlock_wrlock(&mime_types_lock);
3602  for (x = 0; x < mime_types_len; x++) {
3603  if (ast_rtp_mime_types[x].payload_type.format) {
3605  }
3606  }
3607  mime_types_len = 0;
3608  ast_rwlock_unlock(&mime_types_lock);
3609 
3611 
3614 
3617 
3620 }
3621 
3623 {
3624  ast_rwlock_init(&mime_types_lock);
3625  ast_rwlock_init(&static_RTP_PT_lock);
3626 
3627  rtp_topic = stasis_topic_create("rtp:all");
3628  if (!rtp_topic) {
3629  return -1;
3630  }
3634 
3635  /* Define all the RTP mime types available */
3636  set_next_mime_type(ast_format_g723, 0, "audio", "G723", 8000);
3637  set_next_mime_type(ast_format_gsm, 0, "audio", "GSM", 8000);
3638  set_next_mime_type(ast_format_ulaw, 0, "audio", "PCMU", 8000);
3639  set_next_mime_type(ast_format_ulaw, 0, "audio", "G711U", 8000);
3640  set_next_mime_type(ast_format_alaw, 0, "audio", "PCMA", 8000);
3641  set_next_mime_type(ast_format_alaw, 0, "audio", "G711A", 8000);
3642  set_next_mime_type(ast_format_g726, 0, "audio", "G726-32", 8000);
3643  set_next_mime_type(ast_format_adpcm, 0, "audio", "DVI4", 8000);
3644  set_next_mime_type(ast_format_slin, 0, "audio", "L16", 8000);
3645  set_next_mime_type(ast_format_slin16, 0, "audio", "L16", 16000);
3646  set_next_mime_type(ast_format_slin16, 0, "audio", "L16-256", 16000);
3647  set_next_mime_type(ast_format_slin12, 0, "audio", "L16", 12000);
3648  set_next_mime_type(ast_format_slin24, 0, "audio", "L16", 24000);
3649  set_next_mime_type(ast_format_slin32, 0, "audio", "L16", 32000);
3650  set_next_mime_type(ast_format_slin44, 0, "audio", "L16", 44000);
3651  set_next_mime_type(ast_format_slin48, 0, "audio", "L16", 48000);
3652  set_next_mime_type(ast_format_slin96, 0, "audio", "L16", 96000);
3653  set_next_mime_type(ast_format_slin192, 0, "audio", "L16", 192000);
3654  set_next_mime_type(ast_format_lpc10, 0, "audio", "LPC", 8000);
3655  set_next_mime_type(ast_format_g729, 0, "audio", "G729", 8000);
3656  set_next_mime_type(ast_format_g729, 0, "audio", "G729A", 8000);
3657  set_next_mime_type(ast_format_g729, 0, "audio", "G.729", 8000);
3658  set_next_mime_type(ast_format_speex, 0, "audio", "speex", 8000);
3659  set_next_mime_type(ast_format_speex16, 0, "audio", "speex", 16000);
3660  set_next_mime_type(ast_format_speex32, 0, "audio", "speex", 32000);
3661  set_next_mime_type(ast_format_ilbc, 0, "audio", "iLBC", 8000);
3662  /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
3663  set_next_mime_type(ast_format_g722, 0, "audio", "G722", 8000);
3664  set_next_mime_type(ast_format_g726_aal2, 0, "audio", "AAL2-G726-32", 8000);
3665  set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
3666  set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
3667  set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
3668  set_next_mime_type(ast_format_jpeg, 0, "video", "JPEG", 90000);
3669  set_next_mime_type(ast_format_png, 0, "video", "PNG", 90000);
3670  set_next_mime_type(ast_format_h261, 0, "video", "H261", 90000);
3671  set_next_mime_type(ast_format_h263, 0, "video", "H263", 90000);
3672  set_next_mime_type(ast_format_h263p, 0, "video", "h263-1998", 90000);
3673  set_next_mime_type(ast_format_h264, 0, "video", "H264", 90000);
3674  set_next_mime_type(ast_format_h265, 0, "video", "H265", 90000);
3675  set_next_mime_type(ast_format_mp4, 0, "video", "MP4V-ES", 90000);
3676  set_next_mime_type(ast_format_t140_red, 0, "text", "RED", 1000);
3677  set_next_mime_type(ast_format_t140, 0, "text", "T140", 1000);
3678  set_next_mime_type(ast_format_siren7, 0, "audio", "G7221", 16000);
3679  set_next_mime_type(ast_format_siren14, 0, "audio", "G7221", 32000);
3680  set_next_mime_type(ast_format_g719, 0, "audio", "G719", 48000);
3681  /* Opus, VP8, and VP9 */
3682  set_next_mime_type(ast_format_opus, 0, "audio", "opus", 48000);
3683  set_next_mime_type(ast_format_vp8, 0, "video", "VP8", 90000);
3684  set_next_mime_type(ast_format_vp9, 0, "video", "VP9", 90000);
3685 
3686  /* Define the static rtp payload mappings */
3688  #ifdef USE_DEPRECATED_G726
3689  add_static_payload(2, ast_format_g726, 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
3690  #endif
3693  add_static_payload(5, ast_format_adpcm, 0);/* 8 kHz */
3694  add_static_payload(6, ast_format_adpcm, 0); /* 16 kHz */
3698  add_static_payload(10, ast_format_slin, 0); /* 2 channels */
3699  add_static_payload(11, ast_format_slin, 0); /* 1 channel */
3701  add_static_payload(16, ast_format_adpcm, 0); /* 11.025 kHz */
3702  add_static_payload(17, ast_format_adpcm, 0); /* 22.050 kHz */
3704  add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
3708 
3709  /*
3710  * Dynamic payload types - Even when dynamically assigning them we'll fall
3711  * back to using the statically declared values as the default number.
3712  */
3715 
3722  add_static_payload(105, ast_format_t140_red, 0); /* Real time text chat (with redundancy encoding) */
3723  add_static_payload(106, ast_format_t140, 0); /* Real time text chat */
3727 
3731 
3735  add_static_payload(118, ast_format_slin16, 0); /* 16 Khz signed linear */
3737 
3738  add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
3745  /* payload types above 127 are not valid */
3746 
3754 
3755  return 0;
3756 }
3757 
3759 {
3760  return rtp->last_tx;
3761 }
3762 
3763 void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
3764 {
3765  rtp->last_tx = time;
3766 }
3767 
3769 {
3770  return rtp->last_rx;
3771 }
3772 
3773 void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
3774 {
3775  rtp->last_rx = time;
3776 }
3777 
3779 {
3780  unsigned int ssrc = 0;
3781 
3782  ao2_lock(rtp);
3783  if (rtp->engine->ssrc_get) {
3784  ssrc = rtp->engine->ssrc_get(rtp);
3785  }
3786  ao2_unlock(rtp);
3787 
3788  return ssrc;
3789 }
3790 
3792 {
3793  const char *cname = "";
3794 
3795  ao2_lock(rtp);
3796  if (rtp->engine->cname_get) {
3797  cname = rtp->engine->cname_get(rtp);
3798  }
3799  ao2_unlock(rtp);
3800 
3801  return cname;
3802 }
3803 
3805 {
3806  int res = -1;
3807 
3808  if (parent && (child->engine != parent->engine)) {
3809  return -1;
3810  }
3811 
3812  ao2_lock(child);
3813  if (child->engine->bundle) {
3814  res = child->engine->bundle(child, parent);
3815  }
3816  ao2_unlock(child);
3817 
3818  return res;
3819 }
3820 
3821 void ast_rtp_instance_set_remote_ssrc(struct ast_rtp_instance *rtp, unsigned int ssrc)
3822 {
3823  ao2_lock(rtp);
3824  if (rtp->engine->set_remote_ssrc) {
3825  rtp->engine->set_remote_ssrc(rtp, ssrc);
3826  }
3827  ao2_unlock(rtp);
3828 }
3829 
3830 void ast_rtp_instance_set_stream_num(struct ast_rtp_instance *rtp, int stream_num)
3831 {
3832  ao2_lock(rtp);
3833  if (rtp->engine->set_stream_num) {
3834  rtp->engine->set_stream_num(rtp, stream_num);
3835  }
3836  ao2_unlock(rtp);
3837 }
3838 
3839 #ifdef TEST_FRAMEWORK
3840 size_t ast_rtp_instance_get_recv_buffer_max(struct ast_rtp_instance *instance)
3841 {
3842  size_t res;
3843  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3844 
3845  if (!test) {
3846  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3847  return 0;
3848  }
3849 
3850  ao2_lock(instance);
3851  res = test->recv_buffer_max(instance);
3852  ao2_unlock(instance);
3853 
3854  return res;
3855 }
3856 
3857 size_t ast_rtp_instance_get_recv_buffer_count(struct ast_rtp_instance *instance)
3858 {
3859  size_t res;
3860  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3861 
3862  if (!test) {
3863  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3864  return 0;
3865  }
3866 
3867  ao2_lock(instance);
3868  res = test->recv_buffer_count(instance);
3869  ao2_unlock(instance);
3870 
3871  return res;
3872 }
3873 
3874 size_t ast_rtp_instance_get_send_buffer_count(struct ast_rtp_instance *instance)
3875 {
3876  size_t res;
3877  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3878 
3879  if (!test) {
3880  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3881  return 0;
3882  }
3883 
3884  ao2_lock(instance);
3885  res = test->send_buffer_count(instance);
3886  ao2_unlock(instance);
3887 
3888  return res;
3889 }
3890 
3891 void ast_rtp_instance_set_schedid(struct ast_rtp_instance *instance, int id)
3892 {
3893  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3894 
3895  if (!test) {
3896  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3897  return;
3898  }
3899 
3900  ao2_lock(instance);
3901  test->set_schedid(instance, id);
3902  ao2_unlock(instance);
3903 }
3904 
3905 void ast_rtp_instance_drop_packets(struct ast_rtp_instance *instance, int num)
3906 {
3907  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3908 
3909  if (!test) {
3910  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3911  return;
3912  }
3913 
3914  test->packets_to_drop = num;
3915 }
3916 
3917 void ast_rtp_instance_queue_report(struct ast_rtp_instance *instance)
3918 {
3919  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3920 
3921  if (!test) {
3922  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3923  return;
3924  }
3925 
3926  test->send_report = 1;
3927 }
3928 
3929 int ast_rtp_instance_get_sdes_received(struct ast_rtp_instance *instance)
3930 {
3931  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3932 
3933  if (!test) {
3934  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3935  return 0;
3936  }
3937 
3938  return test->sdes_received;
3939 }
3940 
3941 void ast_rtp_instance_reset_test_engine(struct ast_rtp_instance *instance)
3942 {
3943  struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3944 
3945  if (!test) {
3946  ast_log(LOG_ERROR, "There is no test engine set up!\n");
3947  return;
3948  }
3949 
3950  test->packets_to_drop = 0;
3951  test->send_report = 0;
3952  test->sdes_received = 0;
3953 }
3954 #endif
3955 
3957 {
3958  struct ast_json *j_res;
3959  int ret;
3960 
3961  j_res = ast_json_object_create();
3962  if (!j_res) {
3963  return NULL;
3964  }
3965 
3966  /* set mandatory items */
3967  ret = ast_json_object_set(j_res, "txcount", ast_json_integer_create(stats->txcount));
3968  ret |= ast_json_object_set(j_res, "rxcount", ast_json_integer_create(stats->rxcount));
3969 
3970  ret |= ast_json_object_set(j_res, "txploss", ast_json_integer_create(stats->txploss));
3971  ret |= ast_json_object_set(j_res, "rxploss", ast_json_integer_create(stats->rxploss));
3972 
3973  ret |= ast_json_object_set(j_res, "local_ssrc", ast_json_integer_create(stats->local_ssrc));
3974  ret |= ast_json_object_set(j_res, "remote_ssrc", ast_json_integer_create(stats->remote_ssrc));
3975 
3976  ret |= ast_json_object_set(j_res, "txoctetcount", ast_json_integer_create(stats->txoctetcount));
3977  ret |= ast_json_object_set(j_res, "rxoctetcount", ast_json_integer_create(stats->rxoctetcount));
3978 
3979  ret |= ast_json_object_set(j_res, "channel_uniqueid", ast_json_string_create(stats->channel_uniqueid));
3980  if (ret) {
3981  ast_log(LOG_WARNING, "Could not create rtp statistics info. channel: %s\n", stats->channel_uniqueid);
3982  ast_json_unref(j_res);
3983  return NULL;
3984  }
3985 
3986  /* set other items */
3987  SET_AST_JSON_OBJ(j_res, "txjitter", ast_json_real_create(stats->txjitter));
3988  SET_AST_JSON_OBJ(j_res, "rxjitter", ast_json_real_create(stats->rxjitter));
3989 
3990  SET_AST_JSON_OBJ(j_res, "remote_maxjitter", ast_json_real_create(stats->remote_maxjitter));
3991  SET_AST_JSON_OBJ(j_res, "remote_minjitter", ast_json_real_create(stats->remote_minjitter));
3992  SET_AST_JSON_OBJ(j_res, "remote_normdevjitter", ast_json_real_create(stats->remote_normdevjitter));
3993  SET_AST_JSON_OBJ(j_res, "remote_stdevjitter", ast_json_real_create(stats->remote_stdevjitter));
3994 
3995  SET_AST_JSON_OBJ(j_res, "local_maxjitter", ast_json_real_create(stats->local_maxjitter));
3996  SET_AST_JSON_OBJ(j_res, "local_minjitter", ast_json_real_create(stats->local_minjitter));
3997  SET_AST_JSON_OBJ(j_res, "local_normdevjitter", ast_json_real_create(stats->local_normdevjitter));
3998  SET_AST_JSON_OBJ(j_res, "local_stdevjitter", ast_json_real_create(stats->local_stdevjitter));
3999 
4000  SET_AST_JSON_OBJ(j_res, "remote_maxrxploss", ast_json_real_create(stats->remote_maxrxploss));
4001  SET_AST_JSON_OBJ(j_res, "remote_minrxploss", ast_json_real_create(stats->remote_minrxploss));
4002  SET_AST_JSON_OBJ(j_res, "remote_normdevrxploss", ast_json_real_create(stats->remote_normdevrxploss));
4003  SET_AST_JSON_OBJ(j_res, "remote_stdevrxploss", ast_json_real_create(stats->remote_stdevrxploss));
4004 
4005  SET_AST_JSON_OBJ(j_res, "local_maxrxploss", ast_json_real_create(stats->local_maxrxploss));
4006  SET_AST_JSON_OBJ(j_res, "local_minrxploss", ast_json_real_create(stats->local_minrxploss));
4007  SET_AST_JSON_OBJ(j_res, "local_normdevrxploss", ast_json_real_create(stats->local_normdevrxploss));
4008  SET_AST_JSON_OBJ(j_res, "local_stdevrxploss", ast_json_real_create(stats->local_stdevrxploss));
4009 
4010  SET_AST_JSON_OBJ(j_res, "rtt", ast_json_real_create(stats->rtt));
4011  SET_AST_JSON_OBJ(j_res, "maxrtt", ast_json_real_create(stats->maxrtt));
4012  SET_AST_JSON_OBJ(j_res, "minrtt", ast_json_real_create(stats->minrtt));
4013  SET_AST_JSON_OBJ(j_res, "normdevrtt", ast_json_real_create(stats->normdevrtt));
4014  SET_AST_JSON_OBJ(j_res, "stdevrtt", ast_json_real_create(stats->stdevrtt));
4015 
4016  return j_res;
4017 }
4018 
4020 {
4021  struct ast_rtp_instance_stats stats = {0,};
4022 
4023  if(ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
4024  return NULL;
4025  }
4026 
4027  return ast_rtp_convert_stats_json(&stats);
4028 }
4029 
4031 {
4032  /* For those wondering: due to a fluke in RFC publication, G.722 is advertised
4033  * as having a sample rate of 8kHz, while implementations must know that its
4034  * real rate is 16kHz. Seriously.
4035  */
4036  return (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) ? 8000 : (int)ast_format_get_sample_rate(format);
4037 }
uintmax_t ast_debug_category_rtcp_packet_id(void)
Definition: rtp_engine.c:3559
static enum ast_rtp_dtls_connection rtp_dtls_wrap_get_connection(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2942
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
struct stasis_message_type * ast_rtp_rtcp_sent_type(void)
Message type for an RTCP message sent from this Asterisk instance.
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:2574
struct ast_format * ast_format_vp8
Built-in cached vp8 format.
Definition: format_cache.c:191
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:2891
int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
Send a comfort noise packet to the RTP instance.
Definition: rtp_engine.c:2772
static const char type[]
Definition: chan_ooh323.c:109
int(* activate)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:673
struct ast_format * ast_format_g726
Built-in cached g726 format.
Definition: format_cache.c:111
Struct containing info for an AMI event to send out.
Definition: manager.h:491
An object that represents data sent during a SR/RR RTCP report.
Definition: rtp_engine.h:331
int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
Initialize an RTP codecs structure.
Definition: rtp_engine.c:958
char digit
void ast_rtp_instance_extmap_clear(struct ast_rtp_instance *instance)
Clear negotiated RTP extension information.
Definition: rtp_engine.c:862
#define ast_rwlock_rdlock(a)
Definition: lock.h:233
#define ast_channel_lock(chan)
Definition: channel.h:2902
int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
Unregister an RTP engine.
Definition: rtp_engine.c:364
Main Channel structure associated with a channel.
char type[16]
The media type.
Definition: rtp_engine.c:255
int(* bundle)(struct ast_rtp_instance *child, struct ast_rtp_instance *parent)
Definition: rtp_engine.h:685
static int mime_types_len
Definition: rtp_engine.c:262
int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
Buffer a frame in an RTP instance for RED.
Definition: rtp_engine.c:2432
struct ast_format * ast_format_g723
Built-in cached g723.1 format.
Definition: format_cache.c:146
struct ast_format * ast_format_siren14
Built-in cached siren14 format.
Definition: format_cache.c:211
void(* prop_set)(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
Definition: rtp_engine.h:641
void(* payload_set)(struct ast_rtp_instance *instance, int payload, int asterisk_format, struct ast_format *format, int code)
Definition: rtp_engine.h:643
unsigned int cos
Definition: chan_iax2.c:352
void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
Set the RTP keepalive interval.
Definition: rtp_engine.c:2680
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
static void unref_instance_cond(struct ast_rtp_instance **instance)
Conditionally unref an rtp instance.
Definition: rtp_engine.c:2226
#define ao2_t_cleanup(obj, tag)
Definition: astobj2.h:1959
ast_rtp_options
Definition: rtp_engine.h:142
int(* set_configuration)(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
Definition: rtp_engine.h:572
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
Asterisk locking-related definitions:
static void rtp_instance_set_incoming_source_address_nolock(struct ast_rtp_instance *instance, const struct ast_sockaddr *address)
Definition: rtp_engine.c:597
static int find_unused_payload_in_range(const struct ast_rtp_codecs *codecs, int start, int end, struct ast_rtp_payload_type *ignore[])
Definition: rtp_engine.c:1667
Asterisk main include file. File version handling, generic pbx functions.
int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt, char *mimetype, char *mimesubtype, enum ast_rtp_options options, unsigned int sample_rate)
Set tx payload type to a known MIME media type for a codec with a specific sample rate...
Definition: rtp_engine.c:1343
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:1558
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:2500
unsigned int framing
Definition: rtp_engine.h:713
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
struct ast_format * ast_format_slin192
Built-in cached signed linear 192kHz format.
Definition: format_cache.c:81
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
static enum ast_rtp_dtls_hash rtp_dtls_wrap_get_fingerprint_hash(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2978
struct ast_format * ast_format_mp4
Built-in cached mp4 format.
Definition: format_cache.c:186
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:1923
#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:332
int(* set_write_format)(struct ast_rtp_instance *instance, struct ast_format *format)
Definition: rtp_engine.h:667
struct ast_format * ast_format_t140_red
Built-in cached t140 red format.
Definition: format_cache.c:236
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:2700
void *(* extended_prop_get)(struct ast_rtp_instance *instance, int property)
Definition: rtp_engine.h:639
String manipulation functions.
unsigned int highest_seq_no
Definition: rtp_engine.h:322
int ast_rtp_engine_unload_format(struct ast_format *format)
Formats requiring the use of a format attribute interface must have that interface registered in orde...
Definition: rtp_engine.c:3245
enum ast_rtp_dtmf_mode(* dtmf_mode_get)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:649
static struct ast_rtp_engine_dtls rtp_dtls_wrappers
Definition: rtp_engine.c:2998
const char *(* get_password)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:497
static int quality
Definition: codec_speex.c:62
static void rtp_ice_wrap_set_role(struct ast_rtp_instance *instance, enum ast_rtp_ice_role role)
Definition: rtp_engine.c:2851
const char *(* get_fingerprint)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:590
char * ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
Retrieve quality statistics about an RTP instance.
Definition: rtp_engine.c:2460
int ast_rtp_codecs_payload_code_tx(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
Retrieve a tx mapped payload type based on whether it is an Asterisk format and the code...
Definition: rtp_engine.c:1928
void ast_rtp_engine_unregister_srtp(void)
Definition: rtp_engine.c:2725
unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
Get the framing used for a set of codecs.
Definition: rtp_engine.c:1569
int(* sendcng)(struct ast_rtp_instance *instance, int level)
Definition: rtp_engine.h:679
char subtype[64]
The format type.
Definition: rtp_engine.c:257
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)
Record tx payload type information that was seen in an a=rtpmap: SDP line.
Definition: rtp_engine.c:1428
int(* write)(struct ast_rtp_instance *instance, struct ast_frame *frame)
Definition: rtp_engine.h:624
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2938
static ast_rwlock_t mime_types_lock
Definition: rtp_engine.c:261
Support for translation of data formats. translate.c.
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:1460
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:171
#define ast_rwlock_destroy(rwlock)
Definition: lock.h:231
int ast_rtp_engine_init(void)
initializes the rtp engine arrays
Definition: rtp_engine.c:3622
Time-related functions and macros.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
char * address
Definition: f2c.h:59
const char * ast_format_get_codec_name(const struct ast_format *format)
Get the codec name associated with a format.
Definition: format.c:339
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:2251
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1501
struct ast_format * ast_format_slin32
Built-in cached signed linear 32kHz format.
Definition: format_cache.c:61
uintmax_t ast_debug_category_rtp_id(void)
Definition: rtp_engine.c:3538
ast_transport
Definition: netsock2.h:59
static void rtp_engine_shutdown(void)
Definition: rtp_engine.c:3585
const char * ast_rtp_instance_get_cname(struct ast_rtp_instance *rtp)
Retrieve the CNAME used in RTCP SDES items.
Definition: rtp_engine.c:3791
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
static struct ast_rtp_mime_type ast_rtp_mime_types[128]
struct ast_format * ast_format_slin96
Built-in cached signed linear 96kHz format.
Definition: format_cache.c:76
struct ast_frame *(* read)(struct ast_rtp_instance *instance, int rtcp)
Definition: rtp_engine.h:661
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
uintmax_t ast_debug_category_dtls_packet_id(void)
Definition: rtp_engine.c:3573
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
Definition: rtp_engine.c:2710
#define LOG_WARNING
Definition: logger.h:274
enum ast_rtp_extension ast_rtp_instance_extmap_get_extension(struct ast_rtp_instance *instance, int id)
Retrieve the extension for an RTP extension id.
Definition: rtp_engine.c:910
#define AST_RTP_PT_LAST_STATIC
Definition: rtp_engine.h:89
static void rtp_codecs_payloads_copy_rx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
Definition: rtp_engine.c:1108
int ast_file_is_readable(const char *filename)
Test that a file exists and is readable by the effective user.
Definition: main/utils.c:2726
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
AST_JSON_INT_T ast_json_int_t
Primarily used to cast when packing to an "I" type.
Definition: json.h:87
static void rtp_codecs_payload_replace_rx(struct ast_rtp_codecs *codecs, int payload, struct ast_rtp_payload_type *new_type)
Definition: rtp_engine.c:1079
static void instance_destructor(void *obj)
Definition: rtp_engine.c:423
static int rtp_codecs_find_non_primary_dynamic_rx(struct ast_rtp_codecs *codecs)
Definition: rtp_engine.c:1778
int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
Register an RTP engine.
Definition: rtp_engine.c:330
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:1537
unsigned int txcount
Definition: rtp_engine.h:368
void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
Stop an RTP instance.
Definition: rtp_engine.c:2183
static int timeout
Definition: cdr_mysql.c:86
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static const char * rtp_dtls_wrap_get_fingerprint(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2988
void(* stop)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:626
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 void rtp_ice_wrap_change_components(struct ast_rtp_instance *instance, int num_components)
Definition: rtp_engine.c:2869
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
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
const char * ast_rtp_lookup_mime_subtype2(const int asterisk_format, const struct ast_format *format, int code, enum ast_rtp_options options)
Retrieve mime subtype information on a payload.
Definition: rtp_engine.c:1992
Structure representing a snapshot of channel state.
int(* dtmf_end)(struct ast_rtp_instance *instance, char digit)
Definition: rtp_engine.h:630
struct ast_format * ast_format_jpeg
Built-in cached jpeg format.
Definition: format_cache.c:201
static void rtp_ice_wrap_stop(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2807
static void rtp_dtls_wrap_stop(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2928
unsigned short reception_report_count
Definition: rtp_engine.h:332
unsigned int ast_rtp_instance_get_ssrc(struct ast_rtp_instance *rtp)
Retrieve the local SSRC value that we will be using.
Definition: rtp_engine.c:3778
struct ast_format * ast_format_g726_aal2
Built-in cached g726 aal2 format.
Definition: format_cache.c:116
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:2234
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
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
struct ast_rtp_rtcp_report_block * report_block[0]
Definition: rtp_engine.h:344
Definition: sched.c:76
unsigned int rxploss
Definition: rtp_engine.h:394
static const char desc[]
Definition: cdr_mysql.c:73
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:1580
int(* update_peer)(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, const struct ast_format_cap *cap, int nat_active)
Definition: rtp_engine.h:756
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
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
Codec API.
struct ast_rtp_rtcp_report_block::@308 lost_count
const char *(* cname_get)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:683
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:2137
static struct ast_rtp_engine_ice rtp_ice_wrappers
Definition: rtp_engine.c:2877
void(* change_source)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:635
void(* change_components)(struct ast_rtp_instance *instance, int num_components)
Definition: rtp_engine.h:509
void(* add_remote_candidate)(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
Definition: rtp_engine.h:489
Definition of a media format.
Definition: format.c:43
static struct ast_json * rtcp_report_to_json(struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize)
Definition: rtp_engine.c:3387
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
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:1193
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(* stop)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:576
static void rtp_dtls_wrap_reset(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2935
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:2793
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:2022
int ast_rtp_engine_load_format(struct ast_format *format)
Custom formats declared in codecs.conf at startup must be communicated to the rtp_engine so their mim...
Definition: rtp_engine.c:3229
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
ast_rtp_extension
Known RTP extensions.
Definition: rtp_engine.h:542
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
A report block within a SR/RR report.
Definition: rtp_engine.h:316
static struct ast_manager_event_blob * rtcp_report_to_ami(struct stasis_message *msg)
Definition: rtp_engine.c:3297
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:650
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.
Definition: rtp_engine.c:705
#define ao2_unlock(a)
Definition: astobj2.h:730
uintmax_t ast_debug_category_rtp_packet_id(void)
Definition: rtp_engine.c:3545
struct ast_rtp_payload_type * ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
Retrieve rx payload mapped information by payload type.
Definition: rtp_engine.c:1479
struct ast_format * ast_format_slin24
Built-in cached signed linear 24kHz format.
Definition: format_cache.c:56
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
Set the RTP timeout value for when the instance is on hold.
Definition: rtp_engine.c:2675
void(* stop)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:493
#define AST_RTP_PT_FIRST_DYNAMIC
Definition: rtp_engine.h:92
void ast_format_cap_set_framing(struct ast_format_cap *cap, unsigned int framing)
Set the global framing.
Definition: format_cap.c:136
void(* update_source)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:633
int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
Get the file descriptor for an RTP session (or RTCP)
Definition: rtp_engine.c:2192
#define NULL
Definition: resample.c:96
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
Definition: rtp_engine.c:2109
ast_rwlock_t codecs_lock
Definition: rtp_engine.h:707
char * end
Definition: eagi_proxy.c:73
struct ast_format * ast_format_g722
Built-in cached g722 format.
Definition: format_cache.c:106
void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
Copy contents of a DTLS configuration structure.
Definition: rtp_engine.c:3113
int value
Definition: syslog.c:37
unsigned int rtp_timestamp
Definition: rtp_engine.h:337
void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
Set the channel that owns this RTP instance.
Definition: rtp_engine.c:553
static struct ast_srtp_res srtp_res
Definition: res_srtp.c:102
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
int(* add_stream)(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
Definition: res_srtp.h:42
#define ast_rwlock_unlock(a)
Definition: lock.h:232
int(* make_compatible)(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1)
Definition: rtp_engine.h:669
Structure for an ICE candidate.
Definition: rtp_engine.h:474
unsigned int sample_rate
Expected sample rate of the /c subtype.
Definition: rtp_engine.c:259
Socket address structure.
Definition: netsock2.h:97
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
void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the local address that we are expecting RTP on.
Definition: rtp_engine.c:643
static struct ast_str * password
Definition: cdr_mysql.c:77
int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
Destroy an RTP instance.
Definition: rtp_engine.c:458
#define ast_verb(level,...)
Definition: logger.h:455
enum ast_rtp_dtls_hash(* get_fingerprint_hash)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:588
const char * type
Definition: rtp_engine.h:722
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:9727
int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
Retrieve statistics about an RTP instance.
Definition: rtp_engine.c:2446
Structure containing callbacks for Stasis message sanitization.
Definition: stasis.h:200
uintmax_t ast_debug_category_ice_id(void)
Definition: rtp_engine.c:3580
enum ast_rtp_dtls_setup(* get_setup)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:582
struct stasis_message_type * ast_rtp_rtcp_received_type(void)
Message type for an RTCP message received from some external source.
time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
Get the last RTP transmission time.
Definition: rtp_engine.c:3758
void(* remote_address_set)(struct ast_rtp_instance *instance, struct ast_sockaddr *sa)
Definition: rtp_engine.h:645
void(* 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.h:505
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
void ast_translate_available_formats(struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result)
Find available formats.
Definition: translate.c:1565
Utility functions.
Media Format API.
struct ast_format * ast_format_h261
Built-in cached h261 format.
Definition: format_cache.c:161
unsigned int tos
Definition: chan_iax2.c:351
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
static void rtp_rtcp_report_dtor(void *obj)
Definition: rtp_engine.c:3464
struct ast_format * ast_format_g719
Built-in cached g719 format.
Definition: format_cache.c:156
struct ast_rtp_engine_dtls * ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
Obtain a pointer to the DTLS support present on an RTP instance.
Definition: rtp_engine.c:3011
static void add_static_payload(int payload, struct ast_format *format, int rtp_code)
Definition: rtp_engine.c:3183
enum ast_rtp_extension_direction ast_rtp_instance_extmap_get_direction(struct ast_rtp_instance *instance, int id)
Retrieve the negotiated direction for an RTP extension id.
Definition: rtp_engine.c:930
int done
Definition: test_amihooks.c:48
int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
Get the RTP timeout value.
Definition: rtp_engine.c:2685
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
#define ao2_bump(obj)
Definition: astobj2.h:491
static int rtp_dtls_wrap_active(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2918
struct ast_format * ast_format_h263p
Built-in cached h263 plus format.
Definition: format_cache.c:171
struct ast_frame * ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
Receive a frame over RTP.
Definition: rtp_engine.c:578
struct ast_format * ast_format_lpc10
Built-in cached ilbc format.
Definition: format_cache.c:126
int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
Register RTP glue.
Definition: rtp_engine.c:379
#define AST_LOG_CATEGORY_DTLS
Definition: rtp_engine.h:2832
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:2045
enum ast_rtp_dtls_connection(* get_connection)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:580
#define AST_RTP_MAX
Definition: rtp_engine.h:272
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
int(* destroy)(struct ast_rtp_instance *instance)
Definition: rtp_engine.h:622
#define ast_rwlock_tryrdlock(a)
Definition: lock.h:235
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
struct ast_format * ast_format_slin12
Built-in cached signed linear 12kHz format.
Definition: format_cache.c:46
void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
Free contents of a DTLS configuration structure.
Definition: rtp_engine.c:3131
int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
Begin sending a DTMF digit.
Definition: rtp_engine.c:2081
struct ast_rtp_payload_type payload_type
A mapping object between the Asterisk codec and this RTP payload.
Definition: rtp_engine.c:253
struct ast_rtp_engine_dtls * dtls
Definition: rtp_engine.h:693
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the requested target address of the remote endpoint.
Definition: rtp_engine.c:673
STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,.to_ami=rtcp_report_to_ami,.to_json=rtcp_report_to_json,)
Define RTCP/RTP message types.
int(* red_buffer)(struct ast_rtp_instance *instance, struct ast_frame *frame)
Definition: rtp_engine.h:659
static uintmax_t debug_category_dtls_packet_id
Definition: rtp_engine.c:3571
#define MAX(a, b)
Definition: utils.h:228
static uintmax_t debug_category_rtp_id
Definition: rtp_engine.c:3536
#define AST_RTP_MAX_PT
Definition: rtp_engine.h:83
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
unsigned short fraction
Definition: rtp_engine.h:319
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
Indicate to the RTP engine that packets are now expected to be sent/received on the RTP instance...
Definition: rtp_engine.c:2647
int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
Get the RTP keepalive interval.
Definition: rtp_engine.c:2695
int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int payload)
Search for the tx payload type in the ast_rtp_codecs structure.
Definition: rtp_engine.c:1975
General Asterisk PBX channel definitions.
Asterisk JSON abstraction layer.
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
Set QoS parameters on an RTP session.
Definition: rtp_engine.c:2169
enum ast_rtp_glue_result(* get_rtp_info)(struct ast_channel *chan, struct ast_rtp_instance **instance)
Callback for retrieving the RTP instance carrying audio.
Definition: rtp_engine.h:729
struct ast_rtp_payload_type * ast_rtp_engine_alloc_payload_type(void)
Allocation routine for ast_rtp_payload_type.
Definition: rtp_engine.c:325
struct ast_format * ast_format_opus
Built-in cached opus format.
Definition: format_cache.c:221
int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
Send a frame out over RTP.
Definition: rtp_engine.c:568
ast_rtp_instance_stat
Definition: rtp_engine.h:180
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:851
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
Maximum number of RTP properties supported.
Definition: rtp_engine.h:138
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
void(* available_formats)(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
Definition: rtp_engine.h:677
ast_rtp_ice_role
ICE role during negotiation.
Definition: rtp_engine.h:468
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
void(* set_fingerprint)(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
Definition: rtp_engine.h:586
void ast_rtp_instance_set_stream_num(struct ast_rtp_instance *rtp, int stream_num)
Set the stream number for an RTP instance.
Definition: rtp_engine.c:3830
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_rtp_glue * glue
Definition: rtp_engine.c:213
static int find_static_payload_type(int asterisk_format, const struct ast_format *format, int code)
Definition: rtp_engine.c:1624
Scheduler Routines (derived from cheops)
int(* new)(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *sa, void *data)
Definition: rtp_engine.h:620
static void rtp_ice_wrap_ice_lite(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2844
structure to hold extensions
struct ast_format * ast_format_gsm
Built-in cached gsm format.
Definition: format_cache.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
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
struct ast_srtp_res * res_srtp
Definition: rtp_engine.c:177
char channel_uniqueid[MAX_CHANNEL_ID]
Definition: rtp_engine.h:426
#define ao2_lock(a)
Definition: astobj2.h:718
ast_rtp_ice_component_type
ICE component types.
Definition: rtp_engine.h:462
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static int find_unused_payload(const struct ast_rtp_codecs *codecs)
Definition: rtp_engine.c:1701
void * ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
Get the value of an RTP instance extended property.
Definition: rtp_engine.c:690
static uintmax_t debug_category_rtcp_id
Definition: rtp_engine.c:3550
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
Set the RTP timeout value.
Definition: rtp_engine.c:2670
A set of macros to manage forward-linked lists.
enum ast_rtp_dtls_hash hash
Definition: rtp_engine.h:559
struct ast_rtp_glue * ast_rtp_instance_get_glue(const char *type)
Get the RTP glue that binds a channel to the RTP engine.
Definition: rtp_engine.c:2206
void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
Indicate that the RTP marker bit should be set on an RTP stream.
Definition: rtp_engine.c:2151
void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
Clear rx and tx payload mapping information from an RTP instance.
Definition: rtp_engine.c:994
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:3763
#define AST_VECTOR(name, type)
Define a vector structure.
Definition: vector.h:44
struct ast_format * ast_format_speex16
Built-in cached speex at 16kHz format.
Definition: format_cache.c:136
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:617
int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
Request that the underlying RTP engine make two RTP instances compatible with eachother.
Definition: rtp_engine.c:2589
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:2970
struct ast_format * ast_format_png
Built-in cached png format.
Definition: format_cache.c:206
unsigned int txoctetcount
Definition: rtp_engine.h:428
ast_rtp_glue_result
Definition: rtp_engine.h:158
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
void(* get_codec)(struct ast_channel *chan, struct ast_format_cap *result_cap)
Callback for retrieving codecs that the channel can do. Result returned in result_cap.
Definition: rtp_engine.h:761
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
static struct ast_srtp_policy_res policy_res
Definition: res_srtp.c:114
struct ast_str * ast_manager_build_channel_state_string(const struct ast_channel_snapshot *snapshot)
Generate the AMI message body from a channel snapshot.
static const char * rtp_ice_wrap_get_ufrag(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2814
Network socket handling.
Core PBX routines and definitions.
int ast_rtp_get_rate(const struct ast_format *format)
Retrieve the sample rate of a format according to RTP specifications.
Definition: rtp_engine.c:4030
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
Structure that represents the optional DTLS SRTP support within an RTP engine.
Definition: rtp_engine.h:570
static void rtp_ice_wrap_start(struct ast_rtp_instance *instance)
Definition: rtp_engine.c:2800
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:3804
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:525
unsigned int rxcount
Definition: rtp_engine.h:370
static uintmax_t debug_category_rtp_packet_id
Definition: rtp_engine.c:3543
struct ast_rtp_rtcp_report::@309 sender_information
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
Format Capabilities API.
enum ast_rtp_extension_direction direction
Definition: rtp_engine.c:185
struct ast_srtp * srtp
Definition: rtp_engine.c:215
static struct stasis_topic * rtp_topic
Stasis Message Bus API topic for RTP related messages
Definition: rtp_engine.c:278
ast_rtp_dtls_setup
DTLS setup types.
Definition: rtp_engine.h:513
struct ast_sockaddr incoming_source_address
Definition: rtp_engine.c:201
struct ast_module * mod
Definition: rtp_engine.h:618
static force_inline char * ast_str_to_upper(char *str)
Convert a string to all upper-case.
Definition: strings.h:1289
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define AST_LOG_CATEGORY_RTCP_PACKET
Definition: rtp_engine.h:2830
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.