hls-player-js/hls-player.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-08-22 15:44:32 +05:00
var player = document.getElementById('vid1');
class PlaylistLoader {
constructor() {
this.playlist_src = '/list/' + window.location.pathname.slice(6);
this.last_segment = null;
this.refresh_interval = null;
this.new_segments = [];
2023-08-23 11:55:04 +05:00
this.fetch_playlist();
2023-08-22 15:44:32 +05:00
}
2023-08-22 18:20:09 +05:00
async fetch_playlist() {
const response = await fetch(this.playlist_src);
this.parse_playlist(await response.text());
setTimeout(this.fetch_playlist.bind(this), this.refresh_interval * 1000);
2023-08-22 15:44:32 +05:00
}
2023-08-22 18:20:09 +05:00
parse_playlist(playlist_content) {
let lines = playlist_content.split('\n');
let segments = [];
let segment_block_flag = this.last_segment === null ? false : true;
for (let i = 0; i < lines.length; i++) {
2023-08-22 18:20:09 +05:00
if (lines[i].startsWith("#")) {
if (lines[i].startsWith("EXT-X-TARGETDURATION", 1)) {
this.refresh_interval = parseFloat(lines[i].split(':')[1]);
}
} else {
if (segment_block_flag || lines[i] == '') {
if (lines[i] == this.last_segment) {
segment_block_flag = false;
}
} else {
segments.push(lines[i]);
}
}
}
this.new_segments = segments;
this.last_segment = segments.at(-1);
2023-08-23 11:55:04 +05:00
}
2023-08-22 15:44:32 +05:00
}
let test = new PlaylistLoader();