From a6dd7933e0bd8ae1fd083639758f7fee9fc7a151 Mon Sep 17 00:00:00 2001 From: chenshijun <csj_sky@126.com> Date: 星期二, 10 九月 2019 16:34:10 +0800 Subject: [PATCH] Merge branch 'master' of ssh://192.168.1.14:29418/valib/goffmpeg --- csrc/buz/recorder.cpp | 85 +++++++++++++++++++++++++++++++++--------- 1 files changed, 66 insertions(+), 19 deletions(-) diff --git a/csrc/buz/recorder.cpp b/csrc/buz/recorder.cpp index f83a596..2da437c 100644 --- a/csrc/buz/recorder.cpp +++ b/csrc/buz/recorder.cpp @@ -1,7 +1,9 @@ #include "recorder.hpp" +#include "sole.hpp" #include <thread> #include <unistd.h> +#include <chrono> extern "C"{ #include <libavcodec/avcodec.h> @@ -30,11 +32,31 @@ ,file_frame_index_(-1) ,file_path_("") ,func_rec_info_(nullptr) - {} + ,thrd_(nullptr) + ,error_occured_(false) + { + // logIt("RECODER ID: %s", id_.c_str()); + } Recorder::~Recorder(){ - stop_recorder_.store(true); - cv_.notify_one(); + + 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(){ @@ -54,10 +76,11 @@ 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()); } } @@ -69,19 +92,28 @@ } // 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, 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 { std::lock_guard<std::mutex> locker(mutex_pkt_); @@ -89,12 +121,10 @@ 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_); - // logIt("recoder index %d, file name %s\n", file_frame_index_, file_path_.c_str()); - }else{ - // logIt("recorder has no func_rec_info"); } } @@ -113,9 +143,15 @@ 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; @@ -132,21 +168,24 @@ 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){ @@ -165,11 +204,12 @@ 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([&]{ + thrd_.reset(new std::thread([&]{ run_thread(); - }).detach(); + })); + //.detach(); return 0; } @@ -177,9 +217,15 @@ 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; @@ -187,6 +233,7 @@ end_frame = maxduration; } } + // logIt("FIRE REC FRAME ID: %lld", id); return 0; } @@ -235,4 +282,4 @@ } } } -} \ No newline at end of file +} -- Gitblit v1.8.0