Rtsp.h 3.2 KB
Newer Older
xzl committed
1 2 3 4 5 6
#ifndef RTSP_RTSP_H_
#define RTSP_RTSP_H_

#include <string.h>
#include <string>
#include <memory>
xiongzilaing committed
7
#include <unordered_map>
xiongziliang committed
8
#include "Common/config.h"
9
#include "Util/util.h"
xiongzilaing committed
10

xzl committed
11
using namespace std;
12
using namespace ZL::Util;
xzl committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

typedef enum {
	TrackVideo = 0, TrackAudio
} TrackType;

class RtspTrack{
public:
	uint8_t PT;
	uint8_t trackId;
	uint8_t interleaved;
	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]);

56 57 58 59 60 61 62 63

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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
class Parser {
public:
	Parser() {
	}
	virtual ~Parser() {
	}
	void Parse(const char *buf) {
		//解析
		const char *start = buf;
		string line;
		string field;
		string value;
		Clear();
		while (true) {
			line = FindField(start, NULL, "\r\n");
			if (line.size() == 0) {
				break;
			}
			if (start == buf) {
				m_strMethod = FindField(line.c_str(), NULL, " ");
				m_strUrl = FindField(line.c_str(), " ", " ");
				m_strTail = FindField(line.c_str(), (m_strUrl + " ").c_str(), NULL);
			} else {
				field = FindField(line.c_str(), NULL, ": ");
				value = FindField(line.c_str(), ": ", NULL);
				if (field.size() != 0) {
					m_mapValues[field] = value;
				}
			}
			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;
	}
	const string& Tail() const {
		//RTSP/1.0
		return m_strTail;
	}
	const string& operator[](const char *name) const {
		//rtsp field
		auto it = m_mapValues.find(name);
		if (it == m_mapValues.end()) {
			return m_strNull;
		}
		return it->second;
	}
	const string& Content() const {
		return m_strContent;
	}
	void Clear() {
		m_strMethod.clear();
		m_strUrl.clear();
		m_strTail.clear();
		m_strContent.clear();
		m_mapValues.clear();
	}

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

138
	const StrCaseMap& getValues() const {
xzl committed
139 140 141 142 143 144 145 146 147
		return m_mapValues;
	}

private:
	string m_strMethod;
	string m_strUrl;
	string m_strTail;
	string m_strContent;
	string m_strNull;
148
	StrCaseMap m_mapValues;
xzl committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

};

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_