Skip to content

Commit 80f6a09

Browse files
committed
added webcodecs util tests
1 parent eebb030 commit 80f6a09

File tree

3 files changed

+104
-46
lines changed

3 files changed

+104
-46
lines changed

src/utils/webcodecs.spec.ts

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
*/
77

88
import { describe, expect, it } from 'vitest';
9-
import { getVideoEncoderConfigs } from './webcodecs';
9+
import { getAudioEncoderConfigs, getSupportedEncoderConfigs, getVideoEncoderConfigs } from './webcodecs';
1010

1111
describe('The Webcodecs utils', () => {
12-
it('should be able to find render profiles', async () => {
12+
it('should be able to find render profiles (getVideoEncoderConfigs)', async () => {
1313
const profiles = await getVideoEncoderConfigs({
1414
fps: 30,
1515
height: 1080,
@@ -20,4 +20,99 @@ describe('The Webcodecs utils', () => {
2020
expect(profiles.at(0)?.hardwareAcceleration).toBe('prefer-hardware');
2121
expect(profiles.at(-1)?.hardwareAcceleration).toBe('prefer-software');
2222
});
23+
24+
it('should return empty array if video encoder is not defined (getVideoEncoderConfigs)', async () => {
25+
const encoder = window.VideoEncoder;
26+
// @ts-ignore
27+
delete window.VideoEncoder;
28+
29+
const profiles = await getVideoEncoderConfigs({
30+
fps: 30,
31+
height: 1080,
32+
width: 1920,
33+
bitrate: 10e6,
34+
});
35+
expect(profiles.length).toBe(0);
36+
37+
Object.assign(window, { VideoEncoder: encoder });
38+
});
39+
40+
it('should be able to find audio encoding configs (getAudioEncoderConfigs)', async () => {
41+
const profiles = await getAudioEncoderConfigs({
42+
numberOfChannels: 2,
43+
sampleRate: 48000,
44+
bitrate: 128e3,
45+
});
46+
expect(profiles.length).toBe(2);
47+
48+
expect(profiles[0].codec).toBe('mp4a.40.2');
49+
expect(profiles[0].numberOfChannels).toBe(2);
50+
51+
expect(profiles[1].codec).toBe('opus');
52+
expect(profiles[1].numberOfChannels).toBe(2);
53+
});
54+
55+
it('should return empty array if audio encoder is not defined (getAudioEncoderConfigs)', async () => {
56+
const encoder = window.AudioEncoder;
57+
// @ts-ignore
58+
delete window.AudioEncoder;
59+
60+
const profiles = await getAudioEncoderConfigs({
61+
numberOfChannels: 2,
62+
sampleRate: 48000,
63+
bitrate: 128e3,
64+
});
65+
expect(profiles.length).toBe(0);
66+
67+
Object.assign(window, { AudioEncoder: encoder });
68+
});
69+
70+
it('should return audio and video encoder configs (getSupportedEncoderConfigs)', async () => {
71+
const [video, audio] = await getSupportedEncoderConfigs({
72+
video: {
73+
fps: 30,
74+
height: 1080,
75+
width: 1920,
76+
bitrate: 10e6,
77+
},
78+
audio: {
79+
numberOfChannels: 2,
80+
sampleRate: 48000,
81+
bitrate: 128e3,
82+
}
83+
});
84+
85+
expect(video).toBeTruthy();
86+
expect(video.hardwareAcceleration).toBe('prefer-hardware');
87+
expect(video.bitrate).toBe(10e6);
88+
expect(video.framerate).toBe(30);
89+
90+
91+
expect(audio).toBeTruthy();
92+
expect(audio?.codec).toBe('mp4a.40.2');
93+
expect(audio?.numberOfChannels).toBe(2);
94+
});
95+
96+
it('should throw and error if video endocer is not definded (getSupportedEncoderConfigs)', async () => {
97+
const encoder = window.VideoEncoder;
98+
// @ts-ignore
99+
delete window.VideoEncoder;
100+
101+
await expect(() => getSupportedEncoderConfigs({
102+
video: {
103+
fps: 30,
104+
height: 1080,
105+
width: 1920,
106+
bitrate: 10e6,
107+
},
108+
audio: {
109+
numberOfChannels: 2,
110+
sampleRate: 48000,
111+
bitrate: 128e3,
112+
}
113+
})).rejects.toThrowError();
114+
115+
116+
Object.assign(window, { VideoEncoder: encoder });
117+
});
23118
});

src/utils/xml.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

vitest.mocks.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ export class AudioEncoderMock {
144144
public async flush(): Promise<void> {
145145
return;
146146
}
147+
148+
public static async isConfigSupported(config: VideoEncoderConfig): Promise<VideoEncoderSupport> {
149+
return {
150+
supported: true,
151+
config
152+
};
153+
}
147154
}
148155

149156
export class AudioDataMock {

0 commit comments

Comments
 (0)