Home | History | Annotate | Download | only in oboeservice
      1 /*
      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 
     22 using namespace aaudio;
     23 
     24 void TimestampScheduler::start(int64_t startTime) {
     25     mStartTime = startTime;
     26     mLastTime = startTime;
     27 }
     28 
     29 int64_t TimestampScheduler::nextAbsoluteTime() {
     30     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;
     38     int64_t sleepTime = minPeriodsToDelay * mBurstPeriod;
     39     // Generate a random rectangular distribution one burst wide so that we get
     40     // an uncorrelated sampling of the MMAP pointer.
     41     sleepTime += (int64_t)(random() * mBurstPeriod / RAND_MAX);
     42     mLastTime += sleepTime;
     43     return mLastTime;
     44 }
     45