Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 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 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | using namespace aaudio; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 23 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 24 | void TimestampScheduler::start(int64_t startTime) { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 25 | mStartTime = startTime; |
| 26 | mLastTime = startTime; |
| 27 | } |
| 28 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 29 | int64_t TimestampScheduler::nextAbsoluteTime() { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 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; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 38 | int64_t sleepTime = minPeriodsToDelay * mBurstPeriod; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 39 | // Generate a random rectangular distribution one burst wide so that we get |
| 40 | // an uncorrelated sampling of the MMAP pointer. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 41 | sleepTime += (int64_t)(random() * mBurstPeriod / RAND_MAX); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 42 | mLastTime += sleepTime; |
| 43 | return mLastTime; |
| 44 | } |