pax_global_header00006660000000000000000000000064135570416550014525gustar00rootroot0000000000000052 comment=8f2eee9ed846113a58b9f562947f45cd9d8f7cb9 h264-profile-level-id-1.0.1/000077500000000000000000000000001355704165500153445ustar00rootroot00000000000000h264-profile-level-id-1.0.1/.eslintrc.js000066400000000000000000000076471355704165500176210ustar00rootroot00000000000000module.exports = { env: { browser: true, es6: true, node: true, 'jest/globals': true }, plugins: [ 'jest' ], extends: [ 'eslint:recommended' ], settings: {}, parserOptions: { ecmaVersion: 2018, sourceType: 'module', ecmaFeatures: { impliedStrict: true } }, rules: { 'array-bracket-spacing': [ 2, 'always', { objectsInArrays: true, arraysInArrays: true }], 'arrow-parens': [ 2, 'always' ], 'arrow-spacing': 2, 'block-spacing': [ 2, 'always' ], 'brace-style': [ 2, 'allman', { allowSingleLine: true } ], 'comma-dangle': 2, 'comma-spacing': [ 2, { before: false, after: true } ], 'comma-style': 2, 'computed-property-spacing': 2, 'constructor-super': 2, 'func-call-spacing': 2, 'generator-star-spacing': 2, 'guard-for-in': 2, 'indent': [ 2, 'tab', { 'SwitchCase': 1 } ], 'key-spacing': [ 2, { singleLine: { beforeColon: false, afterColon: true }, multiLine: { beforeColon: true, afterColon: true, align: 'colon' } }], 'keyword-spacing': 2, 'linebreak-style': [ 2, 'unix' ], 'lines-around-comment': [ 2, { allowBlockStart: true, allowObjectStart: true, beforeBlockComment: true, beforeLineComment: false }], 'max-len': [ 2, 90, { tabWidth: 2, comments: 100, ignoreUrls: true, ignoreStrings: true, ignoreTemplateLiterals: true, ignoreRegExpLiterals: true }], 'newline-after-var': 2, 'newline-before-return': 2, 'newline-per-chained-call': 2, 'no-alert': 2, 'no-caller': 2, 'no-case-declarations': 2, 'no-catch-shadow': 2, 'no-class-assign': 2, 'no-confusing-arrow': 2, 'no-console': 2, 'no-const-assign': 2, 'no-debugger': 2, 'no-dupe-args': 2, 'no-dupe-keys': 2, 'no-duplicate-case': 2, 'no-div-regex': 2, 'no-empty': [ 2, { allowEmptyCatch: true } ], 'no-empty-pattern': 2, 'no-else-return': 0, 'no-eval': 2, 'no-extend-native': 2, 'no-ex-assign': 2, 'no-extra-bind': 2, 'no-extra-boolean-cast': 2, 'no-extra-label': 2, 'no-extra-semi': 2, 'no-fallthrough': 2, 'no-func-assign': 2, 'no-global-assign': 2, 'no-implicit-coercion': 2, 'no-implicit-globals': 2, 'no-inner-declarations': 2, 'no-invalid-regexp': 2, 'no-invalid-this': 2, 'no-irregular-whitespace': 2, 'no-lonely-if': 2, 'no-mixed-operators': 2, 'no-mixed-spaces-and-tabs': 2, 'no-multi-spaces': 2, 'no-multi-str': 2, 'no-multiple-empty-lines': [ 1, { max: 1, maxEOF: 0, maxBOF: 0 } ], 'no-native-reassign': 2, 'no-negated-in-lhs': 2, 'no-new': 2, 'no-new-func': 2, 'no-new-wrappers': 2, 'no-obj-calls': 2, 'no-proto': 2, 'no-prototype-builtins': 0, 'no-redeclare': 2, 'no-regex-spaces': 2, 'no-restricted-imports': 2, 'no-return-assign': 2, 'no-self-assign': 2, 'no-self-compare': 2, 'no-sequences': 2, 'no-shadow': 2, 'no-shadow-restricted-names': 2, 'no-spaced-func': 2, 'no-sparse-arrays': 2, 'no-this-before-super': 2, 'no-throw-literal': 2, 'no-undef': 2, 'no-unexpected-multiline': 2, 'no-unmodified-loop-condition': 2, 'no-unreachable': 2, 'no-unused-vars': [ 1, { vars: 'all', args: 'after-used' }], 'no-use-before-define': [ 2, { functions: false } ], 'no-useless-call': 2, 'no-useless-computed-key': 2, 'no-useless-concat': 2, 'no-useless-rename': 2, 'no-var': 2, 'no-whitespace-before-property': 2, 'object-curly-newline': 0, 'object-curly-spacing': [ 2, 'always' ], 'object-property-newline': [ 2, { allowMultiplePropertiesPerLine: true } ], 'prefer-const': 2, 'prefer-rest-params': 2, 'prefer-spread': 2, 'prefer-template': 2, 'quotes': [ 2, 'single', { avoidEscape: true } ], 'semi': [ 2, 'always' ], 'semi-spacing': 2, 'space-before-blocks': 2, 'space-before-function-paren': [ 2, { anonymous : 'never', named : 'never', asyncArrow : 'always' }], 'space-in-parens': [ 2, 'never' ], 'spaced-comment': [ 2, 'always' ], 'strict': 2, 'valid-typeof': 2, 'yoda': 2 } }; h264-profile-level-id-1.0.1/.gitignore000066400000000000000000000000321355704165500173270ustar00rootroot00000000000000/node_modules/ /coverage/ h264-profile-level-id-1.0.1/.npmrc000066400000000000000000000000231355704165500164570ustar00rootroot00000000000000package-lock=false h264-profile-level-id-1.0.1/README.md000066400000000000000000000072131355704165500166260ustar00rootroot00000000000000# h264-profile-level-id JavaScript utility to process [H264](https://tools.ietf.org/html/rfc6184) `profile-level-id` values based on Google's [libwebrtc](https://chromium.googlesource.com/external/webrtc/+/refs/heads/master/media/base/h264_profile_level_id.h) C++ code. ```bash $ npm install h264-profile-level-id ``` This library provides TypeScript definitions. ## API ```js import { // H264 Profile identifiers. ProfileConstrainedBaseline, ProfileBaseline, ProfileMain, ProfileConstrainedHigh, ProfileHigh, // H264 Level identifiers. Level1_b, Level1, Level1_1, Level1_2, Level1_3, Level2, Level2_1, Level2_2, Level3, Level3_1, Level3_2, Level4, Level4_1, Level4_2, Level5, Level5_1, Level5_2, // Class. ProfileLevelId, // Functions. parseProfileLevelId, profileLevelIdToString, parseSdpProfileLevelId, isSameProfile, generateProfileLevelIdForAnswer } from 'h264-profile-level-id'; ``` ### ProfileLevelId Class containing both H264 Profile and Level. ```js const profile_level_id = new ProfileLevelId(ProfileMain, Level3_1); console.log('profile:%d, level:%d', profile_level_id.profile, profile_level_id.level); // => profile:3, level:31 ``` Both `profile` and `level` members are public. ### parseProfileLevelId(str) Parse profile level id that is represented as a string of 3 hex bytes. Nothing will be returned if the string is not a recognized H264 profile level id. * `@param` {String} **str** - profile-level-id value as a string of 3 hex bytes. * `@returns` {ProfileLevelId} A instance of the `ProfileLevelId` class. ### profileLevelIdToString(profile_level_id) Returns canonical string representation as three hex bytes of the profile level id, or returns nothing for invalid profile level ids. * `@param` {ProfileLevelId} **profile_level_id** - A instance of the `ProfileLevelId` class. * `@returns` {String} ### parseSdpProfileLevelId(params={}) Parse profile level id that is represented as a string of 3 hex bytes contained in an SDP key-value map. A default profile level id will be returned if the profile-level-id key is missing. Nothing will be returned if the key is present but the string is invalid. * `@param` {Object} **[params={}]** - Codec parameters object. * `@returns` {ProfileLevelId} A instance of the `ProfileLevelId` class. ### isSameProfile(params1={}, params2={}) Returns true if the parameters have the same H264 profile, i.e. the same H264 profile (Baseline, High, etc). * `@param` {Object} **[params1={}]** - Codec parameters object. * `@param` {Object} **[params2={}]** - Codec parameters object. * `@returns` {Boolean} ### generateProfileLevelIdForAnswer(local_supported_params={}, remote_offered_params={}) Generate a profile level id that is represented as a string of 3 hex bytes suitable for an answer in an SDP negotiation based on local supported parameters and remote offered parameters. The parameters that are used when negotiating are the level part of `profile-level-id` and `level-asymmetry-allowed`. **NOTE:** This function is just intended to manage H264 profile levels ids with same Profile (otherwise it will throw). Use `isSameProfile()` API before this one. * `@param` {Object} **[local_supported_params={}]** * `@param` {Object} **[remote_offered_params={}]** * `@returns` {String} Canonical string representation as three hex bytes of the profile level id, or null if no one of the params have `profile-level-id.` * `@throws` {TypeError} If Profile mismatch or invalid params. ## Usage examples See the [unit tests](test/test.js). ## Author * Iñaki Baz Castillo [[website](https://inakibaz.me)|[github](https://github.com/ibc/)] ## License [ISC](./LICENSE) h264-profile-level-id-1.0.1/index.d.ts000066400000000000000000000071341355704165500172520ustar00rootroot00000000000000declare module 'h264-profile-level-id' { export const ProfileConstrainedBaseline: number; export const ProfileBaseline: number; export const ProfileMain: number; export const ProfileConstrainedHigh: number; export const ProfileHigh: number; // All values are equal to ten times the level number, except level 1b which is // special. /* eslint-disable camelcase, @typescript-eslint/camelcase */ export const Level1_b: number; export const Level1: number; export const Level1_1: number; export const Level1_2: number; export const Level1_3: number; export const Level2: number; export const Level2_1: number; export const Level2_2: number; export const Level3: number; export const Level3_1: number; export const Level3_2: number; export const Level4: number; export const Level4_1: number; export const Level4_2: number; export const Level5: number; export const Level5_1: number; export const Level5_2: number; /* eslint-enable camelcase, @typescript-eslint/camelcase */ // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ProfileLevelId {} /** * Parse profile level id that is represented as a string of 3 hex bytes. * Nothing will be returned if the string is not a recognized H264 profile * level id. */ export function parseProfileLevelId(str: string): ProfileLevelId /** * Returns canonical string representation as three hex bytes of the profile * level id, or returns nothing for invalid profile level ids. */ // eslint-disable-next-line camelcase, @typescript-eslint/camelcase export function profileLevelIdToString(profile_level_id: ProfileLevelId): string /** * Parse profile level id that is represented as a string of 3 hex bytes * contained in an SDP key-value map. A default profile level id will be * returned if the profile-level-id key is missing. Nothing will be returned if * the key is present but the string is invalid. */ export function parseSdpProfileLevelId(params: object): ProfileLevelId /** * Returns true if the parameters have the same H264 profile, i.e. the same * H264 profile (Baseline, High, etc). * */ export function isSameProfile(params1: object, params2: object): boolean /** * Generate codec parameters that will be used as answer in an SDP negotiation * based on local supported parameters and remote offered parameters. Both * local_supported_params and remote_offered_params represent sendrecv media * descriptions, i.e they are a mix of both encode and decode capabilities. In * theory, when the profile in local_supported_params represent a strict superset * of the profile in remote_offered_params, we could limit the profile in the * answer to the profile in remote_offered_params. * * However, to simplify the code, each supported H264 profile should be listed * explicitly in the list of local supported codecs, even if they are redundant. * Then each local codec in the list should be tested one at a time against the * remote codec, and only when the profiles are equal should this function be * called. Therefore, this function does not need to handle profile intersection, * and the profile of local_supported_params and remote_offered_params must be * equal before calling this function. The parameters that are used when * negotiating are the level part of profile-level-id and level-asymmetry-allowed. * */ export function generateProfileLevelIdForAnswer( // eslint-disable-next-line camelcase, @typescript-eslint/camelcase local_supported_params: object, // eslint-disable-next-line camelcase, @typescript-eslint/camelcase remote_offered_params: object ): string } h264-profile-level-id-1.0.1/index.js000066400000000000000000000304231355704165500170130ustar00rootroot00000000000000const debug = require('debug')('h264-profile-level-id'); /* eslint-disable no-console */ debug.log = console.info.bind(console); /* eslint-enable no-console */ const ProfileConstrainedBaseline = 1; const ProfileBaseline = 2; const ProfileMain = 3; const ProfileConstrainedHigh = 4; const ProfileHigh = 5; exports.ProfileConstrainedBaseline = ProfileConstrainedBaseline; exports.ProfileBaseline = ProfileBaseline; exports.ProfileMain = ProfileMain; exports.ProfileConstrainedHigh = ProfileConstrainedHigh; exports.ProfileHigh = ProfileHigh; // All values are equal to ten times the level number, except level 1b which is // special. const Level1_b = 0; const Level1 = 10; const Level1_1 = 11; const Level1_2 = 12; const Level1_3 = 13; const Level2 = 20; const Level2_1 = 21; const Level2_2 = 22; const Level3 = 30; const Level3_1 = 31; const Level3_2 = 32; const Level4 = 40; const Level4_1 = 41; const Level4_2 = 42; const Level5 = 50; const Level5_1 = 51; const Level5_2 = 52; exports.Level1_b = Level1_b; exports.Level1 = Level1; exports.Level1_1 = Level1_1; exports.Level1_2 = Level1_2; exports.Level1_3 = Level1_3; exports.Level2 = Level2; exports.Level2_1 = Level2_1; exports.Level2_2 = Level2_2; exports.Level3 = Level3; exports.Level3_1 = Level3_1; exports.Level3_2 = Level3_2; exports.Level4 = Level4; exports.Level4_1 = Level4_1; exports.Level4_2 = Level4_2; exports.Level5 = Level5; exports.Level5_1 = Level5_1; exports.Level5_2 = Level5_2; class ProfileLevelId { constructor(profile, level) { this.profile = profile; this.level = level; } } exports.ProfileLevelId = ProfileLevelId; // Default ProfileLevelId. // // TODO: The default should really be profile Baseline and level 1 according to // the spec: https://tools.ietf.org/html/rfc6184#section-8.1. In order to not // break backwards compatibility with older versions of WebRTC where external // codecs don't have any parameters, use profile ConstrainedBaseline level 3_1 // instead. This workaround will only be done in an interim period to allow // external clients to update their code. // // http://crbug/webrtc/6337. const DefaultProfileLevelId = new ProfileLevelId(ProfileConstrainedBaseline, Level3_1); // For level_idc=11 and profile_idc=0x42, 0x4D, or 0x58, the constraint set3 // flag specifies if level 1b or level 1.1 is used. const ConstraintSet3Flag = 0x10; // Class for matching bit patterns such as "x1xx0000" where 'x' is allowed to be // either 0 or 1. class BitPattern { constructor(str) { this._mask = ~byteMaskString('x', str); this._maskedValue = byteMaskString('1', str); } isMatch(value) { return this._maskedValue === (value & this._mask); } } // Class for converting between profile_idc/profile_iop to Profile. class ProfilePattern { constructor(profile_idc, profile_iop, profile) { this.profile_idc = profile_idc; this.profile_iop = profile_iop; this.profile = profile; } } // This is from https://tools.ietf.org/html/rfc6184#section-8.1. const ProfilePatterns = [ new ProfilePattern(0x42, new BitPattern('x1xx0000'), ProfileConstrainedBaseline), new ProfilePattern(0x4D, new BitPattern('1xxx0000'), ProfileConstrainedBaseline), new ProfilePattern(0x58, new BitPattern('11xx0000'), ProfileConstrainedBaseline), new ProfilePattern(0x42, new BitPattern('x0xx0000'), ProfileBaseline), new ProfilePattern(0x58, new BitPattern('10xx0000'), ProfileBaseline), new ProfilePattern(0x4D, new BitPattern('0x0x0000'), ProfileMain), new ProfilePattern(0x64, new BitPattern('00000000'), ProfileHigh), new ProfilePattern(0x64, new BitPattern('00001100'), ProfileConstrainedHigh) ]; /** * Parse profile level id that is represented as a string of 3 hex bytes. * Nothing will be returned if the string is not a recognized H264 profile * level id. * * @param {String} str - profile-level-id value as a string of 3 hex bytes. * * @returns {ProfileLevelId} */ exports.parseProfileLevelId = function(str) { // The string should consist of 3 bytes in hexadecimal format. if (typeof str !== 'string' || str.length !== 6) return null; const profile_level_id_numeric = parseInt(str, 16); if (profile_level_id_numeric === 0) return null; // Separate into three bytes. const level_idc = profile_level_id_numeric & 0xFF; const profile_iop = (profile_level_id_numeric >> 8) & 0xFF; const profile_idc = (profile_level_id_numeric >> 16) & 0xFF; // Parse level based on level_idc and constraint set 3 flag. let level; switch (level_idc) { case Level1_1: { level = (profile_iop & ConstraintSet3Flag) !== 0 ? Level1_b : Level1_1; break; } case Level1: case Level1_2: case Level1_3: case Level2: case Level2_1: case Level2_2: case Level3: case Level3_1: case Level3_2: case Level4: case Level4_1: case Level4_2: case Level5: case Level5_1: case Level5_2: { level = level_idc; break; } // Unrecognized level_idc. default: { debug('parseProfileLevelId() | unrecognized level_idc:%s', level_idc); return null; } } // Parse profile_idc/profile_iop into a Profile enum. for (const pattern of ProfilePatterns) { if ( profile_idc === pattern.profile_idc && pattern.profile_iop.isMatch(profile_iop) ) { return new ProfileLevelId(pattern.profile, level); } } debug('parseProfileLevelId() | unrecognized profile_idc/profile_iop combination'); return null; }; /** * Returns canonical string representation as three hex bytes of the profile * level id, or returns nothing for invalid profile level ids. * * @param {ProfileLevelId} profile_level_id * * @returns {String} */ exports.profileLevelIdToString = function(profile_level_id) { // Handle special case level == 1b. if (profile_level_id.level == Level1_b) { switch (profile_level_id.profile) { case ProfileConstrainedBaseline: { return '42f00b'; } case ProfileBaseline: { return '42100b'; } case ProfileMain: { return '4d100b'; } // Level 1_b is not allowed for other profiles. default: { debug( 'profileLevelIdToString() | Level 1_b not is allowed for profile:%s', profile_level_id.profile); return null; } } } let profile_idc_iop_string; switch (profile_level_id.profile) { case ProfileConstrainedBaseline: { profile_idc_iop_string = '42e0'; break; } case ProfileBaseline: { profile_idc_iop_string = '4200'; break; } case ProfileMain: { profile_idc_iop_string = '4d00'; break; } case ProfileConstrainedHigh: { profile_idc_iop_string = '640c'; break; } case ProfileHigh: { profile_idc_iop_string = '6400'; break; } default: { debug( 'profileLevelIdToString() | unrecognized profile:%s', profile_level_id.profile); return null; } } let levelStr = (profile_level_id.level).toString(16); if (levelStr.length === 1) levelStr = `0${levelStr}`; return `${profile_idc_iop_string}${levelStr}`; }; /** * Parse profile level id that is represented as a string of 3 hex bytes * contained in an SDP key-value map. A default profile level id will be * returned if the profile-level-id key is missing. Nothing will be returned if * the key is present but the string is invalid. * * @param {Object} [params={}] - Codec parameters object. * * @returns {ProfileLevelId} */ exports.parseSdpProfileLevelId = function(params = {}) { const profile_level_id = params['profile-level-id']; return !profile_level_id ? DefaultProfileLevelId : exports.parseProfileLevelId(profile_level_id); }; /** * Returns true if the parameters have the same H264 profile, i.e. the same * H264 profile (Baseline, High, etc). * * @param {Object} [params1={}] - Codec parameters object. * @param {Object} [params2={}] - Codec parameters object. * * @returns {Boolean} */ exports.isSameProfile = function(params1 = {}, params2 = {}) { const profile_level_id_1 = exports.parseSdpProfileLevelId(params1); const profile_level_id_2 = exports.parseSdpProfileLevelId(params2); // Compare H264 profiles, but not levels. return Boolean( profile_level_id_1 && profile_level_id_2 && profile_level_id_1.profile === profile_level_id_2.profile ); }; /** * Generate codec parameters that will be used as answer in an SDP negotiation * based on local supported parameters and remote offered parameters. Both * local_supported_params and remote_offered_params represent sendrecv media * descriptions, i.e they are a mix of both encode and decode capabilities. In * theory, when the profile in local_supported_params represent a strict superset * of the profile in remote_offered_params, we could limit the profile in the * answer to the profile in remote_offered_params. * * However, to simplify the code, each supported H264 profile should be listed * explicitly in the list of local supported codecs, even if they are redundant. * Then each local codec in the list should be tested one at a time against the * remote codec, and only when the profiles are equal should this function be * called. Therefore, this function does not need to handle profile intersection, * and the profile of local_supported_params and remote_offered_params must be * equal before calling this function. The parameters that are used when * negotiating are the level part of profile-level-id and level-asymmetry-allowed. * * @param {Object} [local_supported_params={}] * @param {Object} [remote_offered_params={}] * * @returns {String} Canonical string representation as three hex bytes of the * profile level id, or null if no one of the params have profile-level-id. * * @throws {TypeError} If Profile mismatch or invalid params. */ exports.generateProfileLevelIdForAnswer = function( local_supported_params = {}, remote_offered_params = {} ) { // If both local and remote params do not contain profile-level-id, they are // both using the default profile. In this case, don't return anything. if ( !local_supported_params['profile-level-id'] && !remote_offered_params['profile-level-id'] ) { debug( 'generateProfileLevelIdForAnswer() | no profile-level-id in local and remote params'); return null; } // Parse profile-level-ids. const local_profile_level_id = exports.parseSdpProfileLevelId(local_supported_params); const remote_profile_level_id = exports.parseSdpProfileLevelId(remote_offered_params); // The local and remote codec must have valid and equal H264 Profiles. if (!local_profile_level_id) throw new TypeError('invalid local_profile_level_id'); if (!remote_profile_level_id) throw new TypeError('invalid remote_profile_level_id'); if (local_profile_level_id.profile !== remote_profile_level_id.profile) throw new TypeError('H264 Profile mismatch'); // Parse level information. const level_asymmetry_allowed = ( isLevelAsymmetryAllowed(local_supported_params) && isLevelAsymmetryAllowed(remote_offered_params) ); const local_level = local_profile_level_id.level; const remote_level = remote_profile_level_id.level; const min_level = minLevel(local_level, remote_level); // Determine answer level. When level asymmetry is not allowed, level upgrade // is not allowed, i.e., the level in the answer must be equal to or lower // than the level in the offer. const answer_level = level_asymmetry_allowed ? local_level : min_level; debug( 'generateProfileLevelIdForAnswer() | result: [profile:%s, level:%s]', local_profile_level_id.profile, answer_level); // Return the resulting profile-level-id for the answer parameters. return exports.profileLevelIdToString( new ProfileLevelId(local_profile_level_id.profile, answer_level)); }; // Convert a string of 8 characters into a byte where the positions containing // character c will have their bit set. For example, c = 'x', str = "x1xx0000" // will return 0b10110000. function byteMaskString(c, str) { return ( ((str[0] === c) << 7) | ((str[1] === c) << 6) | ((str[2] === c) << 5) | ((str[3] === c) << 4) | ((str[4] === c) << 3) | ((str[5] === c) << 2) | ((str[6] === c) << 1) | ((str[7] === c) << 0) ); } // Compare H264 levels and handle the level 1b case. function isLessLevel(a, b) { if (a === Level1_b) return b !== Level1 && b !== Level1_b; if (b === Level1_b) return a !== Level1; return a < b; } function minLevel(a, b) { return isLessLevel(a, b) ? a : b; } function isLevelAsymmetryAllowed(params = {}) { const level_asymmetry_allowed = params['level-asymmetry-allowed']; return ( level_asymmetry_allowed === 1 || level_asymmetry_allowed === '1' ); } h264-profile-level-id-1.0.1/package.json000066400000000000000000000015501355704165500176330ustar00rootroot00000000000000{ "name": "h264-profile-level-id", "version": "1.0.1", "description": "Utility to process H264 profile-level-id values", "author": "Iñaki Baz Castillo (https://inakibaz.me)", "license": "ISC", "repository": { "type": "git", "url": "https://github.com/ibc/h264-profile-level-id.git" }, "main": "index.js", "types": "index.d.ts", "engines": { "node": ">=8.0.0" }, "scripts": { "lint": "eslint -c .eslintrc.js index.js test", "test": "jest", "coverage": "jest --coverage && open-cli coverage/lcov-report/index.html" }, "jest": { "verbose": true, "testEnvironment": "node", "testRegex": "test/test.*\\.js" }, "dependencies": { "debug": "^4.1.1" }, "devDependencies": { "eslint": "^6.6.0", "eslint-plugin-jest": "^23.0.2", "jest": "^24.9.0", "opn-cli": "^5.0.0" } } h264-profile-level-id-1.0.1/test/000077500000000000000000000000001355704165500163235ustar00rootroot00000000000000h264-profile-level-id-1.0.1/test/test.js000066400000000000000000000146331355704165500176470ustar00rootroot00000000000000/* eslint-disable no-unused-vars */ const { ProfileConstrainedBaseline, ProfileBaseline, ProfileMain, ProfileConstrainedHigh, ProfileHigh, Level1_b, Level1, Level1_1, Level1_2, Level1_3, Level2, Level2_1, Level2_2, Level3, Level3_1, Level3_2, Level4, Level4_1, Level4_2, Level5, Level5_1, Level5_2, ProfileLevelId, parseProfileLevelId, profileLevelIdToString, parseSdpProfileLevelId, isSameProfile, generateProfileLevelIdForAnswer } = require('../'); /* eslint-enable no-unused-vars */ test('TestParsingInvalid', () => { // Malformed strings. expect(parseProfileLevelId()).toBeNull(); expect(parseProfileLevelId('')).toBeNull(); expect(parseProfileLevelId(' 42e01f')).toBeNull(); expect(parseProfileLevelId('4242e01f')).toBeNull(); expect(parseProfileLevelId('e01f')).toBeNull(); expect(parseProfileLevelId('gggggg')).toBeNull(); // Invalid level. expect(parseProfileLevelId('42e000')).toBeNull(); expect(parseProfileLevelId('42e00f')).toBeNull(); expect(parseProfileLevelId('42e0ff')).toBeNull(); // Invalid profile. expect(parseProfileLevelId('42e11f')).toBeNull(); expect(parseProfileLevelId('58601f')).toBeNull(); expect(parseProfileLevelId('64e01f')).toBeNull(); }); test('TestParsingLevel', () => { expect(parseProfileLevelId('42e01f').level).toBe(Level3_1); expect(parseProfileLevelId('42e00b').level).toBe(Level1_1); expect(parseProfileLevelId('42f00b').level).toBe(Level1_b); expect(parseProfileLevelId('42C02A').level).toBe(Level4_2); expect(parseProfileLevelId('640c34').level).toBe(Level5_2); }); test('TestParsingConstrainedBaseline', () => { expect(parseProfileLevelId('42e01f').profile).toBe(ProfileConstrainedBaseline); expect(parseProfileLevelId('42C02A').profile).toBe(ProfileConstrainedBaseline); expect(parseProfileLevelId('4de01f').profile).toBe(ProfileConstrainedBaseline); expect(parseProfileLevelId('58f01f').profile).toBe(ProfileConstrainedBaseline); }); test('TestParsingBaseline', () => { expect(parseProfileLevelId('42a01f').profile).toBe(ProfileBaseline); expect(parseProfileLevelId('58A01F').profile).toBe(ProfileBaseline); }); test('TestParsingMain', () => { expect(parseProfileLevelId('4D401f').profile).toBe(ProfileMain); }); test('TestParsingHigh', () => { expect(parseProfileLevelId('64001f').profile).toBe(ProfileHigh); }); test('TestParsingConstrainedHigh', () => { expect(parseProfileLevelId('640c1f').profile).toBe(ProfileConstrainedHigh); }); test('TestToString', () => { expect( profileLevelIdToString(new ProfileLevelId(ProfileConstrainedBaseline, Level3_1)) ).toBe('42e01f'); expect( profileLevelIdToString(new ProfileLevelId(ProfileBaseline, Level1)) ).toBe('42000a'); expect( profileLevelIdToString(new ProfileLevelId(ProfileMain, Level3_1)) ).toBe('4d001f'); expect( profileLevelIdToString(new ProfileLevelId(ProfileConstrainedHigh, Level4_2)) ).toBe('640c2a'); expect( profileLevelIdToString(new ProfileLevelId(ProfileHigh, Level4_2)) ).toBe('64002a'); }); test('TestToStringLevel1b', () => { expect( profileLevelIdToString(new ProfileLevelId(ProfileConstrainedBaseline, Level1_b)) ).toBe('42f00b'); expect( profileLevelIdToString(new ProfileLevelId(ProfileBaseline, Level1_b)) ).toBe('42100b'); expect( profileLevelIdToString(new ProfileLevelId(ProfileMain, Level1_b)) ).toBe('4d100b'); }); test('TestToStringLevel1b', () => { expect(profileLevelIdToString(parseProfileLevelId('42e01f'))).toBe('42e01f'); expect(profileLevelIdToString(parseProfileLevelId('42E01F'))).toBe('42e01f'); expect(profileLevelIdToString(parseProfileLevelId('4d100b'))).toBe('4d100b'); expect(profileLevelIdToString(parseProfileLevelId('4D100B'))).toBe('4d100b'); expect(profileLevelIdToString(parseProfileLevelId('640c2a'))).toBe('640c2a'); expect(profileLevelIdToString(parseProfileLevelId('640C2A'))).toBe('640c2a'); }); test('TestToStringInvalid', () => { expect( profileLevelIdToString(new ProfileLevelId(ProfileHigh, Level1_b)) ).toBeNull(); expect( profileLevelIdToString(new ProfileLevelId(ProfileConstrainedHigh, Level1_b)) ).toBeNull(); expect( profileLevelIdToString(new ProfileLevelId(255, Level3_1)) ).toBeNull(); }); test('TestParseSdpProfileLevelIdEmpty', () => { const profile_level_id = parseSdpProfileLevelId(); expect(profile_level_id).toBeDefined(); expect(profile_level_id.profile).toBe(ProfileConstrainedBaseline); expect(profile_level_id.level).toBe(Level3_1); }); test('TestParseSdpProfileLevelIdConstrainedHigh', () => { const params = { 'profile-level-id': '640c2a' }; const profile_level_id = parseSdpProfileLevelId(params); expect(profile_level_id).toBeDefined(); expect(profile_level_id.profile).toBe(ProfileConstrainedHigh); expect(profile_level_id.level).toBe(Level4_2); }); test('TestParseSdpProfileLevelIdInvalid', () => { const params = { 'profile-level-id': 'foobar' }; expect(parseSdpProfileLevelId(params)).toBeNull(); }); test('TestIsSameProfile', () => { expect( isSameProfile({ foo: 'foo' }, { bar: 'bar' }) ).toBe(true); expect( isSameProfile({ 'profile-level-id': '42e01f' }, { 'profile-level-id': '42C02A' }) ).toBe(true); expect( isSameProfile({ 'profile-level-id': '42a01f' }, { 'profile-level-id': '58A01F' }) ).toBe(true); expect( isSameProfile({ 'profile-level-id': '42e01f' }, undefined) ).toBe(true); }); test('TestIsNotSameProfile', () => { expect( isSameProfile(undefined, { 'profile-level-id': '4d001f' }) ).toBe(false); expect( isSameProfile({ 'profile-level-id': '42a01f' }, { 'profile-level-id': '640c1f' }) ).toBe(false); expect( isSameProfile({ 'profile-level-id': '42000a' }, { 'profile-level-id': '64002a' }) ).toBe(false); }); test('TestGenerateProfileLevelIdForAnswerEmpty', () => { expect(generateProfileLevelIdForAnswer(undefined, undefined)).toBeNull(); }); test('TestGenerateProfileLevelIdForAnswerLevelSymmetryCapped', () => { const low_level = { 'profile-level-id' : '42e015' }; const high_level = { 'profile-level-id' : '42e01f' }; expect(generateProfileLevelIdForAnswer(low_level, high_level)).toBe('42e015'); expect(generateProfileLevelIdForAnswer(high_level, low_level)).toBe('42e015'); }); test('TestGenerateProfileLevelIdForAnswerConstrainedBaselineLevelAsymmetry', () => { const local_params = { 'profile-level-id' : '42e01f', 'level-asymmetry-allowed' : '1' }; const remote_params = { 'profile-level-id' : '42e015', 'level-asymmetry-allowed' : '1' }; expect(generateProfileLevelIdForAnswer(local_params, remote_params)).toBe('42e01f'); });