From 8fb24547e4d068cdcd491e59ddc451b3a252ea4b Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 30 七月 2019 15:42:53 +0800
Subject: [PATCH] update rec frame id

---
 csrc/wrapper.cpp |  206 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 120 insertions(+), 86 deletions(-)

diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index d67cb8d..e26c1f4 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -39,6 +39,8 @@
     ,gb_(0)
     ,cpu_(0)
     ,use_decoder_(false)
+    ,minduration(250)
+    ,maxduration(750)
     {
         makeTheWorld();
     }
@@ -100,6 +102,20 @@
         return nullptr;
     }
 
+    int Wrapper::RunStream(const char* input){
+        if(thread_){
+            logIt("wrapper run stream already run");
+            return 0;
+        }
+
+        input_url_ = input;
+
+        thread_.reset(new std::thread([&]{
+            run_stream_thread();
+        }));
+
+        return 0;
+    }
 
     void Wrapper::run_stream_thread(){
         
@@ -129,6 +145,10 @@
 
                 run_worker(in.get(), pkt);
                 if(!data){
+                    map_rec_.clear();
+                    std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
+                    list_rec_pkt_.clear();
+
                     break;
                 }
                 //test
@@ -139,19 +159,84 @@
         }
     }
 
-    int Wrapper::RunStream(const char* input){
-        if(thread_){
-            logIt("wrapper run stream already run");
-            return 0;
+    void Wrapper::run_worker(ffwrapper::FormatIn *in, avpacket &pkt){
+        if(!pkt.data) return;
+        if (use_decoder_) {
+            if(in->getCodecContext() == NULL){
+                
+                bool flag = true;
+                flag = in->openCodec(AVMEDIA_TYPE_VIDEO, NULL);
+                auto dec_ctx = in->getCodecContext();
+                if(bridge_)delete bridge_;
+
+                scale_w_ = scale_w_ == 0 || scale_w_ > dec_ctx->width ? dec_ctx->width : scale_w_;
+                scale_h_ = scale_h_ == 0 || scale_h_ > dec_ctx->height ? dec_ctx->height : scale_h_;
+
+                AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
+                bridge_ = new cvbridge(
+                        dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
+                        scale_w_, scale_h_, pix_fmt, scale_f_);
+    
+                if (!flag){
+                    logIt("FormatIn openCodec Failed!");
+                }
+            }
+            
+            auto frame(std::make_shared<FrameData>());
+            auto ret = in->decode(frame, pkt.data);
+            if(ret == 1){
+                //鍚愬嚭鏁版嵁
+                cache_pic(frame, pkt.id);
+            }
         }
+        cache_rec_pkt(pkt);
+        for(auto &i : map_rec_){
+            if (!i.second.rec){
+                i.second.rec = i.second.fn_init(in);
+                if (i.second.rec){
+                    std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
+                    for(auto &k : list_rec_pkt_){
+                        avpacket p = {k.data, k.id};
+                        i.second.rec->CachePacket(p);
+                    }
+                    logIt("start rec %d frames", list_rec_pkt_.size());
+                }
+            }else if (i.second.rec){
+                i.second.rec->CachePacket(pkt);
+            }
+        }
+    }
 
-        input_url_ = input;
+    int Wrapper::cache_rec_pkt(const avpacket &pkt){
 
-        thread_.reset(new std::thread([&]{
-            run_stream_thread();
-        }));
-
+        std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
+        //wait I 
+        if (list_rec_pkt_.empty()) {
+            AVPacket &avpkt = pkt.data->getAVPacket();
+            if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
+                return -1;
+            }
+        }
+        maybe_dump_rec_pkt();
+        recpkt k = {pkt.data, pkt.id};
+        list_rec_pkt_.push_back(k);
+        
         return 0;
+    }
+    void Wrapper::maybe_dump_rec_pkt(){
+        //瓒呰繃min/2,涓㈠純gop
+        while (list_rec_pkt_.size() > minduration) {
+            list_rec_pkt_.pop_front();
+            while(!list_rec_pkt_.empty()){
+                auto &cache = list_rec_pkt_.front();
+                AVPacket &avpkt = cache.data->getAVPacket();
+                if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
+                    list_rec_pkt_.pop_front();
+                }else{
+                    break;
+                }
+            }
+        }
     }
 
     //////////////recorder
@@ -189,6 +274,9 @@
         
         FnRec r = FnRec{fn, rec};
         map_rec_[rid] = r;
+
+        minduration = mindur * 25;
+        maxduration = maxdur * 25;
     }
 
     int Wrapper::FireRecorder(const char* sid,const int64_t &id){
@@ -198,67 +286,24 @@
                 iter->second.rec->FireRecorder(id);
             }
         }
-    }
-
-    void Wrapper::run_worker(ffwrapper::FormatIn *in, avpacket &pkt){
-        if(!pkt.data) return;
-        if (use_decoder_) {
-            if(in->getCodecContext() == NULL){
-                
-                bool flag = true;
-                flag = in->openCodec(AVMEDIA_TYPE_VIDEO, NULL);
-                auto dec_ctx = in->getCodecContext();
-                if(bridge_)delete bridge_;
-
-                scale_w_ = scale_w_ == 0 || scale_w_ > dec_ctx->width ? dec_ctx->width : scale_w_;
-                scale_h_ = scale_h_ == 0 || scale_h_ > dec_ctx->height ? dec_ctx->height : scale_h_;
-
-                AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
-                bridge_ = new cvbridge(
-                        dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
-                        scale_w_, scale_h_, pix_fmt, scale_f_);
-    
-                if (!flag){
-                    logIt("FormatIn openCodec Failed!");
-                }
-            }
-            
-            auto frame(std::make_shared<FrameData>());
-            auto ret = in->decode(frame, pkt.data);
-            if(ret == 1){
-                //鍚愬嚭鏁版嵁
-                cache_pic(frame);
-            }
-        }
-
-        for(auto &i : map_rec_){
-            if (!i.second.rec){
-                i.second.rec = i.second.fn_init(in);
-            }
-            if (i.second.rec){
-                i.second.rec->CachePacket(pkt);
-            }
-        }
+        logIt("FIRE REC %s, FRAME ID: %d", sid, id);
     }
 
     void Wrapper::cache_rec_info(std::string &id, int &index, std::string &path){
-        if(func_rec_){ //active api
-            func_rec_(path, index);
-        }else{                  // passive api
-            std::lock_guard<std::mutex> l(mutex_rec_);
-            while(list_rec_.size() > 100){
-                for(int i = 0; i < 25; i++){
-                    list_rec_.pop_front();
-                }
+        
+        std::lock_guard<std::mutex> l(mutex_rec_);
+        while(list_rec_.size() > 100){
+            for(int i = 0; i < 25; i++){
+                list_rec_.pop_front();
             }
-            struct record_file_info info;
-            info.file_frame_index = index;
-            info.file_path = path;
-            list_rec_.emplace_back(info);
-            list_rec_map_[path] = id;
-            logIt("list rec files count : %d", list_rec_.size());
-            map_rec_.erase(id);
         }
+        struct record_file_info info;
+        info.file_frame_index = index;
+        info.file_path = path;
+        list_rec_.emplace_back(info);
+        list_rec_map_[path] = id;
+        logIt("list rec files count : %d", list_rec_.size());
+        map_rec_.erase(id);
     }
 
     void Wrapper::GetInfoRecorder(int &index, std::string &path){
@@ -276,18 +321,20 @@
     }
 
     std::string Wrapper::GetRecorderID(const std::string &path){
+        std::string ret("");
         auto iter = list_rec_map_.find(path);
         if (iter != list_rec_map_.end()){
-            return iter->second;
+            ret = iter->second;
+            list_rec_map_.erase(iter);
         }
-        return "";
+        return ret;
     }
     ////////decoder
     void Wrapper::BuildDecoder(){
         use_decoder_ = true;
     }
 
-    void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame){
+    void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame, int64_t &id){
 
         pic_bgr24 pic;
         if(bridge_){
@@ -298,10 +345,10 @@
             unsigned char *data = (unsigned char*)malloc(pic.w * pic.h * 3);
             bridge_->copyPicture(data, frm);
             pic.data = data;
+            pic.id = id;
         }
-        if(func_dec_){
-            func_dec_(pic.data, pic.w, pic.h);
-        }else{
+        
+        {
             std::lock_guard<std::mutex> l(mutex_pic_);
             while(list_pic_.size() > 10){
                 for(int i = 0; i < 5; i++){
@@ -315,7 +362,7 @@
 
     }
 
-    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h){
+    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id){
         std::lock_guard<std::mutex> l(mutex_pic_);
         if(list_pic_.empty()){
             *data = NULL;
@@ -325,6 +372,7 @@
         }
         auto p = list_pic_.front();
         *data = p.data; *w = p.w; *h = p.h;
+        *id = p.id;
         list_pic_.pop_front();
     }
 
@@ -350,20 +398,6 @@
             }
         }
         list_avpkt_.emplace_back(pkt);
-    }
-
-
-    ///// active api
-    void Wrapper::ActiveRecorder(const char *dir, const int mind, const int maxd,
-                                FUNC_REC func){
-
-        BuildRecorder("", dir, mind, maxd);
-        func_rec_ = func;
-    }
-
-    void Wrapper::ActiveDecoder(FUNC_DEC fn){
-        BuildDecoder();
-        func_dec_ = fn;
     }
 
     ////// test

--
Gitblit v1.8.0