bytedeco/javacv

ffmpegframegrabber srt Delay

Open

#1,664 opened on Jun 18, 2021

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

hey guy , when I using ffmpegframegrabber and try to read srt stream in android , it show the first frame correcttly in my imageview .But after about 10 minute ,the frame show in imageview is not the newer frame , it show the frame is about two minute before ;

 public void startGrabStream() {
        try {
            FFmpegLogCallback.set();  
            grabber = new FFmpegFrameGrabber("srt://192.168.2.102:8080?streamid=live.sls.com/live/test&mode=caller"); 
            grabber.setVideoCodec(AV_CODEC_ID_H264);
            grabber.setAudioCodec(AV_CODEC_ID_AAC);
            grabber.setOption("fflags", "nobuffer"); 
            grabber.start();
        
            while (true) {

                long startRenderImage = System.nanoTime();

                frame = grabber.grabFrame();
                if (frame == null) {
                    lose++;
                    Log.d(TAG, "startGrabStream: frame null");
                    continue;
                } 
                playsound((frame));
                Bitmap bitmap = converter.convert(frame);
                long endRenderImage = System.nanoTime();

                final Float renderFPS = 1000000000.0f / (endRenderImage - startRenderImage + 1);

                if (bitmap != null) {

                    count++;
                    LocalDateTime time2=LocalDateTime.now();
                    int secound2 = time2.getSecond();
                    if(secound2 != secound)
                    {
                        fps = count;
                        count = 0;
                        secound = secound2;
                        loseinsec = lose;
                        lose=0;
                    }
                    int finalFps = fps;
                    int finalLoseinsec = loseinsec;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            {
                                if (first_frame) {
                                    progressBar.setVisibility(View.GONE);
                                    first_frame = false;
                                }
                                try {
                                    infotxt.setText("fps " + finalFps + ": renderfps " + renderFPS + ":loseinsec" + finalLoseinsec);
                                } catch (Exception exception) {
                                    exception.printStackTrace();
                                }
                                previewpic.setImageBitmap(bitmap);
                            }
                        }
                    });
                }else {
                    lose +=1;
                }
            }
            System.out.println("loop end with frame: " + frame);
        } catch (Exception exception) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (recorder != null)
                            recorder.stop();
                        recorder = null;

                    } catch (FFmpegFrameRecorder.Exception e) {
                        e.printStackTrace();
                    }
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this, "fail get srt stream frame !", Toast.LENGTH_SHORT).show();
                }
            });
            exception.printStackTrace();
        }
        System.out.println("end");
    }

Contributor guide