Skip to content

Ubuntu 18.04 System FFmpegFrameGrabber.grab() Out of memory #1366

@ml02218370

Description

@ml02218370

I get the video stream for "Rtsp" and save it as an MP4 file.
After my test:
ubuntu System memory has been rising,but windows System memory normal

My code:

public class CameraPush {
    protected FFmpegFrameGrabber grabber = null;
    protected FFmpegFrameRecorder record = null;
    int width;
    int height;
    protected int audiocodecid;
    protected int codecid;
    protected double framerate;
    protected int bitrate;
    private int audioChannels;
    private int audioBitrate;
    private int sampleRate;

    public CameraPush() { }

    public CameraPush from() throws Exception {
        grabber = FFmpegFrameGrabberUtil.createDefault("rtsp://admin:123456@192.168.6.64:554/id=1&type=0");
        grabber.setTimeout(5000);
        grabber.setOption("rtsp_transport", "tcp");
        grabber.setOption("stimeout", 5000000);
        try {
            grabber.start();
            width = grabber.getImageWidth();
            height = grabber.getImageHeight();         
            if (width == 0 && height == 0) {
                logger.showErrorLogger("[ERROR]   TimeOut...");
                grabber.stop();
                grabber.close();
                return null;
            }
            audiocodecid = grabber.getAudioCodec();
            codecid = grabber.getVideoCodec();
            framerate = grabber.getVideoFrameRate();
            bitrate = grabber.getVideoBitrate();
            audioChannels = grabber.getAudioChannels();
            audioBitrate = grabber.getAudioBitrate();
            if (audioBitrate < 1) {
                audioBitrate = 128 * 1000;
            }
        } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
            grabber.stop();
            grabber.close();
            return null;
        }
        return this;
    }
  
    public CameraPush toMp4(String savePath) throws Exception {
        record = new FFmpegFrameRecorder(savePath, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
        record.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        record.setGopSize((int)framerate/2);
        record.setFormat("mp4");
        record.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
        record.setFrameRate(grabber.getFrameRate());
        record.setSampleRate(grabber.getSampleRate());
        record.start();
        return this;
    }

    public CameraPush goMp4(Thread nowThread)
            throws org.bytedeco.javacv.FrameGrabber.Exception, org.bytedeco.javacv.FrameRecorder.Exception {
        long err_index = 0;
        int allFrameSize=10*(int)framerate; //save frame size
        int indexFrame=0;
        for (int no_frame_index = 0; no_frame_index < 5 && err_index < 5&&indexFrame<allFrameSize;) {
            try {
                nowThread.sleep(1);
                Frame captured_frame = null;
                if((captured_frame = grabber.grab()) != null){
                    record.setTimestamp(grabber.getTimestamp());
                    record.record(captured_frame);
                    indexFrame+=1;
                    err_index=0;
                }else {
                    err_index++;
                }
            } catch (InterruptedException e) {           
                break;
            } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
                err_index++;
            } catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
                err_index++;
            }
        }
        record.stop();
        record.close();
        grabber.stop();
        grabber.close();
        System.gc();
        return this;
    }
}



public class CameraThread {
    public static class MyRunnable implements Runnable {
        public static ExecutorService es = Executors.newCachedThreadPool();
        private Thread nowThread;
        private String savePath;

        public MyRunnable(String savePath) {
            this.savePath = savePath;
        }

        public void setInterrupted() {
            nowThread.interrupt();
        }

        @Override
        public void run() {
            CameraPush push=null;
            try {
                nowThread = Thread.currentThread();
                push= new CameraPush(cameraPojo).from();
                if (push != null) {
                    push.toMp4(savePath).goMp4(nowThread); 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Test Code:

 Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                int id=1;
                while (true) {
                      String savePath="/home/test/savefiles/"+String.valueOf(id)+".mp4";
                       CameraThread.MyRunnable job = new CameraThread.MyRunnable(savePath);
                       CameraThread.MyRunnable.es.execute(job);
                        id++;
                        try {
                            Thread.sleep(30000);
                        } catch (Exception ex) {
                        }               
                }
            }
        });
        thread.setDaemon(true);
        thread.start();

my pom:

<dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.2</version>
 </dependency>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions