From a1da95c9067565f64c0f948b2456cd8a7958b756 Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 7 Aug 2020 17:04:34 +0800 Subject: [PATCH] RTC: Support server hijacker --- trunk/src/app/srs_app_rtc_server.cpp | 26 ++++++++++++++++++++++++++ trunk/src/app/srs_app_rtc_server.hpp | 13 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/trunk/src/app/srs_app_rtc_server.cpp b/trunk/src/app/srs_app_rtc_server.cpp index 1c8c6d747..de8bdcc9e 100644 --- a/trunk/src/app/srs_app_rtc_server.cpp +++ b/trunk/src/app/srs_app_rtc_server.cpp @@ -204,9 +204,18 @@ ISrsRtcServerHandler::~ISrsRtcServerHandler() { } +ISrsRtcServerHijacker::ISrsRtcServerHijacker() +{ +} + +ISrsRtcServerHijacker::~ISrsRtcServerHijacker() +{ +} + SrsRtcServer::SrsRtcServer() { handler = NULL; + hijacker = NULL; timer = new SrsHourGlass(this, 1 * SRS_UTIME_SECONDS); } @@ -257,6 +266,11 @@ void SrsRtcServer::set_handler(ISrsRtcServerHandler* h) handler = h; } +void SrsRtcServer::set_hijacker(ISrsRtcServerHijacker* h) +{ + hijacker = h; +} + srs_error_t SrsRtcServer::listen_udp() { srs_error_t err = srs_success; @@ -309,6 +323,18 @@ srs_error_t SrsRtcServer::on_udp_packet(SrsUdpMuxSocket* skt) } } + // Notify hijack to handle the UDP packet. + if (hijacker) { + bool consumed = false; + if ((err = hijacker->on_udp_packet(&consumed)) != srs_success) { + return srs_error_wrap(err, "hijack consumed=%u", consumed); + } + + if (consumed) { + return err; + } + } + // For STUN, the peer address may change. if (srs_is_stun((uint8_t*)data, size)) { SrsStunPacket ping; diff --git a/trunk/src/app/srs_app_rtc_server.hpp b/trunk/src/app/srs_app_rtc_server.hpp index aa02b7425..51276a7e4 100644 --- a/trunk/src/app/srs_app_rtc_server.hpp +++ b/trunk/src/app/srs_app_rtc_server.hpp @@ -72,6 +72,17 @@ public: virtual void on_timeout(SrsRtcConnection* session) = 0; }; +// The hijacker to hook server. +class ISrsRtcServerHijacker +{ +public: + ISrsRtcServerHijacker(); + virtual ~ISrsRtcServerHijacker(); +public: + // If consumed set to true, server will ignore the packet. + virtual srs_error_t on_udp_packet(bool* pconsumed) = 0; +}; + // The RTC server instance, listen UDP port, handle UDP packet, manage RTC connections. class SrsRtcServer : virtual public ISrsUdpMuxHandler, virtual public ISrsHourGlass { @@ -79,6 +90,7 @@ private: SrsHourGlass* timer; std::vector listeners; ISrsRtcServerHandler* handler; + ISrsRtcServerHijacker* hijacker; private: // TODO: FIXME: Rename it. std::map map_username_session; // key: username(local_ufrag + ":" + remote_ufrag) @@ -93,6 +105,7 @@ public: virtual srs_error_t initialize(); // Set the handler for server events. void set_handler(ISrsRtcServerHandler* h); + void set_hijacker(ISrsRtcServerHijacker* h); public: // TODO: FIXME: Support gracefully quit. // TODO: FIXME: Support reload.