rtcp_packet_parser.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. *
  10. */
  11. #ifndef TEST_RTCP_PACKET_PARSER_H_
  12. #define TEST_RTCP_PACKET_PARSER_H_
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include "api/array_view.h"
  16. #include "modules/rtp_rtcp/source/rtcp_packet/app.h"
  17. #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
  18. #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
  19. #include "modules/rtp_rtcp/source/rtcp_packet/extended_jitter_report.h"
  20. #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
  21. #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
  22. #include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
  23. #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
  24. #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
  25. #include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
  26. #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
  27. #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
  28. #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
  29. #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
  30. #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
  31. #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
  32. #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
  33. #include "rtc_base/checks.h"
  34. namespace webrtc {
  35. namespace test {
  36. // Parse RTCP packet of given type. Assumes RTCP header is valid and that there
  37. // is excatly one packet of correct type in the buffer.
  38. template <typename Packet>
  39. bool ParseSinglePacket(const uint8_t* buffer, size_t size, Packet* packet) {
  40. rtcp::CommonHeader header;
  41. RTC_CHECK(header.Parse(buffer, size));
  42. RTC_CHECK_EQ(size, header.NextPacket() - buffer);
  43. return packet->Parse(header);
  44. }
  45. // Same function, but takes raw buffer as single argument instead of pair.
  46. template <typename Packet>
  47. bool ParseSinglePacket(rtc::ArrayView<const uint8_t> buffer, Packet* packet) {
  48. return ParseSinglePacket(buffer.data(), buffer.size(), packet);
  49. }
  50. class RtcpPacketParser {
  51. public:
  52. // Keeps last parsed packet, count number of parsed packets of given type.
  53. template <typename TypedRtcpPacket>
  54. class PacketCounter : public TypedRtcpPacket {
  55. public:
  56. int num_packets() const { return num_packets_; }
  57. void Parse(const rtcp::CommonHeader& header) {
  58. if (TypedRtcpPacket::Parse(header))
  59. ++num_packets_;
  60. }
  61. bool Parse(const rtcp::CommonHeader& header, uint32_t* sender_ssrc) {
  62. const bool result = TypedRtcpPacket::Parse(header);
  63. if (result) {
  64. ++num_packets_;
  65. if (*sender_ssrc == 0) // Use first sender ssrc in compound packet.
  66. *sender_ssrc = TypedRtcpPacket::sender_ssrc();
  67. }
  68. return result;
  69. }
  70. private:
  71. int num_packets_ = 0;
  72. };
  73. RtcpPacketParser();
  74. ~RtcpPacketParser();
  75. bool Parse(const void* packet, size_t packet_len);
  76. PacketCounter<rtcp::App>* app() { return &app_; }
  77. PacketCounter<rtcp::Bye>* bye() { return &bye_; }
  78. PacketCounter<rtcp::ExtendedJitterReport>* ij() { return &ij_; }
  79. PacketCounter<rtcp::ExtendedReports>* xr() { return &xr_; }
  80. PacketCounter<rtcp::Fir>* fir() { return &fir_; }
  81. PacketCounter<rtcp::Nack>* nack() { return &nack_; }
  82. PacketCounter<rtcp::Pli>* pli() { return &pli_; }
  83. PacketCounter<rtcp::RapidResyncRequest>* rrr() { return &rrr_; }
  84. PacketCounter<rtcp::ReceiverReport>* receiver_report() {
  85. return &receiver_report_;
  86. }
  87. PacketCounter<rtcp::LossNotification>* loss_notification() {
  88. return &loss_notification_;
  89. }
  90. PacketCounter<rtcp::Remb>* remb() { return &remb_; }
  91. PacketCounter<rtcp::Sdes>* sdes() { return &sdes_; }
  92. PacketCounter<rtcp::SenderReport>* sender_report() { return &sender_report_; }
  93. PacketCounter<rtcp::Tmmbn>* tmmbn() { return &tmmbn_; }
  94. PacketCounter<rtcp::Tmmbr>* tmmbr() { return &tmmbr_; }
  95. PacketCounter<rtcp::TransportFeedback>* transport_feedback() {
  96. return &transport_feedback_;
  97. }
  98. uint32_t sender_ssrc() const { return sender_ssrc_; }
  99. size_t processed_rtcp_packets() const { return processed_rtcp_packets_; }
  100. private:
  101. PacketCounter<rtcp::App> app_;
  102. PacketCounter<rtcp::Bye> bye_;
  103. PacketCounter<rtcp::ExtendedJitterReport> ij_;
  104. PacketCounter<rtcp::ExtendedReports> xr_;
  105. PacketCounter<rtcp::Fir> fir_;
  106. PacketCounter<rtcp::Nack> nack_;
  107. PacketCounter<rtcp::Pli> pli_;
  108. PacketCounter<rtcp::RapidResyncRequest> rrr_;
  109. PacketCounter<rtcp::ReceiverReport> receiver_report_;
  110. PacketCounter<rtcp::LossNotification> loss_notification_;
  111. PacketCounter<rtcp::Remb> remb_;
  112. PacketCounter<rtcp::Sdes> sdes_;
  113. PacketCounter<rtcp::SenderReport> sender_report_;
  114. PacketCounter<rtcp::Tmmbn> tmmbn_;
  115. PacketCounter<rtcp::Tmmbr> tmmbr_;
  116. PacketCounter<rtcp::TransportFeedback> transport_feedback_;
  117. uint32_t sender_ssrc_ = 0;
  118. size_t processed_rtcp_packets_ = 0;
  119. };
  120. } // namespace test
  121. } // namespace webrtc
  122. #endif // TEST_RTCP_PACKET_PARSER_H_