playlist early stop, old segment deletion + resyncing + async fixes

This commit is contained in:
Muaz Ahmad 2023-08-24 12:53:22 +05:00
parent 85cbf0e335
commit 822b84b3bc

View file

@ -9,9 +9,11 @@ class PlaylistLoader {
async fetch_playlist() { async fetch_playlist() {
const response = await fetch(this.playlist_src); const response = await fetch(this.playlist_src);
if (response.status == 200) {
this.parse_playlist(await response.text()); this.parse_playlist(await response.text());
setTimeout(this.fetch_playlist.bind(this), this.refresh_interval * 1000); setTimeout(this.fetch_playlist.bind(this), this.refresh_interval * 1000);
} }
}
parse_playlist(playlist_content) { parse_playlist(playlist_content) {
let lines = playlist_content.split('\n'); let lines = playlist_content.split('\n');
@ -58,15 +60,39 @@ class VideoLoader {
this.media_buffer = this.media_source.addSourceBuffer(mime); this.media_buffer = this.media_source.addSourceBuffer(mime);
this.media_buffer.mode = 'segments'; this.media_buffer.mode = 'segments';
this.media_buffer.appendBuffer(init_frag); this.media_buffer.appendBuffer(init_frag);
this.fetch_new_segments(); 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;
}
}
} }
async fetch_new_segments() { async fetch_new_segments() {
while (this.playlist_loader.new_segments.length > 0) { while (this.playlist_loader.new_segments.length > 0) {
if (!this.media_buffer.updating) {
let segment_uri = this.playlist_loader.new_segments.shift(); let segment_uri = this.playlist_loader.new_segments.shift();
let segment_stream = await this.fetch_video(segment_uri); let segment_stream = await this.fetch_video(segment_uri);
this.media_buffer.appendBuffer(segment_stream); this.media_buffer.appendBuffer(segment_stream);
} }
await new Promise(r => setTimeout(r, 250));
}
setTimeout(this.fetch_new_segments.bind(this), this.playlist_loader.refresh_interval * 1000); setTimeout(this.fetch_new_segments.bind(this), this.playlist_loader.refresh_interval * 1000);
} }