Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default mimeType if none is specified #3495

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-ears-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"jspsych": patch
---

add a default `mimeType` of `"video/webm" to `initializeCameraRecorder()`
3 changes: 1 addition & 2 deletions docs/reference/jspsych-pluginAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,7 @@ None.

#### Description

Generates a `MediaRecorder` object from provided `MediaStream` and stores this for access via [`getCameraRecorder()`](#getcamerarecorder).

Generates a `MediaRecorder` object from provided `MediaStream` and stores this for access via [`getCameraRecorder()`](#getcamerarecorder). By default, `mimeType` is set to `"video/webm"`.
#### Example

```javascript
Expand Down
5 changes: 4 additions & 1 deletion examples/extension-record-video.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
const jsPsych = initJsPsych({
extensions: [
{type: jsPsychExtensionRecordVideo}
]
],
on_finish: function() {
jsPsych.data.displayData();
}
});

const initCamera = {
Expand Down
29 changes: 28 additions & 1 deletion packages/jspsych/src/modules/plugin-api/MediaAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,38 @@ export class MediaAPI {
private camera_recorder: MediaRecorder = null;

initializeCameraRecorder(stream: MediaStream, opts?: MediaRecorderOptions) {
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;
}
Expand Down