H264.h 3.25 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
9
 */
xiongziliang committed
10

11 12
#ifndef ZLMEDIAKIT_H264_H
#define ZLMEDIAKIT_H264_H
xiongziliang committed
13 14

#include "Frame.h"
15
#include "Track.h"
xiongziliang committed
16
#include "Util/base64.h"
xiongziliang committed
17
#define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
18
using namespace toolkit;
xiongziliang committed
19

xiongziliang committed
20
namespace mediakit{
xiongziliang committed
21

22 23 24
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
void splitH264(const char *ptr, size_t len, size_t prefix, const std::function<void(const char *, size_t, size_t)> &cb);
size_t prefixSize(const char *ptr, size_t len);
25

xiongziliang committed
26
/**
27
 * 264帧类
xiongziliang committed
28
 */
xiongziliang committed
29
class H264Frame : public FrameImp {
xiongziliang committed
30
public:
31
    using Ptr = std::shared_ptr<H264Frame>;
xiongziliang committed
32

33
    enum {
xiongziliang committed
34
        NAL_IDR = 5,
35
        NAL_SEI = 6,
Zhou Weimin committed
36 37 38
        NAL_SPS = 7,
        NAL_PPS = 8,
        NAL_AUD = 9,
39
        NAL_B_P = 1,
40
    };
xiongziliang committed
41

42 43
    bool keyFrame() const override;
    bool configFrame() const override;
44

45 46 47 48
protected:
    friend class FrameImp;
    friend class ResourcePool_l<H264Frame>;
    H264Frame();
xiongziliang committed
49 50
};

51 52 53 54 55
/**
 * 防止内存拷贝的H264类
 * 用户可以通过该类型快速把一个指针无拷贝的包装成Frame类
 * 该类型在DevChannel中有使用
 */
xiongziliang committed
56
class H264FrameNoCacheAble : public FrameFromPtr {
xiongziliang committed
57
public:
58
    using Ptr = std::shared_ptr<H264FrameNoCacheAble>;
xiongziliang committed
59

60 61 62
    H264FrameNoCacheAble(char *ptr,size_t size,uint32_t dts , uint32_t pts ,size_t prefix_size = 4);
    bool keyFrame() const override;
    bool configFrame() const override;
xiongziliang committed
63 64
};

xiongziliang committed
65 66 67
/**
 * 264视频通道
 */
xiongziliang committed
68
class H264Track : public VideoTrack{
xiongziliang committed
69
public:
70
    using Ptr = std::shared_ptr<H264Track>;
71 72

    /**
xiongziliang committed
73 74 75
     * 不指定sps pps构造h264类型的媒体
     * 在随后的inputFrame中获取sps pps
     */
76 77
    H264Track() = default;

xiongziliang committed
78
    /**
79 80 81 82 83 84
     * 构造h264类型的媒体
     * @param sps sps帧数据
     * @param pps pps帧数据
     * @param sps_prefix_len 264头长度,可以为3个或4个字节,一般为0x00 00 00 01
     * @param pps_prefix_len 264头长度,可以为3个或4个字节,一般为0x00 00 00 01
     */
85
    H264Track(const string &sps,const string &pps,int sps_prefix_len = 4,int pps_prefix_len = 4);
86 87 88 89 90 91

    /**
     * 构造h264类型的媒体
     * @param sps sps帧
     * @param pps pps帧
     */
92
    H264Track(const Frame::Ptr &sps,const Frame::Ptr &pps);
93 94

    /**
95
     * 返回不带0x00 00 00 01头的sps/pps
96
     */
97 98
    const string &getSps() const;
    const string &getPps() const;
99

100 101 102 103 104 105
    bool ready() override;
    CodecId getCodecId() const override;
    int getVideoHeight() const override;
    int getVideoWidth() const override;
    float getVideoFps() const override;
    void inputFrame(const Frame::Ptr &frame) override;
xiongziliang committed
106

107
private:
108 109 110 111 112
    void onReady();
    Sdp::Ptr getSdp() override;
    Track::Ptr clone() override;
    void inputFrame_l(const Frame::Ptr &frame);
    void insertConfigFrame(const Frame::Ptr &frame);
xiongziliang committed
113

xiongziliang committed
114
private:
115
    bool _is_idr = false;
116 117 118
    int _width = 0;
    int _height = 0;
    float _fps = 0;
119 120
    string _sps;
    string _pps;
121 122
};

xiongziliang committed
123
}//namespace mediakit
124
#endif //ZLMEDIAKIT_H264_H