bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FFMPEGFrameGrabber.start() doesn't find stream most of the time and blocks the thread it's running in from running its course

Alin63992 opened this issue · comments

Hello! Hope you're doing well. I'm having some issues with FFMPEGFrameGrabber.
Most of the time, when calling the start() method, the grabber does not grab any frames from the RTMP server I have set up using NGINX on Windows, and the thread the grabbing is running in stays blocked on the line where start() is called, so it doesn't run its course, nor checks if it's interrupted or not when the JavaFX window is closed, so the app stays running indefinitely. I have FFMPEGFrameRecorder set up as in the WebcamAndMicrophoneCapture.java sample (but using VideoCapture instead of OpenCVFrameGrabber because implementing the latter consumed a lot of ram, and skipping the audio lines since I don't need the microphone audio being captured and sent), and I have the grabber set up like so:

final int CONNECTION_TIMEOUT = 10;
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtmp://" + Main.rtmpServerAddress + ":" + Main.rtmpServerPort + "/live/" + Main.employeeSpotlightUsername);
try {
    grabber.setFormat("flv");
    grabber.setVideoCodec(avcodec.AV_CODEC_ID_H264);
    //grabber.setOption("timeout", String.valueOf(CONNECTION_TIMEOUT * 1000000));
    grabber.start();
    JavaFXFrameConverter converter = new JavaFXFrameConverter();
    Frame frame;
    while (!Thread.currentThread().isInterrupted()) {
        if ((frame = grabber.grab()) != null) {
            camImage.setImage(converter.convert(frame));
        }
    }
    grabber.stop();
} catch (FrameGrabber.Exception e) {
    e.printStackTrace();
    try {
          grabber.stop();
    } catch (FFmpegFrameGrabber.Exception ex) {
          ex.printStackTrace();
    }
}

Also, setting the 10s timeout makes the grabber quit trying to find a stream somewhere between 1-5 seconds.
How can I make the grabber give up after 10s and not block the entire thread, so that the application quits successfully when closing the main window?
Thank you!

We can set a timeout on the grabbing side as well, so please try to do that

Hey! I have already tried that in the 6th line, that's commented out, which is how I found out that the 10s timeout isn't reached, and that FFMPEGFrameGrabber quits loading after 1-5s since start() was called. Maybe there's a calculation error I've made there?
But even if that was a problem, there's still one that remains: the one where the stream isn't found or loaded every time I try, even if FFMPEGFrameRecorder doesn't throw an error about sending the stream to the server with the same address I'm trying to grab from.

你用的是1.5.10吗?

rtspUrl = "rtmp://***/live/104";
try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl)) {
if (rtspUrl.contains("rtsp")) {
grabber.setOption("timeout", "20000000");
}
if (rtspUrl.contains("rtmp")) {
grabber.setFormat("flv");
grabber.setOption("listen_timeout", "20000000");
}
avutil.av_log_set_level(avutil.AV_LOG_DEBUG);
FFmpegLogCallback.set();
grabber.start();

}

In this code, I want to stop my program by setting 'listen_timeout', but the current situation is that this thread is consistently blocked