mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 16:55:37 +02:00
✨ feat: Add streaming support (#125)
This adds: - [x] Keyboard and mouse handling on the frontend - [x] Video and audio streaming from the backend to the frontend - [x] Input server that works with Websockets Update - 17/11 - [ ] Master docker container to run this - [ ] Steam runtime - [ ] Entrypoint.sh --------- Co-authored-by: Kristian Ollikainen <14197772+DatCaptainHorse@users.noreply.github.com> Co-authored-by: Kristian Ollikainen <DatCaptainHorse@users.noreply.github.com>
This commit is contained in:
63
packages/moq/playback/player.ts
Normal file
63
packages/moq/playback/player.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import * as Catalog from "../karp/catalog"
|
||||
import type { Connection } from "../transfork/connection"
|
||||
import { Broadcast } from "./broadcast"
|
||||
|
||||
export interface PlayerConfig {
|
||||
connection: Connection
|
||||
path: string[]
|
||||
canvas: HTMLCanvasElement
|
||||
}
|
||||
|
||||
// This class must be created on the main thread due to AudioContext.
|
||||
export class Player {
|
||||
#config: PlayerConfig
|
||||
#running: Promise<void>
|
||||
#active?: Broadcast
|
||||
|
||||
constructor(config: PlayerConfig) {
|
||||
this.#config = config
|
||||
this.#running = this.#run()
|
||||
}
|
||||
|
||||
async #run() {
|
||||
const announced = await this.#config.connection.announced(this.#config.path)
|
||||
|
||||
let activeId = -1
|
||||
|
||||
for (;;) {
|
||||
const announce = await announced.next()
|
||||
if (!announce) break
|
||||
|
||||
if (announce.path.length === this.#config.path.length) {
|
||||
throw new Error("expected resumable broadcast")
|
||||
}
|
||||
|
||||
const path = announce.path.slice(0, this.#config.path.length + 1)
|
||||
|
||||
const id = Number.parseInt(path[path.length - 1])
|
||||
if (id <= activeId) continue
|
||||
|
||||
const catalog = await Catalog.fetch(this.#config.connection, path)
|
||||
|
||||
this.#active?.close()
|
||||
this.#active = new Broadcast(this.#config.connection, catalog, this.#config.canvas)
|
||||
activeId = id
|
||||
}
|
||||
|
||||
this.#active?.close()
|
||||
}
|
||||
|
||||
close() {
|
||||
this.#config.connection.close()
|
||||
this.#active?.close()
|
||||
this.#active = undefined
|
||||
}
|
||||
|
||||
async closed() {
|
||||
await Promise.any([this.#running, this.#config.connection.closed()])
|
||||
}
|
||||
|
||||
unmute() {
|
||||
this.#active?.unmute()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user