2021-09-08 18:00:55 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "WebRtcSession.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
2021-09-10 18:37:32 +08:00
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 10:29:00 +08:00
|
|
|
|
EventPoller::Ptr WebRtcSession::queryPoller(const Buffer::Ptr &buffer) {
|
2021-09-10 18:37:32 +08:00
|
|
|
|
auto user_name = getUserName(buffer);
|
|
|
|
|
if (user_name.empty()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-10-16 10:25:23 +08:00
|
|
|
|
auto ret = WebRtcTransportManager::Instance().getItem(user_name);
|
2021-09-10 18:37:32 +08:00
|
|
|
|
return ret ? ret->getPoller() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 10:29:00 +08:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
WebRtcSession::WebRtcSession(const Socket::Ptr &sock) : UdpSession(sock) {
|
|
|
|
|
socklen_t addr_len = sizeof(_peer_addr);
|
2022-05-08 00:26:01 +08:00
|
|
|
|
getpeername(sock->rawFD(), (struct sockaddr *)&_peer_addr, &addr_len);
|
2021-10-16 10:29:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebRtcSession::~WebRtcSession() {
|
|
|
|
|
InfoP(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 18:00:55 +08:00
|
|
|
|
void WebRtcSession::onRecv(const Buffer::Ptr &buffer) {
|
2021-09-15 11:50:15 +08:00
|
|
|
|
if (_find_transport) {
|
|
|
|
|
//只允许寻找一次transport
|
|
|
|
|
_find_transport = false;
|
2021-10-15 17:12:39 +08:00
|
|
|
|
auto user_name = getUserName(buffer);
|
2021-11-28 21:42:54 +08:00
|
|
|
|
_identifier = to_string(getSock()->rawFD()) + '-' + user_name;
|
2021-10-16 10:25:23 +08:00
|
|
|
|
auto transport = WebRtcTransportManager::Instance().getItem(user_name);
|
2021-10-15 18:56:49 +08:00
|
|
|
|
CHECK(transport && transport->getPoller()->isCurrentThread());
|
|
|
|
|
transport->setSession(shared_from_this());
|
|
|
|
|
_transport = std::move(transport);
|
2021-10-15 17:12:39 +08:00
|
|
|
|
InfoP(this);
|
2021-09-08 18:00:55 +08:00
|
|
|
|
}
|
2021-09-10 22:31:44 +08:00
|
|
|
|
_ticker.resetTime();
|
2021-09-15 11:50:15 +08:00
|
|
|
|
CHECK(_transport);
|
2022-05-08 00:26:01 +08:00
|
|
|
|
_transport->inputSockData(buffer->data(), buffer->size(), (struct sockaddr *)&_peer_addr);
|
2021-09-08 18:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcSession::onError(const SockException &err) {
|
2021-09-10 22:31:44 +08:00
|
|
|
|
//udp链接超时,但是rtc链接不一定超时,因为可能存在udp链接迁移的情况
|
|
|
|
|
//在udp链接迁移时,新的WebRtcSession对象将接管WebRtcTransport对象的生命周期
|
|
|
|
|
//本WebRtcSession对象将在超时后自动销毁
|
|
|
|
|
WarnP(this) << err.what();
|
2021-09-16 10:03:28 +08:00
|
|
|
|
|
2021-09-16 10:05:04 +08:00
|
|
|
|
if (!_transport) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-16 10:03:28 +08:00
|
|
|
|
auto transport = std::move(_transport);
|
2021-10-13 18:41:17 +08:00
|
|
|
|
getPoller()->async([transport] {
|
2021-09-16 10:03:28 +08:00
|
|
|
|
//延时减引用,防止使用transport对象时,销毁对象
|
|
|
|
|
}, false);
|
2021-09-08 18:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcSession::onManager() {
|
2021-09-10 22:31:44 +08:00
|
|
|
|
GET_CONFIG(float, timeoutSec, RTC::kTimeOutSec);
|
|
|
|
|
if (!_transport && _ticker.createdTime() > timeoutSec * 1000) {
|
|
|
|
|
shutdown(SockException(Err_timeout, "illegal webrtc connection"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_ticker.elapsedTime() > timeoutSec * 1000) {
|
|
|
|
|
shutdown(SockException(Err_timeout, "webrtc connection timeout"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-08 18:00:55 +08:00
|
|
|
|
}
|
2021-10-15 17:12:39 +08:00
|
|
|
|
|
|
|
|
|
std::string WebRtcSession::getIdentifier() const {
|
|
|
|
|
return _identifier;
|
|
|
|
|
}
|
|
|
|
|
|