blob: 21e3e708e9949238a07301ba2886ae04c659d471 [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 Burk3316d5e2017-02-15 11:23:01 -080044void IsochronousClockModel::start(int64_t nanoTime)
Phil Burk204a1632017-01-03 17:23:43 -080045{
46 mMarkerNanoTime = nanoTime;
47 mState = STATE_STARTING;
48}
49
Phil Burk3316d5e2017-02-15 11:23:01 -080050void IsochronousClockModel::stop(int64_t nanoTime)
Phil Burk204a1632017-01-03 17:23:43 -080051{
52 mMarkerNanoTime = nanoTime;
53 mMarkerFramePosition = convertTimeToPosition(nanoTime); // TODO should we do this?
54 mState = STATE_STOPPED;
55}
56
Phil Burk3316d5e2017-02-15 11:23:01 -080057void IsochronousClockModel::processTimestamp(int64_t framePosition,
58 int64_t nanoTime) {
Phil Burk204a1632017-01-03 17:23:43 -080059 int64_t framesDelta = framePosition - mMarkerFramePosition;
60 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
61 if (nanosDelta < 1000) {
62 return;
63 }
64
65// ALOGI("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
66// (long long)mMarkerFramePosition,
67// (long long)mMarkerNanoTime);
68// ALOGI("processTimestamp() - framePosition = %lld at nanoTime %llu",
69// (long long)framePosition,
70// (long long)nanoTime);
71
72 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
73// ALOGI("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
74// (long long)expectedNanosDelta,
75// (long long)nanosDelta);
76
77// ALOGI("processTimestamp() - mSampleRate = %d", mSampleRate);
78// ALOGI("processTimestamp() - mState = %d", mState);
79 switch (mState) {
80 case STATE_STOPPED:
81 break;
82 case STATE_STARTING:
83 mMarkerFramePosition = framePosition;
84 mMarkerNanoTime = nanoTime;
85 mState = STATE_SYNCING;
86 break;
87 case STATE_SYNCING:
88 // This will handle a burst of rapid consumption in the beginning.
89 if (nanosDelta < expectedNanosDelta) {
90 mMarkerFramePosition = framePosition;
91 mMarkerNanoTime = nanoTime;
92 } else {
93 ALOGI("processTimestamp() - advance to STATE_RUNNING");
94 mState = STATE_RUNNING;
95 }
96 break;
97 case STATE_RUNNING:
98 if (nanosDelta < expectedNanosDelta) {
99 // Earlier than expected timestamp.
100 // This data is probably more accurate so use it.
101 // or we may be drifting due to a slow HW clock.
102 mMarkerFramePosition = framePosition;
103 mMarkerNanoTime = nanoTime;
Phil Burk71f35bb2017-04-13 16:05:07 -0700104 ALOGV("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
Phil Burk204a1632017-01-03 17:23:43 -0800105 (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
106 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
107 // Later than expected timestamp.
108 mMarkerFramePosition = framePosition;
109 mMarkerNanoTime = nanoTime - mMaxLatenessInNanos;
Phil Burk71f35bb2017-04-13 16:05:07 -0700110 ALOGV("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
Phil Burk204a1632017-01-03 17:23:43 -0800111 (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
112 (int) (mMaxLatenessInNanos / 1000));
113 }
114 break;
115 default:
116 break;
117 }
Phil Burk204a1632017-01-03 17:23:43 -0800118}
119
120void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
121 mSampleRate = sampleRate;
122 update();
123}
124
125void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
126 mFramesPerBurst = framesPerBurst;
127 update();
128}
129
130void IsochronousClockModel::update() {
131 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
132 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
133}
134
Phil Burk3316d5e2017-02-15 11:23:01 -0800135int64_t IsochronousClockModel::convertDeltaPositionToTime(
136 int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800137 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800138}
139
Phil Burk3316d5e2017-02-15 11:23:01 -0800140int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800141 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800142}
143
Phil Burk3316d5e2017-02-15 11:23:01 -0800144int64_t IsochronousClockModel::convertPositionToTime(
145 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);
153 int64_t time = (int64_t) (mMarkerNanoTime + nanosDelta);
Phil Burk204a1632017-01-03 17:23:43 -0800154// ALOGI("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
155// (unsigned long long)framePosition,
156// (unsigned long long)time);
157 return time;
158}
159
Phil Burk3316d5e2017-02-15 11:23:01 -0800160int64_t IsochronousClockModel::convertTimeToPosition(
161 int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800162 if (mState == STATE_STOPPED) {
163 return mMarkerFramePosition;
164 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800165 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
166 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
167 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
168 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
169 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800170// ALOGI("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu",
171// (unsigned long long)nanoTime,
172// (unsigned long long)position);
173// ALOGI("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
174// (long long) framesDelta, mFramesPerBurst);
175 return position;
176}