blob: b8e55385553eb5cab909f76155380cef0458211c [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
17#define LOG_TAG "OboeAudio"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <oboe/OboeDefinitions.h>
23
24#include "IsochronousClockModel.h"
25
26#define MIN_LATENESS_NANOS (10 * OBOE_NANOS_PER_MICROSECOND)
27
28using namespace android;
29using namespace oboe;
30
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
44void IsochronousClockModel::start(oboe_nanoseconds_t nanoTime)
45{
46 mMarkerNanoTime = nanoTime;
47 mState = STATE_STARTING;
48}
49
50void IsochronousClockModel::stop(oboe_nanoseconds_t nanoTime)
51{
52 mMarkerNanoTime = nanoTime;
53 mMarkerFramePosition = convertTimeToPosition(nanoTime); // TODO should we do this?
54 mState = STATE_STOPPED;
55}
56
57void IsochronousClockModel::processTimestamp(oboe_position_frames_t framePosition,
58 oboe_nanoseconds_t nanoTime) {
59 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
136oboe_nanoseconds_t IsochronousClockModel::convertDeltaPositionToTime(
137 oboe_position_frames_t framesDelta) const {
138 return (OBOE_NANOS_PER_SECOND * framesDelta) / mSampleRate;
139}
140
141int64_t IsochronousClockModel::convertDeltaTimeToPosition(oboe_nanoseconds_t nanosDelta) const {
142 return (mSampleRate * nanosDelta) / OBOE_NANOS_PER_SECOND;
143}
144
145oboe_nanoseconds_t IsochronousClockModel::convertPositionToTime(
146 oboe_position_frames_t framePosition) const {
147 if (mState == STATE_STOPPED) {
148 return mMarkerNanoTime;
149 }
150 oboe_position_frames_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
151 oboe_position_frames_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
152 oboe_position_frames_t framesDelta = nextBurstPosition - mMarkerFramePosition;
153 oboe_nanoseconds_t nanosDelta = convertDeltaPositionToTime(framesDelta);
154 oboe_nanoseconds_t time = (oboe_nanoseconds_t) (mMarkerNanoTime + nanosDelta);
155// ALOGI("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu",
156// (unsigned long long)framePosition,
157// (unsigned long long)time);
158 return time;
159}
160
161oboe_position_frames_t IsochronousClockModel::convertTimeToPosition(
162 oboe_nanoseconds_t nanoTime) const {
163 if (mState == STATE_STOPPED) {
164 return mMarkerFramePosition;
165 }
166 oboe_nanoseconds_t nanosDelta = nanoTime - mMarkerNanoTime;
167 oboe_position_frames_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
168 oboe_position_frames_t nextBurstPosition = mMarkerFramePosition + framesDelta;
169 oboe_position_frames_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
170 oboe_position_frames_t position = nextBurstIndex * mFramesPerBurst;
171// 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}