blob: 4c8aabc0f309a39f5fab4ca8ea6222d638f112e4 [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
Phil Burk3316d5e2017-02-15 11:23:01 -080024#include "utility/AudioClock.h"
Phil Burk204a1632017-01-03 17:23:43 -080025#include "IsochronousClockModel.h"
26
Phil Burk5ed503c2017-02-01 09:38:15 -080027#define MIN_LATENESS_NANOS (10 * AAUDIO_NANOS_PER_MICROSECOND)
Phil Burk204a1632017-01-03 17:23:43 -080028
29using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080030using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080031
32IsochronousClockModel::IsochronousClockModel()
Phil Burk3316d5e2017-02-15 11:23:01 -080033 : mMarkerFramePosition(0)
34 , mMarkerNanoTime(0)
35 , mSampleRate(48000)
Phil Burk204a1632017-01-03 17:23:43 -080036 , mFramesPerBurst(64)
37 , mMaxLatenessInNanos(0)
Phil Burk204a1632017-01-03 17:23:43 -080038 , mState(STATE_STOPPED)
39{
40}
41
42IsochronousClockModel::~IsochronousClockModel() {
43}
44
Phil Burk3316d5e2017-02-15 11:23:01 -080045void IsochronousClockModel::start(int64_t nanoTime)
Phil Burk204a1632017-01-03 17:23:43 -080046{
47 mMarkerNanoTime = nanoTime;
48 mState = STATE_STARTING;
49}
50
Phil Burk3316d5e2017-02-15 11:23:01 -080051void IsochronousClockModel::stop(int64_t nanoTime)
Phil Burk204a1632017-01-03 17:23:43 -080052{
53 mMarkerNanoTime = nanoTime;
54 mMarkerFramePosition = convertTimeToPosition(nanoTime); // TODO should we do this?
55 mState = STATE_STOPPED;
56}
57
Phil Burk3316d5e2017-02-15 11:23:01 -080058void IsochronousClockModel::processTimestamp(int64_t framePosition,
59 int64_t nanoTime) {
Phil Burk204a1632017-01-03 17:23:43 -080060 int64_t framesDelta = framePosition - mMarkerFramePosition;
61 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
62 if (nanosDelta < 1000) {
63 return;
64 }
65
66// ALOGI("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
67// (long long)mMarkerFramePosition,
68// (long long)mMarkerNanoTime);
69// ALOGI("processTimestamp() - framePosition = %lld at nanoTime %llu",
70// (long long)framePosition,
71// (long long)nanoTime);
72
73 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
74// ALOGI("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
75// (long long)expectedNanosDelta,
76// (long long)nanosDelta);
77
78// ALOGI("processTimestamp() - mSampleRate = %d", mSampleRate);
79// ALOGI("processTimestamp() - mState = %d", mState);
80 switch (mState) {
81 case STATE_STOPPED:
82 break;
83 case STATE_STARTING:
84 mMarkerFramePosition = framePosition;
85 mMarkerNanoTime = nanoTime;
86 mState = STATE_SYNCING;
87 break;
88 case STATE_SYNCING:
89 // This will handle a burst of rapid consumption in the beginning.
90 if (nanosDelta < expectedNanosDelta) {
91 mMarkerFramePosition = framePosition;
92 mMarkerNanoTime = nanoTime;
93 } else {
94 ALOGI("processTimestamp() - advance to STATE_RUNNING");
95 mState = STATE_RUNNING;
96 }
97 break;
98 case STATE_RUNNING:
99 if (nanosDelta < expectedNanosDelta) {
100 // Earlier than expected timestamp.
101 // This data is probably more accurate so use it.
102 // or we may be drifting due to a slow HW clock.
103 mMarkerFramePosition = framePosition;
104 mMarkerNanoTime = nanoTime;
105 ALOGI("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY",
106 (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000));
107 } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) {
108 // Later than expected timestamp.
109 mMarkerFramePosition = framePosition;
110 mMarkerNanoTime = nanoTime - mMaxLatenessInNanos;
111 ALOGI("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE",
112 (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000),
113 (int) (mMaxLatenessInNanos / 1000));
114 }
115 break;
116 default:
117 break;
118 }
Phil Burk204a1632017-01-03 17:23:43 -0800119}
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 Burk3316d5e2017-02-15 11:23:01 -0800136int64_t IsochronousClockModel::convertDeltaPositionToTime(
137 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 Burk3316d5e2017-02-15 11:23:01 -0800145int64_t IsochronousClockModel::convertPositionToTime(
146 int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800147 if (mState == STATE_STOPPED) {
148 return mMarkerNanoTime;
149 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800150 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
151 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
152 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
153 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
154 int64_t time = (int64_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 Burk3316d5e2017-02-15 11:23:01 -0800161int64_t IsochronousClockModel::convertTimeToPosition(
162 int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800163 if (mState == STATE_STOPPED) {
164 return mMarkerFramePosition;
165 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800166 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
167 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
168 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
169 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
170 int64_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}