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