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 */
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);

View File

@ -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<uvgrtp::formats::media>(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_);
}

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,
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) {
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<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,
void* args)
rtp_error_t uvgrtp::reception_flow::new_install_getter(std::shared_ptr<std::atomic<std::uint32_t>> remote_ssrc,
std::function<rtp_error_t(uvgrtp::frame::rtp_frame**)> 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;

View File

@ -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<rtp_error_t(int, uint8_t*, size_t, frame::rtp_frame** out)> handler_rtp;
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;
struct renamethis_handler {
std::function<rtp_error_t(void*, int, uint8_t*, size_t, frame::rtp_frame** out)> 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<rtp_error_t(uvgrtp::frame::rtp_frame ** out)> 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<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(uvgrtp::frame::rtp_frame**)> getter,
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);
/* 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);
}
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);

View File

@ -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);
}

View File

@ -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:

View File

@ -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;

View File

@ -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);