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.
5 #ifndef MEDIA_CAST_CAST_DEFINES_H_
6 #define MEDIA_CAST_CAST_DEFINES_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "base/time/time.h"
19 const int64 kDontShowTimeoutMs
= 33;
20 const float kDefaultCongestionControlBackOff
= 0.875f
;
21 const uint32 kVideoFrequency
= 90000;
22 const int64 kSkippedFramesCheckPeriodkMs
= 10000;
23 const uint32 kStartFrameId
= GG_UINT32_C(0xffffffff);
25 // Number of skipped frames threshold in fps (as configured) per period above.
26 const int kSkippedFramesThreshold
= 3;
27 const size_t kMaxIpPacketSize
= 1500;
28 const int kStartRttMs
= 20;
29 const int64 kCastMessageUpdateIntervalMs
= 33;
30 const int64 kNackRepeatIntervalMs
= 30;
32 enum DefaultSettings
{
33 kDefaultAudioEncoderBitrate
= 0, // This means "auto," and may mean VBR.
34 kDefaultAudioSamplingRate
= 48000,
37 kDefaultMaxFrameRate
= 30,
38 kDefaultNumberOfVideoBuffers
= 1,
39 kDefaultRtcpIntervalMs
= 500,
40 kDefaultRtpHistoryMs
= 1000,
41 kDefaultRtpMaxDelayMs
= 100,
46 kNewPacketCompletingFrame
,
51 const uint16 kRtcpCastAllPacketsLost
= 0xffff;
53 const size_t kMinLengthOfRtcp
= 8;
55 // Basic RTP header + cast header.
56 const size_t kMinLengthOfRtp
= 12 + 6;
58 const size_t kAesBlockSize
= 16;
59 const size_t kAesKeySize
= 16;
61 // Each uint16 represents one packet id within a cast frame.
62 typedef std::set
<uint16
> PacketIdSet
;
63 // Each uint8 represents one cast frame.
64 typedef std::map
<uint8
, PacketIdSet
> MissingFramesAndPacketsMap
;
66 // TODO(pwestin): Re-factor the functions bellow into a class with static
69 // January 1970, in NTP seconds.
70 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
72 static const int64 kUnixEpochInNtpSeconds
= GG_INT64_C(2208988800);
74 // Magic fractional unit. Used to convert time (in microseconds) to/from
75 // fractional NTP seconds.
76 static const double kMagicFractionalUnit
= 4.294967296E3
;
78 inline bool IsNewerFrameId(uint32 frame_id
, uint32 prev_frame_id
) {
79 return (frame_id
!= prev_frame_id
) &&
80 static_cast<uint32
>(frame_id
- prev_frame_id
) < 0x80000000;
83 inline bool IsNewerRtpTimestamp(uint32 timestamp
, uint32 prev_timestamp
) {
84 return (timestamp
!= prev_timestamp
) &&
85 static_cast<uint32
>(timestamp
- prev_timestamp
) < 0x80000000;
88 inline bool IsOlderFrameId(uint32 frame_id
, uint32 prev_frame_id
) {
89 return (frame_id
== prev_frame_id
) || IsNewerFrameId(prev_frame_id
, frame_id
);
92 inline bool IsNewerPacketId(uint16 packet_id
, uint16 prev_packet_id
) {
93 return (packet_id
!= prev_packet_id
) &&
94 static_cast<uint16
>(packet_id
- prev_packet_id
) < 0x8000;
97 inline bool IsNewerSequenceNumber(uint16 sequence_number
,
98 uint16 prev_sequence_number
) {
99 // Same function as IsNewerPacketId just different data and name.
100 return IsNewerPacketId(sequence_number
, prev_sequence_number
);
103 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
104 // fractions of a second where 0x80000000 is half a second.
105 inline uint32
ConvertToNtpDiff(uint32 delay_seconds
, uint32 delay_fraction
) {
106 return ((delay_seconds
& 0x0000FFFF) << 16) +
107 ((delay_fraction
& 0xFFFF0000) >> 16);
110 inline base::TimeDelta
ConvertFromNtpDiff(uint32 ntp_delay
) {
111 uint32 delay_ms
= (ntp_delay
& 0x0000ffff) * 1000;
113 delay_ms
+= ((ntp_delay
& 0xffff0000) >> 16) * 1000;
114 return base::TimeDelta::FromMilliseconds(delay_ms
);
117 inline void ConvertTimeToFractions(int64 time_us
,
120 DCHECK_GE(time_us
, 0) << "Time must NOT be negative";
121 *seconds
= static_cast<uint32
>(time_us
/ base::Time::kMicrosecondsPerSecond
);
122 *fractions
= static_cast<uint32
>(
123 (time_us
% base::Time::kMicrosecondsPerSecond
) * kMagicFractionalUnit
);
126 inline void ConvertTimeTicksToNtp(const base::TimeTicks
& time
,
128 uint32
* ntp_fractions
) {
129 base::TimeDelta elapsed_since_unix_epoch
=
130 time
- base::TimeTicks::UnixEpoch();
132 int64 ntp_time_us
= elapsed_since_unix_epoch
.InMicroseconds() +
133 (kUnixEpochInNtpSeconds
* base::Time::kMicrosecondsPerSecond
);
135 ConvertTimeToFractions(ntp_time_us
, ntp_seconds
, ntp_fractions
);
138 inline base::TimeTicks
ConvertNtpToTimeTicks(uint32 ntp_seconds
,
139 uint32 ntp_fractions
) {
140 int64 ntp_time_us
= static_cast<int64
>(ntp_seconds
) *
141 base::Time::kMicrosecondsPerSecond
+
142 static_cast<int64
>(ntp_fractions
) / kMagicFractionalUnit
;
144 base::TimeDelta elapsed_since_unix_epoch
=
145 base::TimeDelta::FromMicroseconds(ntp_time_us
-
146 (kUnixEpochInNtpSeconds
* base::Time::kMicrosecondsPerSecond
));
147 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch
;
150 inline std::string
GetAesNonce(uint32 frame_id
, const std::string
& iv_mask
) {
151 std::string
aes_nonce(kAesBlockSize
, 0);
153 // Serializing frame_id in big-endian order (aes_nonce[8] is the most
154 // significant byte of frame_id).
155 aes_nonce
[11] = frame_id
& 0xff;
156 aes_nonce
[10] = (frame_id
>> 8) & 0xff;
157 aes_nonce
[9] = (frame_id
>> 16) & 0xff;
158 aes_nonce
[8] = (frame_id
>> 24) & 0xff;
160 for (size_t i
= 0; i
< kAesBlockSize
; ++i
) {
161 aes_nonce
[i
] ^= iv_mask
[i
];
166 inline uint32
GetVideoRtpTimestamp(const base::TimeTicks
& time_ticks
) {
167 base::TimeTicks zero_time
;
168 base::TimeDelta recorded_delta
= time_ticks
- zero_time
;
169 // Timestamp is in 90 KHz for video.
170 return static_cast<uint32
>(recorded_delta
.InMilliseconds() * 90);
176 #endif // MEDIA_CAST_CAST_DEFINES_H_