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_TRANSPORT_CAST_TRANSPORT_DEFINES_H_
6 #define MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_DEFINES_H_
7
8 #include <stdint.h>
9
10 #include <map>
11 #include <set>
12 #include <string>
13
14 #include "base/basictypes.h"
15 #include "base/time/time.h"
16
17 namespace media {
18 namespace cast {
19 namespace transport {
20
21 // TODO(mikhal): Implement and add more types.
22 enum CastTransportStatus {
23 TRANSPORT_AUDIO_UNINITIALIZED = 0,
24 TRANSPORT_VIDEO_UNINITIALIZED,
25 TRANSPORT_AUDIO_INITIALIZED,
26 TRANSPORT_VIDEO_INITIALIZED,
27 TRANSPORT_INVALID_CRYPTO_CONFIG,
28 TRANSPORT_SOCKET_ERROR,
29 CAST_TRANSPORT_STATUS_LAST = TRANSPORT_SOCKET_ERROR
30 };
31
32 const size_t kMaxIpPacketSize = 1500;
33 // Each uint16 represents one packet id within a cast frame.
34 typedef std::set<uint16> PacketIdSet;
35 // Each uint8 represents one cast frame.
36 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
37
38 // Crypto.
39 const size_t kAesBlockSize = 16;
40 const size_t kAesKeySize = 16;
41
GetAesNonce(uint32 frame_id,const std::string & iv_mask)42 inline std::string GetAesNonce(uint32 frame_id, const std::string& iv_mask) {
43 std::string aes_nonce(kAesBlockSize, 0);
44
45 // Serializing frame_id in big-endian order (aes_nonce[8] is the most
46 // significant byte of frame_id).
47 aes_nonce[11] = frame_id & 0xff;
48 aes_nonce[10] = (frame_id >> 8) & 0xff;
49 aes_nonce[9] = (frame_id >> 16) & 0xff;
50 aes_nonce[8] = (frame_id >> 24) & 0xff;
51
52 for (size_t i = 0; i < kAesBlockSize; ++i) {
53 aes_nonce[i] ^= iv_mask[i];
54 }
55 return aes_nonce;
56 }
57
58 // Rtcp defines.
59
60 enum RtcpPacketFields {
61 kPacketTypeLow = 194, // SMPTE time-code mapping.
62 kPacketTypeInterArrivalJitterReport = 195,
63 kPacketTypeSenderReport = 200,
64 kPacketTypeReceiverReport = 201,
65 kPacketTypeSdes = 202,
66 kPacketTypeBye = 203,
67 kPacketTypeApplicationDefined = 204,
68 kPacketTypeGenericRtpFeedback = 205,
69 kPacketTypePayloadSpecific = 206,
70 kPacketTypeXr = 207,
71 kPacketTypeHigh = 210, // Port Mapping.
72 };
73
74 enum RtcpPacketField {
75 kRtcpSr = 0x0002,
76 kRtcpRr = 0x0004,
77 kRtcpBye = 0x0008,
78 kRtcpPli = 0x0010,
79 kRtcpNack = 0x0020,
80 kRtcpFir = 0x0040,
81 kRtcpSrReq = 0x0200,
82 kRtcpDlrr = 0x0400,
83 kRtcpRrtr = 0x0800,
84 kRtcpRpsi = 0x8000,
85 kRtcpRemb = 0x10000,
86 kRtcpCast = 0x20000,
87 kRtcpSenderLog = 0x40000,
88 kRtcpReceiverLog = 0x80000,
89 };
90
91 // Each uint16 represents one packet id within a cast frame.
92 typedef std::set<uint16> PacketIdSet;
93 // Each uint8 represents one cast frame.
94 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
95
96 // TODO(miu): UGLY IN-LINE DEFINITION IN HEADER FILE! Move to appropriate
97 // location, separated into .h and .cc files.
98 class FrameIdWrapHelper {
99 public:
FrameIdWrapHelper()100 FrameIdWrapHelper()
101 : first_(true), frame_id_wrap_count_(0), range_(kLowRange) {}
102
MapTo32bitsFrameId(const uint8 over_the_wire_frame_id)103 uint32 MapTo32bitsFrameId(const uint8 over_the_wire_frame_id) {
104 if (first_) {
105 first_ = false;
106 if (over_the_wire_frame_id == 0xff) {
107 // Special case for startup.
108 return kStartFrameId;
109 }
110 }
111
112 uint32 wrap_count = frame_id_wrap_count_;
113 switch (range_) {
114 case kLowRange:
115 if (over_the_wire_frame_id > kLowRangeThreshold &&
116 over_the_wire_frame_id < kHighRangeThreshold) {
117 range_ = kMiddleRange;
118 }
119 if (over_the_wire_frame_id >= kHighRangeThreshold) {
120 // Wrap count was incremented in High->Low transition, but this frame
121 // is 'old', actually from before the wrap count got incremented.
122 --wrap_count;
123 }
124 break;
125 case kMiddleRange:
126 if (over_the_wire_frame_id >= kHighRangeThreshold) {
127 range_ = kHighRange;
128 }
129 break;
130 case kHighRange:
131 if (over_the_wire_frame_id <= kLowRangeThreshold) {
132 // Wrap-around detected.
133 range_ = kLowRange;
134 ++frame_id_wrap_count_;
135 // Frame triggering wrap-around so wrap count should be incremented as
136 // as well to match |frame_id_wrap_count_|.
137 ++wrap_count;
138 }
139 break;
140 }
141 return (wrap_count << 8) + over_the_wire_frame_id;
142 }
143
144 private:
145 enum Range { kLowRange, kMiddleRange, kHighRange, };
146
147 static const uint8 kLowRangeThreshold = 63;
148 static const uint8 kHighRangeThreshold = 192;
149 static const uint32 kStartFrameId = UINT32_C(0xffffffff);
150
151 bool first_;
152 uint32 frame_id_wrap_count_;
153 Range range_;
154
155 DISALLOW_COPY_AND_ASSIGN(FrameIdWrapHelper);
156 };
157
GetVideoRtpTimestamp(const base::TimeTicks & time_ticks)158 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) {
159 base::TimeTicks zero_time;
160 base::TimeDelta recorded_delta = time_ticks - zero_time;
161 // Timestamp is in 90 KHz for video.
162 return static_cast<uint32>(recorded_delta.InMilliseconds() * 90);
163 }
164
165 } // namespace transport
166 } // namespace cast
167 } // namespace media
168
169 #endif // MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_DEFINES_H_
170