1*3f982cf4SFabien Sanglard // Copyright 2019 The Chromium Authors. All rights reserved.
2*3f982cf4SFabien Sanglard // Use of this source code is governed by a BSD-style license that can be
3*3f982cf4SFabien Sanglard // found in the LICENSE file.
4*3f982cf4SFabien Sanglard
5*3f982cf4SFabien Sanglard #ifndef CAST_STREAMING_NTP_TIME_H_
6*3f982cf4SFabien Sanglard #define CAST_STREAMING_NTP_TIME_H_
7*3f982cf4SFabien Sanglard
8*3f982cf4SFabien Sanglard #include <stdint.h>
9*3f982cf4SFabien Sanglard
10*3f982cf4SFabien Sanglard #include "platform/api/time.h"
11*3f982cf4SFabien Sanglard
12*3f982cf4SFabien Sanglard namespace openscreen {
13*3f982cf4SFabien Sanglard namespace cast {
14*3f982cf4SFabien Sanglard
15*3f982cf4SFabien Sanglard // NTP timestamps are 64-bit timestamps that consist of two 32-bit parts: 1) The
16*3f982cf4SFabien Sanglard // number of seconds since 1 January 1900; and 2) The fraction of the second,
17*3f982cf4SFabien Sanglard // where 0 maps to 0x00000000 and each unit increment represents another 2^-32
18*3f982cf4SFabien Sanglard // seconds.
19*3f982cf4SFabien Sanglard //
20*3f982cf4SFabien Sanglard // Note that it is part of the design of NTP for the seconds part to roll around
21*3f982cf4SFabien Sanglard // on 7 February 2036.
22*3f982cf4SFabien Sanglard using NtpTimestamp = uint64_t;
23*3f982cf4SFabien Sanglard
24*3f982cf4SFabien Sanglard // NTP fixed-point time math: Declare two std::chrono::duration types with the
25*3f982cf4SFabien Sanglard // bit-width necessary to reliably perform all conversions to/from NTP format.
26*3f982cf4SFabien Sanglard using NtpSeconds = std::chrono::duration<int64_t, std::chrono::seconds::period>;
27*3f982cf4SFabien Sanglard using NtpFraction =
28*3f982cf4SFabien Sanglard std::chrono::duration<int64_t, std::ratio<1, INT64_C(0x100000000)>>;
29*3f982cf4SFabien Sanglard
NtpSecondsPart(NtpTimestamp timestamp)30*3f982cf4SFabien Sanglard constexpr NtpSeconds NtpSecondsPart(NtpTimestamp timestamp) {
31*3f982cf4SFabien Sanglard return NtpSeconds(timestamp >> 32);
32*3f982cf4SFabien Sanglard }
33*3f982cf4SFabien Sanglard
NtpFractionPart(NtpTimestamp timestamp)34*3f982cf4SFabien Sanglard constexpr NtpFraction NtpFractionPart(NtpTimestamp timestamp) {
35*3f982cf4SFabien Sanglard return NtpFraction(timestamp & 0xffffffff);
36*3f982cf4SFabien Sanglard }
37*3f982cf4SFabien Sanglard
AssembleNtpTimestamp(NtpSeconds seconds,NtpFraction fraction)38*3f982cf4SFabien Sanglard constexpr NtpTimestamp AssembleNtpTimestamp(NtpSeconds seconds,
39*3f982cf4SFabien Sanglard NtpFraction fraction) {
40*3f982cf4SFabien Sanglard return (static_cast<uint64_t>(seconds.count()) << 32) |
41*3f982cf4SFabien Sanglard static_cast<uint32_t>(fraction.count());
42*3f982cf4SFabien Sanglard }
43*3f982cf4SFabien Sanglard
44*3f982cf4SFabien Sanglard // Converts between Clock::time_points and NtpTimestamps. The class is
45*3f982cf4SFabien Sanglard // instantiated with the current Clock time and the current wall clock time, and
46*3f982cf4SFabien Sanglard // these are used to determine a fixed origin reference point for all
47*3f982cf4SFabien Sanglard // conversions. Thus, to avoid introducing unintended timing-related behaviors,
48*3f982cf4SFabien Sanglard // only one NtpTimeConverter instance should be used for converting all the NTP
49*3f982cf4SFabien Sanglard // timestamps in the same streaming session.
50*3f982cf4SFabien Sanglard class NtpTimeConverter {
51*3f982cf4SFabien Sanglard public:
52*3f982cf4SFabien Sanglard NtpTimeConverter(
53*3f982cf4SFabien Sanglard Clock::time_point now,
54*3f982cf4SFabien Sanglard std::chrono::seconds since_unix_epoch = GetWallTimeSinceUnixEpoch());
55*3f982cf4SFabien Sanglard ~NtpTimeConverter();
56*3f982cf4SFabien Sanglard
57*3f982cf4SFabien Sanglard NtpTimestamp ToNtpTimestamp(Clock::time_point time_point) const;
58*3f982cf4SFabien Sanglard Clock::time_point ToLocalTime(NtpTimestamp timestamp) const;
59*3f982cf4SFabien Sanglard
60*3f982cf4SFabien Sanglard private:
61*3f982cf4SFabien Sanglard // The time point on the platform clock's timeline that corresponds to
62*3f982cf4SFabien Sanglard // approximately the same time point on the NTP timeline. Note that it is
63*3f982cf4SFabien Sanglard // acceptable for the granularity of the NTP seconds value to be whole seconds
64*3f982cf4SFabien Sanglard // here: Both a Cast Streaming Sender and Receiver will assume their clocks
65*3f982cf4SFabien Sanglard // can be off (with respect to each other) by even a large amount; and all
66*3f982cf4SFabien Sanglard // that matters is that time ticks forward at a reasonable pace from some
67*3f982cf4SFabien Sanglard // initial point.
68*3f982cf4SFabien Sanglard const Clock::time_point start_time_;
69*3f982cf4SFabien Sanglard const NtpSeconds since_ntp_epoch_;
70*3f982cf4SFabien Sanglard };
71*3f982cf4SFabien Sanglard
72*3f982cf4SFabien Sanglard } // namespace cast
73*3f982cf4SFabien Sanglard } // namespace openscreen
74*3f982cf4SFabien Sanglard
75*3f982cf4SFabien Sanglard #endif // CAST_STREAMING_NTP_TIME_H_
76