Add a new TickTimer class to NetEq

The new class is intended to be used as a central time-keeping object
inside NetEq. The actual use of the class will come in subsequent
changes.

BUG=webrtc:5608

Review URL: https://codereview.webrtc.org/1910523003

Cr-Commit-Position: refs/heads/master@{#12477}
This commit is contained in:
henrik.lundin
2016-04-22 13:21:43 -07:00
committed by Commit bot
parent 4311ba59d8
commit 8053f79bd9
6 changed files with 273 additions and 0 deletions

View File

@ -807,6 +807,8 @@ source_set("neteq") {
"neteq/statistics_calculator.h",
"neteq/sync_buffer.cc",
"neteq/sync_buffer.h",
"neteq/tick_timer.cc",
"neteq/tick_timer.h",
"neteq/time_stretch.cc",
"neteq/time_stretch.h",
"neteq/timestamp_scaler.cc",

View File

@ -119,6 +119,8 @@
'rtcp.h',
'sync_buffer.cc',
'sync_buffer.h',
'tick_timer.cc',
'tick_timer.h',
'timestamp_scaler.cc',
'timestamp_scaler.h',
'time_stretch.cc',

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
namespace webrtc {
TickTimer::Stopwatch::Stopwatch(const TickTimer& ticktimer)
: ticktimer_(ticktimer), starttick_(ticktimer.ticks()) {}
TickTimer::Countdown::Countdown(const TickTimer& ticktimer,
uint64_t ticks_to_count)
: stopwatch_(ticktimer.GetNewStopwatch()),
ticks_to_count_(ticks_to_count) {}
TickTimer::Countdown::~Countdown() = default;
} // namespace webrtc

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_TICK_TIMER_H_
#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TICK_TIMER_H_
#include <memory>
#include "webrtc/base/checks.h"
#include "webrtc/base/constructormagic.h"
#include "webrtc/typedefs.h"
namespace webrtc {
// Implements a time counter. The counter is advanced with the Increment()
// methods, and is queried with the ticks() accessor. It is assumed that one
// "tick" och the counter corresponds to 10 ms.
// A TickTimer object can provide two types of associated time-measuring
// objects: Stopwatch and Countdown.
class TickTimer {
public:
// Stopwatch measures time elapsed since it was started, by querying the
// associated TickTimer for the current time. The intended use is to request a
// new Stopwatch object from a TickTimer object with the GetNewStopwatch()
// method. Note: since the Stopwatch object contains a reference to the
// TickTimer it is associated with, it cannot outlive the TickTimer.
class Stopwatch {
public:
explicit Stopwatch(const TickTimer& ticktimer);
uint64_t ElapsedTicks() const { return ticktimer_.ticks() - starttick_; }
uint64_t ElapsedMs() const {
const uint64_t elapsed_ticks = ticktimer_.ticks() - starttick_;
const int ms_per_tick = ticktimer_.ms_per_tick();
return elapsed_ticks < UINT64_MAX / ms_per_tick
? elapsed_ticks * ms_per_tick
: UINT64_MAX;
}
private:
const TickTimer& ticktimer_;
const uint64_t starttick_;
};
// Countdown counts down from a given start value with each tick of the
// associated TickTimer, until zero is reached. The Finished() method will
// return true if zero has been reached, false otherwise. The intended use is
// to request a new Countdown object from a TickTimer object with the
// GetNewCountdown() method. Note: since the Countdown object contains a
// reference to the TickTimer it is associated with, it cannot outlive the
// TickTimer.
class Countdown {
public:
Countdown(const TickTimer& ticktimer, uint64_t ticks_to_count);
~Countdown();
bool Finished() const {
return stopwatch_->ElapsedTicks() >= ticks_to_count_;
}
private:
const std::unique_ptr<Stopwatch> stopwatch_;
const uint64_t ticks_to_count_;
};
TickTimer() : TickTimer(10) {}
explicit TickTimer(int ms_per_tick) : ms_per_tick_(ms_per_tick) {
RTC_DCHECK_GT(ms_per_tick_, 0);
}
void Increment() { ++ticks_; }
// Mainly intended for testing.
void Increment(uint64_t x) { ticks_ += x; }
uint64_t ticks() const { return ticks_; }
int ms_per_tick() const { return ms_per_tick_; }
// Returns a new Stopwatch object, based on the current TickTimer. Note that
// the new Stopwatch object contains a reference to the current TickTimer,
// and must therefore not outlive the TickTimer.
std::unique_ptr<Stopwatch> GetNewStopwatch() const {
return std::unique_ptr<Stopwatch>(new Stopwatch(*this));
}
// Returns a new Countdown object, based on the current TickTimer. Note that
// the new Countdown object contains a reference to the current TickTimer,
// and must therefore not outlive the TickTimer.
std::unique_ptr<Countdown> GetNewCountdown(uint64_t ticks_to_count) const {
return std::unique_ptr<Countdown>(new Countdown(*this, ticks_to_count));
}
private:
uint64_t ticks_ = 0;
const int ms_per_tick_;
RTC_DISALLOW_COPY_AND_ASSIGN(TickTimer);
};
} // namespace webrtc
#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TICK_TIMER_H_

View File

@ -0,0 +1,133 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace webrtc {
// Verify that the default value for ms_per_tick is 10.
TEST(TickTimer, DefaultMsPerTick) {
TickTimer tt;
EXPECT_EQ(10, tt.ms_per_tick());
}
TEST(TickTimer, CustomMsPerTick) {
TickTimer tt(17);
EXPECT_EQ(17, tt.ms_per_tick());
}
TEST(TickTimer, Increment) {
TickTimer tt;
EXPECT_EQ(0u, tt.ticks());
tt.Increment();
EXPECT_EQ(1u, tt.ticks());
for (int i = 0; i < 17; ++i) {
tt.Increment();
}
EXPECT_EQ(18u, tt.ticks());
tt.Increment(17);
EXPECT_EQ(35u, tt.ticks());
}
TEST(TickTimer, WrapAround) {
TickTimer tt;
tt.Increment(UINT64_MAX);
EXPECT_EQ(UINT64_MAX, tt.ticks());
tt.Increment();
EXPECT_EQ(0u, tt.ticks());
}
TEST(TickTimer, Stopwatch) {
TickTimer tt;
// Increment it a "random" number of steps.
tt.Increment(17);
std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
ASSERT_TRUE(sw);
EXPECT_EQ(0u, sw->ElapsedTicks()); // Starts at zero.
EXPECT_EQ(0u, sw->ElapsedMs());
tt.Increment();
EXPECT_EQ(1u, sw->ElapsedTicks()); // Increases with the TickTimer.
EXPECT_EQ(10u, sw->ElapsedMs());
}
TEST(TickTimer, StopwatchWrapAround) {
TickTimer tt;
tt.Increment(UINT64_MAX);
std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
ASSERT_TRUE(sw);
tt.Increment();
EXPECT_EQ(0u, tt.ticks());
EXPECT_EQ(1u, sw->ElapsedTicks());
EXPECT_EQ(10u, sw->ElapsedMs());
tt.Increment();
EXPECT_EQ(1u, tt.ticks());
EXPECT_EQ(2u, sw->ElapsedTicks());
EXPECT_EQ(20u, sw->ElapsedMs());
}
TEST(TickTimer, StopwatchMsOverflow) {
TickTimer tt;
std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
ASSERT_TRUE(sw);
tt.Increment(UINT64_MAX / 10);
EXPECT_EQ(UINT64_MAX, sw->ElapsedMs());
tt.Increment();
EXPECT_EQ(UINT64_MAX, sw->ElapsedMs());
tt.Increment(UINT64_MAX - tt.ticks());
EXPECT_EQ(UINT64_MAX, tt.ticks());
EXPECT_EQ(UINT64_MAX, sw->ElapsedMs());
}
TEST(TickTimer, StopwatchWithCustomTicktime) {
const int kMsPerTick = 17;
TickTimer tt(kMsPerTick);
std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
ASSERT_TRUE(sw);
EXPECT_EQ(0u, sw->ElapsedMs());
tt.Increment();
EXPECT_EQ(static_cast<uint64_t>(kMsPerTick), sw->ElapsedMs());
}
TEST(TickTimer, Countdown) {
TickTimer tt;
// Increment it a "random" number of steps.
tt.Increment(4711);
std::unique_ptr<TickTimer::Countdown> cd = tt.GetNewCountdown(17);
ASSERT_TRUE(cd);
EXPECT_FALSE(cd->Finished());
tt.Increment();
EXPECT_FALSE(cd->Finished());
tt.Increment(16); // Total increment is now 17.
EXPECT_TRUE(cd->Finished());
// Further increments do not change the state.
tt.Increment();
EXPECT_TRUE(cd->Finished());
tt.Increment(1234);
EXPECT_TRUE(cd->Finished());
}
} // namespace webrtc