mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-13 01:05:37 +02:00
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>
64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
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()
|
|
}
|
|
}
|