ZLMediaKit/src/Rtsp/Rtsp.cpp

169 lines
4.3 KiB
C++
Raw Normal View History

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
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* 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"
2019-06-28 16:48:02 +08:00
#include "Common/Parser.h"
2017-04-25 11:35:41 +08:00
2019-03-27 18:41:52 +08:00
namespace mediakit{
2019-06-28 16:48:02 +08:00
void SdpParser::load(const string &sdp) {
2018-10-25 22:57:59 +08:00
_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>();
2019-06-28 16:48:02 +08:00
key = FindField(opt_val.data(), nullptr," ");
2018-10-25 22:57:59 +08:00
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 == "") {
2018-10-26 10:59:13 +08:00
track._type = TrackTitle;
2018-10-25 22:57:59 +08:00
} else if (pr.first == "video") {
2018-10-26 10:59:13 +08:00
track._type = TrackVideo;
2018-10-25 18:50:18 +08:00
} else if (pr.first == "audio") {
2018-10-26 10:59:13 +08:00
track._type = TrackAudio;
2018-10-25 22:57:59 +08:00
} else {
2018-10-26 10:59:13 +08:00
track._type = TrackInvalid;
2018-10-25 22:57:59 +08:00
}
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-26 09:56:29 +08:00
auto surffix = string("/") + track._control;
track._control_surffix = surffix.substr(1 + surffix.rfind('/'));
2018-10-25 18:50:18 +08:00
}
}
2018-10-25 22:57:59 +08:00
}
2018-10-25 18:50:18 +08:00
2019-06-28 16:48:02 +08:00
bool SdpParser::available() const {
2018-10-25 23:24:23 +08:00
return getTrack(TrackAudio) || getTrack(TrackVideo);
}
2019-06-28 16:48:02 +08:00
SdpTrack::Ptr SdpParser::getTrack(TrackType type) const {
2018-10-25 22:57:59 +08:00
for (auto &pr : _track_map){
2018-10-26 10:59:13 +08:00
if(pr.second->_type == type){
2018-10-25 22:57:59 +08:00
return pr.second;
}
}
return nullptr;
}
2018-10-25 18:50:18 +08:00
2019-06-28 16:48:02 +08:00
vector<SdpTrack::Ptr> SdpParser::getAvailableTrack() const {
2018-10-26 09:56:29 +08:00
vector<SdpTrack::Ptr> ret;
auto video = getTrack(TrackVideo);
if(video){
ret.emplace_back(video);
2017-04-01 16:35:56 +08:00
}
2018-10-26 09:56:29 +08:00
auto audio = getTrack(TrackAudio);
if(audio){
ret.emplace_back(audio);
}
return ret;
2017-04-01 16:35:56 +08:00
}
2018-10-26 09:56:29 +08:00
2019-03-27 18:41:52 +08:00
}//namespace mediakit
2018-10-25 22:57:59 +08:00