From f6172eaa5f78d2af04e8e99fdf26b2b0ad4994d6 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期四, 01 八月 2019 15:51:19 +0800
Subject: [PATCH] bug fix record file thread quit dup pkt

---
 csrc/buz/recorder.cpp |   62 ++++++++++++++++++++++---------
 1 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/csrc/buz/recorder.cpp b/csrc/buz/recorder.cpp
index d80cea9..2cb3b12 100644
--- a/csrc/buz/recorder.cpp
+++ b/csrc/buz/recorder.cpp
@@ -1,4 +1,5 @@
 #include "recorder.hpp"
+#include "sole.hpp"
 
 #include <thread>
 #include <unistd.h>
@@ -30,11 +31,22 @@
         ,file_frame_index_(-1)
         ,file_path_("")
         ,func_rec_info_(nullptr)
-        {}
+        {
+            // logIt("RECODER ID: %s", id_.c_str());
+        }
 
         Recorder::~Recorder(){
-            stop_recorder_.store(true);
-            cv_.notify_one();    
+            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(){
@@ -54,10 +66,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,21 +82,27 @@
             }
             // 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
             {
@@ -92,11 +111,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_);
-            }else{
-                // logIt("recorder has no func_rec_info");
             }
         }
 
@@ -134,21 +152,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){
@@ -167,7 +188,7 @@
                 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();
@@ -179,9 +200,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;
@@ -189,7 +216,7 @@
                     end_frame = maxduration;
                 }
             }
-            logIt("FIRE REC FRAME ID: %lld", id);
+            // logIt("FIRE REC FRAME ID: %lld", id);
             return 0;
         }
 
@@ -219,7 +246,6 @@
                 cv_.notify_one();
             }
 
-            logIt("CACHE PACKET FRAME ID %lld", pkt.id);
             return 0;
         }
 

--
Gitblit v1.8.0