-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Live Streaming: Stop Response Task when user cancels the connection #97
Comments
Hi, Checking your code I can't see where you stop ffmpeg, or ffmpeg closes itself? |
Hi, well, that's what I'm trying to achieve... As it's a live stream, it's not supposed to be stopped in any other circumstance except that the client closes the connection. How can I catch that condition and then stop the ffmpeg process? |
Yeah, but why are you starting a ffmpeg each time a client connects? This is the expected behavior? I would recommend leave ffmpeg outside the HTTP connection and just use the StaticFileModule to serve a m3u8 file for HLS (https://en.wikipedia.org/wiki/HTTP_Live_Streaming) |
I might later add some kind of caching, so that only one ffmpeg instance per channel will be used to deliver to the clients, if there are several clients requesting the same channel. But in general, yes, it's intended that ffmpeg is only operating when a client is requesting a channel. There will be several channels (about 10, most in 720p) that will be re-streamed, so I don't want to have several ffmpeg instances running 24/7 all days of the year reading and writing data all the time, because that would be overkill for the internet bandwidth and my HDDs. :) There must surely be a way to detect whether the response.OutputStream is still "alive"? |
Any idea @mariodivece ? |
There are only 2 correct approaches here:
|
Thanks for your reply! Do you have an example for the RAM drive solution? Is it created with C# or is it kind of a virtual drive in Windows that I then use to read and write? The problem still is the constant download of 10 Streams... However, I have thought about the following solution to that: On each request to the m3u8/TS files, a timestamp associated to the ffmpeg process is updated. In the background, every few seconds it is checked whether the timestamp is older than x seconds, and if so, the process is being stopped. On the next request to that channel, the process is started again. This should work, but it still feels like much overhead for all of this... I wonder, how PHP handles this? A PHP script stops as soon as a client closes the browser window, unless you explicitly set Shouldn't it be possible to have a timer constantly check if the client is still there? I'd just need a push in the right direction what I could check to see if the response is still being sent to the client. |
For Ram Disk, how about this: https://stackoverflow.com/a/14728377/1357191 On the streaming side: why do you care so much about whether or not a user is connected? Don't kill the ffmpeg processes creating the m3u8 and chunk files. Just let them run continuously and serve the m3u8 and ts chunks with embedio. Killing the ffmpeg process and restarting it when a user connects won't really work (will fail half the time). |
Hi, |
Hi @Connum , can I close this issue? |
Hi @geoperez, well, I implemented the whole functionality with PHP in the end, where the ffmpeg process gets terminated as soon as the client aborts the connection. I would still be interested in solving this with embedio for perfocmance reasons though, but I didn't couldn't work it out with my poor C# skills. It musst be possible in some way! |
Closing this one. |
@Connum Hi , are you solve this problem ? |
No, unfortunately not. I ended up not using embedio at all. |
so , can help me for live stream , use other code !? |
@rdeago any idea how to pass the interruption of the network connection to the WebModule? |
Maybe CancellationToken? |
EmbedIO always sets the |
No particular reason. We can add this as part of the WebServer options. |
Please be sure that client disconnection just cause a DBG-level log. No reason to log a stack trace, as it may happen at any moment and is not caused by anything in server-side code. The default value in v2 should probably be |
Hi there,
first of all, I'm coming from a PHP and JavaScript background, so this might be very simple, but I'm stuck at this point. I'm trying to write a module for live streaming of MPEG Transport Streams via FFMPEG. I finally got this working today in an asynchronous way, so that I can have multiple simultaneous connections. I oriented myself by the code of the StaticFilesModule, and made some adaptions because I don't know the content length and I don't need to support seeking via byte range headers.
It works well, I can view the stream via VLC for example, or download it via a browser. However, as I can see in the console log, the request never ends. I can see in the Task Manager that ffmpeg keeps running in the background and keeps using resources. I would like to stop the request from being processed / the response from being continously generated and the ffmpeg task from running as soon as the connection is being closed, that is the stream is stopped in a player or the download is aborted in a browser.
As far as I can see, this is not implemented for static files either, so a very large download would keep on being processed even after the connection was stopped. So I'm not sure whether this is possible with EmbedIO without changes to the code, but it would definitely be something to consider for (large) static files as well to save resources.
Anyway, here's my current code:
And this is the console output after I have stopped the download:
As you can see, the request never stops being processed, as opposed to serving a static file or in this case making the stream a finite length by adding
-t 1?
to the ffmpeg arguments, which results in:How do I approach this? Do I need to handle the CancellationToken in a way?
Thanks in advance!
The text was updated successfully, but these errors were encountered: