66 */
77
88import { describe , expect , it } from 'vitest' ;
9- import { getVideoEncoderConfigs } from './webcodecs' ;
9+ import { getAudioEncoderConfigs , getSupportedEncoderConfigs , getVideoEncoderConfigs } from './webcodecs' ;
1010
1111describe ( '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} ) ;
0 commit comments