diff --git a/src/web-capabilities.spec.ts b/src/web-capabilities.spec.ts index f55d1f9..57fde12 100644 --- a/src/web-capabilities.spec.ts +++ b/src/web-capabilities.spec.ts @@ -95,4 +95,59 @@ describe('WebCapabilities', () => { expect(WebCapabilities.supportsEncodedStreamTransforms()).toBe(CapabilityState.NOT_CAPABLE); }); }); + describe('isCapableOfReceivingVideoCodec', () => { + afterEach(() => { + // Clean up window modifications + delete (window as Window & { RTCRtpReceiver?: unknown }).RTCRtpReceiver; + }); + + it('should return CAPABLE when the codec is supported', () => { + expect.assertions(1); + + Object.defineProperty(window, 'RTCRtpReceiver', { + writable: true, + value: { + getCapabilities: jest.fn().mockReturnValue({ + codecs: [{ mimeType: 'video/AV1' }], + } as unknown as RTCRtpCapabilities), + }, + }); + + expect(WebCapabilities.isCapableOfReceivingVideoCodec('video/AV1')).toBe( + CapabilityState.CAPABLE + ); + }); + + it('should return NOT_CAPABLE when the codec is not supported', () => { + expect.assertions(1); + + Object.defineProperty(window, 'RTCRtpReceiver', { + writable: true, + value: { + getCapabilities: jest.fn().mockReturnValue({ + codecs: [{ mimeType: 'video/H264' }], + } as unknown as RTCRtpCapabilities), + }, + }); + + expect(WebCapabilities.isCapableOfReceivingVideoCodec('video/AV1')).toBe( + CapabilityState.NOT_CAPABLE + ); + }); + + it('should return NOT_CAPABLE when getCapabilities returns undefined', () => { + expect.assertions(1); + + Object.defineProperty(window, 'RTCRtpReceiver', { + writable: true, + value: { + getCapabilities: jest.fn().mockReturnValue(undefined), + }, + }); + + expect(WebCapabilities.isCapableOfReceivingVideoCodec('video/AV1')).toBe( + CapabilityState.NOT_CAPABLE + ); + }); + }); }); diff --git a/src/web-capabilities.ts b/src/web-capabilities.ts index 6ee10b7..168b7db 100644 --- a/src/web-capabilities.ts +++ b/src/web-capabilities.ts @@ -80,6 +80,21 @@ export class WebCapabilities { return CapabilityState.CAPABLE; } + /** + * Checks whether the browser is capable of receiving a specific video codec. + * + * @param mimeType - The MIME type of the codec to check. + * @returns A {@link CapabilityState}. + */ + static isCapableOfReceivingVideoCodec( + mimeType: RTCRtpCodecCapability['mimeType'] + ): CapabilityState { + const codecs = RTCRtpReceiver.getCapabilities('video')?.codecs || []; + return codecs.some((codec) => codec.mimeType === mimeType) + ? CapabilityState.CAPABLE + : CapabilityState.NOT_CAPABLE; + } + /** * Checks whether the browser supports encoded stream transforms. *