2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* 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.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Rtsp.h"
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
2018-10-25 22:57:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SdpAttr::load(const string &sdp) {
|
|
|
|
|
_track_map.clear();
|
|
|
|
|
string key;
|
|
|
|
|
SdpTrack::Ptr track = std::make_shared<SdpTrack>();
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
|
|
|
|
auto lines = split(sdp,"\n");
|
|
|
|
|
for (auto &line : lines){
|
|
|
|
|
trim(line);
|
2018-10-25 22:57:59 +08:00
|
|
|
|
if(line.size() < 2 || line[1] != '='){
|
2018-10-25 18:50:18 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
char opt = line[0];
|
|
|
|
|
string opt_val = line.substr(2);
|
|
|
|
|
switch (opt){
|
|
|
|
|
case 'o':
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_o = opt_val;
|
|
|
|
|
break;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
case 's':
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_s = opt_val;
|
|
|
|
|
break;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
case 'i':
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_i = opt_val;
|
|
|
|
|
break;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
case 'c':
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_c = opt_val;
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
track->_t = opt_val;
|
|
|
|
|
break;
|
|
|
|
|
case 'b':
|
|
|
|
|
track->_b = opt_val;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
break;
|
|
|
|
|
case 'm':{
|
2018-10-25 22:57:59 +08:00
|
|
|
|
_track_map[key] = track;
|
|
|
|
|
track = std::make_shared<SdpTrack>();
|
|
|
|
|
key = FindField(opt_val.data(), nullptr," ");;
|
|
|
|
|
track->_m = opt_val;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'a':{
|
|
|
|
|
string attr = FindField(opt_val.data(), nullptr,":");
|
|
|
|
|
if(attr.empty()){
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_attr[opt_val] = "";
|
2018-10-25 18:50:18 +08:00
|
|
|
|
}else{
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_attr[attr] = FindField(opt_val.data(),":", nullptr);
|
2018-10-25 18:50:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track->_other[opt] = opt_val;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-25 22:57:59 +08:00
|
|
|
|
_track_map[key] = track;
|
|
|
|
|
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
for (auto &pr : _track_map) {
|
|
|
|
|
auto &track = *pr.second;
|
|
|
|
|
if (pr.first == "") {
|
|
|
|
|
track._type = TrackTitle;
|
|
|
|
|
} else if (pr.first == "video") {
|
|
|
|
|
track._type = TrackVideo;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
} else if (pr.first == "audio") {
|
2018-10-25 22:57:59 +08:00
|
|
|
|
track._type = TrackAudio;
|
|
|
|
|
} else {
|
|
|
|
|
track._type = TrackInvalid;
|
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
auto it = track._attr.find("range");
|
|
|
|
|
if (it != track._attr.end()) {
|
|
|
|
|
char name[16] = {0}, start[16] = {0}, end[16] = {0};
|
|
|
|
|
int ret = sscanf(it->second.data(), "%15[^=]=%15[^-]-%15s", name, start, end);
|
|
|
|
|
if (3 == ret || 2 == ret) {
|
|
|
|
|
if (strcmp(start, "now") == 0) {
|
|
|
|
|
strcpy(start, "0");
|
|
|
|
|
}
|
|
|
|
|
track._start = atof(start);
|
|
|
|
|
track._end = atof(end);
|
|
|
|
|
track._duration = track._end - track._start;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
it = track._attr.find("rtpmap");
|
|
|
|
|
if(it != track._attr.end()){
|
|
|
|
|
auto rtpmap = it->second;
|
|
|
|
|
int pt, samplerate;
|
|
|
|
|
char codec[16] = {0};
|
|
|
|
|
if (3 == sscanf(rtpmap.data(), "%d %15[^/]/%d", &pt, codec, &samplerate)) {
|
|
|
|
|
track._pt = pt;
|
|
|
|
|
track._codec = codec;
|
|
|
|
|
track._samplerate = samplerate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it = track._attr.find("fmtp");
|
|
|
|
|
if(it != track._attr.end()) {
|
|
|
|
|
track._fmtp = it->second;
|
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
it = track._attr.find("control");
|
|
|
|
|
if(it != track._attr.end()) {
|
|
|
|
|
track._control = it->second;
|
2018-10-25 18:50:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-25 22:57:59 +08:00
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
SdpTrack::Ptr SdpAttr::getTrack(TrackType type) {
|
|
|
|
|
for (auto &pr : _track_map){
|
|
|
|
|
if(pr.second->_type == type){
|
|
|
|
|
return pr.second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2018-10-25 22:57:59 +08:00
|
|
|
|
int parserSDP(const string& sdp, RtspTrack Track[2]) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
int track_cnt = 0;
|
2018-03-26 18:56:22 +08:00
|
|
|
|
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);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (pos_end == string::npos) {
|
|
|
|
|
pos_end = sdp.size();
|
|
|
|
|
}
|
2018-03-26 18:56:22 +08:00
|
|
|
|
auto sdp_mid = sdp.substr(pos_head, pos_end - pos_head);
|
2018-09-18 21:40:26 +08:00
|
|
|
|
pos_head = pos_end;
|
2018-03-26 18:56:22 +08:00
|
|
|
|
Track[track_cnt].trackSdp = sdp_mid;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
Track[track_cnt].inited = false;
|
2018-03-26 18:56:22 +08:00
|
|
|
|
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"));
|
2018-05-18 18:48:17 +08:00
|
|
|
|
Track[track_cnt].controlSuffix = control.substr(1 + control.rfind('/'));
|
2018-03-26 18:56:22 +08:00
|
|
|
|
|
|
|
|
|
if (sdp_mid.find("m=video") != string::npos) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//视频通道
|
|
|
|
|
Track[track_cnt].type = TrackVideo;
|
2018-03-26 18:56:22 +08:00
|
|
|
|
} else if (sdp_mid.find("m=audio") != string::npos) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//音频通道
|
|
|
|
|
Track[track_cnt].type = TrackAudio;
|
2018-03-26 18:56:22 +08:00
|
|
|
|
} else {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//不识别的track
|
2018-09-18 21:40:26 +08:00
|
|
|
|
continue;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-03-26 18:56:22 +08:00
|
|
|
|
track_cnt++;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return track_cnt;
|
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
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"
|
|
|
|
|
"a=range:npt=0-\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);
|
2018-10-25 22:57:59 +08:00
|
|
|
|
SdpAttr attr(str);
|
2018-10-25 18:50:18 +08:00
|
|
|
|
track[0].inited=true;
|
|
|
|
|
});
|
2018-07-30 11:58:39 +08:00
|
|
|
|
bool MakeNalu(uint8_t in, NALU &nal) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-07-30 11:58:39 +08:00
|
|
|
|
bool MakeFU(uint8_t in, FU &fu) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-10-25 22:57:59 +08:00
|
|
|
|
|
|
|
|
|
|