| | |
| | | |
| | | #include <thread> |
| | | #include <unistd.h> |
| | | #include <chrono> |
| | | |
| | | extern "C"{ |
| | | #include <libavcodec/avcodec.h> |
| | |
| | | ,file_path_("") |
| | | ,func_rec_info_(nullptr) |
| | | ,thrd_(nullptr) |
| | | ,error_occured_(false) |
| | | { |
| | | // logIt("RECODER ID: %s", id_.c_str()); |
| | | } |
| | | |
| | | Recorder::~Recorder(){ |
| | | // if (out_){ |
| | | // stop_recorder_.store(true); |
| | | // cv_.notify_one(); |
| | | // int try_cnt = 0; |
| | | // while(stop_recorder_.load()){ |
| | | // logIt("REC WAIT TO QUIT ID %s", id_.c_str()); |
| | | // if (try_cnt > 100){ |
| | | // logIt("REC TRY %d TIMES TO QUIT..., BREAK!!!", try_cnt); |
| | | // break; |
| | | // } |
| | | // try_cnt++; |
| | | // sleep(1); |
| | | // } |
| | | // }else if (stop_recorder_.load()){ |
| | | // logIt("REC NORMAL QUIT"); |
| | | // } |
| | | |
| | | if (thrd_){ |
| | | stop_recorder_.store(true); |
| | | thrd_->join(); |
| | | logIt("REC THREAD JOINED, QUIT!!!"); |
| | | try |
| | | { |
| | | if (thrd_){ |
| | | { |
| | | std::unique_lock<std::mutex> locker(mutex_pkt_); |
| | | stop_recorder_.store(true); |
| | | cv_.notify_one(); |
| | | } |
| | | thrd_->join(); |
| | | logIt("REC THREAD JOINED, QUIT!!!"); |
| | | } |
| | | } |
| | | catch(const std::exception& e) |
| | | { |
| | | logIt("RECODER DESTRUCTOR EXCEPTION: ", e.what()); |
| | | } |
| | | |
| | | } |
| | | |
| | | int Recorder::init_writer(){ |
| | |
| | | |
| | | //callback to frame index and path |
| | | if(func_rec_info_){ |
| | | func_rec_info_(id_,file_frame_index_, file_path_); |
| | | func_rec_info_(id_, file_frame_index_, file_path_); |
| | | } |
| | | } |
| | | |
| | |
| | | std::list<avpacket> pkts; |
| | | { |
| | | std::unique_lock<std::mutex> locker(mutex_pkt_); |
| | | cv_.wait(locker,[&]{ |
| | | auto status = cv_.wait_for(locker, std::chrono::seconds(10), [&]{ |
| | | return !list_pkt_.empty() || stop_recorder_.load(); |
| | | }); |
| | | |
| | | if (!status){ |
| | | end_writer(); |
| | | error_occured_ = true; |
| | | break; |
| | | } |
| | | if(stop_recorder_.load()){ |
| | | end_writer(); |
| | | break; |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |