mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-14 12:05:58 +08:00
完善startSendRtp接口
This commit is contained in:
@@ -19,37 +19,28 @@ using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
RtpSender::RtpSender(uint32_t ssrc, uint8_t payload_type,bool use_ps, bool only_audio) {
|
||||
void RtpSender::startSend(const MediaSourceEvent::SendRtpArgs &args, const function<void(uint16_t local_port, const SockException &ex)> &cb){
|
||||
_args = args;
|
||||
_poller = EventPollerPool::Instance().getPoller();
|
||||
if (use_ps) {
|
||||
_interface = std::make_shared<RtpCachePS>(
|
||||
[this](std::shared_ptr<List<Buffer::Ptr>> list) { onFlushRtpList(std::move(list)); }, ssrc, payload_type);
|
||||
}else{
|
||||
_interface = std::make_shared<RtpCacheRaw>(
|
||||
[this](std::shared_ptr<List<Buffer::Ptr>> list) { onFlushRtpList(std::move(list)); }, ssrc, payload_type,only_audio);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
RtpSender::~RtpSender() {}
|
||||
|
||||
void RtpSender::startSend(const string &dst_url, uint16_t dst_port, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb){
|
||||
_is_udp = is_udp;
|
||||
_socket = Socket::createSocket(_poller, false);
|
||||
_dst_url = dst_url;
|
||||
_dst_port = dst_port;
|
||||
_src_port = src_port;
|
||||
weak_ptr<RtpSender> weak_self = shared_from_this();
|
||||
if (is_udp) {
|
||||
_socket->bindUdpSock(src_port);
|
||||
if (args.is_udp) {
|
||||
_socket->bindUdpSock(args.src_port);
|
||||
auto poller = _poller;
|
||||
auto local_port = _socket->get_local_port();
|
||||
WorkThreadPool::Instance().getPoller()->async([cb, dst_url, dst_port, weak_self, poller, local_port]() {
|
||||
WorkThreadPool::Instance().getPoller()->async([cb, args, weak_self, poller, local_port]() {
|
||||
struct sockaddr addr;
|
||||
//切换线程目的是为了dns解析放在后台线程执行
|
||||
if (!SockUtil::getDomainIP(dst_url.data(), dst_port, addr)) {
|
||||
poller->async([dst_url, cb, local_port]() {
|
||||
if (!SockUtil::getDomainIP(args.dst_url.data(), args.dst_port, addr)) {
|
||||
poller->async([args, cb, local_port]() {
|
||||
//切回自己的线程
|
||||
cb(local_port, SockException(Err_dns, StrPrinter << "dns解析域名失败:" << dst_url));
|
||||
cb(local_port, SockException(Err_dns, StrPrinter << "dns解析域名失败:" << args.dst_url));
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -66,7 +57,7 @@ void RtpSender::startSend(const string &dst_url, uint16_t dst_port, bool is_udp,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
_socket->connect(dst_url, dst_port, [cb, weak_self](const SockException &err) {
|
||||
_socket->connect(args.dst_url, args.dst_port, [cb, weak_self](const SockException &err) {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (strong_self) {
|
||||
if (!err) {
|
||||
@@ -77,7 +68,7 @@ void RtpSender::startSend(const string &dst_url, uint16_t dst_port, bool is_udp,
|
||||
} else {
|
||||
cb(0, err);
|
||||
}
|
||||
}, 5.0F, "0.0.0.0", src_port);
|
||||
}, 5.0F, "0.0.0.0", args.src_port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +76,7 @@ void RtpSender::onConnect(){
|
||||
_is_connect = true;
|
||||
//加大发送缓存,防止udp丢包之类的问题
|
||||
SockUtil::setSendBuf(_socket->rawFD(), 4 * 1024 * 1024);
|
||||
if (!_is_udp) {
|
||||
if (!_args.is_udp) {
|
||||
//关闭tcp no_delay并开启MSG_MORE, 提高发送性能
|
||||
SockUtil::setNoDelay(_socket->rawFD(), false);
|
||||
_socket->setSendFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE);
|
||||
@@ -99,8 +90,8 @@ void RtpSender::onConnect(){
|
||||
}
|
||||
});
|
||||
//获取本地端口,断开重连后确保端口不变
|
||||
_src_port = _socket->get_local_port();
|
||||
InfoL << "开始发送 rtp:" << _socket->get_peer_ip() << ":" << _socket->get_peer_port() << ", 是否为udp方式:" << _is_udp;
|
||||
_args.src_port = _socket->get_local_port();
|
||||
InfoL << "开始发送 rtp:" << _socket->get_peer_ip() << ":" << _socket->get_peer_port() << ", 是否为udp方式:" << _args.is_udp;
|
||||
}
|
||||
|
||||
bool RtpSender::addTrack(const Track::Ptr &track){
|
||||
@@ -128,7 +119,7 @@ void RtpSender::onFlushRtpList(shared_ptr<List<Buffer::Ptr> > rtp_list) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto is_udp = _is_udp;
|
||||
auto is_udp = _args.is_udp;
|
||||
auto socket = _socket;
|
||||
_poller->async([rtp_list, is_udp, socket]() {
|
||||
size_t i = 0;
|
||||
@@ -150,9 +141,9 @@ void RtpSender::onErr(const SockException &ex, bool is_connect) {
|
||||
|
||||
//监听socket断开事件,方便重连
|
||||
if (is_connect) {
|
||||
WarnL << "重连" << _dst_url << ":" << _dst_port << "失败, 原因为:" << ex.what();
|
||||
WarnL << "重连" << _args.dst_url << ":" << _args.dst_port << "失败, 原因为:" << ex.what();
|
||||
} else {
|
||||
WarnL << "停止发送 rtp:" << _dst_url << ":" << _dst_port << ", 原因为:" << ex.what();
|
||||
WarnL << "停止发送 rtp:" << _args.dst_url << ":" << _args.dst_port << ", 原因为:" << ex.what();
|
||||
}
|
||||
|
||||
weak_ptr<RtpSender> weak_self = shared_from_this();
|
||||
@@ -161,7 +152,7 @@ void RtpSender::onErr(const SockException &ex, bool is_connect) {
|
||||
if (!strong_self) {
|
||||
return false;
|
||||
}
|
||||
strong_self->startSend(strong_self->_dst_url, strong_self->_dst_port, strong_self->_is_udp, strong_self->_src_port, [weak_self](uint16_t local_port, const SockException &ex){
|
||||
strong_self->startSend(strong_self->_args, [weak_self](uint16_t local_port, const SockException &ex){
|
||||
auto strong_self = weak_self.lock();
|
||||
if (strong_self && ex) {
|
||||
//连接失败且本对象未销毁,那么重试连接
|
||||
|
||||
Reference in New Issue
Block a user