video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-18 b73029149580370e62dd6c14a270aea902f85cf2
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
#include "stream.hpp"
 
#include "../ffmpeg/data/CodedData.hpp"
 
namespace cffmpeg_wrap{
    stream::stream(const int maxSize)
    :max_size_(maxSize)
    {}
 
    stream::~stream(){
        std::lock_guard<std::mutex> locker(mutex_avpkt_);
        list_avpkt_.clear();
    }
 
    int stream::SetPacket(std::shared_ptr<ffwrapper::CodedData> data){
        if (data){
            std::lock_guard<std::mutex> locker(mutex_avpkt_);
            list_avpkt_.push_back(data);
            
            while(list_avpkt_.size() > max_size_){
                list_avpkt_.pop_front();
                while(!list_avpkt_.empty()){
                    auto &cache = list_avpkt_.front();
                    AVPacket &avpkt = cache->getAVPacket();
                    if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                        list_avpkt_.pop_front();
                    }else{
                        break;
                    }
                }
            }
            return list_avpkt_.size();
        }
        return 0;
    }
 
    void stream::GetPacket(unsigned char **pktData, int *size, int *key){
        std::lock_guard<std::mutex> l(mutex_avpkt_);
        if(list_avpkt_.empty()){
            return;
        }
        auto data = list_avpkt_.front();
        auto pkt = data->getAVPacket();
        *key = pkt.flags & AV_PKT_FLAG_KEY;
        *size = pkt.size;
        *pktData = (unsigned char *)malloc(*size);
        memcpy(*pktData, pkt.data, pkt.size);
 
        list_avpkt_.pop_front();
    }
}