rtsp协议分解函数修订

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rtsp协议分解函数修订相关的知识,希望对你有一定的参考价值。

上次写了一个rtsp协议分解,把rtsp得各个部分分解开来放入到map中,发现了一些错误。
如下字符

string strlines =
"OPTIONS rtsp ://192.168.0.129:8554 RTSP/1.0\\r\\n"
"CSeq: 1\\r\\n"
"User - Agent : RealMedia Player Version 6.0.9.1235 (linux - 2.0 - libc6 - i386 - gcc2.95)\\r\\n"
"ClientChallenge : 9e26d33f2984236010ef6253fb1887f7\\r\\n"
"PlayerStarttime : [28 / 03 / 2003:22 : 50 : 23 00 : 00]\\r\\n"
"CompanyID : KnKV4M4I / B2FjJ1TToLycw ==\\r\\n"
"GUID : 00000000 - 0000 - 0000 - 0000 - 000000000000\\r\\n"
"RegionData : 0\\r\\n"
"ClientID : Linux_2.4_6.0.9.1235_play32_RN01_EN_586\\r\\n\\r\\n";

使用 fetch_head_info_rtsp函数出错,原因是rtsp 协议options 中请求无路由
如果是rtsp 😕/192.168.0.129:8554/live/1001 则没有错误,ok,修正这个错误,代码如下:

//适应rtsp协议
static int fetch_head_info_rtsp(std::string &lines, c_header_map &hmap)

	//we analyse the string like this:
	//OPTIONS rtsp://ip/live/1001 rtsp/1.0
	std::istringstream s(lines);
	std::string request;
	std::getline(s, request);
	size_t len = request.size();
	if (request[len - 1] != '\\r')
	
		//please use log instead of cout,of course ,now we just cout
		std::cout << "request not fit:" << request << std::endl;
		return -1;
	
	request[len - 1] = '\\0';
	//we do not use this,instead, to use request[x]='\\0'  
	//request.erase(request.end() - 1);
	size_t i = 2;
	while (request[++i] != ' ');

	hmap["method"] = request.substr(0, i);
	std::string url = request.substr(i + 1);
	i = 7; //优化rtsp协议
	while (url[++i] != ' ');
	hmap["baseurl"] = url.substr(0, i);
	hmap["protocol"] = url.substr(i + 1);
	std::string &baseurl = hmap["baseurl"];
	i = 7;//rtsp://
    //rtsp://192.168.0.129:8554 error
	size_t lenb = baseurl.size();
	while (++i < lenb)
	
		if (baseurl[i] == '/')
			break;
	
	//while (baseurl[++i] != '/');
	hmap["route"] = i>=lenb?"":baseurl.substr(i + 1);
	hmap["url"] = url;

	std::string header;

	//while (std::getline(s, header) && header != "\\r")
	while (1)
	
		if (!std::getline(s, header))
			break;
		size_t len = header.size();
		if (header[len - 1] == '\\r')
		
			header[len - 1] = '\\0';//remove last char
			//header.erase(header.end() - 1); 
			size_t end = header.find(": ", 0);
			if (end == std::string::npos) //优化rtsp协议
				break;
			std::string key = header.substr(0, end);
			std::string value = header.substr(end + 2);
			hmap[key] = value;
		
	
	return (int)hmap.size();

这种常见错误为边界错误,使用单元测试解决。测试代码如下:

string strlines =
"OPTIONS rtsp ://192.168.0.129:8554/live/1001 RTSP/1.0\\r\\n"
"CSeq: 1\\r\\n"
"User - Agent : RealMedia Player Version 6.0.9.1235 (linux - 2.0 - libc6 - i386 - gcc2.95)\\r\\n"
"ClientChallenge : 9e26d33f2984236010ef6253fb1887f7\\r\\n"
"PlayerStarttime : [28 / 03 / 2003:22 : 50 : 23 00 : 00]\\r\\n"
"CompanyID : KnKV4M4I / B2FjJ1TToLycw ==\\r\\n"
"GUID : 00000000 - 0000 - 0000 - 0000 - 000000000000\\r\\n"
"RegionData : 0\\r\\n"
"ClientID : Linux_2.4_6.0.9.1235_play32_RN01_EN_586\\r\\n\\r\\n";


int main()

	c_header_map maps;
	int size = fetch_head_info_rtsp(strlines, maps);
	cout << size << endl;
	cout << maps["route"] << endl;
	//int num = func_getpacket_num(NULL,1400);
	//cout << num << endl;
	//num = func_getpacket_num(NULL, 2800);
	//cout << num << endl;

以上是关于rtsp协议分解函数修订的主要内容,如果未能解决你的问题,请参考以下文章

什么是RTMP 和 RTSP?它们之间有什么区别?

什么是RTMP 和 RTSP?它们之间有什么区别?

RTSP协议视频流媒体播放器EasyPlayer-RTSP-OCX接口文档API接口函数定义

流媒体专家RTSP协议详解I

RTSP协议学习

RTSP协议详解