Skip to content

Commit

Permalink
New API v2, full TypeScript (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc authored Dec 22, 2023
1 parent e60e2a9 commit 5320021
Show file tree
Hide file tree
Showing 15 changed files with 7,712 additions and 1,221 deletions.
404 changes: 238 additions & 166 deletions .eslintrc.js

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions .github/workflows/h264-profile-level-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: h264-profile-level-id

on: [push, pull_request]

concurrency:
# Cancel a currently running workflow from the same PR, branch or tag when a
# new workflow is triggered.
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
ci:
strategy:
matrix:
ci:
- os: ubuntu-20.04
node: 16
- os: macos-12
node: 18
- os: windows-2022
node: 20

runs-on: ${{ matrix.ci.os }}

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.ci.node }}

- name: Configure cache
uses: actions/cache@v3
with:
path: |
~/.npm
key: ${{ matrix.ci.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ matrix.ci.os }}-node-
- name: npm ci
run: npm ci --foreground-scripts

- name: npm run lint
run: npm run lint

- name: npm run test
run: npm run test
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/node_modules/
/coverage/
## Node.
/node_modules
/lib

## Others.
/coverage
/.cache
5 changes: 4 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
package-lock=false
# Generate package-lock.json.
package-lock=true
# For bad node/npm version to throw actual error.
engine-strict=true
139 changes: 59 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# h264-profile-level-id

JavaScript utility to process [H264](https://tools.ietf.org/html/rfc6184) `profile-level-id` values based on Google's libwebrtc C++ code:
TypeScript utility to process [H264](https://tools.ietf.org/html/rfc6184) `profile-level-id` values based on Google's libwebrtc C++ code:
- [h264_profile_level_id.cc](https://webrtc.googlesource.com/src/+/refs/heads/main/api/video_codecs/h264_profile_level_id.cc)
- [h264_profile_level_id.h](https://webrtc.googlesource.com/src/+/refs/heads/main/api/video_codecs/h264_profile_level_id.h)
- [h264_profile_level_id_unittest.cc](https://webrtc.googlesource.com/src/+/refs/heads/main/api/video_codecs/test/h264_profile_level_id_unittest.cc)
Expand All @@ -9,38 +9,14 @@ JavaScript utility to process [H264](https://tools.ietf.org/html/rfc6184) `profi
$ npm install h264-profile-level-id
```

This library provides TypeScript definitions.


## API

```js
```ts
import {
// H264 Profile identifiers.
ProfileConstrainedBaseline,
ProfileBaseline,
ProfileMain,
ProfileConstrainedHigh,
ProfileHigh,
ProfilePredictiveHigh444,
// 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,
// H264 Profile enum
Profile,
// H264 Level enum
Level,
// Class.
ProfileLevelId,
// Functions.
Expand All @@ -50,90 +26,93 @@ import {
levelToString,
parseSdpProfileLevelId,
isSameProfile,
generateProfileLevelIdForAnswer
generateProfileLevelIdStringForAnswer
} from 'h264-profile-level-id';
```

### ProfileLevelId
```ts
enum Profile
{
ConstrainedBaseline = 1,
Baseline = 2,
Main = 3,
ConstrainedHigh = 4,
High = 5,
PredictiveHigh444 = 6
}
```

```ts
enum Level
{
L1_b = 0,
L1 = 10,
L1_1 = 11,
L1_2 = 12,
L1_3 = 13,
L2 = 20,
L2_1 = 21,
L2_2 = 22,
L3 = 30,
L3_1 = 31,
L3_2 = 32,
L4 = 40,
L4_1 = 41,
L4_2 = 42,
L5 = 50,
L5_1 = 51,
L5_2 = 52
}
```

Class containing both H264 Profile and Level.
### class ProfileLevelId

Class containing both H264 profile and level.

```js
const profile_level_id = new ProfileLevelId(ProfileMain, Level3_1);
const profile_level_id = new ProfileLevelId(Profile.Main, Level.L3_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)
### function parseProfileLevelId(str: string): ProfileLevelId \| undefined

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|null}
### function profileLevelIdToString(profile_level_id: ProfileLevelId): string \| undefined

Return canonical string representation as three hex bytes of the profile level id, or returns nothing for invalid profile level ids.

### profileToString(str)
### function profileToString(profile: Profile): string \| undefined

Prints name of given profile.
Return a human friendly name for the given profile.

* `@param` {number} **profile**
### function levelToString(level: Level): string \| undefined

* `@returns` {String|null}
Return a human friendly name for the given level.

### function parseSdpProfileLevelId(params: any = {}): ProfileLevelId \ undefined

### levelToString(str)
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.

Prints name of given level.
### function isSameProfile(params1: any = {}, params2: any = {}): boolean

* `@param` {number} **level**
Return true if the parameters have the same H264 profile, i.e. the same H264 profile (Baseline, High, etc).

* `@returns` {String|null}


### 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={})
### function generateProfileLevelIdForAnswer(local_supported_params: any = {}, remote_offered_params: any = {}): string \| undefined

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.
**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.


## Usage examples

See the [unit tests](test/test.js).
See the [unit tests](src/tests/test.js).


## Author
Expand Down
106 changes: 0 additions & 106 deletions index.d.ts

This file was deleted.

Loading

0 comments on commit 5320021

Please sign in to comment.