ZLMediaKit/src/Rtsp/Rtsp.h

301 lines
7.2 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* 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.
*/
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-02 17:15:12 +08:00
#include "Common/config.h"
2017-05-05 18:04:23 +08:00
#include "Util/util.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) \
XX(PCMU, TrackAudio, 0, 8000, 1) \
XX(GSM, TrackAudio , 3, 8000, 1) \
XX(G723, TrackAudio, 4, 8000, 1) \
XX(DVI4_8000, TrackAudio, 5, 8000, 1) \
XX(DVI4_16000, TrackAudio, 6, 16000, 1) \
XX(LPC, TrackAudio, 7, 8000, 1) \
XX(PCMA, TrackAudio, 8, 8000, 1) \
XX(G722, TrackAudio, 9, 8000, 1) \
XX(L16_Stereo, TrackAudio, 10, 44100, 2) \
XX(L16_Mono, TrackAudio, 11, 44100, 1) \
XX(QCELP, TrackAudio, 12, 8000, 1) \
XX(CN, TrackAudio, 13, 8000, 1) \
XX(MPA, TrackAudio, 14, 90000, 1) \
XX(G728, TrackAudio, 15, 8000, 1) \
XX(DVI4_11025, TrackAudio, 16, 11025, 1) \
XX(DVI4_22050, TrackAudio, 17, 22050, 1) \
XX(G729, TrackAudio, 18, 8000, 1) \
XX(CelB, TrackVideo, 25, 90000, 1) \
XX(JPEG, TrackVideo, 26, 90000, 1) \
XX(nv, TrackVideo, 28, 90000, 1) \
XX(H261, TrackVideo, 31, 90000, 1) \
XX(MPV, TrackVideo, 32, 90000, 1) \
XX(MP2T, TrackVideo, 33, 90000, 1) \
XX(H263, TrackVideo, 34, 90000, 1) \
typedef enum {
#define ENUM_DEF(name, type, value, clock_rate, channel) PT_ ## name = value,
RTP_PT_MAP(ENUM_DEF)
#undef ENUM_DEF
PT_MAX = 128
} PayloadType;
2019-03-27 18:41:52 +08:00
};
2019-06-28 16:12:39 +08:00
class RtpPacket : public BufferRaw{
public:
2020-03-20 11:51:24 +08:00
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;
2019-06-28 16:12:39 +08:00
};
2020-03-26 17:12:21 +08:00
class RtpPayload{
public:
static int getClockRate(int pt);
static TrackType getTrackType(int pt);
static int getAudioChannel(int pt);
static const char *getName(int pt);
private:
RtpPayload() = delete;
~RtpPayload() = delete;
};
2019-06-28 16:48:02 +08:00
class RtcpCounter {
public:
2020-03-20 11:51:24 +08:00
uint32_t pktCnt = 0;
uint32_t octCount = 0;
//网络字节序
uint32_t timeStamp = 0;
uint32_t lastTimeStamp = 0;
2019-06-28 16:48:02 +08:00
};
2019-03-27 18:41:52 +08:00
class SdpTrack {
2018-10-25 22:57:59 +08:00
public:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<SdpTrack> Ptr;
2018-10-25 22:57:59 +08:00
2020-03-20 11:51:24 +08:00
string _m;
string _o;
string _s;
string _i;
string _c;
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;
map<string, string> _attr;
2019-08-15 19:13:31 +08:00
2020-03-20 11:51:24 +08:00
string toString() const;
2020-03-26 17:12:21 +08:00
string getName() const;
2018-10-25 22:57:59 +08:00
public:
2020-03-20 11:51:24 +08:00
int _pt;
string _codec;
int _samplerate;
string _fmtp;
string _control;
string _control_surffix;
TrackType _type;
2018-10-26 09:56:29 +08:00
public:
2020-03-20 11:51:24 +08:00
uint8_t _interleaved = 0;
bool _inited = false;
uint32_t _ssrc = 0;
uint16_t _seq = 0;
//时间戳,单位毫秒
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:
2020-03-20 11:51:24 +08:00
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 ;
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:
2020-03-20 11:51:24 +08:00
string _url;
string _user;
string _passwd;
string _host;
uint16_t _port;
bool _is_ssl;
public:
2020-03-20 11:51:24 +08:00
RtspUrl() = default;
~RtspUrl() = default;
bool parse(const string &url);
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:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<Sdp> Ptr;
/**
* sdp
* @param sample_rate
* @param playload_type pt类型
*/
Sdp(uint32_t sample_rate, uint8_t playload_type){
_sample_rate = sample_rate;
_playload_type = playload_type;
}
virtual ~Sdp(){}
/**
* sdp字符串
* @return
*/
virtual string getSdp() const = 0;
/**
* pt
* @return
*/
uint8_t getPlayloadType() const{
return _playload_type;
}
/**
*
* @return
*/
uint32_t getSampleRate() const{
return _sample_rate;
}
2019-06-28 16:12:39 +08:00
private:
2020-03-20 11:51:24 +08:00
uint8_t _playload_type;
uint32_t _sample_rate;
2019-06-28 16:12:39 +08:00
};
/**
* sdp中除音视频外的其他描述部分
*/
class TitleSdp : public Sdp{
public:
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 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 {
2020-04-04 19:55:11 +08:00
_printer << "o=- 0 0 IN IP4 0.0.0.0\r\n";
_printer << "s=Streamed by " << SERVER_NAME << "\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";
}
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";
2020-03-20 11:51:24 +08:00
}else{
2020-04-04 19:55:11 +08:00
//点播
2020-04-03 22:17:40 +08:00
_printer << "a=range:npt=0-" << dur_sec << "\r\n";
2020-03-20 11:51:24 +08:00
}
_printer << "a=control:*\r\n";
}
string getSdp() const override {
return _printer;
}
/**
*
* @return
*/
TrackType getTrackType() const override {
return TrackTitle;
}
/**
* id
* @return
*/
CodecId getCodecId() const override{
return CodecInvalid;
}
2019-06-28 16:12:39 +08:00
private:
2020-03-20 11:51:24 +08:00
_StrPrinter _printer;
2019-06-28 16:12:39 +08:00
};
2019-03-27 18:41:52 +08:00
} //namespace mediakit
2017-04-01 16:35:56 +08:00
#endif //RTSP_RTSP_H_