arut / nginx-rtmp-module

NGINX-based Media Streaming Server
http://nginx-rtmp.blogspot.com
BSD 2-Clause "Simplified" License
13.35k stars 3.51k forks source link

restarting hung ffmpeg processes #494

Open chopj opened 9 years ago

chopj commented 9 years ago

I'm having some trouble with hanging ffmpeg proceses started with exec_pull. I'm using exec_static to remux an rtsp source to rtmp via ffmpeg. The incoming rtsp stream is transported over http. The end clients connect via HLS and DASH. We have no rtmp clients.

When we cut network connection from the source, the ffmpeg process still runs. Once the connection is restored, the ffmpeg process doesn't pick up remuxing. However, bouncing nginx (and thus the child ffmpeg) fixes the problem. I realize this isn't likely an nginx problem, but hopefully someone else has had the same issue. How can we automatically restart ffmpeg when detecting an idle stream?

Thanks again. Great software!

Heres the config:

rtmp {
     server {
          listen 1935;
          chunk_size 4000;
          ping 20s;

          #Each RTSP Stream gets re-muxed into RTMP
          exec_static ffmpeg -rtsp_transport http -i rtsp://192.168.1.100:554/axis-media/media.amp?streamprofile=Bandwidth&clock=1&date=1&videobitrate=100&pull=1 -c copy -f flv rtmp://localhost:1935/live/camera01;

          # restart child processes.  used to restart ffmpeg when streams go down
          respawn on;

          drop_idle_publisher 10s;

          # main application
          application live {
             live on;
             idle_streams on;

             # hls settings
             hls on;
             hls_path /tmp/hls;

             # mpeg dash settings
             dash on;
             dash_path /tmp/dash;

             # all streams recorded to local disk first
             record video;
             record_interval 30s;
             record_path /tmp/rec;
             record_suffix .%F-%H-%M-%S.flv;
          }
     }
}
Ideasrefined commented 9 years ago

FFMPEG does not terminate connection on any unusual condition on RTSP . It does support proper teardown though. And its a known behavior mentioned somewhere on their bug tracker also.

If you want to detect network teardown or stream unavailable and kill FFMPEG on that event then use ffmpeg libraries (libavformat , libavcodec etc) to write your own application and implement a timer based frame counter. If the counter doesn't increment in e-g 30 seconds it means something is wrong and you can exit the process.