Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / media / cast / cast_defines.h
blob661c09524924d085fb043676fefe80203a3dbed2
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_
8 #include <stdint.h>
10 #include <cmath>
11 #include <map>
12 #include <set>
14 #include "base/basictypes.h"
15 #include "base/compiler_specific.h"
16 #include "base/logging.h"
17 #include "base/time/time.h"
18 #include "media/cast/net/cast_transport_config.h"
20 namespace media {
21 namespace cast {
23 const int64 kDontShowTimeoutMs = 33;
24 const float kDefaultCongestionControlBackOff = 0.875f;
25 const uint32 kVideoFrequency = 90000;
26 const uint32 kStartFrameId = UINT32_C(0xffffffff);
28 // This is an important system-wide constant. This limits how much history the
29 // implementation must retain in order to process the acknowledgements of past
30 // frames.
31 // This value is carefully choosen such that it fits in the 8-bits range for
32 // frame IDs. It is also less than half of the full 8-bits range such that we
33 // can handle wrap around and compare two frame IDs.
34 const int kMaxUnackedFrames = 120;
36 const int64 kCastMessageUpdateIntervalMs = 33;
37 const int64 kNackRepeatIntervalMs = 30;
39 enum CastInitializationStatus {
40 STATUS_AUDIO_UNINITIALIZED,
41 STATUS_VIDEO_UNINITIALIZED,
42 STATUS_AUDIO_INITIALIZED,
43 STATUS_VIDEO_INITIALIZED,
44 STATUS_INVALID_CAST_ENVIRONMENT,
45 STATUS_INVALID_CRYPTO_CONFIGURATION,
46 STATUS_UNSUPPORTED_AUDIO_CODEC,
47 STATUS_UNSUPPORTED_VIDEO_CODEC,
48 STATUS_INVALID_AUDIO_CONFIGURATION,
49 STATUS_INVALID_VIDEO_CONFIGURATION,
50 STATUS_HW_VIDEO_ENCODER_NOT_SUPPORTED,
53 enum DefaultSettings {
54 kDefaultAudioEncoderBitrate = 0, // This means "auto," and may mean VBR.
55 kDefaultAudioSamplingRate = 48000,
56 kDefaultMaxQp = 56,
57 kDefaultMinQp = 4,
58 kDefaultMaxFrameRate = 30,
59 kDefaultNumberOfVideoBuffers = 1,
60 kDefaultRtcpIntervalMs = 500,
61 kDefaultRtpHistoryMs = 1000,
62 kDefaultRtpMaxDelayMs = 100,
65 enum PacketType {
66 kNewPacket,
67 kNewPacketCompletingFrame,
68 kDuplicatePacket,
69 kTooOldPacket,
72 // kRtcpCastAllPacketsLost is used in PacketIDSet and
73 // on the wire to mean that ALL packets for a particular
74 // frame are lost.
75 const uint16 kRtcpCastAllPacketsLost = 0xffff;
77 // kRtcpCastLastPacket is used in PacketIDSet to ask for
78 // the last packet of a frame to be retransmitted.
79 const uint16 kRtcpCastLastPacket = 0xfffe;
81 const size_t kMinLengthOfRtcp = 8;
83 // Basic RTP header + cast header.
84 const size_t kMinLengthOfRtp = 12 + 6;
86 // Each uint16 represents one packet id within a cast frame.
87 // Can also contain kRtcpCastAllPacketsLost and kRtcpCastLastPacket.
88 typedef std::set<uint16> PacketIdSet;
89 // Each uint8 represents one cast frame.
90 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
92 // TODO(pwestin): Re-factor the functions bellow into a class with static
93 // methods.
95 // January 1970, in NTP seconds.
96 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
97 // 1 January 1900.
98 static const int64 kUnixEpochInNtpSeconds = INT64_C(2208988800);
100 // Magic fractional unit. Used to convert time (in microseconds) to/from
101 // fractional NTP seconds.
102 static const double kMagicFractionalUnit = 4.294967296E3;
104 // The maximum number of Cast receiver events to keep in history for the
105 // purpose of sending the events through RTCP.
106 // The number chosen should be more than the number of events that can be
107 // stored in a RTCP packet.
108 static const size_t kReceiverRtcpEventHistorySize = 512;
110 inline bool IsNewerFrameId(uint32 frame_id, uint32 prev_frame_id) {
111 return (frame_id != prev_frame_id) &&
112 static_cast<uint32>(frame_id - prev_frame_id) < 0x80000000;
115 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
116 return (timestamp != prev_timestamp) &&
117 static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
120 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
121 return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
124 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) {
125 return (packet_id != prev_packet_id) &&
126 static_cast<uint16>(packet_id - prev_packet_id) < 0x8000;
129 inline bool IsNewerSequenceNumber(uint16 sequence_number,
130 uint16 prev_sequence_number) {
131 // Same function as IsNewerPacketId just different data and name.
132 return IsNewerPacketId(sequence_number, prev_sequence_number);
135 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
136 // fractions of a second where 0x80000000 is half a second.
137 inline uint32 ConvertToNtpDiff(uint32 delay_seconds, uint32 delay_fraction) {
138 return ((delay_seconds & 0x0000FFFF) << 16) +
139 ((delay_fraction & 0xFFFF0000) >> 16);
142 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
143 uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
144 delay_ms >>= 16;
145 delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
146 return base::TimeDelta::FromMilliseconds(delay_ms);
149 inline void ConvertTimeToFractions(int64 ntp_time_us,
150 uint32* seconds,
151 uint32* fractions) {
152 DCHECK_GE(ntp_time_us, 0) << "Time must NOT be negative";
153 const int64 seconds_component =
154 ntp_time_us / base::Time::kMicrosecondsPerSecond;
155 // NTP time will overflow in the year 2036. Also, make sure unit tests don't
156 // regress and use an origin past the year 2036. If this overflows here, the
157 // inverse calculation fails to compute the correct TimeTicks value, throwing
158 // off the entire system.
159 DCHECK_LT(seconds_component, INT64_C(4263431296))
160 << "One year left to fix the NTP year 2036 wrap-around issue!";
161 *seconds = static_cast<uint32>(seconds_component);
162 *fractions = static_cast<uint32>(
163 (ntp_time_us % base::Time::kMicrosecondsPerSecond) *
164 kMagicFractionalUnit);
167 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
168 uint32* ntp_seconds,
169 uint32* ntp_fractions) {
170 base::TimeDelta elapsed_since_unix_epoch =
171 time - base::TimeTicks::UnixEpoch();
173 int64 ntp_time_us =
174 elapsed_since_unix_epoch.InMicroseconds() +
175 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
177 ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
180 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
181 uint32 ntp_fractions) {
182 // We need to ceil() here because the calculation of |fractions| in
183 // ConvertTimeToFractions() effectively does a floor().
184 int64 ntp_time_us = ntp_seconds * base::Time::kMicrosecondsPerSecond +
185 static_cast<int64>(std::ceil(ntp_fractions / kMagicFractionalUnit));
187 base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds(
188 ntp_time_us -
189 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
190 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
193 inline base::TimeDelta RtpDeltaToTimeDelta(int64 rtp_delta, int rtp_timebase) {
194 DCHECK_GT(rtp_timebase, 0);
195 return rtp_delta * base::TimeDelta::FromSeconds(1) / rtp_timebase;
198 inline int64 TimeDeltaToRtpDelta(base::TimeDelta delta, int rtp_timebase) {
199 DCHECK_GT(rtp_timebase, 0);
200 return delta * rtp_timebase / base::TimeDelta::FromSeconds(1);
203 } // namespace cast
204 } // namespace media
206 #endif // MEDIA_CAST_CAST_DEFINES_H_