-
Notifications
You must be signed in to change notification settings - Fork 46
/
player.js
114 lines (108 loc) · 3.54 KB
/
player.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
var hls;
var debug;
var recoverDecodingErrorDate,recoverSwapAudioCodecDate;
var pendingTimedMetadata = [];
function handleMediaError(hls) {
var now = performance.now();
if(!recoverDecodingErrorDate || (now - recoverDecodingErrorDate) > 3000) {
recoverDecodingErrorDate = performance.now();
var msg = "trying to recover from media Error ..."
console.warn(msg);
hls.recoverMediaError();
} else {
if(!recoverSwapAudioCodecDate || (now - recoverSwapAudioCodecDate) > 3000) {
recoverSwapAudioCodecDate = performance.now();
var msg = "trying to swap Audio Codec and recover from media Error ..."
console.warn(msg);
hls.swapAudioCodec();
hls.recoverMediaError();
} else {
var msg = "cannot recover, last media error recovery failed ..."
console.error(msg);
}
}
}
function handleTimedMetadata(event, data) {
for (var i = 0; i < data.samples.length; i++) {
var pts = data.samples[i].pts;
var str = new TextDecoder('utf-8').decode(data.samples[i].data.subarray(22));
pendingTimedMetadata.push({pts: pts, value: str});
}
}
function timeUpdateCallback() {
if (pendingTimedMetadata.length == 0 || pendingTimedMetadata[0].pts > video.currentTime) {
return;
}
var e = pendingTimedMetadata[0];
pendingTimedMetadata = pendingTimedMetadata.slice(1);
console.log('Metadata ' + e.value + " at " + e.pts + "s");
}
function playM3u8(url){
var video = document.getElementById('video');
if(native){
video.classList.add("native_mode");
video.classList.remove("zoomed_mode");
} else {
video.classList.remove("native_mode");
video.classList.add("zoomed_mode");
}
if(hls){ hls.destroy(); }
var config = {
debug:debug,
xhrSetup: function (xhr, url) {
xhr.withCredentials = true; // do send cookies
},
fetchSetup: function (context, initParams) {
// Always send cookies, even for cross-origin calls.
initParams.credentials = 'include';
return new Request(context.url, initParams);
},
}
hls = new Hls(config);
hls.on(Hls.Events.ERROR, function(event,data) {
var msg = "Player error: " + data.type + " - " + data.details;
console.error(msg);
if(data.fatal) {
switch(data.type) {
case Hls.ErrorTypes.MEDIA_ERROR:
handleMediaError(hls);
break;
case Hls.ErrorTypes.NETWORK_ERROR:
console.error("network error ...");
break;
default:
console.error("unrecoverable error");
hls.destroy();
break;
}
}
});
var m3u8Url = decodeURIComponent(url)
hls.loadSource(m3u8Url);
hls.attachMedia(video);
video.ontimeupdate = timeUpdateCallback;
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
hls.on(Hls.Events.FRAG_PARSING_METADATA, handleTimedMetadata);
document.title = url
}
chrome.storage.local.get({
hlsjs: currentVersion,
debug: false,
native: false
}, function(settings) {
debug = settings.debug;
native = settings.native;
var s = document.createElement('script');
var version = currentVersion
if (supportedVersions.includes(settings.hlsjs)) {
version = settings.hlsjs
}
s.src = chrome.runtime.getURL('hlsjs/hls.'+version+'.min.js');
s.onload = function() { playM3u8(window.location.href.split("#")[1]); };
(document.head || document.documentElement).appendChild(s);
});
$(window).bind('hashchange', function() {
playM3u8(window.location.href.split("#")[1]);
});