blob: d54996f4ab881a78c2e50f534341db4cecb4a965 [file] [log] [blame]
Phil Burkdec33ab2017-01-17 14:48:16 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// for random()
18#include <stdlib.h>
19
20#include "TimestampScheduler.h"
21
Phil Burk5ed503c2017-02-01 09:38:15 -080022using namespace aaudio;
Phil Burkdec33ab2017-01-17 14:48:16 -080023
Phil Burk3316d5e2017-02-15 11:23:01 -080024void TimestampScheduler::start(int64_t startTime) {
Phil Burkdec33ab2017-01-17 14:48:16 -080025 mStartTime = startTime;
26 mLastTime = startTime;
27}
28
Phil Burk3316d5e2017-02-15 11:23:01 -080029int64_t TimestampScheduler::nextAbsoluteTime() {
Phil Burkdec33ab2017-01-17 14:48:16 -080030 int64_t periodsElapsed = (mLastTime - mStartTime) / mBurstPeriod;
31 // This is an arbitrary schedule that could probably be improved.
32 // It starts out sending a timestamp on every period because we want to
33 // get an accurate picture when the stream starts. Then it slows down
34 // to the occasional timestamps needed to detect a slow drift.
35 int64_t minPeriodsToDelay = (periodsElapsed < 10) ? 1 :
36 (periodsElapsed < 100) ? 3 :
37 (periodsElapsed < 1000) ? 10 : 50;
Phil Burk3316d5e2017-02-15 11:23:01 -080038 int64_t sleepTime = minPeriodsToDelay * mBurstPeriod;
Phil Burkdec33ab2017-01-17 14:48:16 -080039 // Generate a random rectangular distribution one burst wide so that we get
40 // an uncorrelated sampling of the MMAP pointer.
Phil Burk3316d5e2017-02-15 11:23:01 -080041 sleepTime += (int64_t)(random() * mBurstPeriod / RAND_MAX);
Phil Burkdec33ab2017-01-17 14:48:16 -080042 mLastTime += sleepTime;
43 return mLastTime;
44}