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 // Each uint16 represents one packet id within a cast frame.
59 typedef std::set<uint16> PacketIdSet;
60 // Each uint8 represents one cast frame.
61 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
63 // TODO(pwestin): Re-factor the functions bellow into a class with static
66 // January 1970, in NTP seconds.
67 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
69 static const int64 kUnixEpochInNtpSeconds = GG_INT64_C(2208988800);
71 // Magic fractional unit. Used to convert time (in microseconds) to/from
72 // fractional NTP seconds.
73 static const double kMagicFractionalUnit = 4.294967296E3;
75 inline bool IsNewerFrameId(uint32 frame_id, uint32 prev_frame_id) {
76 return (frame_id != prev_frame_id) &&
77 static_cast<uint32>(frame_id - prev_frame_id) < 0x80000000;
80 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
81 return (timestamp != prev_timestamp) &&
82 static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
85 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
86 return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
89 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) {
90 return (packet_id != prev_packet_id) &&
91 static_cast<uint16>(packet_id - prev_packet_id) < 0x8000;
94 inline bool IsNewerSequenceNumber(uint16 sequence_number,
95 uint16 prev_sequence_number) {
96 // Same function as IsNewerPacketId just different data and name.
97 return IsNewerPacketId(sequence_number, prev_sequence_number);
100 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
101 // fractions of a second where 0x80000000 is half a second.
102 inline uint32 ConvertToNtpDiff(uint32 delay_seconds, uint32 delay_fraction) {
103 return ((delay_seconds & 0x0000FFFF) << 16) +
104 ((delay_fraction & 0xFFFF0000) >> 16);
107 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
108 uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
110 delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
111 return base::TimeDelta::FromMilliseconds(delay_ms);
114 inline void ConvertTimeToFractions(int64 time_us,
117 DCHECK_GE(time_us, 0) << "Time must NOT be negative";
118 *seconds = static_cast<uint32>(time_us / base::Time::kMicrosecondsPerSecond);
119 *fractions = static_cast<uint32>(
120 (time_us % base::Time::kMicrosecondsPerSecond) * kMagicFractionalUnit);
123 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
125 uint32* ntp_fractions) {
126 base::TimeDelta elapsed_since_unix_epoch =
127 time - base::TimeTicks::UnixEpoch();
130 elapsed_since_unix_epoch.InMicroseconds() +
131 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
133 ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
136 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
137 uint32 ntp_fractions) {
139 static_cast<int64>(ntp_seconds) * base::Time::kMicrosecondsPerSecond +
140 static_cast<int64>(ntp_fractions) / kMagicFractionalUnit;
142 base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds(
144 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
145 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
148 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) {
149 base::TimeTicks zero_time;
150 base::TimeDelta recorded_delta = time_ticks - zero_time;
151 // Timestamp is in 90 KHz for video.
152 return static_cast<uint32>(recorded_delta.InMilliseconds() * 90);
158 #endif // MEDIA_CAST_CAST_DEFINES_H_