blob: bac69f1f92794c71fa63831913a86dd9d6c07cec [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 Burkec89b2e2017-06-20 15:05:06 -070043void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) {
44 ALOGV("IsochronousClockModel::setPositionAndTime(%lld, %lld)",
45 (long long) framePosition, (long long) nanoTime);
46 mMarkerFramePosition = framePosition;
47 mMarkerNanoTime = nanoTime;
48}
49
Phil Burk87c9f642017-05-17 07:22:39 -070050void IsochronousClockModel::start(int64_t nanoTime) {
Phil Burkbcc36742017-08-31 17:24:51 -070051 ALOGV("IsochronousClockModel::start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080052 mMarkerNanoTime = nanoTime;
53 mState = STATE_STARTING;
54}
55
Phil Burk87c9f642017-05-17 07:22:39 -070056void IsochronousClockModel::stop(int64_t nanoTime) {
Phil Burkbcc36742017-08-31 17:24:51 -070057 ALOGV("IsochronousClockModel::stop(nanos = %lld)\n", (long long) nanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -070058 setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime);
59 // TODO should we set position?
Phil Burk204a1632017-01-03 17:23:43 -080060 mState = STATE_STOPPED;
61}
62
Phil Burkbcc36742017-08-31 17:24:51 -070063bool IsochronousClockModel::isStarting() {
64 return mState == STATE_STARTING;
65}
66
Phil Burk87c9f642017-05-17 07:22:39 -070067void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burkbcc36742017-08-31 17:24:51 -070068// ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu",
69// (long long)framePosition,
70// (long long)nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080071 int64_t framesDelta = framePosition - mMarkerFramePosition;
72 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
73 if (nanosDelta < 1000) {
74 return;
75 }
76
Phil Burk87c9f642017-05-17 07:22:39 -070077// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080078// (long long)mMarkerFramePosition,
79// (long long)mMarkerNanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080080
81 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070082// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080083// (long long)expectedNanosDelta,
84// (long long)nanosDelta);
85
Phil Burk87c9f642017-05-17 07:22:39 -070086// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
87// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -080088 switch (mState) {
89 case STATE_STOPPED:
90 break;
91 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -070092 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080093 mState = STATE_SYNCING;
94 break;
95 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -070096 // This will handle a burst of rapid transfer at the beginning.
Phil Burk204a1632017-01-03 17:23:43 -080097 if (nanosDelta < expectedNanosDelta) {
Phil Burkec89b2e2017-06-20 15:05:06 -070098 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080099 } else {
Phil Burk87c9f642017-05-17 07:22:39 -0700100// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -0800101 mState = STATE_RUNNING;
102 }
103 break;
104 case STATE_RUNNING:
105 if (nanosDelta < expectedNanosDelta) {
106 // Earlier than expected timestamp.
107 // This data is probably more accurate so use it.
108 // or we may be drifting due to a slow HW clock.
Phil Burk87c9f642017-05-17 07:22:39 -0700109// ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
110// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -0700111 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800112 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
113 // Later than expected timestamp.
Phil Burk87c9f642017-05-17 07:22:39 -0700114// ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
115// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
116// (int) (mMaxLatenessInNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -0700117 setPositionAndTime(framePosition - mFramesPerBurst, nanoTime - mMaxLatenessInNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800118 }
119 break;
120 default:
121 break;
122 }
Phil Burkbcc36742017-08-31 17:24:51 -0700123
124// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -0800125}
126
127void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
128 mSampleRate = sampleRate;
129 update();
130}
131
132void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
133 mFramesPerBurst = framesPerBurst;
134 update();
135}
136
137void IsochronousClockModel::update() {
138 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
139 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
140}
141
Phil Burkec89b2e2017-06-20 15:05:06 -0700142int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800143 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800144}
145
Phil Burk3316d5e2017-02-15 11:23:01 -0800146int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800148}
149
Phil Burk87c9f642017-05-17 07:22:39 -0700150int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800151 if (mState == STATE_STOPPED) {
152 return mMarkerNanoTime;
153 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800154 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
155 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
156 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
157 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700158 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burk87c9f642017-05-17 07:22:39 -0700159// ALOGD("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800160// (unsigned long long)framePosition,
161// (unsigned long long)time);
162 return time;
163}
164
Phil Burk87c9f642017-05-17 07:22:39 -0700165int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800166 if (mState == STATE_STOPPED) {
167 return mMarkerFramePosition;
168 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800169 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
170 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
171 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
172 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
173 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burk87c9f642017-05-17 07:22:39 -0700174// ALOGD("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800175// (unsigned long long)nanoTime,
176// (unsigned long long)position);
Phil Burk87c9f642017-05-17 07:22:39 -0700177// ALOGD("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800178// (long long) framesDelta, mFramesPerBurst);
179 return position;
180}
Phil Burkec89b2e2017-06-20 15:05:06 -0700181
182void IsochronousClockModel::dump() const {
183 ALOGD("IsochronousClockModel::mMarkerFramePosition = %lld", (long long) mMarkerFramePosition);
184 ALOGD("IsochronousClockModel::mMarkerNanoTime = %lld", (long long) mMarkerNanoTime);
185 ALOGD("IsochronousClockModel::mSampleRate = %6d", mSampleRate);
186 ALOGD("IsochronousClockModel::mFramesPerBurst = %6d", mFramesPerBurst);
187 ALOGD("IsochronousClockModel::mMaxLatenessInNanos = %6d", mMaxLatenessInNanos);
188 ALOGD("IsochronousClockModel::mState = %6d", mState);
189}