• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #ifndef TEST_RTCP_PACKET_PARSER_H_
13 #define TEST_RTCP_PACKET_PARSER_H_
14 
15 #include <stddef.h>
16 #include <stdint.h>
17 
18 #include "api/array_view.h"
19 #include "modules/rtp_rtcp/source/rtcp_packet/app.h"
20 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
21 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
22 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
23 #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
24 #include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
29 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
30 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
31 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
32 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
33 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
34 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
35 #include "rtc_base/checks.h"
36 
37 namespace webrtc {
38 namespace test {
39 // Parse RTCP packet of given type. Assumes RTCP header is valid and that there
40 // is excatly one packet of correct type in the buffer.
41 template <typename Packet>
ParseSinglePacket(const uint8_t * buffer,size_t size,Packet * packet)42 bool ParseSinglePacket(const uint8_t* buffer, size_t size, Packet* packet) {
43   rtcp::CommonHeader header;
44   RTC_CHECK(header.Parse(buffer, size));
45   RTC_CHECK_EQ(size, header.NextPacket() - buffer);
46   return packet->Parse(header);
47 }
48 // Same function, but takes raw buffer as single argument instead of pair.
49 template <typename Packet>
ParseSinglePacket(rtc::ArrayView<const uint8_t> buffer,Packet * packet)50 bool ParseSinglePacket(rtc::ArrayView<const uint8_t> buffer, Packet* packet) {
51   return ParseSinglePacket(buffer.data(), buffer.size(), packet);
52 }
53 
54 class RtcpPacketParser {
55  public:
56   // Keeps last parsed packet, count number of parsed packets of given type.
57   template <typename TypedRtcpPacket>
58   class PacketCounter : public TypedRtcpPacket {
59    public:
num_packets()60     int num_packets() const { return num_packets_; }
Parse(const rtcp::CommonHeader & header)61     void Parse(const rtcp::CommonHeader& header) {
62       if (TypedRtcpPacket::Parse(header))
63         ++num_packets_;
64     }
Parse(const rtcp::CommonHeader & header,uint32_t * sender_ssrc)65     bool Parse(const rtcp::CommonHeader& header, uint32_t* sender_ssrc) {
66       const bool result = TypedRtcpPacket::Parse(header);
67       if (result) {
68         ++num_packets_;
69         if (*sender_ssrc == 0)  // Use first sender ssrc in compound packet.
70           *sender_ssrc = TypedRtcpPacket::sender_ssrc();
71       }
72       return result;
73     }
74 
75    private:
76     int num_packets_ = 0;
77   };
78 
79   RtcpPacketParser();
80   ~RtcpPacketParser();
81 
82   bool Parse(const void* packet, size_t packet_len);
83 
app()84   PacketCounter<rtcp::App>* app() { return &app_; }
bye()85   PacketCounter<rtcp::Bye>* bye() { return &bye_; }
xr()86   PacketCounter<rtcp::ExtendedReports>* xr() { return &xr_; }
fir()87   PacketCounter<rtcp::Fir>* fir() { return &fir_; }
nack()88   PacketCounter<rtcp::Nack>* nack() { return &nack_; }
pli()89   PacketCounter<rtcp::Pli>* pli() { return &pli_; }
rrr()90   PacketCounter<rtcp::RapidResyncRequest>* rrr() { return &rrr_; }
receiver_report()91   PacketCounter<rtcp::ReceiverReport>* receiver_report() {
92     return &receiver_report_;
93   }
loss_notification()94   PacketCounter<rtcp::LossNotification>* loss_notification() {
95     return &loss_notification_;
96   }
remb()97   PacketCounter<rtcp::Remb>* remb() { return &remb_; }
sdes()98   PacketCounter<rtcp::Sdes>* sdes() { return &sdes_; }
sender_report()99   PacketCounter<rtcp::SenderReport>* sender_report() { return &sender_report_; }
tmmbn()100   PacketCounter<rtcp::Tmmbn>* tmmbn() { return &tmmbn_; }
tmmbr()101   PacketCounter<rtcp::Tmmbr>* tmmbr() { return &tmmbr_; }
transport_feedback()102   PacketCounter<rtcp::TransportFeedback>* transport_feedback() {
103     return &transport_feedback_;
104   }
sender_ssrc()105   uint32_t sender_ssrc() const { return sender_ssrc_; }
processed_rtcp_packets()106   size_t processed_rtcp_packets() const { return processed_rtcp_packets_; }
107 
108  private:
109   PacketCounter<rtcp::App> app_;
110   PacketCounter<rtcp::Bye> bye_;
111   PacketCounter<rtcp::ExtendedReports> xr_;
112   PacketCounter<rtcp::Fir> fir_;
113   PacketCounter<rtcp::Nack> nack_;
114   PacketCounter<rtcp::Pli> pli_;
115   PacketCounter<rtcp::RapidResyncRequest> rrr_;
116   PacketCounter<rtcp::ReceiverReport> receiver_report_;
117   PacketCounter<rtcp::LossNotification> loss_notification_;
118   PacketCounter<rtcp::Remb> remb_;
119   PacketCounter<rtcp::Sdes> sdes_;
120   PacketCounter<rtcp::SenderReport> sender_report_;
121   PacketCounter<rtcp::Tmmbn> tmmbn_;
122   PacketCounter<rtcp::Tmmbr> tmmbr_;
123   PacketCounter<rtcp::TransportFeedback> transport_feedback_;
124   uint32_t sender_ssrc_ = 0;
125   size_t processed_rtcp_packets_ = 0;
126 };
127 
128 }  // namespace test
129 }  // namespace webrtc
130 #endif  // TEST_RTCP_PACKET_PARSER_H_
131