OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_CAST_CAST_DEFINES_H_ | 5 #ifndef MEDIA_CAST_CAST_DEFINES_H_ |
6 #define MEDIA_CAST_CAST_DEFINES_H_ | 6 #define MEDIA_CAST_CAST_DEFINES_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 const size_t kMinLengthOfRtp = 12 + 6; | 47 const size_t kMinLengthOfRtp = 12 + 6; |
48 | 48 |
49 // Each uint16 represents one packet id within a cast frame. | 49 // Each uint16 represents one packet id within a cast frame. |
50 typedef std::set<uint16> PacketIdSet; | 50 typedef std::set<uint16> PacketIdSet; |
51 // Each uint8 represents one cast frame. | 51 // Each uint8 represents one cast frame. |
52 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap; | 52 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap; |
53 | 53 |
54 // TODO(pwestin): Re-factor the functions bellow into a class with static | 54 // TODO(pwestin): Re-factor the functions bellow into a class with static |
55 // methods. | 55 // methods. |
56 | 56 |
| 57 // January 1970, in NTP seconds. |
| 58 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on |
| 59 // 1 January 1900. |
| 60 static const int64 kUnixEpochInNtpSeconds = GG_INT64_C(2208988800); |
| 61 |
57 // Magic fractional unit. Used to convert time (in microseconds) to/from | 62 // Magic fractional unit. Used to convert time (in microseconds) to/from |
58 // fractional NTP seconds. | 63 // fractional NTP seconds. |
59 static const double kMagicFractionalUnit = 4.294967296E3; | 64 static const double kMagicFractionalUnit = 4.294967296E3; |
60 | 65 |
61 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on | |
62 // 1 January 1900. | |
63 static const int64 kNtpEpochDeltaSeconds = GG_INT64_C(9435484800); | |
64 static const int64 kNtpEpochDeltaMicroseconds = | |
65 kNtpEpochDeltaSeconds * base::Time::kMicrosecondsPerSecond; | |
66 | |
67 inline bool IsNewerFrameId(uint8 frame_id, uint8 prev_frame_id) { | 66 inline bool IsNewerFrameId(uint8 frame_id, uint8 prev_frame_id) { |
68 return (frame_id != prev_frame_id) && | 67 return (frame_id != prev_frame_id) && |
69 static_cast<uint8>(frame_id - prev_frame_id) < 0x80; | 68 static_cast<uint8>(frame_id - prev_frame_id) < 0x80; |
70 } | 69 } |
71 | 70 |
72 inline bool IsOlderFrameId(uint8 frame_id, uint8 prev_frame_id) { | 71 inline bool IsOlderFrameId(uint8 frame_id, uint8 prev_frame_id) { |
73 return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id); | 72 return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id); |
74 } | 73 } |
75 | 74 |
76 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) { | 75 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) { |
(...skipping 23 matching lines...) Expand all Loading... |
100 | 99 |
101 inline void ConvertTimeToFractions(int64 time_us, | 100 inline void ConvertTimeToFractions(int64 time_us, |
102 uint32* seconds, | 101 uint32* seconds, |
103 uint32* fractions) { | 102 uint32* fractions) { |
104 DCHECK_GE(time_us, 0) << "Time must NOT be negative"; | 103 DCHECK_GE(time_us, 0) << "Time must NOT be negative"; |
105 *seconds = static_cast<uint32>(time_us / base::Time::kMicrosecondsPerSecond); | 104 *seconds = static_cast<uint32>(time_us / base::Time::kMicrosecondsPerSecond); |
106 *fractions = static_cast<uint32>( | 105 *fractions = static_cast<uint32>( |
107 (time_us % base::Time::kMicrosecondsPerSecond) * kMagicFractionalUnit); | 106 (time_us % base::Time::kMicrosecondsPerSecond) * kMagicFractionalUnit); |
108 } | 107 } |
109 | 108 |
110 inline void ConvertTimeToNtp(const base::TimeTicks& time, | 109 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time, |
111 uint32* ntp_seconds, | 110 uint32* ntp_seconds, |
112 uint32* ntp_fractions) { | 111 uint32* ntp_fractions) { |
113 int64 time_us = time.ToInternalValue() - kNtpEpochDeltaMicroseconds; | 112 base::TimeDelta elapsed_since_unix_epoch = |
114 ConvertTimeToFractions(time_us, ntp_seconds, ntp_fractions); | 113 time - base::TimeTicks::UnixEpoch(); |
| 114 |
| 115 int64 ntp_time_us = elapsed_since_unix_epoch.InMicroseconds() + |
| 116 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond); |
| 117 |
| 118 ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions); |
115 } | 119 } |
116 | 120 |
117 inline base::TimeTicks ConvertNtpToTime(uint32 ntp_seconds, | 121 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds, |
118 uint32 ntp_fractions) { | 122 uint32 ntp_fractions) { |
119 int64 ntp_time_us = static_cast<int64>(ntp_seconds) * | 123 int64 ntp_time_us = static_cast<int64>(ntp_seconds) * |
120 base::Time::kMicrosecondsPerSecond; | 124 base::Time::kMicrosecondsPerSecond + |
121 ntp_time_us += static_cast<int64>(ntp_fractions) / kMagicFractionalUnit; | 125 static_cast<int64>(ntp_fractions) / kMagicFractionalUnit; |
122 return base::TimeTicks::FromInternalValue(ntp_time_us + | 126 |
123 kNtpEpochDeltaMicroseconds); | 127 base::TimeDelta elapsed_since_unix_epoch = |
| 128 base::TimeDelta::FromMicroseconds(ntp_time_us - |
| 129 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond)); |
| 130 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch; |
124 } | 131 } |
125 | 132 |
126 } // namespace cast | 133 } // namespace cast |
127 } // namespace media | 134 } // namespace media |
128 | 135 |
129 #endif // MEDIA_CAST_CAST_DEFINES_H_ | 136 #endif // MEDIA_CAST_CAST_DEFINES_H_ |
OLD | NEW |