Rtsp.cpp 3.96 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
25 26 27
 */

#include <stdlib.h>
xiongzilaing committed
28 29
#include "Rtsp.h"

xzl committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
string FindField(const char* buf, const char* start, const char *end ,int bufSize) {
	if(bufSize <=0 ){
		bufSize = strlen(buf);
	}
	const char *msg_start = buf, *msg_end = buf + bufSize;
	int len = 0;
	if (start != NULL) {
		len = strlen(start);
		msg_start = strstr(buf, start);
	}
	if (msg_start == NULL) {
		return "";
	}
	msg_start += len;
	if (end != NULL) {
		msg_end = strstr(msg_start, end);
		if (msg_end == NULL) {
			return "";
		}
	}
	return string(msg_start, msg_end);
}
int parserSDP(const string& sdp, RtspTrack Track[2]) {
	int track_cnt = 0;
54 55 56
	string::size_type pos_head = 0;
	while ((pos_head = sdp.find("m=",pos_head)) != string::npos ) {
        auto pos_end = sdp.find("m=", pos_head + 2);
xzl committed
57 58 59
		if (pos_end == string::npos) {
			pos_end = sdp.size();
		}
60 61
		auto sdp_mid = sdp.substr(pos_head, pos_end - pos_head);
		Track[track_cnt].trackSdp = sdp_mid;
xzl committed
62
		Track[track_cnt].inited = false;
63 64
		Track[track_cnt].PT = atoi(FindField(sdp_mid.c_str(), "a=rtpmap:", " ").c_str());
        auto control = string("/") + trim(FindField(sdp_mid.c_str(), "a=control:", "\n"));
xiongziliang committed
65
        Track[track_cnt].controlSuffix = control.substr(1 + control.rfind('/'));
66 67

		if (sdp_mid.find("m=video") != string::npos) {
xzl committed
68 69
			//视频通道
			Track[track_cnt].type = TrackVideo;
70
        } else if (sdp_mid.find("m=audio") != string::npos) {
xzl committed
71 72
			//音频通道
			Track[track_cnt].type = TrackAudio;
73
        } else {
xzl committed
74
			//不识别的track
75
			return track_cnt;
xzl committed
76 77
		}
		pos_head = pos_end;
78
        track_cnt++;
xzl committed
79 80 81
	}
	return track_cnt;
}
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//static  onceToken s_token([](){
//   string str = "v=0\n"
//           "o=- 1001 1 IN IP4 192.168.0.22\n"
//           "s=VCP IPC Realtime stream\n"
//           "m=video 0 RTP/AVP 105\n"
//           "c=IN IP4 192.168.0.22\n"
//           "a=control:rtsp://192.168.0.22/media/video1/video\n"
//           "a=rtpmap:105 H264/90000\n"
//           "a=fmtp:105 profile-level-id=64001f; packetization-mode=1; sprop-parameter-sets=Z2QAH6wrUCgC3QgAAB9AAAYahCAA,aO4xsg==\n"
//           "a=recvonly\n"
//           "m=application 0 RTP/AVP 107\n"
//           "c=IN IP4 192.168.0.22\n"
//           "a=control:rtsp://192.168.0.22/media/video1/metadata\n"
//           "a=rtpmap:107 vnd.onvif.metadata/90000\n"
//           "a=fmtp:107 DecoderTag=h3c-v3 RTCP=0\n"
//           "a=recvonly";
//    RtspTrack track[2];
//    parserSDP(str,track);
//    track[0].inited=true;
//});
102
bool MakeNalu(uint8_t in, NALU &nal) {
xzl committed
103 104 105 106 107 108 109 110
	nal.forbidden_zero_bit = in >> 7;
	if (nal.forbidden_zero_bit) {
		return false;
	}
	nal.nal_ref_idc = (in & 0x60) >> 5;
	nal.type = in & 0x1f;
	return true;
}
111
bool MakeFU(uint8_t in, FU &fu) {
xzl committed
112 113 114 115 116 117 118 119 120
	fu.S = in >> 7;
	fu.E = (in >> 6) & 0x01;
	fu.R = (in >> 5) & 0x01;
	fu.type = in & 0x1f;
	if (fu.R != 0) {
		return false;
	}
	return true;
}