blob: 1de33bb8fa6acb48fcc76cf2b6a211bc14cb7ffe [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
19#include <utils/Log.h>
20
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
28using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080029using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080030
31IsochronousClockModel::IsochronousClockModel()
Phil Burk3316d5e2017-02-15 11:23:01 -080032 : mMarkerFramePosition(0)
33 , mMarkerNanoTime(0)
34 , mSampleRate(48000)
Phil Burk204a1632017-01-03 17:23:43 -080035 , mFramesPerBurst(64)
36 , mMaxLatenessInNanos(0)
Phil Burk204a1632017-01-03 17:23:43 -080037 , mState(STATE_STOPPED)
38{
39}
40
41IsochronousClockModel::~IsochronousClockModel() {
42}
43
Phil Burk87c9f642017-05-17 07:22:39 -070044void IsochronousClockModel::start(int64_t nanoTime) {
45 ALOGD("IsochronousClockModel::start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080046 mMarkerNanoTime = nanoTime;
47 mState = STATE_STARTING;
48}
49
Phil Burk87c9f642017-05-17 07:22:39 -070050void IsochronousClockModel::stop(int64_t nanoTime) {
51 ALOGD("IsochronousClockModel::stop(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080052 mMarkerNanoTime = nanoTime;
53 mMarkerFramePosition = convertTimeToPosition(nanoTime); // TODO should we do this?
54 mState = STATE_STOPPED;
55}
56
Phil Burk87c9f642017-05-17 07:22:39 -070057void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burk204a1632017-01-03 17:23:43 -080058 int64_t framesDelta = framePosition - mMarkerFramePosition;
59 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
60 if (nanosDelta < 1000) {
61 return;
62 }
63
Phil Burk87c9f642017-05-17 07:22:39 -070064// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080065// (long long)mMarkerFramePosition,
66// (long long)mMarkerNanoTime);
Phil Burk87c9f642017-05-17 07:22:39 -070067// ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080068// (long long)framePosition,
69// (long long)nanoTime);
70
71 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070072// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080073// (long long)expectedNanosDelta,
74// (long long)nanosDelta);
75
Phil Burk87c9f642017-05-17 07:22:39 -070076// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
77// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -080078 switch (mState) {
79 case STATE_STOPPED:
80 break;
81 case STATE_STARTING:
82 mMarkerFramePosition = framePosition;
83 mMarkerNanoTime = nanoTime;
84 mState = STATE_SYNCING;
85 break;
86 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -070087 // This will handle a burst of rapid transfer at the beginning.
Phil Burk204a1632017-01-03 17:23:43 -080088 if (nanosDelta < expectedNanosDelta) {
89 mMarkerFramePosition = framePosition;
90 mMarkerNanoTime = nanoTime;
91 } else {
Phil Burk87c9f642017-05-17 07:22:39 -070092// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -080093 mState = STATE_RUNNING;
94 }
95 break;
96 case STATE_RUNNING:
97 if (nanosDelta < expectedNanosDelta) {
98 // Earlier than expected timestamp.
99 // This data is probably more accurate so use it.
100 // or we may be drifting due to a slow HW clock.
101 mMarkerFramePosition = framePosition;
102 mMarkerNanoTime = nanoTime;
Phil Burk87c9f642017-05-17 07:22:39 -0700103// ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
104// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
Phil Burk204a1632017-01-03 17:23:43 -0800105 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
106 // Later than expected timestamp.
107 mMarkerFramePosition = framePosition;
108 mMarkerNanoTime = nanoTime - mMaxLatenessInNanos;
Phil Burk87c9f642017-05-17 07:22:39 -0700109// ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
110// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
111// (int) (mMaxLatenessInNanos / 1000));
Phil Burk204a1632017-01-03 17:23:43 -0800112 }
113 break;
114 default:
115 break;
116 }
Phil Burk204a1632017-01-03 17:23:43 -0800117}
118
119void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
120 mSampleRate = sampleRate;
121 update();
122}
123
124void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
125 mFramesPerBurst = framesPerBurst;
126 update();
127}
128
129void IsochronousClockModel::update() {
130 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
131 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
132}
133
Phil Burk3316d5e2017-02-15 11:23:01 -0800134int64_t IsochronousClockModel::convertDeltaPositionToTime(
135 int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800136 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800137}
138
Phil Burk3316d5e2017-02-15 11:23:01 -0800139int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800140 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800141}
142
Phil Burk87c9f642017-05-17 07:22:39 -0700143int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800144 if (mState == STATE_STOPPED) {
145 return mMarkerNanoTime;
146 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800147 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
148 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
149 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
150 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
151 int64_t time = (int64_t) (mMarkerNanoTime + nanosDelta);
Phil Burk87c9f642017-05-17 07:22:39 -0700152// ALOGD("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800153// (unsigned long long)framePosition,
154// (unsigned long long)time);
155 return time;
156}
157
Phil Burk87c9f642017-05-17 07:22:39 -0700158int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800159 if (mState == STATE_STOPPED) {
160 return mMarkerFramePosition;
161 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800162 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
163 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
164 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
165 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
166 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burk87c9f642017-05-17 07:22:39 -0700167// ALOGD("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800168// (unsigned long long)nanoTime,
169// (unsigned long long)position);
Phil Burk87c9f642017-05-17 07:22:39 -0700170// ALOGD("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800171// (long long) framesDelta, mFramesPerBurst);
172 return position;
173}