blob: 747d0e14bc45b161446c0261128f0068af7ee3db [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.
111 // This data is probably more accurate so use it.
112 // or we may be drifting due to a slow HW clock.
Phil Burk87c9f642017-05-17 07:22:39 -0700113// ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
114// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -0700115 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800116 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
117 // Later than expected timestamp.
Phil Burk87c9f642017-05-17 07:22:39 -0700118// ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
119// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
120// (int) (mMaxLatenessInNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -0700121 setPositionAndTime(framePosition - mFramesPerBurst, nanoTime - mMaxLatenessInNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800122 }
123 break;
124 default:
125 break;
126 }
Phil Burkbcc36742017-08-31 17:24:51 -0700127
128// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -0800129}
130
131void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
132 mSampleRate = sampleRate;
133 update();
134}
135
136void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
137 mFramesPerBurst = framesPerBurst;
138 update();
139}
140
141void IsochronousClockModel::update() {
142 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
143 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
144}
145
Phil Burkec89b2e2017-06-20 15:05:06 -0700146int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800148}
149
Phil Burk3316d5e2017-02-15 11:23:01 -0800150int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800151 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800152}
153
Phil Burk87c9f642017-05-17 07:22:39 -0700154int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800155 if (mState == STATE_STOPPED) {
156 return mMarkerNanoTime;
157 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800158 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
159 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
160 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
161 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700162 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burkfbf031e2017-10-12 15:58:31 -0700163// ALOGD("convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800164// (unsigned long long)framePosition,
165// (unsigned long long)time);
166 return time;
167}
168
Phil Burk87c9f642017-05-17 07:22:39 -0700169int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800170 if (mState == STATE_STOPPED) {
171 return mMarkerFramePosition;
172 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800173 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
174 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
175 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
176 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
177 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burkfbf031e2017-10-12 15:58:31 -0700178// ALOGD("convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800179// (unsigned long long)nanoTime,
180// (unsigned long long)position);
Phil Burkfbf031e2017-10-12 15:58:31 -0700181// ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800182// (long long) framesDelta, mFramesPerBurst);
183 return position;
184}
Phil Burkec89b2e2017-06-20 15:05:06 -0700185
186void IsochronousClockModel::dump() const {
Phil Burkfbf031e2017-10-12 15:58:31 -0700187 ALOGD("mMarkerFramePosition = %lld", (long long) mMarkerFramePosition);
188 ALOGD("mMarkerNanoTime = %lld", (long long) mMarkerNanoTime);
189 ALOGD("mSampleRate = %6d", mSampleRate);
190 ALOGD("mFramesPerBurst = %6d", mFramesPerBurst);
191 ALOGD("mMaxLatenessInNanos = %6d", mMaxLatenessInNanos);
192 ALOGD("mState = %6d", mState);
Phil Burkec89b2e2017-06-20 15:05:06 -0700193}