Skip to content

Commit 3af6768

Browse files
SomeBody16fnowakowevujici
authored
feat: add isCapableOfReceivingVideoCodec (#14)
* feat: add CodecInfo class with methods to check codec availability * refactor: remove CodecInfo class and related tests * refactor: update WebCapabilities methods for codec support checks * chore(web-capabilities): add the correct mimeType type * test(web-capabilities): add test for undefined return --------- Co-authored-by: Filip Nowakowski <fnowakow@cisco.com> Co-authored-by: evujici <evujici@cisco.com>
1 parent f0961eb commit 3af6768

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/web-capabilities.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,59 @@ describe('WebCapabilities', () => {
151151
}
152152
);
153153
});
154+
describe('isCapableOfReceivingVideoCodec', () => {
155+
afterEach(() => {
156+
// Clean up window modifications
157+
delete (window as Window & { RTCRtpReceiver?: unknown }).RTCRtpReceiver;
158+
});
159+
160+
it('should return CAPABLE when the codec is supported', () => {
161+
expect.assertions(1);
162+
163+
Object.defineProperty(window, 'RTCRtpReceiver', {
164+
writable: true,
165+
value: {
166+
getCapabilities: jest.fn().mockReturnValue({
167+
codecs: [{ mimeType: 'video/AV1' }],
168+
} as unknown as RTCRtpCapabilities),
169+
},
170+
});
171+
172+
expect(WebCapabilities.isCapableOfReceivingVideoCodec('video/AV1')).toBe(
173+
CapabilityState.CAPABLE
174+
);
175+
});
176+
177+
it('should return NOT_CAPABLE when the codec is not supported', () => {
178+
expect.assertions(1);
179+
180+
Object.defineProperty(window, 'RTCRtpReceiver', {
181+
writable: true,
182+
value: {
183+
getCapabilities: jest.fn().mockReturnValue({
184+
codecs: [{ mimeType: 'video/H264' }],
185+
} as unknown as RTCRtpCapabilities),
186+
},
187+
});
188+
189+
expect(WebCapabilities.isCapableOfReceivingVideoCodec('video/AV1')).toBe(
190+
CapabilityState.NOT_CAPABLE
191+
);
192+
});
193+
194+
it('should return NOT_CAPABLE when getCapabilities returns undefined', () => {
195+
expect.assertions(1);
196+
197+
Object.defineProperty(window, 'RTCRtpReceiver', {
198+
writable: true,
199+
value: {
200+
getCapabilities: jest.fn().mockReturnValue(undefined),
201+
},
202+
});
203+
204+
expect(WebCapabilities.isCapableOfReceivingVideoCodec('video/AV1')).toBe(
205+
CapabilityState.NOT_CAPABLE
206+
);
207+
});
208+
});
154209
});

src/web-capabilities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ export class WebCapabilities {
8080
return CapabilityState.CAPABLE;
8181
}
8282

83+
/**
84+
* Checks whether the browser is capable of receiving a specific video codec.
85+
*
86+
* @param mimeType - The MIME type of the codec to check.
87+
* @returns A {@link CapabilityState}.
88+
*/
89+
static isCapableOfReceivingVideoCodec(
90+
mimeType: RTCRtpCodecCapability['mimeType']
91+
): CapabilityState {
92+
const codecs = RTCRtpReceiver.getCapabilities('video')?.codecs || [];
93+
return codecs.some((codec) => codec.mimeType === mimeType)
94+
? CapabilityState.CAPABLE
95+
: CapabilityState.NOT_CAPABLE;
96+
}
97+
8398
/**
8499
* Checks whether the browser supports encoded stream transforms.
85100
*

0 commit comments

Comments
 (0)