Skip to content

Commit

Permalink
Replace throw with console.error for clear console logging, handle '.…
Browse files Browse the repository at this point in the history
…' at the beginning of formats
  • Loading branch information
calebfoss committed Mar 7, 2022
1 parent 9f4da86 commit b8f74c2
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions p5.videorecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ p5.VideoRecorder = class {
set input(input) {
if (this.#stream !== undefined && this.#stream === input) return;
if (this.recording)
throw "VideoRecorder input was assigned a new value while recording. \
Call stop() before changine the input";
return console.error(
"VideoRecorder input was assigned a new value while recording. \
Call stop() before changing the input"
);
if (input === undefined) {
if (typeof drawingContext?.canvas === "undefined")
throw "VideoRecorder couldn't find canvas to record";
this.addInput(drawingContext.canvas);
console.error("VideoRecorder couldn't find canvas to record");
else this.input = drawingContext.canvas;
if (typeof soundOut !== "undefined" && soundOut.output !== undefined)
this.addInput(soundOut.output);
return;
Expand All @@ -48,10 +50,15 @@ p5.VideoRecorder = class {
return this.#recorder.mimeType;
}
set format(format) {
if (format.charAt(0) === ".") format = format.slice(1);
if (p5.VideoRecorder.isSupported(format) == false)
throw `Video format ${format} not supported`;
return console.error(
`Video format ${format} is not supported on this browser`
);
if (this.recording)
throw "Can't set format while video recorder is recording";
return console.error(
"Can't set format while video recorder is recording"
);
this.#mimeType = format.split("/").length > 1 ? format : `video/${format}`;
if (this.#stream !== undefined) this.#createRecorder();
}
Expand All @@ -60,7 +67,9 @@ p5.VideoRecorder = class {
}
set onFileReady(callback) {
if (typeof callback !== "function")
throw `VideoRecorder onFileReady must be of type function but was assigned to ${typeof callback}`;
return console.error(
`VideoRecorder onFileReady must be of type function but was assigned to ${typeof callback}`
);
this.#onFileReady = callback;
}
get recording() {
Expand Down Expand Up @@ -88,7 +97,9 @@ p5.VideoRecorder = class {
}
erase() {
if (this.recording)
throw "erase() was called while the video recorder was recording. Call stop() before erasing.";
return console.error(
"erase() was called while the video recorder was recording. Call stop() before erasing."
);
this.#chunks = [];
}
canRecord(input) {
Expand Down Expand Up @@ -119,18 +130,29 @@ p5.VideoRecorder = class {
return this.#stream;
}
#inputToStream(input) {
if (this.canRecord(input) === false)
throw "VideoRecorder input cannot be recorded in this browser";
if (this.canRecord(input) == false)
return console.error(
`Selected VideoRecorder input of type ${typeof input} cannot be recorded in this browser`
);
if (input instanceof MediaStream) return input;
if (input instanceof AudioNode) return this.#audioNodeToStream(input);
if (typeof input.captureStream === "function")
return this.#mediaElementToStream(input);
if (input instanceof p5.Element)
return this.#mediaElementToStream(input.elt);
}
isSupported(format) {
return p5.VideoRecorder.isSupported(format);
}
static isSupported(format) {
if (format.charAt(0) === ".") format = format.slice(1);
return MediaRecorder.isTypeSupported(
format.split("/").length > 1 ? format : `video/${format}`
);
}
#mediaElementToStream(mediaElement) {
if (typeof mediaElement.captureStream !== "function")
throw `Can't capture stream from input ${mediaElement}`;
return console.error(`Can't capture stream from input ${mediaElement}`);
return mediaElement.captureStream();
}
pause() {
Expand All @@ -141,8 +163,10 @@ p5.VideoRecorder = class {
}
save(filename) {
if (this.#blob === undefined)
throw "save() was called before a video file was created.\
Use onFileReady event to call a function when the video file is ready.";
return console.error(
"save() was called before a video file was created.\
Use onFileReady event to call a function when the video file is ready."
);
let extension = this.#mimeType.match(/\/([^;]*)/)?.[1];
[filename, extension] = p5.prototype._checkFileExtension(
filename,
Expand All @@ -154,17 +178,11 @@ p5.VideoRecorder = class {
this.erase();
this.#recorder.start();
}
isSupported(format) {
return p5.VideoRecorder.isSupported(format);
}
static isSupported(format) {
return MediaRecorder.isTypeSupported(
format.split("/").length > 1 ? format : `video/${format}`
);
}
stop() {
if (!this.recording)
throw "stop() was called while the video recorder was not recording. Call start() before stopping.";
console.assert(
this.recording,
"stop() was called while the video recorder was not recording. Call start() before stopping."
);
this.#recorder.stop();
}
};

0 comments on commit b8f74c2

Please sign in to comment.