Rtsp.h 4.94 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"
35
#include "Player/Frame.h"
xiongzilaing committed
36

xzl committed
37
using namespace std;
xiongziliang committed
38 39
using namespace toolkit;
using namespace mediakit;
xzl committed
40 41 42 43 44


class RtspTrack{
public:
	uint8_t PT;
45
    uint8_t interleaved;
46
	TrackType type = TrackInvalid;
xzl committed
47
	string trackSdp;
48
    string controlSuffix;
xzl committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	bool inited;
	uint32_t ssrc = 0;
	uint16_t seq;
	uint32_t timeStamp;
};

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]);

65 66 67 68 69 70 71 72

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
73 74
class Parser {
public:
xiongziliang committed
75 76
	Parser() {}
	virtual ~Parser() {}
xzl committed
77 78 79 80 81
	void Parse(const char *buf) {
		//解析
		const char *start = buf;
		Clear();
		while (true) {
xiongziliang committed
82
			auto line = FindField(start, NULL, "\r\n");
xzl committed
83 84 85 86
			if (line.size() == 0) {
				break;
			}
			if (start == buf) {
87 88 89
				_strMethod = FindField(line.c_str(), NULL, " ");
                _strFullUrl = FindField(line.c_str(), " ", " ");
				auto args_pos =  _strFullUrl.find('?');
xiongziliang committed
90
				if(args_pos != string::npos){
91 92
					_strUrl = _strFullUrl.substr(0,args_pos);
					_mapUrlArgs = parseArgs(_strFullUrl.substr(args_pos + 1 ));
xiongziliang committed
93
				}else{
94
					_strUrl = _strFullUrl;
xiongziliang committed
95
				}
96
				_strTail = FindField(line.c_str(), (_strFullUrl + " ").c_str(), NULL);
xzl committed
97
			} else {
xiongziliang committed
98 99
				auto field = FindField(line.c_str(), NULL, ": ");
				auto value = FindField(line.c_str(), ": ", NULL);
xzl committed
100
				if (field.size() != 0) {
101
					_mapHeaders[field] = value;
xzl committed
102 103 104 105
				}
			}
			start = start + line.size() + 2;
			if (strncmp(start, "\r\n", 2) == 0) { //协议解析完毕
106
				_strContent = FindField(start, "\r\n", NULL);
xzl committed
107 108 109 110 111 112
				break;
			}
		}
	}
	const string& Method() const {
		//rtsp方法
113
		return _strMethod;
xzl committed
114 115 116
	}
	const string& Url() const {
		//rtsp url
117
		return _strUrl;
xzl committed
118
	}
119 120
    const string& FullUrl() const {
        //rtsp url with args
121
        return _strFullUrl;
122
    }
xzl committed
123 124
	const string& Tail() const {
		//RTSP/1.0
125
		return _strTail;
xzl committed
126 127 128
	}
	const string& operator[](const char *name) const {
		//rtsp field
129 130 131
		auto it = _mapHeaders.find(name);
		if (it == _mapHeaders.end()) {
			return _strNull;
xzl committed
132 133 134 135
		}
		return it->second;
	}
	const string& Content() const {
136
		return _strContent;
xzl committed
137 138
	}
	void Clear() {
139 140 141 142 143 144 145
		_strMethod.clear();
		_strUrl.clear();
        _strFullUrl.clear();
		_strTail.clear();
		_strContent.clear();
		_mapHeaders.clear();
		_mapUrlArgs.clear();
xzl committed
146 147 148
	}

	void setUrl(const string& url) {
149
		this->_strUrl = url;
xzl committed
150 151
	}
	void setContent(const string& content) {
152
		this->_strContent = content;
xzl committed
153 154
	}

155
	StrCaseMap& getValues() const {
156
		return _mapHeaders;
xzl committed
157
	}
158
	StrCaseMap& getUrlArgs() const {
159
		return _mapUrlArgs;
xiongziliang committed
160
	}
xzl committed
161

162
	static StrCaseMap parseArgs(const string &str,const char *pair_delim = "&", const char *key_delim = "="){
xiongziliang committed
163
		StrCaseMap ret;
164
		auto arg_vec = split(str, pair_delim);
xiongziliang committed
165
		for (string &key_val : arg_vec) {
xiongziliang committed
166 167 168
			auto key = FindField(key_val.data(),NULL,key_delim);
			auto val = FindField(key_val.data(),key_delim, NULL);
			ret[key] = val;
xiongziliang committed
169 170 171
		}
		return ret;
	}
172

xzl committed
173
private:
174 175 176 177 178 179 180 181
	string _strMethod;
	string _strUrl;
	string _strTail;
	string _strContent;
	string _strNull;
    string _strFullUrl;
	mutable StrCaseMap _mapHeaders;
	mutable StrCaseMap _mapUrlArgs;
xzl committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
};

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;

197 198
bool MakeNalu(uint8_t in, NALU &nal) ;
bool MakeFU(uint8_t in, FU &fu) ;
xzl committed
199 200 201


#endif //RTSP_RTSP_H_