Moved the getTracksFromStream functions to sfin.js, as this will allow you to retrieve tracks from recorded events. (MonitorStream.js)

Also: If any error occurs during execution of "appendMseBuffer" (not just 'QuotaExceededError'), restart the thread
This commit is contained in:
IgorA100
2026-02-15 21:53:16 +03:00
committed by Isaac Connor
parent f8fb93d784
commit 72f0bee2d7

View File

@@ -528,7 +528,7 @@ function MonitorStream(monitorData) {
this.hls.on(Hls.Events.MEDIA_ATTACHED, function(event, data) {
console.log(`Video and hls.js are now bound together for monitor ID=${this.id}`);
this.updateStreamInfo('', ''); //HLS
this.getTracksFromStream(); //HLS
getTracksFromStream(this); //HLS
}, this);
this.hls.loadSource(hlsUrl.href);
this.hls.attachMedia(stream);
@@ -1108,49 +1108,6 @@ function MonitorStream(monitorData) {
}
};
/*IMPORTANT DO NOT CALL WITHOUT CONSCIOUS NEED!!!*/
// https://habr.com/ru/companies/timeweb/articles/667148/
this.getTracksFromStream = async function() {
this.mediaStream = this.audioTrack = this.videoTrack = null;
let streamCaptureNotSupported = false;
const el = (-1 !== this.activePlayer.indexOf('go2rtc')) ? document.querySelector('[id ^= "liveStream'+this.id+'"] video') : this.getElement();
let stream = null;
// We should NOT call captureStream again, as there may be problems with capturing the stream!
let moz = false; // Detecting Firefox
if ("captureStream" in el) {
stream = await el.captureStream();
} else if ("mozCaptureStreamUntilEnded" in el) {
stream = await el.mozCaptureStreamUntilEnded();
moz = true;
} else {
console.warn(`"captureStream" NOT found in STREAM for monitor ID=${this.id} or not supported by the browser.`);
streamCaptureNotSupported = true; // This will enable the volume control if the browser does not support captureStream (for example, Safari)
}
if (stream) {
this.audioTrack = stream.getAudioTracks()[0];
this.videoTrack = stream.getVideoTracks()[0];
this.mediaStream = stream;
if (moz && this.audioTrack) {
// Fix Firefox https://stackoverflow.com/questions/72401396/usage-of-mozcapturestream-stop-audio-output-of-video-element
const ctx = new AudioContext();
const dest = ctx.createMediaStreamSource(stream);
dest.connect(ctx.destination);
}
} else if (!streamCaptureNotSupported) {
console.warn(`Failed to capture stream for monitor ID=${this.id} while receiving tracks.`);
}
console.debug(`mediaStream for ID=${this.id}:`, this.mediaStream);
console.debug(`audioTrack for ID=${this.id}:`, this.audioTrack);
console.debug(`videoTrack for ID=${this.id}:`, this.videoTrack);
(this.audioTrack || streamCaptureNotSupported) ? this.volumeControlsHandler('enable') : this.volumeControlsHandler('disable');
//this.connectAudioMotion();
};
this.setStateClass = function(jobj, stateClass) {
if (!jobj) {
console.log("No obj in setStateClass");
@@ -1787,7 +1744,7 @@ async function attachVideo(monitorStream) {
monitorStream.restart();
}
monitorStream.updateStreamInfo('', ''); //JANUS
monitorStream.getTracksFromStream(); //JANUS
getTracksFromStream(monitorStream); //JANUS
}
},
onremotetrack: function(track, mid, on) {
@@ -1987,7 +1944,7 @@ function startRTSP2WebPlay(videoEl, url, stream) {
const webrtcSendChannel = stream.webrtc.createDataChannel('rtsptowebSendChannel');
webrtcSendChannel.onopen = (event) => {
stream.updateStreamInfo('', ''); //WEBRTC
stream.getTracksFromStream(); //WEBRTC
getTracksFromStream(stream); //WEBRTC
console.log(`${webrtcSendChannel.label} for camera ID=${stream.id} has opened`);
webrtcSendChannel.send('ping');
};
@@ -2082,7 +2039,7 @@ function startMsePlay(context, videoEl, url) {
context.mse = new MediaSource();
videoEl.onplay = (event) => {
context.updateStreamInfo('', ''); //MSE
context.getTracksFromStream(); //MSE
getTracksFromStream(context); //MSE
context.streamStartTime = (Date.now() / 1000).toFixed(2);
if (videoEl.buffered.length > 0 && videoEl.currentTime < videoEl.buffered.end(videoEl.buffered.length - 1) - 0.1) {
//For example, after a pause you press Play, you need to adjust the time.
@@ -2174,12 +2131,11 @@ function appendMseBuffer(packet, context) {
secondsInBuffer = (videoEl.buffered.end(videoEl.buffered.length - 1) - videoEl.buffered.start(videoEl.buffered.length - 1)).toFixed(2);
}
console.warn(`${dateTimeToISOLocal(new Date())} Restarting stream due to an error adding data to the buffer '${secondsInBuffer}'sec., and length = ${videoEl.buffered.length} for ID=${context.id}`, e);
// The client's browser needs to rest 1000ms.
context.restart(context.currentChannelStream, 1000);
} else {
console.warn(`${dateTimeToISOLocal(new Date())} Error adding buffer to ID=${context.id}.`, e);
throw e;
//throw e;
}
// The client's browser needs to rest 1000ms.
context.restart(context.currentChannelStream, 1000);
}
}