blob: edf6e97370334971101aaf056922c4314445f43d [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -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
Phil Burk5ed503c2017-02-01 09:38:15 -080017#define LOG_TAG "AAudio"
Phil Burk204a1632017-01-03 17:23:43 -080018//#define LOG_NDEBUG 0
Tom Cherry357552e2017-07-12 13:48:26 -070019#include <log/log.h>
Phil Burk204a1632017-01-03 17:23:43 -080020
21#include <stdint.h>
Phil Burk204a1632017-01-03 17:23:43 -080022
Phil Burk3316d5e2017-02-15 11:23:01 -080023#include "utility/AudioClock.h"
Phil Burk204a1632017-01-03 17:23:43 -080024#include "IsochronousClockModel.h"
25
Phil Burk5ed503c2017-02-01 09:38:15 -080026#define MIN_LATENESS_NANOS (10 * AAUDIO_NANOS_PER_MICROSECOND)
Phil Burk204a1632017-01-03 17:23:43 -080027
Phil Burk5ed503c2017-02-01 09:38:15 -080028using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080029
30IsochronousClockModel::IsochronousClockModel()
Phil Burk3316d5e2017-02-15 11:23:01 -080031 : mMarkerFramePosition(0)
32 , mMarkerNanoTime(0)
33 , mSampleRate(48000)
Phil Burk204a1632017-01-03 17:23:43 -080034 , mFramesPerBurst(64)
35 , mMaxLatenessInNanos(0)
Phil Burk204a1632017-01-03 17:23:43 -080036 , mState(STATE_STOPPED)
37{
38}
39
40IsochronousClockModel::~IsochronousClockModel() {
41}
42
Phil Burk87c9f642017-05-17 07:22:39 -070043void IsochronousClockModel::start(int64_t nanoTime) {
44 ALOGD("IsochronousClockModel::start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080045 mMarkerNanoTime = nanoTime;
46 mState = STATE_STARTING;
47}
48
Phil Burk87c9f642017-05-17 07:22:39 -070049void IsochronousClockModel::stop(int64_t nanoTime) {
50 ALOGD("IsochronousClockModel::stop(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080051 mMarkerNanoTime = nanoTime;
52 mMarkerFramePosition = convertTimeToPosition(nanoTime); // TODO should we do this?
53 mState = STATE_STOPPED;
54}
55
Phil Burk87c9f642017-05-17 07:22:39 -070056void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burk204a1632017-01-03 17:23:43 -080057 int64_t framesDelta = framePosition - mMarkerFramePosition;
58 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
59 if (nanosDelta < 1000) {
60 return;
61 }
62
Phil Burk87c9f642017-05-17 07:22:39 -070063// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080064// (long long)mMarkerFramePosition,
65// (long long)mMarkerNanoTime);
Phil Burk87c9f642017-05-17 07:22:39 -070066// ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080067// (long long)framePosition,
68// (long long)nanoTime);
69
70 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070071// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080072// (long long)expectedNanosDelta,
73// (long long)nanosDelta);
74
Phil Burk87c9f642017-05-17 07:22:39 -070075// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
76// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -080077 switch (mState) {
78 case STATE_STOPPED:
79 break;
80 case STATE_STARTING:
81 mMarkerFramePosition = framePosition;
82 mMarkerNanoTime = nanoTime;
83 mState = STATE_SYNCING;
84 break;
85 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -070086 // This will handle a burst of rapid transfer at the beginning.
Phil Burk204a1632017-01-03 17:23:43 -080087 if (nanosDelta < expectedNanosDelta) {
88 mMarkerFramePosition = framePosition;
89 mMarkerNanoTime = nanoTime;
90 } else {
Phil Burk87c9f642017-05-17 07:22:39 -070091// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -080092 mState = STATE_RUNNING;
93 }
94 break;
95 case STATE_RUNNING:
96 if (nanosDelta < expectedNanosDelta) {
97 // Earlier than expected timestamp.
98 // This data is probably more accurate so use it.
99 // or we may be drifting due to a slow HW clock.
100 mMarkerFramePosition = framePosition;
101 mMarkerNanoTime = nanoTime;
Phil Burk87c9f642017-05-17 07:22:39 -0700102// ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
103// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
Phil Burk204a1632017-01-03 17:23:43 -0800104 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
105 // Later than expected timestamp.
106 mMarkerFramePosition = framePosition;
107 mMarkerNanoTime = nanoTime - mMaxLatenessInNanos;
Phil Burk87c9f642017-05-17 07:22:39 -0700108// ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
109// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
110// (int) (mMaxLatenessInNanos / 1000));
Phil Burk204a1632017-01-03 17:23:43 -0800111 }
112 break;
113 default:
114 break;
115 }
Phil Burk204a1632017-01-03 17:23:43 -0800116}
117
118void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
119 mSampleRate = sampleRate;
120 update();
121}
122
123void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
124 mFramesPerBurst = framesPerBurst;
125 update();
126}
127
128void IsochronousClockModel::update() {
129 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
130 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
131}
132
Phil Burk3316d5e2017-02-15 11:23:01 -0800133int64_t IsochronousClockModel::convertDeltaPositionToTime(
134 int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800135 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800136}
137
Phil Burk3316d5e2017-02-15 11:23:01 -0800138int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800139 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800140}
141
Phil Burk87c9f642017-05-17 07:22:39 -0700142int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800143 if (mState == STATE_STOPPED) {
144 return mMarkerNanoTime;
145 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800146 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
147 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
148 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
149 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
150 int64_t time = (int64_t) (mMarkerNanoTime + nanosDelta);
Phil Burk87c9f642017-05-17 07:22:39 -0700151// ALOGD("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800152// (unsigned long long)framePosition,
153// (unsigned long long)time);
154 return time;
155}
156
Phil Burk87c9f642017-05-17 07:22:39 -0700157int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800158 if (mState == STATE_STOPPED) {
159 return mMarkerFramePosition;
160 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800161 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
162 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
163 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
164 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
165 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burk87c9f642017-05-17 07:22:39 -0700166// ALOGD("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800167// (unsigned long long)nanoTime,
168// (unsigned long long)position);
Phil Burk87c9f642017-05-17 07:22:39 -0700169// ALOGD("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800170// (long long) framesDelta, mFramesPerBurst);
171 return position;
172}