#include "rtspclientdown.h" #include rtspclientdown::rtspclientdown(std::string strrtspserver) { mpthread = new std::thread(&rtspclientdown::threadrtspdown,this,strrtspserver); } rtspclientdown::~rtspclientdown() { mbthreadrun = false; mpthread->join(); } void rtspclientdown::threadrtspdown(std::string strrtspserver) { AVFormatContext *i_fmt_ctx; /* should set to NULL so that avformat_open_input() allocate a new one */ i_fmt_ctx = NULL; AVDictionary *avdic=NULL; char option_key[]="rtsp_transport"; char option_value[]="tcp"; av_dict_set(&avdic,option_key,option_value,0); bool bConnected = false; while(mbthreadrun) { if(bConnected == false) { i_fmt_ctx = NULL; if (avformat_open_input(&i_fmt_ctx, strrtspserver.data(), NULL, &avdic)!=0) { fprintf(stderr, " = could not open input file\n"); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); continue ; } if (avformat_find_stream_info(i_fmt_ctx,NULL)<0) { fprintf(stderr, " = could not find stream info\n"); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); continue ; } bConnected = true; } AVPacket i_pkt; av_init_packet(&i_pkt); i_pkt.size = 0; i_pkt.data = NULL; if (av_read_frame(i_fmt_ctx, &i_pkt) <0 ) { bConnected = false; std::cout<<"connect fail. retry."<(new char[xframe.mdatasize]); memcpy(xframe.mpstr_ptr.get(),i_pkt.data,xframe.mdatasize); mmutexframe.lock(); while(mvectorframe.size()>900) { std::cout<<" rtspclientdown::threadrtspdown. erase frame."<0) { mmutexframe.lock(); if(mvectorframe.size()>0) { xframe = mvectorframe[0]; mvectorframe.erase(mvectorframe.begin()); nrtn = 1; } else { nrtn = 0; } mmutexframe.unlock(); return nrtn; } if(nwaitms == 0)return nrtn; std::unique_lock lk(mmutexcv); if(mcv.wait_for(lk, std::chrono::milliseconds(nwaitms)) == std::cv_status::timeout) { lk.unlock(); } else { lk.unlock(); } mmutexframe.lock(); if(mvectorframe.size()>0) { xframe = mvectorframe[0]; mvectorframe.erase(mvectorframe.begin()); nrtn = 1; } else { nrtn = 0; } mmutexframe.unlock(); return nrtn; }