ZLMediaKit/src/Rtsp/Rtsp.h

342 lines
8.4 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>
2021-01-31 19:19:24 +08:00
#include "Common/macros.h"
2018-10-30 14:59:42 +08:00
#include "Extension/Frame.h"
2022-12-02 14:43:06 +08:00
#include "Network/Socket.h"
2017-04-25 11:35:41 +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, CodecJPEG) \
2020-04-18 23:00:48 +08:00
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
ssize_t getPayloadSize(size_t rtp_size) const;
2021-02-02 11:54:49 +08:00
//打印调试信息
std::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个字节
class RtpPacket : public toolkit::BufferRaw{
2019-06-28 16:12:39 +08:00
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
//打印调试信息
std::string dumpString() const;
2021-04-11 01:15:02 +08:00
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
//主机字节序的时间戳,已经转换为毫秒
2022-08-08 17:13:39 +08:00
uint64_t getStampMS(bool ntp = true) 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 toolkit::ResourcePool_l<RtpPacket>;
2021-02-05 11:28:50 +08:00
RtpPacket() = default;
2021-02-05 16:49:11 +08:00
private:
//对象个数统计
toolkit::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>;
std::string _t;
std::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
std::map<char, std::string> _other;
std::multimap<std::string, std::string> _attr;
2019-08-15 19:13:31 +08:00
std::string toString(uint16_t port = 0) const;
std::string getName() const;
std::string getControlUrl(const std::string &base_url) const;
2021-01-31 21:09:25 +08:00
2018-10-25 22:57:59 +08:00
public:
int _pt = 0xff;
2020-04-18 22:13:11 +08:00
int _channel;
2021-01-31 21:09:25 +08:00
int _samplerate;
TrackType _type;
std::string _codec;
std::string _fmtp;
std::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 std::string &sdp) { load(sdp); }
2020-03-20 11:51:24 +08:00
~SdpParser() {}
2021-01-31 21:09:25 +08:00
void load(const std::string &sdp);
2020-03-20 11:51:24 +08:00
bool available() const;
SdpTrack::Ptr getTrack(TrackType type) const;
std::vector<SdpTrack::Ptr> getAvailableTrack() const;
std::string toString() const;
2021-01-31 21:09:25 +08:00
2018-10-25 22:57:59 +08:00
private:
std::vector<SdpTrack::Ptr> _track_vec;
2018-10-25 22:57:59 +08:00
};
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 std::string getSdp() const = 0;
2020-03-20 11:51:24 +08:00
/**
* 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,
const std::map<std::string, std::string> &header = std::map<std::string, std::string>(),
int version = 0);
2021-09-29 00:04:36 +08:00
std::string getSdp() const override {
2020-03-20 11:51:24 +08:00
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;
toolkit::_StrPrinter _printer;
2019-06-28 16:12:39 +08:00
};
2021-01-31 21:09:25 +08:00
//创建rtp over tcp4个字节的头
toolkit::Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved);
2021-01-31 21:09:25 +08:00
//创建rtp-rtcp端口对
2022-12-02 14:43:06 +08:00
void makeSockPair(std::pair<toolkit::Socket::Ptr, toolkit::Socket::Ptr> &pair, const std::string &local_ip, bool re_use_port = false, bool is_udp = true);
2021-01-31 21:09:25 +08:00
//十六进制方式打印ssrc
std::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_