Rtsp.h 6.64 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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.
xiongziliang committed
9
 */
xiongziliang committed
10

xzl committed
11 12 13 14 15 16
#ifndef RTSP_RTSP_H_
#define RTSP_RTSP_H_

#include <string.h>
#include <string>
#include <memory>
xiongzilaing committed
17
#include <unordered_map>
xiongziliang committed
18
#include "Common/config.h"
19
#include "Util/util.h"
20
#include "Extension/Frame.h"
xiongzilaing committed
21

xzl committed
22
using namespace std;
xiongziliang committed
23 24
using namespace toolkit;
using namespace mediakit;
xzl committed
25

xiongziliang committed
26 27 28 29
namespace mediakit {

namespace Rtsp {
typedef enum {
30 31 32 33
    RTP_Invalid = -1,
    RTP_TCP = 0,
    RTP_UDP = 1,
    RTP_MULTICAST = 2,
xiongziliang committed
34
} eRtpType;
xiongziliang committed
35 36

#define RTP_PT_MAP(XX) \
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    XX(PCMU, TrackAudio, 0, 8000, 1, CodecG711U) \
    XX(GSM, TrackAudio , 3, 8000, 1, CodecInvalid) \
    XX(G723, TrackAudio, 4, 8000, 1, CodecInvalid) \
    XX(DVI4_8000, TrackAudio, 5, 8000, 1, CodecInvalid) \
    XX(DVI4_16000, TrackAudio, 6, 16000, 1, CodecInvalid) \
    XX(LPC, TrackAudio, 7, 8000, 1, CodecInvalid) \
    XX(PCMA, TrackAudio, 8, 8000, 1, CodecG711A) \
    XX(G722, TrackAudio, 9, 8000, 1, CodecInvalid) \
    XX(L16_Stereo, TrackAudio, 10, 44100, 2, CodecInvalid) \
    XX(L16_Mono, TrackAudio, 11, 44100, 1, CodecInvalid) \
    XX(QCELP, TrackAudio, 12, 8000, 1, CodecInvalid) \
    XX(CN, TrackAudio, 13, 8000, 1, CodecInvalid) \
    XX(MPA, TrackAudio, 14, 90000, 1, CodecInvalid) \
    XX(G728, TrackAudio, 15, 8000, 1, CodecInvalid) \
    XX(DVI4_11025, TrackAudio, 16, 11025, 1, CodecInvalid) \
    XX(DVI4_22050, TrackAudio, 17, 22050, 1, CodecInvalid) \
    XX(G729, TrackAudio, 18, 8000, 1, CodecInvalid) \
    XX(CelB, TrackVideo, 25, 90000, 1, CodecInvalid) \
    XX(JPEG, TrackVideo, 26, 90000, 1, CodecInvalid) \
    XX(nv, TrackVideo, 28, 90000, 1, CodecInvalid) \
    XX(H261, TrackVideo, 31, 90000, 1, CodecInvalid) \
    XX(MPV, TrackVideo, 32, 90000, 1, CodecInvalid) \
    XX(MP2T, TrackVideo, 33, 90000, 1, CodecInvalid) \
    XX(H263, TrackVideo, 34, 90000, 1, CodecInvalid) \
xiongziliang committed
61 62

typedef enum {
63
#define ENUM_DEF(name, type, value, clock_rate, channel, codec_id) PT_ ## name = value,
xiongziliang committed
64 65 66 67 68
    RTP_PT_MAP(ENUM_DEF)
#undef ENUM_DEF
    PT_MAX = 128
} PayloadType;

xiongziliang committed
69 70
};

xiongziliang committed
71 72
class RtpPacket : public BufferRaw{
public:
73 74 75 76 77 78 79 80 81 82
    typedef std::shared_ptr<RtpPacket> Ptr;
    uint8_t interleaved;
    uint8_t PT;
    bool mark;
    //时间戳,单位毫秒
    uint32_t timeStamp;
    uint16_t sequence;
    uint32_t ssrc;
    uint32_t offset;
    TrackType type;
xiongziliang committed
83 84
};

xiongziliang committed
85 86 87 88 89 90
class RtpPayload{
public:
    static int getClockRate(int pt);
    static TrackType getTrackType(int pt);
    static int getAudioChannel(int pt);
    static const char *getName(int pt);
91
    static CodecId getCodecId(int pt);
xiongziliang committed
92 93 94 95 96
private:
    RtpPayload() = delete;
    ~RtpPayload() = delete;
};

xiongziliang committed
97 98
class RtcpCounter {
public:
99 100 101 102 103
    uint32_t pktCnt = 0;
    uint32_t octCount = 0;
    //网络字节序
    uint32_t timeStamp = 0;
    uint32_t lastTimeStamp = 0;
xiongziliang committed
104 105
};

xiongziliang committed
106
class SdpTrack {
107
public:
108
    typedef std::shared_ptr<SdpTrack> Ptr;
109

110 111 112 113 114 115 116
    string _m;
    string _o;
    string _s;
    string _i;
    string _c;
    string _t;
    string _b;
xiongziliang committed
117
    uint16_t _port;
118

119 120 121
    float _duration = 0;
    float _start = 0;
    float _end = 0;
122

123 124
    map<char, string> _other;
    map<string, string> _attr;
125

126
    string toString() const;
xiongziliang committed
127
    string getName() const;
128
public:
129 130 131
    int _pt;
    string _codec;
    int _samplerate;
xiongziliang committed
132
    int _channel;
133 134 135 136
    string _fmtp;
    string _control;
    string _control_surffix;
    TrackType _type;
xiongziliang committed
137
public:
138 139 140 141 142 143
    uint8_t _interleaved = 0;
    bool _inited = false;
    uint32_t _ssrc = 0;
    uint16_t _seq = 0;
    //时间戳,单位毫秒
    uint32_t _time_stamp = 0;
144
};
xiongziliang committed
145

xiongziliang committed
146
class SdpParser {
147
public:
148 149 150 151 152 153 154 155 156 157
    typedef std::shared_ptr<SdpParser> Ptr;

    SdpParser() {}
    SdpParser(const string &sdp) { load(sdp); }
    ~SdpParser() {}
    void load(const string &sdp);
    bool available() const;
    SdpTrack::Ptr getTrack(TrackType type) const;
    vector<SdpTrack::Ptr> getAvailableTrack() const;
    string toString() const ;
158
private:
159
    vector<SdpTrack::Ptr> _track_vec;
160
};
xzl committed
161

162 163 164 165 166
/**
 * 解析rtsp url的工具类
 */
class RtspUrl{
public:
167 168 169 170 171 172
    string _url;
    string _user;
    string _passwd;
    string _host;
    uint16_t _port;
    bool _is_ssl;
173
public:
174 175 176
    RtspUrl() = default;
    ~RtspUrl() = default;
    bool parse(const string &url);
177
private:
178
    bool setup(bool,const string &, const string &, const string &);
179
};
xzl committed
180

xiongziliang committed
181 182 183 184 185
/**
* rtsp sdp基类
*/
class Sdp : public CodecInfo{
public:
186 187 188 189 190
    typedef std::shared_ptr<Sdp> Ptr;

    /**
     * 构造sdp
     * @param sample_rate 采样率
xiongziliang committed
191
     * @param payload_type pt类型
192
     */
xiongziliang committed
193
    Sdp(uint32_t sample_rate, uint8_t payload_type){
194
        _sample_rate = sample_rate;
xiongziliang committed
195
        _payload_type = payload_type;
196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }

    virtual ~Sdp(){}

    /**
     * 获取sdp字符串
     * @return
     */
    virtual string getSdp() const  = 0;

    /**
     * 获取pt
     * @return
     */
xiongziliang committed
210 211
    uint8_t getPayloadType() const{
        return _payload_type;
212 213 214 215 216 217 218 219 220
    }

    /**
     * 获取采样率
     * @return
     */
    uint32_t getSampleRate() const{
        return _sample_rate;
    }
xiongziliang committed
221
private:
xiongziliang committed
222
    uint8_t _payload_type;
223
    uint32_t _sample_rate;
xiongziliang committed
224 225 226 227 228 229 230 231
};

/**
* sdp中除音视频外的其他描述部分
*/
class TitleSdp : public Sdp{
public:

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    /**
     * 构造title类型sdp
     * @param dur_sec rtsp点播时长,0代表直播,单位秒
     * @param header 自定义sdp描述
     * @param version sdp版本
     */
    TitleSdp(float dur_sec = 0,
             const map<string,string> &header = map<string,string>(),
             int version = 0) : Sdp(0,0){
        _printer << "v=" << version << "\r\n";

        if(!header.empty()){
            for (auto &pr : header){
                _printer << pr.first << "=" << pr.second << "\r\n";
            }
        } else {
xiongziliang committed
248 249
            _printer << "o=- 0 0 IN IP4 0.0.0.0\r\n";
            _printer << "s=Streamed by " << SERVER_NAME << "\r\n";
250 251 252 253 254
            _printer << "c=IN IP4 0.0.0.0\r\n";
            _printer << "t=0 0\r\n";
        }

        if(dur_sec <= 0){
xiongziliang committed
255
            //直播
256
            _printer << "a=range:npt=now-\r\n";
257
        }else{
xiongziliang committed
258
            //点播
259
            _printer << "a=range:npt=0-" << dur_sec  << "\r\n";
260 261 262 263 264 265 266 267 268 269
        }
        _printer << "a=control:*\r\n";
    }
    string getSdp() const override {
        return _printer;
    }

    CodecId getCodecId() const override{
        return CodecInvalid;
    }
xiongziliang committed
270
private:
271
    _StrPrinter _printer;
xiongziliang committed
272 273
};

274
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip);
xiongziliang committed
275
string printSSRC(uint32_t ui32Ssrc);
276

xiongziliang committed
277
} //namespace mediakit
xzl committed
278
#endif //RTSP_RTSP_H_