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

Click to resume audio playback. #99

Merged
merged 1 commit into from
Apr 20, 2024
Merged
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
10 changes: 1 addition & 9 deletions lib/playback/context.ts → lib/playback/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Message from "./worker/message"
import workletURL from "./worklet?url"

// NOTE: This must be on the main thread
export class Context {
export class Audio {
context: AudioContext
worklet: Promise<AudioWorkletNode>

Expand Down Expand Up @@ -47,12 +47,4 @@ export class Context {
private on(_event: MessageEvent) {
// TODO
}

async resume() {
await this.context.resume()
}

async close() {
await this.context.close()
}
}
13 changes: 7 additions & 6 deletions lib/playback/backend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="vite/client" />

import * as Message from "./worker/message"
import { Context } from "./context"
import { Audio } from "./audio"

import MediaWorker from "./worker?worker"
import { RingShared } from "../common/ring"
Expand All @@ -22,7 +22,7 @@ export default class Backend {
#worker: Worker

// The audio context, which must be created on the main thread.
#context?: Context
#audio?: Audio

constructor(config: PlayerConfig) {
// TODO does this block the main thread? If so, make this async
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class Backend {
ring: new RingShared(2, sampleRate / 20), // 50ms
}

this.#context = new Context(msg.audio)
this.#audio = new Audio(msg.audio)
}

// TODO only send the canvas if we have a video track
Expand All @@ -65,8 +65,9 @@ export default class Backend {
this.send({ config: msg }, msg.video.canvas)
}

// TODO initialize context now since the user clicked
play() {}
async play() {
await this.#audio?.context.resume()
}

init(init: Init) {
this.send({ init })
Expand All @@ -78,7 +79,7 @@ export default class Backend {

async close() {
this.#worker.terminate()
await this.#context?.close()
await this.#audio?.context.close()
}

// Enforce we're sending valid types to the worker
Expand Down
2 changes: 1 addition & 1 deletion lib/playback/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../tsconfig.json",
"include": ["."],
"exclude": ["worklet"],
"exclude": ["./worklet"],
"compilerOptions": {
"types": ["dom-mediacapture-transform", "dom-webcodecs"]
},
Expand Down
4 changes: 3 additions & 1 deletion web/src/components/watch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ export default function Watch(props: { name: string }) {
player.closed().then(setError).catch(setError)
})

const play = () => usePlayer()?.play()

// NOTE: The canvas automatically has width/height set to the decoded video size.
// TODO shrink it if needed via CSS
return (
<>
<Fail error={error()} />
<canvas ref={canvas} class="aspect-video w-full rounded-lg" />
<canvas ref={canvas} onClick={play} class="aspect-video w-full rounded-lg" />
</>
)
}
Loading