-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest1.html
868 lines (769 loc) · 32.4 KB
/
test1.html
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
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-145603134-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-145603134-1');
</script>
<script type="text/javascript" >
var timerStart = Date.now();
</script>
<script src="./js/video.min.js"></script>
<link href="./js/video-js.min.css" rel="stylesheet">
<!-- quality levels for streaming -->
<script src="./js/videojs-contrib-quality-levels.min.js"></script>
<!-- DASH -->
<script src="./js/dash.all.min.js"></script>
<script src="./js/videojs-dash.min.js"></script>
</head>
<body>
<div id="WPTAttributeList" style ="display:none">WPT data here</div>
<div id="WPTData" style ="display:none">WPT data here</div>
<div id="WPTStartTime" style ="display:none">WPT data here</div>
<div id="WPTStallTotal" style ="display:none"> WPT data here</div>
<div id="WPTStallTotalTime" style ="display:none"> WPT data here</div>
<div id="WPTminStall" style ="display:none">WPT data here</div>
<div id="WPTmaxStall" style ="display:none">WPT data here</div>
<div id="WPTavgStall" style ="display:none"> WPT data here</div>
<div id="WPTBufferPercentDanger" style ="display:none">WPT data here</div>
<div id="WPTBufferPercentLow" style ="display:none">WPT data here</div>
<div id="WPTBufferPercentSafe" style ="display:none">WPT data here</div>
<div id="WPTBufferMax" style ="display:none">WPT data here</div>
<div id="WPTBufferAtStart" style ="display:none">WPT data here </div>
<div id="WPTBitrateChangeCount" style ="display:none">WPT data here</div>
<div id="WPTVideoDuration" style ="display:none">WPT data here</div>
<div id="WPTAudio" style ="display:none">WPT data here</div>
<div id="video">
</div>
<div id="images1"></div>
<p>
<p>
<div class="buffered">
<span id="buffered-amount"></span>
</div>
<div class="progress">
<span id="progress-amount"></span>
</div>
<h1>
<div id="networkspeed">
</div>
<div id="playbackstate">
</div>
<div id="timetostart">
</div>
<div id="VideoHasAudio">
</div>
<div id="bufferstart">
</div>
<div id="videotime">
</div>
<div id="bufferstate"></div>
<svg id="svg" width = "1000" height = "100" class="chart">
<polyline id="polyline-red"
fill="none"
stroke="red"
stroke-width="10"
points=""/>
<polyline id="polyline-orange"
fill="none"
stroke="orange"
stroke-width="10"
points=""/>
<polyline id="polyline-green"
fill="none"
stroke="green"
stroke-width="10"
points=""/>
</svg>
<div class="bufferstats" width = "1000" display ="flex">
<div class="danger" width = "300">
Danger (<5s):
<div id="dangervalue">
</div>
</div>
<div class="low" width = "300">
Low (5-10s):
<div id="lowvalue"></div>
</div>
<div class="safe" width = "300">
Safe (>10s):
<div id="safevalue"></div>
</div>
</div>
</div>
<div id="stallstate">
stall data
</div>
<div id="stallsum">
total stall data
</div>
<div id="bitratechange">
</div>
<div id="stoptime">
</div>
</h1>
<div id="images2"></div>
<div id="events">
</div>
<style>
.events{
height: 200px;
position: relative;
width: 1000px;
white-space: pre;
}
.buffered {
height: 20px;
position: relative;
background: #555;
width: 1000px;
}
#buffered-amount {
display: block;
height: 100%;
background-color: #777;
width: 0;
}
.progress {
margin-top: -20px;
height: 20px;
position: relative;
width: 1000px;
}
#progress-amount {
display: block;
height: 100%;
background-color: #595;
width: 0;
}
div.danger {
color: #FF0000;
}
.low {
color: orange;
}
.safe {
color: green;
}
</style>
<script>
//time of script evaluation start
var scriptEvalStart = Date.now();
//get a video url from the query string. Fall back to NASA rocket if no video
//load images and run JS to simulate other processes
//if the images are loaded with http2, they share the connection with the video (if all on Cloudinary)
//if HTTP2= flase, the images are delivered via HTTP1, and thus on another connection
var imageCount=0;
var js = false;
var HTTP2 = false;
var videoUrl = "https://res.cloudinary.com/streamornot/video/upload/sp_hd/v1565685788/rbsp_launch_720p_gz9zfm_dmaijg.m3u8";
//attributes in HTML5 that I can add or remove
var preload="auto";
var controls = false;
var loop = false;
var poster = false;
var muted = true;
var autoplay = true;
var height;
var width = "1080";
var attributes = "";
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('video')){
videoUrl = urlParams.get('video');
}
if (urlParams.has('imagecount')){
imageCount = urlParams.get('imagecount');
}
if (urlParams.has('js')){
js = urlParams.get('js');
}
if (urlParams.has('http2')){
HTTP2 = urlParams.get('http2');
}
if (urlParams.has('preload')){
if(urlParams.get('preload') == "auto" ||urlParams.get('preload') =="metadata" || urlParams.get('preload') == "none"){
preload = urlParams.get('preload');
}
}
attributes += " preload=\""+preload +"\" ";
if(urlParams.has('controls')){
if (urlParams.get('controls') == "true"){
controls = true;
attributes += " controls ";
}
}
if(urlParams.has('poster')){
if (urlParams.get('poster') == "true"){
poster = true;
attributes += " poster=\"https://res.cloudinary.com/streamornot/image/upload/v1565682314/beach_oko0am.jpg\"";
}
}
if(urlParams.has('height')){
height = urlParams.get('height');
attributes += " height=\""+ height+"\" ";
}
if(urlParams.has('width')){
width = urlParams.get('width');
}
attributes += " width=\""+ width+"\" ";
//testing streams
//html5
//videoUrl = "https://res.cloudinary.com/dougsillars/video/upload/v1546731047/campus5_ou5pmy.mp4";
//stream
//videoUrl = "https://res.cloudinary.com/dougsillars/video/upload/sp_hd/v1546731047/campus5_ou5pmy.m3u8";
//get extension
var videoExt = videoUrl.substr(1+videoUrl.lastIndexOf(".")).toLowerCase();
//event listener is different in html5 vs videojs
//so I have to call different functions for HTML5 vs. VideoJS
//this also sets the div to play back html5 or VideoJS
var videoType = "";
if(videoExt =="m3u8"||videoExt=="mpd"){
//streaming video - so add video js tags to the video div
console.log("streaming video")
videoType = "stream";
var streamType="";
if (videoExt =="m3u8"){
streamType = "application/x-mpegURL";
}else{
streamType = "application/dash+xml";
}
document.getElementById("video").innerHTML="<video-js id=\"my-video\" class=\"vjs-default-skin\" "+attributes+" autoplay muted playsinline ><source id=\"videosrc\" src=\""+videoUrl +"\" type=\""+streamType+"\"></video-js>";
console.log("<video-js id=\"my-video\" class=\"vjs-default-skin\" "+attributes+" autoplay muted playsinline ><source id=\"videosrc\" src=\""+videoUrl +"\" type=\""+streamType+"\"></video-js>");
}else{
//static video = so add html5 video tags to video div
console.log("html 5 video");
videoType = "html5";
document.getElementById("video").innerHTML="<video id=\"my-video\" class=\"vjs-default-skin\" "+attributes+" autoplay muted playsinline><source id=\"videosrc\" src=\""+videoUrl +"\"></video>";
console.log("<video id=\"my-video\" class=\"vjs-default-skin\" "+attributes+" autoplay muted playsinline><source id=\"videosrc\" src=\""+videoUrl +"\"></video>");
}
//onloadstuff
window.onload = function(){
var videoStatus = "Not Started";
//set initial state
document.getElementById("playbackstate").innerHTML = videoStatus;
//variables we need
var myVideo="";
var bufferMax= 0;
var videoStopTime =0;
var videoStartTime =0;
var stallTimeSum =0;
var stallCounter = 0;
var stallLengthList =[];
var stallTimeList = [];
var bufferTimer = 0;
var bufferDanger=0;
var bufferLow=0;
var bufferSafe=0;
var bufferStart=0;
//metadataloaded at startup
var startWarning="";
var loadedTime=0;
var metadataLoadTime=0;
var videoLength=0;
var currentBitrate = 0;
var currentHeight = 0;
var currentWidth = 0;
var streamChangeCounter =0;
var duration=0;
var cumulativeBytes=0;
var bytesAtBitrateChange=0;
var dataPerBitrate =[];
var bitrateChangeTime=0;
var bitrateChangeCount = 0;
var jsInterval;
var minStall=0;
var maxStall = 0;
var avgStall=0;
var audio;
//if streaming, set videojs player
if (videoType == "stream"){
myVideo = videojs('my-video', {
responsive:true,
html5:{hls: {enableLowInitialPlaylist:true}}
});
let qualityLevels = myVideo.qualityLevels();
// myVideo.on('play', playStart);
myVideo.on('error', error);
myVideo.on('playing', playStart);
myVideo.on('loadedmetadata', metadataLoad);
myVideo.on('timeupdate', statusUpdate);
myVideo.on('waiting', waiting);
myVideo.on('stalled', stalled);
myVideo.on('emptied', emptied);
myVideo.on('ended', ended);
if (videoExt =="mpd") {
//no bitrate info for dash yet
var x = document.createElement("P");
var t = document.createTextNode("no bitrate data for DASH yet...");
x.appendChild(t);
document.getElementById("events").appendChild(x);
}
}
//html5
else{
myVideo = document.getElementById('my-video');
// myVideo.addEventListener('play', playStart);
myVideo.addEventListener('error', error);
myVideo.addEventListener('playing', playStart);
myVideo.addEventListener('loadedmetadata', metadataLoad);
myVideo.addEventListener('timeupdate', statusUpdate);
myVideo.addEventListener('waiting', waiting);
myVideo.addEventListener('stalled', stalled);
myVideo.addEventListener('emptied', emptied);
myVideo.addEventListener('ended', ended);
}
//if there is an error
function error(){
console.log("error occurred!");
console.log("Error " + myVideo.error.code + "; details: " + myVideo.error.message);
}
//measures when metadata is loaded
function metadataLoad(){
console.log("metadataLoad");
metadataLoadTime = Date.now();
var onloadtoMetadata = metadataLoadTime - timerStart;
var x = document.createElement("P");
var t = document.createTextNode("Video Metadata Loaded in (ms): " + onloadtoMetadata);
x.appendChild(t);
document.getElementById("events").appendChild(x);
console.log(t);
}
//when playback starts, run this
function playStart(){
videoStatus = "playing"
document.getElementById("playbackstate").innerHTML = videoStatus;
var playTime = Date.now();
if(videoStopTime >0){
//this could be a stall
var stallTime = (playTime - videoStopTime);
//total stall time
stallTimeSum+= stallTime;
stallCounter+=1;
stallLengthList.push(stallTime);
var currentTime;
if (videoType=="stream"){
currentTime = myVideo.currentTime();
}else{
currentTime = myVideo.currentTime;
}
stallTimeList.push(currentTime);
var stall = "stalled at:" + currentTime+"s for: " +stallTime;
var stallTotal = "Stall count: " + stallCounter+ " time: " + stallTimeSum;
document.getElementById("stallstate").innerHTML = stall;
document.getElementById("stallsum").innerHTML = stallTotal;
console.log(stall);
console.log(stallTotal);
}
else if (videoStopTime ==0){
//this is the initial startup only run it once
if (videoStartTime>=0){
var videoStart = playTime - scriptEvalStart;
var startupTime = "Video Start Time: " +videoStart;
console.log(startupTime);
videoStartTime = videoStart/1000;
//write to div to collect in WPT
document.getElementById("WPTStartTime").innerHTML = videoStartTime;
//deteriming how much has been buffered at startup
if (videoType=="stream"){
bufferStart = myVideo.bufferedEnd();
videoLength = myVideo.duration();
} else{
bufferStart = myVideo.buffered.end(myVideo.buffered.length - 1);
videoLength = myVideo.duration;
}
//write this to div for WPT now - if video is too long I'll never know the size
var bufferAtStart = "Video Buffer At Start: "+ bufferStart;
document.getElementById("WPTBufferAtStart").innerHTML = bufferStart;
document.getElementById("WPTVideoDuration").innerHTML = videoLength;
console.log("buffer at start:"+ bufferStart);
console.log("video length:"+ videoLength);
var audioMsg="";
//does video have audio (only for HTML5 ATM)
if(videoType == "html5"){
if(hasAudio(myVideo)) {
audio=true;
audioMsg = "video has audio";
} else{
audio=false;
audioMsg= "Video has no audio";
}
console.log(audioMsg);
document.getElementById("WPTAudio").innerHTML = audio;
document.getElementById("VideoHasAudio").innerHTML = audioMsg;
}
}
if (videoStartTime <10){
if(videoStartTime <5){
startWarning = "Video started in under 5s. Thats pretty good!";
document.getElementById("timetostart").style.color = "green";
}else{
startWarning = "Video started in " +videoStartTime+ "s. You might want to see if you can speed this up just a bit by lowering the bitrate";
document.getElementById("timetostart").style.color = "orange";
}
}else{
startWarning = "Videos that take " +videoStartTime+ " seconds to start up are likely to have large abandonment rates. Consider lowering the bitrate to speed startup time.";
document.getElementById("timetostart").style.color = "red";
}
document.getElementById("timetostart").innerHTML = startupTime;
document.getElementById("bufferstart").innerHTML = bufferAtStart;
if (videoType =="stream"){
//initial bitrate
let qualityLevels = myVideo.qualityLevels();
if(qualityLevels.selectedIndex>=0){
currentBitrate = qualityLevels[qualityLevels.selectedIndex].bitrate;
currentHeight = qualityLevels[qualityLevels.selectedIndex].height;
currentWidth = qualityLevels[qualityLevels.selectedIndex].width;
var x = document.createElement("P");
var t = document.createTextNode("Stream bitrate: " +currentBitrate + " " + currentHeight +"x"+currentWidth);
x.appendChild(t);
document.getElementById("events").appendChild(x);
console.log(t);
}
}
videoStopTime=1;
}
}
//does video have audio.
//code from https://stackoverflow.com/questions/21270048/html5-video-how-to-detect-when-there-is-no-audio-track
function hasAudio (video) {
return video.mozHasAudio ||
Boolean(video.webkitAudioDecodedByteCount) ||
Boolean(video.audioTracks && video.audioTracks.length);
}
//ID when the video is waiting
function waiting() {
videoStatus = "waiting";
//console.log(videoStatus);
document.getElementById("playbackstate").innerHTML = videoStatus;
//for streaming, the initial status is 'waiting' But this messes up
//the startup time calculation, so we'll only call it a stop AFTER video has started
if (videoStopTime != 0){
videoStopTime = Date.now();
}
}
//ID when the video has stalled
function stalled() {
videoStatus = "stalled";
//console.log(videoStatus);
document.getElementById("playbackstate").innerHTML = videoStatus;
//for streaming, the initial status is 'waiting' But this messes up
//the startup time calculation, so we'll only call it a stop AFTER video has started
if (videoStopTime != 0){
videoStopTime = Date.now();
}
}
//ID when the video buffer has been emptied
function emptied() {
videoStatus = "empty buffer";
//console.log(videoStatus);
document.getElementById("playbackstate").innerHTML = videoStatus;
//for streaming, the initial status is 'waiting' But this messes up
//the startup time calculation, so we'll only call it a stop AFTER video has started
if (videoStopTime != 0){
videoStopTime = Date.now();
}
}
//this runs every 250 ms
function statusUpdate() {
//network speed
//chrome only
//copy n pastin from stackoverflow FTW
//https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome
var isChromium = window.chrome;
var winNav = window.navigator;
var vendorName = winNav.vendor;
var isOpera = typeof window.opr !== "undefined";
var isIEedge = winNav.userAgent.indexOf("Edge") > -1;
var isIOSChrome = winNav.userAgent.match("CriOS");
if (isIOSChrome) {
// is Google Chrome on IOS
} else if(
isChromium !== null &&
typeof isChromium !== "undefined" &&
vendorName === "Google Inc." &&
isOpera === false &&
isIEedge === false
) {
// is Google Chrome
var downLink = "Connection Speed: "+ navigator.connection.downlink + " MBPS";
document.getElementById("networkspeed").innerHTML = downLink;
}
//buffer calculations
var bufferEnd;
var videoCurrentTime;
if (videoType=="stream"){
duration = myVideo.duration();
bufferEnd = myVideo.bufferedEnd();
videoCurrentTime = myVideo.currentTime();
} else{
duration = myVideo.duration;
bufferEnd = myVideo.buffered.end(myVideo.buffered.length - 1);
videoCurrentTime = myVideo.currentTime;
}
//charting info
//points are duration *4 (4 times a sec)
//number of x points
//width of 1000
var xlength = 1000/(duration*4);
//assume max buffer is 40s and 100 pixels high
var ylength = 100/40;
//calculate time in buffer
var timeInBuffer = (bufferEnd - videoCurrentTime).toFixed(2);
document.getElementById("buffered-amount").style.width = (bufferEnd/ duration) * 100 + "%";
document.getElementById("bufferstate").innerHTML = "Video Buffer (s):" +timeInBuffer;
//console.log(timeInBuffer );
//imma gonna try to add to a svg live
var svg = document.getElementById('svg');
var point = svg.createSVGPoint();
point.x = bufferTimer*xlength;
point.y = 100- timeInBuffer*ylength;
var polyline= document.getElementById('chart-polyline1');
// var stroke = svg.createSVGStroke();
//increment the timer
bufferTimer +=1;
bufferMax = parseFloat(bufferMax);
timeInBuffer = parseFloat(timeInBuffer);
//report back important stuff about buffertime
//console.log("buffermax"+ bufferMax +" Time in buffer:" +timeInBuffer);
if (bufferMax < timeInBuffer){
bufferMax = timeInBuffer;
}
if (timeInBuffer < 10){
if (timeInBuffer < 5){
//I'll call this the danger zone
bufferDanger += 250;
polyline= document.getElementById('polyline-red');
}else{
//under 10 but over 5 - call this low buffer
bufferLow +=250;
polyline= document.getElementById('polyline-orange');
}
} else {
//over 10 probably ok
bufferSafe +=250;
polyline= document.getElementById('polyline-green');
}
polyline.points.appendItem(point);
//console.log(point);
document.getElementById("dangervalue").innerHTML = bufferDanger;
document.getElementById("lowvalue").innerHTML = bufferLow;
document.getElementById("safevalue").innerHTML = bufferSafe;
//playbacktime
var playback = "video (s): " +videoCurrentTime.toFixed(2);
document.getElementById("videotime").innerHTML = playback;
document.getElementById('progress-amount').style.width = ((videoCurrentTime / duration)*100) + "%";
//bitrate information
//only streaming
if(videoType=="stream"){
//how many bytes have we used?
if(videoExt =="m3u8"){
cumulativeBytes = myVideo.tech({IWillNotUseThisInPlugins:true}).hls.stats.mediaBytesTransferred;
}
let qualityLevels = myVideo.qualityLevels();
//console.log(qualityLevels);
if(currentBitrate >0){
//see if the bitrate has changed?
var oldBitrate = currentBitrate;
currentBitrate = qualityLevels[qualityLevels.selectedIndex].bitrate;
if(oldBitrate != currentBitrate){
//bitrate has changed!!!
//how many bytes were streamed at old bitrate?
var bytesAtOldBitrate = cumulativeBytes - bytesAtBitrateChange;
//store bitrate and data usage in array
dataPerBitrate.push([oldBitrate, bytesAtOldBitrate, bitrateChangeTime]);
//now update information in variables
currentHeight = qualityLevels[qualityLevels.selectedIndex].height;
currentWidth = qualityLevels[qualityLevels.selectedIndex].width;
bitrateChangeTime = videoCurrentTime.toFixed(2);
//update events div on page
var x = document.createElement("P");
var t = document.createTextNode("Time: "+bitrateChangeTime+" bitrate: " +currentBitrate + " " + currentHeight +"x"+currentWidth);
x.appendChild(t);
document.getElementById("events").appendChild(x);
console.log('Quality Level changed!');
console.log(t);
//draw a line in the buffer chart showing a change
var svgns = "http://www.w3.org/2000/svg";
var xLength = 1000/(duration*4);
var rect = document.createElementNS(svgns, 'rect');
rect.setAttributeNS(null, 'x', bufferTimer*xLength);
rect.setAttributeNS(null, 'y', 0);
rect.setAttributeNS(null, 'height', 100);
rect.setAttributeNS(null, 'width', xLength);
if (currentBitrate > oldBitrate){
//quality got better: green
rect.setAttributeNS(null, 'fill', 'green');
} else {
//quality got worse: red
rect.setAttributeNS(null, 'fill', 'red');
}
document.getElementById('svg').appendChild(rect);
streamChangeCounter +=1;
}
}else{
//no bitrate set - but video is playing so set the value!
//set bitrates - because they are not set yet
if(qualityLevels.selectedIndex>=0){
currentBitrate = qualityLevels[qualityLevels.selectedIndex].bitrate;
currentHeight = qualityLevels[qualityLevels.selectedIndex].height;
currentWidth = qualityLevels[qualityLevels.selectedIndex].width;
var x = document.createElement("P");
var t = document.createTextNode("Stream bitrate: " +currentBitrate + " " + currentHeight +"x"+currentWidth);
x.appendChild(t);
document.getElementById("events").appendChild(x);
console.log(t);
}
}
}
}
//Video has ended playing.. final stats
function ended() {
videoStatus = "ended"
//if the video plays all the way through, there will be 5s in yellow and 5s in red (as buffer plays out)
//there might also be some at startup
if (videoType=="stream"){
duration = myVideo.duration();
} else{
duration = myVideo.duration;;
}
var expectedDanger = 5/duration*1000;
var expectedLow = 5/duration*1000;
document.getElementById("playbackstate").innerHTML = videoStatus;
var loadedTime = Date.now();
var percentDanger = (bufferDanger/1000/duration*100).toFixed(2);
var percentLow = (bufferLow/1000/duration*100).toFixed(2);
var percentSafe = (bufferSafe/1000/duration*100).toFixed(2);
//lets say if 5% of video playback is in danger, or 10% is in low ist should be flagged
var dangerFlag = percentDanger - expectedDanger;
var lowFlag = percentLow- expectedLow;
var stallWarning = "empty";
if(dangerFlag >5 || lowFlag >10){
//we've got potential stall situation
stallWarning =" your video had very little video in the buffer during playback. consider lowering the bitrate."
}
var stallSummaryStats = "";
if (stallLengthList.length >0){
//calculate stall stats!!
minStall = Math.min(Math.min.apply(null,stallLengthList),0);
maxStall = Math.max(Math.max.apply(null,stallLengthList),0);
var sum = stallLengthList.reduce(function(a, b) { return a + b; });
avgStall = Math.min(sum/stallLengthList.length,0);
var stallTimeStat=" ";
for (var l=0;l<stallLengthList.length; l++){
stallTimeStat+= "Stalled at: (s): "+ stallTimeList[l].toFixed(2) + " Stall Length: "+stallLengthList[l]+"<br/>";
}
stallSummaryStats = "Minimum stall: "+ minStall+"ms. Max Stall: "+maxStall+
" ms. Average Stall length: " +avgStall.toFixed(2)+" ms<br/>"+stallTimeStat +"<br/><br/>";
}
var bitrateStats="";
//bitrate stats
if (dataPerBitrate.length >0){
//add in the last bitrate change
let qualityLevels = myVideo.qualityLevels();
currentBitrate = qualityLevels[qualityLevels.selectedIndex].bitrate;
cumulativeBytes = myVideo.tech().hls.stats.mediaBytesTransferred;
var bytesAtLastBitrate = cumulativeBytes - bytesAtBitrateChange;
//store bitrate and data usage in array
dataPerBitrate.push([currentBitrate, bytesAtLastBitrate, bitrateChangeTime]);
//calculate stall stats!!
bitrateChangeCount = dataPerBitrate.length;
console.log(dataPerBitrate);
var printBitrateArray="";
for(var i=0;i<bitrateChangeCount;i++){
printBitrateArray += "Time: "+dataPerBitrate[i][2]+" Bitrate: "+ dataPerBitrate[i][0]+ " Bytes: "+dataPerBitrate[i][1] + "<br/>";
}
bitrateStats = "Bitrate changed " +bitrateChangeCount+ " times during playback, here are the bitrate and byte usage:<br/>" + printBitrateArray;
}
var t = "total stalls: " +stallCounter + " stall time: " + stallTimeSum+"ms <br/>"
+stallSummaryStats +
"Buffer Level Danger: " + percentDanger +
"% Low: " + percentLow +
"% Safe: "+ percentSafe +"%<br/>"
+stallWarning +"<br/>"+startWarning +"<br/>"+ bitrateStats;
document.getElementById("events").innerHTML = t;
var jsonResults = "{\"stallTotal\":"+stallCounter
+ ", \"stallTotalTime\":"+ stallTimeSum
+ ", \"minStall\":"+ minStall
+ ", \"maxStall\":"+ maxStall
+ ", \"avgStall\":"+ avgStall
+ ", \"bufferPercentDanger\":"+ percentDanger
+ ", \"bufferPercentLow\":"+ percentLow
+ ", \"bufferPercentSafe\":"+ percentSafe
+ ", \"bufferMax\":"+ bufferMax
+ ", \"videoStartTime\":" +videoStartTime
+ ", \"bitrateChangeCount\":" + bitrateChangeCount
+ ", \"bufferStart\":" + bufferStart
+"}";
document.getElementById("WPTData").innerHTML = jsonResults;
document.getElementById("WPTStallTotal").innerHTML = stallCounter;
document.getElementById("WPTStallTotalTime").innerHTML = stallTimeSum;
document.getElementById("WPTminStall").innerHTML = minStall;
document.getElementById("WPTmaxStall").innerHTML = maxStall;
document.getElementById("WPTavgStall").innerHTML = avgStall;
document.getElementById("WPTBufferPercentDanger").innerHTML = percentDanger;
document.getElementById("WPTBufferPercentLow").innerHTML = percentLow;
document.getElementById("WPTBufferPercentSafe").innerHTML = percentSafe;
document.getElementById("WPTBitrateChangeCount").innerHTML = bitrateChangeCount;
document.getElementById("WPTBufferMax").innerHTML = bufferMax;
jsonResults
console.log(jsonResults);
clearInterval(jsInterval);
}
loadImages(imageCount,HTTP2);
function loadImages(imageCount,HTTP2){
//load a bunch of images
var imageSource1="";
var imageSource2="";
var mod2 = 0;
//only allow 100 images
//images are around 100 kb each
imageCount = Math.min(imageCount, 99);
var httpString="https://";
//console.log(httpString + HTTP2);
if(HTTP2 =="false"){
httpString="http://";
//console.log(httpString);
}
for(var i=0;i<imageCount;i++){
if(i<10){
//only a few images to load before the video.. most to load afterward
if(i%4==0){
imageSource1+= "<img width =\"10\" height = \"10\" src=\""+httpString+"res.cloudinary.com/streamornot/image/upload/q_auto,w_100"+i+"/v1565682314/beach_oko0am.jpg\">";
}else{
imageSource2+= "<img width =\"10\" height = \"10\" src=\""+httpString+"res.cloudinary.com/streamornot/image/upload/q_auto,w_100"+i+"/v1565682314/beach_oko0am.jpg\">";
}
}else{
if(i%2==0){
imageSource1+= "<img width =\"10\" height = \"10\" src=\""+httpString+"res.cloudinary.com/streamornot/image/upload/q_auto,w_10"+i+"/v1565682314/beach_oko0am.jpg\">";
}else{
imageSource2+= "<img width =\"10\" height = \"10\" src=\""+httpString+"res.cloudinary.com/streamornot/image/upload/q_auto,w_10"+i+"/v1565682314/beach_oko0am.jpg\">";
}
}
}
document.getElementById("images1").innerHTML = imageSource1;
document.getElementById("images2").innerHTML = imageSource2;
}
if(js){
var fib="";
//just adding random JS processing every 250ms
jsInterval= setInterval(function(){
var delay = Math.random()*30;
var fibStart = Date.now();
fibonacci(delay);
var fibEnd = Date.now();
var fibTime = fibEnd-fibStart;
//console.log("fibcalc ran for: " + fibTime);
});
}
//fibnonacci sequence adds random JS processing to the fun
function fibonacci(num) {
if (num <= 1) return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
</script>
</body>
</html>