Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / cast / cast_defines.h
blob9bf2f44352cf4349364a73670b95b9cdf71672ed
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 // Success/in-progress/failure status codes bubbled up to clients via
40 // StatusChangeCallbacks.
41 enum OperationalStatus {
42 // Client should not send frames yet (sender), or should not expect to receive
43 // frames yet (receiver).
44 STATUS_UNINITIALIZED,
46 // Client may now send or receive frames.
47 STATUS_INITIALIZED,
49 // Codec is being re-initialized. Client may continue sending frames, but
50 // some may be ignored/dropped until a transition back to STATUS_INITIALIZED.
51 STATUS_CODEC_REINIT_PENDING,
53 // Session has halted due to invalid configuration.
54 STATUS_INVALID_CONFIGURATION,
56 // Session has halted due to an unsupported codec.
57 STATUS_UNSUPPORTED_CODEC,
59 // Session has halted due to a codec initialization failure. Note that this
60 // can be reported after STATUS_INITIALIZED/STATUS_CODEC_REINIT_PENDING if the
61 // codec was re-initialized during the session.
62 STATUS_CODEC_INIT_FAILED,
64 // Session has halted due to a codec runtime failure.
65 STATUS_CODEC_RUNTIME_ERROR,
68 enum DefaultSettings {
69 kDefaultAudioEncoderBitrate = 0, // This means "auto," and may mean VBR.
70 kDefaultAudioSamplingRate = 48000,
71 kDefaultMaxQp = 63,
72 kDefaultMinQp = 4,
73 kDefaultMaxFrameRate = 30,
74 kDefaultNumberOfVideoBuffers = 1,
75 kDefaultRtcpIntervalMs = 500,
76 kDefaultRtpHistoryMs = 1000,
77 kDefaultRtpMaxDelayMs = 100,
80 enum PacketType {
81 kNewPacket,
82 kNewPacketCompletingFrame,
83 kDuplicatePacket,
84 kTooOldPacket,
87 // kRtcpCastAllPacketsLost is used in PacketIDSet and
88 // on the wire to mean that ALL packets for a particular
89 // frame are lost.
90 const uint16 kRtcpCastAllPacketsLost = 0xffff;
92 // kRtcpCastLastPacket is used in PacketIDSet to ask for
93 // the last packet of a frame to be retransmitted.
94 const uint16 kRtcpCastLastPacket = 0xfffe;
96 const size_t kMinLengthOfRtcp = 8;
98 // Basic RTP header + cast header.
99 const size_t kMinLengthOfRtp = 12 + 6;
101 // Each uint16 represents one packet id within a cast frame.
102 // Can also contain kRtcpCastAllPacketsLost and kRtcpCastLastPacket.
103 typedef std::set<uint16> PacketIdSet;
104 // Each uint8 represents one cast frame.
105 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
107 // TODO(pwestin): Re-factor the functions bellow into a class with static
108 // methods.
110 // January 1970, in NTP seconds.
111 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
112 // 1 January 1900.
113 static const int64 kUnixEpochInNtpSeconds = INT64_C(2208988800);
115 // Magic fractional unit. Used to convert time (in microseconds) to/from
116 // fractional NTP seconds.
117 static const double kMagicFractionalUnit = 4.294967296E3;
119 // The maximum number of Cast receiver events to keep in history for the
120 // purpose of sending the events through RTCP.
121 // The number chosen should be more than the number of events that can be
122 // stored in a RTCP packet.
123 static const size_t kReceiverRtcpEventHistorySize = 512;
125 inline bool IsNewerFrameId(uint32 frame_id, uint32 prev_frame_id) {
126 return (frame_id != prev_frame_id) &&
127 static_cast<uint32>(frame_id - prev_frame_id) < 0x80000000;
130 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
131 return (timestamp != prev_timestamp) &&
132 static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
135 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
136 return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
139 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) {
140 return (packet_id != prev_packet_id) &&
141 static_cast<uint16>(packet_id - prev_packet_id) < 0x8000;
144 inline bool IsNewerSequenceNumber(uint16 sequence_number,
145 uint16 prev_sequence_number) {
146 // Same function as IsNewerPacketId just different data and name.
147 return IsNewerPacketId(sequence_number, prev_sequence_number);
150 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
151 // fractions of a second where 0x80000000 is half a second.
152 inline uint32 ConvertToNtpDiff(uint32 delay_seconds, uint32 delay_fraction) {
153 return ((delay_seconds & 0x0000FFFF) << 16) +
154 ((delay_fraction & 0xFFFF0000) >> 16);
157 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
158 uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
159 delay_ms >>= 16;
160 delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
161 return base::TimeDelta::FromMilliseconds(delay_ms);
164 inline void ConvertTimeToFractions(int64 ntp_time_us,
165 uint32* seconds,
166 uint32* fractions) {
167 DCHECK_GE(ntp_time_us, 0) << "Time must NOT be negative";
168 const int64 seconds_component =
169 ntp_time_us / base::Time::kMicrosecondsPerSecond;
170 // NTP time will overflow in the year 2036. Also, make sure unit tests don't
171 // regress and use an origin past the year 2036. If this overflows here, the
172 // inverse calculation fails to compute the correct TimeTicks value, throwing
173 // off the entire system.
174 DCHECK_LT(seconds_component, INT64_C(4263431296))
175 << "One year left to fix the NTP year 2036 wrap-around issue!";
176 *seconds = static_cast<uint32>(seconds_component);
177 *fractions = static_cast<uint32>(
178 (ntp_time_us % base::Time::kMicrosecondsPerSecond) *
179 kMagicFractionalUnit);
182 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
183 uint32* ntp_seconds,
184 uint32* ntp_fractions) {
185 base::TimeDelta elapsed_since_unix_epoch =
186 time - base::TimeTicks::UnixEpoch();
188 int64 ntp_time_us =
189 elapsed_since_unix_epoch.InMicroseconds() +
190 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
192 ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
195 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
196 uint32 ntp_fractions) {
197 // We need to ceil() here because the calculation of |fractions| in
198 // ConvertTimeToFractions() effectively does a floor().
199 int64 ntp_time_us = ntp_seconds * base::Time::kMicrosecondsPerSecond +
200 static_cast<int64>(std::ceil(ntp_fractions / kMagicFractionalUnit));
202 base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds(
203 ntp_time_us -
204 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
205 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
208 inline base::TimeDelta RtpDeltaToTimeDelta(int64 rtp_delta, int rtp_timebase) {
209 DCHECK_GT(rtp_timebase, 0);
210 return rtp_delta * base::TimeDelta::FromSeconds(1) / rtp_timebase;
213 inline int64 TimeDeltaToRtpDelta(base::TimeDelta delta, int rtp_timebase) {
214 DCHECK_GT(rtp_timebase, 0);
215 return delta * rtp_timebase / base::TimeDelta::FromSeconds(1);
218 } // namespace cast
219 } // namespace media
221 #endif // MEDIA_CAST_CAST_DEFINES_H_