Skip to content

Commit

Permalink
feat: add 'hasCodec' helper (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaldino committed Oct 24, 2022
1 parent 061b693 commit 75c0b3a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions cspell.json
Expand Up @@ -38,6 +38,7 @@
"libauth",
"mindmeld",
"mkdir",
"mline",
"multistream",
"munge",
"ndarray",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -3,3 +3,4 @@ export * from './lines';
export * from './model';
export * from './munge';
export * from './regex-helpers';
export * from './utils';
17 changes: 17 additions & 0 deletions src/utils.spec.ts
@@ -0,0 +1,17 @@
import { MediaLine, RtpMapLine } from './lines';
import { AvMediaDescription } from './model';
import { hasCodec } from './utils';

describe('hasCodec', () => {
it('should return true only if the codec is present', () => {
expect.hasAssertions();
const mLine = new AvMediaDescription(
new MediaLine('video', 9, 'UDP/TLS/RTP/SAVPF', ['96', '97', '98', '99', '100', '101', '127'])
);
mLine.addLine(new RtpMapLine(96, 'h264', 9000));

expect(hasCodec('h264', mLine)).toBe(true);
expect(hasCodec('H264', mLine)).toBe(true);
expect(hasCodec('vp8', mLine)).toBe(false);
});
});
14 changes: 14 additions & 0 deletions src/utils.ts
@@ -0,0 +1,14 @@
import { AvMediaDescription, CodecInfo } from './model';

/**
* Check if the given codec is present in the given mline.
*
* @param codecName - The string name of the codec to check for (case insensitive).
* @param mLine - The mline to search for the given codec.
* @returns True if the codec is present, false otherwise.
*/
export function hasCodec(codecName: string, mLine: AvMediaDescription): boolean {
return [...mLine.codecs.values()].some(
(ci: CodecInfo) => ci.name?.toLowerCase() === codecName.toLowerCase()
);
}

0 comments on commit 75c0b3a

Please sign in to comment.