MCPcopy Create free account
hub / github.com/Vanilagy/webcodecs-polyfill / createCodecContext

Method createCodecContext

src/audio_encoder.ts:117–150  ·  view source on GitHub ↗
(config: AudioEncoderConfig)

Source from the content-addressed store, hash-verified

115 }
116
117 private static createCodecContext(config: AudioEncoderConfig) {
118 const inferred = inferCodecFromCodecString(config.codec);
119 if (!inferred) {
120 return null;
121 }
122
123 const codec = Codec.findEncoder(inferred);
124 if (!codec) {
125 return null;
126 }
127
128 let sampleFormat = AV_SAMPLE_FMT_FLTP;
129 if (codec.sampleFormats && !codec.sampleFormats.includes(AV_SAMPLE_FMT_FLTP)) {
130 sampleFormat = codec.sampleFormats[0];
131 }
132
133 const codecContext = new CodecContext();
134 codecContext.allocContext3(codec);
135
136 codecContext.sampleRate = config.sampleRate;
137 codecContext.channelLayout = getChannelLayout(config.numberOfChannels);
138 codecContext.codecType = AVMEDIA_TYPE_AUDIO;
139 codecContext.codecId = inferred;
140 codecContext.sampleFormat = sampleFormat;
141 codecContext.timeBase = new Rational(1, 1e6);
142 codecContext.bitRate = BigInt(config.bitrate ?? getDecentAudioBitrate(inferred));
143
144 if (config.bitrateMode === 'constant') {
145 codecContext.rcMinRate = codecContext.bitRate;
146 codecContext.rcMaxRate = codecContext.bitRate;
147 }
148
149 return { codecContext, sampleFormat };
150 }
151
152 /**
153 * Checks if the encoder can support the given configuration.

Callers 2

isConfigSupportedMethod · 0.95
configureInternalMethod · 0.45

Calls 3

getChannelLayoutFunction · 0.85
getDecentAudioBitrateFunction · 0.85

Tested by

no test coverage detected