Stop inactive published RTMP streams which are auto published on demand using FFMpeg

ajayalag1974

New Member
Hi,

I have written my web server code which when user visits the streaming page checks if streaming exist by name via "/rest-api/stream/find", if not it publishes rtmp stream using ffmpeg command, command is executed via child_process spawn method in nodejs (ffmpeg -re -i "https://somedomain.com/somestream.m3u8" -preset ultrafast -acodec aac -vcodec h264 -f flv "rtmp://localhost:1935/live/test").

Now the issue is - once stream is published via ffmpeg and there are no active viewers of such stream, above ffmpeg command/process is still running and publishing the stream to flashphoner rtmp server and accordingly using resources.

I would ideally want it to be stopped/terminated if no active viewers of such stream, probably it could be achieved in 2 ways:
1. Implement some inactive timeout kind of thing either via flashphoner or ffmpeg command, if either supports this, which stops that rtmp being published & associated ffmpeg process after inactivity.
2. Periodically iterate & check all the streams & total count of its viewers (which is available in prometheus data - /?action=stat&format=prometheus, any other json like endpoint for this data ?) and if no viewers of stream than terminate the stream via "/rest-api/stream/terminate".

How should i go about achieving this ?
 
Last edited:

Max

Administrator
Staff member
Good day.
You can check if there are stream subscribers by REST API /stream/find:
Code:
POST /rest-api/stream/find HTTP/1.1
Host: wcs:8081
Content-Type: application/json
 
{
    "name":"test",
    "published":false
}
The query returns 404 Not Found if there are no subscribers to the stream.
So you can periodically poll the query (every minute for example) and, if no subrscibers in last minute, stop ffmpeg by stored PID for example. This is more preferrable way than /stream/terminate query, but the query may also be used
Code:
POST /rest-api/stream/terminate HTTP/1.1
Host: wcs:8081
Content-Type: application/json
 
{
    "name":"test",
    "published":true
}
 

ajayalag1974

New Member
Good day.
You can check if there are stream subscribers by REST API /stream/find:
Code:
POST /rest-api/stream/find HTTP/1.1
Host: wcs:8081
Content-Type: application/json

{
    "name":"test",
    "published":false
}
The query returns 404 Not Found if there are no subscribers to the stream.
So you can periodically poll the query (every minute for example) and, if no subrscibers in last minute, stop ffmpeg by stored PID for example. This is more preferrable way than /stream/terminate query, but the query may also be used
Code:
POST /rest-api/stream/terminate HTTP/1.1
Host: wcs:8081
Content-Type: application/json

{
    "name":"test",
    "published":true
}
Hi,

Thanks, i implemented it like that, at some regular intervals (set_interval), i am fetching all streams /rest-api/stream/find_all and than filtering all such streams which don't have more than 1 stream listed (so only have 1 stream with published=true, but no published=false/playing), and than terminating them all with /rest-api/stream/terminate. That works.

Maybe you would like to give an option for this in flashphoner.properties.
 

Max

Administrator
Staff member
Maybe you would like to give an option for this in flashphoner.properties.
This is a rare case to stop an RTMP publishinig stream without subscribers. So we have no plans to add an option for this.
However, there is a such option for pulled RTSP or RTMP streams.
 

ajayalag1974

New Member
This is a rare case to stop an RTMP publishinig stream without subscribers. So we have no plans to add an option for this.
However, there is a such option for pulled RTSP or RTMP streams.
Hi,

Yes you are right inactivity options are available for pulled streams, i am using it.

I think my usecase could be a popular use case and if it is so than this option would be benefitial. My usecase is simple, user visits the streaming page, than my code (automation) check if such stream is already published or not, if yes directly send them the embed player with that stream, if not fetches the stream link, push it as rtmp to flashphoner rtmp server using ffmpeg and then send embed player with that stream. In such usecase, automatic stream termination was required when no active viewers/subscribers.

Anyone doing automation, fetching links & pulling rtmp/rtsp (inactivity options available) or publishing it as rtmp using ffmpeg (no automatic stream termination option available), should require that, anyway your choice, i have it implemented with the method you shared & its working ok. Thanks.
 
Top