bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.57k stars 1.58k forks source link

ffmpegframegrabber srt Delay #1664

Open DesenYang opened 3 years ago

DesenYang commented 3 years ago

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");
    }
saudet commented 3 years ago

BTW, be sure to increase the verbosity of the log to get more information about what might be going wrong.