UNPKG

@4players/odin-common

Version:

A collection of commonly used type definitions and utility functions across ODIN web projects

86 lines (85 loc) 2.96 kB
import { failure, success, unwrapOr } from './result'; const validCodecs = ['VP8', 'VP9', 'AV1', 'H264']; export class VideoCodec { constructor(codec) { this.codec = codec; this.channels = 0; this.clockRate = 90000; } isValid() { return validCodecs.includes(this.codec); } isSupported() { if (typeof RTCRtpReceiver === 'undefined' || typeof RTCRtpReceiver.getCapabilities === 'undefined') { return null; } const expectedMimeType = unwrapOr(this.getMimeType(), '').toLowerCase(); const expectedFmtpLine = new Set(unwrapOr(this.getSdpFmtpLine(), '') .split(';') .map((arg) => arg.trim().toLowerCase())); return (RTCRtpReceiver.getCapabilities('video')?.codecs?.find((c) => { const actualMimeType = c.mimeType.toLowerCase(); const actualFmtpLine = new Set((c.sdpFmtpLine ?? '') .split(';') .map((arg) => arg.trim().toLowerCase())); if (expectedMimeType !== actualMimeType) return false; if (expectedFmtpLine.size !== actualFmtpLine.size) return false; for (let item of expectedFmtpLine) { if (!actualFmtpLine.has(item)) return false; } return true; }) ?? null); } getPayloadType() { switch (this.codec) { case 'VP8': return success(96); case 'VP9': return success(98); case 'AV1': return success(41); case 'H264': return success(102); default: return failure('invalid video codec'); } } getMimeType() { switch (this.codec) { case 'VP8': return success('video/VP8'); case 'VP9': return success('video/VP9'); case 'AV1': return success('video/AV1'); case 'H264': return success('video/H264'); default: return failure('invalid video codec'); } } getSdpFmtpLine() { switch (this.codec) { case 'VP8': return success(''); case 'VP9': return success( // using high-bit-depth variant profile 'profile-id=2'); case 'AV1': return success( // using baseline profile (https://aomediacodec.github.io/av1-rtp-spec/#72-sdp-parameters) 'level-idx=5;profile=0;tier=0'); case 'H264': return success( // using baseline profile 'level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f'); default: return failure('invalid video codec'); } } }