OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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_COMMON_RTP_TIME_H_ | |
6 #define MEDIA_CAST_COMMON_RTP_TIME_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <limits> | |
11 | |
12 #include "base/time/time.h" | |
13 #include "media/cast/common/expanded_value_base.h" | |
14 | |
15 namespace media { | |
16 namespace cast { | |
17 | |
18 class RtpTimeTicks; // Forward declaration (see below). | |
19 | |
20 // The difference between two RtpTimeTickses. This data type is modeled off of | |
21 // base::TimeDelta, and used for performing compiler-checked arithmetic with | |
22 // RtpTimeTicks. | |
23 // | |
24 // This data type wraps a value, providing only the meaningful set of math | |
25 // operations that may be performed on the value. RtpTimeDeltas may be | |
26 // added/subtracted with other RtpTimeDeltas to produce a RtpTimeDelta holding | |
27 // the sum/difference. RtpTimeDeltas may also be multiplied or divided by | |
28 // integer amounts. Finally, RtpTimeDeltas may be divided by other | |
29 // RtpTimeDeltas to compute a number of periods (floor'ed to an integer), or | |
30 // modulo each other to determine a time period remainder. | |
31 // | |
32 // The base class provides bit truncation/extension features for | |
33 // wire-formatting, and also the comparison operators. | |
34 class RtpTimeDelta : public ExpandedValueBase<int64_t, RtpTimeDelta> { | |
35 public: | |
36 RtpTimeDelta() : ExpandedValueBase(0) {} | |
37 | |
38 // Arithmetic operators (with other deltas). | |
39 RtpTimeDelta operator+(RtpTimeDelta rhs) const { | |
40 return RtpTimeDelta(value_ + rhs.value_); | |
41 } | |
42 RtpTimeDelta operator-(RtpTimeDelta rhs) const { | |
43 return RtpTimeDelta(value_ - rhs.value_); | |
44 } | |
45 RtpTimeDelta& operator+=(RtpTimeDelta rhs) { return (*this = (*this + rhs)); } | |
46 RtpTimeDelta& operator-=(RtpTimeDelta rhs) { return (*this = (*this - rhs)); } | |
47 RtpTimeDelta operator-() const { return RtpTimeDelta(-value_); } | |
48 | |
49 // Multiplicative operators (with integer types). | |
50 template <typename IntType> | |
51 RtpTimeDelta operator*(IntType rhs) const { | |
52 static_assert(std::numeric_limits<IntType>::is_integer, | |
53 "|rhs| must be a POD integer type"); | |
54 return RtpTimeDelta(value_ * rhs); | |
55 } | |
56 template <typename IntType> | |
57 RtpTimeDelta operator/(IntType rhs) const { | |
58 static_assert(std::numeric_limits<IntType>::is_integer, | |
59 "|rhs| must be a POD integer type"); | |
60 return RtpTimeDelta(value_ / rhs); | |
61 } | |
62 template <typename IntType> | |
63 RtpTimeDelta& operator*=(IntType rhs) { return (*this = (*this * rhs)); } | |
64 template <typename IntType> | |
65 RtpTimeDelta& operator/=(IntType rhs) { return (*this = (*this / rhs)); } | |
66 | |
67 // Multiplicative operators (with other deltas). | |
Irfan
2015/12/09 21:24:28
Perhaps group all operators with deltas together ?
miu
2015/12/10 00:38:36
Done.
| |
68 int64_t operator/(RtpTimeDelta rhs) const { return value_ / rhs.value_; } | |
69 RtpTimeDelta operator%(RtpTimeDelta rhs) const { | |
70 return RtpTimeDelta(value_ % rhs.value_); | |
71 } | |
72 RtpTimeDelta& operator%=(RtpTimeDelta rhs) { return (*this = (*this % rhs)); } | |
73 | |
74 // Maps this RtpTimeDelta to an approximate TimeDelta using the given | |
75 // RTP timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued | |
76 // RtpTimeDelta. | |
77 base::TimeDelta ToTimeDelta(int rtp_timebase) const; | |
78 | |
79 // Write-out a human-readable version of this RtpTimeDelta. | |
80 void OutputToLog(std::ostream& out) const; | |
81 | |
82 // Maps the TimeDelta to an approximate RtpTimeDelta using the given RTP | |
83 // timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued | |
84 // RtpTimeDelta. | |
85 static RtpTimeDelta FromTimeDelta(base::TimeDelta delta, int rtp_timebase); | |
86 | |
87 // Construct a RtpTimeDelta from an exact number of ticks. | |
88 static RtpTimeDelta FromTicks(int64_t ticks); | |
89 | |
90 private: | |
91 friend class ExpandedValueBase<int64_t, RtpTimeDelta>; | |
92 friend class RtpTimeTicks; | |
93 | |
94 explicit RtpTimeDelta(int64_t ticks) : ExpandedValueBase(ticks) {} | |
95 | |
96 int64_t value() const { return value_; } | |
97 }; | |
98 | |
99 // A media timestamp whose timebase matches the periodicity of the content | |
100 // (e.g., for audio, the timebase would be the sampling frequency). This data | |
101 // type is modeled off of base::TimeTicks. | |
102 // | |
103 // This data type wraps a value, providing only the meaningful set of math | |
104 // operations that may be performed on the value. The difference between two | |
105 // RtpTimeTickses is a RtpTimeDelta. Likewise, adding or subtracting a | |
106 // RtpTimeTicks with a RtpTimeDelta produces an off-set RtpTimeTicks. | |
107 // | |
108 // The base class provides bit truncation/extension features for | |
109 // wire-formatting, and also the comparison operators. | |
110 class RtpTimeTicks : public ExpandedValueBase<int64_t, RtpTimeTicks> { | |
111 public: | |
112 RtpTimeTicks() : ExpandedValueBase(0) {} | |
113 | |
114 // Compute the difference between two RtpTimeTickses. | |
115 RtpTimeDelta operator-(RtpTimeTicks rhs) const { | |
116 return RtpTimeDelta(value_ - rhs.value_); | |
117 } | |
118 | |
119 // Return a new RtpTimeTicks before or after this one. | |
120 RtpTimeTicks operator+(RtpTimeDelta rhs) const { | |
121 return RtpTimeTicks(value_ + rhs.value()); | |
122 } | |
123 RtpTimeTicks operator-(RtpTimeDelta rhs) const { | |
124 return RtpTimeTicks(value_ - rhs.value()); | |
125 } | |
126 RtpTimeTicks& operator+=(RtpTimeDelta rhs) { return (*this = (*this + rhs)); } | |
127 RtpTimeTicks& operator-=(RtpTimeDelta rhs) { return (*this = (*this - rhs)); } | |
128 | |
129 // Maps this RtpTimeTicks to an approximate TimeDelta using the given | |
130 // RTP timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued | |
131 // RtpTimeTicks. | |
132 base::TimeDelta ToTimeDelta(int rtp_timebase) const; | |
133 | |
134 // Write-out a human-readable version of this RtpTimeTicks. | |
135 void OutputToLog(std::ostream& out) const; | |
Irfan
2015/12/09 21:24:28
Do we need this public method given << ?
miu
2015/12/10 00:38:36
Done. Removed method, and friended the operator<<
| |
136 | |
137 // Maps the TimeDelta to an approximate RtpTimeTicks using the given RTP | |
138 // timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued | |
139 // RtpTimeTicks. | |
140 static RtpTimeTicks FromTimeDelta(base::TimeDelta delta, int rtp_timebase); | |
141 | |
142 private: | |
143 friend class ExpandedValueBase<int64_t, RtpTimeTicks>; | |
144 | |
145 explicit RtpTimeTicks(int64_t value) : ExpandedValueBase(value) {} | |
146 }; | |
147 | |
148 // Convenience operator overloads for logging. | |
149 std::ostream& operator<<(std::ostream& out, RtpTimeDelta rhs); | |
150 std::ostream& operator<<(std::ostream& out, RtpTimeTicks rhs); | |
151 | |
152 } // namespace cast | |
153 } // namespace media | |
154 | |
155 #endif // MEDIA_CAST_COMMON_RTP_TIME_H_ | |
OLD | NEW |