From 6485db2cfd3ec68ce0819c5320754708d0b4edc6 Mon Sep 17 00:00:00 2001 From: Aaro Altonen Date: Tue, 18 Jun 2019 09:18:57 +0300 Subject: [PATCH] Create separate routines for sending RTCP packets These routines convert the fields from host to network byte order and send the packet to all participants --- src/rtcp.cc | 90 +++++++++++++++++++++++++++++++++++++++++++++++------ src/rtcp.hh | 17 ++++++++++ 2 files changed, 98 insertions(+), 9 deletions(-) diff --git a/src/rtcp.cc b/src/rtcp.cc index a38e82c..631ce42 100644 --- a/src/rtcp.cc +++ b/src/rtcp.cc @@ -190,6 +190,85 @@ void kvz_rtp::rtcp::receiver_inc_processed_pkts(uint32_t sender_ssrc, size_t n) receiver_stats_[sender_ssrc]->processed_pkts += n; } +rtp_error_t kvz_rtp::rtcp::send_sender_report_packet(kvz_rtp::frame::rtcp_sender_frame *frame) +{ + if (!frame) + return RTP_INVALID_VALUE; + + uint16_t len = frame->header.length; + + /* RTCP header + SSRC */ + frame->header.length = htons(frame->header.length); + frame->sender_ssrc = htonl(frame->sender_ssrc); + + /* RTCP Sender Info */ + frame->s_info.ntp_msw = htonl(frame->s_info.ntp_msw); + frame->s_info.ntp_lsw = htonl(frame->s_info.ntp_lsw); + frame->s_info.rtp_ts = htonl(frame->s_info.rtp_ts); + frame->s_info.pkt_cnt = htonl(frame->s_info.pkt_cnt); + frame->s_info.byte_cnt = htonl(frame->s_info.byte_cnt); + + /* report block(s) */ + for (size_t i = 0; i < frame->header.report_cnt; ++i) { + frame->blocks[i].last_seq = htonl(frame->blocks[i].last_seq); + frame->blocks[i].jitter = htonl(frame->blocks[i].jitter); + frame->blocks[i].ssrc = htonl(frame->blocks[i].ssrc); + frame->blocks[i].lost = htonl(frame->blocks[i].lost); + frame->blocks[i].dlsr = htonl(frame->blocks[i].dlsr); + frame->blocks[i].lsr = htonl(frame->blocks[i].lsr); + } + + rtp_error_t ret = socket_.sendto((uint8_t *)frame, len, 0, NULL); + kvz_rtp::frame::dealloc_frame(frame); + + return ret; +} + +rtp_error_t kvz_rtp::rtcp::send_receiver_report_packet(kvz_rtp::frame::rtcp_receiver_frame *frame) +{ + if (!frame) + return RTP_INVALID_VALUE; + + uint16_t len = frame->header.length; + + /* rtcp header + ssrc */ + frame->header.length = htons(frame->header.length); + frame->sender_ssrc = htonl(frame->sender_ssrc); + + /* report block(s) */ + for (size_t i = 0; i < frame->header.report_cnt; ++i) { + frame->blocks[i].last_seq = htonl(frame->blocks[i].last_seq); + frame->blocks[i].jitter = htonl(frame->blocks[i].jitter); + frame->blocks[i].ssrc = htonl(frame->blocks[i].ssrc); + frame->blocks[i].lost = htonl(frame->blocks[i].lost); + frame->blocks[i].dlsr = htonl(frame->blocks[i].dlsr); + frame->blocks[i].lsr = htonl(frame->blocks[i].lsr); + } + + rtp_error_t ret = socket_.sendto((uint8_t *)frame, len, 0, NULL); + kvz_rtp::frame::dealloc_frame(frame); + + return ret; +} + +rtp_error_t kvz_rtp::rtcp::send_sdes_packet(kvz_rtp::frame::rtcp_sdes_frame *frame) +{ + (void)frame; + return RTP_OK; +} + +rtp_error_t kvz_rtp::rtcp::send_bye_packet(kvz_rtp::frame::rtcp_bye_frame *frame) +{ + (void)frame; + return RTP_OK; +} + +rtp_error_t kvz_rtp::rtcp::send_app_packet(kvz_rtp::frame::rtcp_app_frame *frame) +{ + (void)frame; + return RTP_OK; +} + rtp_error_t kvz_rtp::rtcp::generate_sender_report() { return RTP_OK; @@ -204,7 +283,6 @@ rtp_error_t kvz_rtp::rtcp::generate_sender_report() } size_t ptr = 0; - rtp_error_t ret = RTP_OK; uint64_t timestamp = tv_to_ntp(); frame->s_info.ntp_msw = timestamp >> 32; @@ -230,10 +308,7 @@ rtp_error_t kvz_rtp::rtcp::generate_sender_report() ptr++; } - ret = socket_.sendto((uint8_t *)frame, frame->header.length, 0, NULL); - kvz_rtp::frame::dealloc_frame(frame); - - return ret; + return send_sender_report_packet(frame); } rtp_error_t kvz_rtp::rtcp::generate_receiver_report() @@ -266,10 +341,7 @@ rtp_error_t kvz_rtp::rtcp::generate_receiver_report() ptr++; } - ret = socket_.sendto((uint8_t *)frame, frame->header.length, 0, NULL); - kvz_rtp::frame::dealloc_frame(frame); - - return ret; + return send_receiver_report_packet(frame); } rtp_error_t kvz_rtp::rtcp::generate_report() diff --git a/src/rtcp.hh b/src/rtcp.hh index dfd37b0..29f41c3 100644 --- a/src/rtcp.hh +++ b/src/rtcp.hh @@ -59,6 +59,22 @@ namespace kvz_rtp { * Return RTP_OK on success and RTP_ERROR on error */ rtp_error_t handle_incoming_packet(uint8_t *buffer, size_t size); + /* Send "frame" to all participants + * + * These routines will convert all necessary fields to network byte order + * + * NOTE: The "parameter" frame should NOT be used after calling this function. + * These routines will deallocate the frame after it's sent + * + * Return RTP_OK on success + * Return RTP_INVALID_VALUE if "frame" is somehow invalid + * Return RTP_SEND_ERROR if sending "frame" did not succeed */ + rtp_error_t send_sender_report_packet(kvz_rtp::frame::rtcp_sender_frame *frame); + rtp_error_t send_receiver_report_packet(kvz_rtp::frame::rtcp_receiver_frame *frame); + rtp_error_t send_sdes_packet(kvz_rtp::frame::rtcp_sdes_frame *frame); + rtp_error_t send_bye_packet(kvz_rtp::frame::rtcp_bye_frame *frame); + rtp_error_t send_app_packet(kvz_rtp::frame::rtcp_app_frame *frame); + /* Return reference to kvz_rtp::socket of the RTCP instance * Used by the rtcp_runner to listen to incoming */ const kvz_rtp::socket& get_socket() const; @@ -155,6 +171,7 @@ namespace kvz_rtp { /* statistics for RTCP Sender and Receiver Reports */ struct statistics sender_stats_; + std::map receiver_stats_; size_t num_receivers_; };