title: How to get the latest frames in ffmpeg, not the next frame
body: <p>I have an application which connects to an RTSP camera and processes some of the frames of video.   Depending on the camera resolution and frame rate, I don't need to process all the frames and sometimes my processing takes a while.   I've designed things so that when the frame is read, its passed off to a work queue for another thread to deal with.   However, depending on system load/resolution/frame rate/network/file system/etc, I occasionally will find cases where the program doesn't keep up with the camera.</p>
  
  <p>I've found that with ffmpeg(I'm using the latest git drop from mid october and running on windows) that being a couple seconds behind is fine and you keep getting the next frame, next frame, etc.   However, once you get, say, 15-20 seconds behind that frames you get from ffmpeg occasionally have corruption.   That is, what is returned as the next frame often has graphical glitches (streaking of the bottom of the frame, etc).</p>
  
  <p>What I'd like to do is put in a check, somehow, to detect if I'm more than X frames behind the live stream and if so, flush the caches frames out and start fetching the latest/current frames.</p>
  
  <p>My current snippet of my frame buffer reading thread (C++) :</p>
  
  <pre><code>while(runThread)
  {
      av_init_packet(&amp;(newPacket));
  
      int errorCheck = av_read_frame(context, &amp;(newPacket));
      if (errorCheck &lt; 0)
      {
          // error
      }
      else
      {
  
          int frameFinished = 0;
          int decodeCode = avcodec_decode_video2(ccontext, actualFrame, &amp;frameFinished, &amp;newPacket);
  
          if (decodeCode &lt;0)
          {
              // error
          }
          else
          if (decodeCode == 0)
          {
              // no frame could be decompressed / decoded / etc
          }
          else
          if ((decodeCode &gt; 0) &amp;&amp; (frameFinished))
          {
              // do my processing / copy the frame off for later processing / etc
          }
          else
          {
              // decoded some data, but frame was not finished...
              // Save data and reconstitute the pieces somehow??
              // Given that we free the packet, I doubt there is any way to use this partial information
          }
          av_free_packet(&amp;(newPacket));
      }
  }
  </code></pre>
  
  <p>I've google'd and looked through the ffmpeg documents for some function I can call to flush things and enable me to catch up but I can't seem to find anything.   This same sort of solution would be needed if you wanted to only occasionally monitor a video source(eg, if you only wanted to snag one frame per second or per minute).   The only thing I could come up with is disconnecting from the camera and reconnecting.   However, I still need a way to detect if the frames I am receiving are old.</p>
  
  <p>Ideally, I'd be able to do something like this :</p>
  
  <pre><code>while(runThread)
  {
      av_init_packet(&amp;(newPacket));
  
      // Not a real function, but I'd like to do something like this
      if (av_check_frame_buffer_size(context) &gt; 30_frames)
      {
          // flush frame buffer.
          av_frame_buffer_flush(context);
      }
  
      int errorCheck = av_read_frame(context, &amp;(newPacket));
  
      ...
      }
  }
  </code></pre>