Rtsp.h 5.26 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

typedef enum {
	TrackVideo = 0, TrackAudio
} TrackType;

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

81 82 83 84 85 86 87 88

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

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

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

178
	static StrCaseMap parseArgs(const string &str,const char *pair_delim = "&", const char *key_delim = "="){
xiongziliang committed
179
		StrCaseMap ret;
180
		auto arg_vec = split(str, pair_delim);
xiongziliang committed
181
		for (string &key_val : arg_vec) {
xiongziliang committed
182 183 184
			auto key = FindField(key_val.data(),NULL,key_delim);
			auto val = FindField(key_val.data(),key_delim, NULL);
			ret[key] = val;
xiongziliang committed
185 186 187
		}
		return ret;
	}
188

xzl committed
189 190 191 192 193 194
private:
	string m_strMethod;
	string m_strUrl;
	string m_strTail;
	string m_strContent;
	string m_strNull;
195
    string m_strFullUrl;
196
	mutable StrCaseMap m_mapHeaders;
197
	mutable StrCaseMap m_mapUrlArgs;
xzl committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
};

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;

213 214
bool MakeNalu(uint8_t in, NALU &nal) ;
bool MakeFU(uint8_t in, FU &fu) ;
xzl committed
215 216 217


#endif //RTSP_RTSP_H_