FFmpeg long-time unresponsive solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.