I have an application that connects to an RTSP camera and processes some of the video frames. Depending on the camera resolution and frame rate, I do not need to process all the frames, and sometimes my processing takes some time. I designed it so that when reading a frame it was transferred to the work queue for another thread. However, depending on the download / resolution of the system / frame rate / network / file system / etc., I sometimes find cases when the program can not cope with the camera.
I found that with ffmpeg (I use the last git drop from mid-October and work on windows), that is good for a couple of seconds behind, and you continue to get the next frame, the next frame, etc. However, once you get, say, 15-20 seconds behind these frames that you get from ffmpeg, sometimes they get corrupt. That is, what is returned, since the next frame often has graphical glitches (strip of the bottom of the frame, etc.).
What I would like to do is check if I find that I am larger than the X-frames behind the stream in real time, and if so, discard the cache frames and start getting the latest / current frames.
My current fragment of the frame buffer read stream (C ++):
while(runThread) { av_init_packet(&(newPacket)); int errorCheck = av_read_frame(context, &(newPacket)); if (errorCheck < 0) { // error } else { int frameFinished = 0; int decodeCode = avcodec_decode_video2(ccontext, actualFrame, &frameFinished, &newPacket); if (decodeCode <0) { // error } else if (decodeCode == 0) { // no frame could be decompressed / decoded / etc } else if ((decodeCode > 0) && (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(&(newPacket)); } }
I have google'd and looked through the ffmpeg docs for some function that I can call to clear things up and allow me to catch up, but I can't find anything. The same solution would be necessary if you only wanted to track the video source from time to time (for example, if you only want to get stuck one frame per second or minute). The only thing I could think of was disconnect from the camera and reconnect. However, I still need a way to determine if the frames that I get are old.
Ideally, I could do something like this:
while(runThread) { av_init_packet(&(newPacket));