|
| 1 | +import { Readable } from 'node:stream' |
| 2 | +import { MediaStreamTrackEvent } from './Events.js' |
| 3 | +import { Track } from '../lib/index.js' |
| 4 | + |
| 5 | +export class MediaStreamTrack extends EventTarget implements globalThis.MediaStreamTrack { |
| 6 | + media |
| 7 | + track: Track |
| 8 | + stream = new Readable({ read: () => {} }) |
| 9 | + #kind: string |
| 10 | + #label: string |
| 11 | + #id = crypto.randomUUID() |
| 12 | + contentHint = '' |
| 13 | + |
| 14 | + onmute |
| 15 | + onunmute |
| 16 | + onended |
| 17 | + |
| 18 | + constructor ({ kind, label }: { kind: string, label: string }) { |
| 19 | + super() |
| 20 | + if (!kind) throw new TypeError("Failed to construct 'MediaStreamTrack': Failed to read the 'kind' property from 'MediaStreamTrackInit': Required member is undefined.") |
| 21 | + this.#kind = kind |
| 22 | + this.#label = label |
| 23 | + |
| 24 | + this.addEventListener('ended', e => { |
| 25 | + this.onended?.(e) |
| 26 | + this.track?.close() |
| 27 | + this.stream.destroy() |
| 28 | + }) |
| 29 | + this.stream.on('close', () => { |
| 30 | + this.stop() |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + async applyConstraints (): Promise<void> { |
| 35 | + console.warn('Constraints unsupported, ignored') |
| 36 | + } |
| 37 | + |
| 38 | + stop (): void { |
| 39 | + this.track?.close() |
| 40 | + this.stream.destroy() |
| 41 | + this.dispatchEvent(new Event('ended')) |
| 42 | + } |
| 43 | + |
| 44 | + getSettings (): globalThis.MediaTrackSettings { |
| 45 | + console.warn('Settings upsupported, ignored') |
| 46 | + return {} |
| 47 | + } |
| 48 | + |
| 49 | + getConstraints (): globalThis.MediaTrackConstraints { |
| 50 | + console.warn('Constraints unsupported, ignored') |
| 51 | + return {} |
| 52 | + } |
| 53 | + |
| 54 | + getCapabilities (): globalThis.MediaTrackCapabilities { |
| 55 | + console.warn('Capabilities unsupported, ignored') |
| 56 | + return {} |
| 57 | + } |
| 58 | + |
| 59 | + clone (): this { |
| 60 | + console.warn('Track clonning is unsupported, returned this instance') |
| 61 | + return this |
| 62 | + } |
| 63 | + |
| 64 | + get kind (): string { |
| 65 | + return this.#kind |
| 66 | + } |
| 67 | + |
| 68 | + get enabled (): boolean | null { |
| 69 | + return this.track?.isOpen() |
| 70 | + } |
| 71 | + |
| 72 | + set enabled (_) { |
| 73 | + console.warn('Track enabling and disabling is unsupported, ignored') |
| 74 | + } |
| 75 | + |
| 76 | + get muted (): boolean { |
| 77 | + return false |
| 78 | + } |
| 79 | + |
| 80 | + get id (): string { |
| 81 | + return this.#id |
| 82 | + } |
| 83 | + |
| 84 | + get label (): string { |
| 85 | + return this.#label |
| 86 | + } |
| 87 | + |
| 88 | + get readyState (): 'ended' | 'live' { |
| 89 | + return this.track?.isClosed() ? 'ended' : 'live' |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @class |
| 95 | + * @implements {globalThis.MediaStream} |
| 96 | + */ |
| 97 | +export class MediaStream extends EventTarget { |
| 98 | + #active = true |
| 99 | + #id = crypto.randomUUID() |
| 100 | + #tracks = new Set<MediaStreamTrack>() |
| 101 | + onaddtrack |
| 102 | + onremovetrack |
| 103 | + onactive |
| 104 | + oninactive |
| 105 | + |
| 106 | + constructor (streamOrTracks) { |
| 107 | + super() |
| 108 | + if (streamOrTracks instanceof MediaStream) { |
| 109 | + for (const track of streamOrTracks.getTracks()) { |
| 110 | + this.addTrack(track) |
| 111 | + } |
| 112 | + } else if (Array.isArray(streamOrTracks)) { |
| 113 | + for (const track of streamOrTracks) { |
| 114 | + this.addTrack(track) |
| 115 | + } |
| 116 | + } |
| 117 | + this.addEventListener('active', e => { |
| 118 | + this.onactive?.(e) |
| 119 | + }) |
| 120 | + this.addEventListener('inactive', e => { |
| 121 | + this.oninactive?.(e) |
| 122 | + }) |
| 123 | + this.addEventListener('removetrack', e => { |
| 124 | + this.onremovetrack?.(e) |
| 125 | + }) |
| 126 | + this.addEventListener('addtrack', e => { |
| 127 | + this.onaddtrack?.(e) |
| 128 | + }) |
| 129 | + this.dispatchEvent(new Event('active')) |
| 130 | + } |
| 131 | + |
| 132 | + get active (): boolean { |
| 133 | + return this.#active |
| 134 | + } |
| 135 | + |
| 136 | + get id (): string { |
| 137 | + return this.#id |
| 138 | + } |
| 139 | + |
| 140 | + addTrack (track) { |
| 141 | + this.#tracks.add(track) |
| 142 | + this.dispatchEvent(new MediaStreamTrackEvent('addtrack', { track })) |
| 143 | + } |
| 144 | + |
| 145 | + getTracks (): MediaStreamTrack[] { |
| 146 | + return [...this.#tracks] |
| 147 | + } |
| 148 | + |
| 149 | + getVideoTracks (): MediaStreamTrack[] { |
| 150 | + return [...this.#tracks].filter(({ kind }) => kind === 'video') |
| 151 | + } |
| 152 | + |
| 153 | + getAudioTracks (): MediaStreamTrack[] { |
| 154 | + return [...this.#tracks].filter(({ kind }) => kind === 'audio') |
| 155 | + } |
| 156 | + |
| 157 | + getTrackById (id): MediaStreamTrack { |
| 158 | + return [...this.#tracks].find(track => track.id === id) ?? null |
| 159 | + } |
| 160 | + |
| 161 | + removeTrack (track): void { |
| 162 | + this.#tracks.delete(track) |
| 163 | + this.dispatchEvent(new MediaStreamTrackEvent('removetrack', { track })) |
| 164 | + } |
| 165 | + |
| 166 | + clone (): MediaStream { |
| 167 | + return new MediaStream([...this.getTracks()]) |
| 168 | + } |
| 169 | + |
| 170 | + stop ():void { |
| 171 | + for (const track of this.getTracks()) { |
| 172 | + track.stop() |
| 173 | + } |
| 174 | + this.#active = false |
| 175 | + this.dispatchEvent(new Event('inactive')) |
| 176 | + } |
| 177 | +} |
0 commit comments