• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_CAST_RTCP_RTCP_UTILITY_H_
6 #define MEDIA_CAST_RTCP_RTCP_UTILITY_H_
7 
8 #include "media/cast/cast_config.h"
9 #include "media/cast/cast_defines.h"
10 #include "media/cast/logging/logging_defines.h"
11 #include "media/cast/rtcp/rtcp_defines.h"
12 
13 namespace media {
14 namespace cast {
15 
16 static const int kRtcpRpsiDataSize = 30;
17 
18 // RFC 3550 page 44, including end null.
19 static const size_t kRtcpCnameSize = 256;
20 static const int kRtcpMaxNumberOfRembFeedbackSsrcs = 255;
21 
22 static const uint32 kRemb = ('R' << 24) + ('E' << 16) + ('M' << 8) + 'B';
23 static const uint32 kCast = ('C' << 24) + ('A' << 16) + ('S' << 8) + 'T';
24 
25 static const uint8 kReceiverLogSubtype = 2;
26 
27 static const size_t kRtcpMaxReceiverLogMessages = 256;
28 static const size_t kRtcpMaxNackFields = 253;
29 static const size_t kRtcpMaxCastLossFields = 100;
30 
31 struct RtcpFieldReceiverReport {
32   // RFC 3550.
33   uint32 sender_ssrc;
34   uint8 number_of_report_blocks;
35 };
36 
37 struct RtcpFieldSenderReport {
38   // RFC 3550.
39   uint32 sender_ssrc;
40   uint8 number_of_report_blocks;
41   uint32 ntp_most_significant;
42   uint32 ntp_least_significant;
43   uint32 rtp_timestamp;
44   uint32 sender_packet_count;
45   uint32 sender_octet_count;
46 };
47 
48 struct RtcpFieldReportBlockItem {
49   // RFC 3550.
50   uint32 ssrc;
51   uint8 fraction_lost;
52   uint32 cumulative_number_of_packets_lost;
53   uint32 extended_highest_sequence_number;
54   uint32 jitter;
55   uint32 last_sender_report;
56   uint32 delay_last_sender_report;
57 };
58 
59 struct RtcpFieldSdesCName {
60   // RFC 3550
61   uint32 sender_ssrc;
62   char name[kRtcpCnameSize];
63 };
64 
65 struct RtcpFieldBye {
66   // RFC 3550.
67   uint32 sender_ssrc;
68 };
69 
70 struct RtcpFieldGenericRtpFeedbackNack {
71   // RFC 4585.
72   uint32 sender_ssrc;
73   uint32 media_ssrc;
74 };
75 
76 struct RtcpFieldGenericRtpFeedbackNackItem {
77   // RFC 4585.
78   uint16 packet_id;
79   uint16 bitmask;
80 };
81 
82 struct RtcpFieldPayloadSpecificFir {
83   // RFC 5104.
84   uint32 sender_ssrc;
85   uint32 media_ssrc;  // zero!
86 };
87 
88 struct RtcpFieldPayloadSpecificFirItem {
89   // RFC 5104.
90   uint32 ssrc;
91   uint8 command_sequence_number;
92 };
93 
94 struct RtcpFieldPayloadSpecificPli {
95   // RFC 4585.
96   uint32 sender_ssrc;
97   uint32 media_ssrc;
98 };
99 
100 struct RtcpFieldPayloadSpecificRpsi {
101   // RFC 4585.
102   uint32 sender_ssrc;
103   uint32 media_ssrc;
104   uint8 payload_type;
105   uint16 number_of_valid_bits;
106   uint8 native_bit_string[kRtcpRpsiDataSize];
107 };
108 
109 struct RtcpFieldXr {
110   // RFC 3611.
111   uint32 sender_ssrc;
112 };
113 
114 struct RtcpFieldXrRrtr {
115   // RFC 3611.
116   uint32 ntp_most_significant;
117   uint32 ntp_least_significant;
118 };
119 
120 struct RtcpFieldXrDlrr {
121   // RFC 3611.
122   uint32 receivers_ssrc;
123   uint32 last_receiver_report;
124   uint32 delay_last_receiver_report;
125 };
126 
127 struct RtcpFieldPayloadSpecificApplication {
128   uint32 sender_ssrc;
129   uint32 media_ssrc;
130 };
131 
132 struct RtcpFieldPayloadSpecificRembItem {
133   uint32 bitrate;
134   uint8 number_of_ssrcs;
135   uint32 ssrcs[kRtcpMaxNumberOfRembFeedbackSsrcs];
136 };
137 
138 struct RtcpFieldPayloadSpecificCastItem {
139   uint8 last_frame_id;
140   uint8 number_of_lost_fields;
141   uint16 target_delay_ms;
142 };
143 
144 struct RtcpFieldPayloadSpecificCastNackItem {
145   uint8 frame_id;
146   uint16 packet_id;
147   uint8 bitmask;
148 };
149 
150 struct RtcpFieldApplicationSpecificCastReceiverLogItem {
151   uint32 sender_ssrc;
152   uint32 rtp_timestamp;
153   uint32 event_timestamp_base;
154   uint8 event;
155   union {
156     uint16 packet_id;
157     int16 delay_delta;
158   } delay_delta_or_packet_id;
159   uint16 event_timestamp_delta;
160 };
161 
162 union RtcpField {
163   RtcpFieldReceiverReport receiver_report;
164   RtcpFieldSenderReport sender_report;
165   RtcpFieldReportBlockItem report_block_item;
166   RtcpFieldSdesCName c_name;
167   RtcpFieldBye bye;
168 
169   RtcpFieldXr extended_report;
170   RtcpFieldXrRrtr rrtr;
171   RtcpFieldXrDlrr dlrr;
172 
173   RtcpFieldGenericRtpFeedbackNack nack;
174   RtcpFieldGenericRtpFeedbackNackItem nack_item;
175 
176   RtcpFieldPayloadSpecificPli pli;
177   RtcpFieldPayloadSpecificRpsi rpsi;
178   RtcpFieldPayloadSpecificFir fir;
179   RtcpFieldPayloadSpecificFirItem fir_item;
180   RtcpFieldPayloadSpecificApplication application_specific;
181   RtcpFieldPayloadSpecificRembItem remb_item;
182   RtcpFieldPayloadSpecificCastItem cast_item;
183   RtcpFieldPayloadSpecificCastNackItem cast_nack_item;
184 
185   RtcpFieldApplicationSpecificCastReceiverLogItem cast_receiver_log;
186 };
187 
188 enum RtcpFieldTypes {
189   kRtcpNotValidCode,
190 
191   // RFC 3550.
192   kRtcpRrCode,
193   kRtcpSrCode,
194   kRtcpReportBlockItemCode,
195   kRtcpSdesCode,
196   kRtcpSdesChunkCode,
197   kRtcpByeCode,
198 
199   // RFC 3611.
200   kRtcpXrCode,
201   kRtcpXrRrtrCode,
202   kRtcpXrDlrrCode,
203   kRtcpXrUnknownItemCode,
204 
205   // RFC 4585.
206   kRtcpGenericRtpFeedbackNackCode,
207   kRtcpGenericRtpFeedbackNackItemCode,
208   kRtcpPayloadSpecificPliCode,
209   kRtcpPayloadSpecificRpsiCode,
210   kRtcpPayloadSpecificAppCode,
211 
212   // Application specific.
213   kRtcpPayloadSpecificRembCode,
214   kRtcpPayloadSpecificRembItemCode,
215   kRtcpPayloadSpecificCastCode,
216   kRtcpPayloadSpecificCastNackItemCode,
217   kRtcpApplicationSpecificCastReceiverLogCode,
218   kRtcpApplicationSpecificCastReceiverLogFrameCode,
219   kRtcpApplicationSpecificCastReceiverLogEventCode,
220 
221   // RFC 5104.
222   kRtcpPayloadSpecificFirCode,
223   kRtcpPayloadSpecificFirItemCode,
224 
225   // RFC 6051.
226   kRtcpGenericRtpFeedbackSrReqCode,
227 };
228 
229 struct RtcpCommonHeader {
230   uint8 V;   // Version.
231   bool P;    // Padding.
232   uint8 IC;  // Item count / subtype.
233   uint8 PT;  // Packet Type.
234   uint16 length_in_octets;
235 };
236 
237 class RtcpParser {
238  public:
239   RtcpParser(const uint8* rtcp_data, size_t rtcp_length);
240   ~RtcpParser();
241 
242   RtcpFieldTypes FieldType() const;
243   const RtcpField& Field() const;
244 
245   bool IsValid() const;
246 
247   RtcpFieldTypes Begin();
248   RtcpFieldTypes Iterate();
249 
250  private:
251   enum ParseState {
252     kStateTopLevel,     // Top level packet
253     kStateReportBlock,  // Sender/Receiver report report blocks.
254     kStateSdes,
255     kStateBye,
256     kStateApplicationSpecificCastReceiverFrameLog,
257     kStateApplicationSpecificCastReceiverEventLog,
258     kStateExtendedReportBlock,
259     kStateExtendedReportDelaySinceLastReceiverReport,
260     kStateGenericRtpFeedbackNack,
261     kStatePayloadSpecificRpsi,
262     kStatePayloadSpecificFir,
263     kStatePayloadSpecificApplication,
264     kStatePayloadSpecificRemb,      // Application specific Remb.
265     kStatePayloadSpecificCast,      // Application specific Cast.
266     kStatePayloadSpecificCastNack,  // Application specific Nack for Cast.
267   };
268 
269   bool RtcpParseCommonHeader(const uint8* begin,
270                              const uint8* end,
271                              RtcpCommonHeader* parsed_header) const;
272 
273   void IterateTopLevel();
274   void IterateReportBlockItem();
275   void IterateSdesItem();
276   void IterateByeItem();
277   void IterateCastReceiverLogFrame();
278   void IterateCastReceiverLogEvent();
279   void IterateExtendedReportItem();
280   void IterateExtendedReportDelaySinceLastReceiverReportItem();
281   void IterateNackItem();
282   void IterateRpsiItem();
283   void IterateFirItem();
284   void IteratePayloadSpecificAppItem();
285   void IteratePayloadSpecificRembItem();
286   void IteratePayloadSpecificCastItem();
287   void IteratePayloadSpecificCastNackItem();
288 
289   void Validate();
290   void EndCurrentBlock();
291 
292   bool ParseRR();
293   bool ParseSR();
294   bool ParseReportBlockItem();
295 
296   bool ParseSdes();
297   bool ParseSdesItem();
298   bool ParseSdesTypes();
299   bool ParseBye();
300   bool ParseByeItem();
301   bool ParseApplicationDefined(uint8 subtype);
302   bool ParseCastReceiverLogFrameItem();
303   bool ParseCastReceiverLogEventItem();
304 
305   bool ParseExtendedReport();
306   bool ParseExtendedReportItem();
307   bool ParseExtendedReportReceiverReferenceTimeReport();
308   bool ParseExtendedReportDelaySinceLastReceiverReport();
309 
310   bool ParseFeedBackCommon(const RtcpCommonHeader& header);
311   bool ParseNackItem();
312   bool ParseRpsiItem();
313   bool ParseFirItem();
314   bool ParsePayloadSpecificAppItem();
315   bool ParsePayloadSpecificRembItem();
316   bool ParsePayloadSpecificCastItem();
317   bool ParsePayloadSpecificCastNackItem();
318 
319  private:
320   const uint8* const rtcp_data_begin_;
321   const uint8* const rtcp_data_end_;
322 
323   bool valid_packet_;
324   const uint8* rtcp_data_;
325   const uint8* rtcp_block_end_;
326 
327   ParseState state_;
328   uint8 number_of_blocks_;
329   RtcpFieldTypes field_type_;
330   RtcpField field_;
331 
332   DISALLOW_COPY_AND_ASSIGN(RtcpParser);
333 };
334 
335 // Converts a log event type to an integer value.
336 // NOTE: We have only allocated 4 bits to represent the type of event over the
337 // wire. Therefore, this function can only return values from 0 to 15.
338 uint8 ConvertEventTypeToWireFormat(CastLoggingEvent event);
339 
340 // The inverse of |ConvertEventTypeToWireFormat()|.
341 CastLoggingEvent TranslateToLogEventFromWireFormat(uint8 event);
342 
343 }  // namespace cast
344 }  // namespace media
345 
346 #endif  // MEDIA_CAST_RTCP_RTCP_UTILITY_H_
347