| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2009, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Joshua Colp <jcolp@digium.com> | 
					
						
							|  |  |  |  * Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \file | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \brief Multicast RTP Engine | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \author Joshua Colp <jcolp@digium.com> | 
					
						
							|  |  |  |  * \author Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com> | 
					
						
							| 
									
										
										
										
											2009-10-30 04:08:39 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \ingroup rtp_engines | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:28:54 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <math.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/frame.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/acl.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/cli.h"
 | 
					
						
							|  |  |  | #include "asterisk/manager.h"
 | 
					
						
							|  |  |  | #include "asterisk/unaligned.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/rtp_engine.h"
 | 
					
						
							| 
									
										
											  
											
												media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.
Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.
Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.
Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.
Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).
For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite
Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.
There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).
Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178
ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464) 
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000) 
  chan_obscure.diff uploaded by file (License 5000) 
  jingle.diff uploaded by file (License 5000) 
  funcs.diff uploaded by file (License 5000) 
  formats.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  bridges.diff uploaded by file (License 5000) 
  mf-codecs-2.diff uploaded by file (License 5000) 
  mf-app_fax.diff uploaded by file (License 5000) 
  mf-apps-3.diff uploaded by file (License 5000) 
  media-formats-3.diff uploaded by file (License 5000) 
ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)
  
ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283) 
  mf-attributes-3.diff uploaded by file (License 5000) 
ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909) 
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283) 
  rb3747.patch uploaded by jrose (License 6182)
ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283) 
  rb3751.patch uploaded by coreyfarrell (License 5909) 
  chan_sip-3.diff uploaded by file (License 5000) 
ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273) 
  pjsip-direct-media.diff uploaded by file (License 5000) 
  format_cap_remove.diff uploaded by opticron (License 6273) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  chan_pjsip-2.diff uploaded by file (License 5000) 
ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000) 
  
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621) 
  moh_cleanup.diff uploaded by opticron (License 6273) 
  bridge_leak.diff uploaded by opticron (License 6273) 
  translate.diff uploaded by file (License 5000) 
  rb3795.patch uploaded by rmudgett (License 5621) 
  tls_fix.diff uploaded by mjordan (License 6283) 
  fax-mf-fix-2.diff uploaded by file (License 5000) 
  rtp_transfer_stuff uploaded by mjordan (License 6283) 
  rb3787.patch uploaded by rmudgett (License 5621) 
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000) 
  format_cache_case_fix.diff uploaded by opticron (License 6273) 
  rb3774.patch uploaded by rmudgett (License 5621) 
  rb3775.patch uploaded by rmudgett (License 5621) 
  rtp_engine_fix.diff uploaded by opticron (License 6273) 
  rtp_crash_fix.diff uploaded by opticron (License 6273) 
  rb3753.patch uploaded by mjordan (License 6283) 
  rb3750.patch uploaded by mjordan (License 6283) 
  rb3748.patch uploaded by rmudgett (License 5621) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  rb3740.patch uploaded by mjordan (License 6283) 
  rb3739.patch uploaded by mjordan (License 6283) 
  rb3734.patch uploaded by mjordan (License 6283) 
  rb3689.patch uploaded by mjordan (License 6283) 
  rb3674.patch uploaded by coreyfarrell (License 5909) 
  rb3671.patch uploaded by coreyfarrell (License 5909) 
  rb3667.patch uploaded by coreyfarrell (License 5909) 
  rb3665.patch uploaded by mjordan (License 6283) 
  rb3625.patch uploaded by coreyfarrell (License 5909) 
  rb3602.patch uploaded by coreyfarrell (License 5909) 
  format_compatibility-2.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2014-07-20 22:06:33 +00:00
										 |  |  | #include "asterisk/format_cache.h"
 | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | #include "asterisk/multicast_rtp.h"
 | 
					
						
							|  |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | #include "asterisk/smoother.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*! Command value used for Linksys paging to indicate we are starting */ | 
					
						
							|  |  |  | #define LINKSYS_MCAST_STARTCMD 6
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! Command value used for Linksys paging to indicate we are stopping */ | 
					
						
							|  |  |  | #define LINKSYS_MCAST_STOPCMD 7
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Type of paging to do */ | 
					
						
							|  |  |  | enum multicast_type { | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	/*! Type has not been set yet */ | 
					
						
							|  |  |  | 	MULTICAST_TYPE_UNSPECIFIED = 0, | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	/*! Simple multicast enabled client/receiver paging like Snom and Barix uses */ | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	MULTICAST_TYPE_BASIC, | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	/*! More advanced Linksys type paging which requires a start and stop packet */ | 
					
						
							|  |  |  | 	MULTICAST_TYPE_LINKSYS, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Structure for a Linksys control packet */ | 
					
						
							|  |  |  | struct multicast_control_packet { | 
					
						
							|  |  |  | 	/*! Unique identifier for the control packet */ | 
					
						
							|  |  |  | 	uint32_t unique_id; | 
					
						
							|  |  |  | 	/*! Actual command in the control packet */ | 
					
						
							|  |  |  | 	uint32_t command; | 
					
						
							|  |  |  | 	/*! IP address for the RTP */ | 
					
						
							|  |  |  | 	uint32_t ip; | 
					
						
							|  |  |  | 	/*! Port for the RTP */ | 
					
						
							|  |  |  | 	uint32_t port; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Structure for a multicast paging instance */ | 
					
						
							|  |  |  | struct multicast_rtp { | 
					
						
							|  |  |  | 	/*! TYpe of multicast paging this instance is doing */ | 
					
						
							|  |  |  | 	enum multicast_type type; | 
					
						
							|  |  |  | 	/*! Socket used for sending the audio on */ | 
					
						
							|  |  |  | 	int socket; | 
					
						
							|  |  |  | 	/*! Synchronization source value, used when creating/sending the RTP packet */ | 
					
						
							|  |  |  | 	unsigned int ssrc; | 
					
						
							|  |  |  | 	/*! Sequence number, used when creating/sending the RTP packet */ | 
					
						
							| 
									
										
										
										
											2011-10-27 19:48:23 +00:00
										 |  |  | 	uint16_t seqno; | 
					
						
							| 
									
										
										
										
											2013-04-14 03:01:33 +00:00
										 |  |  | 	unsigned int lastts;	 | 
					
						
							|  |  |  | 	struct timeval txcore; | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 	struct ast_smoother *smoother; | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 11:04:44 -04:00
										 |  |  | #define MAX_TIMESTAMP_SKEW 640
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | enum { | 
					
						
							|  |  |  | 	OPT_CODEC = (1 << 0), | 
					
						
							|  |  |  | 	OPT_LOOP =  (1 << 1), | 
					
						
							|  |  |  | 	OPT_TTL =   (1 << 2), | 
					
						
							|  |  |  | 	OPT_IF =    (1 << 3), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  | 	OPT_ARG_CODEC = 0, | 
					
						
							|  |  |  | 	OPT_ARG_LOOP, | 
					
						
							|  |  |  | 	OPT_ARG_TTL, | 
					
						
							|  |  |  | 	OPT_ARG_IF, | 
					
						
							|  |  |  | 	OPT_ARG_ARRAY_SIZE, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AST_APP_OPTIONS(multicast_rtp_options, BEGIN_OPTIONS | 
					
						
							|  |  |  | 	/*! Set the codec to be used for multicast RTP */ | 
					
						
							|  |  |  | 	AST_APP_OPTION_ARG('c', OPT_CODEC, OPT_ARG_CODEC), | 
					
						
							|  |  |  | 	/*! Set whether multicast RTP is looped back to the sender */ | 
					
						
							|  |  |  | 	AST_APP_OPTION_ARG('l', OPT_LOOP, OPT_ARG_LOOP), | 
					
						
							|  |  |  | 	/*! Set the hop count for multicast RTP */ | 
					
						
							|  |  |  | 	AST_APP_OPTION_ARG('t', OPT_TTL, OPT_ARG_TTL), | 
					
						
							|  |  |  | 	/*! Set the interface from which multicast RTP is sent */ | 
					
						
							|  |  |  | 	AST_APP_OPTION_ARG('i', OPT_IF, OPT_ARG_IF), | 
					
						
							|  |  |  | END_OPTIONS ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_multicast_rtp_options { | 
					
						
							|  |  |  | 	char *type; | 
					
						
							|  |  |  | 	char *options; | 
					
						
							|  |  |  | 	struct ast_format *fmt; | 
					
						
							|  |  |  | 	struct ast_flags opts; | 
					
						
							|  |  |  | 	char *opt_args[OPT_ARG_ARRAY_SIZE]; | 
					
						
							|  |  |  | 	/*! The type and options are stored in this buffer */ | 
					
						
							|  |  |  | 	char buf[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_multicast_rtp_options *ast_multicast_rtp_create_options(const char *type, | 
					
						
							|  |  |  | 	const char *options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_multicast_rtp_options *mcast_options; | 
					
						
							|  |  |  | 	char *pos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mcast_options = ast_calloc(1, sizeof(*mcast_options) | 
					
						
							|  |  |  | 			+ strlen(type) | 
					
						
							| 
									
										
										
										
											2016-08-24 13:42:34 -06:00
										 |  |  | 			+ strlen(S_OR(options, "")) + 2); | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	if (!mcast_options) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pos = mcast_options->buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Safe */ | 
					
						
							|  |  |  | 	strcpy(pos, type); | 
					
						
							|  |  |  | 	mcast_options->type = pos; | 
					
						
							|  |  |  | 	pos += strlen(type) + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 13:42:34 -06:00
										 |  |  | 	if (!ast_strlen_zero(options)) { | 
					
						
							|  |  |  | 		strcpy(pos, options); /* Safe */ | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	mcast_options->options = pos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_app_parse_options(multicast_rtp_options, &mcast_options->opts, | 
					
						
							|  |  |  | 		mcast_options->opt_args, mcast_options->options)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Error parsing multicast RTP options\n"); | 
					
						
							|  |  |  | 		ast_multicast_rtp_free_options(mcast_options); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return mcast_options; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ast_multicast_rtp_free_options(struct ast_multicast_rtp_options *mcast_options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ast_free(mcast_options); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_format *ast_multicast_rtp_options_get_format(struct ast_multicast_rtp_options *mcast_options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (ast_test_flag(&mcast_options->opts, OPT_CODEC) | 
					
						
							|  |  |  | 		&& !ast_strlen_zero(mcast_options->opt_args[OPT_ARG_CODEC])) { | 
					
						
							|  |  |  | 		return ast_format_cache_get(mcast_options->opt_args[OPT_ARG_CODEC]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | /* Forward Declarations */ | 
					
						
							| 
									
										
										
										
											2010-12-20 17:15:54 +00:00
										 |  |  | static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data); | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | static int multicast_rtp_activate(struct ast_rtp_instance *instance); | 
					
						
							|  |  |  | static int multicast_rtp_destroy(struct ast_rtp_instance *instance); | 
					
						
							|  |  |  | static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame); | 
					
						
							|  |  |  | static struct ast_frame *multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* RTP Engine Declaration */ | 
					
						
							|  |  |  | static struct ast_rtp_engine multicast_rtp_engine = { | 
					
						
							|  |  |  | 	.name = "multicast", | 
					
						
							|  |  |  | 	.new = multicast_rtp_new, | 
					
						
							|  |  |  | 	.activate = multicast_rtp_activate, | 
					
						
							|  |  |  | 	.destroy = multicast_rtp_destroy, | 
					
						
							|  |  |  | 	.write = multicast_rtp_write, | 
					
						
							|  |  |  | 	.read = multicast_rtp_read, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | static int set_type(struct multicast_rtp *multicast, const char *type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!strcasecmp(type, "basic")) { | 
					
						
							|  |  |  | 		multicast->type = MULTICAST_TYPE_BASIC; | 
					
						
							|  |  |  | 	} else if (!strcasecmp(type, "linksys")) { | 
					
						
							|  |  |  | 		multicast->type = MULTICAST_TYPE_LINKSYS; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unrecognized multicast type '%s' specified.\n", type); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void set_ttl(int sock, const char *ttl_str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int ttl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(ttl_str)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_debug(3, "Setting multicast TTL to %s\n", ttl_str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (sscanf(ttl_str, "%30d", &ttl) < 1) { | 
					
						
							| 
									
										
										
										
											2016-06-13 13:33:53 -05:00
										 |  |  | 		ast_log(LOG_WARNING, "Invalid multicast ttl option '%s'\n", ttl_str); | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Could not set multicast ttl to '%s': %s\n", | 
					
						
							|  |  |  | 			ttl_str, strerror(errno)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void set_loop(int sock, const char *loop_str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned char loop; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(loop_str)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_debug(3, "Setting multicast loop to %s\n", loop_str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (sscanf(loop_str, "%30hhu", &loop) < 1) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Invalid multicast loop option '%s'\n", loop_str); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Could not set multicast loop to '%s': %s\n", | 
					
						
							|  |  |  | 			loop_str, strerror(errno)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void set_if(int sock, const char *if_str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct in_addr iface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(if_str)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_debug(3, "Setting multicast if to %s\n", if_str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!inet_aton(if_str, &iface)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Cannot parse if option '%s'\n", if_str); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &iface, sizeof(iface)) < 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Could not set multicast if to '%s': %s\n", | 
					
						
							|  |  |  | 			if_str, strerror(errno)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | /*! \brief Function called to create a new multicast instance */ | 
					
						
							| 
									
										
										
										
											2010-12-20 17:15:54 +00:00
										 |  |  | static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data) | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct multicast_rtp *multicast; | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	struct ast_multicast_rtp_options *mcast_options = data; | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!(multicast = ast_calloc(1, sizeof(*multicast)))) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	if (set_type(multicast, mcast_options->type)) { | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 		ast_free(multicast); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((multicast->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | 
					
						
							|  |  |  | 		ast_free(multicast); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | 	if (ast_test_flag(&mcast_options->opts, OPT_LOOP)) { | 
					
						
							|  |  |  | 		set_loop(multicast->socket, mcast_options->opt_args[OPT_ARG_LOOP]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_test_flag(&mcast_options->opts, OPT_TTL)) { | 
					
						
							|  |  |  | 		set_ttl(multicast->socket, mcast_options->opt_args[OPT_ARG_TTL]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_test_flag(&mcast_options->opts, OPT_IF)) { | 
					
						
							|  |  |  | 		set_if(multicast->socket, mcast_options->opt_args[OPT_ARG_IF]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	multicast->ssrc = ast_random(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_rtp_instance_set_data(instance, multicast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-14 03:01:33 +00:00
										 |  |  | static int rtp_get_rate(struct ast_format *format) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
											  
											
												media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.
Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.
Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.
Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.
Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).
For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite
Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.
There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).
Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178
ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464) 
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000) 
  chan_obscure.diff uploaded by file (License 5000) 
  jingle.diff uploaded by file (License 5000) 
  funcs.diff uploaded by file (License 5000) 
  formats.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  bridges.diff uploaded by file (License 5000) 
  mf-codecs-2.diff uploaded by file (License 5000) 
  mf-app_fax.diff uploaded by file (License 5000) 
  mf-apps-3.diff uploaded by file (License 5000) 
  media-formats-3.diff uploaded by file (License 5000) 
ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)
  
ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283) 
  mf-attributes-3.diff uploaded by file (License 5000) 
ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909) 
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283) 
  rb3747.patch uploaded by jrose (License 6182)
ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283) 
  rb3751.patch uploaded by coreyfarrell (License 5909) 
  chan_sip-3.diff uploaded by file (License 5000) 
ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273) 
  pjsip-direct-media.diff uploaded by file (License 5000) 
  format_cap_remove.diff uploaded by opticron (License 6273) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  chan_pjsip-2.diff uploaded by file (License 5000) 
ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000) 
  
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621) 
  moh_cleanup.diff uploaded by opticron (License 6273) 
  bridge_leak.diff uploaded by opticron (License 6273) 
  translate.diff uploaded by file (License 5000) 
  rb3795.patch uploaded by rmudgett (License 5621) 
  tls_fix.diff uploaded by mjordan (License 6283) 
  fax-mf-fix-2.diff uploaded by file (License 5000) 
  rtp_transfer_stuff uploaded by mjordan (License 6283) 
  rb3787.patch uploaded by rmudgett (License 5621) 
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000) 
  format_cache_case_fix.diff uploaded by opticron (License 6273) 
  rb3774.patch uploaded by rmudgett (License 5621) 
  rb3775.patch uploaded by rmudgett (License 5621) 
  rtp_engine_fix.diff uploaded by opticron (License 6273) 
  rtp_crash_fix.diff uploaded by opticron (License 6273) 
  rb3753.patch uploaded by mjordan (License 6283) 
  rb3750.patch uploaded by mjordan (License 6283) 
  rb3748.patch uploaded by rmudgett (License 5621) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  rb3740.patch uploaded by mjordan (License 6283) 
  rb3739.patch uploaded by mjordan (License 6283) 
  rb3734.patch uploaded by mjordan (License 6283) 
  rb3689.patch uploaded by mjordan (License 6283) 
  rb3674.patch uploaded by coreyfarrell (License 5909) 
  rb3671.patch uploaded by coreyfarrell (License 5909) 
  rb3667.patch uploaded by coreyfarrell (License 5909) 
  rb3665.patch uploaded by mjordan (License 6283) 
  rb3625.patch uploaded by coreyfarrell (License 5909) 
  rb3602.patch uploaded by coreyfarrell (License 5909) 
  format_compatibility-2.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2014-07-20 22:06:33 +00:00
										 |  |  | 	return ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL ? | 
					
						
							|  |  |  | 		8000 : ast_format_get_sample_rate(format); | 
					
						
							| 
									
										
										
										
											2013-04-14 03:01:33 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static unsigned int calc_txstamp(struct multicast_rtp *rtp, struct timeval *delivery) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |         struct timeval t; | 
					
						
							|  |  |  |         long ms; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ast_tvzero(rtp->txcore)) { | 
					
						
							|  |  |  |                 rtp->txcore = ast_tvnow(); | 
					
						
							|  |  |  |                 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow(); | 
					
						
							|  |  |  |         if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) { | 
					
						
							|  |  |  |                 ms = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         rtp->txcore = t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return (unsigned int) ms; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | /*! \brief Helper function which populates a control packet with useful information and sends it */ | 
					
						
							|  |  |  | static int multicast_send_control_packet(struct ast_rtp_instance *instance, struct multicast_rtp *multicast, int command) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct multicast_control_packet control_packet = { .unique_id = htonl((u_long)time(NULL)), | 
					
						
							|  |  |  | 							   .command = htonl(command), | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2010-07-08 22:08:07 +00:00
										 |  |  | 	struct ast_sockaddr control_address, remote_address; | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ast_rtp_instance_get_local_address(instance, &control_address); | 
					
						
							|  |  |  | 	ast_rtp_instance_get_remote_address(instance, &remote_address); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Ensure the user of us have given us both the control address and destination address */ | 
					
						
							| 
									
										
										
										
											2010-07-08 22:08:07 +00:00
										 |  |  | 	if (ast_sockaddr_isnull(&control_address) || | 
					
						
							|  |  |  | 	    ast_sockaddr_isnull(&remote_address)) { | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-08 22:08:07 +00:00
										 |  |  | 	/* The protocol only supports IPv4. */ | 
					
						
							|  |  |  | 	if (ast_sockaddr_is_ipv6(&remote_address)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Cannot send control packet for IPv6 " | 
					
						
							|  |  |  | 			"remote address.\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	control_packet.ip = htonl(ast_sockaddr_ipv4(&remote_address)); | 
					
						
							|  |  |  | 	control_packet.port = htonl(ast_sockaddr_port(&remote_address)); | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Based on a recommendation by Brian West who did the FreeSWITCH implementation we send control packets twice */ | 
					
						
							| 
									
										
										
										
											2010-07-08 22:08:07 +00:00
										 |  |  | 	ast_sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, &control_address); | 
					
						
							|  |  |  | 	ast_sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, &control_address); | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Function called to indicate that audio is now going to flow */ | 
					
						
							|  |  |  | static int multicast_rtp_activate(struct ast_rtp_instance *instance) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (multicast->type != MULTICAST_TYPE_LINKSYS) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STARTCMD); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Function called to destroy a multicast instance */ | 
					
						
							|  |  |  | static int multicast_rtp_destroy(struct ast_rtp_instance *instance) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (multicast->type == MULTICAST_TYPE_LINKSYS) { | 
					
						
							|  |  |  | 		multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STOPCMD); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 	if (multicast->smoother) { | 
					
						
							|  |  |  | 		ast_smoother_free(multicast->smoother); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	close(multicast->socket); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_free(multicast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec) | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance); | 
					
						
							| 
									
										
										
										
											2013-04-14 03:01:33 +00:00
										 |  |  | 	unsigned int ms = calc_txstamp(multicast, &frame->delivery); | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 	unsigned char *rtpheader; | 
					
						
							|  |  |  | 	struct ast_sockaddr remote_address = { {0,} }; | 
					
						
							| 
									
										
											  
											
												media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.
Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.
Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.
Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.
Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).
For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite
Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.
There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).
Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178
ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464) 
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000) 
  chan_obscure.diff uploaded by file (License 5000) 
  jingle.diff uploaded by file (License 5000) 
  funcs.diff uploaded by file (License 5000) 
  formats.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  bridges.diff uploaded by file (License 5000) 
  mf-codecs-2.diff uploaded by file (License 5000) 
  mf-app_fax.diff uploaded by file (License 5000) 
  mf-apps-3.diff uploaded by file (License 5000) 
  media-formats-3.diff uploaded by file (License 5000) 
ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)
  
ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283) 
  mf-attributes-3.diff uploaded by file (License 5000) 
ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909) 
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283) 
  rb3747.patch uploaded by jrose (License 6182)
ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283) 
  rb3751.patch uploaded by coreyfarrell (License 5909) 
  chan_sip-3.diff uploaded by file (License 5000) 
ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273) 
  pjsip-direct-media.diff uploaded by file (License 5000) 
  format_cap_remove.diff uploaded by opticron (License 6273) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  chan_pjsip-2.diff uploaded by file (License 5000) 
ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000) 
  
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621) 
  moh_cleanup.diff uploaded by opticron (License 6273) 
  bridge_leak.diff uploaded by opticron (License 6273) 
  translate.diff uploaded by file (License 5000) 
  rb3795.patch uploaded by rmudgett (License 5621) 
  tls_fix.diff uploaded by mjordan (License 6283) 
  fax-mf-fix-2.diff uploaded by file (License 5000) 
  rtp_transfer_stuff uploaded by mjordan (License 6283) 
  rb3787.patch uploaded by rmudgett (License 5621) 
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000) 
  format_cache_case_fix.diff uploaded by opticron (License 6273) 
  rb3774.patch uploaded by rmudgett (License 5621) 
  rb3775.patch uploaded by rmudgett (License 5621) 
  rtp_engine_fix.diff uploaded by opticron (License 6273) 
  rtp_crash_fix.diff uploaded by opticron (License 6273) 
  rb3753.patch uploaded by mjordan (License 6283) 
  rb3750.patch uploaded by mjordan (License 6283) 
  rb3748.patch uploaded by rmudgett (License 5621) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  rb3740.patch uploaded by mjordan (License 6283) 
  rb3739.patch uploaded by mjordan (License 6283) 
  rb3734.patch uploaded by mjordan (License 6283) 
  rb3689.patch uploaded by mjordan (License 6283) 
  rb3674.patch uploaded by coreyfarrell (License 5909) 
  rb3671.patch uploaded by coreyfarrell (License 5909) 
  rb3667.patch uploaded by coreyfarrell (License 5909) 
  rb3665.patch uploaded by mjordan (License 6283) 
  rb3625.patch uploaded by coreyfarrell (License 5909) 
  rb3602.patch uploaded by coreyfarrell (License 5909) 
  format_compatibility-2.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2014-07-20 22:06:33 +00:00
										 |  |  | 	int rate = rtp_get_rate(frame->subclass.format) / 1000; | 
					
						
							| 
									
										
										
										
											2017-06-06 11:04:44 -04:00
										 |  |  | 	int hdrlen = 12, mark = 0; | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 11:04:44 -04:00
										 |  |  | 	if (ast_format_cmp(frame->subclass.format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) { | 
					
						
							|  |  |  | 		frame->samples /= 2; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) { | 
					
						
							| 
									
										
										
										
											2017-06-06 11:04:44 -04:00
										 |  |  | 		multicast->lastts = frame->ts * rate; | 
					
						
							| 
									
										
										
										
											2013-10-03 18:32:59 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-06-06 11:04:44 -04:00
										 |  |  | 		/* Try to predict what our timestamp should be */ | 
					
						
							|  |  |  | 		int pred = multicast->lastts + frame->samples; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Calculate last TS */ | 
					
						
							|  |  |  | 		multicast->lastts = multicast->lastts + ms * rate; | 
					
						
							|  |  |  | 		if (ast_tvzero(frame->delivery)) { | 
					
						
							|  |  |  | 			int delta = abs((int) multicast->lastts - pred); | 
					
						
							|  |  |  | 			if (delta < MAX_TIMESTAMP_SKEW) { | 
					
						
							|  |  |  | 				multicast->lastts = pred; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ast_debug(3, "Difference is %d, ms is %u\n", delta, ms); | 
					
						
							|  |  |  | 				mark = 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-14 03:01:33 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 11:04:44 -04:00
										 |  |  | 	/* Construct an RTP header for our packet */ | 
					
						
							|  |  |  | 	rtpheader = (unsigned char *)(frame->data.ptr - hdrlen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (multicast->seqno) | (mark << 23))); | 
					
						
							|  |  |  | 	put_unaligned_uint32(rtpheader + 4, htonl(multicast->lastts)); | 
					
						
							| 
									
										
										
										
											2013-10-03 18:32:59 +00:00
										 |  |  | 	put_unaligned_uint32(rtpheader + 8, htonl(multicast->ssrc)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-27 19:48:23 +00:00
										 |  |  | 	/* Increment sequence number and wrap to 0 if it overflows 16 bits. */ | 
					
						
							|  |  |  | 	multicast->seqno = 0xFFFF & (multicast->seqno + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	/* Finally send it out to the eager phones listening for us */ | 
					
						
							|  |  |  | 	ast_rtp_instance_get_remote_address(instance, &remote_address); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 	if (ast_sendto(multicast->socket, (void *) rtpheader, frame->datalen + hdrlen, 0, &remote_address) < 0) { | 
					
						
							| 
									
										
										
										
											2010-07-08 22:08:07 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "Multicast RTP Transmission error to %s: %s\n", | 
					
						
							|  |  |  | 			ast_sockaddr_stringify(&remote_address), | 
					
						
							|  |  |  | 			strerror(errno)); | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Function called to broadcast some audio on a multicast instance */ | 
					
						
							|  |  |  | static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance); | 
					
						
							|  |  |  | 	struct ast_format *format; | 
					
						
							|  |  |  | 	struct ast_frame *f; | 
					
						
							|  |  |  | 	int codec; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* We only accept audio, nothing else */ | 
					
						
							|  |  |  | 	if (frame->frametype != AST_FRAME_VOICE) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:43:12 -04:00
										 |  |  | 	/* Grab the actual payload number for when we create the RTP packet */ | 
					
						
							|  |  |  | 	codec = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(instance), | 
					
						
							|  |  |  | 		1, frame->subclass.format, 0); | 
					
						
							|  |  |  | 	if (codec < 0) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	format = frame->subclass.format; | 
					
						
							|  |  |  | 	if (!multicast->smoother && ast_format_can_be_smoothed(format)) { | 
					
						
							|  |  |  | 		unsigned int smoother_flags = ast_format_get_smoother_flags(format); | 
					
						
							|  |  |  | 		unsigned int framing_ms = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(instance)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!framing_ms && (smoother_flags & AST_SMOOTHER_FLAG_FORCED)) { | 
					
						
							|  |  |  | 			framing_ms = ast_format_get_default_ms(format); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (framing_ms) { | 
					
						
							|  |  |  | 			multicast->smoother = ast_smoother_new((framing_ms * ast_format_get_minimum_bytes(format)) / ast_format_get_minimum_ms(format)); | 
					
						
							|  |  |  | 			if (!multicast->smoother) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %u len %u\n", | 
					
						
							|  |  |  | 						ast_format_get_name(format), framing_ms, ast_format_get_minimum_bytes(format)); | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			ast_smoother_set_flags(multicast->smoother, smoother_flags); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (multicast->smoother) { | 
					
						
							|  |  |  | 		if (ast_smoother_test_flag(multicast->smoother, AST_SMOOTHER_FLAG_BE)) { | 
					
						
							|  |  |  | 			ast_smoother_feed_be(multicast->smoother, frame); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_smoother_feed(multicast->smoother, frame); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while ((f = ast_smoother_read(multicast->smoother)) && f->data.ptr) { | 
					
						
							|  |  |  | 			rtp_raw_write(instance, f, codec); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		int hdrlen = 12; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* If we do not have space to construct an RTP header duplicate the frame so we get some */ | 
					
						
							|  |  |  | 		if (frame->offset < hdrlen) { | 
					
						
							|  |  |  | 			f = ast_frdup(frame); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			f = frame; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (f->data.ptr) { | 
					
						
							|  |  |  | 			rtp_raw_write(instance, f, codec); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (f != frame) { | 
					
						
							|  |  |  | 			ast_frfree(f); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2009-06-25 18:25:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Function called to read from a multicast instance */ | 
					
						
							|  |  |  | static struct ast_frame *multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return &ast_null_frame; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int load_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (ast_rtp_engine_register(&multicast_rtp_engine)) { | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int unload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ast_rtp_engine_unregister(&multicast_rtp_engine); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 15:14:50 -05:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Multicast RTP Engine", | 
					
						
							| 
									
										
										
										
											2014-07-25 16:47:17 +00:00
										 |  |  | 	.support_level = AST_MODULE_SUPPORT_CORE, | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							|  |  |  | 	.load_pri = AST_MODPRI_CHANNEL_DEPEND, | 
					
						
							|  |  |  | ); |