javacv icon indicating copy to clipboard operation
javacv copied to clipboard

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

Open Alin63992 opened this issue 3 months ago • 4 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!

Alin63992 avatar May 02 '24 16:05 Alin63992