Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / cast / cast_defines.h
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.
4
5 #ifndef MEDIA_CAST_CAST_DEFINES_H_
6 #define MEDIA_CAST_CAST_DEFINES_H_
7
8 #include <map>
9 #include <set>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "base/time/time.h"
15
16 namespace media {
17 namespace cast {
18
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);
24
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;
31
32 enum DefaultSettings {
33   kDefaultAudioEncoderBitrate = 0,  // This means "auto," and may mean VBR.
34   kDefaultAudioSamplingRate = 48000,
35   kDefaultMaxQp = 56,
36   kDefaultMinQp = 4,
37   kDefaultMaxFrameRate = 30,
38   kDefaultNumberOfVideoBuffers = 1,
39   kDefaultRtcpIntervalMs = 500,
40   kDefaultRtpHistoryMs = 1000,
41   kDefaultRtpMaxDelayMs = 100,
42 };
43
44 enum PacketType {
45   kNewPacket,
46   kNewPacketCompletingFrame,
47   kDuplicatePacket,
48   kTooOldPacket,
49 };
50
51 const uint16 kRtcpCastAllPacketsLost = 0xffff;
52
53 const size_t kMinLengthOfRtcp = 8;
54
55 // Basic RTP header + cast header.
56 const size_t kMinLengthOfRtp = 12 + 6;
57
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;
62
63 // TODO(pwestin): Re-factor the functions bellow into a class with static
64 // methods.
65
66 // January 1970, in NTP seconds.
67 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
68 // 1 January 1900.
69 static const int64 kUnixEpochInNtpSeconds = GG_INT64_C(2208988800);
70
71 // Magic fractional unit. Used to convert time (in microseconds) to/from
72 // fractional NTP seconds.
73 static const double kMagicFractionalUnit = 4.294967296E3;
74
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;
78 }
79
80 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
81   return (timestamp != prev_timestamp) &&
82          static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
83 }
84
85 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
86   return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
87 }
88
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;
92 }
93
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);
98 }
99
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);
105 }
106
107 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
108   uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
109   delay_ms >>= 16;
110   delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
111   return base::TimeDelta::FromMilliseconds(delay_ms);
112 }
113
114 inline void ConvertTimeToFractions(int64 time_us,
115                                    uint32* seconds,
116                                    uint32* fractions) {
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);
121 }
122
123 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
124                                   uint32* ntp_seconds,
125                                   uint32* ntp_fractions) {
126   base::TimeDelta elapsed_since_unix_epoch =
127       time - base::TimeTicks::UnixEpoch();
128
129   int64 ntp_time_us =
130       elapsed_since_unix_epoch.InMicroseconds() +
131       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
132
133   ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
134 }
135
136 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
137                                              uint32 ntp_fractions) {
138   int64 ntp_time_us =
139       static_cast<int64>(ntp_seconds) * base::Time::kMicrosecondsPerSecond +
140       static_cast<int64>(ntp_fractions) / kMagicFractionalUnit;
141
142   base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds(
143       ntp_time_us -
144       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
145   return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
146 }
147
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);
153 }
154
155 }  // namespace cast
156 }  // namespace media
157
158 #endif  // MEDIA_CAST_CAST_DEFINES_H_