ZLMediaKit/src/Rtsp/Rtsp.h

391 lines
9.3 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2020-04-04 20:30:09 +08:00
* 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.
2017-09-27 16:20:30 +08:00
*/
2020-04-04 20:30:09 +08:00
2017-04-01 16:35:56 +08:00
#ifndef RTSP_RTSP_H_
#define RTSP_RTSP_H_
#include <string.h>
#include <string>
#include <memory>
2017-04-25 11:35:41 +08:00
#include <unordered_map>
2017-05-05 18:04:23 +08:00
#include "Util/util.h"
2021-01-31 19:19:24 +08:00
#include "Common/config.h"
#include "Common/macros.h"
2018-10-30 14:59:42 +08:00
#include "Extension/Frame.h"
2017-04-25 11:35:41 +08:00
2017-04-01 16:35:56 +08:00
using namespace std;
2018-10-24 17:17:55 +08:00
using namespace toolkit;
using namespace mediakit;
2017-04-01 16:35:56 +08:00
2019-03-27 18:41:52 +08:00
namespace mediakit {
namespace Rtsp {
typedef enum {
2020-03-20 11:51:24 +08:00
RTP_Invalid = -1,
RTP_TCP = 0,
RTP_UDP = 1,
RTP_MULTICAST = 2,
2019-03-27 18:41:52 +08:00
} eRtpType;
2020-03-26 17:12:21 +08:00
#define RTP_PT_MAP(XX) \
2020-04-18 23:00:48 +08:00
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) \
2020-03-26 17:12:21 +08:00
typedef enum {
2020-04-18 23:00:48 +08:00
#define ENUM_DEF(name, type, value, clock_rate, channel, codec_id) PT_ ## name = value,
2020-03-26 17:12:21 +08:00
RTP_PT_MAP(ENUM_DEF)
#undef ENUM_DEF
PT_MAX = 128
} PayloadType;
2019-03-27 18:41:52 +08:00
};
2021-01-31 19:19:24 +08:00
#if defined(_WIN32)
#pragma pack(push, 1)
#endif // defined(_WIN32)
class RtpHeader {
public:
#if __BYTE_ORDER == __BIG_ENDIAN
//版本号固定为2
uint32_t version: 2;
//padding
uint32_t padding: 1;
//扩展
uint32_t ext: 1;
//csrc
uint32_t csrc: 4;
//mark
uint32_t mark: 1;
//负载类型
uint32_t pt: 7;
#else
//csrc
uint32_t csrc: 4;
//扩展
uint32_t ext: 1;
//padding
uint32_t padding: 1;
//版本号固定为2
uint32_t version: 2;
//负载类型
uint32_t pt: 7;
//mark
uint32_t mark: 1;
#endif
//序列号
uint32_t seq: 16;
//时间戳
uint32_t stamp;
//ssrc
uint32_t ssrc;
//负载如果有csrc和ext前面为 4 * csrc + (4 + 4 * ext_len)
uint8_t payload;
public:
//返回csrc字段字节长度
size_t getCsrcSize() const;
//返回csrc字段首地址不存在时返回nullptr
uint8_t *getCsrcData();
//返回ext字段字节长度
size_t getExtSize() const;
2021-05-06 12:02:16 +08:00
//返回ext reserved值
uint16_t getExtReserved() const;
2021-01-31 19:19:24 +08:00
//返回ext段首地址不存在时返回nullptr
uint8_t *getExtData();
//返回有效负载指针,跳过csrc、ext
uint8_t* getPayloadData();
//返回有效负载总长度,不包括csrc、ext、padding
2021-02-02 11:54:49 +08:00
size_t getPayloadSize(size_t rtp_size) const;
//打印调试信息
string dumpString(size_t rtp_size) const;
2021-01-31 19:19:24 +08:00
private:
//返回有效负载偏移量
size_t getPayloadOffset() const;
//返回padding长度
size_t getPaddingSize(size_t rtp_size) const;
} PACKED;
#if defined(_WIN32)
#pragma pack(pop)
#endif // defined(_WIN32)
//此rtp为rtp over tcp形式需要忽略前4个字节
2019-06-28 16:12:39 +08:00
class RtpPacket : public BufferRaw{
public:
2021-01-31 19:19:24 +08:00
using Ptr = std::shared_ptr<RtpPacket>;
enum {
kRtpVersion = 2,
kRtpHeaderSize = 12,
kRtpTcpHeaderSize = 4
};
2021-04-11 01:15:02 +08:00
//获取rtp头
2021-01-31 19:19:24 +08:00
RtpHeader* getHeader();
const RtpHeader* getHeader() const;
2021-04-11 01:15:02 +08:00
//打印调试信息
string dumpString() const;
2021-01-31 19:19:24 +08:00
//主机字节序的seq
uint16_t getSeq() const;
uint32_t getStamp() const;
2021-01-31 19:19:24 +08:00
//主机字节序的时间戳,已经转换为毫秒
uint32_t getStampMS() const;
2021-01-31 19:19:24 +08:00
//主机字节序的ssrc
uint32_t getSSRC() const;
2021-01-31 19:19:24 +08:00
//有效负载跳过csrc、ext
uint8_t* getPayload();
//有效负载长度不包括csrc、ext、padding
size_t getPayloadSize() const;
2021-01-31 19:19:24 +08:00
//音视频类型
TrackType type;
2021-01-31 19:19:24 +08:00
//音频为采样率视频一般为90000
uint32_t sample_rate;
//ntp时间戳
uint64_t ntp_stamp;
2021-02-05 11:28:50 +08:00
static Ptr create();
private:
friend class ResourcePool_l<RtpPacket>;
RtpPacket() = default;
2021-02-05 16:49:11 +08:00
private:
//对象个数统计
ObjectStatistic<RtpPacket> _statistic;
2019-06-28 16:12:39 +08:00
};
2021-01-31 21:09:25 +08:00
class RtpPayload {
2020-03-26 17:12:21 +08:00
public:
static int getClockRate(int pt);
static TrackType getTrackType(int pt);
static int getAudioChannel(int pt);
static const char *getName(int pt);
2020-04-18 23:00:48 +08:00
static CodecId getCodecId(int pt);
2021-01-31 21:09:25 +08:00
2020-03-26 17:12:21 +08:00
private:
RtpPayload() = delete;
~RtpPayload() = delete;
};
2019-03-27 18:41:52 +08:00
class SdpTrack {
2018-10-25 22:57:59 +08:00
public:
2021-01-31 21:09:25 +08:00
using Ptr = std::shared_ptr<SdpTrack>;
2020-03-20 11:51:24 +08:00
string _t;
string _b;
2020-03-26 17:12:21 +08:00
uint16_t _port;
2018-10-25 22:57:59 +08:00
2020-03-20 11:51:24 +08:00
float _duration = 0;
float _start = 0;
float _end = 0;
2018-10-25 22:57:59 +08:00
2020-03-20 11:51:24 +08:00
map<char, string> _other;
2021-08-23 21:15:19 +08:00
multimap<string, string> _attr;
2019-08-15 19:13:31 +08:00
string toString(uint16_t port = 0) const;
2020-03-26 17:12:21 +08:00
string getName() const;
2021-02-21 21:27:26 +08:00
string getControlUrl(const string &base_url) const;
2021-01-31 21:09:25 +08:00
2018-10-25 22:57:59 +08:00
public:
2020-03-20 11:51:24 +08:00
int _pt;
2020-04-18 22:13:11 +08:00
int _channel;
2021-01-31 21:09:25 +08:00
int _samplerate;
TrackType _type;
string _codec;
2020-03-20 11:51:24 +08:00
string _fmtp;
string _control;
2021-01-31 21:09:25 +08:00
2018-10-26 09:56:29 +08:00
public:
2020-03-20 11:51:24 +08:00
bool _inited = false;
2021-01-31 21:09:25 +08:00
uint8_t _interleaved = 0;
2020-03-20 11:51:24 +08:00
uint16_t _seq = 0;
2021-01-31 21:09:25 +08:00
uint32_t _ssrc = 0;
2020-03-20 11:51:24 +08:00
//时间戳,单位毫秒
uint32_t _time_stamp = 0;
2018-10-25 22:57:59 +08:00
};
2019-03-27 18:41:52 +08:00
2019-06-28 16:48:02 +08:00
class SdpParser {
2018-10-25 22:57:59 +08:00
public:
2021-01-31 21:09:25 +08:00
using Ptr = std::shared_ptr<SdpParser>;
2020-03-20 11:51:24 +08:00
SdpParser() {}
SdpParser(const string &sdp) { load(sdp); }
~SdpParser() {}
2021-01-31 21:09:25 +08:00
2020-03-20 11:51:24 +08:00
void load(const string &sdp);
bool available() const;
SdpTrack::Ptr getTrack(TrackType type) const;
vector<SdpTrack::Ptr> getAvailableTrack() const;
2021-01-31 21:09:25 +08:00
string toString() const;
2018-10-25 22:57:59 +08:00
private:
2020-03-20 11:51:24 +08:00
vector<SdpTrack::Ptr> _track_vec;
2018-10-25 22:57:59 +08:00
};
2017-04-01 16:35:56 +08:00
/**
* rtsp url的工具类
*/
class RtspUrl{
public:
2021-01-31 21:09:25 +08:00
bool _is_ssl;
uint16_t _port;
2020-03-20 11:51:24 +08:00
string _url;
string _user;
string _passwd;
string _host;
2021-01-31 21:09:25 +08:00
public:
2020-03-20 11:51:24 +08:00
RtspUrl() = default;
~RtspUrl() = default;
bool parse(const string &url);
2021-01-31 21:09:25 +08:00
private:
2020-03-20 11:51:24 +08:00
bool setup(bool,const string &, const string &, const string &);
};
2017-04-01 16:35:56 +08:00
2019-06-28 16:12:39 +08:00
/**
* rtsp sdp基类
*/
class Sdp : public CodecInfo{
public:
2021-01-31 21:09:25 +08:00
using Ptr = std::shared_ptr<Sdp>;
2020-03-20 11:51:24 +08:00
/**
* sdp
* @param sample_rate
2020-05-25 13:51:00 +08:00
* @param payload_type pt类型
2020-03-20 11:51:24 +08:00
*/
2020-05-25 13:51:00 +08:00
Sdp(uint32_t sample_rate, uint8_t payload_type){
2020-03-20 11:51:24 +08:00
_sample_rate = sample_rate;
2020-05-25 13:51:00 +08:00
_payload_type = payload_type;
2020-03-20 11:51:24 +08:00
}
virtual ~Sdp(){}
/**
* sdp字符串
* @return
*/
virtual string getSdp() const = 0;
/**
* pt
* @return
*/
2020-05-25 13:51:00 +08:00
uint8_t getPayloadType() const{
return _payload_type;
2020-03-20 11:51:24 +08:00
}
/**
*
* @return
*/
uint32_t getSampleRate() const{
return _sample_rate;
}
2021-01-31 21:09:25 +08:00
2019-06-28 16:12:39 +08:00
private:
2020-05-25 13:51:00 +08:00
uint8_t _payload_type;
2020-03-20 11:51:24 +08:00
uint32_t _sample_rate;
2019-06-28 16:12:39 +08:00
};
/**
* sdp中除音视频外的其他描述部分
*/
class TitleSdp : public Sdp{
public:
2021-09-29 00:04:36 +08:00
using Ptr = std::shared_ptr<TitleSdp>;
2020-03-20 11:51:24 +08:00
/**
* title类型sdp
* @param dur_sec rtsp点播时长0
* @param header sdp描述
* @param version sdp版本
*/
TitleSdp(float dur_sec = 0,
2021-09-29 00:04:36 +08:00
const map<string, string> &header = map<string, string>(),
int version = 0) : Sdp(0, 0) {
2020-03-20 11:51:24 +08:00
_printer << "v=" << version << "\r\n";
2021-09-29 00:04:36 +08:00
if (!header.empty()) {
for (auto &pr : header) {
2020-03-20 11:51:24 +08:00
_printer << pr.first << "=" << pr.second << "\r\n";
}
} else {
2020-04-04 19:55:11 +08:00
_printer << "o=- 0 0 IN IP4 0.0.0.0\r\n";
2021-10-16 16:58:03 +08:00
_printer << "s=Streamed by " << kServerName << "\r\n";
2020-03-20 11:51:24 +08:00
_printer << "c=IN IP4 0.0.0.0\r\n";
_printer << "t=0 0\r\n";
}
2021-09-29 00:04:36 +08:00
if (dur_sec <= 0) {
2020-04-04 19:55:11 +08:00
//直播
2020-04-03 14:09:03 +08:00
_printer << "a=range:npt=now-\r\n";
2021-09-29 00:04:36 +08:00
} else {
2020-04-04 19:55:11 +08:00
//点播
2021-09-29 00:04:36 +08:00
_dur_sec = dur_sec;
_printer << "a=range:npt=0-" << dur_sec << "\r\n";
2020-03-20 11:51:24 +08:00
}
_printer << "a=control:*\r\n";
}
2021-09-29 00:04:36 +08:00
2020-03-20 11:51:24 +08:00
string getSdp() const override {
return _printer;
}
2021-09-29 00:04:36 +08:00
CodecId getCodecId() const override {
2020-03-20 11:51:24 +08:00
return CodecInvalid;
}
2021-09-29 00:04:36 +08:00
float getDuration() const {
return _dur_sec;
}
2019-06-28 16:12:39 +08:00
private:
2021-09-29 00:04:36 +08:00
float _dur_sec = 0;
2020-03-20 11:51:24 +08:00
_StrPrinter _printer;
2019-06-28 16:12:39 +08:00
};
2021-01-31 21:09:25 +08:00
//创建rtp over tcp4个字节的头
Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved);
//创建rtp-rtcp端口对
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip);
2021-01-31 21:09:25 +08:00
//十六进制方式打印ssrc
2020-07-07 09:58:08 +08:00
string printSSRC(uint32_t ui32Ssrc);
2020-05-12 10:22:21 +08:00
2019-03-27 18:41:52 +08:00
} //namespace mediakit
2017-04-01 16:35:56 +08:00
#endif //RTSP_RTSP_H_