Rtsp.cpp 4.26 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
#include "Rtsp.h"
xiongziliang committed
29
#include "Common/Parser.h"
xiongzilaing committed
30

xiongziliang committed
31 32
namespace mediakit{

xiongziliang committed
33
void SdpParser::load(const string &sdp) {
34 35 36
	_track_map.clear();
	string key;
	SdpTrack::Ptr track = std::make_shared<SdpTrack>();
xiongziliang committed
37 38 39 40

	auto lines = split(sdp,"\n");
	for (auto &line : lines){
		trim(line);
41
		if(line.size() < 2 || line[1] != '='){
xiongziliang committed
42 43 44 45 46 47
			continue;
		}
		char opt = line[0];
		string opt_val = line.substr(2);
		switch (opt){
			case 'o':
48 49
				track->_o = opt_val;
				break;
xiongziliang committed
50
			case 's':
51 52
				track->_s = opt_val;
				break;
xiongziliang committed
53
			case 'i':
54 55
				track->_i = opt_val;
				break;
xiongziliang committed
56
			case 'c':
57 58 59 60 61 62 63
				track->_c = opt_val;
				break;
			case 't':
				track->_t = opt_val;
				break;
			case 'b':
				track->_b = opt_val;
xiongziliang committed
64 65
				break;
			case 'm':{
66 67
				_track_map[key] = track;
				track = std::make_shared<SdpTrack>();
xiongziliang committed
68
				key = FindField(opt_val.data(), nullptr," ");
69
				track->_m = opt_val;
xiongziliang committed
70 71 72 73 74
			}
				break;
			case 'a':{
				string attr = FindField(opt_val.data(), nullptr,":");
				if(attr.empty()){
75
					track->_attr[opt_val] = "";
xiongziliang committed
76
				}else{
77
					track->_attr[attr] = FindField(opt_val.data(),":", nullptr);
xiongziliang committed
78 79 80 81
				}
			}
				break;
			default:
82
				track->_other[opt] = opt_val;
xiongziliang committed
83 84 85
				break;
		}
	}
86 87
	_track_map[key] = track;

xiongziliang committed
88

89 90 91
	for (auto &pr : _track_map) {
		auto &track = *pr.second;
		if (pr.first == "") {
92
			track._type = TrackTitle;
93
		} else if (pr.first == "video") {
94
			track._type = TrackVideo;
xiongziliang committed
95
		} else if (pr.first == "audio") {
96
			track._type = TrackAudio;
97
		} else {
98
			track._type = TrackInvalid;
99
		}
xiongziliang committed
100

101 102 103 104 105 106 107 108 109 110 111
		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;
xiongziliang committed
112 113 114
			}
		}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		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;
		}
xiongziliang committed
131

132 133 134
		it = track._attr.find("control");
		if(it != track._attr.end()) {
			track._control = it->second;
xiongziliang committed
135 136
			auto surffix = string("/") + track._control;
			track._control_surffix = surffix.substr(1 + surffix.rfind('/'));
xiongziliang committed
137 138
		}
	}
139
}
xiongziliang committed
140

xiongziliang committed
141
bool SdpParser::available() const {
xiongziliang committed
142 143 144
    return getTrack(TrackAudio) || getTrack(TrackVideo);
}

xiongziliang committed
145
SdpTrack::Ptr SdpParser::getTrack(TrackType type) const {
146
	for (auto &pr : _track_map){
147
		if(pr.second->_type == type){
148 149 150 151 152
			return pr.second;
		}
	}
	return nullptr;
}
xiongziliang committed
153

xiongziliang committed
154
vector<SdpTrack::Ptr> SdpParser::getAvailableTrack() const {
xiongziliang committed
155 156 157 158
	vector<SdpTrack::Ptr> ret;
	auto video = getTrack(TrackVideo);
	if(video){
		ret.emplace_back(video);
xzl committed
159
	}
xiongziliang committed
160 161 162 163 164
	auto audio = getTrack(TrackAudio);
	if(audio){
		ret.emplace_back(audio);
	}
	return ret;
xzl committed
165
}
xiongziliang committed
166

xiongziliang committed
167
}//namespace mediakit
168