From 602b3b2a792d65e49dba07920b100b5feb39d36f Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 16 九月 2019 13:43:40 +0800
Subject: [PATCH] remove log

---
 csrc/buz/recorder.cpp |   99 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 71 insertions(+), 28 deletions(-)

diff --git a/csrc/buz/recorder.cpp b/csrc/buz/recorder.cpp
index 4bfd370..a7086fe 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>
@@ -17,29 +19,44 @@
 
 namespace cffmpeg_wrap{
     namespace buz{
-        Recorder::Recorder(FormatIn *in)
+        Recorder::Recorder(FormatIn *in, const std::string &id)
         :in_(in)
         ,out_(NULL)
         ,maxduration(30 * 25)
         ,minduration(10 * 25)
         ,end_frame(minduration)
         ,cur_frame(-1)
-        ,thread_(nullptr)
         ,stop_recorder_(false)
+        ,id_(id)
         ,id_frame_(0)
         ,file_frame_index_(-1)
         ,file_path_("")
         ,func_rec_info_(nullptr)
-        {}
+        ,thrd_(nullptr)
+        ,error_occured_(false)
+        {
+            // logIt("RECODER ID: %s", id_.c_str());
+        }
 
         Recorder::~Recorder(){
-            if(thread_){
-                stop_recorder_.store(true);
-                cv_.notify_one();
-                thread_->join();
+            
+            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!!!");
+                }
             }
-            if(out_)
-                delete out_;
+            catch(const std::exception& e)
+            {
+                logIt("RECODER DESTRUCTOR EXCEPTION: ", e.what());
+            }
+            
         }
 
         int Recorder::init_writer(){
@@ -59,9 +76,11 @@
 
         void Recorder::start_writer(){
             if (cur_frame == 0) {
-                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());
             }
         }
 
@@ -73,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_);
@@ -93,12 +121,10 @@
                 end_frame = minduration;
                 list_pkt_.clear();
             }
+
             //callback to frame index and path
             if(func_rec_info_){
-                func_rec_info_(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");
+                func_rec_info_(id_, file_frame_index_, file_path_);
             }
         }
 
@@ -117,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;
@@ -136,24 +168,27 @@
 
                 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){
-            if(thread_){
-                logIt("recorder already run");
-                return 0;
-            }
 
             dir_ = output;
             int ret = init_writer();
@@ -169,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);    
 
-            thread_.reset(new std::thread([&]{
+            thrd_.reset(new std::thread([&]{
                 run_thread();
             }));
+            //.detach();
 
             return 0;
         }
@@ -181,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;
@@ -191,6 +233,7 @@
                     end_frame = maxduration;
                 }
             }
+            // logIt("FIRE REC FRAME ID: %lld", id);
             return 0;
         }
 
@@ -239,4 +282,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0