提高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,20 +85,38 @@ string SdpTrack::toString() const {
} }
return _printer; return _printer;
} }
static TrackType toTrackType(const string &str) {
if (str == "") {
return TrackTitle;
}
if (str == "video") {
return TrackVideo;
}
if (str == "audio") {
return TrackAudio;
}
return TrackInvalid;
}
void SdpParser::load(const string &sdp) { void SdpParser::load(const string &sdp) {
_track_map.clear(); {
_track_vec.clear();
string key; string key;
SdpTrack::Ptr track = std::make_shared<SdpTrack>(); SdpTrack::Ptr track = std::make_shared<SdpTrack>();
auto lines = split(sdp,"\n"); auto lines = split(sdp, "\n");
for (auto &line : lines){ for (auto &line : lines) {
trim(line); trim(line);
if(line.size() < 2 || line[1] != '='){ if (line.size() < 2 || line[1] != '=') {
continue; continue;
} }
char opt = line[0]; char opt = line[0];
string opt_val = line.substr(2); string opt_val = line.substr(2);
switch (opt){ switch (opt) {
case 'o': case 'o':
track->_o = opt_val; track->_o = opt_val;
break; break;
@ -117,19 +135,20 @@ void SdpParser::load(const string &sdp) {
case 'b': case 'b':
track->_b = opt_val; track->_b = opt_val;
break; break;
case 'm':{ case 'm': {
_track_map[key] = track; track->_type = toTrackType(key);
_track_vec.emplace_back(track);
track = std::make_shared<SdpTrack>(); track = std::make_shared<SdpTrack>();
key = FindField(opt_val.data(), nullptr," "); key = FindField(opt_val.data(), nullptr, " ");
track->_m = opt_val; track->_m = opt_val;
} }
break; break;
case 'a':{ case 'a': {
string attr = FindField(opt_val.data(), nullptr,":"); string attr = FindField(opt_val.data(), nullptr, ":");
if(attr.empty()){ if (attr.empty()) {
track->_attr[opt_val] = ""; track->_attr[opt_val] = "";
}else{ } else {
track->_attr[attr] = FindField(opt_val.data(),":", nullptr); track->_attr[attr] = FindField(opt_val.data(), ":", nullptr);
} }
} }
break; break;
@ -138,21 +157,12 @@ void SdpParser::load(const string &sdp) {
break; break;
} }
} }
_track_map[key] = track; track->_type = toTrackType(key);
_track_vec.emplace_back(track);
for (auto &pr : _track_map) {
auto &track = *pr.second;
if (pr.first == "") {
track._type = TrackTitle;
} else if (pr.first == "video") {
track._type = TrackVideo;
} else if (pr.first == "audio") {
track._type = TrackAudio;
} else {
track._type = TrackInvalid;
} }
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;
} }
auto audio = getTrack(TrackAudio); continue;
if(audio){
ret.emplace_back(audio);
} }
return ret;
if(track->_type == TrackVideo ){
if(!video_added){
ret.emplace_back(track);
video_added = true;
}
continue;
}
}
return std::move(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;
}; };
/** /**