RtpCodec.h 2.45 KB
Newer Older
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
 */
10 11 12 13 14 15 16

#ifndef ZLMEDIAKIT_RTPCODEC_H
#define ZLMEDIAKIT_RTPCODEC_H

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

xiongziliang committed
19
namespace mediakit{
20

xiongziliang committed
21
class RtpRing{
22
public:
xiongziliang committed
23
    typedef std::shared_ptr<RtpRing> Ptr;
24 25
    typedef RingBuffer<RtpPacket::Ptr> RingType;

xiongziliang committed
26 27
    RtpRing(){}
    virtual ~RtpRing(){}
xiongziliang committed
28 29 30 31 32

    /**
     * 获取rtp环形缓存
     * @return
     */
xiongziliang committed
33 34 35
    virtual RingType::Ptr getRtpRing() const {
        return _rtpRing;
    }
xiongziliang committed
36 37 38 39 40

    /**
     * 设置rtp环形缓存
     * @param ring
     */
xiongziliang committed
41 42 43
    virtual void setRtpRing(const RingType::Ptr &ring){
        _rtpRing = ring;
    }
xiongziliang committed
44 45 46 47 48 49 50

    /**
     * 输入rtp包
     * @param rtp rtp包
     * @param key_pos 是否为关键帧第一个rtp包
     * @return 是否为关键帧第一个rtp包
     */
xiongziliang committed
51
    virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos){
xiongziliang committed
52 53 54
        if(_rtpRing){
            _rtpRing->write(rtp,key_pos);
        }
xiongziliang committed
55
        return key_pos;
56
    }
57 58
protected:
    RingType::Ptr _rtpRing;
59 60
};

xia-chu committed
61
class RtpInfo{
62
public:
xia-chu committed
63
    using Ptr = std::shared_ptr<RtpInfo>;
64

xiongziliang committed
65 66 67
    RtpInfo(uint32_t ssrc, size_t mtu_size, uint32_t sample_rate, uint8_t pt, uint8_t interleaved) {
        if (ssrc == 0) {
            ssrc = ((uint64_t) this) & 0xFFFFFFFF;
68
        }
xiongziliang committed
69 70 71 72 73
        _pt = pt;
        _ssrc = ssrc;
        _mtu_size = mtu_size;
        _sample_rate = sample_rate;
        _interleaved = interleaved;
74 75
    }

xia-chu committed
76
    virtual ~RtpInfo() {}
77

xiongziliang committed
78 79 80
    //返回rtp负载最大长度
    size_t getMaxSize() const {
        return _mtu_size - RtpPacket::kRtpHeaderSize;
81 82 83
    }

    uint32_t getSsrc() const {
xiongziliang committed
84
        return _ssrc;
85 86
    }

87
    RtpPacket::Ptr makeRtp(TrackType type,const void *data, size_t len, bool mark, uint32_t stamp);
88

xiongziliang committed
89 90 91 92 93 94 95
private:
    uint8_t _pt;
    uint8_t _interleaved;
    uint16_t _seq = 0;
    uint32_t _ssrc;
    uint32_t _sample_rate;
    size_t _mtu_size;
96 97
};

xiongziliang committed
98
class RtpCodec : public RtpRing, public FrameDispatcher , public CodecInfo{
99 100 101 102 103
public:
    typedef std::shared_ptr<RtpCodec> Ptr;
    RtpCodec(){}
    virtual ~RtpCodec(){}
};
104

xiongziliang committed
105
}//namespace mediakit
106 107 108



109

110
#endif //ZLMEDIAKIT_RTPCODEC_H