2020-04-04 20:30:09 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/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.
|
|
|
|
|
*/
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
#include "Util/MD5.h"
|
|
|
|
|
#include "Util/base64.h"
|
|
|
|
|
#include "RtspPusher.h"
|
|
|
|
|
#include "RtspSession.h"
|
|
|
|
|
|
2019-03-27 18:56:49 +08:00
|
|
|
|
using namespace mediakit::Client;
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
RtspPusher::RtspPusher(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src) : TcpClient(poller) {
|
|
|
|
|
_push_src = src;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RtspPusher::~RtspPusher() {
|
|
|
|
|
teardown();
|
|
|
|
|
DebugL << endl;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 10:28:06 +08:00
|
|
|
|
void RtspPusher::sendTeardown(){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if (alive()) {
|
2021-01-17 10:28:06 +08:00
|
|
|
|
if (!_content_base.empty()) {
|
|
|
|
|
sendRtspRequest("TEARDOWN", _content_base);
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
shutdown(SockException(Err_shutdown, "teardown"));
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2021-01-17 10:28:06 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2021-01-17 10:28:06 +08:00
|
|
|
|
void RtspPusher::teardown() {
|
|
|
|
|
sendTeardown();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
reset();
|
2021-01-31 19:18:17 +08:00
|
|
|
|
CLEAR_ARR(_rtp_sock);
|
|
|
|
|
CLEAR_ARR(_rtcp_sock);
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_nonce.clear();
|
|
|
|
|
_realm.clear();
|
|
|
|
|
_track_vec.clear();
|
|
|
|
|
_session_id.clear();
|
|
|
|
|
_content_base.clear();
|
|
|
|
|
_session_id.clear();
|
|
|
|
|
_cseq = 1;
|
|
|
|
|
_publish_timer.reset();
|
|
|
|
|
_beat_timer.reset();
|
|
|
|
|
_rtsp_reader.reset();
|
|
|
|
|
_track_vec.clear();
|
|
|
|
|
_on_res_func = nullptr;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void RtspPusher::publish(const string &url_str) {
|
2020-01-04 12:03:53 +08:00
|
|
|
|
RtspUrl url;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!url.parse(url_str)) {
|
|
|
|
|
onPublishResult(SockException(Err_other, StrPrinter << "illegal rtsp url:" << url_str), false);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-28 09:43:47 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
teardown();
|
|
|
|
|
|
2020-01-04 12:03:53 +08:00
|
|
|
|
if (url._user.size()) {
|
|
|
|
|
(*this)[kRtspUser] = url._user;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-01-04 12:03:53 +08:00
|
|
|
|
if (url._passwd.size()) {
|
|
|
|
|
(*this)[kRtspPwd] = url._passwd;
|
2019-03-27 18:56:49 +08:00
|
|
|
|
(*this)[kRtspPwdIsMD5] = false;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_url = url_str;
|
|
|
|
|
_rtp_type = (Rtsp::eRtpType) (int) (*this)[kRtpType];
|
|
|
|
|
DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " "
|
|
|
|
|
<< (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
weak_ptr<RtspPusher> weak_self = dynamic_pointer_cast<RtspPusher>(shared_from_this());
|
2021-01-17 18:31:50 +08:00
|
|
|
|
float publish_timeout_sec = (*this)[kTimeoutMS].as<int>() / 1000.0f;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_publish_timer.reset(new Timer(publish_timeout_sec, [weak_self]() {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
strong_self->onPublishResult(SockException(Err_timeout, "publish rtsp timeout"), false);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return false;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
}, getPoller()));
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!(*this)[kNetAdapter].empty()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
setNetAdapter((*this)[kNetAdapter]);
|
|
|
|
|
}
|
2020-01-04 12:03:53 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
startConnect(url._host, url._port, publish_timeout_sec);
|
2020-01-04 12:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void RtspPusher::onPublishResult(const SockException &ex, bool handshake_done) {
|
|
|
|
|
if (ex.getErrCode() == Err_shutdown) {
|
|
|
|
|
//主动shutdown的,不触发回调
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!handshake_done) {
|
2020-01-04 12:03:53 +08:00
|
|
|
|
//播放结果回调
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_publish_timer.reset();
|
|
|
|
|
if (_on_published) {
|
|
|
|
|
_on_published(ex);
|
2020-01-04 12:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//播放成功后异常断开回调
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (_on_shutdown) {
|
|
|
|
|
_on_shutdown(ex);
|
2020-01-04 12:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (ex) {
|
2021-01-17 10:28:06 +08:00
|
|
|
|
sendTeardown();
|
2020-01-04 12:03:53 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspPusher::onErr(const SockException &ex) {
|
2020-01-04 12:03:53 +08:00
|
|
|
|
//定时器_pPublishTimer为空后表明握手结束了
|
2020-08-30 11:40:03 +08:00
|
|
|
|
onPublishResult(ex, !_publish_timer);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspPusher::onConnect(const SockException &err) {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
onPublishResult(err, false);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendAnnounce();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void RtspPusher::onRecv(const Buffer::Ptr &buf){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
try {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
input(buf->data(), buf->size());
|
2019-03-27 18:41:52 +08:00
|
|
|
|
} catch (exception &e) {
|
|
|
|
|
SockException ex(Err_other, e.what());
|
2020-01-04 12:03:53 +08:00
|
|
|
|
//定时器_pPublishTimer为空后表明握手结束了
|
2020-08-30 11:40:03 +08:00
|
|
|
|
onPublishResult(ex, !_publish_timer);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspPusher::onWholeRtspPacket(Parser &parser) {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
decltype(_on_res_func) func;
|
|
|
|
|
_on_res_func.swap(func);
|
|
|
|
|
if (func) {
|
|
|
|
|
func(parser);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
parser.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
void RtspPusher::onRtpPacket(const char *data, size_t len) {
|
|
|
|
|
int trackIdx = -1;
|
|
|
|
|
uint8_t interleaved = data[1];
|
|
|
|
|
if (interleaved % 2 != 0) {
|
|
|
|
|
trackIdx = getTrackIndexByInterleaved(interleaved - 1);
|
2021-01-31 19:19:24 +08:00
|
|
|
|
onRtcpPacket(trackIdx, _track_vec[trackIdx], (uint8_t *) data + RtpPacket::kRtpTcpHeaderSize, len - RtpPacket::kRtpTcpHeaderSize);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspPusher::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len){
|
|
|
|
|
auto rtcp_arr = RtcpHeader::loadFromBytes((char *) data, len);
|
|
|
|
|
for (auto &rtcp : rtcp_arr) {
|
|
|
|
|
_rtcp_context[track_idx]->onRtcp(rtcp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void RtspPusher::sendAnnounce() {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
auto src = _push_src.lock();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if (!src) {
|
|
|
|
|
throw std::runtime_error("the media source was released");
|
|
|
|
|
}
|
|
|
|
|
//解析sdp
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_sdp_parser.load(src->getSdp());
|
|
|
|
|
_track_vec = _sdp_parser.getAvailableTrack();
|
|
|
|
|
if (_track_vec.empty()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
throw std::runtime_error("无有效的Sdp Track");
|
|
|
|
|
}
|
2021-01-31 19:18:17 +08:00
|
|
|
|
for (auto &track : _track_vec) {
|
|
|
|
|
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(track->_samplerate));
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_on_res_func = std::bind(&RtspPusher::handleResAnnounce, this, placeholders::_1);
|
|
|
|
|
sendRtspRequest("ANNOUNCE", _url, {}, src->getSdp());
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspPusher::handleResAnnounce(const Parser &parser) {
|
|
|
|
|
string authInfo = parser["WWW-Authenticate"];
|
|
|
|
|
//发送DESCRIBE命令后的回复
|
|
|
|
|
if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) {
|
|
|
|
|
sendAnnounce();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (parser.Url() == "302") {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto newUrl = parser["Location"];
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (newUrl.empty()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
throw std::runtime_error("未找到Location字段(跳转url)");
|
|
|
|
|
}
|
|
|
|
|
publish(newUrl);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (parser.Url() != "200") {
|
|
|
|
|
throw std::runtime_error(StrPrinter << "ANNOUNCE:" << parser.Url() << " " << parser.Tail());
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_content_base = parser["Content-Base"];
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (_content_base.empty()) {
|
|
|
|
|
_content_base = _url;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (_content_base.back() == '/') {
|
|
|
|
|
_content_base.pop_back();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendSetup(0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
bool RtspPusher::handleAuthenticationFailure(const string ¶ms_str) {
|
|
|
|
|
if (!_realm.empty()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//已经认证过了
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
char *realm = new char[params_str.size()];
|
|
|
|
|
char *nonce = new char[params_str.size()];
|
|
|
|
|
char *stale = new char[params_str.size()];
|
|
|
|
|
onceToken token(nullptr, [&]() {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
delete[] realm;
|
|
|
|
|
delete[] nonce;
|
|
|
|
|
delete[] stale;
|
|
|
|
|
});
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (sscanf(params_str.data(), "Digest realm=\"%[^\"]\", nonce=\"%[^\"]\", stale=%[a-zA-Z]", realm, nonce, stale) == 3) {
|
|
|
|
|
_realm = (const char *) realm;
|
|
|
|
|
_nonce = (const char *) nonce;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (sscanf(params_str.data(), "Digest realm=\"%[^\"]\", nonce=\"%[^\"]\"", realm, nonce) == 2) {
|
|
|
|
|
_realm = (const char *) realm;
|
|
|
|
|
_nonce = (const char *) nonce;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (sscanf(params_str.data(), "Basic realm=\"%[^\"]\"", realm) == 1) {
|
|
|
|
|
_realm = (const char *) realm;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 12:15:13 +08:00
|
|
|
|
//有必要的情况下创建udp端口
|
2019-07-11 12:12:33 +08:00
|
|
|
|
void RtspPusher::createUdpSockIfNecessary(int track_idx){
|
2021-01-31 19:18:17 +08:00
|
|
|
|
auto &rtpSockRef = _rtp_sock[track_idx];
|
|
|
|
|
auto &rtcpSockRef = _rtcp_sock[track_idx];
|
|
|
|
|
if (!rtpSockRef || !rtcpSockRef) {
|
|
|
|
|
std::pair<Socket::Ptr, Socket::Ptr> pr = std::make_pair(createSocket(), createSocket());
|
|
|
|
|
makeSockPair(pr, get_local_ip());
|
|
|
|
|
rtpSockRef = pr.first;
|
|
|
|
|
rtcpSockRef = pr.second;
|
2019-07-11 12:12:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void RtspPusher::sendSetup(unsigned int track_idx) {
|
|
|
|
|
_on_res_func = std::bind(&RtspPusher::handleResSetup, this, placeholders::_1, track_idx);
|
|
|
|
|
auto &track = _track_vec[track_idx];
|
|
|
|
|
auto base_url = _content_base + "/" + track->_control_surffix;
|
2020-11-07 17:45:58 +08:00
|
|
|
|
if (track->_control.find("://") != string::npos) {
|
|
|
|
|
base_url = track->_control;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
switch (_rtp_type) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_TCP: {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
sendRtspRequest("SETUP", base_url, {"Transport",
|
|
|
|
|
StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2
|
|
|
|
|
<< "-" << track->_type * 2 + 1});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2019-03-28 09:34:22 +08:00
|
|
|
|
break;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_UDP: {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
createUdpSockIfNecessary(track_idx);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
int port = _rtp_sock[track_idx]->get_local_port();
|
2020-08-30 11:40:03 +08:00
|
|
|
|
sendRtspRequest("SETUP", base_url,
|
|
|
|
|
{"Transport", StrPrinter << "RTP/AVP;unicast;client_port=" << port << "-" << port + 1});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2019-03-28 09:34:22 +08:00
|
|
|
|
break;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
default:
|
2019-03-28 09:34:22 +08:00
|
|
|
|
break;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if (parser.Url() != "200") {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
throw std::runtime_error(StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (track_idx == 0) {
|
|
|
|
|
_session_id = parser["Session"];
|
|
|
|
|
_session_id.append(";");
|
|
|
|
|
_session_id = FindField(_session_id.data(), nullptr, ";");
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
auto transport = parser["Transport"];
|
|
|
|
|
if (transport.find("TCP") != string::npos || transport.find("interleaved") != string::npos) {
|
|
|
|
|
_rtp_type = Rtsp::RTP_TCP;
|
|
|
|
|
string interleaved = FindField(FindField((transport + ";").data(), "interleaved=", ";").data(), NULL, "-");
|
|
|
|
|
_track_vec[track_idx]->_interleaved = atoi(interleaved.data());
|
|
|
|
|
} else if (transport.find("multicast") != string::npos) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
throw std::runtime_error("SETUP rtsp pusher can not support multicast!");
|
2020-08-30 11:40:03 +08:00
|
|
|
|
} else {
|
|
|
|
|
_rtp_type = Rtsp::RTP_UDP;
|
|
|
|
|
createUdpSockIfNecessary(track_idx);
|
|
|
|
|
const char *strPos = "server_port=";
|
|
|
|
|
auto port_str = FindField((transport + ";").data(), strPos, ";");
|
2021-01-31 19:18:17 +08:00
|
|
|
|
uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data());
|
|
|
|
|
uint16_t rtcp_port = atoi(FindField(port_str.data(), "-", NULL).data());
|
|
|
|
|
auto &rtp_sock = _rtp_sock[track_idx];
|
|
|
|
|
auto &rtcp_sock = _rtcp_sock[track_idx];
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
struct sockaddr_in rtpto;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
//设置rtp发送目标,为后续发送rtp做准备
|
|
|
|
|
rtpto.sin_port = ntohs(rtp_port);
|
|
|
|
|
rtpto.sin_family = AF_INET;
|
|
|
|
|
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
|
|
|
|
|
rtp_sock->setSendPeerAddr((struct sockaddr *) &(rtpto));
|
|
|
|
|
|
|
|
|
|
//设置rtcp发送目标,为后续发送rtcp做准备
|
|
|
|
|
rtpto.sin_port = ntohs(rtcp_port);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
rtpto.sin_family = AF_INET;
|
|
|
|
|
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
|
2021-01-31 19:18:17 +08:00
|
|
|
|
rtcp_sock->setSendPeerAddr((struct sockaddr *)&(rtpto));
|
|
|
|
|
|
|
|
|
|
auto srcIP = inet_addr(get_peer_ip().data());
|
|
|
|
|
weak_ptr<RtspPusher> weakSelf = dynamic_pointer_cast<RtspPusher>(shared_from_this());
|
|
|
|
|
if(rtcp_sock) {
|
|
|
|
|
//设置rtcp over udp接收回调处理函数
|
|
|
|
|
rtcp_sock->setOnRead([srcIP, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (((struct sockaddr_in *) addr)->sin_addr.s_addr != srcIP) {
|
|
|
|
|
WarnL << "收到其他地址的rtcp数据:" << SockUtil::inet_ntoa(((struct sockaddr_in *) addr)->sin_addr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strongSelf->onRtcpPacket(track_idx, strongSelf->_track_vec[track_idx], (uint8_t *) buf->data(), buf->size());
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
RtspSplitter::enableRecvRtp(_rtp_type == Rtsp::RTP_TCP);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (track_idx < _track_vec.size() - 1) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//需要继续发送SETUP命令
|
2020-08-30 11:40:03 +08:00
|
|
|
|
sendSetup(track_idx + 1);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendRecord();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void RtspPusher::sendOptions() {
|
2021-01-17 20:15:08 +08:00
|
|
|
|
_on_res_func = [](const Parser &parser) {};
|
2020-08-30 11:40:03 +08:00
|
|
|
|
sendRtspRequest("OPTIONS", _content_base);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
void RtspPusher::updateRtcpContext(const RtpPacket::Ptr &rtp){
|
|
|
|
|
int track_index = getTrackIndexByTrackType(rtp->type);
|
|
|
|
|
auto &ticker = _rtcp_send_ticker[track_index];
|
|
|
|
|
auto &rtcp_ctx = _rtcp_context[track_index];
|
2021-01-31 19:19:24 +08:00
|
|
|
|
rtcp_ctx->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
//send rtcp every 5 second
|
|
|
|
|
if (ticker.elapsedTime() > 5 * 1000) {
|
|
|
|
|
ticker.resetTime();
|
|
|
|
|
static auto send_rtcp = [](RtspPusher *thiz, int index, Buffer::Ptr ptr) {
|
|
|
|
|
if (thiz->_rtp_type == Rtsp::RTP_TCP) {
|
|
|
|
|
auto &track = thiz->_track_vec[index];
|
|
|
|
|
thiz->send(makeRtpOverTcpPrefix((uint16_t) (ptr->size()), track->_interleaved + 1));
|
|
|
|
|
thiz->send(std::move(ptr));
|
|
|
|
|
} else {
|
|
|
|
|
thiz->_rtcp_sock[index]->send(std::move(ptr));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto ssrc = rtp->getSSRC();
|
|
|
|
|
auto rtcp = rtcp_ctx->createRtcpSR(ssrc + 1);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
auto rtcp_sdes = RtcpSdes::create({SERVER_NAME});
|
|
|
|
|
rtcp_sdes->items.type = (uint8_t) SdesType::RTCP_SDES_CNAME;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
rtcp_sdes->items.ssrc = htonl(ssrc);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
send_rtcp(this, track_index, std::move(rtcp));
|
|
|
|
|
send_rtcp(this, track_index, RtcpHeader::toBuffer(rtcp_sdes));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspPusher::sendRtpPacket(const RtspMediaSource::RingDataType &pkt) {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
switch (_rtp_type) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_TCP: {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t i = 0;
|
|
|
|
|
auto size = pkt->size();
|
2020-04-07 13:03:53 +08:00
|
|
|
|
setSendFlushFlag(false);
|
|
|
|
|
pkt->for_each([&](const RtpPacket::Ptr &rtp) {
|
2021-01-31 19:18:17 +08:00
|
|
|
|
updateRtcpContext(rtp);
|
2020-04-07 13:03:53 +08:00
|
|
|
|
if (++i == size) {
|
|
|
|
|
setSendFlushFlag(true);
|
|
|
|
|
}
|
|
|
|
|
BufferRtp::Ptr buffer(new BufferRtp(rtp));
|
2020-11-01 03:41:35 +08:00
|
|
|
|
send(std::move(buffer));
|
2020-04-07 13:03:53 +08:00
|
|
|
|
});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
break;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_UDP: {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t i = 0;
|
|
|
|
|
auto size = pkt->size();
|
2020-04-07 13:03:53 +08:00
|
|
|
|
pkt->for_each([&](const RtpPacket::Ptr &rtp) {
|
2021-01-31 19:18:17 +08:00
|
|
|
|
updateRtcpContext(rtp);
|
2020-04-07 13:03:53 +08:00
|
|
|
|
int iTrackIndex = getTrackIndexByTrackType(rtp->type);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
auto &pSock = _rtp_sock[iTrackIndex];
|
2020-04-07 13:03:53 +08:00
|
|
|
|
if (!pSock) {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
shutdown(SockException(Err_shutdown, "udp sock not opened yet"));
|
2020-04-07 13:03:53 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
BufferRtp::Ptr buffer(new BufferRtp(rtp, 4));
|
2020-11-01 03:41:35 +08:00
|
|
|
|
pSock->send(std::move(buffer), nullptr, 0, ++i == size);
|
2020-04-07 13:03:53 +08:00
|
|
|
|
});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
break;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
}
|
|
|
|
|
default : break;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
int RtspPusher::getTrackIndexByInterleaved(int interleaved) const {
|
|
|
|
|
for (int i = 0; i < (int)_track_vec.size(); ++i) {
|
|
|
|
|
if (_track_vec[i]->_interleaved == interleaved) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_track_vec.size() == 1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
throw SockException(Err_shutdown, StrPrinter << "no such track with interleaved:" << interleaved);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RtspPusher::getTrackIndexByTrackType(TrackType type) const{
|
|
|
|
|
for (int i = 0; i < (int)_track_vec.size(); ++i) {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (type == _track_vec[i]->_type) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (_track_vec.size() == 1) {
|
2019-11-25 17:59:04 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-08 22:29:08 +08:00
|
|
|
|
throw SockException(Err_shutdown, StrPrinter << "no such track with type:" << (int) type);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void RtspPusher::sendRecord() {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_on_res_func = [this](const Parser &parser) {
|
|
|
|
|
auto src = _push_src.lock();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if (!src) {
|
|
|
|
|
throw std::runtime_error("the media source was released");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_rtsp_reader = src->getRing()->attach(getPoller());
|
|
|
|
|
weak_ptr<RtspPusher> weak_self = dynamic_pointer_cast<RtspPusher>(shared_from_this());
|
|
|
|
|
_rtsp_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt) {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
strong_self->sendRtpPacket(pkt);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
});
|
2020-08-30 11:40:03 +08:00
|
|
|
|
_rtsp_reader->setDetachCB([weak_self]() {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (strong_self) {
|
|
|
|
|
strong_self->onPublishResult(SockException(Err_other, "媒体源被释放"), !strong_self->_publish_timer);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (_rtp_type != Rtsp::RTP_TCP) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
/////////////////////////心跳/////////////////////////////////
|
2021-01-17 18:31:50 +08:00
|
|
|
|
_beat_timer.reset(new Timer((*this)[kBeatIntervalMS].as<int>() / 1000.0f, [weak_self]() {
|
2020-08-30 11:40:03 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
strong_self->sendOptions();
|
2019-03-28 09:34:22 +08:00
|
|
|
|
return true;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
}, getPoller()));
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
onPublishResult(SockException(Err_success, "success"), false);
|
2019-09-04 18:57:54 +08:00
|
|
|
|
//提升发送性能
|
|
|
|
|
setSocketFlags();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
};
|
2020-08-30 11:40:03 +08:00
|
|
|
|
sendRtspRequest("RECORD", _content_base, {"Range", "npt=0.000-"});
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 18:57:54 +08:00
|
|
|
|
void RtspPusher::setSocketFlags(){
|
2020-08-30 11:40:03 +08:00
|
|
|
|
GET_CONFIG(int, merge_write_ms, General::kMergeWriteMS);
|
|
|
|
|
if (merge_write_ms > 0) {
|
2019-09-04 18:57:54 +08:00
|
|
|
|
//提高发送性能
|
2020-04-23 17:50:12 +08:00
|
|
|
|
setSendFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE);
|
2020-09-12 19:03:52 +08:00
|
|
|
|
SockUtil::setNoDelay(getSock()->rawFD(), false);
|
2019-09-04 18:57:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void RtspPusher::sendRtspRequest(const string &cmd, const string &url, const std::initializer_list<string> &header,const string &sdp ) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
string key;
|
|
|
|
|
StrCaseMap header_map;
|
|
|
|
|
int i = 0;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
for (auto &val : header) {
|
|
|
|
|
if (++i % 2 == 0) {
|
|
|
|
|
header_map.emplace(key, val);
|
|
|
|
|
} else {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
key = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
sendRtspRequest(cmd, url, header_map, sdp);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2019-03-28 09:34:22 +08:00
|
|
|
|
void RtspPusher::sendRtspRequest(const string &cmd, const string &url,const StrCaseMap &header_const,const string &sdp ) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto header = header_const;
|
2020-08-30 11:40:03 +08:00
|
|
|
|
header.emplace("CSeq", StrPrinter << _cseq++);
|
|
|
|
|
header.emplace("User-Agent", SERVER_NAME);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!_session_id.empty()) {
|
|
|
|
|
header.emplace("Session", _session_id);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!_realm.empty() && !(*this)[kRtspUser].empty()) {
|
|
|
|
|
if (!_nonce.empty()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//MD5认证
|
|
|
|
|
/*
|
|
|
|
|
response计算方法如下:
|
|
|
|
|
RTSP客户端应该使用username + password并计算response如下:
|
|
|
|
|
(1)当password为MD5编码,则
|
|
|
|
|
response = md5( password:nonce:md5(public_method:url) );
|
|
|
|
|
(2)当password为ANSI字符串,则
|
|
|
|
|
response= md5( md5(username:realm:password):nonce:md5(public_method:url) );
|
|
|
|
|
*/
|
|
|
|
|
string encrypted_pwd = (*this)[kRtspPwd];
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!(*this)[kRtspPwdIsMD5].as<bool>()) {
|
|
|
|
|
encrypted_pwd = MD5((*this)[kRtspUser] + ":" + _realm + ":" + encrypted_pwd).hexdigest();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-08-30 11:40:03 +08:00
|
|
|
|
auto response = MD5(encrypted_pwd + ":" + _nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_StrPrinter printer;
|
|
|
|
|
printer << "Digest ";
|
|
|
|
|
printer << "username=\"" << (*this)[kRtspUser] << "\", ";
|
2020-08-30 11:40:03 +08:00
|
|
|
|
printer << "realm=\"" << _realm << "\", ";
|
|
|
|
|
printer << "nonce=\"" << _nonce << "\", ";
|
2019-03-27 18:41:52 +08:00
|
|
|
|
printer << "uri=\"" << url << "\", ";
|
|
|
|
|
printer << "response=\"" << response << "\"";
|
2020-08-30 11:40:03 +08:00
|
|
|
|
header.emplace("Authorization", printer);
|
|
|
|
|
} else if (!(*this)[kRtspPwdIsMD5].as<bool>()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
//base64认证
|
|
|
|
|
string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
|
|
|
|
|
char authStrBase64[1024] = {0};
|
2021-01-17 18:31:50 +08:00
|
|
|
|
av_base64_encode(authStrBase64, sizeof(authStrBase64), (uint8_t *) authStr.data(), (int)authStr.size());
|
2020-08-30 11:40:03 +08:00
|
|
|
|
header.emplace("Authorization", StrPrinter << "Basic " << authStrBase64);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!sdp.empty()) {
|
|
|
|
|
header.emplace("Content-Length", StrPrinter << sdp.size());
|
|
|
|
|
header.emplace("Content-Type", "application/sdp");
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_StrPrinter printer;
|
|
|
|
|
printer << cmd << " " << url << " RTSP/1.0\r\n";
|
2020-08-30 11:40:03 +08:00
|
|
|
|
for (auto &pr : header) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
printer << pr.first << ": " << pr.second << "\r\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printer << "\r\n";
|
|
|
|
|
|
2020-08-30 11:40:03 +08:00
|
|
|
|
if (!sdp.empty()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
printer << sdp;
|
|
|
|
|
}
|
2020-11-01 03:41:35 +08:00
|
|
|
|
SockSender::send(std::move(printer));
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} /* namespace mediakit */
|