From 33a01c9552db97087d6eae56681c60227474dde3 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Wed, 23 Aug 2023 20:58:37 +0500 Subject: [PATCH] move codec parsing to separate file, fix concat assignment --- hls-player.js | 20 ++++++++------------ mp4-tree.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 mp4-tree.js diff --git a/hls-player.js b/hls-player.js index ac452af..5f55aa9 100644 --- a/hls-player.js +++ b/hls-player.js @@ -1,5 +1,3 @@ -var player = document.getElementById('vid1'); - class PlaylistLoader { constructor() { this.playlist_src = '/list/' + window.location.pathname.slice(6); @@ -34,32 +32,30 @@ class PlaylistLoader { } } } - this.new_segments.concat(segments); - this.last_segment = segments.at(-1); + this.new_segments = this.new_segments.concat(segments); + if (segments.length != 0) { + this.last_segment = segments.at(-1); + } } } class VideoLoader { - constructor() { + constructor(vid_tag_id) { this.stream_key = window.location.pathname.slice(6); this.playlist_loader = new PlaylistLoader(); - this.player = document.getElementById('vid1'); + this.player = document.getElementById(vid_tag_id); this.media_source = new MediaSource(); this.prepare_buffer(); } async prepare_buffer() { let init_frag = await this.fetch_video('/vid/' + this.stream_key + '/init.mp4'); - fetch_mime(init_frag); + let mime = (new MP4Tree(init_frag)).get_mime(); } async fetch_video(uri) { const response = await fetch(uri); - fetch_mime(new Uin8Array(response.arrayBuffer())); + return response.arrayBuffer(); } } -function fetch_mime(mp4_byte_buffer) { -} - -let test = new VideoLoader(); diff --git a/mp4-tree.js b/mp4-tree.js new file mode 100644 index 0000000..61b3089 --- /dev/null +++ b/mp4-tree.js @@ -0,0 +1,49 @@ +DataView.prototype.getString = function(offset, length) { + text = ''; + let end = offset + length; + for (let i = offset; i < end; i++) { + text += String.fromCharCode(this.getUint8(i)); + } + return text; +} + + +class MP4Tree { + constructor(data) { + this.data = new DataView(data); + this.codecs = []; + this.idx = 0; + this.parse_codecs(); + } + + parse_codecs() { + this.parse_until('moov'); + console.log(this.idx); + } + + get_mime() { + return 'video/mp4; codecs="' + this.get_codec_string() + '"'; + } + + get_codec_string() { + return this.codecs.join(', '); + } + + parse_until(name) { + let curr_head = null; + while (this.idx < this.data.byteLength) { + curr_head = this.read_next_head(); + console.log(curr_head); + if (curr_head.name == name) { + break; + } else { + this.idx += curr_head.len; + } + } + } + + read_next_head() { + return {"len": this.data.getUint32(this.idx), "name": this.data.getString(this.idx + 4, 4)}; + } +} +