FFmpeg long-time, unresponsive solutions to solve problems
1, ffmpeg to connect when the camera is not online cause avformat_open_input and other functions have been death, causing the program to die
2, Av_read_frame in the process of camera disconnection caused the reading stream has been death
Workaround
Register FFmpeg callback function before opening streaming media
Avformatcontext *m_prtspfmt = Avformat_alloc_context (); M_prtspfmt->interrupt_callback.callback = Avinterruptcallbackfun; M_prtspfmt->interrupt_callback.opaque = this;
The callback function type is:
typedef struct AVIOINTERRUPTCB {int (*callback) (void*); void *opaque;} AVIOINTERRUPTCB;
Returning 1 in the callback function means that the ffmpeg end block can hand over the manipulation to the user thread and return the error code
Returning 0 in the callback function means that FFmpeg continues to block until FFmpeg is working properly
So to exit death you need to return 1
The pseudo code is as follows:
Camera Connection Class Cipcamera{public:cipcamera (); ~cipcamera (); Avreadframe Timeout callback function static int avinterruptcallbackfun (void *ctx);//Read RTSP code flow path static DWORD WINAPI Readstreamthread (LPV OID param);//Heartbeat monitor thread--monitors if thread is dead static DWORD WINAPI monitorthread (LPVOID param);}; int cipcamera::avinterruptcallbackfun (void *param) {Cipcamera *pcamera = (cipcamera*) param;if (NULL = = Pcamera) return 0; if (pcamera->m_bquitffmpegblock) {//notification FFmpeg can release the action from the blocked worker thread return 1;} else{//notify FFmpeg to continue blocking work return 0;}} Connect the camera to read the RTSP code streamlines DWORD WINAPI cipcamera::readstreamthread (LPVOID param) {Cipcamera *pcapture = (cipcamera*) param;if ( NULL = = pcapture) Return-1;pcapture->connectcamera (); while (Pcapture->m_bworkok) {//ffmpeg read stream pcapture-> Readstream ();//Send Heartbeat pcapture->heartbeat (); Sleep (1);} return TRUE;} Monitoring thread DWORD WINAPI cipcamera::monitorthread (LPVOID param) {Cipcamera *pcamera = (cipcamera*) param;if (NULL = = Pcamera) Return-1;while (pcamera->m_brework) {//If heartbeat timeout if (OK! = pcamera->getstate (&ntimeout);) {//notify FFMPEG returns Pcamera->m_bworkok = False;pcamera->m_bquitffmpegblock = TRUE;} Else{//ffmpeg Continue working Pcamera->m_bworkok = TRUE;} Sleep (100);} return 0;}
FFmpeg long-time unresponsive solution