diff options
author | metamuffin <metamuffin@disroot.org> | 2022-10-03 11:28:16 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-10-03 11:28:16 +0200 |
commit | 4e99a3325318c902cd78ea9f760f46d79acde5c0 (patch) | |
tree | cc2bc54f4a0eb27db2b5d38dfbb785c1e9b84bd6 /client-web/source/resource/mod.ts | |
parent | fa44b02da29a0bd1b60026d4f6ffd6c9748a09da (diff) | |
download | keks-meet-4e99a3325318c902cd78ea9f760f46d79acde5c0.tar keks-meet-4e99a3325318c902cd78ea9f760f46d79acde5c0.tar.bz2 keks-meet-4e99a3325318c902cd78ea9f760f46d79acde5c0.tar.zst |
riesencommit (part 1)
Diffstat (limited to 'client-web/source/resource/mod.ts')
-rw-r--r-- | client-web/source/resource/mod.ts | 167 |
1 files changed, 97 insertions, 70 deletions
diff --git a/client-web/source/resource/mod.ts b/client-web/source/resource/mod.ts index 716d42b..f99a252 100644 --- a/client-web/source/resource/mod.ts +++ b/client-web/source/resource/mod.ts @@ -6,84 +6,111 @@ /// <reference lib="dom" /> import { ProvideInfo } from "../../../common/packets.d.ts" -import { ediv } from "../helper.ts"; import { TrackHandle } from "../track_handle.ts"; -import { LocalUser } from "../user/local.ts"; -import { User } from "../user/mod.ts" import { RemoteUser } from "../user/remote.ts" -import { TrackResource } from "./track.ts"; +import { resource_file } from "./file.ts"; +import { resource_track } from "./track.ts"; -export type ChannelState = "enabled" | "await_enable" | "disabled" | "await_disable" -export abstract class Resource extends EventTarget { - local: boolean - el: HTMLElement = ediv({ class: ["channel"] }) - inner_el?: HTMLElement +// export abstract class Resource extends EventTarget { +// abstract transport_method: TransportMethod +// local: boolean +// el: HTMLElement = ediv({ class: ["channel"] }) +// inner_el?: HTMLElement - constructor( - public user: User, - public info: ProvideInfo, - ) { - super() - this.local = this.user instanceof LocalUser - const button = document.createElement("button") - button.onclick = () => { - this.state == "enabled" ? this.request_stop() : this.request() - } - this.addEventListener("statechange", () => { - if (this.user instanceof LocalUser) button.textContent = "End", button.disabled = false - else if (this.state == "enabled") button.textContent = "Disable", button.disabled = false - else if (this.state == "disabled") button.textContent = `Enable ${this.info.kind}`, button.disabled = false - else button.textContent = "Working…", button.disabled = true; - }) - this.dispatchEvent(new CustomEvent("statechange")) - this.el.append(button) - } +// constructor( +// public user: User, +// public info: ProvideInfo, +// ) { +// super() +// this.local = this.user instanceof LocalUser +// const button = document.createElement("button") +// button.onclick = () => { +// this.state == "enabled" ? this.request_stop() : this.request() +// } +// this.addEventListener("statechange", () => { +// if (this.local) button.textContent = "End", button.disabled = false +// else if (this.state == "enabled") button.textContent = "Disable", button.disabled = false +// else if (this.state == "disabled") button.textContent = `Enable ${this.info.kind}`, button.disabled = false +// else button.textContent = "Working…", button.disabled = true; +// }) +// this.dispatchEvent(new CustomEvent("statechange")) +// this.el.append(button) +// } - static create(user: User, info: ProvideInfo): Resource { - if (info.kind == "audio" || info.kind == "video") return new TrackResource(user, info) - else throw new Error("blub"); - } +// static create(user: User, info: ProvideInfo): Resource { +// } - private _state: ChannelState = "disabled" - get state() { return this._state } - set state(value: ChannelState) { - const old_value = this._state - this._state = value - if (value != old_value) this.dispatchEvent(new CustomEvent("statechange")) - } +// private _state: ChannelState = "disabled" +// get state() { return this._state } +// set state(value: ChannelState) { +// const old_value = this._state +// this._state = value +// if (value != old_value) this.dispatchEvent(new CustomEvent("statechange")) +// } - private _track?: TrackHandle - get track() { return this._track } - set track(value: TrackHandle | undefined) { - const handle_end = () => { - this.track = undefined - this.state = "disabled" - this.inner_el?.remove() - if (this.user instanceof LocalUser) this.destroy() - } - this._track?.removeEventListener("ended", handle_end) - this._track = value - if (value) this.el.append(this.inner_el = this.on_track(value)) - if (value) this.state = "enabled" - else this.state = "disabled" - this._track?.addEventListener("ended", handle_end) - } +// private _channel?: TrackHandle | RTCDataChannel +// get channel() { return this._channel } +// set channel(value: TrackHandle | RTCDataChannel | undefined) { +// const handle_end = () => { +// this.channel = undefined +// this.state = "disabled" +// this.inner_el?.remove() +// if (this.user instanceof LocalUser) this.destroy() +// } +// this._channel?.removeEventListener("ended", handle_end) +// this._channel = value +// if (value) this.el.append(this.inner_el = this.on_channel(value)) +// if (value) this.state = "enabled" +// else this.state = "disabled" +// this._channel?.addEventListener("ended", handle_end) +// } - abstract on_track(_track: TrackHandle): HTMLElement +// abstract on_channel(channel: TrackHandle | RTCDataChannel): HTMLElement +// abstract on_request(): void; - destroy() { this.dispatchEvent(new CustomEvent("destroy")) } +// destroy() { this.dispatchEvent(new CustomEvent("destroy")) } - request() { - if (!(this.user instanceof RemoteUser)) return - this.state = "await_enable" - this.user.send_to({ request: { id: this.info.id } }) - } - request_stop() { - if (this.user instanceof RemoteUser) { - this.state = "await_disable" - this.user.send_to({ request_stop: { id: this.info.id } }) - } else if (this.user instanceof LocalUser) { - this.destroy() - } - } +// request() { +// if (!(this.user instanceof RemoteUser)) return +// this.state = "await_enable" +// this.user.send_to({ request: { id: this.info.id } }) +// } +// request_stop() { +// if (this.user instanceof RemoteUser) { +// this.state = "await_disable" +// this.user.send_to({ request_stop: { id: this.info.id } }) +// } else if (this.user instanceof LocalUser) { +// this.destroy() +// } +// } +// } + +export type TransportMethod = "data-channel" | "track" +export type RemoteResourceState = "connected" | "disconnected" | "await_connect" | "await_disconnect" +export interface ResourceHandlerDecl { + kind: string + new_remote(info: ProvideInfo, user: RemoteUser, enable: () => void): RemoteResource +} +export interface RemoteResource { + el: HTMLElement + info: ProvideInfo, + on_statechange(state: RemoteResourceState): void + on_enable(t: TrackHandle | RTCDataChannel, disable: () => void): void +} +export interface LocalResource { + el: HTMLElement + info: ProvideInfo, + destroy(): void + on_request(user: RemoteUser, create_channel: (label: string) => RTCDataChannel): TrackHandle | RTCDataChannel } + +const RESOURCE_HANDLERS: ResourceHandlerDecl[] = [resource_file, resource_track] + +export function new_remote_resource(user: RemoteUser, info: ProvideInfo): RemoteResource | undefined { + const h = RESOURCE_HANDLERS.find(h => h.kind == info.kind) + if (!h) return undefined + const res = h.new_remote(info, user, () => { + user.request_resource(res) + }) + return res +}
\ No newline at end of file |