2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* 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"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
using namespace std;
|
2017-05-05 18:04:23 +08:00
|
|
|
|
using namespace ZL::Util;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
TrackVideo = 0, TrackAudio
|
|
|
|
|
} TrackType;
|
|
|
|
|
|
|
|
|
|
class RtspTrack{
|
|
|
|
|
public:
|
|
|
|
|
uint8_t PT;
|
2018-03-20 11:28:13 +08:00
|
|
|
|
uint8_t interleaved;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
TrackType type = (TrackType) -1;
|
|
|
|
|
string trackSdp;
|
2018-03-26 18:56:22 +08:00
|
|
|
|
string controlSuffix;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
bool inited;
|
|
|
|
|
uint32_t ssrc = 0;
|
|
|
|
|
uint16_t seq;
|
|
|
|
|
uint32_t timeStamp;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RtpPacket {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<RtpPacket> Ptr;
|
|
|
|
|
uint8_t interleaved;
|
|
|
|
|
uint8_t PT;
|
|
|
|
|
bool mark;
|
|
|
|
|
uint32_t length;
|
|
|
|
|
uint32_t timeStamp;
|
|
|
|
|
uint16_t sequence;
|
|
|
|
|
uint32_t ssrc;
|
|
|
|
|
uint8_t payload[1560];
|
|
|
|
|
TrackType type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RtcpCounter {
|
|
|
|
|
public:
|
|
|
|
|
uint32_t pktCnt = 0;
|
|
|
|
|
uint32_t octCount = 0;
|
|
|
|
|
uint32_t timeStamp = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string FindField(const char* buf, const char* start, const char *end,int bufSize = 0 );
|
|
|
|
|
int parserSDP(const string& sdp, RtspTrack Track[2]);
|
|
|
|
|
|
2017-05-05 18:04:23 +08:00
|
|
|
|
|
|
|
|
|
struct StrCaseCompare
|
|
|
|
|
{
|
|
|
|
|
bool operator()(const string& __x, const string& __y) const
|
|
|
|
|
{return strcasecmp(__x.data(), __y.data()) < 0 ;}
|
|
|
|
|
};
|
|
|
|
|
typedef map<string,string,StrCaseCompare> StrCaseMap;
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
class Parser {
|
|
|
|
|
public:
|
2017-12-08 22:37:17 +08:00
|
|
|
|
Parser() {}
|
|
|
|
|
virtual ~Parser() {}
|
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) {
|
|
|
|
|
m_strMethod = FindField(line.c_str(), NULL, " ");
|
2018-02-05 15:56:44 +08:00
|
|
|
|
m_strFullUrl = FindField(line.c_str(), " ", " ");
|
|
|
|
|
auto args_pos = m_strFullUrl.find('?');
|
2017-12-08 22:37:17 +08:00
|
|
|
|
if(args_pos != string::npos){
|
2018-02-05 15:56:44 +08:00
|
|
|
|
m_strUrl = m_strFullUrl.substr(0,args_pos);
|
|
|
|
|
m_mapUrlArgs = parseArgs(m_strFullUrl.substr(args_pos + 1 ));
|
2017-12-08 22:37:17 +08:00
|
|
|
|
}else{
|
2018-02-05 15:56:44 +08:00
|
|
|
|
m_strUrl = m_strFullUrl;
|
2017-12-08 22:37:17 +08:00
|
|
|
|
}
|
2018-02-05 15:56:44 +08:00
|
|
|
|
m_strTail = FindField(line.c_str(), (m_strFullUrl + " ").c_str(), NULL);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} else {
|
2017-12-08 22:37:17 +08:00
|
|
|
|
auto field = FindField(line.c_str(), NULL, ": ");
|
|
|
|
|
auto value = FindField(line.c_str(), ": ", NULL);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (field.size() != 0) {
|
2018-02-06 10:56:58 +08:00
|
|
|
|
m_mapHeaders[field] = value;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
start = start + line.size() + 2;
|
|
|
|
|
if (strncmp(start, "\r\n", 2) == 0) { //协议解析完毕
|
|
|
|
|
m_strContent = FindField(start, "\r\n", NULL);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const string& Method() const {
|
|
|
|
|
//rtsp方法
|
|
|
|
|
return m_strMethod;
|
|
|
|
|
}
|
|
|
|
|
const string& Url() const {
|
|
|
|
|
//rtsp url
|
|
|
|
|
return m_strUrl;
|
|
|
|
|
}
|
2018-02-05 15:56:44 +08:00
|
|
|
|
const string& FullUrl() const {
|
|
|
|
|
//rtsp url with args
|
|
|
|
|
return m_strFullUrl;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
const string& Tail() const {
|
|
|
|
|
//RTSP/1.0
|
|
|
|
|
return m_strTail;
|
|
|
|
|
}
|
|
|
|
|
const string& operator[](const char *name) const {
|
|
|
|
|
//rtsp field
|
2018-02-06 10:56:58 +08:00
|
|
|
|
auto it = m_mapHeaders.find(name);
|
|
|
|
|
if (it == m_mapHeaders.end()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return m_strNull;
|
|
|
|
|
}
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
const string& Content() const {
|
|
|
|
|
return m_strContent;
|
|
|
|
|
}
|
|
|
|
|
void Clear() {
|
|
|
|
|
m_strMethod.clear();
|
|
|
|
|
m_strUrl.clear();
|
2018-02-05 15:56:44 +08:00
|
|
|
|
m_strFullUrl.clear();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
m_strTail.clear();
|
|
|
|
|
m_strContent.clear();
|
2018-02-06 10:56:58 +08:00
|
|
|
|
m_mapHeaders.clear();
|
2017-12-08 22:37:17 +08:00
|
|
|
|
m_mapUrlArgs.clear();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setUrl(const string& url) {
|
|
|
|
|
this->m_strUrl = url;
|
|
|
|
|
}
|
|
|
|
|
void setContent(const string& content) {
|
|
|
|
|
this->m_strContent = content;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:06:08 +08:00
|
|
|
|
StrCaseMap& getValues() const {
|
2018-02-06 10:56:58 +08:00
|
|
|
|
return m_mapHeaders;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-02-02 18:06:08 +08:00
|
|
|
|
StrCaseMap& getUrlArgs() const {
|
2017-12-08 22:37:17 +08:00
|
|
|
|
return m_mapUrlArgs;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2017-12-08 22:37:17 +08:00
|
|
|
|
|
2017-12-10 01:34:43 +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) {
|
2017-12-10 01:34:43 +08:00
|
|
|
|
auto key_val_vec = split(key_val, key_delim);
|
2017-12-08 22:37:17 +08:00
|
|
|
|
if (key_val_vec.size() >= 2) {
|
|
|
|
|
ret[key_val_vec[0]] = key_val_vec[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-12-10 01:34:43 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
|
|
|
|
string m_strMethod;
|
|
|
|
|
string m_strUrl;
|
|
|
|
|
string m_strTail;
|
|
|
|
|
string m_strContent;
|
|
|
|
|
string m_strNull;
|
2018-02-05 15:56:44 +08:00
|
|
|
|
string m_strFullUrl;
|
2018-02-06 10:56:58 +08:00
|
|
|
|
mutable StrCaseMap m_mapHeaders;
|
2018-02-02 18:06:08 +08:00
|
|
|
|
mutable StrCaseMap m_mapUrlArgs;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned forbidden_zero_bit :1;
|
|
|
|
|
unsigned nal_ref_idc :2;
|
|
|
|
|
unsigned type :5;
|
|
|
|
|
} NALU;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned S :1;
|
|
|
|
|
unsigned E :1;
|
|
|
|
|
unsigned R :1;
|
|
|
|
|
unsigned type :5;
|
|
|
|
|
} FU;
|
|
|
|
|
|
|
|
|
|
bool MakeNalu(char in, NALU &nal) ;
|
|
|
|
|
bool MakeFU(char in, FU &fu) ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif //RTSP_RTSP_H_
|