fluxkit-player 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +120 -0
- package/LICENSE +21 -0
- package/README.en.md +224 -0
- package/README.md +549 -2
- package/THIRD-PARTY-NOTICES.md +30 -0
- package/dist/fluxkit.js +5536 -0
- package/dist/fluxkit.js.map +1 -0
- package/dist/fluxkit.umd.cjs +94 -0
- package/dist/fluxkit.umd.cjs.map +1 -0
- package/dist/types/codec/aac.d.ts +19 -0
- package/dist/types/codec/bit.d.ts +15 -0
- package/dist/types/codec/config.d.ts +19 -0
- package/dist/types/codec/g711.d.ts +4 -0
- package/dist/types/codec/h264.d.ts +37 -0
- package/dist/types/codec/h265.d.ts +27 -0
- package/dist/types/config.d.ts +66 -0
- package/dist/types/crypto/hls-aes.d.ts +13 -0
- package/dist/types/demux/flv.d.ts +48 -0
- package/dist/types/demux/hls.d.ts +79 -0
- package/dist/types/demux/mp4.d.ts +45 -0
- package/dist/types/demux/ts.d.ts +41 -0
- package/dist/types/index.d.ts +32 -0
- package/dist/types/io/http.d.ts +32 -0
- package/dist/types/io/websocket.d.ts +27 -0
- package/dist/types/pipeline/segmenter.d.ts +16 -0
- package/dist/types/pipeline/timeline.d.ts +15 -0
- package/dist/types/player.d.ts +169 -0
- package/dist/types/protocol/amf0.d.ts +12 -0
- package/dist/types/remux/fmp4.d.ts +32 -0
- package/dist/types/render/audio.d.ts +61 -0
- package/dist/types/render/backlog.d.ts +27 -0
- package/dist/types/render/mse.d.ts +56 -0
- package/dist/types/render/software.d.ts +110 -0
- package/dist/types/render/subtitles.d.ts +36 -0
- package/dist/types/render/webcodecs.d.ts +73 -0
- package/dist/types/soft/h264bsd.d.ts +23 -0
- package/dist/types/soft/index.d.ts +14 -0
- package/dist/types/soft/libde265.d.ts +16 -0
- package/dist/types/soft/openh264.d.ts +21 -0
- package/dist/types/soft/soft-worker.d.ts +2 -0
- package/dist/types/soft/types.d.ts +37 -0
- package/dist/types/soft/worker-protocol.d.ts +52 -0
- package/dist/types/source/abr.d.ts +22 -0
- package/dist/types/source/hls.d.ts +66 -0
- package/dist/types/source/stream.d.ts +23 -0
- package/dist/types/types.d.ts +354 -0
- package/dist/types/ui/controls.d.ts +68 -0
- package/dist/types/utils/async.d.ts +12 -0
- package/dist/types/utils/buffer.d.ts +17 -0
- package/dist/types/utils/bytes.d.ts +62 -0
- package/dist/types/utils/events.d.ts +10 -0
- package/dist/types/utils/log.d.ts +14 -0
- package/docs/PUBLISHING.md +142 -0
- package/docs/README.md +42 -0
- package/docs/TEST-REPORT.md +370 -0
- package/docs/USAGE.md +513 -0
- package/docs/architecture.md +213 -0
- package/docs/test-logs/build.txt +19 -0
- package/docs/test-logs/coverage.txt +64 -0
- package/docs/test-logs/e2e-firefox.txt +1667 -0
- package/docs/test-logs/e2e.txt +1667 -0
- package/docs/test-logs/environment.json +62 -0
- package/docs/test-logs/typecheck.txt +1 -0
- package/docs/test-logs/unit.txt +81 -0
- package/examples/README.md +53 -0
- package/examples/minimal.html +49 -0
- package/examples/react/FluxPlayer.tsx +48 -0
- package/examples/soft-worker.js +6 -0
- package/examples/vue/FluxPlayer.vue +47 -0
- package/package.json +96 -4
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FluxKit player façade.
|
|
3
|
+
*
|
|
4
|
+
* const player = new FluxKit('#stage', { decoder: 'auto' });
|
|
5
|
+
* await player.load('https://example.com/live/stream.flv');
|
|
6
|
+
*
|
|
7
|
+
* The player wires a Source (container + transport) into a Timeline, a
|
|
8
|
+
* Segmenter, and a Renderer (MSE or WebCodecs), and exposes playback controls
|
|
9
|
+
* plus typed events.
|
|
10
|
+
*/
|
|
11
|
+
import type { AudioTrackInfo, FluxKitOptions, LoadOptions, PlayerEvents, PlayerStats, Protocol, SubtitleTrackInfo, Track } from './types';
|
|
12
|
+
export declare const VERSION = "0.1.0";
|
|
13
|
+
export declare class FluxKit {
|
|
14
|
+
static readonly version = "0.1.0";
|
|
15
|
+
static isSupported(): boolean;
|
|
16
|
+
private readonly options;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
private readonly emitter;
|
|
19
|
+
private readonly stage;
|
|
20
|
+
private readonly video;
|
|
21
|
+
private readonly canvas;
|
|
22
|
+
private readonly tracks;
|
|
23
|
+
private readonly timeline;
|
|
24
|
+
private readonly segmenter;
|
|
25
|
+
private source;
|
|
26
|
+
private renderer;
|
|
27
|
+
private rendererPromise;
|
|
28
|
+
private lastRendererError;
|
|
29
|
+
private url;
|
|
30
|
+
private protocol;
|
|
31
|
+
private live;
|
|
32
|
+
private startMs;
|
|
33
|
+
private nativeMode;
|
|
34
|
+
private loadId;
|
|
35
|
+
private destroyed;
|
|
36
|
+
private preBuffer;
|
|
37
|
+
private readyDeferred;
|
|
38
|
+
private readonly switchAttempts;
|
|
39
|
+
private pendingSeek;
|
|
40
|
+
private statsTimer;
|
|
41
|
+
private lastStats;
|
|
42
|
+
private bytes;
|
|
43
|
+
private lastBytes;
|
|
44
|
+
private lastStatsAt;
|
|
45
|
+
private lastFrames;
|
|
46
|
+
private lastFpsAt;
|
|
47
|
+
private lastWidth;
|
|
48
|
+
private lastHeight;
|
|
49
|
+
private audioOnlyTimer;
|
|
50
|
+
private wcPaused;
|
|
51
|
+
private sourcePaused;
|
|
52
|
+
private controlsUI;
|
|
53
|
+
private stopped;
|
|
54
|
+
private lastUrl;
|
|
55
|
+
private lastDuration;
|
|
56
|
+
private loading;
|
|
57
|
+
private playbackRateValue;
|
|
58
|
+
private hasStarted;
|
|
59
|
+
private preferredAudioId?;
|
|
60
|
+
private preferredBandwidth;
|
|
61
|
+
private activeSubtitleId;
|
|
62
|
+
private subtitleRenderer;
|
|
63
|
+
private pipVideo;
|
|
64
|
+
private pipStream;
|
|
65
|
+
private reconnectAttempts;
|
|
66
|
+
private reconnectTimer;
|
|
67
|
+
private reconnectResetTimer;
|
|
68
|
+
private recorder;
|
|
69
|
+
private recordedChunks;
|
|
70
|
+
private recordingDone;
|
|
71
|
+
constructor(target: HTMLElement | string, options?: FluxKitOptions);
|
|
72
|
+
on<K extends keyof PlayerEvents>(type: K, listener: (event: PlayerEvents[K]) => void): () => void;
|
|
73
|
+
once<K extends keyof PlayerEvents>(type: K, listener: (event: PlayerEvents[K]) => void): () => void;
|
|
74
|
+
off<K extends keyof PlayerEvents>(type: K, listener: (event: PlayerEvents[K]) => void): void;
|
|
75
|
+
get videoElement(): HTMLVideoElement;
|
|
76
|
+
get canvasElement(): HTMLCanvasElement;
|
|
77
|
+
get currentProtocol(): Protocol;
|
|
78
|
+
/** Tracks discovered so far (video + audio). */
|
|
79
|
+
get mediaTracks(): Track[];
|
|
80
|
+
get isLive(): boolean;
|
|
81
|
+
get decoderMode(): string;
|
|
82
|
+
get stats(): PlayerStats;
|
|
83
|
+
/** Current playback position in milliseconds. */
|
|
84
|
+
get currentTime(): number;
|
|
85
|
+
/** Duration in milliseconds; Infinity for live streams. */
|
|
86
|
+
get duration(): number;
|
|
87
|
+
get paused(): boolean;
|
|
88
|
+
get volume(): number;
|
|
89
|
+
get muted(): boolean;
|
|
90
|
+
get isRecording(): boolean;
|
|
91
|
+
get isStopped(): boolean;
|
|
92
|
+
get isLoading(): boolean;
|
|
93
|
+
get playbackRate(): number;
|
|
94
|
+
setVolume(volume: number): void;
|
|
95
|
+
setMuted(muted: boolean): void;
|
|
96
|
+
/** Applies to the next load(). */
|
|
97
|
+
setDecoderMode(mode: FluxKitOptions['decoder']): void;
|
|
98
|
+
/** Applies to the next load(). */
|
|
99
|
+
setNativeFallback(enabled: boolean): void;
|
|
100
|
+
/** Enable/disable the WASM software decoding fallback. */
|
|
101
|
+
setSoftDecode(enabled: boolean): void;
|
|
102
|
+
load(url: string, options?: LoadOptions): Promise<void>;
|
|
103
|
+
play(): Promise<void>;
|
|
104
|
+
pause(): void;
|
|
105
|
+
/** Capture the current frame as an image Blob. */
|
|
106
|
+
snapshot(type?: string, quality?: number): Promise<Blob>;
|
|
107
|
+
/** Start recording the current stream to WebM (video + audio when available). */
|
|
108
|
+
startRecording(options?: {
|
|
109
|
+
mimeType?: string;
|
|
110
|
+
videoBitsPerSecond?: number;
|
|
111
|
+
}): void;
|
|
112
|
+
stopRecording(): Promise<Blob | null>;
|
|
113
|
+
private buildRecordingStream;
|
|
114
|
+
/** Jump back to the live edge (MSE buffer or a fresh live load). */
|
|
115
|
+
seekToLive(): Promise<void>;
|
|
116
|
+
/** Playback rate for native/MSE and canvas (WebCodecs / software) renderers. */
|
|
117
|
+
setPlaybackRate(rate: number): void;
|
|
118
|
+
/** Available HLS alternate audio tracks. */
|
|
119
|
+
get audioTracks(): AudioTrackInfo[];
|
|
120
|
+
/** Available HLS subtitle renditions. */
|
|
121
|
+
get subtitleTracks(): SubtitleTrackInfo[];
|
|
122
|
+
/** Switch to another HLS alternate audio track (restarts at the current time). */
|
|
123
|
+
selectAudioTrack(id: string): Promise<void>;
|
|
124
|
+
/** Enable a WebVTT subtitle rendition, or disable subtitles with null. */
|
|
125
|
+
selectSubtitleTrack(id: string | null): void;
|
|
126
|
+
/**
|
|
127
|
+
* Select an HLS variant by bandwidth (bits/second), or 'auto' to resume ABR.
|
|
128
|
+
* The switch happens on a segment boundary for HLS; other protocols are a no-op.
|
|
129
|
+
*/
|
|
130
|
+
setQuality(bandwidth: number | 'auto'): void;
|
|
131
|
+
togglePictureInPicture(): Promise<void>;
|
|
132
|
+
private cleanupPip;
|
|
133
|
+
private setLoading;
|
|
134
|
+
toggleFullscreen(): Promise<void>;
|
|
135
|
+
seek(ms: number): Promise<void>;
|
|
136
|
+
/** Tear down the current stream but keep the player reusable. */
|
|
137
|
+
stop(): void;
|
|
138
|
+
destroy(): void;
|
|
139
|
+
private shouldUseNative;
|
|
140
|
+
private createSource;
|
|
141
|
+
private onTracks;
|
|
142
|
+
private emitMetadata;
|
|
143
|
+
private startRenderer;
|
|
144
|
+
private createRenderer;
|
|
145
|
+
private onSample;
|
|
146
|
+
private onSourceEnd;
|
|
147
|
+
private onSourceError;
|
|
148
|
+
private shouldReconnect;
|
|
149
|
+
private scheduleReconnect;
|
|
150
|
+
private onReconnected;
|
|
151
|
+
private clearReconnectTimer;
|
|
152
|
+
private cancelReconnect;
|
|
153
|
+
private tryNativeFallback;
|
|
154
|
+
private switchProtocol;
|
|
155
|
+
private restartAt;
|
|
156
|
+
private buildSourceContext;
|
|
157
|
+
private startNative;
|
|
158
|
+
private safeAutoplay;
|
|
159
|
+
private startStats;
|
|
160
|
+
private get bufferAhead();
|
|
161
|
+
private updateStats;
|
|
162
|
+
private findCodec;
|
|
163
|
+
private bindVideoEvents;
|
|
164
|
+
private showRenderer;
|
|
165
|
+
private clearAudioOnlyTimer;
|
|
166
|
+
private stopPipeline;
|
|
167
|
+
private stopInternal;
|
|
168
|
+
}
|
|
169
|
+
export default FluxKit;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type AmfValue = number | string | boolean | null | undefined | AmfObject | AmfValue[];
|
|
2
|
+
export interface AmfObject {
|
|
3
|
+
[key: string]: AmfValue;
|
|
4
|
+
}
|
|
5
|
+
export interface AmfDecodeResult {
|
|
6
|
+
value: AmfValue | undefined;
|
|
7
|
+
offset: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function decodeAmf0(data: Uint8Array, offset?: number): AmfDecodeResult;
|
|
10
|
+
export declare function decodeAmf0All(data: Uint8Array): AmfValue[];
|
|
11
|
+
export declare function encodeAmf0(value: AmfValue): Uint8Array;
|
|
12
|
+
export declare function encodeAmf0Values(...values: AmfValue[]): Uint8Array;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fragmented MP4 muxer.
|
|
3
|
+
*
|
|
4
|
+
* Turns demuxer output (AVCC samples + avcC/hvcC/AudioSpecificConfig) into
|
|
5
|
+
* standards-compliant fMP4 that MSE can append:
|
|
6
|
+
*
|
|
7
|
+
* init segment: ftyp + moov (one track, mvex/trex)
|
|
8
|
+
* media segment: moof (mfhd/traf/tfhd/tfdt/trun) + mdat
|
|
9
|
+
*
|
|
10
|
+
* One track per init segment keeps SourceBuffer ownership simple.
|
|
11
|
+
*/
|
|
12
|
+
import type { AudioTrack, Sample, Track, VideoTrack } from '../types';
|
|
13
|
+
export declare function buildInitSegment(track: Track): Uint8Array;
|
|
14
|
+
export declare function videoMimeType(track: VideoTrack): string;
|
|
15
|
+
export declare function audioMimeType(track: AudioTrack): string;
|
|
16
|
+
export declare function mimeTypeFor(track: Track): string;
|
|
17
|
+
export declare function codecLabel(track: Track): string;
|
|
18
|
+
/**
|
|
19
|
+
* Builds media segments for a single track.
|
|
20
|
+
*/
|
|
21
|
+
export declare class Fmp4Muxer {
|
|
22
|
+
readonly track: Track;
|
|
23
|
+
private sequence;
|
|
24
|
+
private readonly fallbackDuration;
|
|
25
|
+
constructor(track: Track);
|
|
26
|
+
initSegment(): Uint8Array;
|
|
27
|
+
reset(): void;
|
|
28
|
+
/** Build one moof+mdat segment from samples sorted by dts. */
|
|
29
|
+
fragment(samples: Sample[]): Uint8Array;
|
|
30
|
+
/** Ensure dts strictly increases; a non-monotonic DTS makes MSE stall. */
|
|
31
|
+
private normalize;
|
|
32
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared audio output.
|
|
3
|
+
*
|
|
4
|
+
* AAC goes through WebCodecs `AudioDecoder`; G.711 (A-law/μ-law) is decoded in
|
|
5
|
+
* JS because MSE cannot mux it and browser support for the WebCodecs G.711
|
|
6
|
+
* codecs is inconsistent. Both paths are scheduled through Web Audio.
|
|
7
|
+
*
|
|
8
|
+
* Two clock modes:
|
|
9
|
+
* - internal (default): audio is the master clock (used by WebCodecs/soft video)
|
|
10
|
+
* - external: schedule against another clock (the MSE <video>), so a G.711
|
|
11
|
+
* audio side channel stays in sync with an MSE video track
|
|
12
|
+
*/
|
|
13
|
+
import type { AudioTrack, Sample } from '../types';
|
|
14
|
+
export interface AudioOutputOptions {
|
|
15
|
+
volume: number;
|
|
16
|
+
onError?: (err: Error) => void;
|
|
17
|
+
}
|
|
18
|
+
export declare class AudioOutput {
|
|
19
|
+
private readonly options;
|
|
20
|
+
private decoder;
|
|
21
|
+
private track;
|
|
22
|
+
private manualLaw;
|
|
23
|
+
private ctx;
|
|
24
|
+
private gain;
|
|
25
|
+
private destination;
|
|
26
|
+
private anchorMedia;
|
|
27
|
+
private anchorCtx;
|
|
28
|
+
private anchored;
|
|
29
|
+
private volume;
|
|
30
|
+
private destroyed;
|
|
31
|
+
private clockSource;
|
|
32
|
+
private rate;
|
|
33
|
+
decodedFrames: number;
|
|
34
|
+
private peakValue;
|
|
35
|
+
private peakAt;
|
|
36
|
+
constructor(options: AudioOutputOptions);
|
|
37
|
+
get configured(): boolean;
|
|
38
|
+
/** Peak absolute sample over the last second, 0..1. */
|
|
39
|
+
get peak(): number;
|
|
40
|
+
get state(): string;
|
|
41
|
+
get volumeLevel(): number;
|
|
42
|
+
/** Audio-only MediaStream for MediaRecorder (post-gain). */
|
|
43
|
+
captureStream(): MediaStream | null;
|
|
44
|
+
configure(track: AudioTrack): void;
|
|
45
|
+
write(samples: Sample[]): void;
|
|
46
|
+
private writeManual;
|
|
47
|
+
private updatePeak;
|
|
48
|
+
/** Media time in seconds from the active clock, or null before playback. */
|
|
49
|
+
get clock(): number | null;
|
|
50
|
+
setClockSource(source: (() => number | null) | null): void;
|
|
51
|
+
/** Changes the presentation rate; already-scheduled buffers finish at 1x. */
|
|
52
|
+
setRate(rate: number): void;
|
|
53
|
+
private ensureAudioContext;
|
|
54
|
+
private onData;
|
|
55
|
+
private schedule;
|
|
56
|
+
resume(): Promise<void> | void;
|
|
57
|
+
setPaused(paused: boolean): void;
|
|
58
|
+
setVolume(volume: number): void;
|
|
59
|
+
reset(): void;
|
|
60
|
+
destroy(): void;
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live backlog resynchronisation for the software renderer.
|
|
3
|
+
*
|
|
4
|
+
* A software decoder that cannot keep up falls further and further behind the
|
|
5
|
+
* live edge. Decoding every stale frame only makes it worse, so the player
|
|
6
|
+
* periodically resumes at the next keyframe instead. Frame-level skipping is
|
|
7
|
+
* not possible in H.264/H.265: inter-frame prediction requires the whole GOP.
|
|
8
|
+
*/
|
|
9
|
+
/** One decodable access unit with a presentation timestamp. */
|
|
10
|
+
export interface TimedAccessUnit {
|
|
11
|
+
ptsUs: number;
|
|
12
|
+
keyframe: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface LiveResyncPlan {
|
|
15
|
+
/** Number of leading access units to discard. */
|
|
16
|
+
cut: number;
|
|
17
|
+
/** No usable keyframe is buffered; drop non-keyframes until the next one. */
|
|
18
|
+
awaitingKeyframe: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Choose where to resume after falling behind.
|
|
22
|
+
*
|
|
23
|
+
* Keyframes up to `tolerance` seconds before `now` are kept so they can be
|
|
24
|
+
* presented immediately; older GOPs are dropped. When no keyframe is available
|
|
25
|
+
* the caller should discard everything and wait for the next keyframe.
|
|
26
|
+
*/
|
|
27
|
+
export declare function planLiveResync<T extends TimedAccessUnit>(items: readonly T[], nowSeconds: number, tolerance?: number): LiveResyncPlan;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MSE renderer: builds fMP4 fragments and appends them to SourceBuffers.
|
|
3
|
+
* The browser's media pipeline performs hardware decoding.
|
|
4
|
+
*/
|
|
5
|
+
import type { Renderer, RendererStats, Sample, Track } from '../types';
|
|
6
|
+
export interface MseOptions {
|
|
7
|
+
live: boolean;
|
|
8
|
+
liveBufferTarget: number;
|
|
9
|
+
onError?: (err: Error) => void;
|
|
10
|
+
}
|
|
11
|
+
export declare class MseRenderer implements Renderer {
|
|
12
|
+
private readonly video;
|
|
13
|
+
private readonly options;
|
|
14
|
+
readonly mode: "mse";
|
|
15
|
+
private mediaSource;
|
|
16
|
+
private objectUrl;
|
|
17
|
+
private sourceOpen;
|
|
18
|
+
private readonly buffers;
|
|
19
|
+
private readonly queues;
|
|
20
|
+
private readonly muxers;
|
|
21
|
+
private readonly mimeTypes;
|
|
22
|
+
private readonly attached;
|
|
23
|
+
private readonly trackList;
|
|
24
|
+
private readonly fallbackTracks;
|
|
25
|
+
private fallbackAudio;
|
|
26
|
+
private hasMseAudio;
|
|
27
|
+
private endPending;
|
|
28
|
+
private destroyed;
|
|
29
|
+
constructor(video: HTMLVideoElement, options: MseOptions);
|
|
30
|
+
get stats(): RendererStats;
|
|
31
|
+
attach(tracks: Track[]): Promise<void>;
|
|
32
|
+
/** Swap the muxer/init segment for an existing SourceBuffer (HLS ABR). */
|
|
33
|
+
private reconfigure;
|
|
34
|
+
private ensureFallbackAudio;
|
|
35
|
+
private initMediaSource;
|
|
36
|
+
private addTrack;
|
|
37
|
+
write(track: Track, samples: Sample[]): void;
|
|
38
|
+
private enqueue;
|
|
39
|
+
private pump;
|
|
40
|
+
private nothingBuffering;
|
|
41
|
+
/** Drop media far behind the playhead so long VODs don't hit the quota. */
|
|
42
|
+
trimOld(keepBehindSeconds?: number): void;
|
|
43
|
+
private trimBuffer;
|
|
44
|
+
endOfStream(): void;
|
|
45
|
+
flush(): void;
|
|
46
|
+
reset(): void;
|
|
47
|
+
setVolume(volume: number): void;
|
|
48
|
+
resumeAudio(): Promise<void> | void;
|
|
49
|
+
setPaused(paused: boolean): void;
|
|
50
|
+
setPlaybackRate(rate: number): void;
|
|
51
|
+
destroy(): void;
|
|
52
|
+
/** Seconds buffered ahead of the playhead. */
|
|
53
|
+
captureStream(): MediaStream | null;
|
|
54
|
+
captureAudioStream(): MediaStream | null;
|
|
55
|
+
get bufferAhead(): number;
|
|
56
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM software renderer.
|
|
3
|
+
*
|
|
4
|
+
* Decodes H.265 (libde265) or H.264 (h264bsd/OpenH264) on the CPU, converts
|
|
5
|
+
* YUV420 to RGB on the GPU (WebGL) and presents frames against the audio clock.
|
|
6
|
+
* This is the last-resort path for browsers without a platform HEVC decoder.
|
|
7
|
+
*/
|
|
8
|
+
import type { Renderer, RendererStats, Sample, Track } from '../types';
|
|
9
|
+
import { FluxKitError } from '../types';
|
|
10
|
+
import type { SoftLoaders } from '../soft';
|
|
11
|
+
export interface SoftwareOptions {
|
|
12
|
+
fit: 'contain' | 'cover' | 'fill';
|
|
13
|
+
live: boolean;
|
|
14
|
+
volume: number;
|
|
15
|
+
maxDecodeAhead: number;
|
|
16
|
+
dropLateFrames: boolean;
|
|
17
|
+
loaders?: SoftLoaders;
|
|
18
|
+
/** Worker URL or factory for off-main-thread decode. */
|
|
19
|
+
worker?: string | (() => Worker);
|
|
20
|
+
modules?: {
|
|
21
|
+
avc?: string;
|
|
22
|
+
openh264?: string;
|
|
23
|
+
hevc?: string;
|
|
24
|
+
};
|
|
25
|
+
onError?: (err: Error) => void;
|
|
26
|
+
onFirstFrame?: (width: number, height: number) => void;
|
|
27
|
+
}
|
|
28
|
+
export declare class SoftwareRenderer implements Renderer {
|
|
29
|
+
private readonly options;
|
|
30
|
+
readonly mode: "software";
|
|
31
|
+
private decoder;
|
|
32
|
+
private videoTrack;
|
|
33
|
+
private readonly audio;
|
|
34
|
+
private readonly canvas;
|
|
35
|
+
private backlog;
|
|
36
|
+
private present;
|
|
37
|
+
private decoding;
|
|
38
|
+
private rafId;
|
|
39
|
+
private timer;
|
|
40
|
+
private paused;
|
|
41
|
+
private anchored;
|
|
42
|
+
private anchorMedia;
|
|
43
|
+
private rate;
|
|
44
|
+
private anchorWall;
|
|
45
|
+
private decoded;
|
|
46
|
+
private dropped;
|
|
47
|
+
private firstFrameReported;
|
|
48
|
+
private destroyed;
|
|
49
|
+
private ended;
|
|
50
|
+
private gl;
|
|
51
|
+
private glFailed;
|
|
52
|
+
private ctx2d;
|
|
53
|
+
private tmpCanvas;
|
|
54
|
+
private tmpCtx;
|
|
55
|
+
private imageData;
|
|
56
|
+
private resizeObserver;
|
|
57
|
+
private worker;
|
|
58
|
+
private workerReady;
|
|
59
|
+
private workerName;
|
|
60
|
+
private workerInFlight;
|
|
61
|
+
private workerMaxInFlight;
|
|
62
|
+
private workerId;
|
|
63
|
+
private workerFlushPending;
|
|
64
|
+
private workerFlushId;
|
|
65
|
+
private lastDecodedPtsUs;
|
|
66
|
+
private dropBeforeUs;
|
|
67
|
+
private awaitingKeyframe;
|
|
68
|
+
constructor(options: SoftwareOptions, canvas: HTMLCanvasElement);
|
|
69
|
+
get stats(): RendererStats;
|
|
70
|
+
get currentTime(): number;
|
|
71
|
+
get framesPending(): number;
|
|
72
|
+
get usingWorker(): boolean;
|
|
73
|
+
get backlogSeconds(): number;
|
|
74
|
+
get decoderName(): string;
|
|
75
|
+
captureStream(): MediaStream | null;
|
|
76
|
+
captureAudioStream(): MediaStream | null;
|
|
77
|
+
attach(tracks: Track[]): Promise<void>;
|
|
78
|
+
private initWorker;
|
|
79
|
+
private onWorkerMessage;
|
|
80
|
+
private pumpWorker;
|
|
81
|
+
write(track: Track, samples: Sample[]): void;
|
|
82
|
+
private scheduleDecode;
|
|
83
|
+
private decodeStep;
|
|
84
|
+
/**
|
|
85
|
+
* Resync a live stream that has fallen behind. Only whole GOPs are dropped:
|
|
86
|
+
* the software decoder cannot resume in the middle of an inter-frame chain.
|
|
87
|
+
*/
|
|
88
|
+
private maybeResync;
|
|
89
|
+
private mediaTime;
|
|
90
|
+
private startRaf;
|
|
91
|
+
private tick;
|
|
92
|
+
private resize;
|
|
93
|
+
private observeResize;
|
|
94
|
+
private renderBitmap;
|
|
95
|
+
private render;
|
|
96
|
+
private initGl;
|
|
97
|
+
private uploadPlane;
|
|
98
|
+
private renderGl;
|
|
99
|
+
private render2d;
|
|
100
|
+
setPaused(paused: boolean): void;
|
|
101
|
+
resumeAudio(): Promise<void> | void;
|
|
102
|
+
setVolume(volume: number): void;
|
|
103
|
+
setPlaybackRate(rate: number): void;
|
|
104
|
+
flush(): void;
|
|
105
|
+
endOfStream(): void;
|
|
106
|
+
reset(): void;
|
|
107
|
+
destroy(): void;
|
|
108
|
+
}
|
|
109
|
+
/** For callers that want to distinguish "no decoder" from a runtime error. */
|
|
110
|
+
export declare function softwareDecoderError(err: unknown): FluxKitError;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface SubtitleCue {
|
|
2
|
+
startMs: number;
|
|
3
|
+
endMs: number;
|
|
4
|
+
text: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Parse a WebVTT file. HLS subtitle segments may carry an X-TIMESTAMP-MAP
|
|
8
|
+
* header mapping local cue time to the 90 kHz media timeline.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseWebVtt(text: string, baseOffsetMs?: number): SubtitleCue[];
|
|
11
|
+
export interface SubtitleRendererOptions {
|
|
12
|
+
getTime: () => number;
|
|
13
|
+
live?: boolean;
|
|
14
|
+
timeout?: number;
|
|
15
|
+
onError?: (err: Error) => void;
|
|
16
|
+
}
|
|
17
|
+
/** One active subtitle rendition. Rendering is a simple DOM overlay. */
|
|
18
|
+
export declare class SubtitleRenderer {
|
|
19
|
+
private readonly options;
|
|
20
|
+
private readonly host;
|
|
21
|
+
private readonly label;
|
|
22
|
+
private cues;
|
|
23
|
+
private seen;
|
|
24
|
+
private timer;
|
|
25
|
+
private generation;
|
|
26
|
+
private destroyed;
|
|
27
|
+
private current;
|
|
28
|
+
private lastPrune;
|
|
29
|
+
constructor(container: HTMLElement, options: SubtitleRendererOptions);
|
|
30
|
+
get activeText(): string;
|
|
31
|
+
load(playlistUrl: string, headers?: Record<string, string>): Promise<void>;
|
|
32
|
+
private fetchWithRetry;
|
|
33
|
+
private loadSegments;
|
|
34
|
+
private tick;
|
|
35
|
+
destroy(): void;
|
|
36
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebCodecs renderer.
|
|
3
|
+
*
|
|
4
|
+
* Decodes H.264 / H.265 with `VideoDecoder` (hardware accelerated when the
|
|
5
|
+
* browser agrees) and AAC with `AudioDecoder`, presenting video to a canvas
|
|
6
|
+
* and audio through Web Audio. This path exists because MSE does not expose
|
|
7
|
+
* HEVC everywhere (notably Chrome on Linux/Windows without the codec pack),
|
|
8
|
+
* while WebCodecs often can still use the platform decoder.
|
|
9
|
+
*/
|
|
10
|
+
import type { Renderer, RendererStats, Sample, Track } from '../types';
|
|
11
|
+
export interface WebCodecsOptions {
|
|
12
|
+
hardwareAcceleration: 'no-preference' | 'prefer-hardware' | 'prefer-software';
|
|
13
|
+
fit: 'contain' | 'cover' | 'fill';
|
|
14
|
+
volume: number;
|
|
15
|
+
onError?: (err: Error) => void;
|
|
16
|
+
onFirstFrame?: (width: number, height: number) => void;
|
|
17
|
+
}
|
|
18
|
+
export declare function isWebCodecsSupported(): boolean;
|
|
19
|
+
export declare class WebCodecsRenderer implements Renderer {
|
|
20
|
+
private readonly options;
|
|
21
|
+
readonly mode: "webcodecs";
|
|
22
|
+
private videoDecoder;
|
|
23
|
+
private readonly audio;
|
|
24
|
+
private canvas;
|
|
25
|
+
private ctx;
|
|
26
|
+
private frames;
|
|
27
|
+
private videoBacklog;
|
|
28
|
+
private rafId;
|
|
29
|
+
private paused;
|
|
30
|
+
private anchored;
|
|
31
|
+
private anchorWall;
|
|
32
|
+
private anchorMedia;
|
|
33
|
+
private rate;
|
|
34
|
+
private decodedVideo;
|
|
35
|
+
private droppedVideo;
|
|
36
|
+
private hardware;
|
|
37
|
+
private firstFrameReported;
|
|
38
|
+
private destroyed;
|
|
39
|
+
constructor(options: WebCodecsOptions, canvas?: HTMLCanvasElement);
|
|
40
|
+
get stats(): RendererStats;
|
|
41
|
+
/** Presentation time in seconds (0 until the first frame is anchored). */
|
|
42
|
+
get currentTime(): number;
|
|
43
|
+
get framesPending(): number;
|
|
44
|
+
get acceleration(): 'preferred' | 'software' | 'unknown';
|
|
45
|
+
get element(): HTMLCanvasElement;
|
|
46
|
+
captureStream(): MediaStream | null;
|
|
47
|
+
captureAudioStream(): MediaStream | null;
|
|
48
|
+
attach(tracks: Track[]): Promise<void>;
|
|
49
|
+
private configureVideo;
|
|
50
|
+
write(track: Track, samples: Sample[]): void;
|
|
51
|
+
private writeVideo;
|
|
52
|
+
/**
|
|
53
|
+
* Feed the decoder only a few frames ahead of the presentation clock.
|
|
54
|
+
* Without this a fast VOD downloads minutes of frames into the queue and
|
|
55
|
+
* either explodes memory or drops half a GOP (which breaks references).
|
|
56
|
+
*/
|
|
57
|
+
private pumpVideo;
|
|
58
|
+
/** Seconds of compressed video queued ahead of the playhead. */
|
|
59
|
+
get backlogSeconds(): number;
|
|
60
|
+
private onFrame;
|
|
61
|
+
private mediaTime;
|
|
62
|
+
private startRaf;
|
|
63
|
+
private tick;
|
|
64
|
+
private render;
|
|
65
|
+
setPaused(paused: boolean): void;
|
|
66
|
+
resumeAudio(): Promise<void> | void;
|
|
67
|
+
setVolume(volume: number): void;
|
|
68
|
+
setPlaybackRate(rate: number): void;
|
|
69
|
+
flush(): void;
|
|
70
|
+
endOfStream(): void;
|
|
71
|
+
reset(): void;
|
|
72
|
+
destroy(): void;
|
|
73
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* H.264 software decoding through h264bsd compiled to WebAssembly.
|
|
3
|
+
*
|
|
4
|
+
* h264bsd is a Baseline-profile decoder only; High/Main streams are rejected
|
|
5
|
+
* with a clear error so the player can fall back to native playback instead of
|
|
6
|
+
* showing corrupted frames.
|
|
7
|
+
*/
|
|
8
|
+
import type { VideoTrack } from '../types';
|
|
9
|
+
import type { SoftFrame, SoftVideoDecoder } from './types';
|
|
10
|
+
export declare class H264BsdDecoder implements SoftVideoDecoder {
|
|
11
|
+
readonly name = "h264bsd";
|
|
12
|
+
private module;
|
|
13
|
+
private decoder;
|
|
14
|
+
private parameterSets;
|
|
15
|
+
private picId;
|
|
16
|
+
private readonly ptsById;
|
|
17
|
+
configure(track: VideoTrack): Promise<void>;
|
|
18
|
+
init(loader: () => Promise<unknown>): Promise<void>;
|
|
19
|
+
decode(annexB: Uint8Array, ptsUs: number, keyframe: boolean): SoftFrame[];
|
|
20
|
+
flush(): SoftFrame[];
|
|
21
|
+
private toFrame;
|
|
22
|
+
close(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** WASM software decoder factory (H.265 via libde265, H.264 via h264bsd). */
|
|
2
|
+
import type { VideoTrack } from '../types';
|
|
3
|
+
import type { SoftModuleLoader, SoftVideoDecoder } from './types';
|
|
4
|
+
export interface SoftLoaders {
|
|
5
|
+
/** Baseline H.264 (h264bsd). */
|
|
6
|
+
avc?: SoftModuleLoader;
|
|
7
|
+
/** Main/High H.264 (OpenH264). */
|
|
8
|
+
openh264?: SoftModuleLoader;
|
|
9
|
+
hevc?: SoftModuleLoader;
|
|
10
|
+
}
|
|
11
|
+
export declare function createSoftwareDecoder(track: VideoTrack, loaders?: SoftLoaders): Promise<SoftVideoDecoder>;
|
|
12
|
+
export type { SoftFrame, SoftPlane, SoftVideoDecoder } from './types';
|
|
13
|
+
export { installSoftWorker } from './soft-worker';
|
|
14
|
+
export type { SoftWorkerRequest, SoftWorkerResponse, SoftWorkerScope } from './worker-protocol';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** H.265 software decoding through libde265 compiled to WebAssembly. */
|
|
2
|
+
import type { VideoTrack } from '../types';
|
|
3
|
+
import type { SoftFrame, SoftVideoDecoder } from './types';
|
|
4
|
+
export declare class Libde265Decoder implements SoftVideoDecoder {
|
|
5
|
+
readonly name = "libde265";
|
|
6
|
+
private module;
|
|
7
|
+
private decoder;
|
|
8
|
+
private parameterSets;
|
|
9
|
+
configure(track: VideoTrack): Promise<void>;
|
|
10
|
+
/** Must be called once the module factory is available. */
|
|
11
|
+
init(loader: () => Promise<unknown>): Promise<void>;
|
|
12
|
+
decode(annexB: Uint8Array, ptsUs: number, keyframe: boolean): SoftFrame[];
|
|
13
|
+
flush(): SoftFrame[];
|
|
14
|
+
private drain;
|
|
15
|
+
close(): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* H.264 Main/High software decoding through OpenH264 (WASM).
|
|
3
|
+
*
|
|
4
|
+
* h264bsd only handles Baseline; this backend covers Main/High. The KMP glue
|
|
5
|
+
* exposes a synchronous decoder whose output is a packed I420 blob with a
|
|
6
|
+
* 64-byte header.
|
|
7
|
+
*/
|
|
8
|
+
import type { VideoTrack } from '../types';
|
|
9
|
+
import type { SoftFrame, SoftVideoDecoder } from './types';
|
|
10
|
+
export declare class OpenH264Decoder implements SoftVideoDecoder {
|
|
11
|
+
readonly name = "OpenH264";
|
|
12
|
+
private shim;
|
|
13
|
+
private handle;
|
|
14
|
+
private parameterSets;
|
|
15
|
+
configure(track: VideoTrack): Promise<void>;
|
|
16
|
+
init(loader: () => Promise<unknown>): Promise<void>;
|
|
17
|
+
decode(annexB: Uint8Array, ptsUs: number, keyframe: boolean): SoftFrame[];
|
|
18
|
+
flush(): SoftFrame[];
|
|
19
|
+
private takeFrame;
|
|
20
|
+
close(): void;
|
|
21
|
+
}
|