From 34b07066b357bf47ddad8d8bfab31fb4ce3cd9ba Mon Sep 17 00:00:00 2001 From: Aaro Altonen Date: Wed, 12 Aug 2020 11:47:12 +0300 Subject: [PATCH] Implement RTCP sender packet handler This handler only grabs the packet size and changes the role from receiver to sender if that hasn't been done yet --- include/rtcp.hh | 11 +++++++++-- src/media_stream.cc | 5 ++++- src/rtcp.cc | 31 ++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/rtcp.hh b/include/rtcp.hh index 87a296b..eb5f4b6 100644 --- a/include/rtcp.hh +++ b/include/rtcp.hh @@ -193,8 +193,15 @@ namespace uvg_rtp { rtp_error_t install_sdes_hook(void (*hook)(uvg_rtp::frame::rtcp_sdes_frame *)); rtp_error_t install_app_hook(void (*hook)(uvg_rtp::frame::rtcp_app_frame *)); - /* Update RTCP-related session statistics */ - static rtp_error_t packet_handler(void *arg, int flags, frame::rtp_frame **out); + /* Update RTCP-related sender statistics */ + rtp_error_t update_sender_stats(size_t pkt_size); + + /* Update RTCP-related receiver statistics */ + static rtp_error_t recv_packet_handler(void *arg, int flags, frame::rtp_frame **out); + + /* Update RTCP-related sender statistics */ + static rtp_error_t send_packet_handler_buf(void *arg, ssize_t len, void *buf); + static rtp_error_t send_packet_handler_vec(void *arg, std::vector>& buffers); private: static void rtcp_runner(rtcp *rtcp); diff --git a/src/media_stream.cc b/src/media_stream.cc index bf5ff76..b0fba9c 100644 --- a/src/media_stream.cc +++ b/src/media_stream.cc @@ -116,8 +116,11 @@ rtp_error_t uvg_rtp::media_stream::init() return RTP_MEMORY_ERROR; } + socket_.install_handler(rtcp_, rtcp_->send_packet_handler_buf); + socket_.install_handler(rtcp_, rtcp_->send_packet_handler_vec); + rtp_handler_key_ = pkt_dispatcher_->install_handler(rtp_->packet_handler); - pkt_dispatcher_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->packet_handler); + pkt_dispatcher_->install_aux_handler(rtp_handler_key_, rtcp_, rtcp_->recv_packet_handler); switch (fmt_) { case RTP_FORMAT_HEVC: diff --git a/src/rtcp.cc b/src/rtcp.cc index 3dce610..b5d6da3 100644 --- a/src/rtcp.cc +++ b/src/rtcp.cc @@ -251,6 +251,17 @@ rtp_error_t uvg_rtp::rtcp::init_new_participant(uvg_rtp::frame::rtp_frame *frame return ret; } +rtp_error_t uvg_rtp::rtcp::update_sender_stats(size_t pkt_size) +{ + if (our_role_ == RECEIVER) + our_role_ = SENDER; + + our_stats.sent_pkts += 1; + our_stats.sent_bytes += pkt_size; + + return RTP_OK; +} + rtp_error_t uvg_rtp::rtcp::init_participant_seq(uint32_t ssrc, uint16_t base_seq) { if (participants_.find(ssrc) == participants_.end()) @@ -398,7 +409,7 @@ void uvg_rtp::rtcp::update_session_statistics(uvg_rtp::frame::rtp_frame *frame) * have been received. * - it keeps track of participants' SSRCs and if a collision * is detected, the RTP context is updated */ -rtp_error_t uvg_rtp::rtcp::packet_handler(void *arg, int flags, frame::rtp_frame **out) +rtp_error_t uvg_rtp::rtcp::recv_packet_handler(void *arg, int flags, frame::rtp_frame **out) { (void)flags; @@ -427,6 +438,24 @@ rtp_error_t uvg_rtp::rtcp::packet_handler(void *arg, int flags, frame::rtp_frame return RTP_PKT_NOT_HANDLED; } +rtp_error_t uvg_rtp::rtcp::send_packet_handler_buf(void *arg, ssize_t len, void *buf) +{ + return ((uvg_rtp::rtcp *)arg)->update_sender_stats(len - uvg_rtp::frame::HEADER_SIZE_RTP); +} + +rtp_error_t uvg_rtp::rtcp::send_packet_handler_vec(void *arg, std::vector>& buffers) +{ + ssize_t pkt_size = -uvg_rtp::frame::HEADER_SIZE_RTP; + + for (auto& buffer : buffers) + pkt_size += buffer.first; + + if (pkt_size < 0) + return RTP_INVALID_VALUE; + + return ((uvg_rtp::rtcp *)arg)->update_sender_stats(pkt_size); +} + rtp_error_t uvg_rtp::rtcp::handle_incoming_packet(uint8_t *buffer, size_t size) { (void)size;