stormcloud-video-player 0.1.2 → 0.1.4

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.
@@ -1,1123 +0,0 @@
1
- import Hls from "hls.js";
2
- import type {
3
- StormcloudVideoPlayerConfig,
4
- Scte35Marker,
5
- Id3TagInfo,
6
- ImaController,
7
- AdBreak,
8
- AdSchedule,
9
- StormcloudApiResponse,
10
- } from "../types";
11
- import { createImaController } from "../sdk/ima";
12
- import { sendInitialTracking, sendHeartbeat } from "../utils/tracking";
13
-
14
- export class StormcloudVideoPlayer {
15
- private readonly video: HTMLVideoElement;
16
- private readonly config: StormcloudVideoPlayerConfig;
17
- private hls?: Hls;
18
- private ima: ImaController;
19
- private attached = false;
20
- private adSchedule: AdSchedule | undefined;
21
- private inAdBreak = false;
22
- private currentAdBreakStartWallClockMs: number | undefined;
23
- private expectedAdBreakDurationMs: number | undefined;
24
- private adStopTimerId: number | undefined;
25
- private adStartTimerId: number | undefined;
26
- private adFailsafeTimerId: number | undefined;
27
- private ptsDriftEmaMs = 0;
28
- private adPodQueue: string[] = [];
29
- private apiVastTagUrl: string | undefined;
30
- private vastConfig:
31
- | StormcloudApiResponse["response"]["options"]["vast"]
32
- | undefined;
33
- private lastHeartbeatTime: number = 0;
34
- private heartbeatInterval: number | undefined;
35
- private currentAdIndex: number = 0;
36
- private totalAdsInBreak: number = 0;
37
- private showAds: boolean = false;
38
-
39
- constructor(config: StormcloudVideoPlayerConfig) {
40
- this.config = config;
41
- this.video = config.videoElement;
42
- this.adSchedule = config.adSchedule;
43
- this.ima = createImaController(this.video);
44
- }
45
-
46
- async load(): Promise<void> {
47
- if (!this.attached) {
48
- this.attach();
49
- }
50
-
51
- try {
52
- await this.fetchAdConfiguration();
53
- } catch (error) {
54
- if (this.config.debugAdTiming) {
55
- console.warn(
56
- "[StormcloudVideoPlayer] Failed to fetch ad configuration:",
57
- error
58
- );
59
- }
60
- }
61
-
62
- this.initializeTracking();
63
-
64
- if (this.shouldUseNativeHls()) {
65
- this.video.src = this.config.src;
66
- if (this.config.autoplay) {
67
- await this.video.play().catch(() => {});
68
- }
69
- return;
70
- }
71
-
72
- this.hls = new Hls({
73
- enableWorker: true,
74
- backBufferLength: 30,
75
- liveDurationInfinity: true,
76
- lowLatencyMode: !!this.config.lowLatencyMode,
77
- maxLiveSyncPlaybackRate: this.config.lowLatencyMode ? 1.5 : 1.0,
78
- ...(this.config.lowLatencyMode ? { liveSyncDuration: 2 } : {}),
79
- });
80
-
81
- this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
82
- this.hls?.loadSource(this.config.src);
83
- });
84
-
85
- this.hls.on(Hls.Events.MANIFEST_PARSED, async () => {
86
- if (this.config.autoplay) {
87
- await this.video.play().catch(() => {});
88
- }
89
- });
90
-
91
- this.hls.on(Hls.Events.FRAG_PARSING_METADATA, (_evt, data: any) => {
92
- const id3Tags: Id3TagInfo[] = (data?.samples || []).map((s: any) => ({
93
- key: "ID3",
94
- value: s?.data,
95
- ptsSeconds: s?.pts,
96
- }));
97
- id3Tags.forEach((tag) => this.onId3Tag(tag));
98
- });
99
-
100
- this.hls.on(Hls.Events.FRAG_CHANGED, (_evt, data: any) => {
101
- const frag = data?.frag;
102
- const tagList: any[] | undefined = frag?.tagList;
103
- if (!Array.isArray(tagList)) return;
104
-
105
- for (const entry of tagList) {
106
- let tag = "";
107
- let value = "";
108
- if (Array.isArray(entry)) {
109
- tag = String(entry[0] ?? "");
110
- value = String(entry[1] ?? "");
111
- } else if (typeof entry === "string") {
112
- const idx = entry.indexOf(":");
113
- if (idx >= 0) {
114
- tag = entry.substring(0, idx);
115
- value = entry.substring(idx + 1);
116
- } else {
117
- tag = entry;
118
- value = "";
119
- }
120
- }
121
-
122
- if (!tag) continue;
123
- if (tag.includes("EXT-X-CUE-OUT")) {
124
- const durationSeconds = this.parseCueOutDuration(value);
125
- const marker: Scte35Marker = {
126
- type: "start",
127
- ...(durationSeconds !== undefined ? { durationSeconds } : {}),
128
- raw: { tag, value },
129
- } as Scte35Marker;
130
- this.onScte35Marker(marker);
131
- } else if (tag.includes("EXT-X-CUE-OUT-CONT")) {
132
- const prog = this.parseCueOutCont(value);
133
- const marker: Scte35Marker = {
134
- type: "progress",
135
- ...(prog?.duration !== undefined
136
- ? { durationSeconds: prog.duration }
137
- : {}),
138
- ...(prog?.elapsed !== undefined
139
- ? { ptsSeconds: prog.elapsed }
140
- : {}),
141
- raw: { tag, value },
142
- } as Scte35Marker;
143
- this.onScte35Marker(marker);
144
- } else if (tag.includes("EXT-X-CUE-IN")) {
145
- this.onScte35Marker({ type: "end", raw: { tag, value } });
146
- } else if (tag.includes("EXT-X-DATERANGE")) {
147
- const attrs = this.parseAttributeList(value);
148
- const hasScteOut =
149
- "SCTE35-OUT" in attrs || attrs["SCTE35-OUT"] !== undefined;
150
- const hasScteIn =
151
- "SCTE35-IN" in attrs || attrs["SCTE35-IN"] !== undefined;
152
- const klass = String(attrs["CLASS"] ?? "");
153
- const duration = this.toNumber(attrs["DURATION"]);
154
-
155
- if (hasScteOut || /com\.apple\.hls\.cue/i.test(klass)) {
156
- const marker: Scte35Marker = {
157
- type: "start",
158
- ...(duration !== undefined ? { durationSeconds: duration } : {}),
159
- raw: { tag, value, attrs },
160
- } as Scte35Marker;
161
- this.onScte35Marker(marker);
162
- }
163
- if (hasScteIn) {
164
- this.onScte35Marker({ type: "end", raw: { tag, value, attrs } });
165
- }
166
- }
167
- }
168
- });
169
-
170
- this.hls.on(Hls.Events.ERROR, (_evt, data) => {
171
- if (data?.fatal) {
172
- switch (data.type) {
173
- case Hls.ErrorTypes.NETWORK_ERROR:
174
- this.hls?.startLoad();
175
- break;
176
- case Hls.ErrorTypes.MEDIA_ERROR:
177
- this.hls?.recoverMediaError();
178
- break;
179
- default:
180
- this.destroy();
181
- break;
182
- }
183
- }
184
- });
185
-
186
- this.hls.attachMedia(this.video);
187
- }
188
-
189
- private attach(): void {
190
- if (this.attached) return;
191
- this.attached = true;
192
- this.video.autoplay = !!this.config.autoplay;
193
- this.video.muted = !!this.config.muted;
194
-
195
- this.ima.initialize();
196
- this.ima.on("all_ads_completed", () => {
197
- if (!this.inAdBreak) return;
198
- const remaining = this.getRemainingAdMs();
199
- if (remaining > 500 && this.adPodQueue.length > 0) {
200
- const next = this.adPodQueue.shift()!;
201
- this.currentAdIndex++;
202
- this.playSingleAd(next).catch(() => {});
203
- } else {
204
- this.currentAdIndex = 0;
205
- this.totalAdsInBreak = 0;
206
- this.showAds = false;
207
- }
208
- });
209
- this.ima.on("ad_error", () => {
210
- if (this.config.debugAdTiming) {
211
- console.log("[StormcloudVideoPlayer] IMA ad_error event received");
212
- }
213
- if (!this.inAdBreak) return;
214
- const remaining = this.getRemainingAdMs();
215
- if (remaining > 500 && this.adPodQueue.length > 0) {
216
- const next = this.adPodQueue.shift()!;
217
- this.currentAdIndex++;
218
- this.playSingleAd(next).catch(() => {});
219
- } else {
220
- this.handleAdFailure();
221
- }
222
- });
223
- this.ima.on("content_pause", () => {
224
- if (this.config.debugAdTiming) {
225
- console.log("[StormcloudVideoPlayer] IMA content_pause event received");
226
- }
227
- this.clearAdFailsafeTimer();
228
- });
229
- this.ima.on("content_resume", () => {
230
- if (this.config.debugAdTiming) {
231
- console.log(
232
- "[StormcloudVideoPlayer] IMA content_resume event received"
233
- );
234
- }
235
- this.clearAdFailsafeTimer();
236
- });
237
-
238
- this.video.addEventListener("timeupdate", () => {
239
- this.onTimeUpdate(this.video.currentTime);
240
- });
241
- }
242
-
243
- private shouldUseNativeHls(): boolean {
244
- const canNative = this.video.canPlayType("application/vnd.apple.mpegURL");
245
- return !!(this.config.allowNativeHls && canNative);
246
- }
247
-
248
- private onId3Tag(tag: Id3TagInfo): void {
249
- if (typeof tag.ptsSeconds === "number") {
250
- this.updatePtsDrift(tag.ptsSeconds);
251
- }
252
- const marker = this.parseScte35FromId3(tag);
253
- if (marker) {
254
- this.onScte35Marker(marker);
255
- }
256
- }
257
-
258
- private parseScte35FromId3(tag: Id3TagInfo): Scte35Marker | undefined {
259
- const text = this.decodeId3ValueToText(tag.value);
260
- if (!text) return undefined;
261
-
262
- const cueOutMatch =
263
- text.match(/EXT-X-CUE-OUT(?::([^\r\n]*))?/i) ||
264
- text.match(/CUE-OUT(?::([^\r\n]*))?/i);
265
- if (cueOutMatch) {
266
- const arg = (cueOutMatch[1] ?? "").trim();
267
- const dur = this.parseCueOutDuration(arg);
268
- const marker: Scte35Marker = {
269
- type: "start",
270
- ...(tag.ptsSeconds !== undefined ? { ptsSeconds: tag.ptsSeconds } : {}),
271
- ...(dur !== undefined ? { durationSeconds: dur } : {}),
272
- raw: { id3: text },
273
- } as Scte35Marker;
274
- return marker;
275
- }
276
-
277
- const cueOutContMatch = text.match(/EXT-X-CUE-OUT-CONT:([^\r\n]*)/i);
278
- if (cueOutContMatch) {
279
- const arg = (cueOutContMatch[1] ?? "").trim();
280
- const cont = this.parseCueOutCont(arg);
281
- const marker: Scte35Marker = {
282
- type: "progress",
283
- ...(tag.ptsSeconds !== undefined ? { ptsSeconds: tag.ptsSeconds } : {}),
284
- ...(cont?.duration !== undefined
285
- ? { durationSeconds: cont.duration }
286
- : {}),
287
- raw: { id3: text },
288
- } as Scte35Marker;
289
- return marker;
290
- }
291
-
292
- const cueInMatch = text.match(/EXT-X-CUE-IN\b/i) || text.match(/CUE-IN\b/i);
293
- if (cueInMatch) {
294
- const marker: Scte35Marker = {
295
- type: "end",
296
- ...(tag.ptsSeconds !== undefined ? { ptsSeconds: tag.ptsSeconds } : {}),
297
- raw: { id3: text },
298
- } as Scte35Marker;
299
- return marker;
300
- }
301
-
302
- const daterangeMatch = text.match(/EXT-X-DATERANGE:([^\r\n]*)/i);
303
- if (daterangeMatch) {
304
- const attrs = this.parseAttributeList(daterangeMatch[1] ?? "");
305
- const hasScteOut =
306
- "SCTE35-OUT" in attrs || attrs["SCTE35-OUT"] !== undefined;
307
- const hasScteIn =
308
- "SCTE35-IN" in attrs || attrs["SCTE35-IN"] !== undefined;
309
- const klass = String(attrs["CLASS"] ?? "");
310
- const duration = this.toNumber(attrs["DURATION"]);
311
- if (hasScteOut || /com\.apple\.hls\.cue/i.test(klass)) {
312
- const marker: Scte35Marker = {
313
- type: "start",
314
- ...(tag.ptsSeconds !== undefined
315
- ? { ptsSeconds: tag.ptsSeconds }
316
- : {}),
317
- ...(duration !== undefined ? { durationSeconds: duration } : {}),
318
- raw: { id3: text, attrs },
319
- } as Scte35Marker;
320
- return marker;
321
- }
322
- if (hasScteIn) {
323
- const marker: Scte35Marker = {
324
- type: "end",
325
- ...(tag.ptsSeconds !== undefined
326
- ? { ptsSeconds: tag.ptsSeconds }
327
- : {}),
328
- raw: { id3: text, attrs },
329
- } as Scte35Marker;
330
- return marker;
331
- }
332
- }
333
-
334
- if (/SCTE35-OUT/i.test(text)) {
335
- const marker: Scte35Marker = {
336
- type: "start",
337
- ...(tag.ptsSeconds !== undefined ? { ptsSeconds: tag.ptsSeconds } : {}),
338
- raw: { id3: text },
339
- } as Scte35Marker;
340
- return marker;
341
- }
342
- if (/SCTE35-IN/i.test(text)) {
343
- const marker: Scte35Marker = {
344
- type: "end",
345
- ...(tag.ptsSeconds !== undefined ? { ptsSeconds: tag.ptsSeconds } : {}),
346
- raw: { id3: text },
347
- } as Scte35Marker;
348
- return marker;
349
- }
350
-
351
- if (tag.value instanceof Uint8Array) {
352
- const bin = this.parseScte35Binary(tag.value);
353
- if (bin) return bin;
354
- }
355
-
356
- return undefined;
357
- }
358
-
359
- private decodeId3ValueToText(value: string | Uint8Array): string | undefined {
360
- try {
361
- if (typeof value === "string") return value;
362
- const decoder = new TextDecoder("utf-8", { fatal: false });
363
- const text = decoder.decode(value);
364
- if (text && /[\x20-\x7E]/.test(text)) return text;
365
- let out = "";
366
- for (let i = 0; i < value.length; i++)
367
- out += String.fromCharCode(value[i]!);
368
- return out;
369
- } catch {
370
- return undefined;
371
- }
372
- }
373
-
374
- private onScte35Marker(marker: Scte35Marker): void {
375
- if (this.config.debugAdTiming) {
376
- console.log("[StormcloudVideoPlayer] SCTE-35 marker detected:", {
377
- type: marker.type,
378
- ptsSeconds: marker.ptsSeconds,
379
- durationSeconds: marker.durationSeconds,
380
- currentTime: this.video.currentTime,
381
- raw: marker.raw,
382
- });
383
- }
384
-
385
- if (marker.type === "start") {
386
- this.inAdBreak = true;
387
- const durationMs =
388
- marker.durationSeconds != null
389
- ? marker.durationSeconds * 1000
390
- : undefined;
391
- this.expectedAdBreakDurationMs = durationMs;
392
- this.currentAdBreakStartWallClockMs = Date.now();
393
-
394
- const policy = this.adSchedule?.lateJoinPolicy ?? "play_remaining";
395
- if (policy !== "skip_to_content") {
396
- const isManifestMarker = this.isManifestBasedMarker(marker);
397
- const forceImmediate = this.config.immediateManifestAds ?? true; // Default to true for better UX
398
-
399
- if (this.config.debugAdTiming) {
400
- console.log("[StormcloudVideoPlayer] Ad start decision:", {
401
- isManifestMarker,
402
- forceImmediate,
403
- hasPts: typeof marker.ptsSeconds === "number",
404
- policy,
405
- });
406
- }
407
-
408
- if (isManifestMarker && forceImmediate) {
409
- if (this.config.debugAdTiming) {
410
- console.log(
411
- "[StormcloudVideoPlayer] Starting ad immediately (manifest-based)"
412
- );
413
- }
414
- this.clearAdStartTimer();
415
- this.handleAdStart(marker);
416
- } else if (typeof marker.ptsSeconds === "number") {
417
- const tol = this.config.driftToleranceMs ?? 1000;
418
- const nowMs = this.video.currentTime * 1000;
419
- const estCurrentPtsMs = nowMs - this.ptsDriftEmaMs;
420
- const deltaMs = Math.floor(
421
- marker.ptsSeconds * 1000 - estCurrentPtsMs
422
- );
423
-
424
- if (this.config.debugAdTiming) {
425
- console.log(
426
- "[StormcloudVideoPlayer] PTS-based timing calculation:",
427
- {
428
- nowMs,
429
- estCurrentPtsMs,
430
- markerPtsMs: marker.ptsSeconds * 1000,
431
- deltaMs,
432
- tolerance: tol,
433
- }
434
- );
435
- }
436
-
437
- if (deltaMs > tol) {
438
- if (this.config.debugAdTiming) {
439
- console.log(
440
- `[StormcloudVideoPlayer] Scheduling ad start in ${deltaMs}ms`
441
- );
442
- }
443
- this.scheduleAdStartIn(deltaMs);
444
- } else {
445
- if (this.config.debugAdTiming) {
446
- console.log(
447
- "[StormcloudVideoPlayer] Starting ad immediately (within tolerance)"
448
- );
449
- }
450
- this.clearAdStartTimer();
451
- this.handleAdStart(marker);
452
- }
453
- } else {
454
- if (this.config.debugAdTiming) {
455
- console.log(
456
- "[StormcloudVideoPlayer] Starting ad immediately (fallback)"
457
- );
458
- }
459
- this.clearAdStartTimer();
460
- this.handleAdStart(marker);
461
- }
462
- }
463
- if (this.expectedAdBreakDurationMs != null) {
464
- this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
465
- }
466
- return;
467
- }
468
- if (marker.type === "progress" && this.inAdBreak) {
469
- if (marker.durationSeconds != null) {
470
- this.expectedAdBreakDurationMs = marker.durationSeconds * 1000;
471
- }
472
- if (
473
- this.expectedAdBreakDurationMs != null &&
474
- this.currentAdBreakStartWallClockMs != null
475
- ) {
476
- const elapsedMs = Date.now() - this.currentAdBreakStartWallClockMs;
477
- const remainingMs = Math.max(
478
- 0,
479
- this.expectedAdBreakDurationMs - elapsedMs
480
- );
481
- this.scheduleAdStopCountdown(remainingMs);
482
- }
483
- if (!this.ima.isAdPlaying()) {
484
- const policy = this.adSchedule?.lateJoinPolicy ?? "play_remaining";
485
- const scheduled = this.findCurrentOrNextBreak(
486
- this.video.currentTime * 1000
487
- );
488
- const tags =
489
- this.selectVastTagsForBreak(scheduled) ||
490
- (this.apiVastTagUrl ? [this.apiVastTagUrl] : undefined);
491
- if (policy === "play_remaining" && tags && tags.length > 0) {
492
- const first = tags[0] as string;
493
- const rest = tags.slice(1);
494
- this.adPodQueue = rest;
495
- this.playSingleAd(first).catch(() => {});
496
- }
497
- }
498
- return;
499
- }
500
- if (marker.type === "end") {
501
- this.inAdBreak = false;
502
- this.expectedAdBreakDurationMs = undefined;
503
- this.currentAdBreakStartWallClockMs = undefined;
504
- this.clearAdStartTimer();
505
- this.clearAdStopTimer();
506
- if (this.ima.isAdPlaying()) {
507
- this.ima.stop().catch(() => {});
508
- }
509
- return;
510
- }
511
- }
512
-
513
- private parseCueOutDuration(value: string): number | undefined {
514
- const num = parseFloat(value.trim());
515
- if (!Number.isNaN(num)) return num;
516
- const match =
517
- value.match(/(?:^|[,\s])DURATION\s*=\s*([0-9.]+)/i) ||
518
- value.match(/Duration\s*=\s*([0-9.]+)/i);
519
- if (match && match[1] != null) {
520
- const dStr = match[1];
521
- const d = parseFloat(dStr);
522
- return Number.isNaN(d) ? undefined : d;
523
- }
524
- return undefined;
525
- }
526
-
527
- private parseCueOutCont(
528
- value: string
529
- ): { elapsed?: number; duration?: number } | undefined {
530
- const elapsedMatch = value.match(/Elapsed\s*=\s*([0-9.]+)/i);
531
- const durationMatch = value.match(/Duration\s*=\s*([0-9.]+)/i);
532
- const res: { elapsed?: number; duration?: number } = {};
533
- if (elapsedMatch && elapsedMatch[1] != null) {
534
- const e = parseFloat(elapsedMatch[1]);
535
- if (!Number.isNaN(e)) res.elapsed = e;
536
- }
537
- if (durationMatch && durationMatch[1] != null) {
538
- const d = parseFloat(durationMatch[1]);
539
- if (!Number.isNaN(d)) res.duration = d;
540
- }
541
- if ("elapsed" in res || "duration" in res) return res;
542
- return undefined;
543
- }
544
-
545
- private parseAttributeList(value: string): Record<string, string> {
546
- const attrs: Record<string, string> = {};
547
- const regex = /([A-Z0-9-]+)=(("[^"]*")|([^",]*))(?:,|$)/gi;
548
- let match: RegExpExecArray | null;
549
- while ((match = regex.exec(value)) !== null) {
550
- const key: string = (match[1] ?? "") as string;
551
- let rawVal: string = (match[3] ?? match[4] ?? "") as string;
552
- if (rawVal.startsWith('"') && rawVal.endsWith('"')) {
553
- rawVal = rawVal.slice(1, -1);
554
- }
555
- if (key) {
556
- attrs[key] = rawVal;
557
- }
558
- }
559
- return attrs;
560
- }
561
-
562
- private toNumber(val: unknown): number | undefined {
563
- if (val == null) return undefined;
564
- const n = typeof val === "string" ? parseFloat(val) : Number(val);
565
- return Number.isNaN(n) ? undefined : n;
566
- }
567
-
568
- private isManifestBasedMarker(marker: Scte35Marker): boolean {
569
- const raw = marker.raw as any;
570
- if (!raw) return false;
571
-
572
- if (raw.tag) {
573
- const tag = String(raw.tag);
574
- return (
575
- tag.includes("EXT-X-CUE-OUT") ||
576
- tag.includes("EXT-X-CUE-IN") ||
577
- tag.includes("EXT-X-DATERANGE")
578
- );
579
- }
580
-
581
- if (raw.id3) return false;
582
-
583
- if (raw.splice_command_type) return false;
584
-
585
- return false;
586
- }
587
-
588
- private parseScte35Binary(data: Uint8Array): Scte35Marker | undefined {
589
- class BitReader {
590
- private bytePos = 0;
591
- private bitPos = 0; // 0..7
592
- constructor(private readonly buf: Uint8Array) {}
593
- readBits(numBits: number): number {
594
- let result = 0;
595
- while (numBits > 0) {
596
- if (this.bytePos >= this.buf.length) return result;
597
- const remainingInByte = 8 - this.bitPos;
598
- const toRead = Math.min(numBits, remainingInByte);
599
- const currentByte = this.buf[this.bytePos]!;
600
- const shift = remainingInByte - toRead;
601
- const mask = ((1 << toRead) - 1) & 0xff;
602
- const bits = (currentByte >> shift) & mask;
603
- result = (result << toRead) | bits;
604
- this.bitPos += toRead;
605
- if (this.bitPos >= 8) {
606
- this.bitPos = 0;
607
- this.bytePos += 1;
608
- }
609
- numBits -= toRead;
610
- }
611
- return result >>> 0;
612
- }
613
- skipBits(n: number): void {
614
- this.readBits(n);
615
- }
616
- }
617
-
618
- const r = new BitReader(data);
619
- const tableId = r.readBits(8);
620
- if (tableId !== 0xfc) return undefined;
621
- r.readBits(1);
622
- r.readBits(1);
623
- r.readBits(2);
624
- const sectionLength = r.readBits(12);
625
- r.readBits(8);
626
- r.readBits(1);
627
- r.readBits(6);
628
- const ptsAdjHigh = r.readBits(1);
629
- const ptsAdjLow = r.readBits(32);
630
- void ptsAdjHigh;
631
- void ptsAdjLow;
632
- r.readBits(8);
633
- r.readBits(12);
634
- const spliceCommandLength = r.readBits(12);
635
- const spliceCommandType = r.readBits(8);
636
- if (spliceCommandType !== 5) {
637
- return undefined;
638
- }
639
- r.readBits(32);
640
- const cancel = r.readBits(1) === 1;
641
- r.readBits(7);
642
- if (cancel) return undefined;
643
- const outOfNetwork = r.readBits(1) === 1;
644
- const programSpliceFlag = r.readBits(1) === 1;
645
- const durationFlag = r.readBits(1) === 1;
646
- const spliceImmediateFlag = r.readBits(1) === 1;
647
- r.readBits(4);
648
- if (programSpliceFlag && !spliceImmediateFlag) {
649
- const timeSpecifiedFlag = r.readBits(1) === 1;
650
- if (timeSpecifiedFlag) {
651
- r.readBits(6);
652
- r.readBits(33);
653
- } else {
654
- r.readBits(7);
655
- }
656
- } else if (!programSpliceFlag) {
657
- const componentCount = r.readBits(8);
658
- for (let i = 0; i < componentCount; i++) {
659
- r.readBits(8);
660
- if (!spliceImmediateFlag) {
661
- const timeSpecifiedFlag = r.readBits(1) === 1;
662
- if (timeSpecifiedFlag) {
663
- r.readBits(6);
664
- r.readBits(33);
665
- } else {
666
- r.readBits(7);
667
- }
668
- }
669
- }
670
- }
671
- let durationSeconds: number | undefined = undefined;
672
- if (durationFlag) {
673
- r.readBits(6);
674
- r.readBits(1);
675
- const high = r.readBits(1);
676
- const low = r.readBits(32);
677
- const durationTicks = high * 0x100000000 + low;
678
- durationSeconds = durationTicks / 90000;
679
- }
680
- r.readBits(16);
681
- r.readBits(8);
682
- r.readBits(8);
683
-
684
- if (outOfNetwork) {
685
- const marker: Scte35Marker = {
686
- type: "start",
687
- ...(durationSeconds !== undefined ? { durationSeconds } : {}),
688
- raw: { splice_command_type: 5 },
689
- } as Scte35Marker;
690
- return marker;
691
- }
692
- return undefined;
693
- }
694
-
695
- private initializeTracking(): void {
696
- sendInitialTracking().catch((error) => {
697
- if (this.config.debugAdTiming) {
698
- console.warn(
699
- "[StormcloudVideoPlayer] Failed to send initial tracking:",
700
- error
701
- );
702
- }
703
- });
704
-
705
- this.heartbeatInterval = window.setInterval(() => {
706
- this.sendHeartbeatIfNeeded();
707
- }, 1000);
708
- }
709
-
710
- private sendHeartbeatIfNeeded(): void {
711
- const now = Date.now();
712
- if (!this.lastHeartbeatTime || now - this.lastHeartbeatTime > 10000) {
713
- this.lastHeartbeatTime = now;
714
- sendHeartbeat().catch((error) => {
715
- if (this.config.debugAdTiming) {
716
- console.warn(
717
- "[StormcloudVideoPlayer] Failed to send heartbeat:",
718
- error
719
- );
720
- }
721
- });
722
- }
723
- }
724
-
725
- private async fetchAdConfiguration(): Promise<void> {
726
- const apiUrl = "https://adstorm.co/api-adstorm-dev/adstorm/ads/web";
727
-
728
- if (this.config.debugAdTiming) {
729
- console.log(
730
- "[StormcloudVideoPlayer] Fetching ad configuration from:",
731
- apiUrl
732
- );
733
- }
734
-
735
- const response = await fetch(apiUrl);
736
- if (!response.ok) {
737
- throw new Error(`Failed to fetch ad configuration: ${response.status}`);
738
- }
739
-
740
- const data: StormcloudApiResponse = await response.json();
741
-
742
- const imaPayload = data.response?.ima?.["publisherdesk.ima"]?.payload;
743
- if (imaPayload) {
744
- this.apiVastTagUrl = decodeURIComponent(imaPayload);
745
- if (this.config.debugAdTiming) {
746
- console.log(
747
- "[StormcloudVideoPlayer] Extracted VAST tag URL:",
748
- this.apiVastTagUrl
749
- );
750
- }
751
- }
752
-
753
- this.vastConfig = data.response?.options?.vast;
754
-
755
- if (this.config.debugAdTiming) {
756
- console.log("[StormcloudVideoPlayer] Ad configuration loaded:", {
757
- vastTagUrl: this.apiVastTagUrl,
758
- vastConfig: this.vastConfig,
759
- });
760
- }
761
- }
762
-
763
- setAdSchedule(schedule: AdSchedule | undefined): void {
764
- this.adSchedule = schedule;
765
- }
766
-
767
- getCurrentAdIndex(): number {
768
- return this.currentAdIndex;
769
- }
770
-
771
- getTotalAdsInBreak(): number {
772
- return this.totalAdsInBreak;
773
- }
774
-
775
- isAdPlaying(): boolean {
776
- return this.inAdBreak && this.ima.isAdPlaying();
777
- }
778
-
779
- isShowingAds(): boolean {
780
- return this.showAds;
781
- }
782
-
783
- getStreamType(): "hls" | "other" {
784
- const url = this.config.src.toLowerCase();
785
- if (
786
- url.includes(".m3u8") ||
787
- url.includes("/hls/") ||
788
- url.includes("application/vnd.apple.mpegurl")
789
- ) {
790
- return "hls";
791
- }
792
- return "other";
793
- }
794
-
795
- shouldShowNativeControls(): boolean {
796
- return this.getStreamType() !== "hls";
797
- }
798
-
799
- async loadAdScheduleFromUrl(url: string): Promise<void> {
800
- const res = await fetch(url);
801
- if (!res.ok) throw new Error(`Failed to fetch ad schedule: ${res.status}`);
802
- const data = await res.json();
803
- this.adSchedule = data as AdSchedule;
804
- }
805
-
806
- async loadDefaultVastFromAiry(
807
- airyApiUrl: string,
808
- params?: Record<string, string>
809
- ): Promise<void> {
810
- const usp = new URLSearchParams(params || {});
811
- const url = `${airyApiUrl}?${usp.toString()}`;
812
- const res = await fetch(url);
813
- if (!res.ok) throw new Error(`Failed to fetch Airy ads: ${res.status}`);
814
- const data = await res.json();
815
- const tag = data?.adTagUrl || data?.vastTagUrl || data?.tagUrl;
816
- if (typeof tag === "string" && tag.length > 0) {
817
- this.apiVastTagUrl = tag;
818
- }
819
- }
820
-
821
- private async handleAdStart(_marker: Scte35Marker): Promise<void> {
822
- const scheduled = this.findCurrentOrNextBreak(
823
- this.video.currentTime * 1000
824
- );
825
- const tags = this.selectVastTagsForBreak(scheduled);
826
-
827
- let vastTagUrl: string | undefined;
828
- let adsNumber = 1;
829
-
830
- if (this.apiVastTagUrl) {
831
- vastTagUrl = this.apiVastTagUrl;
832
-
833
- if (this.vastConfig) {
834
- const isHls =
835
- this.config.src.includes(".m3u8") || this.config.src.includes("hls");
836
- if (isHls && this.vastConfig.cue_tones?.number_ads) {
837
- adsNumber = this.vastConfig.cue_tones.number_ads;
838
- } else if (!isHls && this.vastConfig.timer_vod?.number_ads) {
839
- adsNumber = this.vastConfig.timer_vod.number_ads;
840
- }
841
- }
842
-
843
- this.adPodQueue = new Array(adsNumber - 1).fill(vastTagUrl);
844
- this.currentAdIndex = 0;
845
- this.totalAdsInBreak = adsNumber;
846
-
847
- if (this.config.debugAdTiming) {
848
- console.log(
849
- `[StormcloudVideoPlayer] Using API VAST tag with ${adsNumber} ads:`,
850
- vastTagUrl
851
- );
852
- }
853
- } else if (tags && tags.length > 0) {
854
- vastTagUrl = tags[0] as string;
855
- const rest = tags.slice(1);
856
- this.adPodQueue = rest;
857
- this.currentAdIndex = 0;
858
- this.totalAdsInBreak = tags.length;
859
-
860
- if (this.config.debugAdTiming) {
861
- console.log(
862
- "[StormcloudVideoPlayer] Using scheduled VAST tag:",
863
- vastTagUrl
864
- );
865
- }
866
- } else {
867
- if (this.config.debugAdTiming) {
868
- console.log("[StormcloudVideoPlayer] No VAST tag available for ad");
869
- }
870
- return;
871
- }
872
-
873
- if (vastTagUrl) {
874
- this.showAds = true;
875
- this.currentAdIndex++;
876
- await this.playSingleAd(vastTagUrl);
877
- }
878
- if (
879
- this.expectedAdBreakDurationMs == null &&
880
- scheduled?.durationMs != null
881
- ) {
882
- this.expectedAdBreakDurationMs = scheduled.durationMs;
883
- this.currentAdBreakStartWallClockMs =
884
- this.currentAdBreakStartWallClockMs ?? Date.now();
885
- this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
886
- }
887
- }
888
-
889
- private findCurrentOrNextBreak(nowMs: number): AdBreak | undefined {
890
- const schedule = this.adSchedule?.breaks || [];
891
- let candidate: AdBreak | undefined;
892
- for (const b of schedule) {
893
- const tol = this.config.driftToleranceMs ?? 1000;
894
- if (
895
- b.startTimeMs <= nowMs + tol &&
896
- (candidate == null || b.startTimeMs > (candidate.startTimeMs || 0))
897
- ) {
898
- candidate = b;
899
- }
900
- }
901
- return candidate;
902
- }
903
-
904
- private onTimeUpdate(currentTimeSec: number): void {
905
- if (!this.adSchedule || this.ima.isAdPlaying()) return;
906
- const nowMs = currentTimeSec * 1000;
907
- const breakToPlay = this.findBreakForTime(nowMs);
908
- if (breakToPlay) {
909
- this.handleMidAdJoin(breakToPlay, nowMs);
910
- }
911
- }
912
-
913
- private async handleMidAdJoin(
914
- adBreak: AdBreak,
915
- nowMs: number
916
- ): Promise<void> {
917
- const durationMs = adBreak.durationMs ?? 0;
918
- const endMs = adBreak.startTimeMs + durationMs;
919
- if (durationMs > 0 && nowMs > adBreak.startTimeMs && nowMs < endMs) {
920
- const remainingMs = endMs - nowMs;
921
- const policy = this.adSchedule?.lateJoinPolicy ?? "play_remaining";
922
- if (policy === "skip_to_content") return;
923
- const tags =
924
- this.selectVastTagsForBreak(adBreak) ||
925
- (this.apiVastTagUrl ? [this.apiVastTagUrl] : undefined);
926
- if (policy === "play_remaining" && tags && tags.length > 0) {
927
- const first = tags[0] as string;
928
- const rest = tags.slice(1);
929
- this.adPodQueue = rest;
930
- await this.playSingleAd(first);
931
- this.inAdBreak = true;
932
- this.expectedAdBreakDurationMs = remainingMs;
933
- this.currentAdBreakStartWallClockMs = Date.now();
934
- this.scheduleAdStopCountdown(remainingMs);
935
- }
936
- }
937
- }
938
-
939
- private scheduleAdStopCountdown(remainingMs: number): void {
940
- this.clearAdStopTimer();
941
- const ms = Math.max(0, Math.floor(remainingMs));
942
- if (ms === 0) {
943
- this.ensureAdStoppedByTimer();
944
- return;
945
- }
946
- this.adStopTimerId = window.setTimeout(() => {
947
- this.ensureAdStoppedByTimer();
948
- }, ms) as unknown as number;
949
- }
950
-
951
- private clearAdStopTimer(): void {
952
- if (this.adStopTimerId != null) {
953
- clearTimeout(this.adStopTimerId);
954
- this.adStopTimerId = undefined;
955
- }
956
- }
957
-
958
- private ensureAdStoppedByTimer(): void {
959
- if (!this.inAdBreak) return;
960
- this.inAdBreak = false;
961
- this.expectedAdBreakDurationMs = undefined;
962
- this.currentAdBreakStartWallClockMs = undefined;
963
- this.adStopTimerId = undefined;
964
- if (this.ima.isAdPlaying()) {
965
- this.ima.stop().catch(() => {});
966
- }
967
- }
968
-
969
- private scheduleAdStartIn(delayMs: number): void {
970
- this.clearAdStartTimer();
971
- const ms = Math.max(0, Math.floor(delayMs));
972
- if (ms === 0) {
973
- this.handleAdStart({ type: "start" } as Scte35Marker).catch(() => {});
974
- return;
975
- }
976
- this.adStartTimerId = window.setTimeout(() => {
977
- this.handleAdStart({ type: "start" } as Scte35Marker).catch(() => {});
978
- }, ms) as unknown as number;
979
- }
980
-
981
- private clearAdStartTimer(): void {
982
- if (this.adStartTimerId != null) {
983
- clearTimeout(this.adStartTimerId);
984
- this.adStartTimerId = undefined;
985
- }
986
- }
987
-
988
- private updatePtsDrift(ptsSecondsSample: number): void {
989
- const sampleMs = (this.video.currentTime - ptsSecondsSample) * 1000;
990
- if (!Number.isFinite(sampleMs) || Math.abs(sampleMs) > 60000) return;
991
- const alpha = 0.1;
992
- this.ptsDriftEmaMs = this.ptsDriftEmaMs * (1 - alpha) + sampleMs * alpha;
993
- }
994
-
995
- private async playSingleAd(vastTagUrl: string): Promise<void> {
996
- if (this.config.debugAdTiming) {
997
- console.log("[StormcloudVideoPlayer] Attempting to play ad:", vastTagUrl);
998
- }
999
-
1000
- this.startAdFailsafeTimer();
1001
-
1002
- try {
1003
- await this.ima.requestAds(vastTagUrl);
1004
- await this.ima.play();
1005
-
1006
- if (this.config.debugAdTiming) {
1007
- console.log("[StormcloudVideoPlayer] Ad playback started successfully");
1008
- }
1009
- } catch (error) {
1010
- if (this.config.debugAdTiming) {
1011
- console.error("[StormcloudVideoPlayer] Ad playback failed:", error);
1012
- }
1013
-
1014
- this.handleAdFailure();
1015
- }
1016
- }
1017
-
1018
- private handleAdFailure(): void {
1019
- if (this.config.debugAdTiming) {
1020
- console.log(
1021
- "[StormcloudVideoPlayer] Handling ad failure - resuming content"
1022
- );
1023
- }
1024
-
1025
- this.inAdBreak = false;
1026
- this.expectedAdBreakDurationMs = undefined;
1027
- this.currentAdBreakStartWallClockMs = undefined;
1028
- this.clearAdStartTimer();
1029
- this.clearAdStopTimer();
1030
- this.clearAdFailsafeTimer();
1031
- this.adPodQueue = [];
1032
- this.showAds = false;
1033
- this.currentAdIndex = 0;
1034
- this.totalAdsInBreak = 0;
1035
-
1036
- if (this.video.paused) {
1037
- this.video.play().catch(() => {
1038
- if (this.config.debugAdTiming) {
1039
- console.error(
1040
- "[StormcloudVideoPlayer] Failed to resume video after ad failure"
1041
- );
1042
- }
1043
- });
1044
- }
1045
- }
1046
-
1047
- private startAdFailsafeTimer(): void {
1048
- this.clearAdFailsafeTimer();
1049
-
1050
- const failsafeMs = this.config.adFailsafeTimeoutMs ?? 10000;
1051
-
1052
- if (this.config.debugAdTiming) {
1053
- console.log(
1054
- `[StormcloudVideoPlayer] Starting failsafe timer (${failsafeMs}ms)`
1055
- );
1056
- }
1057
-
1058
- this.adFailsafeTimerId = window.setTimeout(() => {
1059
- if (this.video.paused) {
1060
- if (this.config.debugAdTiming) {
1061
- console.warn(
1062
- "[StormcloudVideoPlayer] Failsafe timer triggered - forcing video resume"
1063
- );
1064
- }
1065
- this.handleAdFailure();
1066
- }
1067
- }, failsafeMs) as unknown as number;
1068
- }
1069
-
1070
- private clearAdFailsafeTimer(): void {
1071
- if (this.adFailsafeTimerId != null) {
1072
- clearTimeout(this.adFailsafeTimerId);
1073
- this.adFailsafeTimerId = undefined;
1074
- }
1075
- }
1076
-
1077
- private selectVastTagsForBreak(b?: AdBreak): string[] | undefined {
1078
- if (!b || !b.vastTagUrl) return undefined;
1079
- if (b.vastTagUrl.includes(",")) {
1080
- return b.vastTagUrl
1081
- .split(",")
1082
- .map((s) => s.trim())
1083
- .filter((s) => s.length > 0);
1084
- }
1085
- return [b.vastTagUrl];
1086
- }
1087
-
1088
- private getRemainingAdMs(): number {
1089
- if (
1090
- this.expectedAdBreakDurationMs == null ||
1091
- this.currentAdBreakStartWallClockMs == null
1092
- )
1093
- return 0;
1094
- const elapsed = Date.now() - this.currentAdBreakStartWallClockMs;
1095
- return Math.max(0, this.expectedAdBreakDurationMs - elapsed);
1096
- }
1097
-
1098
- private findBreakForTime(nowMs: number): AdBreak | undefined {
1099
- const schedule = this.adSchedule?.breaks || [];
1100
- for (const b of schedule) {
1101
- const end = (b.startTimeMs || 0) + (b.durationMs || 0);
1102
- if (
1103
- nowMs >= (b.startTimeMs || 0) &&
1104
- (b.durationMs ? nowMs < end : true)
1105
- ) {
1106
- return b;
1107
- }
1108
- }
1109
- return undefined;
1110
- }
1111
-
1112
- destroy(): void {
1113
- this.clearAdStartTimer();
1114
- this.clearAdStopTimer();
1115
- this.clearAdFailsafeTimer();
1116
- if (this.heartbeatInterval) {
1117
- clearInterval(this.heartbeatInterval);
1118
- this.heartbeatInterval = undefined;
1119
- }
1120
- this.hls?.destroy();
1121
- this.ima?.destroy();
1122
- }
1123
- }