multiplex: Add new SRTP packet_handler

This commit is contained in:
Heikki Tampio 2023-06-15 12:34:22 +03:00
parent d426d92e37
commit 2d3451c9df
9 changed files with 95 additions and 57 deletions

View File

@ -417,7 +417,7 @@ namespace uvgrtp {
/* Update RTCP-related receiver statistics */ /* Update RTCP-related receiver statistics */
static rtp_error_t recv_packet_handler(void *arg, int rce_flags, frame::rtp_frame **out); 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 */ /* Update RTCP-related sender statistics */
static rtp_error_t send_packet_handler_vec(void *arg, uvgrtp::buf_vec& buffers); static rtp_error_t send_packet_handler_vec(void *arg, uvgrtp::buf_vec& buffers);

View File

@ -206,30 +206,42 @@ rtp_error_t uvgrtp::media_stream::create_media(rtp_format_t fmt)
case RTP_FORMAT_H264: case RTP_FORMAT_H264:
{ {
uvgrtp::formats::h264* format_264 = new uvgrtp::formats::h264(socket_, rtp_, rce_flags_); uvgrtp::formats::h264* format_264 = new uvgrtp::formats::h264(socket_, rtp_, rce_flags_);
reception_flow_->new_install_handler2( reception_flow_->new_install_handler(
remote_ssrc_, 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), std::bind(&uvgrtp::formats::h264::new_packet_handler, format_264, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::bind(&uvgrtp::formats::h264::frame_getter, format_264, std::placeholders::_1), nullptr); 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); media_.reset(format_264);
break; break;
} }
case RTP_FORMAT_H265: case RTP_FORMAT_H265:
{ {
uvgrtp::formats::h265* format_265 = new uvgrtp::formats::h265(socket_, rtp_, rce_flags_); uvgrtp::formats::h265* format_265 = new uvgrtp::formats::h265(socket_, rtp_, rce_flags_);
reception_flow_->new_install_handler2( reception_flow_->new_install_handler(
remote_ssrc_, 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), std::bind(&uvgrtp::formats::h265::new_packet_handler, format_265, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::bind(&uvgrtp::formats::h265::frame_getter, format_265, std::placeholders::_1), nullptr); 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); media_.reset(format_265);
break; break;
} }
case RTP_FORMAT_H266: case RTP_FORMAT_H266:
{ {
uvgrtp::formats::h266* format_266 = new uvgrtp::formats::h266(socket_, rtp_, rce_flags_); uvgrtp::formats::h266* format_266 = new uvgrtp::formats::h266(socket_, rtp_, rce_flags_);
reception_flow_->new_install_handler2( reception_flow_->new_install_handler(
remote_ssrc_, 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), std::bind(&uvgrtp::formats::h266::new_packet_handler, format_266, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::bind(&uvgrtp::formats::h266::frame_getter, format_266, std::placeholders::_1), nullptr); 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); media_.reset(format_266);
break; break;
} }
@ -259,10 +271,10 @@ rtp_error_t uvgrtp::media_stream::create_media(rtp_format_t fmt)
case RTP_FORMAT_VDVI: case RTP_FORMAT_VDVI:
{ {
media_ = std::unique_ptr<uvgrtp::formats::media>(new uvgrtp::formats::media(socket_, rtp_, rce_flags_)); media_ = std::unique_ptr<uvgrtp::formats::media>(new uvgrtp::formats::media(socket_, rtp_, rce_flags_));
reception_flow_->new_install_handler2( reception_flow_->new_install_handler(
remote_ssrc_, 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), std::bind(&uvgrtp::formats::media::new_packet_handler, media_.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
nullptr, media_->get_media_frame_info()); std::placeholders::_4, std::placeholders::_5), media_->get_media_frame_info());
break; break;
} }
default: 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_->install_aux_handler(rtp_handler_key_, rtcp_.get(), rtcp_->recv_packet_handler, nullptr);
reception_flow_->new_install_handler( reception_flow_->new_install_handler(
1, 1, remote_ssrc_,
remote_ssrc_, std::bind(&uvgrtp::rtp::new_packet_handler, rtp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::bind(&uvgrtp::rtp::new_packet_handler, rtp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); std::placeholders::_4, std::placeholders::_5),
nullptr);
if (rce_flags_ & RCE_RTCP_MUX) { if (rce_flags_ & RCE_RTCP_MUX) {
rtcp_->set_socket(socket_); rtcp_->set_socket(socket_);
reception_flow_->new_install_handler( reception_flow_->new_install_handler(
2, 2, remote_ssrc_,
remote_ssrc_, std::bind(&uvgrtp::rtcp::new_recv_packet_handler, rtcp_, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
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(); 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(rtcp_.get(), rtcp_->send_packet_handler_vec);
socket_->install_handler(srtp_.get(), srtp_->send_packet_handler); socket_->install_handler(srtp_.get(), srtp_->send_packet_handler);
rtp_handler_key_ = reception_flow_->install_handler(rtp_->packet_handler); reception_flow_->new_install_handler(
reception_flow_->map_handler_key(rtp_handler_key_, remote_ssrc_); 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); //rtp_handler_key_ = reception_flow_->install_handler(rtp_->packet_handler);
reception_flow_->install_aux_handler(rtp_handler_key_, srtp_.get(), srtp_->recv_packet_handler, nullptr); //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) { if (rce_flags_ & RCE_RTCP_MUX) {
rtcp_->set_socket(socket_); rtcp_->set_socket(socket_);
} }

View File

@ -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<std::atomic<std::uint32_t>> remote_ssrc, rtp_error_t uvgrtp::reception_flow::new_install_handler(int type, std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc,
std::function<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler) std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> handler, void* args)
{ {
switch (type) { switch (type) {
case 1: { 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; break;
} }
case 2: { 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; break;
} }
case 3: { 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; break;
} }
case 4: { 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; break;
} }
default: { default: {
@ -350,14 +359,10 @@ rtp_error_t uvgrtp::reception_flow::new_install_handler(int type, std::shared_pt
return RTP_OK; return RTP_OK;
} }
rtp_error_t uvgrtp::reception_flow::new_install_handler2(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc, rtp_error_t uvgrtp::reception_flow::new_install_getter(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc,
std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> handler, std::function<rtp_error_t(uvgrtp::frame::rtp_frame**)> getter)
std::function<rtp_error_t(uvgrtp::frame::rtp_frame**)> getter,
void* args)
{ {
NEW_packet_handlers_[remote_ssrc].handler_media = handler;
NEW_packet_handlers_[remote_ssrc].getter = getter; NEW_packet_handlers_[remote_ssrc].getter = getter;
NEW_packet_handlers_[remote_ssrc].args = args;
return RTP_OK; return RTP_OK;
} }
@ -614,7 +619,7 @@ void uvgrtp::reception_flow::process_packet(int rce_flags)
uint8_t pt = (uint8_t)ptr[1]; uint8_t pt = (uint8_t)ptr[1];
//UVG_LOG_DEBUG("Received frame with pt %u", pt); //UVG_LOG_DEBUG("Received frame with pt %u", pt);
if (pt >= 200 && pt <= 204) { 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; break;
} }
} }
@ -631,10 +636,16 @@ void uvgrtp::reception_flow::process_packet(int rce_flags)
/* -------------------- RTP check ---------------------------------- */ /* -------------------- RTP check ---------------------------------- */
else if (version == 0x2) { 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) { 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) { if (retval == RTP_PKT_READY) {
return_frame(frame); return_frame(frame);
break; break;

View File

@ -63,15 +63,18 @@ namespace uvgrtp {
// -----------new packet handlers // -----------new packet handlers
//typedef rtp_error_t(*packet_handler_new)(void*, int, uint8_t*, size_t); //typedef rtp_error_t(*packet_handler_new)(void*, int, uint8_t*, size_t);
struct handler_new { struct renamethis_handler {
std::function<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler_rtp; std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> handler;
std::function<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler_rtcp;
std::function<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler_zrtp;
std::function<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler_srtp;
std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> handler_media;
std::function<rtp_error_t(uvgrtp::frame::rtp_frame ** out)> getter;
void* args = nullptr; 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<rtp_error_t(uvgrtp::frame::rtp_frame ** out)> getter;
};
/* This class handles the reception processing of received RTP packets. It /* This class handles the reception processing of received RTP packets. It
* utilizes function dispatching to other classes to achieve this. * utilizes function dispatching to other classes to achieve this.
@ -135,16 +138,16 @@ namespace uvgrtp {
2 rtcp 2 rtcp
3 zrtp 3 zrtp
4 srtp 4 srtp
5 media
getter can be nullptr if there is no getter (for media handlers mostly) getter can be nullptr if there is no getter (for media handlers mostly)
*/ */
rtp_error_t new_install_handler(int type, std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc, rtp_error_t new_install_handler(int type, std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc,
std::function<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler);
rtp_error_t new_install_handler2(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc,
std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> handler, std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> handler,
std::function<rtp_error_t(uvgrtp::frame::rtp_frame**)> getter,
void* args); void* args);
rtp_error_t new_install_getter(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc,
std::function<rtp_error_t(uvgrtp::frame::rtp_frame**)> getter);
rtp_error_t new_remove_handlers(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc); rtp_error_t new_remove_handlers(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc);
/* Install auxiliary handler for the packet /* Install auxiliary handler for the packet

View File

@ -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); ((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()); //UVG_LOG_INFO("RTCP packet handled from %u", remote_ssrc_.get()->load());
return handle_incoming_packet(read_ptr, size); return handle_incoming_packet(read_ptr, size);

View File

@ -233,7 +233,7 @@ uint32_t uvgrtp::rtp::get_rtp_ts() const {
return rtp_ts_; 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); return packet_handler(size, (void*)read_ptr, rce_flags, out);
} }

View File

@ -45,7 +45,7 @@ namespace uvgrtp {
/* Validates the RTP header pointed to by "packet" */ /* 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); 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: private:

View File

@ -46,6 +46,11 @@ rtp_error_t uvgrtp::srtp::encrypt(uint32_t ssrc, uint16_t seq, uint8_t *buffer,
return RTP_OK; 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) rtp_error_t uvgrtp::srtp::recv_packet_handler(void *arg, int rce_flags, frame::rtp_frame **out)
{ {
(void)rce_flags; (void)rce_flags;

View File

@ -15,6 +15,7 @@ namespace uvgrtp {
/* Decrypt the payload of an RTP packet and verify authentication tag (if enabled) */ /* 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); 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) */ /* 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); static rtp_error_t send_packet_handler(void *arg, buf_vec& buffers);