RtpCodec.h 4.52 KB
Newer Older
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
/*
 * MIT License
 *
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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 30 31 32 33

#ifndef ZLMEDIAKIT_RTPCODEC_H
#define ZLMEDIAKIT_RTPCODEC_H

#include <memory>
#include "Util/RingBuffer.h"
#include "Rtsp/Rtsp.h"
#include "Player/PlayerBase.h"
xiongziliang committed
34
using namespace toolkit;
35

xiongziliang committed
36
namespace mediakit{
37

38
class RtpPacket {
39
public:
40 41 42 43 44
    typedef std::shared_ptr<RtpPacket> Ptr;
    uint8_t interleaved;
    uint8_t PT;
    bool mark;
    uint32_t length;
45
    //时间戳,单位毫秒
46 47 48 49 50 51 52 53 54 55 56
    uint32_t timeStamp;
    uint16_t sequence;
    uint32_t ssrc;
    uint8_t payload[1560];
    uint8_t offset;
    TrackType type;
};

class RtpRingInterface {
public:
    typedef RingBuffer<RtpPacket::Ptr> RingType;
xiongziliang committed
57
    typedef std::shared_ptr<RtpRingInterface> Ptr;
58 59 60

    RtpRingInterface(){}
    virtual ~RtpRingInterface(){}
xiongziliang committed
61 62 63 64 65

    /**
     * 获取rtp环形缓存
     * @return
     */
66
    virtual RingType::Ptr getRtpRing() const = 0;
xiongziliang committed
67 68 69 70 71

    /**
     * 设置rtp环形缓存
     * @param ring
     */
72
    virtual void setRtpRing(const RingType::Ptr &ring) = 0;
xiongziliang committed
73 74 75 76 77 78 79 80

    /**
     * 输入rtp包
     * @param rtp rtp包
     * @param key_pos 是否为关键帧第一个rtp包
     * @return 是否为关键帧第一个rtp包
     */
    virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) = 0;
81 82 83 84 85
};

class RtpRing : public RtpRingInterface {
public:
    typedef std::shared_ptr<RtpRing> Ptr;
86

87
    RtpRing(){
88
    }
89
    virtual ~RtpRing(){}
90

91
    RingType::Ptr getRtpRing() const override {
92 93 94
        return _rtpRing;
    }

95 96
    void setRtpRing(const RingType::Ptr &ring) override {
        _rtpRing = ring;
97
    }
98

xiongziliang committed
99
    bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override{
xiongziliang committed
100 101 102
        if(_rtpRing){
            _rtpRing->write(rtp,key_pos);
        }
xiongziliang committed
103
        return key_pos;
104
    }
105 106
protected:
    RingType::Ptr _rtpRing;
107 108 109
};


xiongziliang committed
110
class RtpInfo : public  ResourcePoolHelper<RtpPacket>{
111
public:
xiongziliang committed
112
    typedef std::shared_ptr<RtpInfo> Ptr;
113

xiongziliang committed
114
    RtpInfo(uint32_t ui32Ssrc,
115 116 117 118 119 120 121
            uint32_t ui32MtuSize,
            uint32_t ui32SampleRate,
            uint8_t ui8PlayloadType,
            uint8_t ui8Interleaved) {
        if(ui32Ssrc == 0){
            ui32Ssrc = ((uint64_t)this) & 0xFFFFFFFF;
        }
122 123 124 125 126
        _ui32Ssrc = ui32Ssrc;
        _ui32SampleRate = ui32SampleRate;
        _ui32MtuSize = ui32MtuSize;
        _ui8PlayloadType = ui8PlayloadType;
        _ui8Interleaved = ui8Interleaved;
127 128
    }

xiongziliang committed
129
    virtual ~RtpInfo(){}
130 131

    int getInterleaved() const {
132
        return _ui8Interleaved;
133 134 135
    }

    int getPlayloadType() const {
136
        return _ui8PlayloadType;
137 138 139
    }

    int getSampleRate() const {
140
        return _ui32SampleRate;
141 142 143
    }

    uint32_t getSsrc() const {
144
        return _ui32Ssrc;
145 146 147
    }

    uint16_t getSeqence() const {
148
        return _ui16Sequence;
149 150
    }
    uint32_t getTimestamp() const {
151
        return _ui32TimeStamp;
152 153
    }
    uint32_t getMtuSize() const {
154
        return _ui32MtuSize;
155
    }
xiongziliang committed
156
    RtpPacket::Ptr makeRtp(TrackType type,const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
157
protected:
158 159 160 161 162 163 164
    uint32_t _ui32Ssrc;
    uint32_t _ui32SampleRate;
    uint32_t _ui32MtuSize;
    uint8_t _ui8PlayloadType;
    uint8_t _ui8Interleaved;
    uint16_t _ui16Sequence = 0;
    uint32_t _ui32TimeStamp = 0;
165 166
};

xiongziliang committed
167
class RtpCodec : public RtpRing, public FrameRingInterfaceDelegate , public CodecInfo{
168 169 170 171 172
public:
    typedef std::shared_ptr<RtpCodec> Ptr;
    RtpCodec(){}
    virtual ~RtpCodec(){}
};
173

xiongziliang committed
174
}//namespace mediakit
175 176 177



178

179
#endif //ZLMEDIAKIT_RTPCODEC_H