Rtsp.h 5.28 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */
xzl committed
26 27 28 29 30 31
#ifndef RTSP_RTSP_H_
#define RTSP_RTSP_H_

#include <string.h>
#include <string>
#include <memory>
xiongzilaing committed
32
#include <unordered_map>
xiongziliang committed
33
#include "Common/config.h"
34
#include "Util/util.h"
xiongzilaing committed
35

xzl committed
36
using namespace std;
37
using namespace ZL::Util;
xzl committed
38 39 40 41 42 43 44 45 46

typedef enum {
	TrackVideo = 0, TrackAudio
} TrackType;

class RtspTrack{
public:
	uint8_t PT;
	uint8_t trackId;
47 48
    string trackIdStr;
    uint8_t interleaved;
xzl committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	TrackType type = (TrackType) -1;
	string trackSdp;
	string trackStyle;
	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]);

82 83 84 85 86 87 88 89

struct StrCaseCompare
{
    bool operator()(const string& __x, const string& __y) const
    {return strcasecmp(__x.data(), __y.data()) < 0 ;}
};
typedef map<string,string,StrCaseCompare> StrCaseMap;

xzl committed
90 91
class Parser {
public:
xiongziliang committed
92 93
	Parser() {}
	virtual ~Parser() {}
xzl committed
94 95 96 97 98
	void Parse(const char *buf) {
		//解析
		const char *start = buf;
		Clear();
		while (true) {
xiongziliang committed
99
			auto line = FindField(start, NULL, "\r\n");
xzl committed
100 101 102 103 104
			if (line.size() == 0) {
				break;
			}
			if (start == buf) {
				m_strMethod = FindField(line.c_str(), NULL, " ");
105 106
                m_strFullUrl = FindField(line.c_str(), " ", " ");
				auto args_pos =  m_strFullUrl.find('?');
xiongziliang committed
107
				if(args_pos != string::npos){
108 109
					m_strUrl = m_strFullUrl.substr(0,args_pos);
					m_mapUrlArgs = parseArgs(m_strFullUrl.substr(args_pos + 1 ));
xiongziliang committed
110
				}else{
111
					m_strUrl = m_strFullUrl;
xiongziliang committed
112
				}
113
				m_strTail = FindField(line.c_str(), (m_strFullUrl + " ").c_str(), NULL);
xzl committed
114
			} else {
xiongziliang committed
115 116
				auto field = FindField(line.c_str(), NULL, ": ");
				auto value = FindField(line.c_str(), ": ", NULL);
xzl committed
117
				if (field.size() != 0) {
118
					m_mapHeaders[field] = value;
xzl committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
				}
			}
			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;
	}
136 137 138 139
    const string& FullUrl() const {
        //rtsp url with args
        return m_strFullUrl;
    }
xzl committed
140 141 142 143 144 145
	const string& Tail() const {
		//RTSP/1.0
		return m_strTail;
	}
	const string& operator[](const char *name) const {
		//rtsp field
146 147
		auto it = m_mapHeaders.find(name);
		if (it == m_mapHeaders.end()) {
xzl committed
148 149 150 151 152 153 154 155 156 157
			return m_strNull;
		}
		return it->second;
	}
	const string& Content() const {
		return m_strContent;
	}
	void Clear() {
		m_strMethod.clear();
		m_strUrl.clear();
158
        m_strFullUrl.clear();
xzl committed
159 160
		m_strTail.clear();
		m_strContent.clear();
161
		m_mapHeaders.clear();
xiongziliang committed
162
		m_mapUrlArgs.clear();
xzl committed
163 164 165 166 167 168 169 170 171
	}

	void setUrl(const string& url) {
		this->m_strUrl = url;
	}
	void setContent(const string& content) {
		this->m_strContent = content;
	}

172
	StrCaseMap& getValues() const {
173
		return m_mapHeaders;
xzl committed
174
	}
175
	StrCaseMap& getUrlArgs() const {
xiongziliang committed
176 177
		return m_mapUrlArgs;
	}
xzl committed
178

xiongziliang committed
179

180
	static StrCaseMap parseArgs(const string &str,const char *pair_delim = "&", const char *key_delim = "="){
xiongziliang committed
181
		StrCaseMap ret;
182
		auto arg_vec = split(str, pair_delim);
xiongziliang committed
183
		for (string &key_val : arg_vec) {
184
			auto key_val_vec = split(key_val, key_delim);
xiongziliang committed
185 186 187 188 189 190
			if (key_val_vec.size() >= 2) {
				ret[key_val_vec[0]] = key_val_vec[1];
			}
		}
		return ret;
	}
191

xzl committed
192 193 194 195 196 197
private:
	string m_strMethod;
	string m_strUrl;
	string m_strTail;
	string m_strContent;
	string m_strNull;
198
    string m_strFullUrl;
199
	mutable StrCaseMap m_mapHeaders;
200
	mutable StrCaseMap m_mapUrlArgs;
xzl committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
};

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_