Chromium Code Reviews| 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..b7be9326195e7c1a05389e08679070394a901edf |
| --- /dev/null |
| +++ b/media/cast/common/rtp_time.cc |
| @@ -0,0 +1,74 @@ |
| +// 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> |
| +#include <sstream> |
| + |
| +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 */; |
|
apacible
2015/12/11 23:54:59
nit: indent to align with previous line
miu
2015/12/12 00:23:30
Hahahah. Irfan caught this too. This is what `gi
|
| + 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 |
| + |
| +std::ostream& operator<<(std::ostream& out, const RtpTimeDelta rhs) { |
| + if (rhs.value_ >= 0) |
| + out << "RTP+"; |
| + else |
| + out << "RTP"; |
| + return out << rhs.value_; |
| +} |
| + |
| +std::ostream& operator<<(std::ostream& out, const RtpTimeTicks rhs) { |
| + return out << "RTP@" << rhs.value_; |
| +} |
| + |
| +base::TimeDelta RtpTimeDelta::ToTimeDelta(int rtp_timebase) const { |
| + return TicksToTimeDelta(value_, rtp_timebase); |
| +} |
| + |
| +// 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); |
| +} |
| + |
| +// static |
| +RtpTimeTicks RtpTimeTicks::FromTimeDelta(base::TimeDelta delta, |
| + int rtp_timebase) { |
| + return RtpTimeTicks(TimeDeltaToTicks(delta, rtp_timebase)); |
| +} |
| + |
| +} // namespace cast |
| +} // namespace media |