@scarlett-player/hls 0.1.2 → 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Hackney Enterprises Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,263 @@
1
+ // src/quality.ts
2
+ function formatLevel(level) {
3
+ if (level.name) {
4
+ return level.name;
5
+ }
6
+ if (level.height) {
7
+ const standardLabels = {
8
+ 2160: "4K",
9
+ 1440: "1440p",
10
+ 1080: "1080p",
11
+ 720: "720p",
12
+ 480: "480p",
13
+ 360: "360p",
14
+ 240: "240p",
15
+ 144: "144p"
16
+ };
17
+ const closest = Object.keys(standardLabels).map(Number).sort((a, b) => Math.abs(a - level.height) - Math.abs(b - level.height))[0];
18
+ if (Math.abs(closest - level.height) <= 20) {
19
+ return standardLabels[closest];
20
+ }
21
+ return `${level.height}p`;
22
+ }
23
+ if (level.bitrate) {
24
+ return formatBitrate(level.bitrate);
25
+ }
26
+ return "Unknown";
27
+ }
28
+ function formatBitrate(bitrate) {
29
+ if (bitrate >= 1e6) {
30
+ return `${(bitrate / 1e6).toFixed(1)} Mbps`;
31
+ }
32
+ if (bitrate >= 1e3) {
33
+ return `${Math.round(bitrate / 1e3)} Kbps`;
34
+ }
35
+ return `${bitrate} bps`;
36
+ }
37
+ function mapLevels(levels, currentLevel) {
38
+ return levels.map((level, index) => ({
39
+ index,
40
+ width: level.width || 0,
41
+ height: level.height || 0,
42
+ bitrate: level.bitrate || 0,
43
+ label: formatLevel(level),
44
+ codec: level.codecSet
45
+ }));
46
+ }
47
+
48
+ // src/event-map.ts
49
+ var HLS_ERROR_TYPES = {
50
+ NETWORK_ERROR: "networkError",
51
+ MEDIA_ERROR: "mediaError",
52
+ KEY_SYSTEM_ERROR: "keySystemError",
53
+ MUX_ERROR: "muxError",
54
+ OTHER_ERROR: "otherError"
55
+ };
56
+ function mapErrorType(hlsType) {
57
+ switch (hlsType) {
58
+ case HLS_ERROR_TYPES.NETWORK_ERROR:
59
+ return "network";
60
+ case HLS_ERROR_TYPES.MEDIA_ERROR:
61
+ return "media";
62
+ case HLS_ERROR_TYPES.MUX_ERROR:
63
+ return "mux";
64
+ default:
65
+ return "other";
66
+ }
67
+ }
68
+ function parseHlsError(data) {
69
+ return {
70
+ type: mapErrorType(data.type),
71
+ details: data.details || "Unknown error",
72
+ fatal: data.fatal || false,
73
+ url: data.url,
74
+ reason: data.reason,
75
+ response: data.response
76
+ };
77
+ }
78
+ function setupHlsEventHandlers(hls, api, callbacks) {
79
+ const handlers = [];
80
+ const addHandler = (event, handler) => {
81
+ hls.on(event, handler);
82
+ handlers.push({ event, handler });
83
+ };
84
+ addHandler("hlsManifestParsed", (_event, data) => {
85
+ api.logger.debug("HLS manifest parsed", { levels: data.levels.length });
86
+ const levels = data.levels.map((level, index) => ({
87
+ id: `level-${index}`,
88
+ label: formatLevel(level),
89
+ width: level.width,
90
+ height: level.height,
91
+ bitrate: level.bitrate,
92
+ active: index === hls.currentLevel
93
+ }));
94
+ api.setState("qualities", levels);
95
+ api.emit("quality:levels", {
96
+ levels: levels.map((l) => ({ id: l.id, label: l.label }))
97
+ });
98
+ callbacks.onManifestParsed?.(data.levels);
99
+ });
100
+ addHandler("hlsLevelSwitched", (_event, data) => {
101
+ const level = hls.levels[data.level];
102
+ const isAuto = callbacks.getIsAutoQuality?.() ?? hls.autoLevelEnabled;
103
+ api.logger.debug("HLS level switched", { level: data.level, height: level?.height, auto: isAuto });
104
+ if (level) {
105
+ const label = isAuto ? `Auto (${formatLevel(level)})` : formatLevel(level);
106
+ api.setState("currentQuality", {
107
+ id: isAuto ? "auto" : `level-${data.level}`,
108
+ label,
109
+ width: level.width,
110
+ height: level.height,
111
+ bitrate: level.bitrate,
112
+ active: true
113
+ });
114
+ }
115
+ api.emit("quality:change", {
116
+ quality: level ? formatLevel(level) : "auto",
117
+ auto: isAuto
118
+ });
119
+ callbacks.onLevelSwitched?.(data.level);
120
+ });
121
+ addHandler("hlsFragBuffered", () => {
122
+ api.setState("buffering", false);
123
+ callbacks.onBufferUpdate?.();
124
+ });
125
+ addHandler("hlsFragLoading", () => {
126
+ api.setState("buffering", true);
127
+ });
128
+ addHandler("hlsLevelLoaded", (_event, data) => {
129
+ if (data.details?.live !== void 0) {
130
+ api.setState("live", data.details.live);
131
+ callbacks.onLiveUpdate?.();
132
+ }
133
+ });
134
+ addHandler("hlsError", (_event, data) => {
135
+ const error = parseHlsError(data);
136
+ api.logger.warn("HLS error", { error });
137
+ callbacks.onError?.(error);
138
+ });
139
+ return () => {
140
+ for (const { event, handler } of handlers) {
141
+ hls.off(event, handler);
142
+ }
143
+ handlers.length = 0;
144
+ };
145
+ }
146
+ function setupVideoEventHandlers(video, api) {
147
+ const handlers = [];
148
+ const addHandler = (event, handler) => {
149
+ video.addEventListener(event, handler);
150
+ handlers.push({ event, handler });
151
+ };
152
+ addHandler("playing", () => {
153
+ api.setState("playing", true);
154
+ api.setState("paused", false);
155
+ api.setState("playbackState", "playing");
156
+ });
157
+ addHandler("pause", () => {
158
+ api.setState("playing", false);
159
+ api.setState("paused", true);
160
+ api.setState("playbackState", "paused");
161
+ });
162
+ addHandler("ended", () => {
163
+ api.setState("playing", false);
164
+ api.setState("ended", true);
165
+ api.setState("playbackState", "ended");
166
+ api.emit("playback:ended", void 0);
167
+ });
168
+ addHandler("timeupdate", () => {
169
+ api.setState("currentTime", video.currentTime);
170
+ api.emit("playback:timeupdate", { currentTime: video.currentTime });
171
+ });
172
+ addHandler("durationchange", () => {
173
+ api.setState("duration", video.duration || 0);
174
+ api.emit("media:loadedmetadata", { duration: video.duration || 0 });
175
+ });
176
+ addHandler("waiting", () => {
177
+ api.setState("waiting", true);
178
+ api.setState("buffering", true);
179
+ api.emit("media:waiting", void 0);
180
+ });
181
+ addHandler("canplay", () => {
182
+ api.setState("waiting", false);
183
+ api.setState("playbackState", "ready");
184
+ api.emit("media:canplay", void 0);
185
+ });
186
+ addHandler("canplaythrough", () => {
187
+ api.setState("buffering", false);
188
+ api.emit("media:canplaythrough", void 0);
189
+ });
190
+ addHandler("progress", () => {
191
+ if (video.buffered.length > 0) {
192
+ const bufferedEnd = video.buffered.end(video.buffered.length - 1);
193
+ const bufferedAmount = video.duration > 0 ? bufferedEnd / video.duration : 0;
194
+ api.setState("bufferedAmount", bufferedAmount);
195
+ api.setState("buffered", video.buffered);
196
+ api.emit("media:progress", { buffered: bufferedAmount });
197
+ }
198
+ });
199
+ addHandler("seeking", () => {
200
+ api.setState("seeking", true);
201
+ });
202
+ addHandler("seeked", () => {
203
+ api.setState("seeking", false);
204
+ api.emit("playback:seeked", { time: video.currentTime });
205
+ });
206
+ addHandler("volumechange", () => {
207
+ api.setState("volume", video.volume);
208
+ api.setState("muted", video.muted);
209
+ api.emit("volume:change", { volume: video.volume, muted: video.muted });
210
+ });
211
+ addHandler("ratechange", () => {
212
+ api.setState("playbackRate", video.playbackRate);
213
+ api.emit("playback:ratechange", { rate: video.playbackRate });
214
+ });
215
+ addHandler("loadedmetadata", () => {
216
+ api.setState("duration", video.duration);
217
+ api.setState("mediaType", video.videoWidth > 0 ? "video" : "audio");
218
+ });
219
+ addHandler("error", () => {
220
+ const error = video.error;
221
+ if (error) {
222
+ api.logger.error("Video element error", { code: error.code, message: error.message });
223
+ api.emit("media:error", { error: new Error(error.message || "Video playback error") });
224
+ }
225
+ });
226
+ addHandler("enterpictureinpicture", () => {
227
+ api.setState("pip", true);
228
+ api.logger.debug("PiP: entered (standard)");
229
+ });
230
+ addHandler("leavepictureinpicture", () => {
231
+ api.setState("pip", false);
232
+ api.logger.debug("PiP: exited (standard)");
233
+ if (!video.paused || api.getState("playing")) {
234
+ video.play().catch(() => {
235
+ });
236
+ }
237
+ });
238
+ const webkitVideo = video;
239
+ if ("webkitPresentationMode" in video) {
240
+ addHandler("webkitpresentationmodechanged", () => {
241
+ const mode = webkitVideo.webkitPresentationMode;
242
+ const isInPip = mode === "picture-in-picture";
243
+ api.setState("pip", isInPip);
244
+ api.logger.debug(`PiP: mode changed to ${mode} (webkit)`);
245
+ if (mode === "inline" && video.paused) {
246
+ video.play().catch(() => {
247
+ });
248
+ }
249
+ });
250
+ }
251
+ return () => {
252
+ for (const { event, handler } of handlers) {
253
+ video.removeEventListener(event, handler);
254
+ }
255
+ handlers.length = 0;
256
+ };
257
+ }
258
+
259
+ export {
260
+ mapLevels,
261
+ setupHlsEventHandlers,
262
+ setupVideoEventHandlers
263
+ };
package/dist/index.cjs CHANGED
@@ -390,6 +390,10 @@ function createHLSPlugin(config) {
390
390
  video.preload = "metadata";
391
391
  video.controls = false;
392
392
  video.playsInline = true;
393
+ const poster = api?.getState("poster");
394
+ if (poster) {
395
+ video.poster = poster;
396
+ }
393
397
  api?.container.appendChild(video);
394
398
  return video;
395
399
  };
package/dist/index.d.cts CHANGED
@@ -1,102 +1,6 @@
1
- import { Plugin } from '@scarlett-player/core';
2
-
3
- /**
4
- * HLS Plugin Types
5
- */
6
-
7
- /** HLS plugin configuration options */
8
- interface HLSPluginConfig {
9
- /** Enable debug logging */
10
- debug?: boolean;
11
- /** Auto start loading when source is set */
12
- autoStartLoad?: boolean;
13
- /** Start position in seconds (-1 for default) */
14
- startPosition?: number;
15
- /** Enable low latency mode for live streams */
16
- lowLatencyMode?: boolean;
17
- /** Max buffer length in seconds */
18
- maxBufferLength?: number;
19
- /** Max max buffer length in seconds */
20
- maxMaxBufferLength?: number;
21
- /** Back buffer length in seconds for DVR */
22
- backBufferLength?: number;
23
- /** Enable worker for hls.js (better performance) */
24
- enableWorker?: boolean;
25
- /** Max network error retries before giving up (default: 3) */
26
- maxNetworkRetries?: number;
27
- /** Max media error retries before giving up (default: 2) */
28
- maxMediaRetries?: number;
29
- /** Base retry delay in milliseconds (default: 1000) */
30
- retryDelayMs?: number;
31
- /** Exponential backoff multiplier (default: 2) */
32
- retryBackoffFactor?: number;
33
- /** Index signature for PluginConfig compatibility */
34
- [key: string]: unknown;
35
- }
36
- /** Quality level information */
37
- interface HLSQualityLevel {
38
- /** Level index in hls.js */
39
- index: number;
40
- /** Video width */
41
- width: number;
42
- /** Video height */
43
- height: number;
44
- /** Bitrate in bits per second */
45
- bitrate: number;
46
- /** Human-readable label (e.g., "1080p") */
47
- label: string;
48
- /** Codec info */
49
- codec?: string;
50
- }
51
- /** HLS error types */
52
- type HLSErrorType = 'network' | 'media' | 'mux' | 'other';
53
- /** HLS error details */
54
- interface HLSError {
55
- type: HLSErrorType;
56
- details: string;
57
- fatal: boolean;
58
- url?: string;
59
- reason?: string;
60
- response?: {
61
- code: number;
62
- text: string;
63
- };
64
- }
65
- /** Live stream info */
66
- interface HLSLiveInfo {
67
- /** Whether stream is live */
68
- isLive: boolean;
69
- /** Edge latency in seconds */
70
- latency: number;
71
- /** Target latency for low latency mode */
72
- targetLatency: number;
73
- /** Drift from live edge */
74
- drift: number;
75
- }
76
- /** HLS Plugin interface extending base Plugin */
77
- interface IHLSPlugin extends Plugin<HLSPluginConfig> {
78
- readonly id: 'hls-provider';
79
- /** Check if this provider can play a source */
80
- canPlay(src: string): boolean;
81
- /** Load and play a source */
82
- loadSource(src: string): Promise<void>;
83
- /** Get current quality level index (-1 = auto) */
84
- getCurrentLevel(): number;
85
- /** Set quality level (-1 for auto) */
86
- setLevel(index: number): void;
87
- /** Get all available quality levels */
88
- getLevels(): HLSQualityLevel[];
89
- /** Get the raw hls.js instance (for advanced use) */
90
- getHlsInstance(): unknown | null;
91
- /** Check if using native HLS (Safari) */
92
- isNativeHLS(): boolean;
93
- /** Get live stream info */
94
- getLiveInfo(): HLSLiveInfo | null;
95
- /** Switch from hls.js to native HLS (for AirPlay) */
96
- switchToNative(): Promise<void>;
97
- /** Switch from native HLS back to hls.js */
98
- switchToHlsJs(): Promise<void>;
99
- }
1
+ import { H as HLSPluginConfig, I as IHLSPlugin } from './types-Co8zOXVb.cjs';
2
+ export { b as HLSError, c as HLSLiveInfo, a as HLSQualityLevel } from './types-Co8zOXVb.cjs';
3
+ import '@scarlett-player/core';
100
4
 
101
5
  /**
102
6
  * HLS Provider Plugin for Scarlett Player
@@ -130,4 +34,4 @@ interface IHLSPlugin extends Plugin<HLSPluginConfig> {
130
34
  */
131
35
  declare function createHLSPlugin(config?: Partial<HLSPluginConfig>): IHLSPlugin;
132
36
 
133
- export { type HLSError, type HLSLiveInfo, type HLSPluginConfig, type HLSQualityLevel, type IHLSPlugin, createHLSPlugin, createHLSPlugin as default };
37
+ export { HLSPluginConfig, IHLSPlugin, createHLSPlugin, createHLSPlugin as default };
package/dist/index.d.ts CHANGED
@@ -1,102 +1,6 @@
1
- import { Plugin } from '@scarlett-player/core';
2
-
3
- /**
4
- * HLS Plugin Types
5
- */
6
-
7
- /** HLS plugin configuration options */
8
- interface HLSPluginConfig {
9
- /** Enable debug logging */
10
- debug?: boolean;
11
- /** Auto start loading when source is set */
12
- autoStartLoad?: boolean;
13
- /** Start position in seconds (-1 for default) */
14
- startPosition?: number;
15
- /** Enable low latency mode for live streams */
16
- lowLatencyMode?: boolean;
17
- /** Max buffer length in seconds */
18
- maxBufferLength?: number;
19
- /** Max max buffer length in seconds */
20
- maxMaxBufferLength?: number;
21
- /** Back buffer length in seconds for DVR */
22
- backBufferLength?: number;
23
- /** Enable worker for hls.js (better performance) */
24
- enableWorker?: boolean;
25
- /** Max network error retries before giving up (default: 3) */
26
- maxNetworkRetries?: number;
27
- /** Max media error retries before giving up (default: 2) */
28
- maxMediaRetries?: number;
29
- /** Base retry delay in milliseconds (default: 1000) */
30
- retryDelayMs?: number;
31
- /** Exponential backoff multiplier (default: 2) */
32
- retryBackoffFactor?: number;
33
- /** Index signature for PluginConfig compatibility */
34
- [key: string]: unknown;
35
- }
36
- /** Quality level information */
37
- interface HLSQualityLevel {
38
- /** Level index in hls.js */
39
- index: number;
40
- /** Video width */
41
- width: number;
42
- /** Video height */
43
- height: number;
44
- /** Bitrate in bits per second */
45
- bitrate: number;
46
- /** Human-readable label (e.g., "1080p") */
47
- label: string;
48
- /** Codec info */
49
- codec?: string;
50
- }
51
- /** HLS error types */
52
- type HLSErrorType = 'network' | 'media' | 'mux' | 'other';
53
- /** HLS error details */
54
- interface HLSError {
55
- type: HLSErrorType;
56
- details: string;
57
- fatal: boolean;
58
- url?: string;
59
- reason?: string;
60
- response?: {
61
- code: number;
62
- text: string;
63
- };
64
- }
65
- /** Live stream info */
66
- interface HLSLiveInfo {
67
- /** Whether stream is live */
68
- isLive: boolean;
69
- /** Edge latency in seconds */
70
- latency: number;
71
- /** Target latency for low latency mode */
72
- targetLatency: number;
73
- /** Drift from live edge */
74
- drift: number;
75
- }
76
- /** HLS Plugin interface extending base Plugin */
77
- interface IHLSPlugin extends Plugin<HLSPluginConfig> {
78
- readonly id: 'hls-provider';
79
- /** Check if this provider can play a source */
80
- canPlay(src: string): boolean;
81
- /** Load and play a source */
82
- loadSource(src: string): Promise<void>;
83
- /** Get current quality level index (-1 = auto) */
84
- getCurrentLevel(): number;
85
- /** Set quality level (-1 for auto) */
86
- setLevel(index: number): void;
87
- /** Get all available quality levels */
88
- getLevels(): HLSQualityLevel[];
89
- /** Get the raw hls.js instance (for advanced use) */
90
- getHlsInstance(): unknown | null;
91
- /** Check if using native HLS (Safari) */
92
- isNativeHLS(): boolean;
93
- /** Get live stream info */
94
- getLiveInfo(): HLSLiveInfo | null;
95
- /** Switch from hls.js to native HLS (for AirPlay) */
96
- switchToNative(): Promise<void>;
97
- /** Switch from native HLS back to hls.js */
98
- switchToHlsJs(): Promise<void>;
99
- }
1
+ import { H as HLSPluginConfig, I as IHLSPlugin } from './types-Co8zOXVb.js';
2
+ export { b as HLSError, c as HLSLiveInfo, a as HLSQualityLevel } from './types-Co8zOXVb.js';
3
+ import '@scarlett-player/core';
100
4
 
101
5
  /**
102
6
  * HLS Provider Plugin for Scarlett Player
@@ -130,4 +34,4 @@ interface IHLSPlugin extends Plugin<HLSPluginConfig> {
130
34
  */
131
35
  declare function createHLSPlugin(config?: Partial<HLSPluginConfig>): IHLSPlugin;
132
36
 
133
- export { type HLSError, type HLSLiveInfo, type HLSPluginConfig, type HLSQualityLevel, type IHLSPlugin, createHLSPlugin, createHLSPlugin as default };
37
+ export { HLSPluginConfig, IHLSPlugin, createHLSPlugin, createHLSPlugin as default };