package com.luqj.javacv.utils; import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.Frame; import org.bytedeco.javacv.Java2DFrameConverter; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; public class RtspUtil { private static final int DEFAULT_TIMEOUT_MS = 5000; private static final int DEFAULT_SCREENSHOT_TIMEOUT_MS = 15000; public static boolean isStreamAvailable(String rtspUrl) { return isStreamAvailable(rtspUrl, DEFAULT_TIMEOUT_MS); } public static boolean isStreamAvailable(String rtspUrl, int timeoutMs) { AtomicReference result = new AtomicReference<>(false); Thread thread = new Thread(() -> { FFmpegFrameGrabber grabber = null; Frame frame = null; try { grabber = new FFmpegFrameGrabber(rtspUrl); grabber.setOption("rtsp_transport", "tcp"); grabber.setOption("stimeout", "3000000"); grabber.setOption("buffer_size", "1024000"); grabber.start(); frame = grabber.grabImage(); result.set(frame != null); } catch (Exception ignored) { } finally { if (frame != null) { try { frame.close(); } catch (Exception ignored) { } } if (grabber != null) { try { grabber.stop(); grabber.close(); } catch (Exception ignored) { } } } }); thread.setDaemon(true); thread.start(); try { thread.join(timeoutMs); return result.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } } public static String screenshot(String rtspUrl, String basePath) throws Exception { return screenshot(rtspUrl, basePath, DEFAULT_SCREENSHOT_TIMEOUT_MS); } public static String screenshot(String rtspUrl, String path, int timeoutMs) throws Exception { AtomicReference result = new AtomicReference<>(); AtomicReference error = new AtomicReference<>(); Thread thread = new Thread(() -> { FFmpegFrameGrabber grabber = null; Frame frame = null; Java2DFrameConverter converter = null; try { grabber = new FFmpegFrameGrabber(rtspUrl); grabber.setOption("rtsp_transport", "tcp"); grabber.setOption("stimeout", "3000000"); grabber.setOption("buffer_size", "1024000"); grabber.start(); converter = new Java2DFrameConverter(); String basePath = path; if (!basePath.endsWith("/")) { basePath = basePath + "/"; } String filename = uuid() + "_snapshot.jpg"; String savePathUrl = basePath + filename; for (int i = 0; i < 10; i++) { frame = grabber.grabImage(); if (frame != null) { BufferedImage img = converter.getBufferedImage(frame); ImageIO.write(img, "jpg", new File(savePathUrl)); break; } } result.set(filename); } catch (Exception e) { error.set(e); } finally { if (frame != null) { try { frame.close(); } catch (Exception ignored) { } } if (converter != null) { try { converter.close(); } catch (Exception ignored) { } } if (grabber != null) { try { grabber.stop(); grabber.close(); } catch (Exception ignored) { } } } }); thread.setDaemon(true); thread.start(); try { thread.join(timeoutMs); if (thread.isAlive()) { throw new Exception("screenshot timeout"); } if (error.get() != null) { throw error.get(); } return result.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new Exception("screenshot interrupted"); } } public static byte[] screenshot(String rtspUrl) throws Exception { return screenshot(rtspUrl, DEFAULT_SCREENSHOT_TIMEOUT_MS); } public static byte[] screenshot(String rtspUrl, int timeoutMs) throws Exception { AtomicReference result = new AtomicReference<>(new byte[0]); AtomicReference error = new AtomicReference<>(); Thread thread = new Thread(() -> { FFmpegFrameGrabber grabber = null; Frame frame = null; Java2DFrameConverter converter = null; try { grabber = new FFmpegFrameGrabber(rtspUrl); grabber.setOption("rtsp_transport", "tcp"); grabber.setOption("stimeout", "3000000"); grabber.setOption("buffer_size", "1024000"); grabber.start(); converter = new Java2DFrameConverter(); byte[] bytes = new byte[0]; for (int i = 0; i < 10; i++) { frame = grabber.grabImage(); if (frame != null) { BufferedImage img = converter.getBufferedImage(frame); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "jpg", baos); bytes = baos.toByteArray(); break; } } result.set(bytes); } catch (Exception e) { error.set(e); } finally { if (frame != null) { try { frame.close(); } catch (Exception ignored) { } } if (converter != null) { try { converter.close(); } catch (Exception ignored) { } } if (grabber != null) { try { grabber.stop(); grabber.close(); } catch (Exception ignored) { } } } }); thread.setDaemon(true); thread.start(); try { thread.join(timeoutMs); if (thread.isAlive()) { throw new Exception("screenshot timeout"); } if (error.get() != null) { throw error.get(); } return result.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new Exception("screenshot interrupted"); } } public static String uuid() { return UUID.randomUUID().toString().replaceAll("-", ""); } }