ZLMediaKit/src/Rtsp/Rtsp.cpp

407 lines
12 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
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{
2020-03-26 17:12:21 +08:00
int RtpPayload::getClockRate(int pt){
switch (pt){
2020-04-18 23:00:48 +08:00
#define SWITCH_CASE(name, type, value, clock_rate, channel, codec_id) case value : return clock_rate;
2020-03-26 17:12:21 +08:00
RTP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE
default: return 90000;
}
}
TrackType RtpPayload::getTrackType(int pt){
switch (pt){
2020-04-18 23:00:48 +08:00
#define SWITCH_CASE(name, type, value, clock_rate, channel, codec_id) case value : return type;
2020-03-26 17:12:21 +08:00
RTP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE
default: return TrackInvalid;
}
}
int RtpPayload::getAudioChannel(int pt){
switch (pt){
2020-04-18 23:00:48 +08:00
#define SWITCH_CASE(name, type, value, clock_rate, channel, codec_id) case value : return channel;
2020-03-26 17:12:21 +08:00
RTP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE
default: return 1;
}
}
const char * RtpPayload::getName(int pt){
switch (pt){
2020-04-18 23:00:48 +08:00
#define SWITCH_CASE(name, type, value, clock_rate, channel, codec_id) case value : return #name;
2020-03-26 17:12:21 +08:00
RTP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE
default: return "unknown payload type";
}
}
2020-04-18 23:00:48 +08:00
CodecId RtpPayload::getCodecId(int pt) {
switch (pt) {
#define SWITCH_CASE(name, type, value, clock_rate, channel, codec_id) case value : return codec_id;
RTP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE
default : return CodecInvalid;
}
}
2019-08-15 19:13:31 +08:00
static void getAttrSdp(const map<string, string> &attr, _StrPrinter &printer){
2020-03-20 11:51:24 +08:00
const map<string, string>::value_type *ptr = nullptr;
for(auto &pr : attr){
if(pr.first == "control"){
ptr = &pr;
continue;
}
if(pr.second.empty()){
printer << "a=" << pr.first << "\r\n";
}else{
printer << "a=" << pr.first << ":" << pr.second << "\r\n";
}
}
if(ptr){
printer << "a=" << ptr->first << ":" << ptr->second << "\r\n";
}
2019-08-15 19:13:31 +08:00
}
2020-03-26 17:12:21 +08:00
string SdpTrack::getName() const{
switch (_pt){
2020-04-18 23:00:48 +08:00
#define SWITCH_CASE(name, type, value, clock_rate, channel, codec_id) case value : return #name;
2020-03-26 17:12:21 +08:00
RTP_PT_MAP(SWITCH_CASE)
#undef SWITCH_CASE
default: return _codec;
}
}
2019-08-15 19:13:31 +08:00
string SdpTrack::toString() const {
2020-03-20 11:51:24 +08:00
_StrPrinter _printer;
switch (_type){
case TrackTitle:{
_printer << "v=" << 0 << "\r\n";
if(!_o.empty()){
_printer << "o="<< _o << "\r\n";
}
if(!_c.empty()){
_printer << "c=" << _c << "\r\n";
}
if(!_t.empty()){
_printer << "t=" << _t << "\r\n";
}
2019-08-15 19:13:31 +08:00
2020-04-04 19:55:11 +08:00
_printer << "s=Streamed by " << SERVER_NAME << "\r\n";
2020-03-20 11:51:24 +08:00
getAttrSdp(_attr,_printer);
}
break;
case TrackAudio:
case TrackVideo:{
if(_type == TrackAudio){
_printer << "m=audio 0 RTP/AVP " << _pt << "\r\n";
}else{
_printer << "m=video 0 RTP/AVP " << _pt << "\r\n";
}
if(!_b.empty()){
_printer << "b=" <<_b << "\r\n";
}
getAttrSdp(_attr,_printer);
}
break;
default:
break;
}
return _printer;
2019-08-15 19:13:31 +08:00
}
2020-01-14 18:11:10 +08:00
static TrackType toTrackType(const string &str) {
2020-03-20 11:51:24 +08:00
if (str == "") {
return TrackTitle;
}
2020-01-14 18:11:10 +08:00
2020-03-20 11:51:24 +08:00
if (str == "video") {
return TrackVideo;
}
2020-01-14 18:11:10 +08:00
2020-03-20 11:51:24 +08:00
if (str == "audio") {
return TrackAudio;
}
2020-01-14 18:11:10 +08:00
2020-03-20 11:51:24 +08:00
return TrackInvalid;
2020-01-14 18:11:10 +08:00
}
2019-06-28 16:48:02 +08:00
void SdpParser::load(const string &sdp) {
2020-03-20 11:51:24 +08:00
{
_track_vec.clear();
SdpTrack::Ptr track = std::make_shared<SdpTrack>();
2020-03-26 17:12:21 +08:00
track->_type = TrackTitle;
_track_vec.emplace_back(track);
2018-10-25 18:50:18 +08:00
2020-03-20 11:51:24 +08:00
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 = std::make_shared<SdpTrack>();
2020-03-26 17:12:21 +08:00
int pt, port;
char rtp[16] = {0}, type[16];
if (4 == sscanf(opt_val.data(), " %15[^ ] %d %15[^ ] %d", type, &port, rtp, &pt)) {
track->_pt = pt;
track->_samplerate = RtpPayload::getClockRate(pt) ;
2020-04-18 22:13:11 +08:00
track->_channel = RtpPayload::getAudioChannel(pt);
2020-03-26 17:12:21 +08:00
track->_type = toTrackType(type);
track->_m = opt_val;
track->_port = port;
_track_vec.emplace_back(track);
}
2020-03-20 11:51:24 +08:00
}
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;
}
}
}
2018-10-25 18:50:18 +08:00
2020-03-20 11:51:24 +08:00
for (auto &track_ptr : _track_vec) {
auto &track = *track_ptr;
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
2020-03-20 11:51:24 +08:00
it = track._attr.find("rtpmap");
if(it != track._attr.end()){
auto rtpmap = it->second;
2020-04-18 22:13:11 +08:00
int pt, samplerate, channel;
2020-03-20 11:51:24 +08:00
char codec[16] = {0};
2020-04-18 22:13:11 +08:00
if (4 == sscanf(rtpmap.data(), "%d %15[^/]/%d/%d", &pt, codec, &samplerate, &channel)) {
track._pt = pt;
track._codec = codec;
track._samplerate = samplerate;
track._channel = channel;
}else if (3 == sscanf(rtpmap.data(), "%d %15[^/]/%d", &pt, codec, &samplerate)) {
2020-03-20 11:51:24 +08:00
track._pt = pt;
track._codec = codec;
track._samplerate = samplerate;
}
}
2018-10-25 22:57:59 +08:00
2020-03-20 11:51:24 +08:00
it = track._attr.find("fmtp");
if(it != track._attr.end()) {
track._fmtp = it->second;
}
2018-10-25 18:50:18 +08:00
2020-03-20 11:51:24 +08:00
it = track._attr.find("control");
if(it != track._attr.end()) {
track._control = it->second;
auto surffix = string("/") + track._control;
track._control_surffix = surffix.substr(1 + surffix.rfind('/'));
}
}
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 {
2020-03-20 11:51:24 +08:00
for (auto &track : _track_vec){
if(track->_type == type){
return track;
}
}
return nullptr;
2018-10-25 22:57:59 +08:00
}
2018-10-25 18:50:18 +08:00
2019-06-28 16:48:02 +08:00
vector<SdpTrack::Ptr> SdpParser::getAvailableTrack() const {
2020-03-20 11:51:24 +08:00
vector<SdpTrack::Ptr> ret;
bool audio_added = false;
bool video_added = false;
for (auto &track : _track_vec){
if(track->_type == TrackAudio ){
if(!audio_added){
ret.emplace_back(track);
audio_added = true;
}
continue;
}
2020-01-14 18:11:10 +08:00
2020-03-20 11:51:24 +08:00
if(track->_type == TrackVideo ){
if(!video_added){
ret.emplace_back(track);
video_added = true;
}
continue;
}
}
return std::move(ret);
2017-04-01 16:35:56 +08:00
}
2018-10-26 09:56:29 +08:00
2019-08-15 19:13:31 +08:00
string SdpParser::toString() const {
2020-03-20 11:51:24 +08:00
string title,audio,video;
for(auto &track : _track_vec){
switch (track->_type){
case TrackTitle:{
title = track->toString();
}
break;
case TrackVideo:{
video = track->toString();
}
break;
case TrackAudio:{
audio = track->toString();
}
break;
default:
break;
}
}
return title + video + audio;
2019-08-15 19:13:31 +08:00
}
bool RtspUrl::parse(const string &strUrl) {
2020-03-20 11:51:24 +08:00
auto schema = FindField(strUrl.data(), nullptr, "://");
bool isSSL = strcasecmp(schema.data(), "rtsps") == 0;
//查找"://"与"/"之间的字符串,用于提取用户名密码
auto middle_url = FindField(strUrl.data(), "://", "/");
if (middle_url.empty()) {
middle_url = FindField(strUrl.data(), "://", nullptr);
}
auto pos = middle_url.rfind('@');
if (pos == string::npos) {
//并没有用户名密码
return setup(isSSL, strUrl, "", "");
}
2020-03-20 11:51:24 +08:00
//包含用户名密码
auto user_pwd = middle_url.substr(0, pos);
auto suffix = strUrl.substr(schema.size() + 3 + pos + 1);
auto url = StrPrinter << "rtsp://" << suffix << endl;
if (user_pwd.find(":") == string::npos) {
return setup(isSSL, url, user_pwd, "");
}
auto user = FindField(user_pwd.data(), nullptr, ":");
auto pwd = FindField(user_pwd.data(), ":", nullptr);
return setup(isSSL, url, user, pwd);
}
bool RtspUrl::setup(bool isSSL, const string &strUrl, const string &strUser, const string &strPwd) {
2020-03-20 11:51:24 +08:00
auto ip = FindField(strUrl.data(), "://", "/");
if (ip.empty()) {
ip = split(FindField(strUrl.data(), "://", NULL), "?")[0];
}
auto port = atoi(FindField(ip.data(), ":", NULL).data());
if (port <= 0 || port > UINT16_MAX) {
//rtsp 默认端口554
port = isSSL ? 322 : 554;
} else {
//服务器域名
ip = FindField(ip.data(), NULL, ":");
}
2020-03-20 11:51:24 +08:00
if (ip.empty()) {
return false;
}
2020-03-20 11:51:24 +08:00
_url = std::move(strUrl);
_user = std::move(strUser);
_passwd = std::move(strPwd);
_host = std::move(ip);
_port = port;
_is_ssl = isSSL;
return true;
}
2020-05-12 10:22:21 +08:00
std::pair<Socket::Ptr, Socket::Ptr> makeSockPair_l(const EventPoller::Ptr &poller, const string &local_ip){
auto pSockRtp = std::make_shared<Socket>(poller);
if (!pSockRtp->bindUdpSock(0, local_ip.data())) {
//分配端口失败
throw runtime_error("open udp socket failed");
}
//是否是偶数
2020-05-12 10:37:23 +08:00
bool even_numbers = pSockRtp->get_local_port() % 2 == 0;
2020-05-12 10:22:21 +08:00
auto pSockRtcp = std::make_shared<Socket>(poller);
if (!pSockRtcp->bindUdpSock(pSockRtp->get_local_port() + (even_numbers ? 1 : -1), local_ip.data())) {
//分配端口失败
throw runtime_error("open udp socket failed");
}
if (!even_numbers) {
//如果rtp端口不是偶数那么与rtcp端口互换目的是兼容一些要求严格的播放器或服务器
Socket::Ptr tmp = pSockRtp;
pSockRtp = pSockRtcp;
pSockRtcp = tmp;
}
return std::make_pair(pSockRtp, pSockRtcp);
}
std::pair<Socket::Ptr, Socket::Ptr> makeSockPair(const EventPoller::Ptr &poller, const string &local_ip){
int try_count = 0;
while (true) {
try {
return makeSockPair_l(poller, local_ip);
} catch (...) {
if (++try_count == 3) {
throw;
}
WarnL << "open udp socket failed, retry: " << try_count;
}
}
}
2018-10-25 22:57:59 +08:00
2020-05-12 10:22:21 +08:00
}//namespace mediakit