-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathamp.diagnoverlay.js
1696 lines (1450 loc) · 78.7 KB
/
amp.diagnoverlay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The MIT License (MIT)
Copyright (c) 2015 Microsoft Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
//****************************************
//[email protected], 2/2018, Commercial Software Engineering
//****************************************
(function (mediaPlayer) {
"use strict";
mediaPlayer.plugin('diagnoverlay', function (options) {
//****************************************
// INPUTS & VARIABLES
//****************************************
//plugin level variables
var stopEventUpdate = false; //indate whether to stop event update in updateEvent such as when error occurs
var events = []; //holding all events
var EVENTS_TO_DISPLAY = 650;
var timeupdateDisplay = "", streamDisplay = "", audioBufferDataDisplay = "", videoBufferDataDisplay = "", framerate = "";
var player = this,
overlayCssClass = "amp-diagnoverlay",
tableCssClass = "overlay-table",
titleCssClass = "overlay-table-title",
selectCssClass = "overlay-select",
wrapperCssClass = "overlay-select-wrapper";
//input parameters
var title = !!options && !!options.title ? options.title : "",
opacity = !!options && !!options.opacity ? options.opacity : 0.6,
bgColor = !!options && !!options.bgColor ? options.bgColor : "Black",
x = !!options && !!options.x ? options.x : "left",
y = !!options && !!options.y ? options.y : "top";
//****************************************
// PLUGIN
//****************************************
var Component = mediaPlayer.getComponent("Component");
//create overlay
mediaPlayer.Overlay = amp.extend(Component, {
init: function (player, options) {
Component.call(this, player, options);
}
});
mediaPlayer.Overlay.prototype.createEl = function () {
var el = Component.prototype.createEl.call(this, "div", { className: overlayCssClass });
el.id = "outerdiv";
el.style.opacity = opacity;
el.style.backgroundColor = bgColor;
//el.style.borderRadius = '14px'; // standard
//el.style.MozBorderRadius = '15px'; // Mozilla
//el.style.WebkitBorderRadius = '15px'; // WebKit
el.onload = function () {
updateOverlay();
};
this.container = el; //and this.div, this.eventdiv
addElementsToOverlay(this);
return el;
};
//add HTML elements into the overlay
function addElementsToOverlay(overlay) {
//image
if (!!options && !!options.image && options.image.length > 0) {
var thumbnail = document.createElement('img');
thumbnail.id = "thumbnail";
thumbnail.style.visibility = "visible";
thumbnail.src = options.image;
thumbnail.style.width = "30px";
overlay.container.appendChild(thumbnail);
}
//top div
var div = videojs.createEl("div", {});
div.id = "innerdiv";
div.onload = function () {
updateOverlay();
};
overlay.container.appendChild(div);
//var label = document.createElement('label')
//label.htmlFor = "chkevent";
//label.appendChild(document.createTextNode("show events or errors"));
//overlay.container.appendChild(checkbox);
//overlay.container.appendChild(label);
//select
var select = document.createElement("select");
select.name = "select";
select.id = "select";
select.className = selectCssClass;
var dropdowns = ["More ...", "Events, errors, downloads", "DRM", "Browser, AMP, screen", "Renditions, streams, tracks"]; //options variable is taken
var dropdown;
for (var i = 0; i < dropdowns.length; i++) {
dropdown = document.createElement("option");
dropdown.innerHTML = dropdowns[i];
dropdown.value = dropdowns[i];
select.appendChild(dropdown)
}
select.onchange = function () {
//initial visibility status
stopEventUpdate = true;
player.overlay.pre.textContent = "";
player.overlay.pre.style.display = "none";
player.overlay.eventdiv.style.visibility = "visible";
player.overlay.eventdiv.style.display = "block";
player.overlay.eventdiv.innerHTML = "";
switch (select.options[select.selectedIndex].value) {
case dropdowns[0]:
//hide eventdiv
player.overlay.eventdiv.style.visibility = "hidden";
player.overlay.eventdiv.style.display = "none";
break;
case dropdowns[1]:
//start displaying events
stopEventUpdate = false;
updateEvent(EVENTS_TO_DISPLAY);
break;
case dropdowns[2]:
//display DRM info
getProtectionInfo();
break;
case dropdowns[3]:
//display browser and AMP info
BrowserUtils.getBrowserAMPInfo();
break;
case dropdowns[4]:
//display video renditions
AMPUtils.displayRenditions();
AMPUtils.displayAudioStreams();
AMPUtils.displayTextTracks();
//highlight the currentPlaybackBitrate(), without waiting for amp.eventName.playbackbitratechanged
if (!!player.currentPlaybackBitrate()) {
AMPUtils.updateCurrentPlaybackBitrate(player.currentPlaybackBitrate());
}
break;
default:
break;
}
} //onchange
//overlay-select-wrapper <div> containing select element for better styling select
var wrapperdiv = videojs.createEl("div", {});
wrapperdiv.className = wrapperCssClass;
wrapperdiv.appendChild(select);
overlay.container.appendChild(wrapperdiv);
//event div
var eventdiv = videojs.createEl("div", {});
eventdiv.id = "eventdiv";
eventdiv.style.visibility = "hidden";
eventdiv.style.display = "none";
eventdiv.onload = function () {
updateOverlay();
};
eventdiv.onclick = function () {
BrowserUtils.copyToClipboard(eventdiv.textContent);
};
overlay.container.appendChild(eventdiv);
//pre
var pre = document.createElement("pre");
pre.textContent = "";
pre.style.display = "none";
pre.onclick = function () {
BrowserUtils.copyToClipboard(pre.textContent);
};
overlay.container.appendChild(pre);
//expose div and eventdiv
overlay.div = div;
overlay.eventdiv = eventdiv;
overlay.pre = pre;
overlay.select = select;
}
player.ready(function () { //main function
var overlay = new mediaPlayer.Overlay(player);
player.overlay = player.addChild(overlay);
registerOverlayEvents();
events.push("player.ready event");
});
//****************************************
// POSITION & SIZE
//****************************************
//function showOverlay() {
// updateOverlay();
// player.overlay.removeClass("vjs-user-inactive");
// player.overlay.addClass("vjs-user-active");
//}
//function hideOverlay() {
// player.overlay.removeClass("vjs-user-active");
// player.overlay.removeClass("vjs-user-inactive");
// player.overlay.addClass("vjs-user-hide");
//}
function getX(innerdiv, x) {
var videoElement = player.el();
var position;
switch (x)
{
case "center":
position = (videoElement.clientWidth / 2) - (innerdiv.parentElement.clientWidth / 2);
break;
case "right":
position = videoElement.clientWidth - innerdiv.parentElement.clientWidth - 1;
break;
default:
position = 0;
break;
}
return position;
}
function getY(innerdiv, y) {
var position;
var videoElement = player.el(),
controlBarHeight = player.controlBar.el().clientHeight || 31,
progressControlHeight = player.controlBar.progressControl.el().clientHeight || 12;
switch(y)
{
case "middle":
position = (videoElement.clientHeight / 2) - (innerdiv.parentElement.clientHeight / 2) - (controlBarHeight / 2) - (progressControlHeight / 2);
break;
case "bottom":
position = videoElement.clientHeight - innerdiv.parentElement.clientHeight - controlBarHeight - progressControlHeight;
break;
default:
position = 0
break;
}
return position;
}
function updateOverlayMaxSize(innerdiv) {
// Update image max size according video size
var videoElement = player.el();
if ((videoElement.clientHeight < innerdiv.parentElement.clientHeight) || (videoElement.clientWidth < innerdiv.parentElement.clientWidth)) {
innerdiv.style.maxHeight = videoElement.clientHeight + 'px';
innerdiv.style.maxWidth = videoElement.clientWidth + 'px';
} else {
innerdiv.style.maxHeight = '100%';
innerdiv.style.maxWidth = '100%';
}
}
function updateOverlayPosition(outerdiv, innerdiv) {
// Update DIV based on image values (now calculated because it was added to the DOM)
outerdiv.style.left = getX(innerdiv, x) + 'px';
outerdiv.style.top = getY(innerdiv, y) + 'px';
}
//****************************************
// UPDATE CONTENT
//****************************************
function updateOverlay() {
//update position when the video returns from fullscreen
player.overlay.container.style.left = '0';
player.overlay.container.style.top = '0';
//check framerate plugin
var timecode;
if (!!amp.eventName.framerateready) {
timecode = "- current timecode: " + player.toTimecode(player.toPresentationTime(player.currentTime()));
} else {
timecode = "- current time: " + player.currentTime();
}
var audioStream = getCurrentAudioStream(player);
timeupdateDisplay = timecode +
"\n- current media time: " + ((!!player.currentMediaTime()) ? player.currentMediaTime().toFixed(3) : "") +
"\n- current absolute time: " + ((!!player.currentAbsoluteTime()) ? player.currentAbsoluteTime().toFixed(3) : "") +
"\n- current playback bitrate: " + addCommas(player.currentPlaybackBitrate()) +
"\n- current download bitrate: " + addCommas(player.currentDownloadBitrate()) +
"\n- current audio name: " + (!!audioStream ? audioStream.name : "") +
"\n- current audio codec: " + (!!audioStream ? audioStream.codec : "") +
"\n- current audio bitrate: " + (!!audioStream ? addCommas(audioStream.bitrate) : "") +
"\n- current audio language: " + (!!audioStream ? audioStream.language : "") +
"\n- current video track size: " + player.videoWidth() + " x " + player.videoHeight();
updateContent();
updateOverlayMaxSize(player.overlay.div);
updateOverlayPosition(player.overlay.container, player.overlay.div);
}
function updateContent()
{
var displayTitle = !!title && title.length > 0? title + "\n" : "";
player.overlay.div.innerText = displayTitle + timeupdateDisplay + streamDisplay + audioBufferDataDisplay + videoBufferDataDisplay + framerate;
}
//count: number of recent events to display
function updateEvent(count) {
//var clock = getWallClock();
var length = events.length;
if (!stopEventUpdate && length > 0 ) {
var msg = "";
count = Math.min(count, length)
for (var i = length - 1; i >= length - count; i--) {
if (i == length - 1) {
msg += "- " + events[i];
} else {
msg += "\n- " + events[i];
}
}
player.overlay.eventdiv.innerText= msg;
}
//in case events array gets too large
if (events.length > 15000) {
events = [];
}
}
function getCurrentAudioStream(player) {
var audioStreamList = player.currentAudioStreamList();
var audioStream = null;
if (audioStreamList) {
for (var i = 0; i < audioStreamList.streams.length; i++) {
if (audioStreamList.streams[i].enabled) {
audioStream = audioStreamList.streams[i];
break;
}
}
}
return audioStream;
}
//****************************************
//AMPUtils
//****************************************
function AMPUtils() { };
//get smooth URL
function getSmoothUrl() {
var url = player.currentSrc();
url = url.substr(0, url.toLowerCase().indexOf("/manifest") + 9);
return url;
}
function getDashUrl() {
return getSmoothUrl() + "(format=mpd-time-csf)";
}
function getHlsUrl() {
return getSmoothUrl() + "(format=m3u8-aapl)";
}
AMPUtils.getRenditions = function (amPlayer) {
var renditions = [];
if (amPlayer.currentVideoStreamList() != undefined) {
var videoStreamList = amPlayer.currentVideoStreamList();
var videoTracks;
for (var i = 0; i < videoStreamList.streams.length; i++) {
videoTracks = videoStreamList.streams[i].tracks;
if (videoTracks != undefined) {
for (var j = 0; j < videoTracks.length; j++)
renditions.push({
bitrate: videoTracks[j].bitrate,
width: videoTracks[j].width,
height: videoTracks[j].height,
selectable: videoTracks[j].selectable
});
}
}
}
return renditions;
}
//display video rendition array as a table in player.overlay.eventdiv
AMPUtils.displayRenditions = function () {
var ID = "rendition_table";
// if a table with the same id exists, clean up the data first
var tbl = document.getElementById(ID);
if (!!tbl) {
while (tbl.rows.length > 0) {
tbl.deleteRow(0);
}
} else {
tbl = document.createElement("table");
tbl.id = ID;
}
tbl.className = tableCssClass;
var renditions = AMPUtils.getRenditions(player);
var tblBody = document.createElement("tbody");
var row, cell, cellText;
var headers = ["Index", "Bitrate", "Width", "Height", "Selectable", "Restrict Bitrate"];
var titles = ["", "VIDEO RENDITIONS:"];
//space and title on top of table
for (var i = 0; i < titles.length; i++) {
row = document.createElement("tr");
cell = document.createElement("td");
cell.colSpan = headers.length;
cell.className = titleCssClass;
cellText = document.createTextNode(titles[i]);
cell.appendChild(cellText);
row.appendChild(cell);
tblBody.appendChild(row);
}
//create column headers row
row = document.createElement("tr");
for (var i = 0; i < headers.length; i++) {
cell = document.createElement("th");
cellText = document.createTextNode(headers[i]);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
//create data rows
if (!!renditions && renditions.length > 0) {
var columns;
for (var i = 0; i < renditions.length; i++) {
row = document.createElement("tr");
row.id = "rendition_" + i; //used to update background of currentPlaybackBitrate
columns = [i,
addCommas(renditions[i].bitrate),
renditions[i].width,
renditions[i].height,
renditions[i].selectable,
];
for (var j = 0; j < columns.length; j++) {
cell = document.createElement("td");
cellText = document.createTextNode(columns[j]);
cell.appendChild(cellText);
row.appendChild(cell);
}
//column: select (force-select a bitrate)
cell = document.createElement("td");
cellText = document.createElement("a");
cellText.onclick = (function (i) { return function () { AMPUtils.selectRendition(i); } })(i); //IIFE http://benalman.com/news/2010/11/immediately-invoked-function-expression/
cellText.innerHTML = "select";
cell.appendChild(cellText);
row.appendChild(cell);
tblBody.appendChild(row);
}
}
//create last row showing "Auto-adapt" link
row = document.createElement("tr");
//column: text
cell = document.createElement("td");
cell.colSpan = 5;
cellText = document.createTextNode("You can either force-select a bitrate or let it auto-adapt");
cell.appendChild(cellText);
row.appendChild(cell);
//column: Auto-adapt link
cell = document.createElement("td");
cellText = document.createElement("a");
cellText.onclick = function () { AMPUtils.selectRendition(-1); };
cellText.innerHTML = "Auto-adapt";
cell.appendChild(cellText);
row.appendChild(cell);
tblBody.appendChild(row);
// append the <tbody> inside the <table>
tbl.appendChild(tblBody);
player.overlay.eventdiv.appendChild(tbl);
}
//display audio stream array as a table in player.overlay.eventdiv
AMPUtils.displayAudioStreams = function () {
//var ID = "audio_streams_table";
var ID = "rendition_table";
// if a table with the same id exists, clean up the data first
var tbl = document.getElementById(ID);
//if (!!tbl) {
// while (tbl.rows.length > 0) {
// tbl.deleteRow(0);
// }
//} else {
// tbl = document.createElement("table");
// tbl.id = ID;
//}
tbl.className = tableCssClass;
var tblBody = document.createElement("tbody");
var row, cell, cellText;
var headers = ["Index", "Bitrate", "Enabled", "Language", "Name", "Codec"];
var titles = ["", "AUDIO STREAMS:"];
//space and title on top of table
for (var i = 0; i < titles.length; i++) {
row = document.createElement("tr");
cell = document.createElement("td");
cell.colSpan = headers.length;
cell.className = titleCssClass;
cellText = document.createTextNode(titles[i]);
cell.appendChild(cellText);
row.appendChild(cell);
tblBody.appendChild(row);
}
//create column headers row
row = document.createElement("tr");
for (var i = 0; i < headers.length; i++) {
cell = document.createElement("th");
cellText = document.createTextNode(headers[i]);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
if (!!player.currentAudioStreamList()) {
var columns;
var streams = player.currentAudioStreamList().streams;
if (!!streams) {
//create data rows
for (var i = 0; i < streams.length; i++) {
row = document.createElement("tr");
row.id = "audiostream_" + i;
columns = [i,
addCommas(streams[i].bitrate),
streams[i].enabled,
streams[i].language,
streams[i].name,
streams[i].codec
];
for (var j = 0; j < columns.length; j++) {
cell = document.createElement("td");
cellText = document.createTextNode(columns[j]);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
}
}
// append the <tbody> inside the <table>
tbl.appendChild(tblBody);
player.overlay.eventdiv.appendChild(tbl);
}
//display text tracks
AMPUtils.displayTextTracks = function () {
var ID = "rendition_table";
var tbl = document.getElementById(ID);
tbl.className = tableCssClass;
var tblBody = document.createElement("tbody");
var row, cell, cellText;
var headers = ["Index", "Language", "Kind", "Mode", "Label", "Source"];
var titles = ["", "TEXT TRACKS:"];
//space and title on top of table
for (var i = 0; i < titles.length; i++) {
row = document.createElement("tr");
cell = document.createElement("td");
cell.colSpan = headers.length;
cell.className = titleCssClass;
cellText = document.createTextNode(titles[i]);
cell.appendChild(cellText);
row.appendChild(cell);
tblBody.appendChild(row);
}
//create column headers row
row = document.createElement("tr");
for (var i = 0; i < headers.length; i++) {
cell = document.createElement("th");
cellText = document.createTextNode(headers[i]);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
if (!!player.textTracks_) {
var columns;
var tracks = player.textTracks_;
if (!!tracks && tracks.length > 0) {
//create data rows
for (var i = 0; i < tracks.length; i++) {
row = document.createElement("tr");
row.id = "texttrack_" + i;
columns = [i,
tracks[i].language,
tracks[i].kind,
tracks[i].mode,
tracks[i].label,
"..." + tracks[i].src.substr(tracks[i].src.length - 25, 25)
];
for (var j = 0; j < columns.length; j++) {
cell = document.createElement("td");
cellText = document.createTextNode(columns[j]);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
}
}
// append the <tbody> inside the <table>
tbl.appendChild(tblBody);
player.overlay.eventdiv.appendChild(tbl);
}
//create <table> with given matrix and id
AMPUtils.createTable = function (matrix, id) {
// if a table with the same id exists, clean up the data first
var tbl = document.getElementById(id);
if (!!tbl) {
while (tbl.rows.length > 0) {
tbl.deleteRow(0);
}
} else {
tbl = document.createElement("table");
}
var tblBody = document.createElement("tbody");
var row, cell, cellText;
// cells creation
for (var i = 0; i < matrix.length; i++) {
row = document.createElement("tr");
for (var j = 0; j < matrix[i].length; j++) {
cell = document.createElement("td");
cellText = document.createTextNode(matrix[i][j]);
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
// append the <tbody> inside the <table>
tbl.appendChild(tblBody);
return tbl;
}
//index = -1: auto-adapt, index >= 0: restrict to specific bitrate
AMPUtils.selectRendition = function (index) {
var videoStreamList = player.currentVideoStreamList();
if (!!videoStreamList) {
if (!!videoStreamList.streams) {
videoStreamList.streams[0].selectTrackByIndex(index);
}
}
}
//change background color of current playback videotrack in the <table>
//this method is called in the following events: amp.eventName.playbackbitratechanged, function displayInfo(3)
AMPUtils.updateCurrentPlaybackBitrate = function (bitrate) {
var renditions = AMPUtils.getRenditions(player);
var selectedRow;
if (!!renditions) {
for (var i = 0; i < renditions.length; i++) {
selectedRow = document.getElementById("rendition_" + i); //may be undefined if not shown
if (!!selectedRow) {
if (renditions[i].bitrate == bitrate) {
selectedRow.style.background = "green";
} else {
selectedRow.style.background = "none";
}
}
}
}
}
//****************************************
// BROWSER UTILS
//****************************************
function BrowserUtils() { };
//Utility function for making XMLHttpRequest
//httpMethod: GET, or POST
//responseType: arraybuffer, "" (default: text), blob, stream
//msCaching: auto, enabled, disabled
BrowserUtils.xhrRequest = function (url, httpMethod, responseType, msCaching, context, callback) {
var xhr = new XMLHttpRequest();
xhr.open(httpMethod, url);
xhr.responseType = responseType;
xhr.msCaching = msCaching;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (context == "useResponseXML") { //MPD request
callback(xhr.responseXML, context);
}
else { //fragment/LA request
callback(xhr.response, context);
}
} else {
console.log("XHR: failed. URL = " + url + ". Status = " + xhr.status + ". " + xhr.statusText);
callback(null, context);
}
}
}
xhr.send();
console.log("XHR: method=" + httpMethod + ", ResponseType=" + responseType + ", URL=" + url);
return xhr;
}
BrowserUtils.getBrowserAMPInfo = function () {
var LENGTH = Math.floor(navigator.userAgent.length / 2) - 12;
var msg = BrowserUtils.getBrowserPlugins() +
BrowserUtils.getBrowserMimeTypes() +
"\n- MSE: " + BrowserUtils.isMSESupported() +
"\n- EME: " + BrowserUtils.getEMESupport() +
"\n- user agent: " + (navigator.userAgent.length > LENGTH? navigator.userAgent.substr(0, LENGTH) + "\n " + navigator.userAgent.substr(LENGTH, navigator.userAgent.length - LENGTH) : navigator.userAgent) +
"\n- screen resolution: " + window.screen.width + " x " + window.screen.height +
"\n- screen available resolution: " + window.screen.availWidth + " x " + window.screen.availHeight +
"\n- screen color depth: " + window.screen.colorDepth +
"\n- screen pixel depth: " + window.screen.pixelDepth +
"\n- device pixel ratio: " + window.devicePixelRatio +
"\n- cookie enabled: " + navigator.cookieEnabled +
"\n- browser language: " + navigator.language +
"\n- HEVC support: " + BrowserUtils.supportCodec("video/mp4", "hev1") +
"\n- AMP version: " + player.getAmpVersion();
player.overlay.eventdiv.innerText = msg;
}
//get browser plugins (into ordered list)
BrowserUtils.getBrowserPlugins = function () {
var plugins = "\n- browser plugins:";
if (!!navigator.plugins && navigator.plugins.length > 0) {
for (var i = 0; i < navigator.plugins.length; i++) {
plugins += "\n -- " + navigator.plugins[i].name;
if (!!navigator.plugins[i].description){
plugins += " (" + navigator.plugins[i].description + ")";
}
}
} else {
plugins += " None detected."
}
return plugins;
}
BrowserUtils.getBrowserMimeTypes = function() {
var types = "\n- brwoser mime types: ";
if (!!navigator.mimeTypes && navigator.mimeTypes.length > 0) {
var mimes = navigator.mimeTypes;
for (var i=0; i < mimes.length; i++) {
types += "\n -- " + mimes[i].type;
if (!!mimes[i].description) {
types += " (" + mimes[i].description + ")";
}
}
}
else {
types += "None detected";
}
return types;
}
BrowserUtils.isMSESupported = function () {
var supported = false;
if (typeof MediaSource == "function") {
var mse = new MediaSource();
if (mse) { supported = true; }
}
return supported;
}
BrowserUtils.getEMESupport = function () {
var eme = "";
window.MediaKeys = window.MediaKeys || window.MSMediaKeys || window.WebKitMediaKeys;
if (window.MediaKeys && window.MediaKeys.isTypeSupported) {
//HTMLMediaElement.canPlayType(); navigator.requestMediaKeySystemAccess();
if (window.MediaKeys.isTypeSupported(ContentProtection.MediaKey_PlayReady) || window.MediaKeys.isTypeSupported(null, ContentProtection.MediaKey_PlayReady)) {
eme += ContentProtection.MediaKey_PlayReady + "; ";
}
if (window.MediaKeys.isTypeSupported(ContentProtection.MediaKey_Widevine) || window.MediaKeys.isTypeSupported(null, ContentProtection.MediaKey_Widevine)) {
eme += ContentProtection.MediaKey_Widevine + "; ";
}
if (window.MediaKeys.isTypeSupported(ContentProtection.MediaKey_ClearKey) || window.MediaKeys.isTypeSupported(null, ContentProtection.MediaKey_ClearKey)) {
eme += ContentProtection.MediaKey_ClearKey + "; ";
}
if (window.MediaKeys.isTypeSupported(ContentProtection.MediaKey_Access) || window.MediaKeys.isTypeSupported(null, ContentProtection.MediaKey_Access)) {
eme += ContentProtection.MediaKey_Access + "; ";
}
if (window.MediaKeys.isTypeSupported(ContentProtection.MediaKey_FairPlay) || window.MediaKeys.isTypeSupported(null, ContentProtection.MediaKey_FairPlay)) {
eme += ContentProtection.MediaKey_FairPlay;
}
}
//var config = [{
// "initDataTypes": ["cenc"],
// "audioCapabilities": [{
// "contentType": "audio/mp4;codecs=\"mp4a.40.2\""
// }],
// "videoCapabilities": [{
// "contentType": "video/mp4;codecs=\"avc1.42E01E\""
// }]
//}];
//try {
// navigator.requestMediaKeySystemAccess(ContentProtection.MediaKey_Widevine, config).then(function (mediaKeySystemAccess) {
// eme += ContentProtection.MediaKey_Widevine;
// }).catch(function (e) {
// console.log('no widevine support');
// console.log(e);
// });
//} catch (e) {
// console.log('no widevine support');
// console.log(e);
//}
//try {
// navigator.requestMediaKeySystemAccess(ContentProtection.MediaKey_PlayReady, config).then(function (mediaKeySystemAccess) {
// eme += ContentProtection.MediaKey_PlayReady;
// }).catch(function (e) {
// console.log('no playready support');
// console.log(e);
// });
//} catch (e) {
// console.log('no playready support');
// console.log(e);
//}
//try {
// navigator.requestMediaKeySystemAccess(ContentProtection.MediaKey_FairPlay, config).then(function (mediaKeySystemAccess) {
// eme += ContentProtection.MediaKey_FairPlay;
// }).catch(function (e) {
// console.log('no FairPlay support');
// console.log(e);
// });
//} catch (e) {
// console.log('no FairPlay support');
// console.log(e);
//}
return eme;
}
BrowserUtils.supportCodec = function (videoType, codecType) {
var vid = document.createElement('video');
var isSupported = vid.canPlayType(videoType + ';codecs="' + codecType + '"');
if (isSupported == "") {
isSupported = "No";
}
return isSupported;
}
//copy textual data into clipboard
BrowserUtils.copyToClipboard = function(text){
var clipboard = {
data: "",
intercept: false,
hook: function (evt) {
if (clipboard.intercept) {
evt.preventDefault();
evt.clipboardData.setData("text/plain", clipboard.data); //text/plain
clipboard.intercept = false;
clipboard.data = "";
}
}
};
window.addEventListener("copy", clipboard.hook);
clipboard.data = text;
clipboard.intercept = true;
document.execCommand("copy");
window.alert("Copied to clipboard.");
}
//****************************************
// DRM
//****************************************
//credit: http://dean.edwards.name/weblog/2009/12/getelementsbytagname/ This works in all major browsers
function getElementsByTagNameCustom(node, tagName) {
var elements = [], i = 0, anyTag = tagName === "*", next = node.firstChild;
while ((node = next)) {
if (anyTag ? node.nodeType === 1 : node.nodeName === tagName) elements[i++] = node;
next = node.firstChild || node.nextSibling;
while (!next && (node = node.parentNode)) next = node.nextSibling;
}
return elements;
}
//decode base64 binary and display in <div id="info">
function decodeBase64(base64Data) {
var a = Base64Binary.decode(base64Data), h = new Blob([a]), f = new FileReader;
f.onload = function (a) {
a = "ascii";
f.onload = function (a) {
//put protection header in pre
var protectionHeader = a.target.result.replace(/[^\x20-\x7E]/g, '');
player.overlay.pre.textContent = protectionHeader;
player.overlay.pre.style.display = "block";
var laurl = extractFromProtectionHeader(protectionHeader, "LA_URL");
player.overlay.eventdiv.innerText += "\nPlayReady LA_URL: " + laurl +
"\nmspr:pro: ";
}, f.readAsText(h, a);
};
f.readAsArrayBuffer(h);
}
function extractFromProtectionHeader(protectionHeader, node) {
var start = "<" + node + ">";
var end = "</" + node + ">";
var startIndex = protectionHeader.indexOf(start) + 2 + node.length;
var endIndex = protectionHeader.indexOf(end);
return protectionHeader.substring(startIndex, endIndex);
}
//credit goes to: http://base64online.org/decode/ for Base64Binary
var Base64Binary = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
/* will return a Uint8Array type */
decodeArrayBuffer: function (input) {
var bytes = (input.length / 4) * 3;
var ab = new ArrayBuffer(bytes);