blob: 4d0a7b8a544e70a26442c36a20e16ee32ab9cdb5 [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 Burkec89b2e2017-06-20 15:05:06 -070044void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) {
45 ALOGV("IsochronousClockModel::setPositionAndTime(%lld, %lld)",
46 (long long) framePosition, (long long) nanoTime);
47 mMarkerFramePosition = framePosition;
48 mMarkerNanoTime = nanoTime;
49}
50
Phil Burk87c9f642017-05-17 07:22:39 -070051void IsochronousClockModel::start(int64_t nanoTime) {
52 ALOGD("IsochronousClockModel::start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080053 mMarkerNanoTime = nanoTime;
54 mState = STATE_STARTING;
55}
56
Phil Burk87c9f642017-05-17 07:22:39 -070057void IsochronousClockModel::stop(int64_t nanoTime) {
58 ALOGD("IsochronousClockModel::stop(nanos = %lld)\n", (long long) nanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -070059 setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime);
60 // TODO should we set position?
Phil Burk204a1632017-01-03 17:23:43 -080061 mState = STATE_STOPPED;
62}
63
Phil Burk87c9f642017-05-17 07:22:39 -070064void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burk204a1632017-01-03 17:23:43 -080065 int64_t framesDelta = framePosition - mMarkerFramePosition;
66 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
67 if (nanosDelta < 1000) {
68 return;
69 }
70
Phil Burk87c9f642017-05-17 07:22:39 -070071// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080072// (long long)mMarkerFramePosition,
73// (long long)mMarkerNanoTime);
Phil Burk87c9f642017-05-17 07:22:39 -070074// ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080075// (long long)framePosition,
76// (long long)nanoTime);
77
78 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070079// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080080// (long long)expectedNanosDelta,
81// (long long)nanosDelta);
82
Phil Burk87c9f642017-05-17 07:22:39 -070083// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
84// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -080085 switch (mState) {
86 case STATE_STOPPED:
87 break;
88 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -070089 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080090 mState = STATE_SYNCING;
91 break;
92 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -070093 // This will handle a burst of rapid transfer at the beginning.
Phil Burk204a1632017-01-03 17:23:43 -080094 if (nanosDelta < expectedNanosDelta) {
Phil Burkec89b2e2017-06-20 15:05:06 -070095 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080096 } else {
Phil Burk87c9f642017-05-17 07:22:39 -070097// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -080098 mState = STATE_RUNNING;
99 }
100 break;
101 case STATE_RUNNING:
102 if (nanosDelta < expectedNanosDelta) {
103 // Earlier than expected timestamp.
104 // This data is probably more accurate so use it.
105 // or we may be drifting due to a slow HW clock.
Phil Burk87c9f642017-05-17 07:22:39 -0700106// ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
107// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -0700108 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800109 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
110 // Later than expected timestamp.
Phil Burk87c9f642017-05-17 07:22:39 -0700111// ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
112// (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
113// (int) (mMaxLatenessInNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -0700114 setPositionAndTime(framePosition - mFramesPerBurst, nanoTime - mMaxLatenessInNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800115 }
116 break;
117 default:
118 break;
119 }
Phil Burk204a1632017-01-03 17:23:43 -0800120}
121
122void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
123 mSampleRate = sampleRate;
124 update();
125}
126
127void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
128 mFramesPerBurst = framesPerBurst;
129 update();
130}
131
132void IsochronousClockModel::update() {
133 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
134 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
135}
136
Phil Burkec89b2e2017-06-20 15:05:06 -0700137int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800138 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800139}
140
Phil Burk3316d5e2017-02-15 11:23:01 -0800141int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800142 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800143}
144
Phil Burk87c9f642017-05-17 07:22:39 -0700145int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800146 if (mState == STATE_STOPPED) {
147 return mMarkerNanoTime;
148 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800149 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
150 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
151 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
152 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700153 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burk87c9f642017-05-17 07:22:39 -0700154// ALOGD("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800155// (unsigned long long)framePosition,
156// (unsigned long long)time);
157 return time;
158}
159
Phil Burk87c9f642017-05-17 07:22:39 -0700160int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800161 if (mState == STATE_STOPPED) {
162 return mMarkerFramePosition;
163 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800164 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
165 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
166 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
167 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
168 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burk87c9f642017-05-17 07:22:39 -0700169// ALOGD("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800170// (unsigned long long)nanoTime,
171// (unsigned long long)position);
Phil Burk87c9f642017-05-17 07:22:39 -0700172// ALOGD("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800173// (long long) framesDelta, mFramesPerBurst);
174 return position;
175}
Phil Burkec89b2e2017-06-20 15:05:06 -0700176
177void IsochronousClockModel::dump() const {
178 ALOGD("IsochronousClockModel::mMarkerFramePosition = %lld", (long long) mMarkerFramePosition);
179 ALOGD("IsochronousClockModel::mMarkerNanoTime = %lld", (long long) mMarkerNanoTime);
180 ALOGD("IsochronousClockModel::mSampleRate = %6d", mSampleRate);
181 ALOGD("IsochronousClockModel::mFramesPerBurst = %6d", mFramesPerBurst);
182 ALOGD("IsochronousClockModel::mMaxLatenessInNanos = %6d", mMaxLatenessInNanos);
183 ALOGD("IsochronousClockModel::mState = %6d", mState);
184}