summaryrefslogtreecommitdiff
path: root/res/res_format_attr_h264.c
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2014-07-20 22:06:33 +0000
committerMatthew Jordan <mjordan@digium.com>2014-07-20 22:06:33 +0000
commita2c912e9972c91973ea66902d217746133f96026 (patch)
tree50e01d14ba62950e3f78766d5ba435ba51ca327d /res/res_format_attr_h264.c
parentb299052e203807c9a2111eb2cd919246d7589cb3 (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_format_attr_h264.c')
-rw-r--r--res/res_format_attr_h264.c400
1 files changed, 209 insertions, 191 deletions
diff --git a/res/res_format_attr_h264.c b/res/res_format_attr_h264.c
index 2bd47e025..81784bb48 100644
--- a/res/res_format_attr_h264.c
+++ b/res/res_format_attr_h264.c
@@ -50,266 +50,284 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
* H264_MAX_SPS_PPS_SIZE */
#define H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "15"
-enum h264_attr_keys {
- H264_ATTR_KEY_PROFILE_IDC,
- H264_ATTR_KEY_PROFILE_IOP,
- H264_ATTR_KEY_LEVEL,
- H264_ATTR_KEY_MAX_MBPS,
- H264_ATTR_KEY_MAX_FS,
- H264_ATTR_KEY_MAX_CPB,
- H264_ATTR_KEY_MAX_DPB,
- H264_ATTR_KEY_MAX_BR,
- H264_ATTR_KEY_MAX_SMBPS,
- H264_ATTR_KEY_MAX_FPS,
- H264_ATTR_KEY_REDUNDANT_PIC_CAP,
- H264_ATTR_KEY_PARAMETER_ADD,
- H264_ATTR_KEY_PACKETIZATION_MODE,
- H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH,
- H264_ATTR_KEY_SPROP_DEINT_BUF_REQ,
- H264_ATTR_KEY_DEINT_BUF_CAP,
- H264_ATTR_KEY_SPROP_INIT_BUF_TIME,
- H264_ATTR_KEY_SPROP_MAX_DON_DIFF,
- H264_ATTR_KEY_MAX_RCMD_NALU_SIZE,
- H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED,
- H264_ATTR_KEY_SPS_LEN,
- H264_ATTR_KEY_PPS_LEN,
- H264_ATTR_KEY_SPS,
- H264_ATTR_KEY_PPS = H264_ATTR_KEY_SPS + H264_MAX_SPS_PPS_SIZE,
- H264_ATTR_KEY_END = H264_ATTR_KEY_PPS + H264_MAX_SPS_PPS_SIZE,
+struct h264_attr {
+ unsigned int PROFILE_IDC;
+ unsigned int PROFILE_IOP;
+ unsigned int LEVEL;
+ unsigned int MAX_MBPS;
+ unsigned int MAX_FS;
+ unsigned int MAX_CPB;
+ unsigned int MAX_DPB;
+ unsigned int MAX_BR;
+ unsigned int MAX_SMBPS;
+ unsigned int MAX_FPS;
+ unsigned int REDUNDANT_PIC_CAP;
+ unsigned int PARAMETER_ADD;
+ unsigned int PACKETIZATION_MODE;
+ unsigned int SPROP_INTERLEAVING_DEPTH;
+ unsigned int SPROP_DEINT_BUF_REQ;
+ unsigned int DEINT_BUF_CAP;
+ unsigned int SPROP_INIT_BUF_TIME;
+ unsigned int SPROP_MAX_DON_DIFF;
+ unsigned int MAX_RCMD_NALU_SIZE;
+ unsigned int LEVEL_ASYMMETRY_ALLOWED;
+ char SPS[H264_MAX_SPS_PPS_SIZE];
+ char PPS[H264_MAX_SPS_PPS_SIZE];
};
-static enum ast_format_cmp_res h264_format_attr_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
+static void h264_destroy(struct ast_format *format)
{
- if (!fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] || !fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC] ||
- (fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] == fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC])) {
+ struct h264_attr *attr = ast_format_get_attribute_data(format);
+
+ ast_free(attr);
+}
+
+static int h264_clone(const struct ast_format *src, struct ast_format *dst)
+{
+ struct h264_attr *original = ast_format_get_attribute_data(src);
+ struct h264_attr *attr = ast_calloc(1, sizeof(*attr));
+
+ if (!attr) {
+ return -1;
+ }
+
+ if (original) {
+ *attr = *original;
+ }
+
+ ast_format_set_attribute_data(dst, attr);
+
+ return 0;
+}
+
+static enum ast_format_cmp_res h264_cmp(const struct ast_format *format1, const struct ast_format *format2)
+{
+ struct h264_attr *attr1 = ast_format_get_attribute_data(format1);
+ struct h264_attr *attr2 = ast_format_get_attribute_data(format2);
+
+ if (!attr1 || !attr1->PROFILE_IDC || !attr2 || !attr2->PROFILE_IDC ||
+ (attr1->PROFILE_IDC == attr2->PROFILE_IDC)) {
return AST_FORMAT_CMP_EQUAL;
}
return AST_FORMAT_CMP_NOT_EQUAL;
}
-static int h264_format_attr_get_joint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
+#define DETERMINE_JOINT(joint, attr1, attr2, field) (joint->field = (attr1 && attr1->field) ? attr1->field : (attr2 && attr2->field) ? attr2->field : 0)
+
+static struct ast_format *h264_getjoint(const struct ast_format *format1, const struct ast_format *format2)
{
- int i;
+ struct ast_format *cloned;
+ struct h264_attr *attr, *attr1, *attr2;
+
+ cloned = ast_format_clone(format1);
+ if (!cloned) {
+ return NULL;
+ }
+ attr = ast_format_get_attribute_data(cloned);
+
+ attr1 = ast_format_get_attribute_data(format1);
+ attr2 = ast_format_get_attribute_data(format2);
+
+ DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IDC);
+ DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IOP);
+ DETERMINE_JOINT(attr, attr1, attr2, LEVEL);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_MBPS);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_FS);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_CPB);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_DPB);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_BR);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_SMBPS);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_FPS);
+ DETERMINE_JOINT(attr, attr1, attr2, REDUNDANT_PIC_CAP);
+ DETERMINE_JOINT(attr, attr1, attr2, PARAMETER_ADD);
+ DETERMINE_JOINT(attr, attr1, attr2, SPROP_INTERLEAVING_DEPTH);
+ DETERMINE_JOINT(attr, attr1, attr2, SPROP_DEINT_BUF_REQ);
+ DETERMINE_JOINT(attr, attr1, attr2, DEINT_BUF_CAP);
+ DETERMINE_JOINT(attr, attr1, attr2, SPROP_INIT_BUF_TIME);
+ DETERMINE_JOINT(attr, attr1, attr2, SPROP_MAX_DON_DIFF);
+ DETERMINE_JOINT(attr, attr1, attr2, MAX_RCMD_NALU_SIZE);
+ DETERMINE_JOINT(attr, attr1, attr2, LEVEL_ASYMMETRY_ALLOWED);
+ DETERMINE_JOINT(attr, attr1, attr2, PACKETIZATION_MODE);
+
+ if (attr1 && !ast_strlen_zero(attr1->SPS)) {
+ ast_copy_string(attr->SPS, attr1->SPS, sizeof(attr->SPS));
+ } else if (attr2 && !ast_strlen_zero(attr2->SPS)) {
+ ast_copy_string(attr->SPS, attr1->SPS, sizeof(attr->SPS));
+ }
- for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
- result->format_attr[i] = fattr1->format_attr[i] ? fattr1->format_attr[i] : fattr2->format_attr[i];
+ if (attr1 && !ast_strlen_zero(attr1->PPS)) {
+ ast_copy_string(attr->PPS, attr1->PPS, sizeof(attr->PPS));
+ } else if (attr2 && !ast_strlen_zero(attr2->PPS)) {
+ ast_copy_string(attr->PPS, attr1->PPS, sizeof(attr->PPS));
}
- return 0;
+ return cloned;
}
-static int h264_format_attr_sdp_parse(struct ast_format_attr *format_attr, const char *attributes)
+static struct ast_format *h264_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
{
char *attribs = ast_strdupa(attributes), *attrib;
+ struct ast_format *cloned;
+ struct h264_attr *attr;
- format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = H264_ATTR_KEY_UNSET;
- format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = H264_ATTR_KEY_UNSET;
- format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = H264_ATTR_KEY_UNSET;
- format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = H264_ATTR_KEY_UNSET;
+ cloned = ast_format_clone(format);
+ if (!cloned) {
+ return NULL;
+ }
+ attr = ast_format_get_attribute_data(cloned);
+
+ attr->REDUNDANT_PIC_CAP = H264_ATTR_KEY_UNSET;
+ attr->PARAMETER_ADD = H264_ATTR_KEY_UNSET;
+ attr->PACKETIZATION_MODE = H264_ATTR_KEY_UNSET;
+ attr->LEVEL_ASYMMETRY_ALLOWED = H264_ATTR_KEY_UNSET;
while ((attrib = strsep(&attribs, ";"))) {
unsigned int val;
unsigned long int val2;
- char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
if (sscanf(attrib, "profile-level-id=%lx", &val2) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] = ((val2 >> 16) & 0xFF);
- format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] = ((val2 >> 8) & 0xFF);
- format_attr->format_attr[H264_ATTR_KEY_LEVEL] = (val2 & 0xFF);
- } else if (sscanf(attrib, "sprop-parameter-sets=%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "[^','],%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "s", sps, pps) == 2) {
+ attr->PROFILE_IDC = ((val2 >> 16) & 0xFF);
+ attr->PROFILE_IOP = ((val2 >> 8) & 0xFF);
+ attr->LEVEL = (val2 & 0xFF);
+ } else if (sscanf(attrib, "sprop-parameter-sets=%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "[^','],%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "s", attr->SPS, attr->PPS) == 2) {
/* XXX sprop-parameter-sets can actually be of unlimited length. This may need to be addressed later. */
- unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
- int i;
-
- ast_base64decode(spsdecoded, sps, sizeof(spsdecoded));
- ast_base64decode(ppsdecoded, pps, sizeof(ppsdecoded));
-
- format_attr->format_attr[H264_ATTR_KEY_SPS_LEN] = 0;
- format_attr->format_attr[H264_ATTR_KEY_PPS_LEN] = 0;
-
- for (i = 0; i < H264_MAX_SPS_PPS_SIZE; i++) {
- if (spsdecoded[i]) {
- format_attr->format_attr[H264_ATTR_KEY_SPS + i] = spsdecoded[i];
- format_attr->format_attr[H264_ATTR_KEY_SPS_LEN]++;
- }
- if (ppsdecoded[i]) {
- format_attr->format_attr[H264_ATTR_KEY_PPS + i] = ppsdecoded[i];
- format_attr->format_attr[H264_ATTR_KEY_PPS_LEN]++;
- }
- }
} else if (sscanf(attrib, "max-mbps=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_MBPS] = val;
+ attr->MAX_MBPS = val;
} else if (sscanf(attrib, "max-fs=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_FS] = val;
+ attr->MAX_FS = val;
} else if (sscanf(attrib, "max-cpb=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_CPB] = val;
+ attr->MAX_CPB = val;
} else if (sscanf(attrib, "max-dpb=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_DPB] = val;
+ attr->MAX_DPB = val;
} else if (sscanf(attrib, "max-br=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_BR] = val;
+ attr->MAX_BR = val;
} else if (sscanf(attrib, "max-smbps=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_SMBPS] = val;
+ attr->MAX_SMBPS = val;
} else if (sscanf(attrib, "max-fps=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_FPS] = val;
+ attr->MAX_FPS = val;
} else if (sscanf(attrib, "redundant-pic-cap=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = val;
+ attr->REDUNDANT_PIC_CAP = val;
} else if (sscanf(attrib, "parameter-add=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = val;
+ attr->PARAMETER_ADD = val;
} else if (sscanf(attrib, "packetization-mode=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = val;
+ attr->PACKETIZATION_MODE = val;
} else if (sscanf(attrib, "sprop-interleaving-depth=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH] = val;
+ attr->SPROP_INTERLEAVING_DEPTH = val;
} else if (sscanf(attrib, "sprop-deint-buf-req=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_SPROP_DEINT_BUF_REQ] = val;
+ attr->SPROP_DEINT_BUF_REQ = val;
} else if (sscanf(attrib, "deint-buf-cap=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_DEINT_BUF_CAP] = val;
+ attr->DEINT_BUF_CAP = val;
} else if (sscanf(attrib, "sprop-init-buf-time=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_SPROP_INIT_BUF_TIME] = val;
+ attr->SPROP_INIT_BUF_TIME = val;
} else if (sscanf(attrib, "sprop-max-don-diff=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_SPROP_MAX_DON_DIFF] = val;
+ attr->SPROP_MAX_DON_DIFF = val;
} else if (sscanf(attrib, "max-rcmd-nalu-size=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_MAX_RCMD_NALU_SIZE] = val;
+ attr->MAX_RCMD_NALU_SIZE = val;
} else if (sscanf(attrib, "level-asymmetry-allowed=%30u", &val) == 1) {
- format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = val;
+ attr->LEVEL_ASYMMETRY_ALLOWED = val;
}
}
- return 0;
+ return cloned;
}
-/*! \brief Helper function which converts a key enum into a string value for SDP */
-static const char *h264_attr_key_to_str(enum h264_attr_keys key)
+#define APPEND_IF_NOT_H264_UNSET(field, str, name) do { \
+ if (field != H264_ATTR_KEY_UNSET) { \
+ if (added) { \
+ ast_str_append(str, 0, ";"); \
+ } else { \
+ added = 1; \
+ } \
+ ast_str_append(str, 0, "%s=%u", name, field); \
+ } \
+} while (0)
+
+#define APPEND_IF_NONZERO(field, str, name) do { \
+ if (field) { \
+ if (added) { \
+ ast_str_append(str, 0, ";"); \
+ } else { \
+ added = 1; \
+ } \
+ ast_str_append(str, 0, "%s=%u", name, field); \
+ } \
+} while (0)
+
+static void h264_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
{
- switch (key) {
- case H264_ATTR_KEY_MAX_MBPS:
- return "max-mbps";
- case H264_ATTR_KEY_MAX_FS:
- return "max-fs";
- case H264_ATTR_KEY_MAX_CPB:
- return "max-cpb";
- case H264_ATTR_KEY_MAX_DPB:
- return "max-dpb";
- case H264_ATTR_KEY_MAX_BR:
- return "max-br";
- case H264_ATTR_KEY_MAX_SMBPS:
- return "max-smbps";
- case H264_ATTR_KEY_MAX_FPS:
- return "max-fps";
- case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
- return "redundant-pic-cap";
- case H264_ATTR_KEY_PARAMETER_ADD:
- return "parameter-add";
- case H264_ATTR_KEY_PACKETIZATION_MODE:
- return "packetization-mode";
- case H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH:
- return "sprop-interleaving-depth";
- case H264_ATTR_KEY_SPROP_DEINT_BUF_REQ:
- return "sprop-deint-buf-req";
- case H264_ATTR_KEY_DEINT_BUF_CAP:
- return "deint-buf-cap";
- case H264_ATTR_KEY_SPROP_INIT_BUF_TIME:
- return "sprop-init-buf-time";
- case H264_ATTR_KEY_SPROP_MAX_DON_DIFF:
- return "sprop-max-don-diff";
- case H264_ATTR_KEY_MAX_RCMD_NALU_SIZE:
- return "max-rcmd-nalu-size";
- case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
- return "level-asymmetry-allowed";
- default:
- return NULL;
- }
+ struct h264_attr *attr = ast_format_get_attribute_data(format);
+ int added = 0;
- return NULL;
-}
-
-/*! \brief Helper function which determines if the value of an attribute can be placed into the SDP */
-static int h264_attr_key_addable(const struct ast_format_attr *format_attr, enum h264_attr_keys key)
-{
- switch (key) {
- case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
- case H264_ATTR_KEY_PARAMETER_ADD:
- case H264_ATTR_KEY_PACKETIZATION_MODE:
- case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
- return (format_attr->format_attr[key] != H264_ATTR_KEY_UNSET) ? 1 : 0;
- default:
- return format_attr->format_attr[key] ? 1 : 0;
+ if (!attr) {
+ return;
}
- return 1;
-}
+ ast_str_append(str, 0, "a=fmtp:%u ", payload);
+
+ APPEND_IF_NONZERO(attr->MAX_MBPS, str, "max-mbps");
+ APPEND_IF_NONZERO(attr->MAX_FS, str, "max-fs");
+ APPEND_IF_NONZERO(attr->MAX_CPB, str, "max-cpb");
+ APPEND_IF_NONZERO(attr->MAX_DPB, str, "max-dpb");
+ APPEND_IF_NONZERO(attr->MAX_BR, str, "max-br");
+ APPEND_IF_NONZERO(attr->MAX_SMBPS, str, "max-smbps");
+ APPEND_IF_NONZERO(attr->MAX_FPS, str, "max-fps");
+ APPEND_IF_NONZERO(attr->SPROP_INTERLEAVING_DEPTH, str, "sprop-interleaving-depth");
+ APPEND_IF_NONZERO(attr->SPROP_DEINT_BUF_REQ, str, "sprop-deint-buf-req");
+ APPEND_IF_NONZERO(attr->DEINT_BUF_CAP, str, "deint-buf-cap");
+ APPEND_IF_NONZERO(attr->SPROP_INIT_BUF_TIME, str, "sprop-init-buf-time");
+ APPEND_IF_NONZERO(attr->SPROP_MAX_DON_DIFF, str, "sprop-max-don-diff");
+ APPEND_IF_NONZERO(attr->MAX_RCMD_NALU_SIZE, str, "max-rcmd-nalu-size");
+
+ APPEND_IF_NOT_H264_UNSET(attr->REDUNDANT_PIC_CAP, str, "redundant-pic-cap");
+ APPEND_IF_NOT_H264_UNSET(attr->PARAMETER_ADD, str, "parameter-add");
+ APPEND_IF_NOT_H264_UNSET(attr->PACKETIZATION_MODE, str, "packetization-mode");
+ APPEND_IF_NOT_H264_UNSET(attr->LEVEL_ASYMMETRY_ALLOWED, str, "level-asymmetry-allowed");
+
+ if (attr->PROFILE_IDC && attr->PROFILE_IOP && attr->LEVEL) {
+ if (added) {
+ ast_str_append(str, 0, ";");
+ } else {
+ added = 1;
+ }
+ ast_str_append(str, 0, "profile-level-id=%02X%02X%02X", attr->PROFILE_IDC, attr->PROFILE_IOP, attr->LEVEL);
+ }
-static void h264_format_attr_sdp_generate(const struct ast_format_attr *format_attr, unsigned int payload, struct ast_str **str)
-{
- int i, added = 0;
-
- for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
- const char *name;
-
- if (i == H264_ATTR_KEY_SPS && format_attr->format_attr[H264_ATTR_KEY_SPS] && format_attr->format_attr[H264_ATTR_KEY_PPS]) {
- unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
- int pos;
- char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
-
- for (pos = 0; pos < H264_MAX_SPS_PPS_SIZE; pos++) {
- spsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_SPS + pos];
- ppsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_PPS + pos];
- }
-
- ast_base64encode(sps, spsdecoded, format_attr->format_attr[H264_ATTR_KEY_SPS_LEN], H264_MAX_SPS_PPS_SIZE);
- ast_base64encode(pps, ppsdecoded, format_attr->format_attr[H264_ATTR_KEY_PPS_LEN], H264_MAX_SPS_PPS_SIZE);
-
- if (!added) {
- ast_str_append(str, 0, "a=fmtp:%u sprop-parameter-sets=%s,%s", payload, sps, pps);
- added = 1;
- } else {
- ast_str_append(str, 0, ";sprop-parameter-sets=%s,%s", sps, pps);
- }
- } else if (i == H264_ATTR_KEY_PROFILE_IDC && format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] &&
- format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] && format_attr->format_attr[H264_ATTR_KEY_LEVEL]) {
- if (!added) {
- ast_str_append(str, 0, "a=fmtp:%u profile-level-id=%02X%02X%02X", payload, format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
- format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
- added = 1;
- } else {
- ast_str_append(str, 0, ";profile-level-id=%02X%02X%02X", format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
- format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
- }
- } else if ((name = h264_attr_key_to_str(i)) && h264_attr_key_addable(format_attr, i)) {
- if (!added) {
- ast_str_append(str, 0, "a=fmtp:%u %s=%u", payload, name, format_attr->format_attr[i]);
- added = 1;
- } else {
- ast_str_append(str, 0, ";%s=%u", name, format_attr->format_attr[i]);
- }
+ if (!ast_strlen_zero(attr->SPS) && !ast_strlen_zero(attr->PPS)) {
+ if (added) {
+ ast_str_append(str, 0, ";");
+ } else {
+ added = 1;
}
+ ast_str_append(str, 0, ";sprop-parameter-sets=%s,%s", attr->SPS, attr->PPS);
}
-
- if (added) {
+
+ if (!added) {
+ ast_str_reset(*str);
+ } else {
ast_str_append(str, 0, "\r\n");
}
return;
}
-static struct ast_format_attr_interface h264_format_attr_interface = {
- .id = AST_FORMAT_H264,
- .format_attr_cmp = h264_format_attr_cmp,
- .format_attr_get_joint = h264_format_attr_get_joint,
- .format_attr_sdp_parse = h264_format_attr_sdp_parse,
- .format_attr_sdp_generate = h264_format_attr_sdp_generate,
+static struct ast_format_interface h264_interface = {
+ .format_destroy = h264_destroy,
+ .format_clone = h264_clone,
+ .format_cmp = h264_cmp,
+ .format_get_joint = h264_getjoint,
+ .format_parse_sdp_fmtp = h264_parse_sdp_fmtp,
+ .format_generate_sdp_fmtp = h264_generate_sdp_fmtp,
};
static int unload_module(void)
{
- ast_format_attr_unreg_interface(&h264_format_attr_interface);
-
return 0;
}
static int load_module(void)
{
- if (ast_format_attr_reg_interface(&h264_format_attr_interface)) {
+ if (ast_format_interface_register("h264", &h264_interface)) {
return AST_MODULE_LOAD_DECLINE;
}