blob: d26b3523ac6b86db61bc71032b2025fc0ee307e1 [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 Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "IsochronousClockModel"
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) {
Phil Burkfbf031e2017-10-12 15:58:31 -070044 ALOGV("setPositionAndTime(%lld, %lld)",
Phil Burkec89b2e2017-06-20 15:05:06 -070045 (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 Burkfbf031e2017-10-12 15:58:31 -070051 ALOGV("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 Burkfbf031e2017-10-12 15:58:31 -070057 ALOGV("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 Burk377c1c22018-12-12 16:06:54 -080063bool IsochronousClockModel::isStarting() const {
Phil Burkbcc36742017-08-31 17:24:51 -070064 return mState == STATE_STARTING;
65}
66
Phil Burk377c1c22018-12-12 16:06:54 -080067bool IsochronousClockModel::isRunning() const {
68 return mState == STATE_RUNNING;
69}
70
Phil Burk87c9f642017-05-17 07:22:39 -070071void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burkbcc36742017-08-31 17:24:51 -070072// ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu",
73// (long long)framePosition,
74// (long long)nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080075 int64_t framesDelta = framePosition - mMarkerFramePosition;
76 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
77 if (nanosDelta < 1000) {
78 return;
79 }
80
Phil Burk87c9f642017-05-17 07:22:39 -070081// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080082// (long long)mMarkerFramePosition,
83// (long long)mMarkerNanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080084
85 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070086// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080087// (long long)expectedNanosDelta,
88// (long long)nanosDelta);
89
Phil Burk87c9f642017-05-17 07:22:39 -070090// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
91// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -080092 switch (mState) {
93 case STATE_STOPPED:
94 break;
95 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -070096 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080097 mState = STATE_SYNCING;
98 break;
99 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -0700100 // This will handle a burst of rapid transfer at the beginning.
Phil Burk204a1632017-01-03 17:23:43 -0800101 if (nanosDelta < expectedNanosDelta) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700102 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800103 } else {
Phil Burk87c9f642017-05-17 07:22:39 -0700104// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -0800105 mState = STATE_RUNNING;
106 }
107 break;
108 case STATE_RUNNING:
109 if (nanosDelta < expectedNanosDelta) {
110 // Earlier than expected timestamp.
Phil Burk1815a762019-05-24 08:52:27 -0700111 // This data is probably more accurate, so use it.
112 // Or we may be drifting due to a fast HW clock.
113// int microsDelta = (int) (nanosDelta / 1000);
114// int expectedMicrosDelta = (int) (expectedNanosDelta / 1000);
115// ALOGD("processTimestamp() - STATE_RUNNING - %7d < %7d so %4d micros EARLY",
116// microsDelta, expectedMicrosDelta, (expectedMicrosDelta - microsDelta));
117
Phil Burkec89b2e2017-06-20 15:05:06 -0700118 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800119 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
120 // Later than expected timestamp.
Phil Burk1815a762019-05-24 08:52:27 -0700121// int microsDelta = (int) (nanosDelta / 1000);
122// int expectedMicrosDeadline = (int) ((expectedNanosDelta + mMaxLatenessInNanos) / 1000);
123// ALOGD("processTimestamp() - STATE_RUNNING - %7d > %7d so %4d micros LATE",
124// microsDelta, expectedMicrosDeadline, (microsDelta - expectedMicrosDeadline));
125
126 // When we are late it may be because of preemption in the kernel or
127 // we may be drifting due to a slow HW clock.
128 setPositionAndTime(framePosition, nanoTime - mMaxLatenessInNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800129 }
130 break;
131 default:
132 break;
133 }
Phil Burkbcc36742017-08-31 17:24:51 -0700134
135// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -0800136}
137
138void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
139 mSampleRate = sampleRate;
140 update();
141}
142
143void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
144 mFramesPerBurst = framesPerBurst;
145 update();
146}
147
148void IsochronousClockModel::update() {
149 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
150 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
151}
152
Phil Burkec89b2e2017-06-20 15:05:06 -0700153int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800154 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800155}
156
Phil Burk3316d5e2017-02-15 11:23:01 -0800157int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800158 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800159}
160
Phil Burk87c9f642017-05-17 07:22:39 -0700161int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800162 if (mState == STATE_STOPPED) {
163 return mMarkerNanoTime;
164 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800165 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
166 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
167 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
168 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700169 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burkfbf031e2017-10-12 15:58:31 -0700170// ALOGD("convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800171// (unsigned long long)framePosition,
172// (unsigned long long)time);
173 return time;
174}
175
Phil Burk87c9f642017-05-17 07:22:39 -0700176int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800177 if (mState == STATE_STOPPED) {
178 return mMarkerFramePosition;
179 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800180 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
181 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
182 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
183 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
184 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burkfbf031e2017-10-12 15:58:31 -0700185// ALOGD("convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800186// (unsigned long long)nanoTime,
187// (unsigned long long)position);
Phil Burkfbf031e2017-10-12 15:58:31 -0700188// ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800189// (long long) framesDelta, mFramesPerBurst);
190 return position;
191}
Phil Burkec89b2e2017-06-20 15:05:06 -0700192
193void IsochronousClockModel::dump() const {
Phil Burkfbf031e2017-10-12 15:58:31 -0700194 ALOGD("mMarkerFramePosition = %lld", (long long) mMarkerFramePosition);
195 ALOGD("mMarkerNanoTime = %lld", (long long) mMarkerNanoTime);
196 ALOGD("mSampleRate = %6d", mSampleRate);
197 ALOGD("mFramesPerBurst = %6d", mFramesPerBurst);
198 ALOGD("mMaxLatenessInNanos = %6d", mMaxLatenessInNanos);
199 ALOGD("mState = %6d", mState);
Phil Burkec89b2e2017-06-20 15:05:06 -0700200}