Skip to content

Commit

Permalink
Stop screencast recorder when user removes permission / stops using b…
Browse files Browse the repository at this point in the history
…rowser button. (streamlit#4180)
  • Loading branch information
tvst authored Dec 20, 2021
1 parent b0ff1af commit 497e3fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
26 changes: 16 additions & 10 deletions frontend/src/hocs/withScreencast/withScreencast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ function withScreencast(
public startRecording = async (): Promise<any> => {
const { recordAudio } = this.state

this.recorder = new ScreenCastRecorder({ recordAudio })
this.recorder = new ScreenCastRecorder({
recordAudio,
onErrorOrStop: () => this.stopRecording(),
})

try {
await this.recorder.initialize()
Expand Down Expand Up @@ -125,15 +128,18 @@ function withScreencast(
})
}

if (
currentState === "RECORDING" &&
this.recorder.getState() !== "inactive"
) {
outputBlob = await this.recorder.stop()
this.setState({
outputBlob,
currentState: "PREVIEW_FILE",
})
if (currentState === "RECORDING") {
if (this.recorder.getState() === "inactive") {
this.setState({
currentState: "OFF",
})
} else {
outputBlob = await this.recorder.stop()
this.setState({
outputBlob,
currentState: "PREVIEW_FILE",
})
}
}
}

Expand Down
19 changes: 17 additions & 2 deletions frontend/src/lib/ScreenCastRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const BLOB_TYPE = "video/webm"

interface ScreenCastRecorderOptions {
recordAudio: boolean
onErrorOrStop: () => void
}

class ScreenCastRecorder {
Expand All @@ -31,6 +32,8 @@ class ScreenCastRecorder {

private mediaRecorder: MediaRecorder | null

private onErrorOrStopCallback: () => void

/** True if the current browser likely supports screencasts. */
public static isSupportedBrowser(): boolean {
return (
Expand All @@ -42,8 +45,9 @@ class ScreenCastRecorder {
)
}

constructor({ recordAudio }: ScreenCastRecorderOptions) {
constructor({ recordAudio, onErrorOrStop }: ScreenCastRecorderOptions) {
this.recordAudio = recordAudio
this.onErrorOrStopCallback = onErrorOrStop

this.inputStream = null
this.recordedChunks = []
Expand Down Expand Up @@ -102,10 +106,21 @@ class ScreenCastRecorder {
return false
}

const logRecorderError = (e: any): void => {
logWarning(`mediaRecorder.start threw an error: ${e}`)
}

this.mediaRecorder.onerror = (e: any): void => {
logRecorderError(e)
this.onErrorOrStopCallback()
}

this.mediaRecorder.onstop = (): void => this.onErrorOrStopCallback()

try {
this.mediaRecorder.start()
} catch (e) {
logWarning(`mediaRecorder.start threw an error: ${e}`)
logRecorderError(e)
return false
}

Expand Down

0 comments on commit 497e3fc

Please sign in to comment.