Skip to content

Commit

Permalink
feature: wait for keyframe when restarting playback
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaquinBCh committed Nov 26, 2024
1 parent 668d684 commit c04145f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/playback/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default class Backend {
}

async pause() {
this.send({ pause: true })
await this.#audio?.context.suspend()
}

Expand Down
11 changes: 4 additions & 7 deletions lib/playback/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class Player {
this.#muted = false
this.#paused = false


const abort = new Promise<void>((resolve, reject) => {
this.#close = resolve
this.#abort = reject
Expand All @@ -63,7 +62,7 @@ export class Player {
this.#running = abort.catch(this.#close)

this.#run().catch((err) => {
console.error('Error in #run():', err)
console.error("Error in #run():", err)
this.#abort(err)
})
}
Expand Down Expand Up @@ -125,7 +124,7 @@ export class Player {
async #trackTask(track: Catalog.Track) {
if (!track.namespace) throw new Error("track has no namespace")

if (this.#paused) return;
if (this.#paused) return

const kind = Catalog.isVideoTrack(track) ? "video" : Catalog.isAudioTrack(track) ? "audio" : "unknown"
if (kind == "audio" && this.#muted) return
Expand Down Expand Up @@ -188,7 +187,6 @@ export class Player {
task.finally(() => {
this.#trackTasks.delete(track.name)
})

}

getCatalog() {
Expand Down Expand Up @@ -291,13 +289,12 @@ export class Player {
*/

async play() {
if (this.#paused){
if (this.#paused) {
this.#paused = false
this.subscribeFromTrackName(this.#videoTrackName)
this.subscribeFromTrackName(this.#audioTrackName)
this.#backend.play()
}
else {
} else {
this.unsubscribeFromTrack(this.#videoTrackName)
this.unsubscribeFromTrack(this.#audioTrackName)
this.#backend.pause()
Expand Down
8 changes: 8 additions & 0 deletions lib/playback/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Worker {
this.#onInit(msg.init)
} else if (msg.segment) {
this.#onSegment(msg.segment).catch(console.warn)
} else if (msg.pause) {
this.#onPause(msg.pause)
} else {
throw new Error(`unknown message: + ${JSON.stringify(msg)}`)
}
Expand Down Expand Up @@ -100,6 +102,12 @@ class Worker {
// We done.
await segment.close()
}

#onPause(pause: boolean) {
if (this.#video && pause) {
this.#video.pause()
}
}
}

// Pass all events to the worker
Expand Down
1 change: 1 addition & 0 deletions lib/playback/worker/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface ToWorker {
// Sent on each init/data stream
init?: Init
segment?: Segment
pause?: boolean

/*
// Sent to control playback
Expand Down
5 changes: 5 additions & 0 deletions lib/playback/worker/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export class Renderer {
this.#run().catch(console.error)
}

pause() {
console.log("pause")
this.#waitingForKeyframe = true
}

async #run() {
const reader = this.#timeline.frames.pipeThrough(this.#queue).getReader()
for (;;) {
Expand Down

0 comments on commit c04145f

Please sign in to comment.