Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
interim17 committed Nov 16, 2023
1 parent 55e695a commit 81af32a
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/simularium/StreamRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class StreamRecorder {
private canvasEl: HTMLCanvasElement;
private encoder: VideoEncoder;
private frameCounter: number;
// TODO: this is working but the linter is throwing a type error, what is best practice here?
// TODO: this is working but the linter is throwing a type error, not sure if that's a major issue?
private trackProcessor: MediaStreamTrackProcessor;

Check failure on line 8 in src/simularium/StreamRecorder.ts

View workflow job for this annotation

GitHub Actions / Type Check

Cannot find name 'MediaStreamTrackProcessor'.
private reader: ReadableStreamDefaultReader<VideoFrame>;

Check failure on line 9 in src/simularium/StreamRecorder.ts

View workflow job for this annotation

GitHub Actions / Type Check

Property 'reader' has no initializer and is not definitely assigned in the constructor.
private muxer?: Muxer<ArrayBufferTarget>;
Expand Down Expand Up @@ -36,27 +36,28 @@ class StreamRecorder {
// returns a media stream captured from the viewer canvas
// more details here: https://developer.chrome.com/en/articles/webcodecs/

// fixed at 30 fps for now
// TODO: in future we could let users specify config options?
const fps = 30;
const config: VideoEncoderConfig = {
codec: "avc1.420028",
width: this.canvasEl.width,
height: this.canvasEl.height,
};

const stream = this.canvasEl.captureStream(fps);
const track = stream.getVideoTracks()[0];
// MediaStreamTrackProcessor makes the captured stream readable
this.trackProcessor = new MediaStreamTrackProcessor(track);

Check failure on line 50 in src/simularium/StreamRecorder.ts

View workflow job for this annotation

GitHub Actions / Type Check

Cannot find name 'MediaStreamTrackProcessor'.
this.reader = this.trackProcessor.readable.getReader();

const config: VideoEncoderConfig = {
codec: "avc1.420028", // TODO: do we want different configs?
width: this.canvasEl.width,
height: this.canvasEl.height,
};
const { supported, config: supportedConfig } =
await VideoEncoder.isConfigSupported(config);
if (supported && supportedConfig) {
this.encoder.configure(config);
} else {
console.log("unsupported config");
}

// Muxer will handle the conversion from raw video data to mp4
this.muxer = new Muxer({
target: new ArrayBufferTarget(),
video: {
Expand All @@ -78,13 +79,13 @@ class StreamRecorder {
if (result.done) break;

const frame = result.value;
// Drop the frame if encoder is overwhelmed
if (this.encoder.encodeQueueSize > 2) {
frame.close(); // Drop the frame if encoder is overwhelmed
frame.close();
} else {
// if recording and the queue is healthy, encode the frame and then close it
// don't entirely understand the use of keyFrames
// this application is straight from the docs and will make the resulting file larger but also scrubbable
// can optimize/alter these parameters?
// this application of keyframes is straight from the docs
// and will make the resulting file larger but also scrubbable
// We could optimize/alter the frequency of keyframes?
this.frameCounter++;
const keyFrame = this.frameCounter % 150 === 0;
this.encoder.encode(frame, { keyFrame });
Expand All @@ -106,7 +107,7 @@ class StreamRecorder {
this.muxer.finalize();
const { buffer } = this.muxer.target;

// create a blob from the muxer output and download it
// Create a blob from the muxer output and download it
const videoBlob = new Blob([buffer], { type: "video/mp4" });
const url = URL.createObjectURL(videoBlob);
this.download(this.trajectoryTitle + ".mp4", url);
Expand Down

0 comments on commit 81af32a

Please sign in to comment.