2023-08-22 15:44:32 +05:00
|
|
|
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
|
|
|
|
2023-08-23 15:43:29 +05:00
|
|
|
async fetch_playlist() {
|
|
|
|
const response = await fetch(this.playlist_src);
|
2023-08-24 12:53:22 +05:00
|
|
|
if (response.status == 200) {
|
|
|
|
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) {
|
2023-08-23 15:43:29 +05:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 20:58:37 +05:00
|
|
|
this.new_segments = this.new_segments.concat(segments);
|
|
|
|
if (segments.length != 0) {
|
|
|
|
this.last_segment = segments.at(-1);
|
|
|
|
}
|
2023-08-23 11:55:04 +05:00
|
|
|
}
|
2023-08-22 15:44:32 +05:00
|
|
|
}
|
|
|
|
|
2023-08-23 16:31:57 +05:00
|
|
|
class VideoLoader {
|
2023-08-23 20:58:37 +05:00
|
|
|
constructor(vid_tag_id) {
|
2023-08-23 16:31:57 +05:00
|
|
|
this.stream_key = window.location.pathname.slice(6);
|
|
|
|
this.playlist_loader = new PlaylistLoader();
|
2023-08-23 20:58:37 +05:00
|
|
|
this.player = document.getElementById(vid_tag_id);
|
2023-08-23 16:31:57 +05:00
|
|
|
this.media_source = new MediaSource();
|
2023-08-23 23:23:51 +05:00
|
|
|
this.media_source.addEventListener('sourceopen', this.prepare_buffer.bind(this));
|
|
|
|
this.player.src = window.URL.createObjectURL(this.media_source);
|
2023-08-23 16:31:57 +05:00
|
|
|
}
|
|
|
|
|
2023-08-23 23:23:51 +05:00
|
|
|
async prepare_buffer(_) {
|
2023-08-23 16:31:57 +05:00
|
|
|
let init_frag = await this.fetch_video('/vid/' + this.stream_key + '/init.mp4');
|
2023-08-23 20:58:37 +05:00
|
|
|
let mime = (new MP4Tree(init_frag)).get_mime();
|
2023-08-23 23:23:51 +05:00
|
|
|
if (!MediaSource.isTypeSupported(mime)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.media_buffer = this.media_source.addSourceBuffer(mime);
|
|
|
|
this.media_buffer.mode = 'segments';
|
|
|
|
this.media_buffer.appendBuffer(init_frag);
|
2023-08-24 12:53:22 +05:00
|
|
|
await this.fetch_new_segments();
|
|
|
|
this.resync_video();
|
|
|
|
this.cleanup_old();
|
|
|
|
}
|
|
|
|
|
|
|
|
async cleanup_old() {
|
|
|
|
let timeout = 60000;
|
|
|
|
if (!this.media_buffer.updating) {
|
|
|
|
this.media_buffer.remove(0, this.media_buffer.buffered.end(0) - 10);
|
|
|
|
} else {
|
|
|
|
timeout = 250;
|
|
|
|
}
|
|
|
|
setTimeout(this.cleanup_old.bind(this), timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async resync_video() {
|
|
|
|
if (!this.media_buffer.updating) {
|
|
|
|
const buffer_end = this.media_buffer.buffered.end(0);
|
|
|
|
if (buffer_end - this.player.currentTime > 15) {
|
|
|
|
this.player.currentTime = buffer_end - 5;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 00:11:09 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetch_new_segments() {
|
|
|
|
while (this.playlist_loader.new_segments.length > 0) {
|
2023-08-24 12:53:22 +05:00
|
|
|
if (!this.media_buffer.updating) {
|
|
|
|
let segment_uri = this.playlist_loader.new_segments.shift();
|
|
|
|
let segment_stream = await this.fetch_video(segment_uri);
|
|
|
|
this.media_buffer.appendBuffer(segment_stream);
|
|
|
|
}
|
|
|
|
await new Promise(r => setTimeout(r, 250));
|
2023-08-24 00:11:09 +05:00
|
|
|
}
|
|
|
|
setTimeout(this.fetch_new_segments.bind(this), this.playlist_loader.refresh_interval * 1000);
|
2023-08-23 16:31:57 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetch_video(uri) {
|
|
|
|
const response = await fetch(uri);
|
2023-08-23 20:58:37 +05:00
|
|
|
return response.arrayBuffer();
|
2023-08-23 16:31:57 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|