implement compatible mimetype searching if no mimetype provided

This commit is contained in:
jade 2025-02-08 10:18:36 -05:00
parent 1f535826a2
commit d258a29aae

View File

@ -284,17 +284,38 @@ export class MediaAPI {
private camera_recorder: MediaRecorder = null;
initializeCameraRecorder(stream: MediaStream, opts?: MediaRecorderOptions) {
if (!opts) {
opts = { mimeType: "video/webm" };
} else if (!opts.mimeType) {
opts.mimeType = "video/webm";
let mimeType = this.getCompatibleMimeType() || "video/webm";
const recorderOptions: MediaRecorderOptions = {
...opts,
mimeType
}
this.camera_stream = stream;
const recorder = new MediaRecorder(stream, opts);
const recorder = new MediaRecorder(stream, recorderOptions);
this.camera_recorder = recorder;
}
// mimetype checking code adapted from https://github.com/lookit/lookit-jspsych/blob/develop/packages/record/src/videoConfig.ts#L673-L699
/** returns a compatible mimetype string, or null if none from the array are supported. */
private getCompatibleMimeType(): string {
const types = [
// chrome firefox edge
"video/webm;codecs=vp9,opus",
"video/webm;codecs=vp8,opus",
// general
'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',
// safari
"video/mp4;codecs=h264,aac",
"video/mp4;codecs=hevc,aac",
]
for (const mimeType of types) {
if (MediaRecorder.isTypeSupported(mimeType)) {
return mimeType;
}
}
return null;
}
getCameraStream(): MediaStream {
return this.camera_stream;
}