Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: media/cast/common/rtp_time.h

Issue 1515433002: Replace uses of raw uint32's with a type-checked RtpTimeTicks data type. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Compile fixes for cast_messages.h for chromeos cross-compiles. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include <sstream>
12
13 #include "base/time/time.h"
14 #include "media/cast/common/expanded_value_base.h"
15
16 namespace media {
17 namespace cast {
18
19 // Forward declarations (see below).
20 class RtpTimeDelta;
21 class RtpTimeTicks;
22
23 // Convenience operator overloads for logging.
24 std::ostream& operator<<(std::ostream& out, const RtpTimeDelta rhs);
25 std::ostream& operator<<(std::ostream& out, const RtpTimeTicks rhs);
26
27 // The difference between two RtpTimeTickses. This data type is modeled off of
Mike West 2015/12/10 08:54:46 Nit: "Tickses" can't possibly be the right plurali
miu 2015/12/11 23:00:28 Done...but it was cuter the way I had it. ;)
28 // base::TimeDelta, and used for performing compiler-checked arithmetic with
29 // RtpTimeTicks.
30 //
31 // This data type wraps a value, providing only the meaningful set of math
32 // operations that may be performed on the value. RtpTimeDeltas may be
33 // added/subtracted with other RtpTimeDeltas to produce a RtpTimeDelta holding
34 // the sum/difference. RtpTimeDeltas may also be multiplied or divided by
35 // integer amounts. Finally, RtpTimeDeltas may be divided by other
36 // RtpTimeDeltas to compute a number of periods (floor'ed to an integer), or
37 // modulo each other to determine a time period remainder.
38 //
39 // The base class provides bit truncation/extension features for
40 // wire-formatting, and also the comparison operators.
41 //
42 // Usage example:
43 //
44 // // Time math.
45 // RtpTimeDelta zero;
46 // RtpTimeDelta one_second_later =
47 // zero + RtpTimeDelta::FromTicks(kAudioSamplingRate);
48 // RtpTimeDelta ten_seconds_later = one_second_later * 10;
49 // int64_t ten_periods = ten_seconds_later / one_second_later;
50 //
51 // // Logging convenience.
52 // DLOG(INFO) << "The RTP time offset is " << ten_seconds_later;
53 //
54 // // Convert (approximately!) between RTP timebase and microsecond timebase:
55 // base::TimeDelta nine_seconds_offset =
56 // (ten_seconds_later - one_second_later).ToTimeDelta(kAudioSamplingRate);
57 // RtpTimeDelta nine_seconds_rtp =
58 // RtpTimeDelta::FromTimeDelta(nine_seconds_offset, kAudioSamplingRate);
59 class RtpTimeDelta : public ExpandedValueBase<int64_t, RtpTimeDelta> {
60 public:
61 RtpTimeDelta() : ExpandedValueBase(0) {}
62
63 // Arithmetic operators (with other deltas).
64 RtpTimeDelta operator+(RtpTimeDelta rhs) const {
65 return RtpTimeDelta(value_ + rhs.value_);
66 }
67 RtpTimeDelta operator-(RtpTimeDelta rhs) const {
68 return RtpTimeDelta(value_ - rhs.value_);
69 }
70 RtpTimeDelta& operator+=(RtpTimeDelta rhs) { return (*this = (*this + rhs)); }
71 RtpTimeDelta& operator-=(RtpTimeDelta rhs) { return (*this = (*this - rhs)); }
72 RtpTimeDelta operator-() const { return RtpTimeDelta(-value_); }
73
74 // Multiplicative operators (with other deltas).
75 int64_t operator/(RtpTimeDelta rhs) const { return value_ / rhs.value_; }
76 RtpTimeDelta operator%(RtpTimeDelta rhs) const {
77 return RtpTimeDelta(value_ % rhs.value_);
78 }
79 RtpTimeDelta& operator%=(RtpTimeDelta rhs) { return (*this = (*this % rhs)); }
80
81 // Multiplicative operators (with integer types).
82 template <typename IntType>
83 RtpTimeDelta operator*(IntType rhs) const {
84 static_assert(std::numeric_limits<IntType>::is_integer,
85 "|rhs| must be a POD integer type");
86 return RtpTimeDelta(value_ * rhs);
87 }
88 template <typename IntType>
89 RtpTimeDelta operator/(IntType rhs) const {
90 static_assert(std::numeric_limits<IntType>::is_integer,
91 "|rhs| must be a POD integer type");
92 return RtpTimeDelta(value_ / rhs);
93 }
94 template <typename IntType>
95 RtpTimeDelta& operator*=(IntType rhs) { return (*this = (*this * rhs)); }
96 template <typename IntType>
97 RtpTimeDelta& operator/=(IntType rhs) { return (*this = (*this / rhs)); }
98
99 // Maps this RtpTimeDelta to an approximate TimeDelta using the given
100 // RTP timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
101 // RtpTimeDelta.
102 base::TimeDelta ToTimeDelta(int rtp_timebase) const;
103
104 // Maps the TimeDelta to an approximate RtpTimeDelta using the given RTP
105 // timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
106 // RtpTimeDelta.
107 static RtpTimeDelta FromTimeDelta(base::TimeDelta delta, int rtp_timebase);
108
109 // Construct a RtpTimeDelta from an exact number of ticks.
110 static RtpTimeDelta FromTicks(int64_t ticks);
111
112 private:
113 friend class ExpandedValueBase<int64_t, RtpTimeDelta>;
114 friend class RtpTimeTicks;
115 friend std::ostream& operator<<(std::ostream& out, const RtpTimeDelta rhs);
116
117 explicit RtpTimeDelta(int64_t ticks) : ExpandedValueBase(ticks) {}
118
119 int64_t value() const { return value_; }
120 };
121
122 // A media timestamp whose timebase matches the periodicity of the content
123 // (e.g., for audio, the timebase would be the sampling frequency). This data
124 // type is modeled off of base::TimeTicks.
125 //
126 // This data type wraps a value, providing only the meaningful set of math
127 // operations that may be performed on the value. The difference between two
128 // RtpTimeTickses is a RtpTimeDelta. Likewise, adding or subtracting a
129 // RtpTimeTicks with a RtpTimeDelta produces an off-set RtpTimeTicks.
130 //
131 // The base class provides bit truncation/extension features for
132 // wire-formatting, and also the comparison operators.
133 //
134 // Usage example:
135 //
136 // // Time math.
137 // RtpTimeTicks origin;
138 // RtpTimeTicks at_one_second =
139 // origin + RtpTimeDelta::FromTicks(kAudioSamplingRate);
140 // RtpTimeTicks at_two_seconds =
141 // at_one_second + RtpTimeDelta::FromTicks(kAudioSamplingRate);
142 // RtpTimeDelta elasped_in_between = at_two_seconds - at_one_second;
143 // RtpTimeDelta thrice_as_much_elasped = elasped_in_between * 3;
144 // RtpTimeTicks at_four_seconds = at_one_second + thrice_as_much_elasped;
145 //
146 // // Logging convenience.
147 // DLOG(INFO) << "The RTP timestamp is " << at_four_seconds;
148 //
149 // // Convert (approximately!) between RTP timebase and media timestamps in
150 // // microsecond timebase:
151 // base::TimeDelta four_seconds_timestamp =
152 // at_four_seconds.ToTimeDelta(kAudioSamplingRate);
153 // video_frame->set_timestamp(four_seconds_timestamp);
154 // RtpTimeTicks four_seconds_rtp = RtpTimeDelta::FromTimeDelta(
155 // video_frame->timestamp(), kAudioSamplingRate);
156 class RtpTimeTicks : public ExpandedValueBase<int64_t, RtpTimeTicks> {
157 public:
158 RtpTimeTicks() : ExpandedValueBase(0) {}
159
160 // Compute the difference between two RtpTimeTickses.
161 RtpTimeDelta operator-(RtpTimeTicks rhs) const {
162 return RtpTimeDelta(value_ - rhs.value_);
163 }
164
165 // Return a new RtpTimeTicks before or after this one.
166 RtpTimeTicks operator+(RtpTimeDelta rhs) const {
167 return RtpTimeTicks(value_ + rhs.value());
168 }
169 RtpTimeTicks operator-(RtpTimeDelta rhs) const {
170 return RtpTimeTicks(value_ - rhs.value());
171 }
172 RtpTimeTicks& operator+=(RtpTimeDelta rhs) { return (*this = (*this + rhs)); }
173 RtpTimeTicks& operator-=(RtpTimeDelta rhs) { return (*this = (*this - rhs)); }
174
175 // Maps this RtpTimeTicks to an approximate TimeDelta using the given
176 // RTP timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
177 // RtpTimeTicks.
178 base::TimeDelta ToTimeDelta(int rtp_timebase) const;
179
180 // Maps the TimeDelta to an approximate RtpTimeTicks using the given RTP
181 // timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
182 // RtpTimeTicks.
183 static RtpTimeTicks FromTimeDelta(base::TimeDelta delta, int rtp_timebase);
184
185 private:
186 friend class ExpandedValueBase<int64_t, RtpTimeTicks>;
187 friend std::ostream& operator<<(std::ostream& out, const RtpTimeTicks rhs);
188
189 explicit RtpTimeTicks(int64_t value) : ExpandedValueBase(value) {}
190
191 int64_t value() const { return value_; }
192 };
193
194 } // namespace cast
195 } // namespace media
196
197 #endif // MEDIA_CAST_COMMON_RTP_TIME_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698