video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-16 602b3b2a792d65e49dba07920b100b5feb39d36f
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,21 +92,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 +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_);
            }else{
                // logIt("recorder has no func_rec_info");
                func_rec_info_(id_, file_frame_index_, file_path_);
            }
        }
@@ -115,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;
@@ -134,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){
@@ -167,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;
        }
@@ -179,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;
@@ -189,7 +233,7 @@
                    end_frame = maxduration;
                }
            }
            logIt("FIRE REC FRAME ID: %lld", id);
            // logIt("FIRE REC FRAME ID: %lld", id);
            return 0;
        }
@@ -219,7 +263,6 @@
                cv_.notify_one();
            }
            logIt("CACHE PACKET FRAME ID %lld", pkt.id);
            return 0;
        }
@@ -239,4 +282,4 @@
            }
        }
    }
}
}