@brandbrigade/ott-bb-player 1.0.45 → 1.0.48

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/src/Augmentor.js DELETED
@@ -1,1288 +0,0 @@
1
- import Composer3DForMainThread from "./Composer3DForMainThread";
2
- import Composer3dWorker from "./Composer3DWorker";
3
- import { RenderingMode } from "./enums/RenderingMode";
4
- import Ads from "./Ads";
5
- import {
6
- Platform,
7
- printTimecode,
8
- frames2Timecode,
9
- timecode2Frames,
10
- timeDate2TC,
11
- timeDate2TCString,
12
- } from "./utils";
13
- import MetaDataFetcher from "./MetaDataFetcher";
14
-
15
- export default class Augmentor {
16
- metaDataFetcher = null;
17
- composer = null;
18
- ctxGl = null;
19
- firstFrameTimecode = [0, 0, 0, 0, 0];
20
-
21
- constructor(video, options) {
22
- this.options = options;
23
- this.video = video;
24
- this.onFrameVideoChromeMac = this.onFrameVideoChromeMac.bind(this);
25
- this.onFrameVideoChromeWin = this.onFrameVideoChromeWin.bind(this);
26
- this.onAnimatedFrameSafari = this.onAnimatedFrameSafari.bind(this);
27
- this.onAnimatedFrameChromeWin = this.onAnimatedFrameChromeWin.bind(this);
28
- this.onAnimatedFrameChromeMac = this.onAnimatedFrameChromeMac.bind(this);
29
- this.calculateBlackBars = this.calculateBlackBars.bind(this);
30
-
31
- if (options.renderingMode == RenderingMode.Augment) {
32
- if (window.BB.RENDER_IN_WORKER) {
33
- this.canvas = document.createElement("canvas");
34
- } else {
35
- let canvas = document.createElement("canvas");
36
- canvas.width = 500;
37
- canvas.height = 500;
38
- document.body.appendChild(canvas);
39
- this.ctxGl = canvas.getContext("webgl");
40
- }
41
- }
42
-
43
- this.lastVideoFrameRenderedInGL = -1;
44
- this.lastCorrection = false;
45
- this.sourceFPS = 60000 / 1001; //hardcoded as all calculations with timecodes goes in 59.94 fps
46
-
47
- if (Platform.isSafariMac()) {
48
- if (options.debug) {
49
- document.getElementById("numberInput").value=2;
50
- }
51
- window.BB.RENDER_IN_WORKER = false;
52
- window.requestAnimationFrame(this.onAnimatedFrameSafari);
53
- } else if (Platform.isChromeMac()) {
54
- if (options.debug) {
55
- document.getElementById("numberInput").value=1;
56
- }
57
- window.BB.RENDER_IN_WORKER = true;
58
- window.requestAnimationFrame(this.onAnimatedFrameChromeMac);
59
-
60
- if (video.readyState >= 1) {
61
- video.requestVideoFrameCallback(this.onFrameVideoChromeMac);
62
- //calculateBlackBars(video);
63
- } else {
64
- video.addEventListener("loadedmetadata", () => {
65
- video.requestVideoFrameCallback(this.onFrameVideoChromeMac);
66
- //calculateBlackBars(video);
67
- });
68
- }
69
- } else if (false && Platform.isChromeWin()) {
70
- // not supported yet
71
- window.BB.RENDER_IN_WORKER = true;
72
- window.requestAnimationFrame(this.onAnimatedFrameChromeWin);
73
- if (video.readyState >= 1) {
74
- console.log(
75
- "video element is already loaded. Setting up requestVideoFrameCallback",
76
- );
77
- video.requestVideoFrameCallback(this.onFrameVideoChromeWin);
78
- //calculateBlackBars(video);
79
- } else {
80
- console.log(
81
- "video element is not yet loaded. Setting up loadedmetadata event to wait until it's loaded. video.readyState=" +
82
- video.readyState,
83
- );
84
- video.addEventListener("loadedmetadata", () => {
85
- console.log(
86
- 'now video element is loaded in "loadedmetadata" event. Setting up requestVideoFrameCallback',
87
- );
88
- video.requestVideoFrameCallback(this.onFrameVideoChromeWin);
89
- //calculateBlackBars(video);
90
- });
91
- }
92
- } else {
93
- console.BBerror("Platform not supported yet");
94
- }
95
-
96
- window.addEventListener("resize", () => {
97
- this.calculateBlackBars(video);
98
- });
99
- }
100
-
101
- // MK OnMetaData Sync Begin
102
- processTimedMetadata(messageData, presentationTime) {
103
- // Timed metadata
104
- let finalTimeCodeData = "";
105
- if (messageData && messageData.includes("www.mediakind.com/NTC")) {
106
- const metadata = messageData.replace(/.*NTC./, "");
107
- let pos = 0;
108
- while (pos + 8 <= metadata.length) {
109
- const type = metadata.substring(pos, pos + 4);
110
- const length =
111
- ((metadata.charCodeAt(pos + 4) * 256 + metadata.charCodeAt(pos + 5)) *
112
- 256 +
113
- metadata.charCodeAt(pos + 6)) *
114
- 256 +
115
- metadata.charCodeAt(pos + 7);
116
- if (pos + 8 + length > metadata.length) {
117
- break;
118
- }
119
- const value = metadata.substring(pos + 8, pos + 8 + length);
120
- if (type == "utc_") {
121
- //previousTimeCode = currentTimeCode;
122
- const timecodeDateTime = new Date(parseInt(value)).getTime() / 1000;
123
- const timecodeDelta = timecodeDateTime - presentationTime;
124
- //finalTimeCodeData += `${new Date(parseInt(value)).toISOString()} | timestamp: ${new Date(parseInt(value)).getTime()/1000} | delta: ${timecodeDelta}`;
125
- }
126
- if (type == "tc__") {
127
- //previousTimeCode = currentTimeCode;
128
- //tcDateTime = new Date(parseInt(value)).getTime()/1000;
129
- //tcDelta = tcDateTime - presentationTime;
130
- this.tcTimeStamp = new Date(parseInt(value)).getTime();
131
- //finalTimeCodeData += `${new Date(parseInt(value)).toISOString()} | tcTimestamp: ${tcTimeStamp} | segmentPlaybackDateTime: ${hlsDateTime}`;
132
- }
133
- if (type === "offs") {
134
- this.timecode_offset = parseInt(value) / 90;
135
- if (this.options.debug)
136
- console.log("We are HERE:TimeCode", this.timecode_offset);
137
- }
138
- pos += 8 + length;
139
- }
140
- }
141
- return finalTimeCodeData;
142
- }
143
-
144
- onNewMetadata(data) {
145
- if (
146
- (data && data.metadata && data.metadata.id == 0) ||
147
- Platform.isSafariMac()
148
- ) {
149
- //mac check is temp. Need to get id from safari CallBack
150
- if (data.metadata.messageData) {
151
- // Typically seen on Chrome, Edge browsers
152
- data.metadata.messageData = this.processTimedMetadata(
153
- data.metadata.messageData,
154
- data.metadata.presentationTime,
155
- );
156
- } else if (
157
- data.metadata.frames &&
158
- data.metadata.frames.length &&
159
- data.metadata.frames[0].key &&
160
- data.metadata.frames[0].key.toLowerCase() === "priv"
161
- ) {
162
- // Typically seen on Safari browser
163
- const NTC_DATA = data.metadata.frames.filter((el) => {
164
- return el.info && el.info.toLowerCase().includes("ntc");
165
- });
166
- if (NTC_DATA.length) {
167
- const messageData = `${NTC_DATA[NTC_DATA.length - 1].info}\u0000${String.fromCharCode(...NTC_DATA[NTC_DATA.length - 1].data)}`;
168
- data.metadata.messageData = this.processTimedMetadata(
169
- messageData,
170
- data.metadata.presentationTime,
171
- );
172
- }
173
- }
174
- if (
175
- data.metadata.id == 0 ||
176
- (Platform.isSafariMac() && this.timecode_offset == 0)
177
- ) {
178
- //mac check is temp. Need to get id from safari CallBack
179
- const presentationTime = data.metadata.presentationTime * 1000;
180
-
181
- const offsetTimeCode = new Date(
182
- this.tcTimeStamp - this.timecode_offset,
183
- );
184
- //offsetTimeCode = new Date(1721866388183);
185
- let offsetTimeCodeframeNumber =
186
- Math.round((offsetTimeCode.getUTCMilliseconds() * 30 * 2) / 1001) / 2;
187
- const decimalPartOff = offsetTimeCodeframeNumber % 1;
188
- let partoff = 1;
189
- if (decimalPartOff == 0.5) {
190
- offsetTimeCodeframeNumber = Math.floor(offsetTimeCodeframeNumber);
191
- partoff = 2;
192
- }
193
-
194
- this.firstFrameTimecode = [
195
- offsetTimeCode.getUTCHours(),
196
- offsetTimeCode.getUTCMinutes(),
197
- offsetTimeCode.getUTCSeconds(),
198
- Math.floor(offsetTimeCodeframeNumber),
199
- partoff == 1 ? 0 : 1,
200
- ];
201
-
202
- // let syncPoint = {frame:0,playerTime:0}
203
- // syncPoint.frame = timecode2Frames(firstFrameTimecode,60000/1001,true);//@alal
204
- // syncPoint.stringTime = printTimecode(firstFrameTimecode,true,true);// timeDate2TCString(eventObj.dateTime, 30000/1001);
205
- // console.log("New syncPoint by metaData : " + syncPoint.stringTime + " (" + syncPoint.frame + ")" + " playing at " + video.currentTime);
206
- // syncPoint.playerTime = video.currentTime;
207
- // options.syncPoint = syncPoint;
208
-
209
- //timeCodeOff = `${offsetTimeCode.getUTCHours()}:${offsetTimeCode.getUTCMinutes()}:${offsetTimeCode.getUTCSeconds()}:${parseInt(offsetTimeCodeframeNumber)}.${partoff}`;
210
-
211
- //console.log("timeCodeOff=", timeCodeOff);
212
- //data.metadata.messageData = ``;
213
- //data.metadata.messageData = `TimeCode: ${timeCodeOff} | offsetTimeCodeframeNumber: ${offsetTimeCodeframeNumber} | offsetTimeCodeMilliseconds: ${offsetTimeCode.getUTCMilliseconds()}`;
214
- //dataForCSV += `${timeCodeOff},`
215
- //timedMetadata.textContent = JSON.stringify(data.metadata, null, 4);
216
- //timedMetadata.textContent+=`{"counter" : ${counter++}}`;
217
- }
218
- }
219
- }
220
-
221
- onNewSegmentForMetadata(eventObj) {
222
- if (typeof globalConfig !== "undefined") {
223
- if (typeof globalConfig.MKsync !== "undefined")
224
- if (globalConfig.MKsync == "no") {
225
- return; //Temp for testing without MK
226
- }
227
- }
228
-
229
- let syncPoint = { frame: 0, playerTime: 0 };
230
- syncPoint.frame = timecode2Frames(
231
- this.firstFrameTimecode,
232
- 60000 / 1001,
233
- true,
234
- ); //@alal
235
- syncPoint.stringTime = printTimecode(this.firstFrameTimecode, true, true); // timeDate2TCString(eventObj.dateTime, 30000/1001);
236
- console.log(
237
- "New segment (" +
238
- eventObj.url +
239
- "): " +
240
- syncPoint.stringTime +
241
- " (" +
242
- syncPoint.frame +
243
- ")" +
244
- " playing at " +
245
- eventObj.playbackTime,
246
- );
247
- syncPoint.playerTime = eventObj.playbackTime;
248
- this.options.syncPoint = syncPoint;
249
-
250
- if (eventObj.mimeType.includes("video")) {
251
- console.log("url: " + eventObj.url);
252
- }
253
- }
254
- // MK OnMetaData Sync End
255
- onNewSegment(eventObj) {
256
- switch (
257
- eventObj.eventType //just for check
258
- ) {
259
- case wmcEvents.AMC_EVENT_PLAYER_SEGMENT_PLAYBACK:
260
- let syncPoint = { frame: 0, playerTime: 0 };
261
-
262
- console.log(
263
- "New segment (" +
264
- eventObj.url +
265
- "): " +
266
- timeDate2TCString(eventObj.dateTime, 60000 / 1001) +
267
- " playing at " +
268
- eventObj.playbackTime,
269
- );
270
- syncPoint.frame = timecode2Frames(
271
- timeDate2TC(eventObj.dateTime),
272
- fps,
273
- true,
274
- ); //@alal
275
- syncPoint.stringTime = timeDate2TCString(
276
- eventObj.dateTime,
277
- 60000 / 1001,
278
- );
279
-
280
- syncPoint.playerTime = eventObj.playbackTime;
281
- this.options.syncPoint = syncPoint;
282
- //bbPlayer.SetSyncPoint(syncPoint);
283
- break;
284
- default:
285
- }
286
- }
287
-
288
- render(currentFrame, correction) {
289
- let doNotRenderAugmentation = false;
290
- const options = this.options;
291
- if (options.debug) {
292
- var checkbox_doNotRenderAugmentation = document.getElementById(
293
- "doNotRenderAugmentationCheckbox",
294
- );
295
- doNotRenderAugmentation = checkbox_doNotRenderAugmentation.checked;
296
- }
297
- if (doNotRenderAugmentation) {
298
- return 0;
299
- }
300
- const startTime = performance.now();
301
- if (
302
- this.lastVideoFrameRenderedInGL == currentFrame &&
303
- correction == this.lastCorrection
304
- ) {
305
- if (options.debug)
306
- console.log(
307
- "render(): lastVideoFrameRenderedInGL==currentFrame: " + currentFrame,
308
- );
309
- return 0;
310
- }
311
- if (!options.enabled || options.stopAugment) {
312
- this.composer.cleanGL();
313
- return 0;
314
- }
315
-
316
- if (options.stopAugmentFor > 0) {
317
- this.composer?.cleanGL();
318
- options.stopAugmentFor -= 1.0 / options.hls_fps;
319
- return 0;
320
- }
321
-
322
- if (options.renderingMode == RenderingMode.RenderDigits)
323
- renderDigit(this.ctxGl, currentFrame);
324
- else if (options.renderingMode == RenderingMode.RenderTimecode) {
325
- if (this.sourceFPS == 60000 / 1001 || this.sourceFPS == 60)
326
- renderTimecode(
327
- this.ctxGl,
328
- frames2Timecode(currentFrame, sourceFPS, true),
329
- true,
330
- );
331
- else if (this.sourceFPS == 30000 / 1001 || this.sourceFPS == 30)
332
- renderTimecode(
333
- this.ctxGl,
334
- frames2Timecode(currentFrame, this.sourceFPS, false),
335
- false,
336
- );
337
- } else if (options.renderingMode == RenderingMode.Augment && this.composer)
338
- if (this.sourceFPS == 60000 / 1001 || sourceFPS == 60)
339
- this.composer.renderAugmentation(
340
- printTimecode(frames2Timecode(currentFrame, this.sourceFPS, true)),
341
- );
342
- else if (this.sourceFPS == 30000 / 1001 || this.sourceFPS == 30) {
343
- if (correction) {
344
- this.composer.renderAugmentation(
345
- printTimecode(
346
- frames2Timecode(currentFrame * 2 + 1, this.sourceFPS * 2, true),
347
- true,
348
- ),
349
- ); //@alal
350
- } else {
351
- this.composer.renderAugmentation(
352
- printTimecode(
353
- frames2Timecode(currentFrame, this.sourceFPS, false),
354
- false,
355
- ),
356
- ); //@alal
357
- }
358
- }
359
- this.lastVideoFrameRenderedInGL = currentFrame;
360
- this.lastCorrection = correction;
361
- const endTime = performance.now();
362
- return endTime - startTime;
363
- }
364
-
365
- countVid = 0;
366
- lastVidDateTime = 0;
367
- lastCalled = "";
368
- frameNumberToRender = 0;
369
- lastOffset = 0;
370
- lastOffsetShown;
371
- countOfCorrectionsInRow = 0; // this is used for correction in 29.97
372
- deltaVidDeltaArray = [];
373
- deltaVidDeltaArrayCapacity = 20;
374
-
375
- addNumberToVidDeltaArray(num) {
376
- this.deltaVidDeltaArray.push(num);
377
-
378
- // Keep the array size at deltaVidDeltaArrayCapacity
379
- if (this.deltaVidDeltaArray.length > this.deltaVidDeltaArrayCapacity) {
380
- this.deltaVidDeltaArray.shift(); // Remove the oldest number
381
- }
382
-
383
- // Calculate the preliminary average
384
- const sum = this.deltaVidDeltaArray.reduce((acc, curr) => acc + curr, 0);
385
- const preliminaryAverage = sum / this.deltaVidDeltaArray.length;
386
-
387
- // Filter values within 20% range of preliminary average
388
- const filteredArray = this.deltaVidDeltaArray.filter(
389
- n => Math.abs(n - preliminaryAverage) <= preliminaryAverage * 0.2
390
- );
391
-
392
- // Calculate the average of filtered values
393
- const filteredSum = filteredArray.reduce((acc, curr) => acc + curr, 0);
394
- const average = filteredSum / filteredArray.length;
395
-
396
- return average;
397
- }
398
-
399
- onFrameVideoChromeMac(now, metadata) {
400
- const options = this.options;
401
- this.lastCalled = "VID";
402
-
403
- if (options.hls_fps != 60000 / 1001 && options.hls_fps != 30000 / 1001) {
404
- console.log("Unsupported FPS: options.hls_fps = " + options.hls_fps);
405
- this.video.requestVideoFrameCallback(this.onFrameVideoChromeMac);
406
- return;
407
- }
408
- var dblFullFrame =
409
- ((metadata.mediaTime - options.syncPoint.playerTime) % (60 * 60 * 24)) *
410
- (60000 / 1001); // take time excluding days
411
- var timeFromLastSync = metadata.mediaTime - options.syncPoint.playerTime;
412
- let lastFrameFromVideoCallback =
413
- Math.floor(dblFullFrame + 0.1) + options.syncPoint.frame;
414
- let deltaVid = Date.now() - this.lastVidDateTime;
415
- var videoFPS = 1000 / this.addNumberToVidDeltaArray(deltaVid);
416
- if (options.debug) {
417
- if ((videoFPS>25)&&(videoFPS<35)) {
418
- lastFrameFromVideoCallback+=1; // compensate 15 ms timer for 29 fps
419
- }
420
- var numberInput = document.getElementById("numberInput").value;
421
- var number = parseInt(numberInput);
422
- lastFrameFromVideoCallback += number;
423
- } else {
424
- if (videoFPS > 25 && videoFPS < 35) lastFrameFromVideoCallback += 2;
425
- else lastFrameFromVideoCallback += 1;
426
- }
427
-
428
- let DebugLine2 = document.getElementById("debugText2");
429
- var timeStr = "";
430
- timeStr = printTimecode(
431
- frames2Timecode(lastFrameFromVideoCallback, this.sourceFPS, true),
432
- true,
433
- true,
434
- );
435
-
436
- var offsetStr = "";
437
- if (options.augmentation.offset<0){
438
- offsetStr+="-";
439
- offsetStr += printTimecode(
440
- frames2Timecode((-1)*options.augmentation.offset, this.sourceFPS, true),
441
- true,
442
- true,
443
- );
444
- } else{
445
- offsetStr += printTimecode(
446
- frames2Timecode(options.augmentation.offset, this.sourceFPS, true),
447
- true,
448
- true,
449
- );
450
- }
451
-
452
- let resultFrames = lastFrameFromVideoCallback - options.augmentation.offset;
453
- if (resultFrames < 0) resultFrames += 5178816;
454
- if (resultFrames >= 5178816) resultFrames -= 5178816;// number of frames in 23:59:59:59 + 1 = 24h
455
-
456
- if (this.metaDataFetcher) this.metaDataFetcher.setLastFrame(resultFrames);
457
-
458
- var resultTimecodeStr = "";
459
- resultTimecodeStr = printTimecode(
460
- frames2Timecode(resultFrames, this.sourceFPS, true),
461
- true,
462
- true,
463
- );
464
-
465
- let nowTime = new Date();
466
- let hours = String(nowTime.getHours()).padStart(2, "0");
467
- let minutes = String(nowTime.getMinutes()).padStart(2, "0");
468
- let seconds = String(nowTime.getSeconds()).padStart(2, "0");
469
- let milliseconds = String(nowTime.getMilliseconds()).padStart(3, "0");
470
- let formattedTime = `${hours}:${minutes}:${seconds}.${milliseconds}`;
471
-
472
- if (options.debug) {
473
- var offsetColor = "black";
474
- if (
475
- options.augmentation.offset != this.lastOffset ||
476
- this.lastOffsetShown < 10 * 60
477
- ) {
478
- var offsetColor = "red";
479
- }
480
- DebugLine2.innerHTML =
481
- "VID. " +
482
- this.countVid +
483
- " Game: " +
484
- options.gameId +
485
- ", Time Fired: " +
486
- formattedTime +
487
- ", PlayerTime:" +
488
- metadata.mediaTime.toFixed(4) +
489
- ", dblFrame: " +
490
- dblFullFrame.toFixed(4) +
491
- ", Stream Time-Code: " +
492
- timeStr +
493
- ", offset: <span style='color:" +
494
- offsetColor +
495
- "'>" +
496
- options.augmentation.offset +
497
- "(" +
498
- offsetStr +
499
- ")</span>" +
500
- " </br>" +
501
- " </br>" +
502
- "<span style='font-size: 30px;'>, Brain Time-Code: " +
503
- resultTimecodeStr +
504
- "( " +
505
- resultFrames +
506
- " )</span>" +
507
- " </br>" +
508
- " </br>" +
509
- ", expectedDisplayTime: " +
510
- metadata.expectedDisplayTime.toFixed(4) +
511
- ", presentationTime: " +
512
- metadata.presentationTime.toFixed(4) +
513
- ", presentationTime-expectedDisplayTime: " +
514
- (metadata.presentationTime - metadata.expectedDisplayTime).toFixed(4) +
515
- ", expectedDisplayTime-now: " +
516
- (metadata.expectedDisplayTime - now).toFixed(4) +
517
- ", TimeFromLastSync:" +
518
- timeFromLastSync.toFixed(4) +
519
- ", LastSyncPointTime:" +
520
- options.syncPoint.playerTime.toFixed(4) +
521
- ", LastSyncPointFrame:" +
522
- options.syncPoint.frame +
523
- ", LastSyncPoint:" +
524
- options.syncPoint.stringTime +
525
- " </br>";
526
- if (options.augmentation.offset != this.lastOffset) {
527
- this.lastOffsetShown = 0;
528
- }
529
- this.lastOffset = options.augmentation.offset;
530
- this.lastOffsetShown++;
531
- }
532
-
533
- this.countVid++;
534
- this.lastVidDateTime = Date.now();
535
- if (videoFPS > 50 && videoFPS < 70) this.render(resultFrames, false);
536
- else if (videoFPS > 25 && videoFPS < 35) {
537
- //frameNumberToRender = resultFrames;
538
- const delayRender = (number) => {
539
- let DebugLine = document.getElementById("debugText");
540
- if (DebugLine != null) DebugLine.innerHTML = "Rendering now " + number;
541
- //try this approach to update any element of the page to force dom update //NEED TO MAKE ANOTHER HACK!!!!!!
542
- let codetmp = document.getElementById("backend-modules");
543
- if (codetmp != null) codetmp.innerHTML = "Rendering now " + number;
544
- this.render(number, false);
545
- }
546
-
547
- if (((metadata.expectedDisplayTime-now)>5)) {
548
- setTimeout(delayRender, 20, resultFrames);
549
- //setTimeout(delayRender, 5, resultFrames-1);
550
- } else {
551
- setTimeout(delayRender, 5, resultFrames);
552
- //render(resultFrames,false);
553
- }
554
-
555
- } else {
556
- if (this.countVid % 60 == 0)
557
- console.BBerror(
558
- "video FPS is " + videoFPS.toFixed(4) + ". is not supported yet.",
559
- );
560
- this.composer?.cleanGL();
561
- }
562
- this.video.requestVideoFrameCallback(this.onFrameVideoChromeMac);
563
- return;
564
- }
565
-
566
- onFrameVideoChromeWin(now, metadata) {
567
- if (options.hls_fps != 60000 / 1001 && options.hls_fps != 30000 / 1001) {
568
- //stopping augmentation fr 3 sec. Waiting until video fps will be good
569
- options.stopAugmentFor = 3;
570
- console.log("Unsupported FPS: options.hls_fps = " + options.hls_fps);
571
- video.requestVideoFrameCallback(onFrameVideoChromeWin);
572
- return;
573
- }
574
- let deltaVid = Date.now() - lastVidDateTime;
575
- lastVidDateTime = Date.now();
576
- // var videoFPS = 1000/addNumberToVidDeltaArray(deltaVid);
577
- // if ((videoFPS<50)||(videoFPS>70))
578
- // { //stopping augmentation fr 3 sec. Waiting until video fps will be good
579
- // options.stopAugmentFor = 3;
580
- // console.BBerror("Video fps is not supported: " + videoFPS.toFixed(4));
581
- // }
582
- const options = this.options;
583
-
584
- if (this.lastCalled == "ANI2" && options.hls_fps == 60000 / 1001) {
585
- if (options.debug)
586
- console.log(
587
- "This videoCallback can be incorrect. This time doing stuff in requestAnimation frame",
588
- );
589
- this.video.requestVideoFrameCallback(this.onFrameVideoChromeWin);
590
- return;
591
- }
592
-
593
- if (options.hls_fps == 30000 / 1001) {
594
- if (this.lastCalled == "ANI3" && this.countOfCorrectionsInRow <= 0) {
595
- //if requestAnimatedFrame was called 3 times between VideoCallbacks
596
- //then we do not believe videoCallbacks for 3 calls. Rendering on every
597
- //second requestAnimatedFrame. For this we are saying that current stage is
598
- //already "ANI" (like there was no videoCallback) but anyway we should
599
- //continue here to update timecode
600
- this.lastCalled = "ANI";
601
- this.countOfCorrectionsInRow++;
602
- } else {
603
- this.lastCalled = "VID";
604
- this.countOfCorrectionsInRow = 0;
605
- }
606
- } //if we are in 59.94
607
- else {
608
- this.lastCalled = "VID";
609
- }
610
-
611
- var dblFullFrame =
612
- ((metadata.mediaTime - options.syncPoint.playerTime) % (60 * 60 * 24)) *
613
- (60000 / 1001); // take time excluding days
614
- var timeFromLastSync = metadata.mediaTime - options.syncPoint.playerTime;
615
- let lastFrameFromVideoCallback =
616
- Math.floor(dblFullFrame + 0.1) + options.syncPoint.frame;
617
- if (options.debug) {
618
- var numberInput = document.getElementById("numberInput").value;
619
- var number = parseInt(numberInput);
620
- lastFrameFromVideoCallback += number;
621
- } else {
622
- lastFrameFromVideoCallback += 1;
623
- }
624
- lastMediaTime = metadata.mediaTime;
625
-
626
- let DebugLine2 = document.getElementById("debugText2");
627
- var timeStr = "";
628
- timeStr = printTimecode(
629
- frames2Timecode(lastFrameFromVideoCallback, this.sourceFPS, true),
630
- true,
631
- true,
632
- );
633
-
634
- var offsetStr = "";
635
- if (options.augmentation.offset<0){
636
- offsetStr+="-";
637
- offsetStr += printTimecode(
638
- frames2Timecode((-1)*options.augmentation.offset, this.sourceFPS, true),
639
- true,
640
- true,
641
- );
642
- } else{
643
- offsetStr += printTimecode(
644
- frames2Timecode(options.augmentation.offset, this.sourceFPS, true),
645
- true,
646
- true,
647
- );
648
- }
649
-
650
- let resultFrames = lastFrameFromVideoCallback - options.augmentation.offset;
651
- if (resultFrames < 0) resultFrames += 5178816;
652
- if (resultFrames >= 5178816) resultFrames -= 5178816;// number of frames in 23:59:59;59 + 1 = 24h
653
- if (this.metaDataFetcher) this.metaDataFetcher.setLastFrame(resultFrames);
654
- var resultTimecodeStr = "";
655
- resultTimecodeStr = printTimecode(
656
- frames2Timecode(resultFrames, this.sourceFPS, true),
657
- true,
658
- true,
659
- );
660
-
661
- let nowTime = new Date();
662
- let hours = String(nowTime.getHours()).padStart(2, "0");
663
- let minutes = String(nowTime.getMinutes()).padStart(2, "0");
664
- let seconds = String(nowTime.getSeconds()).padStart(2, "0");
665
- let milliseconds = String(nowTime.getMilliseconds()).padStart(3, "0");
666
- let formattedTime = `${hours}:${minutes}:${seconds}.${milliseconds}`;
667
-
668
- if (options.debug) {
669
- var offsetColor = "black";
670
- if (
671
- options.augmentation.offset != this.lastOffset ||
672
- this.lastOffsetShown < 10 * 60
673
- ) {
674
- var offsetColor = "red";
675
- }
676
- DebugLine2.innerHTML =
677
- "VID. " +
678
- this.countVid +
679
- " Game: " +
680
- options.gameId +
681
- ", Time Fired: " +
682
- formattedTime +
683
- ", PlayerTime:" +
684
- metadata.mediaTime.toFixed(4) +
685
- ", dblFrame: " +
686
- dblFullFrame.toFixed(4) +
687
- ", Stream Time-Code: " +
688
- timeStr +
689
- ", offset: <span style='color:" +
690
- offsetColor +
691
- "'>" +
692
- options.augmentation.offset +
693
- "(" +
694
- offsetStr +
695
- ")</span>" +
696
- " </br>" +
697
- " </br>" +
698
- "<span style='font-size: 30px;'>, Brain Time-Code: " +
699
- resultTimecodeStr +
700
- "( " +
701
- resultFrames +
702
- " )</span>" +
703
- " </br>" +
704
- " </br>" +
705
- ", expectedDisplayTime: " +
706
- metadata.expectedDisplayTime.toFixed(4) +
707
- ", presentationTime: " +
708
- metadata.presentationTime.toFixed(4) +
709
- ", presentationTime-expectedDisplayTime: " +
710
- (metadata.presentationTime - metadata.expectedDisplayTime).toFixed(4) +
711
- ", expectedDisplayTime-now: " +
712
- (metadata.expectedDisplayTime - now).toFixed(4) +
713
- ", TimeFromLastSync:" +
714
- timeFromLastSync.toFixed(4) +
715
- ", LastSyncPointTime:" +
716
- options.syncPoint.playerTime.toFixed(4) +
717
- ", LastSyncPointFrame:" +
718
- options.syncPoint.frame +
719
- ", LastSyncPoint:" +
720
- options.syncPoint.stringTime +
721
- " </br>";
722
- if (options.augmentation.offset != this.lastOffset) {
723
- this.lastOffsetShown = 0;
724
- }
725
- this.lastOffset = options.augmentation.offset;
726
- this.lastOffsetShown++;
727
- }
728
-
729
- this.countVid++;
730
- let plus05Correction = false;
731
- if (options.debug) {
732
- var checkbox_plus05Correction = document.getElementById(
733
- "plus05CorrectionCheckbox",
734
- );
735
- plus05Correction = checkbox_plus05Correction.checked;
736
- }
737
-
738
- if (options.hls_fps == 30000 / 1001) {
739
- //in 29.94 we remember the current timecode and rendering itself
740
- // will be done in requestAnimatedFrame
741
- this.renderOnNextFrame = resultFrames;
742
- } //if we are in 59.94
743
- else {
744
- //disable check for debugging alal
745
- //if ((videoFPS>50)&&(videoFPS<70))
746
- renderingDuration = this.render(resultFrames, false);
747
- //else
748
- // {
749
- // if (countVid%60==0)
750
- // console.BBerror("video FPS is " + videoFPS.toFixed(4) + ". is not supported yet.");
751
- // composer.cleanGL();
752
- // }
753
- }
754
-
755
- // if (options.debug)
756
- // {
757
- // console.log("VID."
758
- // + " TimeFired: " + formattedTime
759
- // + ", rendering: " + timeStr
760
- // + ", took: " + renderingDuration.toFixed(4) + " ms"
761
- // );
762
- // }
763
- this.video.requestVideoFrameCallback(this.onFrameVideoChromeWin);
764
- return;
765
- }
766
-
767
- countANI = 0;
768
- aniDelltaTimeArray = [];
769
- aniDelltaTimeArrayCapacity = 60;
770
- lastAniNow = 0;
771
-
772
- addNumberToAniDeltaArray(num) {
773
- // Add the new number to the array
774
- this.aniDelltaTimeArray.push(num);
775
-
776
- // Keep the array size at aniDelltaTimeArrayCapacity
777
- if (this.aniDelltaTimeArray.length > this.aniDelltaTimeArrayCapacity) {
778
- this.aniDelltaTimeArray.shift(); // Remove the oldest number
779
- }
780
-
781
- // Calculate the preliminary average
782
- const sum = this.aniDelltaTimeArray.reduce((acc, curr) => acc + curr, 0);
783
- const preliminaryAverage = sum / this.aniDelltaTimeArray.length;
784
-
785
- // Filter values within 20% range of preliminary average
786
- const filteredArray = this.aniDelltaTimeArray.filter(
787
- (n) => Math.abs(n - preliminaryAverage) <= preliminaryAverage * 0.2,
788
- );
789
-
790
- if (filteredArray.length < this.aniDelltaTimeArrayCapacity*85/100)
791
- {
792
- return -1;
793
- }
794
-
795
- // Calculate the average of filtered values
796
- const filteredSum = filteredArray.reduce((acc, curr) => acc + curr, 0);
797
- const average = filteredSum / filteredArray.length;
798
-
799
- return average;
800
- }
801
-
802
- onAnimatedFrameChromeWin(timeStamp) {
803
- this.countANI++;
804
- let nowTime = performance.now(); //new Date();
805
- var delta = nowTime - this.lastAniNow;
806
- this.lastAniNow = nowTime;
807
- const options = this.options;
808
- var screenFPS = 1000 / this.addNumberToAniDeltaArray(delta);
809
- if (screenFPS == -1000){
810
- options.stopAugmentFor = 3;
811
- console.BBerror("Experience difficulties with overloading: (not consistant Calls of requestAnimatedFrame). Stopping augmentation."); //for debugging printing it every frame
812
- console.log(aniDelltaTimeArray);
813
- }
814
- else if (Math.abs(screenFPS - 60.0) > 5) {
815
- options.stopAugmentFor = 3;
816
- console.BBerror("Unsupported FPS: " + screenFPS); //for debugging printing it every frame
817
- // if (countANI%30==0)
818
- // {
819
- // console.BBerror("Unsupported FPS: " + screenFPS);
820
- // }
821
- }
822
- // else
823
- // {
824
- // options.stopAugment = false;
825
- // }
826
-
827
- if (!options.debug) {
828
- let text = document.getElementById("forceUpdate");
829
- if (text != null) text.innerHTML = this.countANI;
830
- window.requestAnimationFrame(this.onAnimatedFrameChromeWin);
831
- return;
832
- }
833
-
834
- let hours = String(nowTime.getHours()).padStart(2, "0");
835
- let minutes = String(nowTime.getMinutes()).padStart(2, "0");
836
- let seconds = String(nowTime.getSeconds()).padStart(2, "0");
837
- let milliseconds = String(nowTime.getMilliseconds()).padStart(3, "0");
838
- let formattedTime = `${hours}:${minutes}:${seconds}.${milliseconds}`;
839
- if (this.video.paused) {
840
- //doing nothing
841
- window.requestAnimationFrame(this.onAnimatedFrameChromeWin);
842
- return;
843
- }
844
- let DebugLine = document.getElementById("debugText");
845
- if (options.debug)
846
- DebugLine.innerHTML =
847
- "ANI. " + this.countANI + " Time Fired: " + formattedTime;
848
-
849
- if (options.hls_fps == 30000 / 1001) {
850
- DebugLine.innerHTML += "VideoTime: " + this.video.currentTime.toFixed(4);
851
- if (this.lastCalled == "VID") {
852
- DebugLine.innerHTML += " rendering on the current frame ";
853
- this.render(this.renderOnNextFrame, false);
854
- this.lastCalled = "ANI";
855
- } else if (this.lastCalled == "ANI") {
856
- //DebugLine.innerHTML += " rendering on the next frame ";
857
- //render(renderOnNextFrame,false);
858
- this.lastCalled = "ANI2";
859
- } else if (this.lastCalled == "ANI2") {
860
- DebugLine.innerHTML += " extra call. Correcting ";
861
- this.render(this.renderOnNextFrame + 2, false);
862
- lastCalled = "ANI3";
863
- }
864
- DebugLine.innerHTML += "stage:" + this.lastCalled;
865
- // console.log("ANI."
866
- // + " Time Fired: " + formattedTime
867
- // + " stage: " + lastCalled);
868
- window.requestAnimationFrame(this.onAnimatedFrameChromeWin);
869
- return;
870
- } else if (options.hls_fps == 60000 / 1001) {
871
- //if we are in 59.94
872
- if (this.lastCalled == "ANI" || this.lastCalled == "ANI2") {
873
- var timeStr = "";
874
- timeStr = printTimecode(
875
- frames2Timecode(
876
- this.lastVideoFrameRenderedInGL + 1,
877
- this.sourceFPS,
878
- true,
879
- ),
880
- true,
881
- true,
882
- );
883
-
884
- var offsetStr = "";
885
- if (options.augmentation.offset<0){
886
- offsetStr+="-";
887
- offsetStr += printTimecode(
888
- frames2Timecode((-1)*options.augmentation.offset, this.sourceFPS, true),
889
- true,
890
- true,
891
- );
892
- } else{
893
- offsetStr += printTimecode(
894
- frames2Timecode(options.augmentation.offset, this.sourceFPS, true),
895
- true,
896
- true,
897
- );
898
- }
899
-
900
- let resultFrames = this.lastVideoFrameRenderedInGL - options.augmentation.offset;
901
- if (resultFrames < 0) resultFrames += 5178816;// number of frames in 23:59:59;59 + 1 = 24h
902
- if (resultFrames >= 5178816) resultFrames -= 5178816;// number of frames in 23:59:59;59 + 1 = 24h
903
- var resultTimecodeStr = "";
904
- resultTimecodeStr = printTimecode(
905
- frames2Timecode(resultFrames, this.sourceFPS, true),
906
- true,
907
- true,
908
- );
909
-
910
- if (options.debug) {
911
- console.log(
912
- "Probable videoFrameCallback was not fired. Apllying correction mechanism (" +
913
- timeStr +
914
- ")" +
915
- ", stage: " +
916
- lastCalled,
917
- );
918
- DebugLine.innerHTML += " correction";
919
- }
920
-
921
- let renderingDuration = this.render(
922
- this.lastVideoFrameRenderedInGL + 1,
923
- false,
924
- );
925
- // if (options.debug) console.log("ANI."
926
- // + " Time Fired: " + formattedTime
927
- // + ", correction"
928
- // + ", time: " + timeStr
929
- // + ", offset: " + options.augmentation.offset + "(" + offsetStr + ")"
930
- // + ", resultTime: " + resultTimecodeStr
931
- // + ", took: " + renderingDuration.toFixed(4) + " ms");
932
- if (this.lastCalled == "ANI") this.lastCalled = "ANI2";
933
- else if (this.lastCalled == "ANI2") this.lastCalled = "ANI";
934
- } else {
935
- //DebugLine.innerHTML = "ANI. ";
936
- // if (options.debug) console.log("ANI."
937
- // + " Time Fired: " + formattedTime);
938
- this.lastCalled = "ANI";
939
- }
940
- } // unsupported fps
941
- else {
942
- console.log("Unsupported FPS: options.hls_fps = " + options.hls_fps);
943
- }
944
-
945
- window.requestAnimationFrame(this.onAnimatedFrameChromeWin);
946
- }
947
-
948
- onAnimatedFrameChromeMac(timeStamp) {
949
- this.countAppleANI++;
950
- let DebugLine;
951
- let resultFramesStr = printTimecode(
952
- frames2Timecode(this.frameNumberToRender, this.sourceFPS, true),
953
- true,
954
- true,
955
- );
956
- if (this.options.debug) {
957
- DebugLine = document.getElementById("debugText");
958
- DebugLine.innerHTML = this.countAppleANI;
959
- }
960
- let codetmp = document.getElementById("backend-modules");
961
- if (codetmp != null) codetmp.innerHTML = "Rendering now " + this.countAppleANI;
962
-
963
- //window.requestAnimationFrame(this.onAnimatedFrameChromeMac);
964
- //return;
965
- if (this.lastCalled == "VID") {
966
- this.lastCalled = "ANI";
967
- DebugLine.innerHTML =
968
- "ANI: " +
969
- this.countAppleANI +
970
- " Stage: " +
971
- this.lastCalled +
972
- //" _TimeCode: " +
973
- //resultFramesStr +
974
- " rendering now";
975
- //this.render(frameNumberToRender, true);
976
- } else if (this.lastCalled == "ANI") {
977
- this.lastCalled = "ANI2";
978
- DebugLine.innerHTML =
979
- "ANI: " +
980
- this.countAppleANI +
981
- " Stage: " +
982
- //this.lastCalled +
983
- //" TimeCode: " +
984
- resultFramesStr +
985
- " correction";;
986
- this.render(this.lastVideoFrameRenderedInGL+1, true);
987
- } else if (this.lastCalled == "ANI2") {
988
- //cannot go into here in normal
989
- this.lastCalled = "ANI3";
990
- DebugLine.innerHTML =
991
- "ANI: " +
992
- this.countAppleANI +
993
- " Stage: " +
994
- this.lastCalled +
995
- " TimeCode: " +
996
- resultFramesStr +
997
- " extra call";
998
- } else {
999
- //cannot go into here in normal
1000
- }
1001
-
1002
- window.requestAnimationFrame(this.onAnimatedFrameChromeMac);
1003
- }
1004
-
1005
- vidFiredTime = 0;
1006
-
1007
- correction = false;
1008
-
1009
- prevFraction = 0.99;
1010
- lastDblFrame = 0;
1011
- lastRealFrame = 0;
1012
- lastNow = 0;
1013
- // numberOfFramesAfterFpsFail=-99;
1014
-
1015
- onAnimatedFrameSafari(timeStamp) {
1016
- this.countANI++;
1017
- let nowTime = new Date();
1018
- const options = this.options;
1019
- var performanceNow = performance.now();
1020
- var delta = performanceNow - this.lastNow;
1021
- this.lastNow = performanceNow;
1022
- var screenFPS = 1000.0 / this.addNumberToAniDeltaArray(delta);
1023
-
1024
- if (screenFPS == -1000){
1025
- options.stopAugmentFor = 3;
1026
- if (this.countANI%60==0)
1027
- {
1028
- console.BBerror("Experience difficulties with overloading: (not consistant Calls of requestAnimatedFrame). Stopping augmentation."); //for debugging printing it every frame
1029
- console.log(this.aniDelltaTimeArray);
1030
- }
1031
- }
1032
- else if (Math.abs(screenFPS - 60.0) > 5) {
1033
- //options.stopAugment = true;
1034
- options.stopAugmentFor = 3;
1035
- console.BBerror("Unsupported screen FPS: " + screenFPS); //for debugging printing it every frame
1036
- }
1037
-
1038
- let hours = String(nowTime.getHours()).padStart(2, "0");
1039
- let minutes = String(nowTime.getMinutes()).padStart(2, "0");
1040
- let seconds = String(nowTime.getSeconds()).padStart(2, "0");
1041
- let milliseconds = String(nowTime.getMilliseconds()).padStart(3, "0");
1042
- let formattedTime = `${hours}:${minutes}:${seconds}.${milliseconds}`;
1043
- var number = 2;
1044
- let DebugLine;
1045
- if (options.debug) {
1046
- DebugLine = document.getElementById("debugText");
1047
- var numberInput = document.getElementById("numberInput").value;
1048
- number = parseInt(numberInput);
1049
- }
1050
- var realFrame = 0;
1051
-
1052
- var dblFrame =
1053
- (((this.video.currentTime - options.syncPoint.playerTime) %
1054
- (60 * 60 * 24)) *
1055
- 60000) /
1056
- 1001; // take time excluding days
1057
- realFrame = Math.floor(dblFrame) + options.syncPoint.frame;
1058
- var correction2FrameJump = false;
1059
- if (
1060
- realFrame - this.lastRealFrame == 2 &&
1061
- dblFrame - this.lastDblFrame < 1.5
1062
- ) {
1063
- this.lastRealFrame = realFrame;
1064
- realFrame--;
1065
- correction2FrameJump = true;
1066
- }
1067
- if (correction2FrameJump) {
1068
- this.lastRealFrame = realFrame + 1; //because we changed it and need to save orig
1069
- } else {
1070
- this.lastRealFrame = realFrame;
1071
- }
1072
-
1073
- this.lastDblFrame = dblFrame;
1074
- realFrame += number;
1075
- var fraction = dblFrame - Math.floor(dblFrame);
1076
- if (this.correction || (fraction < 0.226 && this.prevFraction < 0.226))
1077
- this.correction = true;
1078
- if (fraction > 0.8) this.correction = false;
1079
- this.prevFraction = fraction;
1080
- if (this.correction) realFrame--;
1081
- var timeStr = "";
1082
- timeStr = printTimecode(
1083
- frames2Timecode(realFrame, this.sourceFPS, true),
1084
- true,
1085
- true,
1086
- );
1087
-
1088
- var offsetStr = "";
1089
- if (options.augmentation.offset<0){
1090
- offsetStr+="-";
1091
- offsetStr += printTimecode(
1092
- frames2Timecode((-1)*options.augmentation.offset, this.sourceFPS, true),
1093
- true,
1094
- true,
1095
- );
1096
- } else{
1097
- offsetStr += printTimecode(
1098
- frames2Timecode(options.augmentation.offset, this.sourceFPS, true),
1099
- true,
1100
- true,
1101
- );
1102
- }
1103
-
1104
- let resultFrames = realFrame - options.augmentation.offset;
1105
- if (resultFrames < 0) resultFrames += 5178816;// number of frames in 23:59:59;59 + 1 = 24h
1106
- if (resultFrames >= 5178816) resultFrames -= 5178816;// number of frames in 23:59:59;59 + 1 = 24h
1107
- var resultFramesStr = "";
1108
- resultFramesStr = printTimecode(
1109
- frames2Timecode(resultFrames, this.sourceFPS, true),
1110
- true,
1111
- true,
1112
- );
1113
-
1114
- if (options.debug) {
1115
- var offsetColor = "black";
1116
- if (
1117
- options.augmentation.offset != this.lastOffset ||
1118
- this.lastOffsetShown < 10 * 60
1119
- ) {
1120
- var offsetColor = "red";
1121
- }
1122
- DebugLine.innerHTML =
1123
- " countAppleANI: " +
1124
- this.countAppleANI +
1125
- ", Time Fired: " +
1126
- formattedTime +
1127
- ", delta: " +
1128
- delta.toFixed(0) +
1129
- ", screenFPS: " +
1130
- screenFPS.toFixed(4) +
1131
- ", dblFrame: " +
1132
- dblFrame.toFixed(4) +
1133
- ", ANI-VID: " +
1134
- (nowTime - this.vidFiredTime) +
1135
- ", Game: " +
1136
- options.gameId +
1137
- ", stremTime: " +
1138
- timeStr +
1139
- "( " +
1140
- realFrame +
1141
- " )" +
1142
- ", offset: <span style='color:" +
1143
- offsetColor +
1144
- "'>" +
1145
- options.augmentation.offset +
1146
- "(" +
1147
- offsetStr +
1148
- ")</span>" +
1149
- " </br>" +
1150
- " </br>" +
1151
- "<span style='font-size: 30px;'>, resultTime: " +
1152
- resultFramesStr +
1153
- "(" +
1154
- resultFrames +
1155
- ")</span>" +
1156
- " </br>" +
1157
- " </br>" +
1158
- ", LastSyncPointTime:" +
1159
- options.syncPoint.playerTime.toFixed(4) +
1160
- ", LastSyncPointFrame:" +
1161
- options.syncPoint.frame +
1162
- ", LastSyncPoint:" +
1163
- options.syncPoint.stringTime +
1164
- ", correction: " +
1165
- this.correction +
1166
- ", correction2FrameJump: " +
1167
- correction2FrameJump;
1168
- }
1169
- if (this.metaDataFetcher) this.metaDataFetcher.setLastFrame(resultFrames);
1170
- this.render(resultFrames);
1171
- this.countAppleANI++;
1172
-
1173
- window.requestAnimationFrame(this.onAnimatedFrameSafari);
1174
- return;
1175
- }
1176
-
1177
- renderOnNextFrame = -1;
1178
- countAppleANI = 0;
1179
-
1180
- doCalculateBlackBars(videoElement) {
1181
- var cssWidth = parseInt(
1182
- window.getComputedStyle(videoElement).getPropertyValue("width"),
1183
- 10,
1184
- );
1185
- var cssHeight = parseInt(
1186
- window.getComputedStyle(videoElement).getPropertyValue("height"),
1187
- 10,
1188
- );
1189
-
1190
- var videoWidth = videoElement.videoWidth;
1191
- var videoHeight = videoElement.videoHeight;
1192
-
1193
- var cssAspectRatio = cssWidth / cssHeight;
1194
- var videoAspectRatio = videoWidth / videoHeight;
1195
-
1196
- var blackBars = { top: 0, bottom: 0, left: 0, right: 0 };
1197
-
1198
- if (cssAspectRatio > videoAspectRatio) {
1199
- // Black bars on the left and right (pillarboxing)
1200
- var newWidth = cssHeight * videoAspectRatio;
1201
- blackBars.left = (cssWidth - newWidth) / 2;
1202
- blackBars.right = blackBars.left;
1203
- } else if (cssAspectRatio < videoAspectRatio) {
1204
- // Black bars on the top and bottom (letterboxing)
1205
- var newHeight = cssWidth / videoAspectRatio;
1206
- blackBars.top = (cssHeight - newHeight) / 2;
1207
- blackBars.bottom = blackBars.top;
1208
- }
1209
- return blackBars;
1210
- }
1211
-
1212
- calculateBlackBars(videoElement) {
1213
- const canvas = this.getCanvas();
1214
- if (!canvas) return;
1215
-
1216
- var blackBars = this.doCalculateBlackBars(videoElement);
1217
- canvas.style.top = Math.round(blackBars.top) + "px";
1218
- canvas.style.left = Math.round(blackBars.left) + "px";
1219
-
1220
- return blackBars;
1221
- }
1222
-
1223
- initializeComponents = () => {
1224
- if (window.BB.RENDER_IN_WORKER) {
1225
- this.initializeComponents_Worker();
1226
- } else {
1227
- this.initializeComponents_MainThread();
1228
- }
1229
- };
1230
-
1231
- initializeComponents_MainThread = () => {
1232
- let playerCont = this.video.parentElement;
1233
- this.composer = new Composer3DForMainThread(this.options, playerCont);
1234
- this.ads = new Ads(this.composer, this.options);
1235
- this.metaDataFetcher = new MetaDataFetcher(this.options, this.composer);
1236
- };
1237
-
1238
- initializeComponents_Worker = () => {
1239
- let playerCont = this.video.parentElement;
1240
- let oTop = playerCont.offsetTop;
1241
- let oLeft = playerCont.offsetLeft;
1242
- this.canvas.style.top = 0; //oTop + 'px';
1243
- this.canvas.style.left = 0; //oLeft + 'px';
1244
-
1245
- var blackBars = this.doCalculateBlackBars(this.video);
1246
-
1247
- if (this.video.readyState >= 1) {
1248
- var blackBars = this.doCalculateBlackBars(this.video);
1249
- this.canvas.style.top = Math.round(blackBars.top) + "px";
1250
- this.canvas.style.left = Math.round(blackBars.left) + "px";
1251
- } else {
1252
- console.log(
1253
- "video element is not yet loaded. Setting up loadedmetadata event to wait until it's loaded. video.readyState=" +
1254
- this.video.readyState,
1255
- );
1256
- this.video.addEventListener("loadedmetadata", () => {
1257
- var blackBars = this.doCalculateBlackBars(this.video);
1258
- this.canvas.style.top = Math.round(blackBars.top) + "px";
1259
- this.canvas.style.left = Math.round(blackBars.left) + "px";
1260
- });
1261
- }
1262
-
1263
- this.canvas.style.width = "100%";
1264
- this.canvas.style.zIndex = 100000;
1265
- this.canvas.width = playerCont.clientWidth;
1266
- this.canvas.height = playerCont.clientHeight;
1267
- this.canvas.style.position = "absolute";
1268
-
1269
- let videoTag = playerCont.getElementsByTagName("video")[0];
1270
- let nextElement = videoTag.nextSibling;
1271
-
1272
- if (nextElement) playerCont.insertBefore(this.canvas, nextElement);
1273
- else playerCont.appendChild(this.canvas);
1274
- this.canvas.style["pointer-events"] = "none";
1275
- this.composer = new Composer3dWorker(this.canvas, this.options);
1276
- videoTag.addEventListener("click", (e) => {
1277
- var rect = this.canvas.getBoundingClientRect();
1278
- this.composer.onUserClickOnCanvas(e.clientX, e.clientY, rect);
1279
- });
1280
-
1281
- this.ads = new Ads(this.composer, this.options);
1282
- this.metaDataFetcher = new MetaDataFetcher(this.options, this.composer);
1283
- };
1284
-
1285
- getCanvas = () => {
1286
- return this.composer?.getCanvas();
1287
- };
1288
- }