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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: media/cast/common/rtp_time.h
diff --git a/media/cast/common/rtp_time.h b/media/cast/common/rtp_time.h
new file mode 100644
index 0000000000000000000000000000000000000000..71ec5b2e5793f397150cc0d4f879140b8e4e3195
--- /dev/null
+++ b/media/cast/common/rtp_time.h
@@ -0,0 +1,155 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIA_CAST_COMMON_RTP_TIME_H_
+#define MEDIA_CAST_COMMON_RTP_TIME_H_
+
+#include <stdint.h>
+
+#include <limits>
+
+#include "base/time/time.h"
+#include "media/cast/common/expanded_value_base.h"
+
+namespace media {
+namespace cast {
+
+class RtpTimeTicks; // Forward declaration (see below).
+
+// The difference between two RtpTimeTickses. This data type is modeled off of
+// base::TimeDelta, and used for performing compiler-checked arithmetic with
+// RtpTimeTicks.
+//
+// This data type wraps a value, providing only the meaningful set of math
+// operations that may be performed on the value. RtpTimeDeltas may be
+// added/subtracted with other RtpTimeDeltas to produce a RtpTimeDelta holding
+// the sum/difference. RtpTimeDeltas may also be multiplied or divided by
+// integer amounts. Finally, RtpTimeDeltas may be divided by other
+// RtpTimeDeltas to compute a number of periods (floor'ed to an integer), or
+// modulo each other to determine a time period remainder.
+//
+// The base class provides bit truncation/extension features for
+// wire-formatting, and also the comparison operators.
+class RtpTimeDelta : public ExpandedValueBase<int64_t, RtpTimeDelta> {
+ public:
+ RtpTimeDelta() : ExpandedValueBase(0) {}
+
+ // Arithmetic operators (with other deltas).
+ RtpTimeDelta operator+(RtpTimeDelta rhs) const {
+ return RtpTimeDelta(value_ + rhs.value_);
+ }
+ RtpTimeDelta operator-(RtpTimeDelta rhs) const {
+ return RtpTimeDelta(value_ - rhs.value_);
+ }
+ RtpTimeDelta& operator+=(RtpTimeDelta rhs) { return (*this = (*this + rhs)); }
+ RtpTimeDelta& operator-=(RtpTimeDelta rhs) { return (*this = (*this - rhs)); }
+ RtpTimeDelta operator-() const { return RtpTimeDelta(-value_); }
+
+ // Multiplicative operators (with integer types).
+ template <typename IntType>
+ RtpTimeDelta operator*(IntType rhs) const {
+ static_assert(std::numeric_limits<IntType>::is_integer,
+ "|rhs| must be a POD integer type");
+ return RtpTimeDelta(value_ * rhs);
+ }
+ template <typename IntType>
+ RtpTimeDelta operator/(IntType rhs) const {
+ static_assert(std::numeric_limits<IntType>::is_integer,
+ "|rhs| must be a POD integer type");
+ return RtpTimeDelta(value_ / rhs);
+ }
+ template <typename IntType>
+ RtpTimeDelta& operator*=(IntType rhs) { return (*this = (*this * rhs)); }
+ template <typename IntType>
+ RtpTimeDelta& operator/=(IntType rhs) { return (*this = (*this / rhs)); }
+
+ // 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.
+ int64_t operator/(RtpTimeDelta rhs) const { return value_ / rhs.value_; }
+ RtpTimeDelta operator%(RtpTimeDelta rhs) const {
+ return RtpTimeDelta(value_ % rhs.value_);
+ }
+ RtpTimeDelta& operator%=(RtpTimeDelta rhs) { return (*this = (*this % rhs)); }
+
+ // Maps this RtpTimeDelta to an approximate TimeDelta using the given
+ // RTP timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
+ // RtpTimeDelta.
+ base::TimeDelta ToTimeDelta(int rtp_timebase) const;
+
+ // Write-out a human-readable version of this RtpTimeDelta.
+ void OutputToLog(std::ostream& out) const;
+
+ // Maps the TimeDelta to an approximate RtpTimeDelta using the given RTP
+ // timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
+ // RtpTimeDelta.
+ static RtpTimeDelta FromTimeDelta(base::TimeDelta delta, int rtp_timebase);
+
+ // Construct a RtpTimeDelta from an exact number of ticks.
+ static RtpTimeDelta FromTicks(int64_t ticks);
+
+ private:
+ friend class ExpandedValueBase<int64_t, RtpTimeDelta>;
+ friend class RtpTimeTicks;
+
+ explicit RtpTimeDelta(int64_t ticks) : ExpandedValueBase(ticks) {}
+
+ int64_t value() const { return value_; }
+};
+
+// A media timestamp whose timebase matches the periodicity of the content
+// (e.g., for audio, the timebase would be the sampling frequency). This data
+// type is modeled off of base::TimeTicks.
+//
+// This data type wraps a value, providing only the meaningful set of math
+// operations that may be performed on the value. The difference between two
+// RtpTimeTickses is a RtpTimeDelta. Likewise, adding or subtracting a
+// RtpTimeTicks with a RtpTimeDelta produces an off-set RtpTimeTicks.
+//
+// The base class provides bit truncation/extension features for
+// wire-formatting, and also the comparison operators.
+class RtpTimeTicks : public ExpandedValueBase<int64_t, RtpTimeTicks> {
+ public:
+ RtpTimeTicks() : ExpandedValueBase(0) {}
+
+ // Compute the difference between two RtpTimeTickses.
+ RtpTimeDelta operator-(RtpTimeTicks rhs) const {
+ return RtpTimeDelta(value_ - rhs.value_);
+ }
+
+ // Return a new RtpTimeTicks before or after this one.
+ RtpTimeTicks operator+(RtpTimeDelta rhs) const {
+ return RtpTimeTicks(value_ + rhs.value());
+ }
+ RtpTimeTicks operator-(RtpTimeDelta rhs) const {
+ return RtpTimeTicks(value_ - rhs.value());
+ }
+ RtpTimeTicks& operator+=(RtpTimeDelta rhs) { return (*this = (*this + rhs)); }
+ RtpTimeTicks& operator-=(RtpTimeDelta rhs) { return (*this = (*this - rhs)); }
+
+ // Maps this RtpTimeTicks to an approximate TimeDelta using the given
+ // RTP timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
+ // RtpTimeTicks.
+ base::TimeDelta ToTimeDelta(int rtp_timebase) const;
+
+ // Write-out a human-readable version of this RtpTimeTicks.
+ 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<<
+
+ // Maps the TimeDelta to an approximate RtpTimeTicks using the given RTP
+ // timebase. Assumes a zero-valued TimeDelta corresponds to a zero-valued
+ // RtpTimeTicks.
+ static RtpTimeTicks FromTimeDelta(base::TimeDelta delta, int rtp_timebase);
+
+ private:
+ friend class ExpandedValueBase<int64_t, RtpTimeTicks>;
+
+ explicit RtpTimeTicks(int64_t value) : ExpandedValueBase(value) {}
+};
+
+// Convenience operator overloads for logging.
+std::ostream& operator<<(std::ostream& out, RtpTimeDelta rhs);
+std::ostream& operator<<(std::ostream& out, RtpTimeTicks rhs);
+
+} // namespace cast
+} // namespace media
+
+#endif // MEDIA_CAST_COMMON_RTP_TIME_H_

Powered by Google App Engine
This is Rietveld 408576698