1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 20:01:56 +00:00

RTC: Support server hijacker

This commit is contained in:
winlin 2020-08-07 17:04:34 +08:00
parent 73eb60a9d5
commit a1da95c906
2 changed files with 39 additions and 0 deletions

View file

@ -204,9 +204,18 @@ ISrsRtcServerHandler::~ISrsRtcServerHandler()
{ {
} }
ISrsRtcServerHijacker::ISrsRtcServerHijacker()
{
}
ISrsRtcServerHijacker::~ISrsRtcServerHijacker()
{
}
SrsRtcServer::SrsRtcServer() SrsRtcServer::SrsRtcServer()
{ {
handler = NULL; handler = NULL;
hijacker = NULL;
timer = new SrsHourGlass(this, 1 * SRS_UTIME_SECONDS); timer = new SrsHourGlass(this, 1 * SRS_UTIME_SECONDS);
} }
@ -257,6 +266,11 @@ void SrsRtcServer::set_handler(ISrsRtcServerHandler* h)
handler = h; handler = h;
} }
void SrsRtcServer::set_hijacker(ISrsRtcServerHijacker* h)
{
hijacker = h;
}
srs_error_t SrsRtcServer::listen_udp() srs_error_t SrsRtcServer::listen_udp()
{ {
srs_error_t err = srs_success; 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. // For STUN, the peer address may change.
if (srs_is_stun((uint8_t*)data, size)) { if (srs_is_stun((uint8_t*)data, size)) {
SrsStunPacket ping; SrsStunPacket ping;

View file

@ -72,6 +72,17 @@ public:
virtual void on_timeout(SrsRtcConnection* session) = 0; 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. // The RTC server instance, listen UDP port, handle UDP packet, manage RTC connections.
class SrsRtcServer : virtual public ISrsUdpMuxHandler, virtual public ISrsHourGlass class SrsRtcServer : virtual public ISrsUdpMuxHandler, virtual public ISrsHourGlass
{ {
@ -79,6 +90,7 @@ private:
SrsHourGlass* timer; SrsHourGlass* timer;
std::vector<SrsUdpMuxListener*> listeners; std::vector<SrsUdpMuxListener*> listeners;
ISrsRtcServerHandler* handler; ISrsRtcServerHandler* handler;
ISrsRtcServerHijacker* hijacker;
private: private:
// TODO: FIXME: Rename it. // TODO: FIXME: Rename it.
std::map<std::string, SrsRtcConnection*> map_username_session; // key: username(local_ufrag + ":" + remote_ufrag) std::map<std::string, SrsRtcConnection*> map_username_session; // key: username(local_ufrag + ":" + remote_ufrag)
@ -93,6 +105,7 @@ public:
virtual srs_error_t initialize(); virtual srs_error_t initialize();
// Set the handler for server events. // Set the handler for server events.
void set_handler(ISrsRtcServerHandler* h); void set_handler(ISrsRtcServerHandler* h);
void set_hijacker(ISrsRtcServerHijacker* h);
public: public:
// TODO: FIXME: Support gracefully quit. // TODO: FIXME: Support gracefully quit.
// TODO: FIXME: Support reload. // TODO: FIXME: Support reload.