Fixes for substream.

- Fixed error in destroySubstreamProcess.
Fixed no viewers pushed to viewerList.
- When using "Only When Watching, Use Substream" substreams are now started automatically and the http requests wait's for the output.
- Substream folder is deleted befor new substream starts, so that there can be checked when the s.m3u8 file is created.
- Fixed missing monitorId in FLV and x265 stream path.
merge-requests/521/merge^2
TheNetStriker 2024-10-16 17:13:01 +02:00
parent 9a8fb32f7e
commit bd5b6409cd
4 changed files with 113 additions and 14 deletions

View File

@ -192,11 +192,20 @@ module.exports = (s,config,lang) => {
//`e` is the monitor object
//`x` is an object used to contain temporary values.
const channelStreamDirectory = !isNaN(parseInt(number)) ? `${e.sdir || s.getStreamsDirectory(e)}channel${number}/` : e.sdir
if(channelStreamDirectory !== e.sdir && !fs.existsSync(channelStreamDirectory)){
try{
fs.mkdirSync(channelStreamDirectory)
}catch(err){
// s.debugLog(err)
if(channelStreamDirectory !== e.sdir){
if (fs.existsSync(channelStreamDirectory)) {
try {
fs.rmdirSync(channelStreamDirectory, { recursive: true, force: true })
}catch(err){
// s.debugLog(err)
}
}
if (!fs.existsSync(channelStreamDirectory)) {
try {
fs.mkdirSync(channelStreamDirectory)
}catch(err){
// s.debugLog(err)
}
}
}
const channelNumber = number - config.pipeAddition

View File

@ -27,6 +27,7 @@ module.exports = function(s,config,lang){
getMonitorConfiguration,
copyMonitorConfiguration,
checkObjectsInMonitorDetails,
spawnSubstreamProcess,
} = require('./monitor/utils.js')(s,config,lang)
const {
canAddMoreMonitors,
@ -664,6 +665,38 @@ module.exports = function(s,config,lang){
}
return endData
}
s.getSubstreamWaitTimeout = function (groupId, monitorId) {
const monitorConfig = s.group[groupId].rawMonitorConfigurations[monitorId];
const subStreamType = monitorConfig.details.substream.output.stream_type;
return subStreamType == 'hls'
? (parseInt(monitorConfig.details.substream.output.hls_time) * 1000) + 10000
: 10000;
}
s.toggleSubstreamAndWaitForOutput = async function (groupId, monitorId) {
const monitorConfig = s.group[groupId].rawMonitorConfigurations[monitorId];
const streamType = monitorConfig.details.stream_type;
if (streamType === 'useSubstream') {
const activeMonitor = s.group[groupId].activeMonitors[monitorId];
if (!activeMonitor.subStreamProcess) {
spawnSubstreamProcess(monitorConfig);
}
if (!activeMonitor.subStreamOutputReady) {
const checkTime = 250;
var monitorTimeout = s.getSubstreamWaitTimeout(groupId, monitorId);
return await new Promise((resolve, reject) => {
let totalTime = 0;
const timer = setInterval(function () {
totalTime += checkTime;
if (activeMonitor.subStreamOutputReady || totalTime >= monitorTimeout) {
clearInterval(timer);
resolve(activeMonitor.subStreamOutputReady);
}
}, checkTime);
});
}
}
return false;
}
s.camera = async (selectedMode,e,cn) => {
// e = monitor object
// cn = socket connection or callback or options (depends on function chosen)

View File

@ -287,6 +287,14 @@ module.exports = (s,config,lang) => {
channel: activeMonitor.subStreamChannel
},'GRP_'+groupKey);
}
const sendSubstreamEventActiveMonitor = function(activeMonitor, eventName = 'substream_start'){
s.tx({
f: eventName,
mid: activeMonitor.mid,
ke: activeMonitor.ke,
channel: activeMonitor.subStreamChannel
},'GRP_'+activeMonitor.mid);
}
const spawnSubstreamProcess = function(e){
// e = monitorConfig
try{
@ -387,6 +395,26 @@ module.exports = (s,config,lang) => {
},2000)
}
})
activeMonitor.subStreamOutputReady = false;
if (outputFields.stream_type == 'hls') {
const channelStream = subStreamProcess.spawnargs.at(-1);
activeMonitor.subStreamOutputReadyCheck = setInterval(function () {
if (fs.existsSync(channelStream)) {
activeMonitor.subStreamOutputReady = true;
clearInterval(activeMonitor.subStreamOutputReadyCheck);
}
}, 1000);
} else if (outputFields.stream_type == 'mp4') {
const pipeNumber = activeMonitor.subStreamChannel + config.pipeAddition;
subStreamProcess.stdio[pipeNumber].once('data', (data) => {
activeMonitor.subStreamOutputReady = true;
});
} else {
const pipeNumber = activeMonitor.subStreamChannel + config.pipeAddition;
activeMonitor.emitterChannel[pipeNumber].once('data', (data) => {
activeMonitor.subStreamOutputReady = true;
});
}
activeMonitor.subStreamProcess = subStreamProcess
sendSubstreamEvent(groupKey, monitorId)
return subStreamProcess
@ -407,11 +435,13 @@ module.exports = (s,config,lang) => {
}else if(activeMonitor.subStreamProcess){
activeMonitor.subStreamProcessClosing = true
activeMonitor.subStreamChannel = null;
activeMonitor.subStreamOutputReady = false;
clearInterval(activeMonitor.subStreamOutputReadyCheck);
const closeResponse = await processKill(activeMonitor.subStreamProcess)
response.hadSubStream = true
response.closeResponse = closeResponse
delete(activeMonitor.subStreamProcess)
sendSubstreamEvent(activeMonitor.mid, activeMonitor.ke, 'substream_end')
sendSubstreamEventActiveMonitor(activeMonitor, 'substream_end')
activeMonitor.subStreamProcessClosing = false
}
}catch(err){
@ -462,7 +492,7 @@ module.exports = (s,config,lang) => {
function setActiveViewer(groupKey,monitorId,connectionId,isBeingAdded){
const viewerList = s.group[groupKey].activeMonitors[monitorId].watch;
if(isBeingAdded){
if(viewerList.indexOf(connectionId) > -1)viewerList.push(connectionId);
if(viewerList.indexOf(connectionId) == -1)viewerList.push(connectionId);
}else{
viewerList.splice(viewerList.indexOf(connectionId), 1)
}
@ -811,6 +841,13 @@ module.exports = (s,config,lang) => {
setActiveViewer(groupKey,monitorId,cn.id,true)
activeMonitor.allowDestroySubstream = false
clearTimeout(activeMonitor.noViewerCountDisableSubstream)
if (e.monitorTimeout) {
const uniqueId = cn.url + cn.id;
clearTimeout(streamViewerCountTimeouts[uniqueId])
streamViewerCountTimeouts[uniqueId] = setTimeout(() => {
monitorRemoveViewer(e,cn)
},e.monitorTimeout)
}
}
function monitorRemoveViewer(e,cn){
const groupKey = e.ke
@ -1861,5 +1898,6 @@ module.exports = (s,config,lang) => {
attachMainProcessHandlers: attachMainProcessHandlers,
removeSenstiveInfoFromMonitorConfig,
sendSubstreamEvent,
sendSubstreamEventActiveMonitor
}
}

View File

@ -95,10 +95,11 @@ module.exports = function(s,config,lang,app){
res.end('404 : Monitor not found');
return
}
s.checkChildProxy(req.params,function(){
s.checkChildProxy(req.params,async function(){
var Channel = 'MAIN'
if(req.params.channel){
Channel = parseInt(req.params.channel)+config.pipeAddition
await s.toggleSubstreamAndWaitForOutput(req.params.ke, monitorId);
}
var mp4frag = s.group[req.params.ke].activeMonitors[req.params.id].mp4frag[Channel];
var errorMessage = 'MP4 Stream is not enabled'
@ -158,7 +159,7 @@ module.exports = function(s,config,lang,app){
s.closeJsonResponse(res,{ok: false, msg: lang['Not Authorized']});
return;
}
s.checkChildProxy(req.params,function(){
s.checkChildProxy(req.params,async function(){
if(s.group[req.params.ke]&&s.group[req.params.ke].activeMonitors&&s.group[req.params.ke].activeMonitors[req.params.id]){
if(user.permissions.watch_stream==="0"||user.details.sub&&user.details.allmonitors!=='1'&&user.details.monitors.indexOf(req.params.id)===-1){
res.end(user.lang['Not Permitted'])
@ -170,6 +171,7 @@ module.exports = function(s,config,lang,app){
if(!req.params.channel){
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].emitter
}else{
await s.toggleSubstreamAndWaitForOutput(req.params.ke, monitorId);
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].emitterChannel[chosenChannel]
}
res.writeHead(200, {
@ -228,7 +230,7 @@ module.exports = function(s,config,lang,app){
s.closeJsonResponse(res,{ok: false, msg: lang['Not Authorized']});
return;
}
s.checkChildProxy(req.params,function(){
s.checkChildProxy(req.params,async function(){
noCache(res)
if(user.permissions.watch_stream==="0"||user.details.sub&&user.details.allmonitors!=='1'&&user.details.monitors.indexOf(req.params.id)===-1){
res.end(user.lang['Not Permitted'])
@ -241,6 +243,19 @@ module.exports = function(s,config,lang,app){
req.dir+=req.params.file;
}
res.on('finish',function(){res.end();});
if (req.params.file.endsWith('.m3u8')) {
await s.toggleSubstreamAndWaitForOutput(req.params.ke, monitorId);
const monitorTimeout = s.getSubstreamWaitTimeout(req.params.ke, monitorId);
var ip = s.getClientIp(req)
s.camera('watch_on',{
id : req.params.id,
ke: req.params.ke,
monitorTimeout: monitorTimeout
},{
id: req.params.auth + ip + req.headers['user-agent'],
url: req.originalUrl
})
}
if (fs.existsSync(req.dir)){
fs.createReadStream(req.dir).pipe(res);
}else{
@ -310,19 +325,21 @@ module.exports = function(s,config,lang,app){
*/
app.get([config.webPaths.apiPrefix+':auth/flv/:ke/:id/s.flv',config.webPaths.apiPrefix+':auth/flv/:ke/:id/:channel/s.flv'], function(req,res) {
s.auth(req.params,function(user){
const monitorId = req.params.id
if(cantLiveStreamPermission(user,monitorId,'watch_stream')){
s.closeJsonResponse(res,{ok: false, msg: lang['Not Authorized']});
return;
}
s.checkChildProxy(req.params,function(){
s.checkChildProxy(req.params, async function () {
noCache(res)
var Emitter,chunkChannel
if(!req.params.channel){
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].emitter
chunkChannel = 'MAIN'
}else{
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].emitterChannel[parseInt(req.params.channel)+config.pipeAddition]
chunkChannel = parseInt(req.params.channel)+config.pipeAddition
await s.toggleSubstreamAndWaitForOutput(req.params.ke, monitorId);
chunkChannel = parseInt(req.params.channel) + config.pipeAddition;
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].emitterChannel[chunkChannel];
}
if(s.group[req.params.ke].activeMonitors[req.params.id].firstStreamChunk[chunkChannel]){
//variable name of contentWriter
@ -370,17 +387,19 @@ module.exports = function(s,config,lang,app){
config.webPaths.apiPrefix+':auth/h264/:ke/:id'
], function (req, res) {
s.auth(req.params,function(user){
const monitorId = req.params.id;
if(cantLiveStreamPermission(user,monitorId,'watch_stream')){
s.closeJsonResponse(res,{ok: false, msg: lang['Not Authorized']});
return;
}
s.checkChildProxy(req.params,function(){
s.checkChildProxy(req.params, async function(){
noCache(res)
if(!req.query.feed){req.query.feed='1'}
var Emitter
if(!req.params.feed){
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].streamIn[req.query.feed]
}else{
await s.toggleSubstreamAndWaitForOutput(req.params.ke, monitorId);
Emitter = s.group[req.params.ke].activeMonitors[req.params.id].emitterChannel[parseInt(req.params.feed)+config.pipeAddition]
}
var contentWriter