Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/web-capabilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
});
});
15 changes: 15 additions & 0 deletions src/web-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down