diff options
author | Matthew Jordan <mjordan@digium.com> | 2014-07-20 22:06:33 +0000 |
---|---|---|
committer | Matthew Jordan <mjordan@digium.com> | 2014-07-20 22:06:33 +0000 |
commit | a2c912e9972c91973ea66902d217746133f96026 (patch) | |
tree | 50e01d14ba62950e3f78766d5ba435ba51ca327d /res/res_pjsip_sdp_rtp.c | |
parent | b299052e203807c9a2111eb2cd919246d7589cb3 (diff) |
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
Diffstat (limited to 'res/res_pjsip_sdp_rtp.c')
-rw-r--r-- | res/res_pjsip_sdp_rtp.c | 245 |
1 files changed, 104 insertions, 141 deletions
diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c index b7e1eef3d..90a2cec46 100644 --- a/res/res_pjsip_sdp_rtp.c +++ b/res/res_pjsip_sdp_rtp.c @@ -41,6 +41,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/module.h" +#include "asterisk/format.h" +#include "asterisk/format_cap.h" #include "asterisk/rtp_engine.h" #include "asterisk/netsock2.h" #include "asterisk/channel.h" @@ -68,38 +70,39 @@ static const char STR_VIDEO[] = "video"; static const int FD_VIDEO = 2; /*! \brief Retrieves an ast_format_type based on the given stream_type */ -static enum ast_format_type stream_to_media_type(const char *stream_type) +static enum ast_media_type stream_to_media_type(const char *stream_type) { if (!strcasecmp(stream_type, STR_AUDIO)) { - return AST_FORMAT_TYPE_AUDIO; + return AST_MEDIA_TYPE_AUDIO; } else if (!strcasecmp(stream_type, STR_VIDEO)) { - return AST_FORMAT_TYPE_VIDEO; + return AST_MEDIA_TYPE_VIDEO; } return 0; } /*! \brief Get the starting descriptor for a media type */ -static int media_type_to_fdno(enum ast_format_type media_type) +static int media_type_to_fdno(enum ast_media_type media_type) { switch (media_type) { - case AST_FORMAT_TYPE_AUDIO: return FD_AUDIO; - case AST_FORMAT_TYPE_VIDEO: return FD_VIDEO; - case AST_FORMAT_TYPE_TEXT: - case AST_FORMAT_TYPE_IMAGE: break; + case AST_MEDIA_TYPE_AUDIO: return FD_AUDIO; + case AST_MEDIA_TYPE_VIDEO: return FD_VIDEO; + case AST_MEDIA_TYPE_TEXT: + case AST_MEDIA_TYPE_UNKNOWN: + case AST_MEDIA_TYPE_IMAGE: break; } return -1; } /*! \brief Remove all other cap types but the one given */ -static void format_cap_only_type(struct ast_format_cap *caps, enum ast_format_type media_type) +static void format_cap_only_type(struct ast_format_cap *caps, enum ast_media_type media_type) { - int i = AST_FORMAT_INC; - while (i <= AST_FORMAT_TYPE_TEXT) { - if (i != media_type) { - ast_format_cap_remove_bytype(caps, i); + int i = 0; + while (i <= AST_MEDIA_TYPE_TEXT) { + if (i != media_type && i != AST_MEDIA_TYPE_UNKNOWN) { + ast_format_cap_remove_by_type(caps, i); } - i += AST_FORMAT_INC; + i += 1; } } @@ -116,9 +119,6 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1); ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric); - ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp), - session_media->rtp, &session->endpoint->media.prefs); - if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) { ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND); } @@ -185,74 +185,97 @@ static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) { sscanf(pj_strbuf(&fmtp.fmt), "%d", &num); if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) { + struct ast_format *format_parsed; + ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param)); - ast_format_sdp_parse(format, fmt_param); + + format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param); + if (format_parsed) { + ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed); + ao2_ref(format_parsed, -1); + } + + ao2_ref(format, -1); } } } + + /* Get the packetization, if it exists */ + if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) { + unsigned long framing = pj_strtoul(pj_strltrim(&attr->value)); + if (framing && session->endpoint->media.rtp.use_ptime) { + ast_rtp_codecs_set_framing(codecs, framing); + } + } } static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_media *stream) { - RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy); - RAII_VAR(struct ast_format_cap *, peer, NULL, ast_format_cap_destroy); - RAII_VAR(struct ast_format_cap *, joint, NULL, ast_format_cap_destroy); - enum ast_format_type media_type = stream_to_media_type(session_media->stream_type); - struct ast_rtp_codecs codecs; - struct ast_format fmt; + RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup); + RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup); + RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup); + enum ast_media_type media_type = stream_to_media_type(session_media->stream_type); + struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT; int fmts = 0; int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) && - !ast_format_cap_is_empty(session->direct_media_cap); + ast_format_cap_count(session->direct_media_cap); - if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK)) || - !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) { + if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) || + !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) || + !(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type); return -1; } /* get the endpoint capabilities */ if (direct_media_enabled) { - ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps); + ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps); + format_cap_only_type(caps, media_type); } else { - ast_format_cap_copy(caps, session->endpoint->media.codecs); + ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type); } - format_cap_only_type(caps, media_type); /* get the capabilities on the peer */ get_codecs(session, stream, &codecs); ast_rtp_codecs_payload_formats(&codecs, peer, &fmts); /* get the joint capabilities between peer and endpoint */ - if (!(joint = ast_format_cap_joint(caps, peer))) { - char usbuf[64], thembuf[64]; + ast_format_cap_get_compatible(caps, peer, joint); + if (!ast_format_cap_count(joint)) { + struct ast_str *usbuf = ast_str_alloca(64); + struct ast_str *thembuf = ast_str_alloca(64); ast_rtp_codecs_payloads_destroy(&codecs); - - ast_getformatname_multiple(usbuf, sizeof(usbuf), caps); - ast_getformatname_multiple(thembuf, sizeof(thembuf), peer); - ast_log(LOG_WARNING, "No joint capabilities between our configuration(%s) and incoming SDP(%s)\n", usbuf, thembuf); + ast_log(LOG_WARNING, "No joint capabilities between our configuration(%s) and incoming SDP(%s)\n", + ast_format_cap_get_names(peer, &usbuf), + ast_format_cap_get_names(caps, &thembuf)); return -1; } ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp), session_media->rtp); - ast_format_cap_copy(caps, session->req_caps); - ast_format_cap_remove_bytype(caps, media_type); - ast_format_cap_append(caps, joint); - ast_format_cap_append(session->req_caps, caps); + ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN); + ast_format_cap_append_from_cap(caps, session->req_caps, AST_MEDIA_TYPE_UNKNOWN); + ast_format_cap_remove_by_type(caps, media_type); + ast_format_cap_append_from_cap(caps, joint, AST_MEDIA_TYPE_UNKNOWN); + ast_format_cap_append_from_cap(session->req_caps, caps, AST_MEDIA_TYPE_UNKNOWN); if (session->channel) { - ast_format_cap_copy(caps, ast_channel_nativeformats(session->channel)); - ast_format_cap_remove_bytype(caps, media_type); - ast_codec_choose(&session->endpoint->media.prefs, joint, 1, &fmt); - ast_format_cap_add(caps, &fmt); + struct ast_format *fmt; + + ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel), AST_MEDIA_TYPE_UNKNOWN); + ast_format_cap_remove_by_type(caps, media_type); + fmt = ast_format_cap_get_format(joint, 0); + ast_format_cap_append(caps, fmt, 0); /* Apply the new formats to the channel, potentially changing read/write formats while doing so */ - ast_format_cap_copy(ast_channel_nativeformats(session->channel), caps); - ast_set_read_format(session->channel, ast_channel_readformat(session->channel)); - ast_set_write_format(session->channel, ast_channel_writeformat(session->channel)); + ast_channel_nativeformats_set(session->channel, caps); + ast_channel_set_rawwriteformat(session->channel, fmt); + ast_channel_set_rawreadformat(session->channel, fmt); + + ao2_ref(fmt, -1); } ast_rtp_codecs_payloads_destroy(&codecs); @@ -286,7 +309,7 @@ static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format * pjmedia_sdp_attr *attr = NULL; char *tmp; - ast_format_sdp_generate(format, rtp_code, &fmtp0); + ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0); if (ast_str_strlen(fmtp0)) { tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1; /* remove any carriage return line feeds */ @@ -304,18 +327,6 @@ static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format * return attr; } -static int codec_pref_has_type(struct ast_codec_pref *prefs, enum ast_format_type media_type) -{ - int i; - struct ast_format fmt; - for (i = 0; ast_codec_pref_index(prefs, i, &fmt); ++i) { - if (AST_FORMAT_GET_TYPE(fmt.id) == media_type) { - return 1; - } - } - return 0; -} - /*! \brief Function which adds ICE attributes to a media stream */ static void add_ice_to_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media) { @@ -469,38 +480,6 @@ static void process_ice_attributes(struct ast_sip_session *session, struct ast_s ice->start(session_media->rtp); } -static void apply_packetization(struct ast_sip_session *session, struct ast_sip_session_media *session_media, - const struct pjmedia_sdp_media *remote_stream) -{ - pjmedia_sdp_attr *attr; - pj_str_t value; - unsigned long framing; - int codec; - struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref; - - /* Apply packetization if available and configured to do so */ - if (!session->endpoint->media.rtp.use_ptime || !(attr = pjmedia_sdp_media_find_attr2(remote_stream, "ptime", NULL))) { - return; - } - - value = attr->value; - framing = pj_strtoul(pj_strltrim(&value)); - - for (codec = 0; codec < AST_RTP_MAX_PT; codec++) { - struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs( - session_media->rtp), codec); - - if (!format.asterisk_format) { - continue; - } - - ast_codec_pref_setsize(pref, &format.format, framing); - } - - ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp), - session_media->rtp, pref); -} - /*! \brief figure out media transport encryption type from the media transport string */ static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport) { @@ -722,7 +701,7 @@ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct { char host[NI_MAXHOST]; RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr); - enum ast_format_type media_type = stream_to_media_type(session_media->stream_type); + enum ast_media_type media_type = stream_to_media_type(session_media->stream_type); /* If no type formats have been configured reject this stream */ if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) { @@ -759,11 +738,6 @@ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct if (set_caps(session, session_media, stream)) { return -1; } - - if (media_type == AST_FORMAT_TYPE_AUDIO) { - apply_packetization(session, session_media, stream); - } - return 1; } @@ -892,18 +866,14 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) ? AST_RTP_DTMF : 0; int min_packet_size = 0, max_packet_size = 0; int rtp_code; - struct ast_format format; - RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy); - enum ast_format_type media_type = stream_to_media_type(session_media->stream_type); + RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup); + enum ast_media_type media_type = stream_to_media_type(session_media->stream_type); + int use_override_prefs = ast_format_cap_count(session->req_caps); int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) && - !ast_format_cap_is_empty(session->direct_media_cap); + ast_format_cap_count(session->direct_media_cap); - int use_override_prefs = session->override_prefs.formats[0].id; - struct ast_codec_pref *prefs = use_override_prefs ? - &session->override_prefs : &session->endpoint->media.prefs; - - if ((use_override_prefs && !codec_pref_has_type(&session->override_prefs, media_type)) || + if ((use_override_prefs && !ast_format_cap_has_type(session->req_caps, media_type)) || (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) { /* If no type formats are configured don't add a stream */ return 0; @@ -954,59 +924,53 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as /* Add ICE attributes and candidates */ add_ice_to_stream(session, session_media, pool, media); - if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) { + if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type); return -1; } if (direct_media_enabled) { - ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps); - } else if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) { - ast_format_cap_copy(caps, session->endpoint->media.codecs); + ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps); + } else if (!ast_format_cap_count(session->req_caps) || + !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) { + ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type); } else { - ast_format_cap_copy(caps, session->req_caps); + ast_format_cap_append_from_cap(caps, session->req_caps, media_type); } - for (index = 0; ast_codec_pref_index(prefs, index, &format); ++index) { - struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref; - - if (AST_FORMAT_GET_TYPE(format.id) != media_type) { - continue; - } + for (index = 0; index < ast_format_cap_count(caps); ++index) { + struct ast_format *format = ast_format_cap_get_format(caps, index); - if (!use_override_prefs && !ast_format_cap_get_compatible_format(caps, &format, &format)) { + if (ast_format_get_type(format) != media_type) { + ao2_ref(format, -1); continue; } - if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, &format, 0)) == -1) { - ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n",ast_getformatname(&format)); + if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) { + ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format)); + ao2_ref(format, -1); continue; } - if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, &format, 0))) { + if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, format, 0))) { + ao2_ref(format, -1); continue; } - media->attr[media->attr_count++] = attr; - if ((attr = generate_fmtp_attr(pool, &format, rtp_code))) { + if ((attr = generate_fmtp_attr(pool, format, rtp_code))) { media->attr[media->attr_count++] = attr; } - if (pref && media_type != AST_FORMAT_TYPE_VIDEO) { - struct ast_format_list fmt = ast_codec_pref_getsize(pref, &format); - if (fmt.cur_ms && ((fmt.cur_ms < min_packet_size) || !min_packet_size)) { - min_packet_size = fmt.cur_ms; - } - - if (fmt.max_ms && ((fmt.max_ms < max_packet_size) || !max_packet_size)) { - max_packet_size = fmt.max_ms; - } + if (ast_format_get_maximum_ms(format) && + ((ast_format_get_maximum_ms(format) < max_packet_size) || !max_packet_size)) { + max_packet_size = ast_format_get_maximum_ms(format); } + ao2_ref(format, -1); } /* Add non-codec formats */ - if (media_type != AST_FORMAT_TYPE_VIDEO) { + if (media_type != AST_MEDIA_TYPE_VIDEO) { for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) { if (!(noncodec & index) || (rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index)) == -1) { @@ -1033,6 +997,10 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as } /* If ptime is set add it as an attribute */ + min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(session_media->rtp)); + if (!min_packet_size) { + min_packet_size = ast_format_cap_get_framing(caps); + } if (min_packet_size) { snprintf(tmp, sizeof(tmp), "%d", min_packet_size); attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp)); @@ -1061,7 +1029,7 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream) { RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr); - enum ast_format_type media_type = stream_to_media_type(session_media->stream_type); + enum ast_media_type media_type = stream_to_media_type(session_media->stream_type); char host[NI_MAXHOST]; int fdno; @@ -1095,15 +1063,10 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a /* Apply connection information to the RTP instance */ ast_sockaddr_set_port(addrs, remote_stream->desc.port); ast_rtp_instance_set_remote_address(session_media->rtp, addrs); - if (set_caps(session, session_media, local_stream)) { return -1; } - if (media_type == AST_FORMAT_TYPE_AUDIO) { - apply_packetization(session, session_media, remote_stream); - } - if ((fdno = media_type_to_fdno(media_type)) < 0) { return -1; } @@ -1117,7 +1080,7 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a ast_rtp_instance_activate(session_media->rtp); /* audio stream handles music on hold */ - if (media_type != AST_FORMAT_TYPE_AUDIO) { + if (media_type != AST_MEDIA_TYPE_AUDIO) { return 1; } |