Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / cast / net / rtcp / rtcp_utility.h
1 // Copyright 2014 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/net/rtcp/rtcp_defines.h"
12
13 namespace media {
14 namespace cast {
15
16 // RFC 3550 page 44, including end null.
17 static const size_t kRtcpCnameSize = 256;
18
19 static const uint32 kCast = ('C' << 24) + ('A' << 16) + ('S' << 8) + 'T';
20
21 static const uint8 kReceiverLogSubtype = 2;
22
23 static const size_t kRtcpMaxReceiverLogMessages = 256;
24 static const size_t kRtcpMaxCastLossFields = 100;
25
26 struct RtcpFieldReceiverReport {
27   // RFC 3550.
28   uint32 sender_ssrc;
29   uint8 number_of_report_blocks;
30 };
31
32 struct RtcpFieldSenderReport {
33   // RFC 3550.
34   uint32 sender_ssrc;
35   uint8 number_of_report_blocks;
36   uint32 ntp_most_significant;
37   uint32 ntp_least_significant;
38   uint32 rtp_timestamp;
39   uint32 sender_packet_count;
40   uint32 sender_octet_count;
41 };
42
43 struct RtcpFieldReportBlockItem {
44   // RFC 3550.
45   uint32 ssrc;
46   uint8 fraction_lost;
47   uint32 cumulative_number_of_packets_lost;
48   uint32 extended_highest_sequence_number;
49   uint32 jitter;
50   uint32 last_sender_report;
51   uint32 delay_last_sender_report;
52 };
53
54 struct RtcpFieldXr {
55   // RFC 3611.
56   uint32 sender_ssrc;
57 };
58
59 struct RtcpFieldXrRrtr {
60   // RFC 3611.
61   uint32 ntp_most_significant;
62   uint32 ntp_least_significant;
63 };
64
65 struct RtcpFieldXrDlrr {
66   // RFC 3611.
67   uint32 receivers_ssrc;
68   uint32 last_receiver_report;
69   uint32 delay_last_receiver_report;
70 };
71
72 struct RtcpFieldPayloadSpecificApplication {
73   uint32 sender_ssrc;
74   uint32 media_ssrc;
75 };
76
77 struct RtcpFieldPayloadSpecificCastItem {
78   uint8 last_frame_id;
79   uint8 number_of_lost_fields;
80   uint16 target_delay_ms;
81 };
82
83 struct RtcpFieldPayloadSpecificCastNackItem {
84   uint8 frame_id;
85   uint16 packet_id;
86   uint8 bitmask;
87 };
88
89 struct RtcpFieldApplicationSpecificCastReceiverLogItem {
90   uint32 sender_ssrc;
91   uint32 rtp_timestamp;
92   uint32 event_timestamp_base;
93   uint8 event;
94   union {
95     uint16 packet_id;
96     int16 delay_delta;
97   } delay_delta_or_packet_id;
98   uint16 event_timestamp_delta;
99 };
100
101 union RtcpField {
102   RtcpFieldReceiverReport receiver_report;
103   RtcpFieldSenderReport sender_report;
104   RtcpFieldReportBlockItem report_block_item;
105
106   RtcpFieldXr extended_report;
107   RtcpFieldXrRrtr rrtr;
108   RtcpFieldXrDlrr dlrr;
109
110   RtcpFieldPayloadSpecificApplication application_specific;
111   RtcpFieldPayloadSpecificCastItem cast_item;
112   RtcpFieldPayloadSpecificCastNackItem cast_nack_item;
113
114   RtcpFieldApplicationSpecificCastReceiverLogItem cast_receiver_log;
115 };
116
117 enum RtcpFieldTypes {
118   kRtcpNotValidCode,
119
120   // RFC 3550.
121   kRtcpRrCode,
122   kRtcpSrCode,
123   kRtcpReportBlockItemCode,
124
125   // RFC 3611.
126   kRtcpXrCode,
127   kRtcpXrRrtrCode,
128   kRtcpXrDlrrCode,
129   kRtcpXrUnknownItemCode,
130
131   // RFC 4585.
132   kRtcpPayloadSpecificAppCode,
133
134   // Application specific.
135   kRtcpPayloadSpecificCastCode,
136   kRtcpPayloadSpecificCastNackItemCode,
137   kRtcpApplicationSpecificCastReceiverLogCode,
138   kRtcpApplicationSpecificCastReceiverLogFrameCode,
139   kRtcpApplicationSpecificCastReceiverLogEventCode,
140 };
141
142 struct RtcpCommonHeader {
143   uint8 V;   // Version.
144   bool P;    // Padding.
145   uint8 IC;  // Item count / subtype.
146   uint8 PT;  // Packet Type.
147   uint16 length_in_octets;
148 };
149
150 class RtcpParser {
151  public:
152   RtcpParser(const uint8* rtcp_data, size_t rtcp_length);
153   ~RtcpParser();
154
155   RtcpFieldTypes FieldType() const;
156   const RtcpField& Field() const;
157
158   bool IsValid() const;
159
160   RtcpFieldTypes Begin();
161   RtcpFieldTypes Iterate();
162
163  private:
164   enum ParseState {
165     kStateTopLevel,     // Top level packet
166     kStateReportBlock,  // Sender/Receiver report report blocks.
167     kStateApplicationSpecificCastReceiverFrameLog,
168     kStateApplicationSpecificCastReceiverEventLog,
169     kStateExtendedReportBlock,
170     kStateExtendedReportDelaySinceLastReceiverReport,
171     kStatePayloadSpecificApplication,
172     kStatePayloadSpecificCast,      // Application specific Cast.
173     kStatePayloadSpecificCastNack,  // Application specific Nack for Cast.
174   };
175
176   bool RtcpParseCommonHeader(const uint8* begin,
177                              const uint8* end,
178                              RtcpCommonHeader* parsed_header) const;
179
180   void IterateTopLevel();
181   void IterateReportBlockItem();
182   void IterateCastReceiverLogFrame();
183   void IterateCastReceiverLogEvent();
184   void IterateExtendedReportItem();
185   void IterateExtendedReportDelaySinceLastReceiverReportItem();
186   void IteratePayloadSpecificAppItem();
187   void IteratePayloadSpecificCastItem();
188   void IteratePayloadSpecificCastNackItem();
189
190   void Validate();
191   void EndCurrentBlock();
192
193   bool ParseRR();
194   bool ParseSR();
195   bool ParseReportBlockItem();
196
197   bool ParseApplicationDefined(uint8 subtype);
198   bool ParseCastReceiverLogFrameItem();
199   bool ParseCastReceiverLogEventItem();
200
201   bool ParseExtendedReport();
202   bool ParseExtendedReportItem();
203   bool ParseExtendedReportReceiverReferenceTimeReport();
204   bool ParseExtendedReportDelaySinceLastReceiverReport();
205
206   bool ParseFeedBackCommon(const RtcpCommonHeader& header);
207   bool ParsePayloadSpecificAppItem();
208   bool ParsePayloadSpecificCastItem();
209   bool ParsePayloadSpecificCastNackItem();
210
211  private:
212   const uint8* const rtcp_data_begin_;
213   const uint8* const rtcp_data_end_;
214
215   bool valid_packet_;
216   const uint8* rtcp_data_;
217   const uint8* rtcp_block_end_;
218
219   ParseState state_;
220   uint8 number_of_blocks_;
221   RtcpFieldTypes field_type_;
222   RtcpField field_;
223
224   DISALLOW_COPY_AND_ASSIGN(RtcpParser);
225 };
226
227 // Converts a log event type to an integer value.
228 // NOTE: We have only allocated 4 bits to represent the type of event over the
229 // wire. Therefore, this function can only return values from 0 to 15.
230 uint8 ConvertEventTypeToWireFormat(CastLoggingEvent event);
231
232 // The inverse of |ConvertEventTypeToWireFormat()|.
233 CastLoggingEvent TranslateToLogEventFromWireFormat(uint8 event);
234
235 }  // namespace cast
236 }  // namespace media
237
238 #endif  // MEDIA_CAST_RTCP_RTCP_UTILITY_H_