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_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/logging.h"
16 #include "base/time/time.h"
17 #include "media/cast/net/cast_transport_config.h"
22 const int64 kDontShowTimeoutMs
= 33;
23 const float kDefaultCongestionControlBackOff
= 0.875f
;
24 const uint32 kVideoFrequency
= 90000;
25 const uint32 kStartFrameId
= UINT32_C(0xffffffff);
27 // This is an important system-wide constant. This limits how much history the
28 // implementation must retain in order to process the acknowledgements of past
30 const int kMaxUnackedFrames
= 255;
32 const int kStartRttMs
= 20;
33 const int64 kCastMessageUpdateIntervalMs
= 33;
34 const int64 kNackRepeatIntervalMs
= 30;
36 enum CastInitializationStatus
{
37 STATUS_AUDIO_UNINITIALIZED
,
38 STATUS_VIDEO_UNINITIALIZED
,
39 STATUS_AUDIO_INITIALIZED
,
40 STATUS_VIDEO_INITIALIZED
,
41 STATUS_INVALID_CAST_ENVIRONMENT
,
42 STATUS_INVALID_CRYPTO_CONFIGURATION
,
43 STATUS_UNSUPPORTED_AUDIO_CODEC
,
44 STATUS_UNSUPPORTED_VIDEO_CODEC
,
45 STATUS_INVALID_AUDIO_CONFIGURATION
,
46 STATUS_INVALID_VIDEO_CONFIGURATION
,
47 STATUS_GPU_ACCELERATION_NOT_SUPPORTED
,
48 STATUS_GPU_ACCELERATION_ERROR
,
51 enum DefaultSettings
{
52 kDefaultAudioEncoderBitrate
= 0, // This means "auto," and may mean VBR.
53 kDefaultAudioSamplingRate
= 48000,
56 kDefaultMaxFrameRate
= 30,
57 kDefaultNumberOfVideoBuffers
= 1,
58 kDefaultRtcpIntervalMs
= 500,
59 kDefaultRtpHistoryMs
= 1000,
60 kDefaultRtpMaxDelayMs
= 100,
65 kNewPacketCompletingFrame
,
70 // kRtcpCastAllPacketsLost is used in PacketIDSet and
71 // on the wire to mean that ALL packets for a particular
73 const uint16 kRtcpCastAllPacketsLost
= 0xffff;
75 // kRtcpCastLastPacket is used in PacketIDSet to ask for
76 // the last packet of a frame to be retransmitted.
77 const uint16 kRtcpCastLastPacket
= 0xfffe;
79 const size_t kMinLengthOfRtcp
= 8;
81 // Basic RTP header + cast header.
82 const size_t kMinLengthOfRtp
= 12 + 6;
84 // Each uint16 represents one packet id within a cast frame.
85 // Can also contain kRtcpCastAllPacketsLost and kRtcpCastLastPacket.
86 typedef std::set
<uint16
> PacketIdSet
;
87 // Each uint8 represents one cast frame.
88 typedef std::map
<uint8
, PacketIdSet
> MissingFramesAndPacketsMap
;
90 // TODO(pwestin): Re-factor the functions bellow into a class with static
93 // January 1970, in NTP seconds.
94 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
96 static const int64 kUnixEpochInNtpSeconds
= INT64_C(2208988800);
98 // Magic fractional unit. Used to convert time (in microseconds) to/from
99 // fractional NTP seconds.
100 static const double kMagicFractionalUnit
= 4.294967296E3
;
102 // The maximum number of Cast receiver events to keep in history for the
103 // purpose of sending the events through RTCP.
104 // The number chosen should be more than the number of events that can be
105 // stored in a RTCP packet.
106 static const size_t kReceiverRtcpEventHistorySize
= 512;
108 inline bool IsNewerFrameId(uint32 frame_id
, uint32 prev_frame_id
) {
109 return (frame_id
!= prev_frame_id
) &&
110 static_cast<uint32
>(frame_id
- prev_frame_id
) < 0x80000000;
113 inline bool IsNewerRtpTimestamp(uint32 timestamp
, uint32 prev_timestamp
) {
114 return (timestamp
!= prev_timestamp
) &&
115 static_cast<uint32
>(timestamp
- prev_timestamp
) < 0x80000000;
118 inline bool IsOlderFrameId(uint32 frame_id
, uint32 prev_frame_id
) {
119 return (frame_id
== prev_frame_id
) || IsNewerFrameId(prev_frame_id
, frame_id
);
122 inline bool IsNewerPacketId(uint16 packet_id
, uint16 prev_packet_id
) {
123 return (packet_id
!= prev_packet_id
) &&
124 static_cast<uint16
>(packet_id
- prev_packet_id
) < 0x8000;
127 inline bool IsNewerSequenceNumber(uint16 sequence_number
,
128 uint16 prev_sequence_number
) {
129 // Same function as IsNewerPacketId just different data and name.
130 return IsNewerPacketId(sequence_number
, prev_sequence_number
);
133 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
134 // fractions of a second where 0x80000000 is half a second.
135 inline uint32
ConvertToNtpDiff(uint32 delay_seconds
, uint32 delay_fraction
) {
136 return ((delay_seconds
& 0x0000FFFF) << 16) +
137 ((delay_fraction
& 0xFFFF0000) >> 16);
140 inline base::TimeDelta
ConvertFromNtpDiff(uint32 ntp_delay
) {
141 uint32 delay_ms
= (ntp_delay
& 0x0000ffff) * 1000;
143 delay_ms
+= ((ntp_delay
& 0xffff0000) >> 16) * 1000;
144 return base::TimeDelta::FromMilliseconds(delay_ms
);
147 inline void ConvertTimeToFractions(int64 ntp_time_us
,
150 DCHECK_GE(ntp_time_us
, 0) << "Time must NOT be negative";
151 const int64 seconds_component
=
152 ntp_time_us
/ base::Time::kMicrosecondsPerSecond
;
153 // NTP time will overflow in the year 2036. Also, make sure unit tests don't
154 // regress and use an origin past the year 2036. If this overflows here, the
155 // inverse calculation fails to compute the correct TimeTicks value, throwing
156 // off the entire system.
157 DCHECK_LT(seconds_component
, INT64_C(4263431296))
158 << "One year left to fix the NTP year 2036 wrap-around issue!";
159 *seconds
= static_cast<uint32
>(seconds_component
);
160 *fractions
= static_cast<uint32
>(
161 (ntp_time_us
% base::Time::kMicrosecondsPerSecond
) *
162 kMagicFractionalUnit
);
165 inline void ConvertTimeTicksToNtp(const base::TimeTicks
& time
,
167 uint32
* ntp_fractions
) {
168 base::TimeDelta elapsed_since_unix_epoch
=
169 time
- base::TimeTicks::UnixEpoch();
172 elapsed_since_unix_epoch
.InMicroseconds() +
173 (kUnixEpochInNtpSeconds
* base::Time::kMicrosecondsPerSecond
);
175 ConvertTimeToFractions(ntp_time_us
, ntp_seconds
, ntp_fractions
);
178 inline base::TimeTicks
ConvertNtpToTimeTicks(uint32 ntp_seconds
,
179 uint32 ntp_fractions
) {
181 static_cast<int64
>(ntp_seconds
) * base::Time::kMicrosecondsPerSecond
+
182 static_cast<int64
>(ntp_fractions
) / kMagicFractionalUnit
;
184 base::TimeDelta elapsed_since_unix_epoch
= base::TimeDelta::FromMicroseconds(
186 (kUnixEpochInNtpSeconds
* base::Time::kMicrosecondsPerSecond
));
187 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch
;
190 inline base::TimeDelta
RtpDeltaToTimeDelta(int64 rtp_delta
, int rtp_timebase
) {
191 DCHECK_GT(rtp_timebase
, 0);
192 return rtp_delta
* base::TimeDelta::FromSeconds(1) / rtp_timebase
;
198 #endif // MEDIA_CAST_CAST_DEFINES_H_