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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
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
|
|
|
|
*/
|
|
|
|
|
|
2022-03-13 20:48:01 +08:00
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cinttypes>
|
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
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
namespace mediakit {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2021-08-23 21:15:19 +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
|
2021-09-29 01:16:52 +08:00
|
|
|
|
default: return 90000;
|
2020-03-26 17:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
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
|
2021-09-29 01:16:52 +08:00
|
|
|
|
default: return TrackInvalid;
|
2020-03-26 17:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
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
|
2021-09-29 01:16:52 +08:00
|
|
|
|
default: return 1;
|
2020-03-26 17:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
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
|
2021-09-29 01:16:52 +08:00
|
|
|
|
default: return "unknown payload type";
|
2020-03-26 17:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2021-09-29 01:16:52 +08:00
|
|
|
|
default: return CodecInvalid;
|
2020-04-18 23:00:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
static void getAttrSdp(const multimap<string, string> &attr, _StrPrinter &printer) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
const map<string, string>::value_type *ptr = nullptr;
|
2021-08-23 21:15:19 +08:00
|
|
|
|
for (auto &pr : attr) {
|
|
|
|
|
if (pr.first == "control") {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
ptr = ≺
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
if (pr.second.empty()) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
printer << "a=" << pr.first << "\r\n";
|
2021-08-23 21:15:19 +08:00
|
|
|
|
} else {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
printer << "a=" << pr.first << ":" << pr.second << "\r\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
if (ptr) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
printer << "a=" << ptr->first << ":" << ptr->second << "\r\n";
|
|
|
|
|
}
|
2019-08-15 19:13:31 +08:00
|
|
|
|
}
|
2020-03-26 17:12:21 +08:00
|
|
|
|
|
2021-08-23 21:15:19 +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
|
2021-09-29 01:16:52 +08:00
|
|
|
|
default: return _codec;
|
2020-03-26 17:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
string SdpTrack::getControlUrl(const string &base_url) const {
|
2021-02-21 21:27:26 +08:00
|
|
|
|
if (_control.find("://") != string::npos) {
|
|
|
|
|
//以rtsp://开头
|
|
|
|
|
return _control;
|
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
return base_url + "/" + _control;
|
2021-02-21 21:27:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 14:10:04 +08:00
|
|
|
|
string SdpTrack::toString(uint16_t port) const {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
_StrPrinter _printer;
|
2021-08-23 21:15:19 +08:00
|
|
|
|
switch (_type) {
|
|
|
|
|
case TrackTitle: {
|
2021-10-25 19:39:45 +08:00
|
|
|
|
TitleSdp title(_duration);
|
2021-09-29 01:16:52 +08:00
|
|
|
|
_printer << title.getSdp();
|
2020-03-20 11:51:24 +08:00
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
case TrackAudio:
|
2021-08-23 21:15:19 +08:00
|
|
|
|
case TrackVideo: {
|
|
|
|
|
if (_type == TrackAudio) {
|
2021-10-15 14:10:04 +08:00
|
|
|
|
_printer << "m=audio " << port << " RTP/AVP " << _pt << "\r\n";
|
2021-08-23 21:15:19 +08:00
|
|
|
|
} else {
|
2021-10-15 14:10:04 +08:00
|
|
|
|
_printer << "m=video " << port << " RTP/AVP " << _pt << "\r\n";
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
if (!_b.empty()) {
|
|
|
|
|
_printer << "b=" << _b << "\r\n";
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
getAttrSdp(_attr, _printer);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
default: break;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return std::move(_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 '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;
|
2021-02-28 21:02:23 +08:00
|
|
|
|
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->_port = port;
|
|
|
|
|
_track_vec.emplace_back(track);
|
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
case 'a': {
|
|
|
|
|
string attr = FindField(opt_val.data(), nullptr, ":");
|
|
|
|
|
if (attr.empty()) {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
track->_attr.emplace(opt_val, "");
|
2020-03-20 11:51:24 +08:00
|
|
|
|
} else {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
track->_attr.emplace(attr, FindField(opt_val.data(), ":", nullptr));
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
default: track->_other[opt] = opt_val; break;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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");
|
|
|
|
|
}
|
2021-02-28 21:02:23 +08:00
|
|
|
|
track._start = (float) atof(start);
|
|
|
|
|
track._end = (float) atof(end);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
track._duration = track._end - track._start;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2021-08-28 13:02:53 +08:00
|
|
|
|
for (it = track._attr.find("rtpmap"); it != track._attr.end() && it->first == "rtpmap";) {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
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};
|
2021-08-23 21:15:19 +08:00
|
|
|
|
|
|
|
|
|
sscanf(rtpmap.data(), "%d", &pt);
|
|
|
|
|
if (track._pt != pt) {
|
|
|
|
|
//pt不匹配
|
|
|
|
|
it = track._attr.erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-04-18 22:13:11 +08:00
|
|
|
|
if (4 == sscanf(rtpmap.data(), "%d %15[^/]/%d/%d", &pt, codec, &samplerate, &channel)) {
|
|
|
|
|
track._codec = codec;
|
|
|
|
|
track._samplerate = samplerate;
|
|
|
|
|
track._channel = channel;
|
2021-02-28 21:02:23 +08:00
|
|
|
|
} 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;
|
|
|
|
|
}
|
2021-02-28 21:02:23 +08:00
|
|
|
|
if (!track._samplerate && track._type == TrackVideo) {
|
|
|
|
|
//未设置视频采样率时,赋值为90000
|
|
|
|
|
track._samplerate = 90000;
|
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
++it;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2018-10-25 22:57:59 +08:00
|
|
|
|
|
2021-08-28 13:02:53 +08:00
|
|
|
|
for (it = track._attr.find("fmtp"); it != track._attr.end() && it->first == "fmtp"; ) {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
auto &fmtp = it->second;
|
|
|
|
|
int pt;
|
|
|
|
|
sscanf(fmtp.data(), "%d", &pt);
|
|
|
|
|
if (track._pt != pt) {
|
|
|
|
|
//pt不匹配
|
|
|
|
|
it = track._attr.erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
track._fmtp = FindField(fmtp.data(), " ", nullptr);
|
|
|
|
|
++it;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2018-10-25 18:50:18 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
it = track._attr.find("control");
|
2021-02-28 21:02:23 +08:00
|
|
|
|
if (it != track._attr.end()) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
track._control = it->second;
|
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
for (auto &track : _track_vec) {
|
|
|
|
|
if (track->_type == type) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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;
|
2021-08-23 21:15:19 +08:00
|
|
|
|
for (auto &track : _track_vec) {
|
|
|
|
|
if (track->_type == TrackAudio) {
|
|
|
|
|
if (!audio_added) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
ret.emplace_back(track);
|
|
|
|
|
audio_added = true;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-01-14 18:11:10 +08:00
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
if (track->_type == TrackVideo) {
|
|
|
|
|
if (!video_added) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
ret.emplace_back(track);
|
|
|
|
|
video_added = true;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-21 14:32:56 +08:00
|
|
|
|
return 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 {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
string title, audio, video;
|
|
|
|
|
for (auto &track : _track_vec) {
|
|
|
|
|
switch (track->_type) {
|
|
|
|
|
case TrackTitle: {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
title = track->toString();
|
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
case TrackVideo: {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
video = track->toString();
|
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
case TrackAudio: {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
audio = track->toString();
|
|
|
|
|
break;
|
2021-09-29 01:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
default: break;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return title + video + audio;
|
2019-08-15 19:13:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
template<int type>
|
|
|
|
|
class PortManager : public std::enable_shared_from_this<PortManager<type> > {
|
2022-03-13 20:48:01 +08:00
|
|
|
|
public:
|
|
|
|
|
PortManager() {
|
|
|
|
|
static auto func = [](const string &str, int index) {
|
|
|
|
|
uint16_t port[] = { 30000, 35000 };
|
|
|
|
|
sscanf(str.data(), "%" SCNu16 "-%" SCNu16, port, port + 1);
|
|
|
|
|
return port[index];
|
|
|
|
|
};
|
|
|
|
|
GET_CONFIG_FUNC(uint16_t, s_min_port, RtpProxy::kPortRange, [](const string &str) { return func(str, 0); });
|
|
|
|
|
GET_CONFIG_FUNC(uint16_t, s_max_port, RtpProxy::kPortRange, [](const string &str) { return func(str, 1); });
|
2022-03-26 10:15:18 +08:00
|
|
|
|
assert(s_max_port >= s_min_port + 36 -1);
|
2022-03-13 20:48:01 +08:00
|
|
|
|
setRange((s_min_port + 1) / 2, s_max_port / 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PortManager& Instance() {
|
|
|
|
|
static auto instance = std::make_shared<PortManager>();
|
|
|
|
|
return *instance;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip, bool re_use_port, bool is_udp) {
|
2022-03-13 20:48:01 +08:00
|
|
|
|
auto &sock0 = pair.first;
|
|
|
|
|
auto &sock1 = pair.second;
|
|
|
|
|
auto sock_pair = getPortPair();
|
|
|
|
|
if (!sock_pair) {
|
2022-05-14 23:25:22 +08:00
|
|
|
|
throw runtime_error("none reserved port in pool");
|
2022-03-13 20:48:01 +08:00
|
|
|
|
}
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (is_udp) {
|
|
|
|
|
if (!sock0->bindUdpSock(2 * *sock_pair, local_ip.data(), re_use_port)) {
|
|
|
|
|
//分配端口失败
|
|
|
|
|
throw runtime_error("open udp socket[0] failed");
|
|
|
|
|
}
|
2022-03-13 20:48:01 +08:00
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (!sock1->bindUdpSock(2 * *sock_pair + 1, local_ip.data(), re_use_port)) {
|
|
|
|
|
//分配端口失败
|
|
|
|
|
throw runtime_error("open udp socket[1] failed");
|
|
|
|
|
}
|
2020-09-12 19:03:52 +08:00
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
auto on_cycle = [sock_pair](Socket::Ptr &, std::shared_ptr<void> &) {};
|
|
|
|
|
// udp socket没onAccept事件,设置该回调,目的是为了在销毁socket时,回收对象
|
|
|
|
|
sock0->setOnAccept(on_cycle);
|
|
|
|
|
sock1->setOnAccept(on_cycle);
|
|
|
|
|
} else {
|
|
|
|
|
if (!sock0->listen(2 * *sock_pair, local_ip.data())) {
|
|
|
|
|
//分配端口失败
|
|
|
|
|
throw runtime_error("listen tcp socket[0] failed");
|
|
|
|
|
}
|
2022-03-13 20:48:01 +08:00
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (!sock1->listen(2 * *sock_pair + 1, local_ip.data())) {
|
|
|
|
|
//分配端口失败
|
|
|
|
|
throw runtime_error("listen tcp socket[1] failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto on_cycle = [sock_pair](const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len) {};
|
|
|
|
|
// udp socket没onAccept事件,设置该回调,目的是为了在销毁socket时,回收对象
|
|
|
|
|
sock0->setOnRead(on_cycle);
|
|
|
|
|
sock1->setOnRead(on_cycle);
|
|
|
|
|
}
|
2020-05-12 10:22:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 20:48:01 +08:00
|
|
|
|
private:
|
|
|
|
|
void setRange(uint16_t start_pos, uint16_t end_pos) {
|
|
|
|
|
lock_guard<recursive_mutex> lck(_pool_mtx);
|
|
|
|
|
while (start_pos < end_pos) {
|
|
|
|
|
_port_pair_pool.emplace_back(start_pos++);
|
|
|
|
|
}
|
2020-05-12 10:22:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 20:48:01 +08:00
|
|
|
|
std::shared_ptr<uint16_t> getPortPair() {
|
|
|
|
|
lock_guard<recursive_mutex> lck(_pool_mtx);
|
|
|
|
|
if (_port_pair_pool.empty()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto pos = _port_pair_pool.front();
|
|
|
|
|
_port_pair_pool.pop_front();
|
|
|
|
|
InfoL << "got port from pool:" << 2 * pos << "-" << 2 * pos + 1;
|
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
weak_ptr<PortManager> weak_self = this->shared_from_this();
|
2022-03-13 20:48:01 +08:00
|
|
|
|
std::shared_ptr<uint16_t> ret(new uint16_t(pos), [weak_self, pos](uint16_t *ptr) {
|
|
|
|
|
delete ptr;
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
InfoL << "return port to pool:" << 2 * pos << "-" << 2 * pos + 1;
|
|
|
|
|
//回收端口号
|
|
|
|
|
lock_guard<recursive_mutex> lck(strong_self->_pool_mtx);
|
|
|
|
|
strong_self->_port_pair_pool.emplace_back(pos);
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
2020-05-12 10:22:21 +08:00
|
|
|
|
}
|
2022-03-13 20:48:01 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
recursive_mutex _pool_mtx;
|
|
|
|
|
deque<uint16_t> _port_pair_pool;
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip, bool re_use_port, bool is_udp) {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
int try_count = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
2022-05-14 23:25:22 +08:00
|
|
|
|
//udp和tcp端口池使用相同算法和范围分配,但是互不相干
|
|
|
|
|
if (is_udp) {
|
|
|
|
|
PortManager<0>::Instance().makeSockPair(pair, local_ip, re_use_port, is_udp);
|
|
|
|
|
} else {
|
|
|
|
|
PortManager<1>::Instance().makeSockPair(pair, local_ip, re_use_port, is_udp);
|
|
|
|
|
}
|
2021-08-23 21:15:19 +08:00
|
|
|
|
break;
|
2022-03-13 20:48:01 +08:00
|
|
|
|
} catch (exception &ex) {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
if (++try_count == 3) {
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2022-05-14 23:25:22 +08:00
|
|
|
|
WarnL << "open socket failed:" << ex.what() << ", retry: " << try_count;
|
2021-08-23 21:15:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-12 10:22:21 +08:00
|
|
|
|
}
|
2018-10-25 22:57:59 +08:00
|
|
|
|
|
2020-07-07 09:58:08 +08:00
|
|
|
|
string printSSRC(uint32_t ui32Ssrc) {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
char tmp[9] = {0};
|
2020-07-07 09:58:08 +08:00
|
|
|
|
ui32Ssrc = htonl(ui32Ssrc);
|
|
|
|
|
uint8_t *pSsrc = (uint8_t *) &ui32Ssrc;
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
sprintf(tmp + 2 * i, "%02X", pSsrc[i]);
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved) {
|
2021-02-07 14:55:09 +08:00
|
|
|
|
auto rtp_tcp = BufferRaw::create();
|
|
|
|
|
rtp_tcp->setCapacity(RtpPacket::kRtpTcpHeaderSize);
|
2021-01-31 19:19:24 +08:00
|
|
|
|
rtp_tcp->setSize(RtpPacket::kRtpTcpHeaderSize);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
auto ptr = rtp_tcp->data();
|
|
|
|
|
ptr[0] = '$';
|
|
|
|
|
ptr[1] = interleaved;
|
|
|
|
|
ptr[2] = (size >> 8) & 0xFF;
|
|
|
|
|
ptr[3] = size & 0xFF;
|
|
|
|
|
return rtp_tcp;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
#define AV_RB16(x) \
|
|
|
|
|
((((const uint8_t*)(x))[0] << 8) | \
|
|
|
|
|
((const uint8_t*)(x))[1])
|
|
|
|
|
|
|
|
|
|
size_t RtpHeader::getCsrcSize() const {
|
|
|
|
|
//每个csrc占用4字节
|
|
|
|
|
return csrc << 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t *RtpHeader::getCsrcData() {
|
|
|
|
|
if (!csrc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return &payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t RtpHeader::getExtSize() const {
|
|
|
|
|
//rtp有ext
|
|
|
|
|
if (!ext) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
auto ext_ptr = &payload + getCsrcSize();
|
2021-05-06 12:02:16 +08:00
|
|
|
|
//uint16_t reserved = AV_RB16(ext_ptr);
|
2021-01-31 19:19:24 +08:00
|
|
|
|
//每个ext占用4字节
|
|
|
|
|
return AV_RB16(ext_ptr + 2) << 2;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
uint16_t RtpHeader::getExtReserved() const {
|
2021-05-06 12:02:16 +08:00
|
|
|
|
//rtp有ext
|
|
|
|
|
if (!ext) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
auto ext_ptr = &payload + getCsrcSize();
|
|
|
|
|
return AV_RB16(ext_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
uint8_t *RtpHeader::getExtData() {
|
|
|
|
|
if (!ext) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto ext_ptr = &payload + getCsrcSize();
|
|
|
|
|
//多出的4个字节分别为reserved、ext_len
|
2021-05-06 12:02:16 +08:00
|
|
|
|
return ext_ptr + 4;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t RtpHeader::getPayloadOffset() const {
|
|
|
|
|
//有ext时,还需要忽略reserved、ext_len 4个字节
|
|
|
|
|
return getCsrcSize() + (ext ? (4 + getExtSize()) : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t *RtpHeader::getPayloadData() {
|
|
|
|
|
return &payload + getPayloadOffset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t RtpHeader::getPaddingSize(size_t rtp_size) const {
|
|
|
|
|
if (!padding) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-02-02 12:00:03 +08:00
|
|
|
|
auto end = (uint8_t *) this + rtp_size - 1;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
return *end;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 10:34:54 +08:00
|
|
|
|
ssize_t RtpHeader::getPayloadSize(size_t rtp_size) const {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto invalid_size = getPayloadOffset() + getPaddingSize(rtp_size);
|
2022-05-28 10:34:54 +08:00
|
|
|
|
return (ssize_t)rtp_size - invalid_size - RtpPacket::kRtpHeaderSize;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
string RtpHeader::dumpString(size_t rtp_size) const {
|
2021-02-02 11:54:49 +08:00
|
|
|
|
_StrPrinter printer;
|
2021-08-23 21:15:19 +08:00
|
|
|
|
printer << "version:" << (int) version << "\r\n";
|
2021-02-02 11:54:49 +08:00
|
|
|
|
printer << "padding:" << getPaddingSize(rtp_size) << "\r\n";
|
|
|
|
|
printer << "ext:" << getExtSize() << "\r\n";
|
|
|
|
|
printer << "csrc:" << getCsrcSize() << "\r\n";
|
2021-08-23 21:15:19 +08:00
|
|
|
|
printer << "mark:" << (int) mark << "\r\n";
|
|
|
|
|
printer << "pt:" << (int) pt << "\r\n";
|
2021-02-02 11:54:49 +08:00
|
|
|
|
printer << "seq:" << ntohs(seq) << "\r\n";
|
|
|
|
|
printer << "stamp:" << ntohl(stamp) << "\r\n";
|
|
|
|
|
printer << "ssrc:" << ntohl(ssrc) << "\r\n";
|
|
|
|
|
printer << "rtp size:" << rtp_size << "\r\n";
|
|
|
|
|
printer << "payload offset:" << getPayloadOffset() << "\r\n";
|
|
|
|
|
printer << "payload size:" << getPayloadSize(rtp_size) << "\r\n";
|
|
|
|
|
return std::move(printer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
RtpHeader *RtpPacket::getHeader() {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
//需除去rtcp over tcp 4个字节长度
|
2021-07-12 21:18:22 +08:00
|
|
|
|
return (RtpHeader *) (data() + RtpPacket::kRtpTcpHeaderSize);
|
2021-01-31 19:19:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
const RtpHeader *RtpPacket::getHeader() const {
|
|
|
|
|
return (RtpHeader *) (data() + RtpPacket::kRtpTcpHeaderSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string RtpPacket::dumpString() const {
|
2021-04-11 01:15:02 +08:00
|
|
|
|
return ((RtpPacket *) this)->getHeader()->dumpString(size() - RtpPacket::kRtpTcpHeaderSize);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
uint16_t RtpPacket::getSeq() const {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
return ntohs(getHeader()->seq);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
uint32_t RtpPacket::getStamp() const {
|
|
|
|
|
return ntohl(getHeader()->stamp);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 01:02:54 +08:00
|
|
|
|
uint32_t RtpPacket::getStampMS(bool ntp) const {
|
|
|
|
|
return ntp ? ntp_stamp & 0xFFFFFFFF : getStamp() * uint64_t(1000) / sample_rate;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
uint32_t RtpPacket::getSSRC() const {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
return ntohl(getHeader()->ssrc);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
uint8_t *RtpPacket::getPayload() {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
return getHeader()->getPayloadData();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
size_t RtpPacket::getPayloadSize() const {
|
2021-01-31 19:19:24 +08:00
|
|
|
|
//需除去rtcp over tcp 4个字节长度
|
|
|
|
|
return getHeader()->getPayloadSize(size() - kRtpTcpHeaderSize);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:15:19 +08:00
|
|
|
|
RtpPacket::Ptr RtpPacket::create() {
|
2021-02-05 11:28:50 +08:00
|
|
|
|
#if 0
|
|
|
|
|
static ResourcePool<RtpPacket> packet_pool;
|
|
|
|
|
static onceToken token([]() {
|
|
|
|
|
packet_pool.setSize(1024);
|
|
|
|
|
});
|
2022-01-06 14:30:44 +08:00
|
|
|
|
auto ret = packet_pool.obtain2();
|
2021-02-05 11:28:50 +08:00
|
|
|
|
ret->setSize(0);
|
|
|
|
|
return ret;
|
|
|
|
|
#else
|
|
|
|
|
return Ptr(new RtpPacket);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 16:49:11 +08:00
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
namespace toolkit {
|
2021-08-23 21:15:19 +08:00
|
|
|
|
StatisticImp(mediakit::RtpPacket);
|
2021-02-05 16:49:11 +08:00
|
|
|
|
}
|