| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 ((delay_fraction & 0xFFFF0000) >> 16); | 128 ((delay_fraction & 0xFFFF0000) >> 16); |
| 129 } | 129 } |
| 130 | 130 |
| 131 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) { | 131 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) { |
| 132 uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000; | 132 uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000; |
| 133 delay_ms >>= 16; | 133 delay_ms >>= 16; |
| 134 delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000; | 134 delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000; |
| 135 return base::TimeDelta::FromMilliseconds(delay_ms); | 135 return base::TimeDelta::FromMilliseconds(delay_ms); |
| 136 } | 136 } |
| 137 | 137 |
| 138 inline void ConvertTimeToFractions(int64 time_us, | 138 inline void ConvertTimeToFractions(int64 ntp_time_us, |
| 139 uint32* seconds, | 139 uint32* seconds, |
| 140 uint32* fractions) { | 140 uint32* fractions) { |
| 141 DCHECK_GE(time_us, 0) << "Time must NOT be negative"; | 141 DCHECK_GE(ntp_time_us, 0) << "Time must NOT be negative"; |
| 142 *seconds = static_cast<uint32>(time_us / base::Time::kMicrosecondsPerSecond); | 142 const int64 seconds_component = |
| 143 ntp_time_us / base::Time::kMicrosecondsPerSecond; |
| 144 // NTP time will overflow in the year 2036. Also, make sure unit tests don't |
| 145 // regress and use an origin past the year 2036. If this overflows here, the |
| 146 // inverse calculation fails to compute the correct TimeTicks value, throwing |
| 147 // off the entire system. |
| 148 DCHECK_LT(seconds_component, INT64_C(4263431296)) |
| 149 << "One year left to fix the NTP year 2036 wrap-around issue!"; |
| 150 *seconds = static_cast<uint32>(seconds_component); |
| 143 *fractions = static_cast<uint32>( | 151 *fractions = static_cast<uint32>( |
| 144 (time_us % base::Time::kMicrosecondsPerSecond) * kMagicFractionalUnit); | 152 (ntp_time_us % base::Time::kMicrosecondsPerSecond) * |
| 153 kMagicFractionalUnit); |
| 145 } | 154 } |
| 146 | 155 |
| 147 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time, | 156 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time, |
| 148 uint32* ntp_seconds, | 157 uint32* ntp_seconds, |
| 149 uint32* ntp_fractions) { | 158 uint32* ntp_fractions) { |
| 150 base::TimeDelta elapsed_since_unix_epoch = | 159 base::TimeDelta elapsed_since_unix_epoch = |
| 151 time - base::TimeTicks::UnixEpoch(); | 160 time - base::TimeTicks::UnixEpoch(); |
| 152 | 161 |
| 153 int64 ntp_time_us = | 162 int64 ntp_time_us = |
| 154 elapsed_since_unix_epoch.InMicroseconds() + | 163 elapsed_since_unix_epoch.InMicroseconds() + |
| 155 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond); | 164 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond); |
| 156 | 165 |
| 157 ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions); | 166 ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions); |
| 158 } | 167 } |
| 159 | 168 |
| 160 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds, | 169 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds, |
| 161 uint32 ntp_fractions) { | 170 uint32 ntp_fractions) { |
| 162 int64 ntp_time_us = | 171 int64 ntp_time_us = |
| 163 static_cast<int64>(ntp_seconds) * base::Time::kMicrosecondsPerSecond + | 172 static_cast<int64>(ntp_seconds) * base::Time::kMicrosecondsPerSecond + |
| 164 static_cast<int64>(ntp_fractions) / kMagicFractionalUnit; | 173 static_cast<int64>(ntp_fractions) / kMagicFractionalUnit; |
| 165 | 174 |
| 166 base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds( | 175 base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds( |
| 167 ntp_time_us - | 176 ntp_time_us - |
| 168 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond)); | 177 (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond)); |
| 169 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch; | 178 return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch; |
| 170 } | 179 } |
| 171 | 180 |
| 181 inline base::TimeDelta RtpDeltaToTimeDelta(int64 rtp_delta, int rtp_timebase) { |
| 182 DCHECK_GT(rtp_timebase, 0); |
| 183 return rtp_delta * base::TimeDelta::FromSeconds(1) / rtp_timebase; |
| 184 } |
| 185 |
| 172 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) { | 186 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) { |
| 173 base::TimeTicks zero_time; | 187 base::TimeTicks zero_time; |
| 174 base::TimeDelta recorded_delta = time_ticks - zero_time; | 188 base::TimeDelta recorded_delta = time_ticks - zero_time; |
| 175 // Timestamp is in 90 KHz for video. | 189 // Timestamp is in 90 KHz for video. |
| 176 return static_cast<uint32>(recorded_delta.InMilliseconds() * 90); | 190 return static_cast<uint32>(recorded_delta.InMilliseconds() * 90); |
| 177 } | 191 } |
| 178 | 192 |
| 179 } // namespace cast | 193 } // namespace cast |
| 180 } // namespace media | 194 } // namespace media |
| 181 | 195 |
| 182 #endif // MEDIA_CAST_CAST_DEFINES_H_ | 196 #endif // MEDIA_CAST_CAST_DEFINES_H_ |
| OLD | NEW |