OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/base/timeutils.h" |
| 12 #include "webrtc/modules/audio_conference_mixer/source/time_scheduler.h" |
| 13 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 14 |
| 15 namespace webrtc { |
| 16 TimeScheduler::TimeScheduler(const int64_t periodicityInMs) |
| 17 : _crit(CriticalSectionWrapper::CreateCriticalSection()), |
| 18 _isStarted(false), |
| 19 _lastPeriodMark(), |
| 20 _periodicityInMs(periodicityInMs), |
| 21 _periodicityInTicks(periodicityInMs * rtc::kNumNanosecsPerMillisec), |
| 22 _missedPeriods(0) {} |
| 23 |
| 24 TimeScheduler::~TimeScheduler() { |
| 25 delete _crit; |
| 26 } |
| 27 |
| 28 int32_t TimeScheduler::UpdateScheduler() { |
| 29 CriticalSectionScoped cs(_crit); |
| 30 if (!_isStarted) { |
| 31 _isStarted = true; |
| 32 _lastPeriodMark = rtc::TimeNanos(); |
| 33 return 0; |
| 34 } |
| 35 // Don't perform any calculations until the debt of pending periods have |
| 36 // been worked off. |
| 37 if (_missedPeriods > 0) { |
| 38 _missedPeriods--; |
| 39 return 0; |
| 40 } |
| 41 |
| 42 // Calculate the time that has past since previous call to this function. |
| 43 int64_t tickNow = rtc::TimeNanos(); |
| 44 int64_t amassedTicks = tickNow - _lastPeriodMark; |
| 45 int64_t amassedMs = amassedTicks / rtc::kNumNanosecsPerMillisec; |
| 46 |
| 47 // Calculate the number of periods the time that has passed correspond to. |
| 48 int64_t periodsToClaim = amassedMs / _periodicityInMs; |
| 49 |
| 50 // One period will be worked off by this call. Make sure that the number of |
| 51 // pending periods don't end up being negative (e.g. if this function is |
| 52 // called to often). |
| 53 if (periodsToClaim < 1) { |
| 54 periodsToClaim = 1; |
| 55 } |
| 56 |
| 57 // Update the last period mark without introducing any drifting. |
| 58 // Note that if this fuunction is called to often _lastPeriodMark can |
| 59 // refer to a time in the future which in turn will yield TimeToNextUpdate |
| 60 // that is greater than the periodicity |
| 61 for (int64_t i = 0; i < periodsToClaim; i++) { |
| 62 _lastPeriodMark += _periodicityInTicks; |
| 63 } |
| 64 |
| 65 // Update the total amount of missed periods note that we have processed |
| 66 // one period hence the - 1 |
| 67 _missedPeriods += periodsToClaim - 1; |
| 68 return 0; |
| 69 } |
| 70 |
| 71 int32_t TimeScheduler::TimeToNextUpdate(int64_t& updateTimeInMS) const { |
| 72 CriticalSectionScoped cs(_crit); |
| 73 // Missed periods means that the next UpdateScheduler() should happen |
| 74 // immediately. |
| 75 if (_missedPeriods > 0) { |
| 76 updateTimeInMS = 0; |
| 77 return 0; |
| 78 } |
| 79 |
| 80 // Calculate the time (in ms) that has past since last call to |
| 81 // UpdateScheduler() |
| 82 int64_t tickNow = rtc::TimeNanos(); |
| 83 int64_t ticksSinceLastUpdate = tickNow - _lastPeriodMark; |
| 84 const int64_t millisecondsSinceLastUpdate = |
| 85 ticksSinceLastUpdate / rtc::kNumNanosecsPerMillisec; |
| 86 |
| 87 updateTimeInMS = _periodicityInMs - millisecondsSinceLastUpdate; |
| 88 updateTimeInMS = (updateTimeInMS < 0) ? 0 : updateTimeInMS; |
| 89 return 0; |
| 90 } |
| 91 } // namespace webrtc |
OLD | NEW |