主动发送rtp接口(startSendRtp)支持返回本地端口: #538

This commit is contained in:
xiongziliang
2021-01-02 20:43:02 +08:00
parent 113430345c
commit c69e9b8ec1
10 changed files with 36 additions and 28 deletions

View File

@@ -26,7 +26,7 @@ RtpSender::RtpSender(uint32_t ssrc, uint8_t payload_type) {
RtpSender::~RtpSender() {
}
void RtpSender::startSend(const string &dst_url, uint16_t dst_port, bool is_udp, uint16_t src_port, const function<void(const SockException &ex)> &cb){
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;
@@ -36,21 +36,22 @@ void RtpSender::startSend(const string &dst_url, uint16_t dst_port, bool is_udp,
if (is_udp) {
_socket->bindUdpSock(src_port);
auto poller = _poller;
WorkThreadPool::Instance().getPoller()->async([cb, dst_url, dst_port, weak_self, poller]() {
auto local_port = _socket->get_local_port();
WorkThreadPool::Instance().getPoller()->async([cb, dst_url, dst_port, weak_self, poller, local_port]() {
struct sockaddr addr;
//切换线程目的是为了dns解析放在后台线程执行
if (!SockUtil::getDomainIP(dst_url.data(), dst_port, addr)) {
poller->async([dst_url, cb]() {
poller->async([dst_url, cb, local_port]() {
//切回自己的线程
cb(SockException(Err_dns, StrPrinter << "dns解析域名失败:" << dst_url));
cb(local_port, SockException(Err_dns, StrPrinter << "dns解析域名失败:" << dst_url));
});
return;
}
//dns解析成功
poller->async([addr, weak_self, cb]() {
poller->async([addr, weak_self, cb, local_port]() {
//切回自己的线程
cb(SockException());
cb(local_port, SockException());
auto strong_self = weak_self.lock();
if (strong_self) {
strong_self->_socket->setSendPeerAddr(&addr);
@@ -60,11 +61,15 @@ 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) {
cb(err);
auto strong_self = weak_self.lock();
if (strong_self && !err) {
//tcp连接成功
strong_self->onConnect();
if (strong_self) {
if (!err) {
//tcp连接成功
strong_self->onConnect();
}
cb(strong_self->_socket->get_local_port(), err);
} else {
cb(0, err);
}
}, 5.0F, "0.0.0.0", src_port);
}
@@ -87,6 +92,8 @@ void RtpSender::onConnect(){
strong_self->onErr(err);
}
});
//获取本地端口,断开重连后确保端口不变
_src_port = _socket->get_local_port();
InfoL << "开始发送 rtp:" << _socket->get_peer_ip() << ":" << _socket->get_peer_port() << ", 是否为udp方式:" << _is_udp;
}
@@ -150,7 +157,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](const SockException &ex){
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){
auto strong_self = weak_self.lock();
if (strong_self && ex) {
//连接失败且本对象未销毁,那么重试连接