blob: 38f3c24a39f19b782a51de91192258451de02f55 [file] [log] [blame]
Phil Burke4d7bb42017-03-28 11:32:39 -07001/*
2 * Copyright 2017 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 "AudioStreamLegacy"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
Phil Burk3d786cb2018-04-09 11:58:09 -070022
23#include <aaudio/AAudio.h>
24#include <audio_utils/primitives.h>
Phil Burke4d7bb42017-03-28 11:32:39 -070025#include <media/AudioTrack.h>
Phil Burk7328a802017-08-30 09:29:48 -070026#include <media/AudioTimestamp.h>
Phil Burk3d786cb2018-04-09 11:58:09 -070027#include <utils/String16.h>
Phil Burke4d7bb42017-03-28 11:32:39 -070028
Phil Burka9876702020-04-20 18:16:15 -070029#include "core/AudioGlobal.h"
Phil Burke4d7bb42017-03-28 11:32:39 -070030#include "core/AudioStream.h"
31#include "legacy/AudioStreamLegacy.h"
32
33using namespace android;
34using namespace aaudio;
35
36AudioStreamLegacy::AudioStreamLegacy()
Phil Burk58f5ce12020-08-12 14:29:10 +000037 : AudioStream() {
Phil Burke4d7bb42017-03-28 11:32:39 -070038}
39
Phil Burke4d7bb42017-03-28 11:32:39 -070040
Phil Burk3d786cb2018-04-09 11:58:09 -070041aaudio_data_callback_result_t AudioStreamLegacy::callDataCallbackFrames(uint8_t *buffer,
42 int32_t numFrames) {
43 void *finalAudioData = buffer;
Phil Burk7328a802017-08-30 09:29:48 -070044 if (getDirection() == AAUDIO_DIRECTION_INPUT) {
45 // Increment before because we already got the data from the device.
46 incrementFramesRead(numFrames);
Phil Burk3d786cb2018-04-09 11:58:09 -070047 finalAudioData = (void *) maybeConvertDeviceData(buffer, numFrames);
Phil Burk7328a802017-08-30 09:29:48 -070048 }
49
Phil Burke4d7bb42017-03-28 11:32:39 -070050 // Call using the AAudio callback interface.
Phil Burk3d786cb2018-04-09 11:58:09 -070051 aaudio_data_callback_result_t callbackResult = maybeCallDataCallback(finalAudioData, numFrames);
Phil Burk7328a802017-08-30 09:29:48 -070052
53 if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE
54 && getDirection() == AAUDIO_DIRECTION_OUTPUT) {
55 // Increment after because we are going to write the data to the device.
56 incrementFramesWritten(numFrames);
57 }
58 return callbackResult;
59}
60
61// Implement FixedBlockProcessor
62int32_t AudioStreamLegacy::onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) {
Phil Burk4b867492020-02-12 10:58:05 -080063 int32_t numFrames = numBytes / mBlockAdapterBytesPerFrame;
Phil Burk134f1972017-12-08 13:06:11 -080064 return (int32_t) callDataCallbackFrames(buffer, numFrames);
Phil Burke4d7bb42017-03-28 11:32:39 -070065}
66
Atneya Nair7daa4f92021-11-19 14:00:24 -050067
68void AudioStreamLegacy::onNewIAudioTrack() {
69 ALOGD("%s stream disconnected", __func__);
70 forceDisconnect();
71 mCallbackEnabled.store(false);
72}
73
74size_t AudioStreamLegacy::onMoreData(const android::AudioTrack::Buffer& buffer) {
Phil Burk1e83bee2018-12-17 14:15:20 -080075 // This illegal size can be used to tell AudioRecord or AudioTrack to stop calling us.
76 // This takes advantage of them killing the stream when they see a size out of range.
77 // That is an undocumented behavior.
Phil Burk3d786cb2018-04-09 11:58:09 -070078 // TODO add to API in AudioRecord and AudioTrack
Phil Burk134f1972017-12-08 13:06:11 -080079 const size_t SIZE_STOP_CALLBACKS = SIZE_MAX;
Atneya Nair7daa4f92021-11-19 14:00:24 -050080 aaudio_data_callback_result_t callbackResult;
81 (void) checkForDisconnectRequest(true);
Eric Laurentfb00fc72017-05-25 18:17:12 -070082
Atneya Nair7daa4f92021-11-19 14:00:24 -050083 // Note that this code assumes an AudioTrack::Buffer is the same as
84 // AudioRecord::Buffer
85 // TODO define our own AudioBuffer and pass it from the subclasses.
86 size_t written = buffer.size;
87 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
88 ALOGW("%s() data, stream disconnected", __func__);
89 // This will kill the stream and prevent it from being restarted.
90 // That is OK because the stream is disconnected.
91 written = SIZE_STOP_CALLBACKS;
92 } else if (!mCallbackEnabled.load()) {
93 ALOGW("%s() no data because callback disabled, set size=0", __func__);
94 // Do NOT use SIZE_STOP_CALLBACKS here because that will kill the stream and
95 // prevent it from being restarted. This can occur because of a race condition
96 // caused by Legacy callbacks running after the track is "stopped".
97 written = 0;
98 } else {
99 if (buffer.frameCount == 0) {
100 ALOGW("%s() data, frameCount is zero", __func__);
101 return written;
Phil Burke4d7bb42017-03-28 11:32:39 -0700102 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700103
Atneya Nair7daa4f92021-11-19 14:00:24 -0500104 // If the caller specified an exact size then use a block size adapter.
105 if (mBlockAdapter != nullptr) {
106 int32_t byteCount = buffer.frameCount * getBytesPerDeviceFrame();
107 callbackResult = mBlockAdapter->processVariableBlock(
108 static_cast<uint8_t*>(buffer.raw), byteCount);
109 } else {
110 // Call using the AAudio callback interface.
111 callbackResult = callDataCallbackFrames(static_cast<uint8_t *>(buffer.raw),
112 buffer.frameCount);
113 }
114 if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) {
115 written = buffer.frameCount * getBytesPerDeviceFrame();
116 } else {
117 if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) {
118 ALOGD("%s() callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__);
119 } else {
120 ALOGW("%s() callback returned invalid result = %d",
121 __func__, callbackResult);
122 }
123 written = 0;
124 systemStopInternal();
125 // Disable the callback just in case the system keeps trying to call us.
126 mCallbackEnabled.store(false);
127 }
128
129 if (updateStateMachine() != AAUDIO_OK) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700130 forceDisconnect();
Phil Burke4d7bb42017-03-28 11:32:39 -0700131 mCallbackEnabled.store(false);
Atneya Nair7daa4f92021-11-19 14:00:24 -0500132 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700133 }
Atneya Nair7daa4f92021-11-19 14:00:24 -0500134 return written;
Phil Burke4d7bb42017-03-28 11:32:39 -0700135}
Phil Burk5204d312017-05-04 17:16:13 -0700136
Atneya Nair7daa4f92021-11-19 14:00:24 -0500137
Phil Burk134f1972017-12-08 13:06:11 -0800138aaudio_result_t AudioStreamLegacy::checkForDisconnectRequest(bool errorCallbackEnabled) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700139 if (mRequestDisconnect.isRequested()) {
140 ALOGD("checkForDisconnectRequest() mRequestDisconnect acknowledged");
Phil Burk134f1972017-12-08 13:06:11 -0800141 forceDisconnect(errorCallbackEnabled);
Phil Burk2d5ba532017-09-06 14:36:11 -0700142 mRequestDisconnect.acknowledge();
143 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800144 return AAUDIO_ERROR_DISCONNECTED;
145 } else {
146 return AAUDIO_OK;
Phil Burk2d5ba532017-09-06 14:36:11 -0700147 }
148}
149
Phil Burk134f1972017-12-08 13:06:11 -0800150void AudioStreamLegacy::forceDisconnect(bool errorCallbackEnabled) {
Phil Burk58f5ce12020-08-12 14:29:10 +0000151 // There is no need to disconnect if already in these states.
152 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED
153 && getState() != AAUDIO_STREAM_STATE_CLOSING
154 && getState() != AAUDIO_STREAM_STATE_CLOSED
155 ) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700156 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk134f1972017-12-08 13:06:11 -0800157 if (errorCallbackEnabled) {
158 maybeCallErrorCallback(AAUDIO_ERROR_DISCONNECTED);
Phil Burk2d5ba532017-09-06 14:36:11 -0700159 }
160 }
161}
162
Phil Burk5204d312017-05-04 17:16:13 -0700163aaudio_result_t AudioStreamLegacy::getBestTimestamp(clockid_t clockId,
164 int64_t *framePosition,
165 int64_t *timeNanoseconds,
166 ExtendedTimestamp *extendedTimestamp) {
167 int timebase;
168 switch (clockId) {
169 case CLOCK_BOOTTIME:
170 timebase = ExtendedTimestamp::TIMEBASE_BOOTTIME;
171 break;
172 case CLOCK_MONOTONIC:
173 timebase = ExtendedTimestamp::TIMEBASE_MONOTONIC;
174 break;
175 default:
176 ALOGE("getTimestamp() - Unrecognized clock type %d", (int) clockId);
Phil Burk17fff382017-05-16 14:06:45 -0700177 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
Phil Burk5204d312017-05-04 17:16:13 -0700178 break;
179 }
Phil Burk7328a802017-08-30 09:29:48 -0700180 ExtendedTimestamp::Location location = ExtendedTimestamp::Location::LOCATION_INVALID;
181 int64_t localPosition;
182 status_t status = extendedTimestamp->getBestTimestamp(&localPosition, timeNanoseconds,
183 timebase, &location);
Phil Burkc0959642018-04-05 18:11:01 -0700184 if (status == OK) {
185 // use MonotonicCounter to prevent retrograde motion.
186 mTimestampPosition.update32((int32_t) localPosition);
187 *framePosition = mTimestampPosition.get();
188 }
Phil Burk7328a802017-08-30 09:29:48 -0700189
190// ALOGD("getBestTimestamp() fposition: server = %6lld, kernel = %6lld, location = %d",
191// (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_SERVER],
192// (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_KERNEL],
193// (int)location);
Phil Burkc0959642018-04-05 18:11:01 -0700194 return AAudioConvert_androidToAAudioResult(status);
Phil Burk5204d312017-05-04 17:16:13 -0700195}
Eric Laurentfb00fc72017-05-25 18:17:12 -0700196
Phil Burk58f5ce12020-08-12 14:29:10 +0000197void AudioStreamLegacy::onAudioDeviceUpdate(audio_io_handle_t /* audioIo */,
198 audio_port_handle_t deviceId) {
Phil Burk7ba46552019-04-15 08:58:08 -0700199 // Device routing is a common source of errors and DISCONNECTS.
Phil Burk58f5ce12020-08-12 14:29:10 +0000200 // Please leave this log in place. If there is a bug then this might
201 // get called after the stream has been deleted so log before we
202 // touch the stream object.
203 ALOGD("%s(deviceId = %d)", __func__, (int)deviceId);
204 if (getDeviceId() != AAUDIO_UNSPECIFIED
205 && getDeviceId() != deviceId
206 && getState() != AAUDIO_STREAM_STATE_DISCONNECTED
207 ) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700208 // Note that isDataCallbackActive() is affected by state so call it before DISCONNECTING.
209 // If we have a data callback and the stream is active, then ask the data callback
210 // to DISCONNECT and call the error callback.
211 if (isDataCallbackActive()) {
Phil Burk58f5ce12020-08-12 14:29:10 +0000212 ALOGD("%s() request DISCONNECT in data callback, device %d => %d",
213 __func__, (int) getDeviceId(), (int) deviceId);
Phil Burk2d5ba532017-09-06 14:36:11 -0700214 // If the stream is stopped before the data callback has a chance to handle the
Phil Burkdd582922020-10-15 20:29:51 +0000215 // request then the requestStop_l() and requestPause() methods will handle it after
Phil Burk2d5ba532017-09-06 14:36:11 -0700216 // the callback has stopped.
217 mRequestDisconnect.request();
218 } else {
Phil Burk58f5ce12020-08-12 14:29:10 +0000219 ALOGD("%s() DISCONNECT the stream now, device %d => %d",
220 __func__, (int) getDeviceId(), (int) deviceId);
Phil Burk2d5ba532017-09-06 14:36:11 -0700221 forceDisconnect();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700222 }
223 }
224 setDeviceId(deviceId);
225}