2020-09-06 18:09:31 +08:00
|
|
|
|
/*
|
2020-09-06 17:56:05 +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-09-06 17:56:05 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-09-12 19:46:58 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-10-24 23:33:13 +08:00
|
|
|
|
#include "RtpSender.h"
|
2020-09-06 17:56:05 +08:00
|
|
|
|
#include "Rtsp/RtspSession.h"
|
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
2022-05-14 23:25:22 +08:00
|
|
|
|
#include "Util/uv_errno.h"
|
2020-10-24 23:33:13 +08:00
|
|
|
|
#include "RtpCache.h"
|
2020-09-06 17:56:05 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2020-09-06 17:56:05 +08:00
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
RtpSender::RtpSender() {
|
|
|
|
|
_poller = EventPollerPool::Instance().getPoller();
|
|
|
|
|
_socket = Socket::createSocket(_poller, false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 18:25:36 +08:00
|
|
|
|
void RtpSender::startSend(const MediaSourceEvent::SendRtpArgs &args, const function<void(uint16_t local_port, const SockException &ex)> &cb){
|
|
|
|
|
_args = args;
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (!_interface) {
|
|
|
|
|
//重连时不重新创建对象
|
|
|
|
|
auto lam = [this](std::shared_ptr<List<Buffer::Ptr>> list) { onFlushRtpList(std::move(list)); };
|
|
|
|
|
if (args.use_ps) {
|
|
|
|
|
_interface = std::make_shared<RtpCachePS>(lam, atoi(args.ssrc.data()), args.pt);
|
|
|
|
|
} else {
|
|
|
|
|
_interface = std::make_shared<RtpCacheRaw>(lam, atoi(args.ssrc.data()), args.pt, args.only_audio);
|
|
|
|
|
}
|
2022-04-01 18:28:09 +08:00
|
|
|
|
}
|
2022-05-14 23:25:22 +08:00
|
|
|
|
|
2020-10-24 23:33:13 +08:00
|
|
|
|
weak_ptr<RtpSender> weak_self = shared_from_this();
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (args.passive) {
|
|
|
|
|
// tcp被动发流模式
|
|
|
|
|
_args.is_udp = false;
|
|
|
|
|
try {
|
|
|
|
|
auto tcp_listener = Socket::createSocket(_poller, false);
|
|
|
|
|
if (args.src_port) {
|
|
|
|
|
//指定端口
|
|
|
|
|
if (!tcp_listener->listen(args.src_port)) {
|
|
|
|
|
throw std::invalid_argument(StrPrinter << "open tcp passive server failed on port:" << args.src_port
|
|
|
|
|
<< ", err:" << get_uv_errmsg(true));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
auto pr = std::make_pair(tcp_listener, Socket::createSocket(_poller, false));
|
|
|
|
|
//从端口池获取随机端口
|
|
|
|
|
makeSockPair(pr, "::", false, false);
|
|
|
|
|
}
|
|
|
|
|
// tcp服务器默认开启5秒
|
|
|
|
|
auto delay_task = _poller->doDelayTask(5 * 1000, [tcp_listener, cb]() mutable {
|
|
|
|
|
cb(0, SockException(Err_timeout, "wait tcp connection timeout"));
|
|
|
|
|
tcp_listener = nullptr;
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
tcp_listener->setOnAccept([weak_self, cb, delay_task](Socket::Ptr &sock, std::shared_ptr<void> &complete) {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//立即关闭tcp服务器
|
|
|
|
|
delay_task->cancel();
|
|
|
|
|
strong_self->_socket = sock;
|
|
|
|
|
strong_self->onConnect();
|
|
|
|
|
cb(sock->get_local_port(), SockException());
|
|
|
|
|
InfoL << "accept connection from:" << sock->get_peer_ip() << ":" << sock->get_peer_port();
|
|
|
|
|
});
|
|
|
|
|
InfoL << "start tcp passive server on:" << tcp_listener->get_local_port();
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
cb(0, SockException(Err_other, ex.what()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-03 18:25:36 +08:00
|
|
|
|
if (args.is_udp) {
|
2020-09-12 18:53:51 +08:00
|
|
|
|
auto poller = _poller;
|
2022-05-14 17:14:38 +08:00
|
|
|
|
WorkThreadPool::Instance().getPoller()->async([cb, args, weak_self, poller]() {
|
2022-05-07 20:06:08 +08:00
|
|
|
|
struct sockaddr_storage addr;
|
2020-09-12 18:53:51 +08:00
|
|
|
|
//切换线程目的是为了dns解析放在后台线程执行
|
2022-05-10 14:48:05 +08:00
|
|
|
|
if (!SockUtil::getDomainIP(args.dst_url.data(), args.dst_port, addr, AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {
|
2022-05-14 17:14:38 +08:00
|
|
|
|
poller->async([args, cb]() {
|
2020-09-12 18:53:51 +08:00
|
|
|
|
//切回自己的线程
|
2022-05-14 17:14:38 +08:00
|
|
|
|
cb(0, SockException(Err_dns, StrPrinter << "dns解析域名失败:" << args.dst_url));
|
2020-09-12 18:53:51 +08:00
|
|
|
|
});
|
2020-09-06 17:56:05 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 18:53:51 +08:00
|
|
|
|
|
|
|
|
|
//dns解析成功
|
2022-05-14 17:14:38 +08:00
|
|
|
|
poller->async([args, addr, weak_self, cb]() {
|
2020-09-12 18:53:51 +08:00
|
|
|
|
//切回自己的线程
|
|
|
|
|
auto strong_self = weak_self.lock();
|
2022-05-14 17:14:38 +08:00
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string ifr_ip = addr.ss_family == AF_INET ? "0.0.0.0" : "::";
|
|
|
|
|
try {
|
|
|
|
|
if (args.src_port) {
|
|
|
|
|
//指定端口
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (!strong_self->_socket->bindUdpSock(args.src_port, ifr_ip)) {
|
|
|
|
|
throw std::invalid_argument(StrPrinter << "bindUdpSock failed on port:" << args.src_port
|
|
|
|
|
<< ", err:" << get_uv_errmsg(true));
|
|
|
|
|
}
|
2022-05-14 17:14:38 +08:00
|
|
|
|
} else {
|
2022-05-14 23:25:22 +08:00
|
|
|
|
auto pr = std::make_pair(strong_self->_socket, Socket::createSocket(strong_self->_poller, false));
|
2022-05-14 17:14:38 +08:00
|
|
|
|
//从端口池获取随机端口
|
|
|
|
|
makeSockPair(pr, ifr_ip, true);
|
|
|
|
|
}
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
cb(0, SockException(Err_other, ex.what()));
|
|
|
|
|
return;
|
2020-09-12 18:53:51 +08:00
|
|
|
|
}
|
2022-05-14 17:14:38 +08:00
|
|
|
|
strong_self->_socket->bindPeerAddr((struct sockaddr *)&addr);
|
|
|
|
|
strong_self->onConnect();
|
|
|
|
|
cb(strong_self->_socket->get_local_port(), SockException());
|
2020-09-12 18:53:51 +08:00
|
|
|
|
});
|
2020-09-06 17:56:05 +08:00
|
|
|
|
});
|
|
|
|
|
} else {
|
2022-04-03 18:25:36 +08:00
|
|
|
|
_socket->connect(args.dst_url, args.dst_port, [cb, weak_self](const SockException &err) {
|
2020-09-06 17:56:05 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
2021-01-02 20:43:02 +08:00
|
|
|
|
if (strong_self) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
//tcp连接成功
|
|
|
|
|
strong_self->onConnect();
|
|
|
|
|
}
|
|
|
|
|
cb(strong_self->_socket->get_local_port(), err);
|
|
|
|
|
} else {
|
|
|
|
|
cb(0, err);
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
2022-05-14 23:25:22 +08:00
|
|
|
|
}, 5.0F, "::", args.src_port);
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 23:33:13 +08:00
|
|
|
|
void RtpSender::onConnect(){
|
2020-09-06 17:56:05 +08:00
|
|
|
|
_is_connect = true;
|
|
|
|
|
//加大发送缓存,防止udp丢包之类的问题
|
|
|
|
|
SockUtil::setSendBuf(_socket->rawFD(), 4 * 1024 * 1024);
|
2022-04-03 18:25:36 +08:00
|
|
|
|
if (!_args.is_udp) {
|
2020-09-06 17:56:05 +08:00
|
|
|
|
//关闭tcp no_delay并开启MSG_MORE, 提高发送性能
|
|
|
|
|
SockUtil::setNoDelay(_socket->rawFD(), false);
|
|
|
|
|
_socket->setSendFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE);
|
|
|
|
|
}
|
|
|
|
|
//连接建立成功事件
|
2020-10-24 23:33:13 +08:00
|
|
|
|
weak_ptr<RtpSender> weak_self = shared_from_this();
|
2020-09-06 17:56:05 +08:00
|
|
|
|
_socket->setOnErr([weak_self](const SockException &err) {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (strong_self) {
|
|
|
|
|
strong_self->onErr(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-01-02 20:43:02 +08:00
|
|
|
|
//获取本地端口,断开重连后确保端口不变
|
2022-04-03 18:25:36 +08:00
|
|
|
|
_args.src_port = _socket->get_local_port();
|
|
|
|
|
InfoL << "开始发送 rtp:" << _socket->get_peer_ip() << ":" << _socket->get_peer_port() << ", 是否为udp方式:" << _args.is_udp;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool RtpSender::addTrack(const Track::Ptr &track){
|
|
|
|
|
return _interface->addTrack(track);
|
2020-09-12 18:53:51 +08:00
|
|
|
|
}
|
2020-09-06 17:56:05 +08:00
|
|
|
|
|
2020-10-24 23:33:13 +08:00
|
|
|
|
void RtpSender::addTrackCompleted(){
|
|
|
|
|
_interface->addTrackCompleted();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtpSender::resetTracks(){
|
|
|
|
|
_interface->resetTracks();
|
2020-09-12 18:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//此函数在其他线程执行
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool RtpSender::inputFrame(const Frame::Ptr &frame) {
|
|
|
|
|
//连接成功后才做实质操作(节省cpu资源)
|
|
|
|
|
return _is_connect ? _interface->inputFrame(frame) : false;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 18:53:51 +08:00
|
|
|
|
//此函数在其他线程执行
|
2020-10-24 23:33:13 +08:00
|
|
|
|
void RtpSender::onFlushRtpList(shared_ptr<List<Buffer::Ptr> > rtp_list) {
|
2020-09-12 18:53:51 +08:00
|
|
|
|
if(!_is_connect){
|
|
|
|
|
//连接成功后才能发送数据
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 18:25:36 +08:00
|
|
|
|
auto is_udp = _args.is_udp;
|
2020-09-12 18:53:51 +08:00
|
|
|
|
auto socket = _socket;
|
|
|
|
|
_poller->async([rtp_list, is_udp, socket]() {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t i = 0;
|
|
|
|
|
auto size = rtp_list->size();
|
2020-10-24 23:33:13 +08:00
|
|
|
|
rtp_list->for_each([&](Buffer::Ptr &packet) {
|
2020-09-12 18:53:51 +08:00
|
|
|
|
if (is_udp) {
|
|
|
|
|
//udp模式,rtp over tcp前4个字节可以忽略
|
2020-10-24 23:33:13 +08:00
|
|
|
|
socket->send(std::make_shared<BufferRtp>(std::move(packet), 4), nullptr, 0, ++i == size);
|
2020-09-12 18:53:51 +08:00
|
|
|
|
} else {
|
|
|
|
|
//tcp模式, rtp over tcp前2个字节可以忽略,只保留后续rtp长度的2个字节
|
2020-10-24 23:33:13 +08:00
|
|
|
|
socket->send(std::make_shared<BufferRtp>(std::move(packet), 2), nullptr, 0, ++i == size);
|
2020-09-12 18:53:51 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2020-09-06 17:56:05 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 23:33:13 +08:00
|
|
|
|
void RtpSender::onErr(const SockException &ex, bool is_connect) {
|
2020-09-06 17:56:05 +08:00
|
|
|
|
_is_connect = false;
|
|
|
|
|
|
2022-05-14 23:25:22 +08:00
|
|
|
|
if (_args.passive) {
|
|
|
|
|
WarnL << "tcp passive connection lost: " << ex.what();
|
2020-09-06 17:56:05 +08:00
|
|
|
|
} else {
|
2022-05-14 23:25:22 +08:00
|
|
|
|
//监听socket断开事件,方便重连
|
|
|
|
|
if (is_connect) {
|
|
|
|
|
WarnL << "重连" << _args.dst_url << ":" << _args.dst_port << "失败, 原因为:" << ex.what();
|
|
|
|
|
} else {
|
|
|
|
|
WarnL << "停止发送 rtp:" << _args.dst_url << ":" << _args.dst_port << ", 原因为:" << ex.what();
|
|
|
|
|
}
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 23:33:13 +08:00
|
|
|
|
weak_ptr<RtpSender> weak_self = shared_from_this();
|
2021-01-17 18:31:50 +08:00
|
|
|
|
_connect_timer = std::make_shared<Timer>(10.0f, [weak_self]() {
|
2020-09-06 17:56:05 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-03 18:25:36 +08:00
|
|
|
|
strong_self->startSend(strong_self->_args, [weak_self](uint16_t local_port, const SockException &ex){
|
2020-09-06 17:56:05 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (strong_self && ex) {
|
|
|
|
|
//连接失败且本对象未销毁,那么重试连接
|
|
|
|
|
strong_self->onErr(ex, true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}, _poller);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:46:58 +08:00
|
|
|
|
}//namespace mediakit
|
|
|
|
|
#endif// defined(ENABLE_RTPPROXY)
|