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

Unified Diff: media/cast/common/rtp_time.cc

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.cc
diff --git a/media/cast/common/rtp_time.cc b/media/cast/common/rtp_time.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8233f57cf30446bf97a041f4b155466d83a0f5a5
--- /dev/null
+++ b/media/cast/common/rtp_time.cc
@@ -0,0 +1,83 @@
+// 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.
+
+#include "media/cast/common/rtp_time.h"
+
+#include <limits>
+
+namespace media {
+namespace cast {
+
+namespace {
+
+// Returns the base::TimeDelta nearest to the time represented by a tick count
+// in the given timebase.
+base::TimeDelta TicksToTimeDelta(int64_t ticks, int timebase) {
+ DCHECK_GT(timebase, 0);
+ const double micros = static_cast<double>(ticks) / timebase *
+ base::Time::kMicrosecondsPerSecond +
+ 0.5 /* rounding */;
Irfan 2015/12/09 21:24:28 nit: alignment ?
miu 2015/12/10 00:38:36 This is what `git cl format media` produced. ;-)
+ DCHECK_LT(micros, static_cast<double>(std::numeric_limits<int64_t>::max()));
+ return base::TimeDelta::FromMicroseconds(static_cast<int64_t>(micros));
+}
+
+// Returns the tick count in the given timebase nearest to the base::TimeDelta.
+int64_t TimeDeltaToTicks(base::TimeDelta delta, int timebase) {
+ DCHECK_GT(timebase, 0);
+ const double ticks = delta.InSecondsF() * timebase + 0.5 /* rounding */;
+ DCHECK_LT(ticks, static_cast<double>(std::numeric_limits<int64_t>::max()));
+ return static_cast<int64_t>(ticks);
+}
+
+} // namespace
+
+base::TimeDelta RtpTimeDelta::ToTimeDelta(int rtp_timebase) const {
+ return TicksToTimeDelta(value_, rtp_timebase);
+}
+
+void RtpTimeDelta::OutputToLog(std::ostream& out) const {
+ if (value_ >= 0)
+ out << "RTPd+";
+ else
+ out << "RTPd";
+ out << value_;
+}
+
+// static
+RtpTimeDelta RtpTimeDelta::FromTimeDelta(base::TimeDelta delta,
+ int rtp_timebase) {
+ return RtpTimeDelta(TimeDeltaToTicks(delta, rtp_timebase));
+}
+
+// static
+RtpTimeDelta RtpTimeDelta::FromTicks(int64_t ticks) {
+ return RtpTimeDelta(ticks);
+}
+
+base::TimeDelta RtpTimeTicks::ToTimeDelta(int rtp_timebase) const {
+ return TicksToTimeDelta(value_, rtp_timebase);
+}
+
+void RtpTimeTicks::OutputToLog(std::ostream& out) const {
+ out << "RTP@" << value_;
+}
+
+// static
+RtpTimeTicks RtpTimeTicks::FromTimeDelta(base::TimeDelta delta,
+ int rtp_timebase) {
+ return RtpTimeTicks(TimeDeltaToTicks(delta, rtp_timebase));
+}
+
+std::ostream& operator<<(std::ostream& out, RtpTimeDelta rhs) {
+ rhs.OutputToLog(out);
+ return out;
+}
+
+std::ostream& operator<<(std::ostream& out, RtpTimeTicks rhs) {
+ rhs.OutputToLog(out);
+ return out;
+}
+
+} // namespace cast
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698