mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-25 12:11:36 +08:00
webrtc单端口模式支持线程安全,支持链接迁移
This commit is contained in:
parent
c025b5092d
commit
7ba44d1ac8
@ -1 +1 @@
|
|||||||
Subproject commit a827f0bdcb4221c19ab1a243ebd1f01fa7dc50f2
|
Subproject commit b8e066222b757a2c11b5e44c49ef6982acb95fe2
|
@ -309,6 +309,17 @@ int start_main(int argc,char *argv[]) {
|
|||||||
#if defined(ENABLE_WEBRTC)
|
#if defined(ENABLE_WEBRTC)
|
||||||
//webrtc udp服务器
|
//webrtc udp服务器
|
||||||
UdpServer::Ptr rtcSrv = std::make_shared<UdpServer>();
|
UdpServer::Ptr rtcSrv = std::make_shared<UdpServer>();
|
||||||
|
rtcSrv->setOnCreateSocket([](const EventPoller::Ptr &poller, const Buffer::Ptr &buf, struct sockaddr *, int) {
|
||||||
|
if (!buf) {
|
||||||
|
return Socket::createSocket(poller, false);
|
||||||
|
}
|
||||||
|
auto new_poller = WebRtcSession::getPoller(buf);
|
||||||
|
if (!new_poller) {
|
||||||
|
//该数据对应的webrtc对象未找到,丢弃之
|
||||||
|
return Socket::Ptr();
|
||||||
|
}
|
||||||
|
return Socket::createSocket(new_poller, false);
|
||||||
|
});
|
||||||
uint16_t rtcPort = mINI::Instance()[RTC::kPort];
|
uint16_t rtcPort = mINI::Instance()[RTC::kPort];
|
||||||
#endif//defined(ENABLE_WEBRTC)
|
#endif//defined(ENABLE_WEBRTC)
|
||||||
|
|
||||||
|
@ -21,26 +21,54 @@ WebRtcSession::~WebRtcSession() {
|
|||||||
InfoP(this);
|
InfoP(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string getUserName(const Buffer::Ptr &buffer) {
|
||||||
|
auto buf = buffer->data();
|
||||||
|
auto len = buffer->size();
|
||||||
|
if (!RTC::StunPacket::IsStun((const uint8_t *) buf, len)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
std::unique_ptr<RTC::StunPacket> packet(RTC::StunPacket::Parse((const uint8_t *) buf, len));
|
||||||
|
if (!packet) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (packet->GetClass() != RTC::StunPacket::Class::REQUEST ||
|
||||||
|
packet->GetMethod() != RTC::StunPacket::Method::BINDING) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
//收到binding request请求
|
||||||
|
auto vec = split(packet->GetUsername(), ":");
|
||||||
|
return vec[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
EventPoller::Ptr WebRtcSession::getPoller(const Buffer::Ptr &buffer) {
|
||||||
|
auto user_name = getUserName(buffer);
|
||||||
|
if (user_name.empty()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
auto ret = WebRtcTransportImp::getTransport(user_name);
|
||||||
|
return ret ? ret->getPoller() : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void WebRtcSession::onRecv(const Buffer::Ptr &buffer) {
|
void WebRtcSession::onRecv(const Buffer::Ptr &buffer) {
|
||||||
auto buf = buffer->data();
|
auto buf = buffer->data();
|
||||||
auto len = buffer->size();
|
auto len = buffer->size();
|
||||||
|
|
||||||
if (!_transport && RTC::StunPacket::IsStun((const uint8_t *) buf, len)) {
|
if (!_transport) {
|
||||||
std::unique_ptr<RTC::StunPacket> packet(RTC::StunPacket::Parse((const uint8_t *) buf, len));
|
auto user_name = getUserName(buffer);
|
||||||
if (!packet) {
|
if (user_name.empty()) {
|
||||||
WarnL << "parse stun error";
|
//逻辑分支不太可能走到这里
|
||||||
|
WarnL << user_name;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (packet->GetClass() == RTC::StunPacket::Class::REQUEST &&
|
_transport = WebRtcTransportImp::getTransport(user_name);
|
||||||
packet->GetMethod() == RTC::StunPacket::Method::BINDING) {
|
if (!_transport) {
|
||||||
//收到binding request请求
|
//逻辑分支不太可能走到这里
|
||||||
_transport = createTransport(packet->GetUsername());
|
WarnL << user_name;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
_transport->setSession(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_transport) {
|
|
||||||
_transport->inputSockData(buf, len, &_peer_addr);
|
_transport->inputSockData(buf, len, &_peer_addr);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRtcSession::onError(const SockException &err) {
|
void WebRtcSession::onError(const SockException &err) {
|
||||||
@ -53,13 +81,3 @@ void WebRtcSession::onError(const SockException &err) {
|
|||||||
void WebRtcSession::onManager() {
|
void WebRtcSession::onManager() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<WebRtcTransport> WebRtcSession::createTransport(const string &user_name) {
|
|
||||||
if (user_name.empty()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto vec = split(user_name, ":");
|
|
||||||
auto ret = WebRtcTransportImp::getTransport(vec[0]);
|
|
||||||
ret->setSession(this);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
@ -20,6 +20,8 @@ using namespace toolkit;
|
|||||||
|
|
||||||
class WebRtcSession : public UdpSession {
|
class WebRtcSession : public UdpSession {
|
||||||
public:
|
public:
|
||||||
|
static EventPoller::Ptr getPoller(const Buffer::Ptr &);
|
||||||
|
|
||||||
WebRtcSession(const Socket::Ptr &sock);
|
WebRtcSession(const Socket::Ptr &sock);
|
||||||
~WebRtcSession() override;
|
~WebRtcSession() override;
|
||||||
|
|
||||||
@ -27,12 +29,9 @@ public:
|
|||||||
void onError(const SockException &err) override;
|
void onError(const SockException &err) override;
|
||||||
void onManager() override;
|
void onManager() override;
|
||||||
|
|
||||||
private:
|
|
||||||
std::shared_ptr<WebRtcTransport> createTransport(const string &user_name);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct sockaddr _peer_addr;
|
struct sockaddr _peer_addr;
|
||||||
std::shared_ptr<WebRtcTransport> _transport;
|
std::shared_ptr<WebRtcTransportImp> _transport;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user