Rewrite WS+MP4 format to keyframes stream

This commit is contained in:
Alexey Khit
2023-01-15 00:12:26 +03:00
parent 39662e10af
commit a1a73f7b45
3 changed files with 28 additions and 33 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ func handlerWSMP4(tr *api.Transport, msg *api.Message) error {
return errors.New(api.StreamNotFound) return errors.New(api.StreamNotFound)
} }
cons := &mp4.Segment{} cons := &mp4.Segment{OnlyKeyframe: true}
if codecs, ok := msg.Value.(string); ok { if codecs, ok := msg.Value.(string); ok {
log.Trace().Str("codecs", codecs).Msgf("[mp4] new WS/MP4 consumer") log.Trace().Str("codecs", codecs).Msgf("[mp4] new WS/MP4 consumer")
+24 -32
View File
@@ -228,16 +228,12 @@ export class VideoRTC extends HTMLElement {
this.video.playsInline = true; this.video.playsInline = true;
this.video.preload = "auto"; this.video.preload = "auto";
this.appendChild(this.video);
// important for second video for mode MP4
this.style.display = "block";
this.style.position = "relative";
this.video.style.display = "block"; // fix bottom margin 4px this.video.style.display = "block"; // fix bottom margin 4px
this.video.style.width = "100%"; this.video.style.width = "100%";
this.video.style.height = "100%" this.video.style.height = "100%"
this.appendChild(this.video);
if (this.background) return; if (this.background) return;
if ("hidden" in document && this.visibilityCheck) { if ("hidden" in document && this.visibilityCheck) {
@@ -547,46 +543,42 @@ export class VideoRTC extends HTMLElement {
onmjpeg() { onmjpeg() {
this.ondata = data => { this.ondata = data => {
this.video.controls = false;
this.video.poster = "data:image/jpeg;base64," + VideoRTC.btoa(data); this.video.poster = "data:image/jpeg;base64," + VideoRTC.btoa(data);
}; };
this.send({type: "mjpeg"}); this.send({type: "mjpeg"});
this.video.controls = false;
} }
onmp4() { onmp4() {
/** @type {HTMLVideoElement} */ /** @type {HTMLCanvasElement} **/
let video2; const canvas = document.createElement("canvas");
/** @type {CanvasRenderingContext2D} */
let context;
this.ondata = data => { /** @type {HTMLVideoElement} */
// first video with default position (set container size) const video2 = document.createElement("video");
// second video with position=absolute and top=0px video2.autoplay = true;
if (video2) { video2.muted = true;
this.removeChild(this.video);
this.video.src = ""; video2.addEventListener("loadeddata", ev => {
this.video = video2; if (!context) {
video2.style.position = ""; canvas.width = video2.videoWidth;
video2.style.top = ""; canvas.height = video2.videoHeight;
context = canvas.getContext('2d');
} }
video2 = this.video.cloneNode(); context.drawImage(video2, 0, 0, canvas.width, canvas.height);
video2.style.position = "absolute";
video2.style.top = "0px";
this.appendChild(video2);
video2.src = "data:video/mp4;base64," + VideoRTC.btoa(data); this.video.controls = false;
video2.play().catch(() => console.log); this.video.poster = canvas.toDataURL("image/jpeg");
};
this.ws.addEventListener("close", () => {
if (!video2) return;
this.removeChild(video2);
video2.src = "";
}); });
this.ondata = data => {
video2.src = "data:video/mp4;base64," + VideoRTC.btoa(data);
};
this.send({type: "mp4", value: this.codecs("mp4")}); this.send({type: "mp4", value: this.codecs("mp4")});
this.video.controls = false;
} }
static btoa(buffer) { static btoa(buffer) {
+3
View File
@@ -22,6 +22,9 @@ class VideoStream extends VideoRTC {
this.innerHTML = ` this.innerHTML = `
<style> <style>
video-stream {
position: relative;
}
.info { .info {
position: absolute; position: absolute;
top: 0; top: 0;