mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 20:47:08 +08:00
webrtc支持通过http参数指定是否优先tcp模式 (#2105)
* webrtc push/play支持通过http参数指定tcp * force_tcp改成perferred_tcp Co-authored-by: xiongziliang <771730766@qq.com>
This commit is contained in:
parent
50fa671564
commit
fef9d31631
@ -16,8 +16,9 @@ namespace mediakit {
|
|||||||
|
|
||||||
WebRtcPlayer::Ptr WebRtcPlayer::create(const EventPoller::Ptr &poller,
|
WebRtcPlayer::Ptr WebRtcPlayer::create(const EventPoller::Ptr &poller,
|
||||||
const RtspMediaSource::Ptr &src,
|
const RtspMediaSource::Ptr &src,
|
||||||
const MediaInfo &info) {
|
const MediaInfo &info,
|
||||||
WebRtcPlayer::Ptr ret(new WebRtcPlayer(poller, src, info), [](WebRtcPlayer *ptr) {
|
bool perferred_tcp) {
|
||||||
|
WebRtcPlayer::Ptr ret(new WebRtcPlayer(poller, src, info, perferred_tcp), [](WebRtcPlayer *ptr) {
|
||||||
ptr->onDestory();
|
ptr->onDestory();
|
||||||
delete ptr;
|
delete ptr;
|
||||||
});
|
});
|
||||||
@ -27,7 +28,8 @@ WebRtcPlayer::Ptr WebRtcPlayer::create(const EventPoller::Ptr &poller,
|
|||||||
|
|
||||||
WebRtcPlayer::WebRtcPlayer(const EventPoller::Ptr &poller,
|
WebRtcPlayer::WebRtcPlayer(const EventPoller::Ptr &poller,
|
||||||
const RtspMediaSource::Ptr &src,
|
const RtspMediaSource::Ptr &src,
|
||||||
const MediaInfo &info) : WebRtcTransportImp(poller) {
|
const MediaInfo &info,
|
||||||
|
bool perferred_tcp) : WebRtcTransportImp(poller,perferred_tcp) {
|
||||||
_media_info = info;
|
_media_info = info;
|
||||||
_play_src = src;
|
_play_src = src;
|
||||||
CHECK(_play_src);
|
CHECK(_play_src);
|
||||||
|
@ -19,7 +19,7 @@ class WebRtcPlayer : public WebRtcTransportImp {
|
|||||||
public:
|
public:
|
||||||
using Ptr = std::shared_ptr<WebRtcPlayer>;
|
using Ptr = std::shared_ptr<WebRtcPlayer>;
|
||||||
~WebRtcPlayer() override = default;
|
~WebRtcPlayer() override = default;
|
||||||
static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info);
|
static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info, bool perferred_tcp = false);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
///////WebRtcTransportImp override///////
|
///////WebRtcTransportImp override///////
|
||||||
@ -29,7 +29,7 @@ protected:
|
|||||||
void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override {};
|
void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override {};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WebRtcPlayer(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info);
|
WebRtcPlayer(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info, bool perferred_tcp);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//媒体相关元数据
|
//媒体相关元数据
|
||||||
|
@ -18,8 +18,9 @@ WebRtcPusher::Ptr WebRtcPusher::create(const EventPoller::Ptr &poller,
|
|||||||
const RtspMediaSourceImp::Ptr &src,
|
const RtspMediaSourceImp::Ptr &src,
|
||||||
const std::shared_ptr<void> &ownership,
|
const std::shared_ptr<void> &ownership,
|
||||||
const MediaInfo &info,
|
const MediaInfo &info,
|
||||||
const ProtocolOption &option) {
|
const ProtocolOption &option,
|
||||||
WebRtcPusher::Ptr ret(new WebRtcPusher(poller, src, ownership, info, option), [](WebRtcPusher *ptr) {
|
bool perferred_tcp) {
|
||||||
|
WebRtcPusher::Ptr ret(new WebRtcPusher(poller, src, ownership, info, option,perferred_tcp), [](WebRtcPusher *ptr) {
|
||||||
ptr->onDestory();
|
ptr->onDestory();
|
||||||
delete ptr;
|
delete ptr;
|
||||||
});
|
});
|
||||||
@ -31,7 +32,8 @@ WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller,
|
|||||||
const RtspMediaSourceImp::Ptr &src,
|
const RtspMediaSourceImp::Ptr &src,
|
||||||
const std::shared_ptr<void> &ownership,
|
const std::shared_ptr<void> &ownership,
|
||||||
const MediaInfo &info,
|
const MediaInfo &info,
|
||||||
const ProtocolOption &option) : WebRtcTransportImp(poller) {
|
const ProtocolOption &option,
|
||||||
|
bool perferred_tcp) : WebRtcTransportImp(poller,perferred_tcp) {
|
||||||
_media_info = info;
|
_media_info = info;
|
||||||
_push_src = src;
|
_push_src = src;
|
||||||
_push_src_ownership = ownership;
|
_push_src_ownership = ownership;
|
||||||
|
@ -20,7 +20,7 @@ public:
|
|||||||
using Ptr = std::shared_ptr<WebRtcPusher>;
|
using Ptr = std::shared_ptr<WebRtcPusher>;
|
||||||
~WebRtcPusher() override = default;
|
~WebRtcPusher() override = default;
|
||||||
static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src,
|
static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src,
|
||||||
const std::shared_ptr<void> &ownership, const MediaInfo &info, const ProtocolOption &option);
|
const std::shared_ptr<void> &ownership, const MediaInfo &info, const ProtocolOption &option, bool perferred_tcp = false);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
///////WebRtcTransportImp override///////
|
///////WebRtcTransportImp override///////
|
||||||
@ -51,7 +51,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
WebRtcPusher(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src,
|
WebRtcPusher(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src,
|
||||||
const std::shared_ptr<void> &ownership, const MediaInfo &info, const ProtocolOption &option);
|
const std::shared_ptr<void> &ownership, const MediaInfo &info, const ProtocolOption &option, bool perferred_tcp);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _simulcast = false;
|
bool _simulcast = false;
|
||||||
|
@ -69,6 +69,7 @@ void WebRtcSession::onRecv_l(const char *data, size_t len) {
|
|||||||
//WebRtcTransport在其他poller线程上,需要切换poller线程并重新创建WebRtcSession对象
|
//WebRtcTransport在其他poller线程上,需要切换poller线程并重新创建WebRtcSession对象
|
||||||
if (!transport->getPoller()->isCurrentThread()) {
|
if (!transport->getPoller()->isCurrentThread()) {
|
||||||
auto sock = Socket::createSocket(transport->getPoller());
|
auto sock = Socket::createSocket(transport->getPoller());
|
||||||
|
//1、克隆socket(fd不变),切换poller线程到WebRtcTransport所在线程
|
||||||
sock->cloneFromPeerSocket(*(getSock()));
|
sock->cloneFromPeerSocket(*(getSock()));
|
||||||
auto server = _server;
|
auto server = _server;
|
||||||
std::string str(data, len);
|
std::string str(data, len);
|
||||||
@ -76,9 +77,11 @@ void WebRtcSession::onRecv_l(const char *data, size_t len) {
|
|||||||
auto strong_server = server.lock();
|
auto strong_server = server.lock();
|
||||||
if (strong_server) {
|
if (strong_server) {
|
||||||
auto session = static_pointer_cast<WebRtcSession>(strong_server->createSession(sock));
|
auto session = static_pointer_cast<WebRtcSession>(strong_server->createSession(sock));
|
||||||
|
//2、创建新的WebRtcSession对象(绑定到WebRtcTransport所在线程),重新处理一遍ice binding request命令
|
||||||
session->onRecv_l(str.data(), str.size());
|
session->onRecv_l(str.data(), str.size());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
//3、销毁原先的socket和WebRtcSession(原先的对象跟WebRtcTransport不在同一条线程)
|
||||||
throw std::runtime_error("webrtc over tcp change poller: " + getPoller()->getThreadName() + " -> " + sock->getPoller()->getThreadName());
|
throw std::runtime_error("webrtc over tcp change poller: " + getPoller()->getThreadName() + " -> " + sock->getPoller()->getThreadName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -402,8 +402,8 @@ void WebRtcTransportImp::OnDtlsTransportApplicationDataReceived(const RTC::DtlsT
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller)
|
WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller,bool perferred_tcp)
|
||||||
: WebRtcTransport(poller) {
|
: WebRtcTransport(poller), _perferred_tcp(perferred_tcp) {
|
||||||
InfoL << getIdentifier();
|
InfoL << getIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -629,13 +629,13 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
|
|||||||
if (extern_ips.empty()) {
|
if (extern_ips.empty()) {
|
||||||
std::string local_ip = SockUtil::get_local_ip();
|
std::string local_ip = SockUtil::get_local_ip();
|
||||||
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_udp_port, 120, "udp")); }
|
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_udp_port, 120, "udp")); }
|
||||||
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_tcp_port, 110, "tcp")); }
|
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_tcp_port, _perferred_tcp ? 125 : 115, "tcp")); }
|
||||||
} else {
|
} else {
|
||||||
const uint32_t delta = 10;
|
const uint32_t delta = 10;
|
||||||
uint32_t priority = 100 + delta * extern_ips.size();
|
uint32_t priority = 100 + delta * extern_ips.size();
|
||||||
for (auto ip : extern_ips) {
|
for (auto ip : extern_ips) {
|
||||||
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(ip, local_udp_port, priority + 5, "udp")); }
|
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(ip, local_udp_port, priority, "udp")); }
|
||||||
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(ip, local_tcp_port, priority, "tcp")); }
|
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(ip, local_tcp_port, priority - (_perferred_tcp ? -5 : 5), "tcp")); }
|
||||||
priority -= delta;
|
priority -= delta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1153,7 +1153,9 @@ void echo_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana
|
|||||||
|
|
||||||
void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
||||||
MediaInfo info(args["url"]);
|
MediaInfo info(args["url"]);
|
||||||
Broadcast::PublishAuthInvoker invoker = [cb, info](const string &err, const ProtocolOption &option) mutable {
|
bool perferred_tcp = args["perferred_tcp"];
|
||||||
|
|
||||||
|
Broadcast::PublishAuthInvoker invoker = [cb, info, perferred_tcp](const string &err, const ProtocolOption &option) mutable {
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
cb(WebRtcException(SockException(Err_other, err)));
|
cb(WebRtcException(SockException(Err_other, err)));
|
||||||
return;
|
return;
|
||||||
@ -1192,7 +1194,7 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana
|
|||||||
push_src_ownership = push_src->getOwnership();
|
push_src_ownership = push_src->getOwnership();
|
||||||
push_src->setProtocolOption(option);
|
push_src->setProtocolOption(option);
|
||||||
}
|
}
|
||||||
auto rtc = WebRtcPusher::create(EventPollerPool::Instance().getPoller(), push_src, push_src_ownership, info, option);
|
auto rtc = WebRtcPusher::create(EventPollerPool::Instance().getPoller(), push_src, push_src_ownership, info, option, perferred_tcp);
|
||||||
push_src->setListener(rtc);
|
push_src->setListener(rtc);
|
||||||
cb(*rtc);
|
cb(*rtc);
|
||||||
};
|
};
|
||||||
@ -1207,8 +1209,10 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana
|
|||||||
|
|
||||||
void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
||||||
MediaInfo info(args["url"]);
|
MediaInfo info(args["url"]);
|
||||||
|
bool perferred_tcp = args["perferred_tcp"];
|
||||||
|
|
||||||
auto session_ptr = sender.shared_from_this();
|
auto session_ptr = sender.shared_from_this();
|
||||||
Broadcast::AuthInvoker invoker = [cb, info, session_ptr](const string &err) mutable {
|
Broadcast::AuthInvoker invoker = [cb, info, session_ptr, perferred_tcp](const string &err) mutable {
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
cb(WebRtcException(SockException(Err_other, err)));
|
cb(WebRtcException(SockException(Err_other, err)));
|
||||||
return;
|
return;
|
||||||
@ -1224,7 +1228,7 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana
|
|||||||
}
|
}
|
||||||
// 还原成rtc,目的是为了hook时识别哪种播放协议
|
// 还原成rtc,目的是为了hook时识别哪种播放协议
|
||||||
info._schema = RTC_SCHEMA;
|
info._schema = RTC_SCHEMA;
|
||||||
auto rtc = WebRtcPlayer::create(EventPollerPool::Instance().getPoller(), src, info);
|
auto rtc = WebRtcPlayer::create(EventPollerPool::Instance().getPoller(), src, info, perferred_tcp);
|
||||||
cb(*rtc);
|
cb(*rtc);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -251,7 +251,7 @@ public:
|
|||||||
void createRtpChannel(const std::string &rid, uint32_t ssrc, MediaTrack &track);
|
void createRtpChannel(const std::string &rid, uint32_t ssrc, MediaTrack &track);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
WebRtcTransportImp(const EventPoller::Ptr &poller);
|
WebRtcTransportImp(const EventPoller::Ptr &poller,bool perferred_tcp = false);
|
||||||
void OnDtlsTransportApplicationDataReceived(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) override;
|
void OnDtlsTransportApplicationDataReceived(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) override;
|
||||||
void onStartWebRTC() override;
|
void onStartWebRTC() override;
|
||||||
void onSendSockData(Buffer::Ptr buf, bool flush = true, RTC::TransportTuple *tuple = nullptr) override;
|
void onSendSockData(Buffer::Ptr buf, bool flush = true, RTC::TransportTuple *tuple = nullptr) override;
|
||||||
@ -281,6 +281,7 @@ private:
|
|||||||
void onCheckAnswer(RtcSession &sdp);
|
void onCheckAnswer(RtcSession &sdp);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool _perferred_tcp;
|
||||||
uint16_t _rtx_seq[2] = {0, 0};
|
uint16_t _rtx_seq[2] = {0, 0};
|
||||||
//用掉的总流量
|
//用掉的总流量
|
||||||
uint64_t _bytes_usage = 0;
|
uint64_t _bytes_usage = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user