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 {
|
|
|
|
|
RTP_Invalid = -1,
|
|
|
|
|
RTP_TCP = 0,
|
|
|
|
|
RTP_UDP = 1,
|
|
|
|
|
RTP_MULTICAST = 2,
|
|
|
|
|
} eRtpType;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SdpTrack {
|
2018-10-25 22:57:59 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<SdpTrack> Ptr;
|
|
|
|
|
|
|
|
|
|
string _m;
|
|
|
|
|
string _o;
|
|
|
|
|
string _s;
|
|
|
|
|
string _i;
|
|
|
|
|
string _c;
|
|
|
|
|
string _t;
|
|
|
|
|
string _b;
|
|
|
|
|
|
|
|
|
|
float _duration = 0;
|
|
|
|
|
float _start = 0;
|
|
|
|
|
float _end = 0;
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
map<char, string> _other;
|
|
|
|
|
map<string, string> _attr;
|
2018-10-25 22:57:59 +08:00
|
|
|
|
public:
|
|
|
|
|
int _pt;
|
|
|
|
|
string _codec;
|
|
|
|
|
int _samplerate;
|
|
|
|
|
string _fmtp;
|
|
|
|
|
string _control;
|
2018-10-26 09:56:29 +08:00
|
|
|
|
string _control_surffix;
|
2018-10-26 10:59:13 +08:00
|
|
|
|
TrackType _type;
|
2018-10-26 09:56:29 +08:00
|
|
|
|
public:
|
2019-03-27 18:41:52 +08:00
|
|
|
|
uint8_t _interleaved = 0;
|
|
|
|
|
bool _inited = false;
|
|
|
|
|
uint32_t _ssrc = 0;
|
|
|
|
|
uint16_t _seq = 0;
|
2018-10-26 14:12:16 +08:00
|
|
|
|
//时间戳,单位毫秒
|
2019-03-27 18:41:52 +08:00
|
|
|
|
uint32_t _time_stamp = 0;
|
2018-10-25 22:57:59 +08:00
|
|
|
|
};
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
class SdpAttr {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<SdpAttr> Ptr;
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
SdpAttr() {}
|
|
|
|
|
|
|
|
|
|
SdpAttr(const string &sdp) { load(sdp); }
|
|
|
|
|
|
|
|
|
|
~SdpAttr() {}
|
|
|
|
|
|
|
|
|
|
void load(const string &sdp);
|
|
|
|
|
|
|
|
|
|
bool available() const;
|
|
|
|
|
|
|
|
|
|
SdpTrack::Ptr getTrack(TrackType type) const;
|
|
|
|
|
|
|
|
|
|
vector<SdpTrack::Ptr> getAvailableTrack() const;
|
2018-10-26 09:56:29 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
private:
|
2019-03-27 18:41:52 +08:00
|
|
|
|
map<string, SdpTrack::Ptr> _track_map;
|
2018-10-25 22:57:59 +08:00
|
|
|
|
};
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RtcpCounter {
|
|
|
|
|
public:
|
2019-03-27 18:41:52 +08:00
|
|
|
|
uint32_t pktCnt = 0;
|
|
|
|
|
uint32_t octCount = 0;
|
2019-05-08 17:49:05 +08:00
|
|
|
|
//网络字节序
|
2019-03-27 18:41:52 +08:00
|
|
|
|
uint32_t timeStamp = 0;
|
2019-05-09 13:35:54 +08:00
|
|
|
|
uint32_t lastTimeStamp = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
string FindField(const char *buf, const char *start, const char *end, int bufSize = 0);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
struct StrCaseCompare {
|
|
|
|
|
bool operator()(const string &__x, const string &__y) const { return strcasecmp(__x.data(), __y.data()) < 0; }
|
2017-05-05 18:04:23 +08:00
|
|
|
|
};
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
typedef map<string, string, StrCaseCompare> StrCaseMap;
|
2017-05-05 18:04:23 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
class Parser {
|
|
|
|
|
public:
|
2017-12-08 22:37:17 +08:00
|
|
|
|
Parser() {}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2017-12-08 22:37:17 +08:00
|
|
|
|
virtual ~Parser() {}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void Parse(const char *buf) {
|
|
|
|
|
//解析
|
|
|
|
|
const char *start = buf;
|
|
|
|
|
Clear();
|
|
|
|
|
while (true) {
|
2017-12-08 22:37:17 +08:00
|
|
|
|
auto line = FindField(start, NULL, "\r\n");
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (line.size() == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (start == buf) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_strMethod = FindField(line.data(), NULL, " ");
|
|
|
|
|
_strFullUrl = FindField(line.data(), " ", " ");
|
|
|
|
|
auto args_pos = _strFullUrl.find('?');
|
|
|
|
|
if (args_pos != string::npos) {
|
|
|
|
|
_strUrl = _strFullUrl.substr(0, args_pos);
|
|
|
|
|
_mapUrlArgs = parseArgs(_strFullUrl.substr(args_pos + 1));
|
|
|
|
|
} else {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strUrl = _strFullUrl;
|
2017-12-08 22:37:17 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_strTail = FindField(line.data(), (_strFullUrl + " ").data(), NULL);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} else {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto field = FindField(line.data(), NULL, ": ");
|
|
|
|
|
auto value = FindField(line.data(), ": ", NULL);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (field.size() != 0) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_mapHeaders[field] = value;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
start = start + line.size() + 2;
|
|
|
|
|
if (strncmp(start, "\r\n", 2) == 0) { //协议解析完毕
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strContent = FindField(start, "\r\n", NULL);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
const string &Method() const {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//rtsp方法
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strMethod;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
const string &Url() const {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//rtsp url
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strUrl;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
const string &FullUrl() const {
|
|
|
|
|
//rtsp url with args
|
|
|
|
|
return _strFullUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &Tail() const {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//RTSP/1.0
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strTail;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
const string &operator[](const char *name) const {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//rtsp field
|
2018-10-24 15:43:52 +08:00
|
|
|
|
auto it = _mapHeaders.find(name);
|
|
|
|
|
if (it == _mapHeaders.end()) {
|
|
|
|
|
return _strNull;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
const string &Content() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strContent;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void Clear() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strMethod.clear();
|
|
|
|
|
_strUrl.clear();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_strFullUrl.clear();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strTail.clear();
|
|
|
|
|
_strContent.clear();
|
|
|
|
|
_mapHeaders.clear();
|
|
|
|
|
_mapUrlArgs.clear();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
void setUrl(const string &url) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
this->_strUrl = url;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
void setContent(const string &content) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
this->_strContent = content;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
StrCaseMap &getValues() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _mapHeaders;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
StrCaseMap &getUrlArgs() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _mapUrlArgs;
|
2017-12-08 22:37:17 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
static StrCaseMap parseArgs(const string &str, const char *pair_delim = "&", const char *key_delim = "=") {
|
2017-12-08 22:37:17 +08:00
|
|
|
|
StrCaseMap ret;
|
2017-12-10 01:34:43 +08:00
|
|
|
|
auto arg_vec = split(str, pair_delim);
|
2017-12-08 22:37:17 +08:00
|
|
|
|
for (string &key_val : arg_vec) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto key = FindField(key_val.data(), NULL, key_delim);
|
|
|
|
|
auto val = FindField(key_val.data(), key_delim, NULL);
|
2018-08-04 23:59:03 +08:00
|
|
|
|
ret[key] = val;
|
2017-12-08 22:37:17 +08:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-12-10 01:34:43 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _strMethod;
|
|
|
|
|
string _strUrl;
|
|
|
|
|
string _strTail;
|
|
|
|
|
string _strContent;
|
|
|
|
|
string _strNull;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
string _strFullUrl;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
mutable StrCaseMap _mapHeaders;
|
|
|
|
|
mutable StrCaseMap _mapUrlArgs;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
} //namespace mediakit
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif //RTSP_RTSP_H_
|