javacv icon indicating copy to clipboard operation
javacv copied to clipboard

FFmpegFrameGrabber missed packet problem

Open osmantoker opened this issue 4 years ago • 4 comments

Hello, I'm trying to capture and display rawvideo stream via sdp file. There is a perfect solution with the command line but when the process is done through java ffmpegframegrabber, packet loss occurs. Same SDP file, same parameters but i dont understand what is the difference...

Here is the command line ffmpeg; ffmpeg -v debug -re -probesize 32 -buffer_size 500000 -protocol_whitelist file,rtp,udp -strict -2 -i test.sdp -pix_fmt yuv420p -f sdl "TEST" or ffplay -protocol_whitelist file,udp,rtp -strict -2 -i test.sdp

And Java Code;

    grabber = new FFmpegFrameGrabber("/var/tmp/test.106.sdp");
	grabber.setVideoOption("strict", "-2");
	grabber.setOption("protocol_whitelist", "file,udp,rtp");
	grabber.setVideoOption("buffer_size", "500000");
	grabber.setVideoOption("probesize", "32");
    try {
    	grabber.start();
        while (true) {
        	frame = grabber.grab();
            canvas.showImage(frame);
            grabber.flush();
            Thread.sleep(INTERVAL);
        }

I tried add grabber.setOption("threads", "64"); and grabber.flush(); but nothings changed.

Here is the log;

[sdp @ 0x29aaa00] RTP: missed 1545 packets
[sdp @ 0x29aaa00] Missed previous RTP Marker
[sdp @ 0x29aaa00] max delay reached. need to consume packet

When I researched the problem on the internet, I came across suggestions to increase the buffer size, but I did not get any results although I tried.

Does anyone know about this issue?

Osman

osmantoker avatar May 06 '20 21:05 osmantoker

It looks like a network issue that UDP can't deal with. If that's the case, I would recommend switching to TCP.

saudet avatar May 06 '20 22:05 saudet

I couldn't change the source udp to tcp because the point is latency. Maybe network is an important factor but both command line process and java code run at the same network. I removed the thread.sleep(), loss decreased 80-90 percent however, the image is still not smooth (as if one frame got into the next frame). The answer to the problem is hidden in the ffmpeg buffer configuration or setOption()/setVideoOption() but which of them make it smooth??

Osman

osmantoker avatar May 07 '20 08:05 osmantoker

Options you could try are documented here: https://ffmpeg.org/ffmpeg-protocols.html

saudet avatar May 27 '20 06:05 saudet

I had a similar problem when I used FFmpegFrameGrabber to take a screenshot of the RTSP stream, the screen was blurred. This is my code.

// source is rtsp url
try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(source);
     Java2DFrameConverter java2dFrameConverter = new Java2DFrameConverter()) {
    grabber.setOption("rtsp_transport", "tcp");
    grabber.setOption("stimeout", "30000000");
    grabber.start(true);
    reEntry++;
    log.info("Current source {} frame rate is {} {}, Re entry count {}", source, grabber.getFrameRate(), grabber.getVideoFrameRate(),
            reEntry);

    while (true) {
        Frame frame = grabber.grabImage();
        // do something... 
        if (image != null) {
            log.debug("Source {} screenshot image, start Time {}, frame count {}, second {}", source,
                    startTime, frameCounter, second);
            callback.onResult((int) second, image);
        }
    }
}

loprx avatar May 22 '23 02:05 loprx