H264Rtp.h 3.16 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
26 27 28 29

#ifndef ZLMEDIAKIT_H264RTPCODEC_H
#define ZLMEDIAKIT_H264RTPCODEC_H

xiongziliang committed
30
#include "Rtsp/RtpCodec.h"
xiongziliang committed
31
#include "Util/ResourcePool.h"
32
#include "Extension/H264.h"
xiongziliang committed
33
using namespace toolkit;
34

xiongziliang committed
35
namespace mediakit{
xiongziliang committed
36

37 38
/**
 * h264 rtp解码类
3503207480@qq.com committed
39 40
 * 将 h264 over rtsp-rtp 解复用出 h264-Frame
 * rfc3984
41
 */
42
class H264RtpDecoder : public RtpCodec , public ResourcePoolHelper<H264Frame> {
43
public:
xiongziliang committed
44 45
    typedef std::shared_ptr<H264RtpDecoder> Ptr;

46
    H264RtpDecoder();
xiongziliang committed
47 48
    ~H264RtpDecoder() {}

49 50 51 52 53
    /**
     * 输入264 rtp包
     * @param rtp rtp包
     * @param key_pos 此参数忽略之
     */
xiongziliang committed
54
    bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
55 56 57 58 59 60 61 62

    TrackType getTrackType() const override{
        return TrackVideo;
    }

    CodecId getCodecId() const override{
        return CodecH264;
    }
63
private:
64
    bool decodeRtp(const RtpPacket::Ptr &rtp);
xiongziliang committed
65
    void onGetH264(const H264Frame::Ptr &frame);
66
    H264Frame::Ptr obtainFrame();
67
private:
68
    H264Frame::Ptr _h264frame;
69
    int _lastSeq = 0;
70 71
};

72 73 74
/**
 * 264 rtp打包类
 */
75
class H264RtpEncoder : public H264RtpDecoder ,public RtpInfo{
xiongziliang committed
76
public:
xiongziliang committed
77
    typedef std::shared_ptr<H264RtpEncoder> Ptr;
78 79 80 81 82 83 84 85

    /**
     * @param ui32Ssrc ssrc
     * @param ui32MtuSize mtu大小
     * @param ui32SampleRate 采样率,强制为90000
     * @param ui8PlayloadType pt类型
     * @param ui8Interleaved rtsp interleaved
     */
xiongziliang committed
86 87 88 89 90 91 92
    H264RtpEncoder(uint32_t ui32Ssrc,
                   uint32_t ui32MtuSize = 1400,
                   uint32_t ui32SampleRate = 90000,
                   uint8_t ui8PlayloadType = 96,
                   uint8_t ui8Interleaved = TrackVideo * 2);
    ~H264RtpEncoder() {}

93 94 95 96
    /**
     * 输入264帧
     * @param frame 帧数据,必须
     */
97
    void inputFrame(const Frame::Ptr &frame) override;
xiongziliang committed
98
private:
99
    void makeH264Rtp(int nal_type,const void *pData, unsigned int uiLen, bool bMark,  bool first_packet, uint32_t uiStamp);
xiongziliang committed
100 101
};

xiongziliang committed
102
}//namespace mediakit{
103 104

#endif //ZLMEDIAKIT_H264RTPCODEC_H