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