| | |
| | | #include "recorder.hpp" |
| | | #include "sole.hpp" |
| | | |
| | | #include <thread> |
| | | #include <unistd.h> |
| | |
| | | ,file_frame_index_(-1) |
| | | ,file_path_("") |
| | | ,func_rec_info_(nullptr) |
| | | {} |
| | | { |
| | | // logIt("RECODER ID: %s", id_.c_str()); |
| | | } |
| | | |
| | | Recorder::~Recorder(){ |
| | | if (out_ && !stop_recorder_.load()){ |
| | | stop_recorder_.store(true); |
| | | cv_.notify_one(); |
| | | while(stop_recorder_.load()){ |
| | | logIt("REC WAIT TO QUIT ID %s", id_.c_str()); |
| | | sleep(1); |
| | | } |
| | | }else if (stop_recorder_.load()){ |
| | | logIt("REC NORMAL QUIT"); |
| | | } |
| | | |
| | | } |
| | | |
| | | int Recorder::init_writer(){ |
| | |
| | | |
| | | void Recorder::start_writer(){ |
| | | if (cur_frame == 0) { |
| | | srandom(time(NULL)); |
| | | file_path_ = dir_ + "/" + std::to_string(random()) + ".mp4"; |
| | | |
| | | sole::uuid u4 = sole::uuid4(); |
| | | file_path_ = dir_ + "/" + u4.base62() + ".mp4"; |
| | | out_->JustWriter(in_->getStream(), file_path_.c_str()); |
| | | logIt("start record %s", file_path_.c_str()); |
| | | logIt("START RECORD %s", file_path_.c_str()); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | // writer error, reinit writer |
| | | int64_t cur = cur_frame++; |
| | | if(!out_->writeFrame(pkt.data->getAVPacket(), cur)){ |
| | | AVPacket &op = pkt.data->getAVPacket(); |
| | | AVPacket np(op); |
| | | av_copy_packet(&np, &op); |
| | | if(!out_->writeFrame(np, cur)){ |
| | | av_packet_unref(&np); |
| | | end_writer(); |
| | | return -1; |
| | | } |
| | | av_packet_unref(&np); |
| | | if(pkt.id == id_frame_){ |
| | | file_frame_index_ = cur_frame; |
| | | file_frame_index_ = cur_frame-1; |
| | | } |
| | | // logIt("WRITE FRAME ID: %d, RECORD ID: %d", pkt.id, id_frame_); |
| | | return 0; |
| | | } |
| | | |
| | | void Recorder::end_writer(){ |
| | | if(cur_frame == -1) return; |
| | | out_->endWriter(); |
| | | logIt("INDEX %d, FILE %s, CURFrame %d, ENDFrame %d\n", |
| | | file_frame_index_, file_path_.c_str(), cur_frame, end_frame); |
| | | logIt("INDEX %d, REAL-FRAME-ID %d, FILE %s, CURFrame %d, ENDFrame %d\n", |
| | | file_frame_index_, id_frame_, file_path_.c_str(), cur_frame, end_frame); |
| | | |
| | | //reinit cur_frame clear list pkt |
| | | { |
| | |
| | | end_frame = minduration; |
| | | list_pkt_.clear(); |
| | | } |
| | | |
| | | //callback to frame index and path |
| | | if(func_rec_info_){ |
| | | func_rec_info_(id_,file_frame_index_, file_path_); |
| | | }else{ |
| | | // logIt("recorder has no func_rec_info"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | for(auto &i : pkts){ |
| | | if (cur_frame < end_frame){ |
| | | const int ret = write_correctly(i); |
| | | if(ret != 0){ |
| | | if(ret == -1) reinit_writer = true; |
| | | if(write_correctly(i) != 0){ |
| | | stop_recorder_.store(true); |
| | | break; |
| | | } |
| | | }else{ |
| | | end_writer(); |
| | | stop_recorder_.store(true); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | if (out_){ |
| | | delete out_; |
| | | out_ = NULL; |
| | | } |
| | | // stop_recorder_.store(false); |
| | | } |
| | | |
| | | int Recorder::Run(const char* output, const int mind, const int maxd){ |
| | |
| | | end_frame = minduration; |
| | | } |
| | | |
| | | logIt("min %d max %d endcount %d", minduration, maxduration, end_frame); |
| | | logIt("minduration %d maxduration %d curduration %d", minduration, maxduration, end_frame); |
| | | |
| | | std::thread([&]{ |
| | | run_thread(); |
| | |
| | | int Recorder::FireRecorder(const int64_t &id){ |
| | | if(cur_frame == -1){ |
| | | id_frame_ = id; |
| | | logIt("FIRST FIRE RECORD ID: %lld", id); |
| | | { |
| | | std::lock_guard<std::mutex> locker(mutex_pkt_); |
| | | cur_frame = 0; |
| | | if (list_pkt_.size() > end_frame){ |
| | | end_frame = list_pkt_.size() + minduration/2; |
| | | if (end_frame > maxduration) |
| | | end_frame = maxduration; |
| | | } |
| | | } |
| | | }else if(end_frame - cur_frame > minduration/2 && end_frame < maxduration){ |
| | | end_frame = end_frame + minduration / 2; |
| | |
| | | end_frame = maxduration; |
| | | } |
| | | } |
| | | logIt("FIRE REC FRAME ID: %lld", id); |
| | | // logIt("FIRE REC FRAME ID: %lld", id); |
| | | return 0; |
| | | } |
| | | |
| | |
| | | cv_.notify_one(); |
| | | } |
| | | |
| | | logIt("CACHE PACKET FRAME ID %lld", pkt.id); |
| | | return 0; |
| | | } |
| | | |