video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2023-12-26 18a05d269516a5e33d8460291c2f93e73d95adce
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
#include "stream.hpp"
 
extern "C"{
#include <libavcodec/avcodec.h>
}
 
#include "../ffmpeg/format/FormatIn.hpp"
#include "../ffmpeg/data/CodedData.hpp"
#include "../ffmpeg/log/log.hpp"
using namespace logif;
 
namespace cffmpeg_wrap{
    stream::stream(ffwrapper::FormatIn *in, const int maxSize)
    :streamRef_(in)
    ,max_size_(maxSize)
    ,audio_(false)
    {}
 
    stream::~stream(){
        std::lock_guard<std::mutex> locker(mutex_avpkt_);
        list_pkt_.clear();
    }
 
    int stream::SetPacket(const CPacket &pkt){
        if (pkt.data){
            // 如果包是音频包,但是不使用音频,直接返回
            if (!audio_ && streamRef_->isAudioPkt(&pkt.data->getAVPacket())){
                return 0;
            }
            
            std::lock_guard<std::mutex> locker(mutex_avpkt_);
            list_pkt_.push_back(pkt);
            while(list_pkt_.size() > max_size_/2*3){
                CPacket &tmpkt = list_pkt_.front();
                if (tmpkt.data->getAVPacket().flags & AV_PKT_FLAG_KEY){
                    break;
                }
                list_pkt_.pop_front();
            }
 
            return list_pkt_.size();
        }
        return 0;
    }
 
    void stream::GetPacket(unsigned char **pktData, int *size, int *key){
        std::lock_guard<std::mutex> l(mutex_avpkt_);
        if(list_pkt_.empty()){
            return;
        }
 
        auto data = list_pkt_.front();
        list_pkt_.pop_front();
 
        auto pkt = data.data->getAVPacket();
        *key = pkt.flags & AV_PKT_FLAG_KEY;
        *size = pkt.size;
        *pktData = (unsigned char *)malloc(*size);
        memcpy(*pktData, pkt.data, pkt.size);
    }
}