RtpCodec.h 4.2 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 30 31 32

#ifndef ZLMEDIAKIT_RTPCODEC_H
#define ZLMEDIAKIT_RTPCODEC_H

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

xiongziliang committed
35
namespace mediakit{
36

37 38 39
class RtpRingInterface {
public:
    typedef RingBuffer<RtpPacket::Ptr> RingType;
xiongziliang committed
40
    typedef std::shared_ptr<RtpRingInterface> Ptr;
41 42 43

    RtpRingInterface(){}
    virtual ~RtpRingInterface(){}
xiongziliang committed
44 45 46 47 48

    /**
     * 获取rtp环形缓存
     * @return
     */
49
    virtual RingType::Ptr getRtpRing() const = 0;
xiongziliang committed
50 51 52 53 54

    /**
     * 设置rtp环形缓存
     * @param ring
     */
55
    virtual void setRtpRing(const RingType::Ptr &ring) = 0;
xiongziliang committed
56 57 58 59 60 61 62 63

    /**
     * 输入rtp包
     * @param rtp rtp包
     * @param key_pos 是否为关键帧第一个rtp包
     * @return 是否为关键帧第一个rtp包
     */
    virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) = 0;
64 65 66 67 68
};

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

70
    RtpRing(){
71
    }
72
    virtual ~RtpRing(){}
73

74
    RingType::Ptr getRtpRing() const override {
75 76 77
        return _rtpRing;
    }

78 79
    void setRtpRing(const RingType::Ptr &ring) override {
        _rtpRing = ring;
80
    }
81

xiongziliang committed
82
    bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override{
xiongziliang committed
83 84 85
        if(_rtpRing){
            _rtpRing->write(rtp,key_pos);
        }
xiongziliang committed
86
        return key_pos;
87
    }
88 89
protected:
    RingType::Ptr _rtpRing;
90 91 92
};


xiongziliang committed
93
class RtpInfo : public  ResourcePoolHelper<RtpPacket>{
94
public:
xiongziliang committed
95
    typedef std::shared_ptr<RtpInfo> Ptr;
96

xiongziliang committed
97
    RtpInfo(uint32_t ui32Ssrc,
98 99 100 101 102 103 104
            uint32_t ui32MtuSize,
            uint32_t ui32SampleRate,
            uint8_t ui8PlayloadType,
            uint8_t ui8Interleaved) {
        if(ui32Ssrc == 0){
            ui32Ssrc = ((uint64_t)this) & 0xFFFFFFFF;
        }
105 106 107 108 109
        _ui32Ssrc = ui32Ssrc;
        _ui32SampleRate = ui32SampleRate;
        _ui32MtuSize = ui32MtuSize;
        _ui8PlayloadType = ui8PlayloadType;
        _ui8Interleaved = ui8Interleaved;
110 111
    }

xiongziliang committed
112
    virtual ~RtpInfo(){}
113 114

    int getInterleaved() const {
115
        return _ui8Interleaved;
116 117 118
    }

    int getPlayloadType() const {
119
        return _ui8PlayloadType;
120 121 122
    }

    int getSampleRate() const {
123
        return _ui32SampleRate;
124 125 126
    }

    uint32_t getSsrc() const {
127
        return _ui32Ssrc;
128 129 130
    }

    uint16_t getSeqence() const {
131
        return _ui16Sequence;
132 133
    }
    uint32_t getTimestamp() const {
134
        return _ui32TimeStamp;
135 136
    }
    uint32_t getMtuSize() const {
137
        return _ui32MtuSize;
138
    }
xiongziliang committed
139
    RtpPacket::Ptr makeRtp(TrackType type,const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
140
protected:
141 142 143 144 145 146 147
    uint32_t _ui32Ssrc;
    uint32_t _ui32SampleRate;
    uint32_t _ui32MtuSize;
    uint8_t _ui8PlayloadType;
    uint8_t _ui8Interleaved;
    uint16_t _ui16Sequence = 0;
    uint32_t _ui32TimeStamp = 0;
148 149
};

xiongziliang committed
150
class RtpCodec : public RtpRing, public FrameRingInterfaceDelegate , public CodecInfo{
151 152 153 154 155
public:
    typedef std::shared_ptr<RtpCodec> Ptr;
    RtpCodec(){}
    virtual ~RtpCodec(){}
};
156

xiongziliang committed
157
}//namespace mediakit
158 159 160



161

162
#endif //ZLMEDIAKIT_RTPCODEC_H