pkasting@chromium.org 0b1534c52e Use int64_t for milliseconds more often, primarily for TimeUntilNextProcess.
This fixes a variety of MSVC warnings about value truncations when implicitly
storing the 64-bit values we get back from e.g. TimeTicks in 32-bit objects, and
removes the need for a number of explicit casts.

This also moves a number of constants so they're declared right where they're used, which is easier to read and maintain, and makes some of them of integral type rather than using the "enum hack".

BUG=chromium:81439
TEST=none
R=tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/33649004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7905 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-12-15 22:09:40 +00:00

102 lines
3.2 KiB
C++

/*
* Copyright (c) 2011 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_conference_mixer/source/time_scheduler.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
namespace webrtc {
TimeScheduler::TimeScheduler(const int64_t periodicityInMs)
: _crit(CriticalSectionWrapper::CreateCriticalSection()),
_isStarted(false),
_lastPeriodMark(),
_periodicityInMs(periodicityInMs),
_periodicityInTicks(TickTime::MillisecondsToTicks(periodicityInMs)),
_missedPeriods(0)
{
}
TimeScheduler::~TimeScheduler()
{
delete _crit;
}
int32_t TimeScheduler::UpdateScheduler()
{
CriticalSectionScoped cs(_crit);
if(!_isStarted)
{
_isStarted = true;
_lastPeriodMark = TickTime::Now();
return 0;
}
// Don't perform any calculations until the debt of pending periods have
// been worked off.
if(_missedPeriods > 0)
{
_missedPeriods--;
return 0;
}
// Calculate the time that has past since previous call to this function.
TickTime tickNow = TickTime::Now();
TickInterval amassedTicks = tickNow - _lastPeriodMark;
int64_t amassedMs = amassedTicks.Milliseconds();
// Calculate the number of periods the time that has passed correspond to.
int64_t periodsToClaim = amassedMs / _periodicityInMs;
// One period will be worked off by this call. Make sure that the number of
// pending periods don't end up being negative (e.g. if this function is
// called to often).
if(periodsToClaim < 1)
{
periodsToClaim = 1;
}
// Update the last period mark without introducing any drifting.
// Note that if this fuunction is called to often _lastPeriodMark can
// refer to a time in the future which in turn will yield TimeToNextUpdate
// that is greater than the periodicity
for(int64_t i = 0; i < periodsToClaim; i++)
{
_lastPeriodMark += _periodicityInTicks;
}
// Update the total amount of missed periods note that we have processed
// one period hence the - 1
_missedPeriods += periodsToClaim - 1;
return 0;
}
int32_t TimeScheduler::TimeToNextUpdate(
int64_t& updateTimeInMS) const
{
CriticalSectionScoped cs(_crit);
// Missed periods means that the next UpdateScheduler() should happen
// immediately.
if(_missedPeriods > 0)
{
updateTimeInMS = 0;
return 0;
}
// Calculate the time (in ms) that has past since last call to
// UpdateScheduler()
TickTime tickNow = TickTime::Now();
TickInterval ticksSinceLastUpdate = tickNow - _lastPeriodMark;
const int64_t millisecondsSinceLastUpdate =
ticksSinceLastUpdate.Milliseconds();
updateTimeInMS = _periodicityInMs - millisecondsSinceLastUpdate;
updateTimeInMS = (updateTimeInMS < 0) ? 0 : updateTimeInMS;
return 0;
}
} // namespace webrtc