提高rtsp客户端兼容性

This commit is contained in:
xiongziliang 2020-01-14 18:11:10 +08:00
parent 2b511a05cc
commit a573d7302b
2 changed files with 100 additions and 79 deletions

View File

@ -85,74 +85,84 @@ string SdpTrack::toString() const {
} }
return _printer; return _printer;
} }
void SdpParser::load(const string &sdp) {
_track_map.clear();
string key;
SdpTrack::Ptr track = std::make_shared<SdpTrack>();
auto lines = split(sdp,"\n"); static TrackType toTrackType(const string &str) {
for (auto &line : lines){ if (str == "") {
trim(line); return TrackTitle;
if(line.size() < 2 || line[1] != '='){
continue;
}
char opt = line[0];
string opt_val = line.substr(2);
switch (opt){
case 'o':
track->_o = opt_val;
break;
case 's':
track->_s = opt_val;
break;
case 'i':
track->_i = opt_val;
break;
case 'c':
track->_c = opt_val;
break;
case 't':
track->_t = opt_val;
break;
case 'b':
track->_b = opt_val;
break;
case 'm':{
_track_map[key] = track;
track = std::make_shared<SdpTrack>();
key = FindField(opt_val.data(), nullptr," ");
track->_m = opt_val;
}
break;
case 'a':{
string attr = FindField(opt_val.data(), nullptr,":");
if(attr.empty()){
track->_attr[opt_val] = "";
}else{
track->_attr[attr] = FindField(opt_val.data(),":", nullptr);
}
}
break;
default:
track->_other[opt] = opt_val;
break;
}
} }
_track_map[key] = track;
if (str == "video") {
return TrackVideo;
}
for (auto &pr : _track_map) { if (str == "audio") {
auto &track = *pr.second; return TrackAudio;
if (pr.first == "") { }
track._type = TrackTitle;
} else if (pr.first == "video") { return TrackInvalid;
track._type = TrackVideo; }
} else if (pr.first == "audio") {
track._type = TrackAudio; void SdpParser::load(const string &sdp) {
} else { {
track._type = TrackInvalid; _track_vec.clear();
string key;
SdpTrack::Ptr track = std::make_shared<SdpTrack>();
auto lines = split(sdp, "\n");
for (auto &line : lines) {
trim(line);
if (line.size() < 2 || line[1] != '=') {
continue;
}
char opt = line[0];
string opt_val = line.substr(2);
switch (opt) {
case 'o':
track->_o = opt_val;
break;
case 's':
track->_s = opt_val;
break;
case 'i':
track->_i = opt_val;
break;
case 'c':
track->_c = opt_val;
break;
case 't':
track->_t = opt_val;
break;
case 'b':
track->_b = opt_val;
break;
case 'm': {
track->_type = toTrackType(key);
_track_vec.emplace_back(track);
track = std::make_shared<SdpTrack>();
key = FindField(opt_val.data(), nullptr, " ");
track->_m = opt_val;
}
break;
case 'a': {
string attr = FindField(opt_val.data(), nullptr, ":");
if (attr.empty()) {
track->_attr[opt_val] = "";
} else {
track->_attr[attr] = FindField(opt_val.data(), ":", nullptr);
}
}
break;
default:
track->_other[opt] = opt_val;
break;
}
} }
track->_type = toTrackType(key);
_track_vec.emplace_back(track);
}
for (auto &track_ptr : _track_vec) {
auto &track = *track_ptr;
auto it = track._attr.find("range"); auto it = track._attr.find("range");
if (it != track._attr.end()) { if (it != track._attr.end()) {
char name[16] = {0}, start[16] = {0}, end[16] = {0}; char name[16] = {0}, start[16] = {0}, end[16] = {0};
@ -198,9 +208,9 @@ bool SdpParser::available() const {
} }
SdpTrack::Ptr SdpParser::getTrack(TrackType type) const { SdpTrack::Ptr SdpParser::getTrack(TrackType type) const {
for (auto &pr : _track_map){ for (auto &track : _track_vec){
if(pr.second->_type == type){ if(track->_type == type){
return pr.second; return track;
} }
} }
return nullptr; return nullptr;
@ -208,31 +218,42 @@ SdpTrack::Ptr SdpParser::getTrack(TrackType type) const {
vector<SdpTrack::Ptr> SdpParser::getAvailableTrack() const { vector<SdpTrack::Ptr> SdpParser::getAvailableTrack() const {
vector<SdpTrack::Ptr> ret; vector<SdpTrack::Ptr> ret;
auto video = getTrack(TrackVideo); bool audio_added = false;
if(video){ bool video_added = false;
ret.emplace_back(video); for (auto &track : _track_vec){
if(track->_type == TrackAudio ){
if(!audio_added){
ret.emplace_back(track);
audio_added = true;
}
continue;
}
if(track->_type == TrackVideo ){
if(!video_added){
ret.emplace_back(track);
video_added = true;
}
continue;
}
} }
auto audio = getTrack(TrackAudio); return std::move(ret);
if(audio){
ret.emplace_back(audio);
}
return ret;
} }
string SdpParser::toString() const { string SdpParser::toString() const {
string title,audio,video; string title,audio,video;
for(auto &pr : _track_map){ for(auto &track : _track_vec){
switch (pr.second->_type){ switch (track->_type){
case TrackTitle:{ case TrackTitle:{
title = pr.second->toString(); title = track->toString();
} }
break; break;
case TrackVideo:{ case TrackVideo:{
video = pr.second->toString(); video = track->toString();
} }
break; break;
case TrackAudio:{ case TrackAudio:{
audio = pr.second->toString(); audio = track->toString();
} }
break; break;
default: default:

View File

@ -122,7 +122,7 @@ public:
vector<SdpTrack::Ptr> getAvailableTrack() const; vector<SdpTrack::Ptr> getAvailableTrack() const;
string toString() const ; string toString() const ;
private: private:
map<string, SdpTrack::Ptr> _track_map; vector<SdpTrack::Ptr> _track_vec;
}; };
/** /**