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_rtp_asterisk.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_rtp_asterisk.c')
-rw-r--r-- | res/res_rtp_asterisk.c | 187 |
1 files changed, 108 insertions, 79 deletions
diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c index 7251e8f80..bd930295b 100644 --- a/res/res_rtp_asterisk.c +++ b/res/res_rtp_asterisk.c @@ -56,6 +56,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/stun.h" #include "asterisk/pbx.h" #include "asterisk/frame.h" +#include "asterisk/format_cache.h" #include "asterisk/channel.h" #include "asterisk/acl.h" #include "asterisk/config.h" @@ -66,6 +67,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/unaligned.h" #include "asterisk/module.h" #include "asterisk/rtp_engine.h" +#include "asterisk/smoother.h" #include "asterisk/test.h" #define MAX_TIMESTAMP_SKEW 640 @@ -216,8 +218,8 @@ struct ast_rtp { unsigned int cycles; /*!< Shifted count of sequence number cycles */ double rxjitter; /*!< Interarrival jitter at the moment in seconds */ double rxtransit; /*!< Relative transit time for previous packet */ - struct ast_format lasttxformat; - struct ast_format lastrxformat; + struct ast_format *lasttxformat; + struct ast_format *lastrxformat; int rtptimeout; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */ int rtpholdtimeout; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */ @@ -1833,7 +1835,11 @@ static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, static int rtp_get_rate(struct ast_format *format) { - return (format->id == AST_FORMAT_G722) ? 8000 : ast_format_rate(format); + /* For those wondering: due to a fluke in RFC publication, G.722 is advertised + * as having a sample rate of 8kHz, while implementations must know that its + * real rate is 16kHz. Seriously. + */ + return (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) ? 8000 : (int)ast_format_get_sample_rate(format); } static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp) @@ -2184,6 +2190,10 @@ static int ast_rtp_new(struct ast_rtp_instance *instance, rtp->dtlstimerid = -1; #endif + rtp->f.subclass.format = ao2_bump(ast_format_none); + rtp->lastrxformat = ao2_bump(ast_format_none); + rtp->lasttxformat = ao2_bump(ast_format_none); + return 0; } @@ -2265,6 +2275,10 @@ static int ast_rtp_destroy(struct ast_rtp_instance *instance) } #endif + ao2_cleanup(rtp->lasttxformat); + ao2_cleanup(rtp->lastrxformat); + ao2_cleanup(rtp->f.subclass.format); + /* Destroy synchronization items */ ast_mutex_destroy(&rtp->lock); ast_cond_destroy(&rtp->cond); @@ -2444,7 +2458,7 @@ static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, cha rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000)); - if (duration > 0 && (measured_samples = duration * rtp_get_rate(&rtp->f.subclass.format) / 1000) > rtp->send_duration) { + if (duration > 0 && (measured_samples = duration * rtp_get_rate(rtp->f.subclass.format) / 1000) > rtp->send_duration) { ast_debug(2, "Adjusting final end duration from %d to %u\n", rtp->send_duration, measured_samples); rtp->send_duration = measured_samples; } @@ -2620,7 +2634,7 @@ static int ast_rtcp_write_report(struct ast_rtp_instance *instance, int sr) int fraction_lost; struct timeval dlsr = { 0, }; char bdata[512]; - int rate = rtp_get_rate(&rtp->f.subclass.format); + int rate = rtp_get_rate(rtp->f.subclass.format); int ice; int header_offset = 0; struct ast_sockaddr remote_address = { {0,} }; @@ -2791,9 +2805,9 @@ static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame int pred, mark = 0; unsigned int ms = calc_txstamp(rtp, &frame->delivery); struct ast_sockaddr remote_address = { {0,} }; - int rate = rtp_get_rate(&frame->subclass.format) / 1000; + int rate = rtp_get_rate(frame->subclass.format) / 1000; - if (frame->subclass.format.id == AST_FORMAT_G722) { + if (ast_format_cmp(frame->subclass.format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) { frame->samples /= 2; } @@ -2817,7 +2831,7 @@ static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame } } } else if (frame->frametype == AST_FRAME_VIDEO) { - mark = ast_format_get_video_mark(&frame->subclass.format); + mark = frame->subclass.frame_ending; pred = rtp->lastovidtimestamp + frame->samples; /* Re-calculate last TS */ rtp->lastts = rtp->lastts + ms * 90; @@ -2959,7 +2973,7 @@ static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *fr { struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); struct ast_sockaddr remote_address = { {0,} }; - struct ast_format subclass; + struct ast_format *format; int codec; ast_rtp_instance_get_remote_address(instance, &remote_address); @@ -3029,17 +3043,28 @@ static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *fr } /* Grab the subclass and look up the payload we are going to use */ - ast_format_copy(&subclass, &frame->subclass.format); - if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, &subclass, 0)) < 0) { - ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(&frame->subclass.format)); + codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), + 1, + frame->subclass.format, + 0); + if (codec < 0) { + ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", + ast_format_get_name(frame->subclass.format)); return -1; } - /* Oh dear, if the format changed we will have to set up a new smoother */ - if (ast_format_cmp(&rtp->lasttxformat, &subclass) == AST_FORMAT_CMP_NOT_EQUAL) { - ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(&rtp->lasttxformat), ast_getformatname(&subclass)); - rtp->lasttxformat = subclass; - ast_format_copy(&rtp->lasttxformat, &subclass); + /* Note that we do not increase the ref count here as this pointer + * will not be held by any thing explicitly. The format variable is + * merely a convenience reference to frame->subclass.format */ + format = frame->subclass.format; + if (ast_format_cmp(rtp->lasttxformat, format) == AST_FORMAT_CMP_NOT_EQUAL) { + /* Oh dear, if the format changed we will have to set up a new smoother */ + if (option_debug > 0) { + ast_debug(1, "Ooh, format changed from %s to %s\n", + ast_format_get_name(rtp->lasttxformat), + ast_format_get_name(frame->subclass.format)); + } + ao2_replace(rtp->lasttxformat, format); if (rtp->smoother) { ast_smoother_free(rtp->smoother); rtp->smoother = NULL; @@ -3047,34 +3072,15 @@ static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *fr } /* If no smoother is present see if we have to set one up */ - if (!rtp->smoother) { - struct ast_format_list fmt = ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance)->pref, &subclass); - - switch (subclass.id) { - case AST_FORMAT_SPEEX: - case AST_FORMAT_SPEEX16: - case AST_FORMAT_SPEEX32: - case AST_FORMAT_SILK: - case AST_FORMAT_CELT: - case AST_FORMAT_G723_1: - case AST_FORMAT_SIREN7: - case AST_FORMAT_SIREN14: - case AST_FORMAT_G719: - /* Opus */ - case AST_FORMAT_OPUS: - /* these are all frame-based codecs and cannot be safely run through - a smoother */ - break; - default: - if (fmt.inc_ms) { - if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) { - ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms)); - return -1; - } - if (fmt.flags) { - ast_smoother_set_flags(rtp->smoother, fmt.flags); - } - ast_debug(1, "Created smoother: format: %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms)); + if (!rtp->smoother && ast_format_can_be_smoothed(format)) { + unsigned int framing_ms = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(instance)); + + if (framing_ms) { + rtp->smoother = ast_smoother_new((framing_ms * ast_format_get_minimum_bytes(format)) / ast_format_get_minimum_ms(format)); + if (!rtp->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; } } } @@ -3122,7 +3128,7 @@ static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int t double d; double dtv; double prog; - int rate = rtp_get_rate(&rtp->f.subclass.format); + int rate = rtp_get_rate(rtp->f.subclass.format); double normdev_rxjitter_current; if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) { @@ -3277,7 +3283,7 @@ static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned cha rtp->dtmf_duration = new_duration; rtp->resp = resp; f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0)); - f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0)); + f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.format)), ast_tv(0, 0)); rtp->resp = 0; rtp->dtmf_duration = rtp->dtmf_timeout = 0; AST_LIST_INSERT_TAIL(frames, f, frame_list); @@ -3308,7 +3314,7 @@ static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned cha if (rtp->resp && rtp->resp != resp) { /* Another digit already began. End it */ f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0)); - f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0)); + f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.format)), ast_tv(0, 0)); rtp->resp = 0; rtp->dtmf_duration = rtp->dtmf_timeout = 0; AST_LIST_INSERT_TAIL(frames, f, frame_list); @@ -3405,10 +3411,10 @@ static struct ast_frame *process_dtmf_cisco(struct ast_rtp_instance *instance, u } } else if ((rtp->resp == resp) && !power) { f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)); - f->samples = rtp->dtmfsamples * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8); + f->samples = rtp->dtmfsamples * (rtp_get_rate(rtp->lastrxformat) / 1000); rtp->resp = 0; } else if (rtp->resp == resp) { - rtp->dtmfsamples += 20 * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8); + rtp->dtmfsamples += 20 * (rtp_get_rate(rtp->lastrxformat) / 1000); } rtp->dtmf_timeout = 0; @@ -3424,7 +3430,8 @@ static struct ast_frame *process_cn_rfc3389(struct ast_rtp_instance *instance, u totally help us out becuase we don't have an engine to keep it going and we are not guaranteed to have it every 20ms or anything */ if (rtpdebug) { - ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", (int) rtp->lastrxformat.id, len); + ast_debug(0, "- RTP 3389 Comfort noise event: Format %s (len = %d)\n", + ast_format_get_name(rtp->lastrxformat), len); } if (ast_test_flag(rtp, FLAG_3389_WARNING)) { @@ -3796,7 +3803,7 @@ static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int struct ast_rtp_instance *instance1 = ast_rtp_instance_get_bridged(instance); struct ast_rtp *rtp = ast_rtp_instance_get_data(instance), *bridged = ast_rtp_instance_get_data(instance1); int res = 0, payload = 0, bridged_payload = 0, mark; - struct ast_rtp_payload_type payload_type; + RAII_VAR(struct ast_rtp_payload_type *, payload_type, NULL, ao2_cleanup); int reconstruct = ntohl(rtpheader[0]); struct ast_sockaddr remote_address = { {0,} }; int ice; @@ -3806,10 +3813,13 @@ static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int mark = (((reconstruct & 0x800000) >> 23) != 0); /* Check what the payload value should be */ - payload_type = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payload); + payload_type = ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payload); + if (!payload_type) { + return -1; + } /* Otherwise adjust bridged payload to match */ - bridged_payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance1), payload_type.asterisk_format, &payload_type.format, payload_type.rtp_code); + bridged_payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance1), payload_type->asterisk_format, payload_type->format, payload_type->rtp_code); /* If no codec could be matched between instance and instance1, then somehow things were made incompatible while we were still bridged. Bail. */ if (bridged_payload < 0) { @@ -3880,7 +3890,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc struct ast_sockaddr addr; int res, hdrlen = 12, version, payloadtype, padding, mark, ext, cc, prev_seqno; unsigned int *rtpheader = (unsigned int*)(rtp->rawdata + AST_FRIENDLY_OFFSET), seqno, ssrc, timestamp; - struct ast_rtp_payload_type payload; + RAII_VAR(struct ast_rtp_payload_type *, payload, NULL, ao2_cleanup); struct ast_sockaddr remote_address = { {0,} }; struct frame_list frames; @@ -4120,20 +4130,20 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc payloadtype, seqno, timestamp,res - hdrlen); } - payload = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payloadtype); + payload = ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payloadtype); /* If the payload is not actually an Asterisk one but a special one pass it off to the respective handler */ - if (!payload.asterisk_format) { + if (!payload->asterisk_format) { struct ast_frame *f = NULL; - if (payload.rtp_code == AST_RTP_DTMF) { + if (payload->rtp_code == AST_RTP_DTMF) { /* process_dtmf_rfc2833 may need to return multiple frames. We do this * by passing the pointer to the frame list to it so that the method * can append frames to the list as needed. */ process_dtmf_rfc2833(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark, &frames); - } else if (payload.rtp_code == AST_RTP_CISCO_DTMF) { + } else if (payload->rtp_code == AST_RTP_CISCO_DTMF) { f = process_dtmf_cisco(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark); - } else if (payload.rtp_code == AST_RTP_CN) { + } else if (payload->rtp_code == AST_RTP_CN) { f = process_cn_rfc3389(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark); } else { ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", @@ -4153,10 +4163,25 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc return &ast_null_frame; } - ast_format_copy(&rtp->lastrxformat, &payload.format); - ast_format_copy(&rtp->f.subclass.format, &payload.format); - rtp->f.frametype = (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_AUDIO) ? AST_FRAME_VOICE : (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_VIDEO) ? AST_FRAME_VIDEO : AST_FRAME_TEXT; - + ao2_replace(rtp->lastrxformat, payload->format); + ao2_replace(rtp->f.subclass.format, payload->format); + switch (ast_format_get_type(rtp->f.subclass.format)) { + case AST_MEDIA_TYPE_AUDIO: + rtp->f.frametype = AST_FRAME_VOICE; + break; + case AST_MEDIA_TYPE_VIDEO: + rtp->f.frametype = AST_FRAME_VIDEO; + break; + case AST_MEDIA_TYPE_TEXT: + rtp->f.frametype = AST_FRAME_TEXT; + break; + case AST_MEDIA_TYPE_IMAGE: + /* Fall through */ + default: + ast_log(LOG_WARNING, "Unknown or unsupported media type: %s\n", + ast_codec_media_type2str(ast_format_get_type(rtp->f.subclass.format))); + return &ast_null_frame; + } rtp->rxseqno = seqno; if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) { @@ -4165,7 +4190,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc if (rtp->resp) { struct ast_frame *f; f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0); - f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0)); + f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.format)), ast_tv(0, 0)); rtp->resp = 0; rtp->dtmf_timeout = rtp->dtmf_duration = 0; AST_LIST_INSERT_TAIL(&frames, f, frame_list); @@ -4182,7 +4207,9 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET; rtp->f.seqno = seqno; - if (rtp->f.subclass.format.id == AST_FORMAT_T140 && (int)seqno - (prev_seqno+1) > 0 && (int)seqno - (prev_seqno+1) < 10) { + if ((ast_format_cmp(rtp->f.subclass.format, ast_format_t140) == AST_FORMAT_CMP_EQUAL) + && ((int)seqno - (prev_seqno + 1) > 0) + && ((int)seqno - (prev_seqno + 1) < 10)) { unsigned char *data = rtp->f.data.ptr; memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen); @@ -4192,7 +4219,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc *data = 0xBD; } - if (rtp->f.subclass.format.id == AST_FORMAT_T140RED) { + if (ast_format_cmp(rtp->f.subclass.format, ast_format_t140_red) == AST_FORMAT_CMP_EQUAL) { unsigned char *data = rtp->f.data.ptr; unsigned char *header_end; int num_generations; @@ -4201,7 +4228,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc int diff =(int)seqno - (prev_seqno+1); /* if diff = 0, no drop*/ int x; - ast_format_set(&rtp->f.subclass.format, AST_FORMAT_T140, 0); + ao2_replace(rtp->f.subclass.format, ast_format_t140); header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen); if (header_end == NULL) { return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame; @@ -4239,17 +4266,17 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc } } - if (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_AUDIO) { - rtp->f.samples = ast_codec_get_samples(&rtp->f); - if (ast_format_is_slinear(&rtp->f.subclass.format)) { + if (ast_format_get_type(rtp->f.subclass.format) == AST_MEDIA_TYPE_AUDIO) { + rtp->f.samples = ast_codec_samples_count(&rtp->f); + if (ast_format_cache_is_slinear(rtp->f.subclass.format)) { ast_frame_byteswap_be(&rtp->f); } calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark); /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */ ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO); - rtp->f.ts = timestamp / (rtp_get_rate(&rtp->f.subclass.format) / 1000); - rtp->f.len = rtp->f.samples / ((ast_format_rate(&rtp->f.subclass.format) / 1000)); - } else if (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_VIDEO) { + rtp->f.ts = timestamp / (rtp_get_rate(rtp->f.subclass.format) / 1000); + rtp->f.len = rtp->f.samples / ((ast_format_get_sample_rate(rtp->f.subclass.format) / 1000)); + } else if (ast_format_get_type(rtp->f.subclass.format) == AST_MEDIA_TYPE_VIDEO) { /* Video -- samples is # of samples vs. 90000 */ if (!rtp->lastividtimestamp) rtp->lastividtimestamp = timestamp; @@ -4258,10 +4285,8 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc rtp->f.delivery.tv_sec = 0; rtp->f.delivery.tv_usec = 0; /* Pass the RTP marker bit as bit */ - if (mark) { - ast_format_set_video_mark(&rtp->f.subclass.format); - } - } else { + rtp->f.subclass.frame_ending = mark; + } else if (ast_format_get_type(rtp->f.subclass.format) == AST_MEDIA_TYPE_TEXT) { /* TEXT -- samples is # of samples vs. 1000 */ if (!rtp->lastitexttimestamp) rtp->lastitexttimestamp = timestamp; @@ -4269,6 +4294,10 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc rtp->lastitexttimestamp = timestamp; rtp->f.delivery.tv_sec = 0; rtp->f.delivery.tv_usec = 0; + } else { + ast_log(LOG_WARNING, "Unknown or unsupported media type: %s\n", + ast_codec_media_type2str(ast_format_get_type(rtp->f.subclass.format))); + return &ast_null_frame;; } AST_LIST_INSERT_TAIL(&frames, &rtp->f, frame_list); @@ -4413,7 +4442,7 @@ static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int } rtp->red->t140.frametype = AST_FRAME_TEXT; - ast_format_set(&rtp->red->t140.subclass.format, AST_FORMAT_T140RED, 0); + ao2_replace(rtp->red->t140.subclass.format, ast_format_t140_red); rtp->red->t140.data.ptr = &rtp->red->buf_data; rtp->red->t140.ts = 0; |