video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-16 602b3b2a792d65e49dba07920b100b5feb39d36f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef _cffmpeg_rec_hpp_
#define _cffmpeg_rec_hpp_
 
#include <string>
#include <memory>
#include <unordered_map>
#include <list>
#include <mutex>
 
namespace ffwrapper
{
    class FormatIn;
    class CodedData;
} // namespace ffwrapper
 
 
namespace cffmpeg_wrap
{
    namespace buz{
        class Recorder;
        struct avpacket;
    }
 
 
    class rec
    {
    private:
        ffwrapper::FormatIn *recRef_;
        int     maxduration_;
        int     minduration_;
 
        // 录像的实例,对应任务
        typedef struct _fn_rec{
            std::string rid;        //id对应任务id
            std::string dir;
            int min;
            int max;
            std::unique_ptr<buz::Recorder> rec;    
        }FnRec;
        std::unordered_map<std::string, FnRec> map_rec_;
        // 多线程添加任务实例,在读流线程使用录像,但是添加在另一个线程
        std::mutex mtx_rec_;
 
        // recoder将录像文件的信息放到此处
        typedef struct record_file_info{
            int frmIdx;
            std::string fPath;
            std::string recID;
        }RecInfo;
        std::list<RecInfo>  list_recInfo_;
        // 多线程录像,加锁获取录像后的信息
        std::mutex mtx_recInfo_;
 
        // 缓存的视频帧,等待firerecsignal触发开始录像
        typedef struct _cache_pkt{
            std::shared_ptr<ffwrapper::CodedData> data;
            int64_t id;
        }CPacket;
        std::list<CPacket> list_pkt_;
        // 多线程,生产者线程reader push pkt,消费者,录像线程pop
        std::mutex mtx_pkt_;
 
    private: 
        // 创建录像实例
        std::unique_ptr<buz::Recorder> newRec(std::string id, std::string dir, const int mind, const int maxd);
        // 录像实例的回调函数,录像完成后设置录像文件路径,id和帧id
        void setRecInfo(std::string &id, int &index, std::string &path);
        // 缓存视频包
        void cachePacket(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id);
        // 丢弃缓存
        int shrinkCache();
    public:
        void NewRec(const char* id, const char *output, const int mindur, const int maxdur);
 
        // 缓存录像的视频包,等待触发录像,或直接放到录像缓存
        void SetPacket(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id);
        // 触发录像
        void FireRecSignal(const char* sid,const int64_t &id);
        // 获取录像文件路径和帧id
        void GetRecInfo(std::string &recID, int &index, std::string &path);
        
    public:
        explicit rec(ffwrapper::FormatIn *in);
        ~rec();
    };
} // namespace cffmpeg_wrap
 
 
#endif