Skip to content

Commit d258a29

Browse files
committed
implement compatible mimetype searching if no mimetype provided
1 parent 1f53582 commit d258a29

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

packages/jspsych/src/modules/plugin-api/MediaAPI.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,38 @@ export class MediaAPI {
284284
private camera_recorder: MediaRecorder = null;
285285

286286
initializeCameraRecorder(stream: MediaStream, opts?: MediaRecorderOptions) {
287-
if (!opts) {
288-
opts = { mimeType: "video/webm" };
289-
} else if (!opts.mimeType) {
290-
opts.mimeType = "video/webm";
287+
let mimeType = this.getCompatibleMimeType() || "video/webm";
288+
const recorderOptions: MediaRecorderOptions = {
289+
...opts,
290+
mimeType
291291
}
292292

293293
this.camera_stream = stream;
294-
const recorder = new MediaRecorder(stream, opts);
294+
const recorder = new MediaRecorder(stream, recorderOptions);
295295
this.camera_recorder = recorder;
296296
}
297297

298+
// mimetype checking code adapted from https://github.com/lookit/lookit-jspsych/blob/develop/packages/record/src/videoConfig.ts#L673-L699
299+
/** returns a compatible mimetype string, or null if none from the array are supported. */
300+
private getCompatibleMimeType(): string {
301+
const types = [
302+
// chrome firefox edge
303+
"video/webm;codecs=vp9,opus",
304+
"video/webm;codecs=vp8,opus",
305+
// general
306+
'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',
307+
// safari
308+
"video/mp4;codecs=h264,aac",
309+
"video/mp4;codecs=hevc,aac",
310+
]
311+
for (const mimeType of types) {
312+
if (MediaRecorder.isTypeSupported(mimeType)) {
313+
return mimeType;
314+
}
315+
}
316+
return null;
317+
}
318+
298319
getCameraStream(): MediaStream {
299320
return this.camera_stream;
300321
}

0 commit comments

Comments
 (0)