blob: bdb491d2e2e0b4f13b7aabe37fc6df10166fd5a4 [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 Burk5ed503c2017-02-01 09:38:15 -080022#include <aaudio/AAudioDefinitions.h>
Phil Burk204a1632017-01-03 17:23:43 -080023
24#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()
32 : mSampleRate(48000)
33 , mFramesPerBurst(64)
34 , mMaxLatenessInNanos(0)
35 , mMarkerFramePosition(0)
36 , mMarkerNanoTime(0)
37 , mState(STATE_STOPPED)
38{
39}
40
41IsochronousClockModel::~IsochronousClockModel() {
42}
43
Phil Burk5ed503c2017-02-01 09:38:15 -080044void IsochronousClockModel::start(aaudio_nanoseconds_t nanoTime)
Phil Burk204a1632017-01-03 17:23:43 -080045{
46 mMarkerNanoTime = nanoTime;
47 mState = STATE_STARTING;
48}
49
Phil Burk5ed503c2017-02-01 09:38:15 -080050void IsochronousClockModel::stop(aaudio_nanoseconds_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 Burk5ed503c2017-02-01 09:38:15 -080057void IsochronousClockModel::processTimestamp(aaudio_position_frames_t framePosition,
58 aaudio_nanoseconds_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;
104 ALOGI("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
105 (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
106 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
107 // Later than expected timestamp.
108 mMarkerFramePosition = framePosition;
109 mMarkerNanoTime = nanoTime - mMaxLatenessInNanos;
110 ALOGI("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
111 (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
112 (int) (mMaxLatenessInNanos / 1000));
113 }
114 break;
115 default:
116 break;
117 }
118 ++mTimestampCount;
119}
120
121void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
122 mSampleRate = sampleRate;
123 update();
124}
125
126void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
127 mFramesPerBurst = framesPerBurst;
128 update();
129}
130
131void IsochronousClockModel::update() {
132 int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
133 mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS;
134}
135
Phil Burk5ed503c2017-02-01 09:38:15 -0800136aaudio_nanoseconds_t IsochronousClockModel::convertDeltaPositionToTime(
137 aaudio_position_frames_t framesDelta) const {
138 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800139}
140
Phil Burk5ed503c2017-02-01 09:38:15 -0800141int64_t IsochronousClockModel::convertDeltaTimeToPosition(aaudio_nanoseconds_t nanosDelta) const {
142 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800143}
144
Phil Burk5ed503c2017-02-01 09:38:15 -0800145aaudio_nanoseconds_t IsochronousClockModel::convertPositionToTime(
146 aaudio_position_frames_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800147 if (mState == STATE_STOPPED) {
148 return mMarkerNanoTime;
149 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 aaudio_position_frames_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
151 aaudio_position_frames_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
152 aaudio_position_frames_t framesDelta = nextBurstPosition - mMarkerFramePosition;
153 aaudio_nanoseconds_t nanosDelta = convertDeltaPositionToTime(framesDelta);
154 aaudio_nanoseconds_t time = (aaudio_nanoseconds_t) (mMarkerNanoTime + nanosDelta);
Phil Burk204a1632017-01-03 17:23:43 -0800155// ALOGI("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
156// (unsigned long long)framePosition,
157// (unsigned long long)time);
158 return time;
159}
160
Phil Burk5ed503c2017-02-01 09:38:15 -0800161aaudio_position_frames_t IsochronousClockModel::convertTimeToPosition(
162 aaudio_nanoseconds_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800163 if (mState == STATE_STOPPED) {
164 return mMarkerFramePosition;
165 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800166 aaudio_nanoseconds_t nanosDelta = nanoTime - mMarkerNanoTime;
167 aaudio_position_frames_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
168 aaudio_position_frames_t nextBurstPosition = mMarkerFramePosition + framesDelta;
169 aaudio_position_frames_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
170 aaudio_position_frames_t position = nextBurstIndex * mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800171// ALOGI("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu",
172// (unsigned long long)nanoTime,
173// (unsigned long long)position);
174// ALOGI("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
175// (long long) framesDelta, mFramesPerBurst);
176 return position;
177}