Video streaming connection closed automatically

I’m trying to stream my webcam in mpeg-ts format using ffmpeg in chunk mode. but connection closed every one minute, please help me to keep connection active.

def live_cam(conn, _params) do                
  conn = put_resp_header(conn, "content-type", "video/MP2T")
  conn = send_chunked(conn, 200)
  ffmpeg_args = ["-y", "-nostdin", "-hide_banner", "-loglevel", "0", "-f", "v4l2", "-framerate", "25", "-video_size", "1280x720", "-input_format", "mjpeg", "-i", "/dev/video1",
						"-c", "libx264", "-movflags", "faststart", "-f", "mpegts", "-"]        
  port = Port.open({:spawn_executable, "/usr/local/bin/ffmpeg"}, [{:args, ffmpeg_args}, :stream, :binary, :exit_status, :hide, :use_stdio, :stderr_to_stdout])

  handle_output(port, conn)        
  conn
end

defp handle_output(port, conn) do
  receive do
    {^port, {:data, data}} ->                        
      chunk(conn, data)
      handle_output(port, conn)
  
    {^port, {:exit_status, status}} ->            
      status        
  end
end
1 Like

any error message/log you can post?

that exact 60 seconds makes me suspicious of the new 60 second idle_timeout in cowboy 2 (though it shouldn’t matter here - but who knows…)

see https://github.com/phoenixframework/phoenix/issues/3190

1 Like

thanks, i tried with idle_timeout :infinity, now it works.
i have doubt, whether it affect server performance ?

1 Like

Not really, but it can leave stale connections so be sure to have tests to kill them yourselves under various situations as appropriate for your app. :slight_smile: