bytedeco/javacv

RTSP stream stops after 30 secods when using TCP

Open

#2,218 opened on Apr 22, 2024

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I am streaming video over RTSP using FFmpegFrameGrabber. If I do not specify UDP vs TCP it seems that the connection is stable and but I sometimes see artifiacts and distortion in the image I am receiving from the camera. However, if I specify TCP as the rtsp_transport, the image is much better with no artifacts. However, after about 30 seconds, when I call Frame.grabImage(), it returns null. At this point, I can call FFmpegFrameGrabber.stop(), followed by FFmpegFrameGrabber.start(), and the streaming of frames will continue for another 30 seconds.

I see the same behavior using ffplay from the command line. I have also discovered that if I set the -err_detect flag to ignore_err, ffplay will continue streaming frames indefinitely and the problem goes away.

ffplay rtsp://192.168.1.254/xxx.mov -rtsp_transport tcp -err_detect ignore_err

However, setting the "err_detect" flag does not seem to make any difference using javacv. Here is the code I am using:

FFmpegLogCallback.set()
val grabber = FFmpegFrameGrabber(url)
grabber.format = "rtsp"
grabber.videoCodecName = "h264_mediacodec"
grabber.options["rtsp_transport"] = "tcp"  //this stops artifacts and distortion but only runs for 30 seconds then stops
grabber.options["err_detect"] = "ignore_err" //this doesn't seem to make any difference as it does using ffplay from the command line 
grabber.frameRate = 30.0
grabber.sampleRate = 30
grabber.start() 
while (alive) {
    val frame = grabber.grabImage()
    if (frame == null) {
        grabber.stop()
        grabber.start()
    } else {
        //process frame 
    }
}

Contributor guide