forked from nanocosmos/h5live
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnanoplayer-adaptive.html
More file actions
641 lines (575 loc) · 27.3 KB
/
nanoplayer-adaptive.html
File metadata and controls
641 lines (575 loc) · 27.3 KB
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
<!DOCTYPE html>
<!--
nanoStream Player
(c) 2020, nanocosmos gmbh
http://www.nanocosmos.de
sales@nanocosmos.de
LEGAL NOTICE:
This material is subject to the terms and conditions defined in
separate license conditions ('LICENSE.txt')
All information contained herein is, and remains the property
of nanocosmos GmbH and its suppliers if any. The intellectual and technical concepts
contained herein are proprietary to nanocosmos GmbH, and are protected by trade secret
or copyright law. Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained from nanocosmos.
All modifications will remain property of nanocosmos.
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>nanoplayer</title>
<link href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="./css/main.css">
<link rel="stylesheet" href="./css/main-light.css">
<link rel="shortcut icon" href="./assets/flavicon.ico" type="image/x-icon" />
<!-- REMOVE LATER !!! -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<div id="container">
<p class="normal">
<h1 id="demo-title">nanoStream H5Live Player Demo</h1>
<strong id="demo-version"></strong>
</p>
<div id="playerDiv" class="player responsive">
<!---->
<video id="h5live" playsinline style="display:none;width:100%;height:100%"></video>
</div>
<p class="normal" id="timestamp-container" style="display:none">
<h1 id="timestamp" style="display:none">00:00:000</h1>
</p>
<p class="normal" id="metadata-container" style="display:none">
<strong>metadata</strong>
<pre id="metadata" style="display:none"></pre>
</p>
<div id="controlsWrapper" class="row-flex row-padding">
<div>
<button id="buttonPlay" onclick="play();" style="margin-right:5px">play</button>
<button id="buttonPause" onclick="pause();" style="margin-right:5px">pause</button>
<button id="buttonReset" onclick="startPlayer(null, true);" style="margin-right:5px">reset</button>
<button id="buttonFullscreenRequest" onclick="requestFullscreenPlayer();" style="margin-right:5px">fullscreen</button></div>
<div>
<button id="buttonMute" onclick="mute(this);" data-muted="false" style="margin-left:5px">mute</button>
<input type="range" min="0" max="100" id="faderVolume" oninput="setVolume(this, value);"
onchange="setVolume(this, value);" style="margin-left:5px; display:inline-block; vertical-align: middle;"/>
<output for="fader" id="volume" style="margin-left:5px; display:inline-block; vertical-align: middle;">100%</output>
</div>
</div>
<p class="normal">
<strong>switch rule: </strong><select id="switch-rule" onchange="onSetAdaption(this);">
<option value="none">none</option>
<option value="deviationOfMean">deviationOfMean</option>
<option value="deviationOfMean2">deviationOfMean2</option>
</select>
<strong>adaption downstep: </strong><select id="adaption-downstep" onchange="onSetAdaption(this);"></select>
</p>
<div class="col">
<div class="row">
<div class="col col-12">
<h2>entries (qualities):</h2>
</div>
</div>
<div id="qualitiesWrapper" class="col">
</div>
</div>
<p class="normal" id="warning-container" style="display:none">
<strong>warning: </strong><span id="warning" style="color:orange"></span>
</p>
<p class="normal" id="group-container" style="display:none">
<strong>group: </strong><span id="group">uninitialized</span>
</p>
<p class="normal" id="streams-container" style="display:none">
<strong>streams: </strong><select id="stream-select" onchange="resetPlayer();"></select>
</p>
<!-- <div id="update-source-container" style="display:none">
<p class="normal" style="display:flex">
<strong>server: </strong><input id="inputServer" data-prop="server" style="flex:2" />
</p>
<p class="normal" style="display:flex">
<strong>url: </strong><input id="inputUrl" data-prop="url" style="flex:2" />
</p>
<p class="normal" style="display:flex">
<strong>streamname: </strong><input id="inputStreamname" data-prop="streamname" style="flex:2" />
</p>
<p class="normal" style="display:flex">
<button id="buttonUpdateSource" onclick="updateSource();" style="flex:1">update</button>
</p>
</div> -->
<p class="normal">
<strong>status: </strong><span id="status">uninitialized</span>
</p>
<p class="normal">
<strong>play latency (avg/min/max/dev): </strong><span
id="bufferTimeDelay">0.0</span><span>/</span><span id="bufferTimeDelayMin">0.0</span><span>/</span><span
id="bufferTimeDelayMax">0.0</span><span>/</span><span id="bufferTimeDelayDeviation">0.00</span><br />
</p>
<p class="normal">
<strong>current time: </strong><span id="currentTime">0.0</span><br />
</p>
<p class="normal">
<strong>play time (start/end): </strong><span id="playTimeStart">0.0</span><span>/</span><span
id="playTimeEnd">0.0</span>
</p>
<p class="normal">
<strong>buffer time (start/end): </strong><span id="bufferTimeStart">0.0</span><span>/</span><span
id="bufferTimeEnd">0.0</span>
</p>
<p class="normal">
<strong>bitrate (avg/min/max/dev): </strong><span id="bitrateAvg">0 kBps</span><span>/</span><span
id="bitrateMin">0 kBps</span><span>/</span><span id="bitrateMax">0 kBps</span><span>/</span><span
id="bitrateDeviation">0 kBps</span>
</p>
<p class="normal">
<strong>framerate (current/avg/min/max/dev): </strong><span id="framerateCurrent">0
kBps</span><span>/</span><span id="framerateAvg">0 kBps</span><span>/</span><span id="framerateMin">0
kBps</span><span>/</span><span id="framerateMax">0 kBps</span><span>/</span><span
id="framerateDeviation">0 kBps</span>
</p>
<p class="normal">
<strong>deviationOfMean (dev): </strong><span id="adaptiveBufferTimeDelayDeviation">0.0</span>
</p>
<p class="normal" id="error-container" style="display:none">
<strong>error: </strong><span id="error" style="color:orangered"></span>
</p>
<div id="code-snippet-container" style="display:none">
<strong>code snippet</strong>
<pre id="code-snippet"></pre>
</div>
<p class="normal">
<strong>documentation: </strong><a href="../docs/nanoplayer/index.html" target="_blank">open api
documentation</a>
</p>
<p class="normal">
<strong>log: </strong><br />
<span id="log"></span>
</p>
</div>
<script src="./js/nanoplayer-custom-config.js"></script>
<script src="./js/nanoplayer-helper.js"></script>
<script src="./js/nanoplayer-helper-events.js"></script>
<script src="./js/nanoplayer-helper-multistream.js"></script>
<script src="./js/nanoplayer-helper-codesnippet.js"></script>
<script src="./js/nanoplayer-metrics-config.js"></script>
<script src="./js/multi/nano.bintu.js"></script>
<script src="./js/nanoplayer/nanoplayer.4.14.1.min.js"></script>
<script>
var playerVersion = '4.14.1';
console.log('nanoplayer html version: ' + playerVersion);
document.getElementById('demo-version').innerText = 'version ' + playerVersion;
var getPlayerLocation = function () {
return '//demo.nanocosmos.de/nanoplayer/api/release/nanoplayer.4.min.js?20220316';
};
var player;
document.addEventListener('DOMContentLoaded', function () {
var cap = NanoPlayer.capabilities;
!cap.length ? console.log('no supported player tech') : cap.forEach(function (c) { console.log('supported player tech: ' + c) });
init();
});
</script>
<script>
function updateStreamQualityWrapper(streamInfo) {
if (this.config.source.entries) {
var streams = this.config.source.entries;
var qualitiesWrapper = document.getElementById("qualitiesWrapper");
var currentStreamname = streamInfo.rtmp.streamname;
var currentQualityIndex = getCurrentQualityIndex(currentStreamname);
removeAllChildNodes(qualitiesWrapper);
for (var i = 0; i < streams.length; i++) {
var qualityWrapper = document.createElement("div");
qualityWrapper.classList.add("streamQualityWrapper");
qualityWrapper.style.cursor = 'pointer';
var streamIndex = document.createElement("div");
streamIndex.textContent = (i + 1) + ".";
qualityWrapper.classList.add("row")
qualityWrapper.appendChild(streamIndex);
if (i === currentQualityIndex) {
streamIndex.classList.add("streamIndex--active");
var streamDetailsWrapper = document.createElement("div");
streamDetailsWrapper.classList.add("col-12");
// TITLE
var streamDetailsTitle = document.createElement("div");
streamDetailsTitle.classList.add("row");
// add streamname
var labelStreamname = document.createElement("div");
labelStreamname.textContent = streams[i].h5live.rtmp.streamname;
labelStreamname.classList.add("labelStreamname");
labelStreamname.classList.add("labelStreamname--active");
streamDetailsTitle.appendChild(labelStreamname);
// SUBTITLE
var streamDetailsSubtitle = document.createElement("div");
streamDetailsSubtitle.classList.add("col");
// add bitrate
var labelBitrate = document.createElement("div");
var bitrate = streams[i] && streams[i].info && streams[i].info.bitrate ? streams[i].info.bitrate : "N/A";
labelBitrate.textContent = "Bitrate: " + bitrate + (bitrate != "N/A" ? " kbit/s" : "");
labelBitrate.classList.add("labelBitrate");
streamDetailsSubtitle.appendChild(labelBitrate);
// add resolution
if (streamInfo.videoInfo) {
var labelResolution = document.createElement("div");
var resolution = streamInfo.videoInfo.width && streamInfo.videoInfo.height ? (streamInfo.videoInfo.width + "x" + streamInfo.videoInfo.height) : (streams[i] && streams[i].info && streams[i].info.width && streams[i].info.height ? (streams[i].info.width + "x" + streams[i].info.height) : "N/A");
labelResolution.textContent = "Resolution: " + resolution;
labelResolution.classList.add("labelResolution");
streamDetailsSubtitle.appendChild(labelResolution);
}
// add framerate
if (streamInfo.videoInfo) {
var labelFrameRate = document.createElement("div");
var frameRate = streamInfo.videoInfo.frameRate ? streamInfo.videoInfo.frameRate : (streams[i] && streams[i].info && streams[i].info.frameRate ? streams[i].info.frameRate : "N/A");
labelFrameRate.textContent = "Framerate: " + frameRate + (frameRate != "N/A" ? " fps" : "");
labelFrameRate.classList.add("labelFrameRate");
streamDetailsSubtitle.appendChild(labelFrameRate);
}
streamDetailsWrapper.appendChild(streamDetailsTitle);
streamDetailsWrapper.appendChild(streamDetailsSubtitle);
qualityWrapper.appendChild(streamDetailsWrapper);
} else {
streamIndex.classList.add("streamIndex");
var labelStreamname = document.createElement("div");
labelStreamname.textContent = streams[i].h5live.rtmp.streamname;
labelStreamname.classList.add("labelStreamname");
qualityWrapper.appendChild(labelStreamname);
}
qualityWrapper.addEventListener('click', function (index) {
if (player) {
player.switchStream(index).then(function(_config){
log('switchStream initialized');
}, function error(_err) {
log('switchStream aborted with error ' + _err.code + ': ' + _err.message);
});
}
}.bind(null, i))
qualitiesWrapper.appendChild(qualityWrapper);
}
}
}
function extractStreamname(streamInfoUrl) {
var prefix = "stream=";
var cutOff = streamInfoUrl.substring(streamInfoUrl.indexOf(prefix));
var streamname = cutOff.substring(prefix.length, cutOff.indexOf("&"));
return window.decodeURIComponent(streamname);
}
function getCurrentQualityIndex(streamname) {
var currentQualityIndex = 0;
if (this.config.source.entries) {
var entries = this.config.source.entries;
for (var i = 0; i < entries.length; i++) {
if (entries[i].h5live.rtmp.streamname === streamname) {
currentQualityIndex = i;
break;
}
}
}
return currentQualityIndex;
}
function getMaxQualityIndex() {
return config.source.entries ? config.source.entries.length - 1 : 0;
}
function removeAllChildNodes(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
}
function onSetAdaption(e) {
var select = document.getElementById('switch-rule');
var rule = select.options[select.selectedIndex].value;
select = document.getElementById('adaption-downstep');
var downStep = parseInt(select.options[select.selectedIndex].value, 10) || 1;
if (player) {
player.setAdaption({
'rule': rule,
'downStep': downStep
});
}
}
function setAdaption(_rule) {
document.querySelector('#switch-rule option[value="' + _rule + '"]').selected = true;
}
</script>
<script>
var buffering = {
start: 0,
end: 0
};
var bintu, bintuQ;
var metaDataTimeout = 0;
var forceAutoplay = null;
var forceMuted = null;
var searchRefreshInterval = 0;
var config = {
source: {},
playback: {
autoplay: true,
automute: true,
muted: true
},
events: {},
style: {
displayMutedAutoplay: true
}
};
events = events || {};
events.onReady = function (e) {
log('onReady');
var source = e.data.config.source;
updateStreamQualityWrapper(source.entries[+source.startIndex].h5live);
};
events.onMute = function (e) {
log('onMute');
var buttonMute = document.getElementById('buttonMute');
buttonMute.dataset.muted = true;
buttonMute.innerText = 'unmute';
};
events.onUnmute = function (e) {
log('onUnmute');
var buttonMute = document.getElementById('buttonMute');
buttonMute.dataset.muted = false;
buttonMute.innerText = 'mute';
};
events.onVolumeChange = function (e) {
log('onVolumeChange ' + e.data.volume * 100);
var volumeFader = document.getElementById('faderVolume'), volumeOutput = document.getElementById('volume'), volume = Math.round(e.data.volume * 100);
(parseInt(volumeFader.value) !== volume) && (volumeFader.value = volume);
volumeOutput.value = volume + '%';
};
function init() {
document.querySelector('#faderVolume').value = 100;
var doStartPlayer = getNanoPlayerParameters();
if (doStartPlayer) {
startPlayer(config);
}
}
function resetPlayer() {
var select = document.getElementById('stream-select');
if (!select.options[select.selectedIndex]) {
return;
}
var selectedValue = select.options[select.selectedIndex].value;
if (!!streamObj) {
if (streamObj.streamname === selectedValue) {
console.log('already playing this stream');
return;
}
}
var i, len = streamObjs.length;
for (i = 0; i < len; i += 1) {
if (streamObjs[i].streamname === selectedValue)
streamObj = streamObjs[i];
}
var h5liveQ = {};
h5liveQ.rtmp = {};
h5liveQ.rtmp.url = streamObj.url;
h5liveQ.rtmp.streamname = streamObj.streamname;
h5liveQ.server = {};
h5liveQ.server.websocket = streamObj.server.websocket;
h5liveQ.server.hls = streamObj.server.hls;
h5liveQ.server.progressive = streamObj.server.progressive;
config.source.h5live = h5liveQ;
checkSecurity();
if (!player) {
startPlayer();
} else {
updateSource();
}
}
function startPlayer(config, reset) {
if (!player) {
player = new NanoPlayer('playerDiv');
}
config = config || this.config;
for (var event in events) {
if (Object.prototype.hasOwnProperty.call(events, event)) {
player.on(event.substr(2), events[event]);
}
}
var toString = Object.prototype.toString;
function deepCopy(obj) {
var rv;
switch (typeof obj) {
case 'object':
if (obj === null) {
// null => null
rv = null;
}
else {
switch (toString.call(obj)) {
case '[object Array]':
// It's an array, create a new array with
// deep copies of the entries
rv = obj.map(deepCopy);
break;
case '[object Date]':
// Clone the date
rv = new Date(obj);
break;
case '[object RegExp]':
// Clone the RegExp
rv = new RegExp(obj);
break;
// ...probably a few others
default:
// Some other kind of object, deep-copy its
// properties into a new object
rv = Object.keys(obj).reduce(function (prev, key) {
prev[key] = deepCopy(obj[key]);
return prev;
}, {});
break;
}
}
break;
default:
// It's a primitive, copy via assignment
rv = obj;
break;
}
return rv;
}
if (typeof customConfig !== 'undefined') {
this.config = customConfig;
} else if (typeof config !== 'undefined') {
//var mobile = /(Android|iPhone|iPad|iPod)/.test(navigator.userAgent);
//config.playback.autoplay = (!mobile && config.playback.autoplay);
if (forceAutoplay !== null) {
config.playback.autoplay = forceAutoplay;
}
if (forceMuted !== null) {
config.playback.muted = forceMuted;
}
this.config = config;
}
if (window.nanoPlayerMetricsConfig) {
this.config.metrics = window.nanoPlayerMetricsConfig;
}
var _config = deepCopy(this.config);
if (reset && getHTTPParam('reset') === 'update') {
player.updateSource(_config.source)
.then(function (__config) {
log('UpdateSource Call Success');
})
.catch(function (error) {
log('UpdateSource Error: ' + JSON.stringify(error));
});
}
else {
var buttonMute = document.getElementById('buttonMute');
buttonMute.dataset.muted = _config.playback.muted;
buttonMute.innerText = _config.playback.muted ? 'unmute' : 'mute';
createCodeSippet(_config);
// updateStreamQualityWrapper();
player.setup(_config).then(function (conf) {
this.config.source = conf.source;
setVolume(null, document.getElementById('faderVolume').value);
checkConfig(conf);
document.querySelector('#switch-rule option[value="' + conf.source.options.adaption.rule + '"]').selected = true;
for (var i = 1; i < conf.source.entries.length; i += 1) {
var option = document.createElement('option');
option.value = i;
option.textContent = i;
document.querySelector('#adaption-downstep').appendChild(option);
}
document.querySelector('#adaption-downstep option[value="' + conf.source.options.adaption.downStep + '"]').selected = true;
try {
conf = JSON.stringify(conf)
} catch (err) { }
log('setup ok: ' + conf);
player.on("StreamInfo", function (e) {
var streamname = e.data.streamInfo.rtmp.streamname;
console.log(streamname);
updateStreamQualityWrapper(e.data.streamInfo);
});
player.on("SwitchStreamInit", function (e) {
setAdaption(e.data.rule);
});
player.on("UpdateSourceInit", function (e) {
setAdaption(e.data.rule);
});
}.bind(this), function (error) {
if (error.message) {
error = error.message;
} else {
try {
var err = JSON.stringify(error);
if (err === '{}') {
err = error.message;
}
error = err;
} catch (err) { }
}
log('Setup Error: ' + error);
document.getElementById('status').innerText = 'Setup Error: ' + error;
});
}
}
function requestFullscreenPlayer() {
if (player) {
player.requestFullscreen()
.then(function (){
log('requestFullscreen resolved');
})
.catch(function(err) {
log('requestFullscreen rejected: ' + err.reason);
});
}
}
function checkConfig(conf) {
if (document.getElementById('demo-version').innerText !== 'version ' + player.version) {
alert('The page has been updated during the last few minutes! Please empty your cache!');
}
document.getElementById('demo-version').innerHTML = '<a href="' + document.location.href + '" target="_blank">version ' + player.version + '</a>';
}
function play() {
player.play();
}
function pause() {
player.pause();
}
function mute(e) {
var muted = (e.dataset.muted === 'true') ? true : false;
if (muted) {
player.unmute();
} else {
player.mute();
}
}
function setVolume(e, value) {
if (!player) {
return;
}
if (value < 0) {
value = 0;
} else if (value > 100) {
value = 100;
}
var volume = value / 100;
player.setVolume(volume);
}
function updateSource() {
if (!player) {
return;
}
player.updateSource(this.config.source).then(function (conf) {
this.config.source = conf;
checkConfig(conf);
try {
conf = JSON.stringify(conf);
} catch (err) { }
log('update source ok: ' + conf);
}.bind(this), function (error) {
if (error.message) {
error = error.message;
} else {
try {
var err = JSON.stringify(error);
if (err === '{}') {
err = error.message;
}
error = err;
} catch (err) { }
}
log('update source error: ' + error);
document.getElementById('status').innerText = 'Update Source Error: ' + error;
});
}
</script>
</body>
</html>