From 2d3451c9dff27a8d8b2d530b535eeae65c515175 Mon Sep 17 00:00:00 2001 From: Heikki Tampio Date: Thu, 15 Jun 2023 12:34:22 +0300 Subject: [PATCH] multiplex: Add new SRTP packet_handler --- include/uvgrtp/rtcp.hh | 2 +- src/media_stream.cc | 74 ++++++++++++++++++++++++++---------------- src/reception_flow.cc | 39 ++++++++++++++-------- src/reception_flow.hh | 25 +++++++------- src/rtcp.cc | 2 +- src/rtp.cc | 2 +- src/rtp.hh | 2 +- src/srtp/srtp.cc | 5 +++ src/srtp/srtp.hh | 1 + 9 files changed, 95 insertions(+), 57 deletions(-) diff --git a/include/uvgrtp/rtcp.hh b/include/uvgrtp/rtcp.hh index aefb6f4..edc1f2b 100644 --- a/include/uvgrtp/rtcp.hh +++ b/include/uvgrtp/rtcp.hh @@ -417,7 +417,7 @@ namespace uvgrtp { /* Update RTCP-related receiver statistics */ static rtp_error_t recv_packet_handler(void *arg, int rce_flags, frame::rtp_frame **out); - rtp_error_t new_recv_packet_handler(int rce_flags, uint8_t* read_ptr, size_t size); + rtp_error_t new_recv_packet_handler(void* args, int rce_flags, uint8_t* read_ptr, size_t size, frame::rtp_frame** out); /* Update RTCP-related sender statistics */ static rtp_error_t send_packet_handler_vec(void *arg, uvgrtp::buf_vec& buffers); diff --git a/src/media_stream.cc b/src/media_stream.cc index 72337ec..baf74a3 100644 --- a/src/media_stream.cc +++ b/src/media_stream.cc @@ -206,30 +206,42 @@ rtp_error_t uvgrtp::media_stream::create_media(rtp_format_t fmt) case RTP_FORMAT_H264: { uvgrtp::formats::h264* format_264 = new uvgrtp::formats::h264(socket_, rtp_, rce_flags_); - reception_flow_->new_install_handler2( - remote_ssrc_, - std::bind(&uvgrtp::formats::h264::new_packet_handler, format_264, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), - std::bind(&uvgrtp::formats::h264::frame_getter, format_264, std::placeholders::_1), nullptr); + reception_flow_->new_install_handler( + 5, remote_ssrc_, + std::bind(&uvgrtp::formats::h264::new_packet_handler, format_264, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), nullptr + ); + reception_flow_->new_install_getter(remote_ssrc_, + std::bind(&uvgrtp::formats::h264::frame_getter, format_264, std::placeholders::_1)); + media_.reset(format_264); break; } case RTP_FORMAT_H265: { uvgrtp::formats::h265* format_265 = new uvgrtp::formats::h265(socket_, rtp_, rce_flags_); - reception_flow_->new_install_handler2( - remote_ssrc_, - std::bind(&uvgrtp::formats::h265::new_packet_handler, format_265, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), - std::bind(&uvgrtp::formats::h265::frame_getter, format_265, std::placeholders::_1), nullptr); + reception_flow_->new_install_handler( + 5, remote_ssrc_, + std::bind(&uvgrtp::formats::h265::new_packet_handler, format_265, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), nullptr + ); + reception_flow_->new_install_getter(remote_ssrc_, + std::bind(&uvgrtp::formats::h265::frame_getter, format_265, std::placeholders::_1)); + media_.reset(format_265); break; } case RTP_FORMAT_H266: { uvgrtp::formats::h266* format_266 = new uvgrtp::formats::h266(socket_, rtp_, rce_flags_); - reception_flow_->new_install_handler2( - remote_ssrc_, - std::bind(&uvgrtp::formats::h266::new_packet_handler, format_266, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), - std::bind(&uvgrtp::formats::h266::frame_getter, format_266, std::placeholders::_1), nullptr); + reception_flow_->new_install_handler( + 5, remote_ssrc_, + std::bind(&uvgrtp::formats::h266::new_packet_handler, format_266, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), nullptr + ); + reception_flow_->new_install_getter(remote_ssrc_, + std::bind(&uvgrtp::formats::h266::frame_getter, format_266, std::placeholders::_1)); + media_.reset(format_266); break; } @@ -259,10 +271,10 @@ rtp_error_t uvgrtp::media_stream::create_media(rtp_format_t fmt) case RTP_FORMAT_VDVI: { media_ = std::unique_ptr(new uvgrtp::formats::media(socket_, rtp_, rce_flags_)); - reception_flow_->new_install_handler2( - remote_ssrc_, - std::bind(&uvgrtp::formats::media::new_packet_handler, media_.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), - nullptr, media_->get_media_frame_info()); + reception_flow_->new_install_handler( + 5, remote_ssrc_, + std::bind(&uvgrtp::formats::media::new_packet_handler, media_.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), media_->get_media_frame_info()); break; } default: @@ -318,16 +330,17 @@ rtp_error_t uvgrtp::media_stream::init() reception_flow_->install_aux_handler(rtp_handler_key_, rtcp_.get(), rtcp_->recv_packet_handler, nullptr); reception_flow_->new_install_handler( - 1, - remote_ssrc_, - std::bind(&uvgrtp::rtp::new_packet_handler, rtp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); + 1, remote_ssrc_, + std::bind(&uvgrtp::rtp::new_packet_handler, rtp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), + nullptr); if (rce_flags_ & RCE_RTCP_MUX) { - rtcp_->set_socket(socket_); - reception_flow_->new_install_handler( - 2, - remote_ssrc_, - std::bind(&uvgrtp::rtcp::new_recv_packet_handler, rtcp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + rtcp_->set_socket(socket_); + reception_flow_->new_install_handler( + 2, remote_ssrc_, + std::bind(&uvgrtp::rtcp::new_recv_packet_handler, rtcp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), nullptr); } return start_components(); @@ -438,11 +451,16 @@ rtp_error_t uvgrtp::media_stream::add_srtp_ctx(uint8_t *key, uint8_t *salt) socket_->install_handler(rtcp_.get(), rtcp_->send_packet_handler_vec); socket_->install_handler(srtp_.get(), srtp_->send_packet_handler); - rtp_handler_key_ = reception_flow_->install_handler(rtp_->packet_handler); - reception_flow_->map_handler_key(rtp_handler_key_, remote_ssrc_); + reception_flow_->new_install_handler( + 4, remote_ssrc_, + std::bind(&uvgrtp::srtp::new_recv_packet_handler, srtp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), srtp_.get()); - reception_flow_->install_aux_handler(rtp_handler_key_, rtcp_.get(), rtcp_->recv_packet_handler, nullptr); - reception_flow_->install_aux_handler(rtp_handler_key_, srtp_.get(), srtp_->recv_packet_handler, nullptr); + //rtp_handler_key_ = reception_flow_->install_handler(rtp_->packet_handler); + //reception_flow_->map_handler_key(rtp_handler_key_, remote_ssrc_); + + //reception_flow_->install_aux_handler(rtp_handler_key_, rtcp_.get(), rtcp_->recv_packet_handler, nullptr); + //reception_flow_->install_aux_handler(rtp_handler_key_, srtp_.get(), srtp_->recv_packet_handler, nullptr); if (rce_flags_ & RCE_RTCP_MUX) { rtcp_->set_socket(socket_); } diff --git a/src/reception_flow.cc b/src/reception_flow.cc index 98d5330..1804806 100644 --- a/src/reception_flow.cc +++ b/src/reception_flow.cc @@ -323,23 +323,32 @@ rtp_error_t uvgrtp::reception_flow::install_aux_handler( } rtp_error_t uvgrtp::reception_flow::new_install_handler(int type, std::shared_ptr> remote_ssrc, - std::function handler) + std::function handler, void* args) { switch (type) { case 1: { - NEW_packet_handlers_[remote_ssrc].handler_rtp = handler; + NEW_packet_handlers_[remote_ssrc].handler_rtp.handler = handler; + NEW_packet_handlers_[remote_ssrc].handler_rtp.args = args; break; } case 2: { - NEW_packet_handlers_[remote_ssrc].handler_rtcp = handler; + NEW_packet_handlers_[remote_ssrc].handler_rtcp.handler = handler; + NEW_packet_handlers_[remote_ssrc].handler_rtcp.args = args; break; } case 3: { - NEW_packet_handlers_[remote_ssrc].handler_zrtp = handler; + NEW_packet_handlers_[remote_ssrc].handler_zrtp.handler = handler; + NEW_packet_handlers_[remote_ssrc].handler_zrtp.args = args; break; } case 4: { - NEW_packet_handlers_[remote_ssrc].handler_srtp = handler; + NEW_packet_handlers_[remote_ssrc].handler_srtp.handler = handler; + NEW_packet_handlers_[remote_ssrc].handler_srtp.args = args; + break; + } + case 5: { + NEW_packet_handlers_[remote_ssrc].handler_media.handler = handler; + NEW_packet_handlers_[remote_ssrc].handler_media.args = args; break; } default: { @@ -350,14 +359,10 @@ rtp_error_t uvgrtp::reception_flow::new_install_handler(int type, std::shared_pt return RTP_OK; } -rtp_error_t uvgrtp::reception_flow::new_install_handler2(std::shared_ptr> remote_ssrc, - std::function handler, - std::function getter, - void* args) +rtp_error_t uvgrtp::reception_flow::new_install_getter(std::shared_ptr> remote_ssrc, + std::function getter) { - NEW_packet_handlers_[remote_ssrc].handler_media = handler; NEW_packet_handlers_[remote_ssrc].getter = getter; - NEW_packet_handlers_[remote_ssrc].args = args; return RTP_OK; } @@ -614,7 +619,7 @@ void uvgrtp::reception_flow::process_packet(int rce_flags) uint8_t pt = (uint8_t)ptr[1]; //UVG_LOG_DEBUG("Received frame with pt %u", pt); if (pt >= 200 && pt <= 204) { - retval = handlers->handler_rtcp(rce_flags, &ptr[0], size, &frame); + retval = handlers->handler_rtcp.handler(nullptr, rce_flags, &ptr[0], size, &frame); break; } } @@ -631,10 +636,16 @@ void uvgrtp::reception_flow::process_packet(int rce_flags) /* -------------------- RTP check ---------------------------------- */ else if (version == 0x2) { - retval = handlers->handler_rtp(rce_flags, &ptr[0], size, &frame); + retval = RTP_PKT_MODIFIED; + if (rce_flags & RCE_SRTP) { + retval = handlers->handler_srtp.handler(handlers->handler_srtp.args, rce_flags, &ptr[0], size, &frame); + } + if (retval == RTP_PKT_MODIFIED) { + retval = handlers->handler_rtp.handler(nullptr, rce_flags, &ptr[0], size, &frame); + } if (retval == RTP_PKT_MODIFIED) { - retval = handlers->handler_media(handlers->args, rce_flags, &ptr[0], size, &frame); + retval = handlers->handler_media.handler(handlers->handler_media.args, rce_flags, &ptr[0], size, &frame); if (retval == RTP_PKT_READY) { return_frame(frame); break; diff --git a/src/reception_flow.hh b/src/reception_flow.hh index c6cb64c..699b231 100644 --- a/src/reception_flow.hh +++ b/src/reception_flow.hh @@ -63,15 +63,18 @@ namespace uvgrtp { // -----------new packet handlers //typedef rtp_error_t(*packet_handler_new)(void*, int, uint8_t*, size_t); - struct handler_new { - std::function handler_rtp; - std::function handler_rtcp; - std::function handler_zrtp; - std::function handler_srtp; - std::function handler_media; - std::function getter; + struct renamethis_handler { + std::function handler; void* args = nullptr; }; + struct handler_new { + renamethis_handler handler_rtp; + renamethis_handler handler_rtcp; + renamethis_handler handler_zrtp; + renamethis_handler handler_srtp; + renamethis_handler handler_media; + std::function getter; + }; /* This class handles the reception processing of received RTP packets. It * utilizes function dispatching to other classes to achieve this. @@ -135,16 +138,16 @@ namespace uvgrtp { 2 rtcp 3 zrtp 4 srtp + 5 media getter can be nullptr if there is no getter (for media handlers mostly) */ rtp_error_t new_install_handler(int type, std::shared_ptr> remote_ssrc, - std::function handler); - - rtp_error_t new_install_handler2(std::shared_ptr> remote_ssrc, std::function handler, - std::function getter, void* args); + rtp_error_t new_install_getter(std::shared_ptr> remote_ssrc, + std::function getter); + rtp_error_t new_remove_handlers(std::shared_ptr> remote_ssrc); /* Install auxiliary handler for the packet diff --git a/src/rtcp.cc b/src/rtcp.cc index 28857e0..3dfd1de 100644 --- a/src/rtcp.cc +++ b/src/rtcp.cc @@ -1031,7 +1031,7 @@ void uvgrtp::rtcp::update_session_statistics(const uvgrtp::frame::rtp_frame *fra ((double)trans_difference - participants_[frame->header.ssrc]->stats.jitter); } -rtp_error_t uvgrtp::rtcp::new_recv_packet_handler(int rce_flags, uint8_t* read_ptr, size_t size) +rtp_error_t uvgrtp::rtcp::new_recv_packet_handler(void* args, int rce_flags, uint8_t* read_ptr, size_t size, frame::rtp_frame** out) { //UVG_LOG_INFO("RTCP packet handled from %u", remote_ssrc_.get()->load()); return handle_incoming_packet(read_ptr, size); diff --git a/src/rtp.cc b/src/rtp.cc index 7bc2a70..99e06b6 100644 --- a/src/rtp.cc +++ b/src/rtp.cc @@ -233,7 +233,7 @@ uint32_t uvgrtp::rtp::get_rtp_ts() const { return rtp_ts_; } -rtp_error_t uvgrtp::rtp::new_packet_handler(int rce_flags, uint8_t* read_ptr, size_t size, frame::rtp_frame** out) +rtp_error_t uvgrtp::rtp::new_packet_handler(void* args, int rce_flags, uint8_t* read_ptr, size_t size, frame::rtp_frame** out) { return packet_handler(size, (void*)read_ptr, rce_flags, out); } diff --git a/src/rtp.hh b/src/rtp.hh index 7c30d9f..249316e 100644 --- a/src/rtp.hh +++ b/src/rtp.hh @@ -45,7 +45,7 @@ namespace uvgrtp { /* Validates the RTP header pointed to by "packet" */ static rtp_error_t packet_handler(ssize_t size, void *packet, int rce_flags, frame::rtp_frame **out); - rtp_error_t new_packet_handler(int rce_flags, uint8_t* read_ptr, size_t size, frame::rtp_frame** out); + rtp_error_t new_packet_handler(void* args, int rce_flags, uint8_t* read_ptr, size_t size, frame::rtp_frame** out); private: diff --git a/src/srtp/srtp.cc b/src/srtp/srtp.cc index 30b85cb..febb989 100644 --- a/src/srtp/srtp.cc +++ b/src/srtp/srtp.cc @@ -46,6 +46,11 @@ rtp_error_t uvgrtp::srtp::encrypt(uint32_t ssrc, uint16_t seq, uint8_t *buffer, return RTP_OK; } +rtp_error_t uvgrtp::srtp::new_recv_packet_handler(void* args, int rce_flags, uint8_t* read_ptr, size_t size, uvgrtp::frame::rtp_frame** out) +{ + return recv_packet_handler(args, rce_flags, out); +} + rtp_error_t uvgrtp::srtp::recv_packet_handler(void *arg, int rce_flags, frame::rtp_frame **out) { (void)rce_flags; diff --git a/src/srtp/srtp.hh b/src/srtp/srtp.hh index 2c135b4..7291880 100644 --- a/src/srtp/srtp.hh +++ b/src/srtp/srtp.hh @@ -15,6 +15,7 @@ namespace uvgrtp { /* Decrypt the payload of an RTP packet and verify authentication tag (if enabled) */ static rtp_error_t recv_packet_handler(void *arg, int rce_flags, frame::rtp_frame **out); + rtp_error_t new_recv_packet_handler(void* args, int rce_flags, uint8_t* read_ptr, size_t size, uvgrtp::frame::rtp_frame** out); /* Encrypt the payload of an RTP packet and add authentication tag (if enabled) */ static rtp_error_t send_packet_handler(void *arg, buf_vec& buffers);