| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <aaudio/AAudio.h> | 
|  | 24 | #include <audio_utils/primitives.h> | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 25 | #include <media/AudioTrack.h> | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 26 | #include <media/AudioTimestamp.h> | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 27 | #include <utils/String16.h> | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 28 |  | 
| Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 29 | #include "core/AudioGlobal.h" | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 30 | #include "core/AudioStream.h" | 
|  | 31 | #include "legacy/AudioStreamLegacy.h" | 
|  | 32 |  | 
|  | 33 | using namespace android; | 
|  | 34 | using namespace aaudio; | 
|  | 35 |  | 
|  | 36 | AudioStreamLegacy::AudioStreamLegacy() | 
| Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 37 | : AudioStream() { | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
|  | 40 | AudioStreamLegacy::~AudioStreamLegacy() { | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | // Called from AudioTrack.cpp or AudioRecord.cpp | 
|  | 44 | static void AudioStreamLegacy_callback(int event, void* userData, void *info) { | 
|  | 45 | AudioStreamLegacy *streamLegacy = (AudioStreamLegacy *) userData; | 
|  | 46 | streamLegacy->processCallback(event, info); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | aaudio_legacy_callback_t AudioStreamLegacy::getLegacyCallback() { | 
|  | 50 | return AudioStreamLegacy_callback; | 
|  | 51 | } | 
|  | 52 |  | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 53 | aaudio_data_callback_result_t AudioStreamLegacy::callDataCallbackFrames(uint8_t *buffer, | 
|  | 54 | int32_t numFrames) { | 
|  | 55 | void *finalAudioData = buffer; | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 56 | if (getDirection() == AAUDIO_DIRECTION_INPUT) { | 
|  | 57 | // Increment before because we already got the data from the device. | 
|  | 58 | incrementFramesRead(numFrames); | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 59 | finalAudioData = (void *) maybeConvertDeviceData(buffer, numFrames); | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 62 | // Call using the AAudio callback interface. | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 63 | aaudio_data_callback_result_t callbackResult = maybeCallDataCallback(finalAudioData, numFrames); | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE | 
|  | 66 | && getDirection() == AAUDIO_DIRECTION_OUTPUT) { | 
|  | 67 | // Increment after because we are going to write the data to the device. | 
|  | 68 | incrementFramesWritten(numFrames); | 
|  | 69 | } | 
|  | 70 | return callbackResult; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | // Implement FixedBlockProcessor | 
|  | 74 | int32_t AudioStreamLegacy::onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) { | 
| Phil Burk | 4b86749 | 2020-02-12 10:58:05 -0800 | [diff] [blame] | 75 | int32_t numFrames = numBytes / mBlockAdapterBytesPerFrame; | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 76 | return (int32_t) callDataCallbackFrames(buffer, numFrames); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | void AudioStreamLegacy::processCallbackCommon(aaudio_callback_operation_t opcode, void *info) { | 
|  | 80 | aaudio_data_callback_result_t callbackResult; | 
| Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 81 | // This illegal size can be used to tell AudioRecord or AudioTrack to stop calling us. | 
|  | 82 | // This takes advantage of them killing the stream when they see a size out of range. | 
|  | 83 | // That is an undocumented behavior. | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 84 | // TODO add to API in AudioRecord and AudioTrack | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 85 | const size_t SIZE_STOP_CALLBACKS = SIZE_MAX; | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 86 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 87 | switch (opcode) { | 
|  | 88 | case AAUDIO_CALLBACK_OPERATION_PROCESS_DATA: { | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 89 | (void) checkForDisconnectRequest(true); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | // Note that this code assumes an AudioTrack::Buffer is the same as | 
|  | 92 | // AudioRecord::Buffer | 
|  | 93 | // TODO define our own AudioBuffer and pass it from the subclasses. | 
|  | 94 | AudioTrack::Buffer *audioBuffer = static_cast<AudioTrack::Buffer *>(info); | 
| Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 95 | if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) { | 
|  | 96 | ALOGW("processCallbackCommon() data, stream disconnected"); | 
|  | 97 | audioBuffer->size = SIZE_STOP_CALLBACKS; | 
|  | 98 | } else if (!mCallbackEnabled.load()) { | 
| Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 99 | ALOGW("processCallbackCommon() no data because callback disabled"); | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 100 | audioBuffer->size = SIZE_STOP_CALLBACKS; | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 101 | } else { | 
|  | 102 | if (audioBuffer->frameCount == 0) { | 
| Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 103 | ALOGW("processCallbackCommon() data, frameCount is zero"); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 104 | return; | 
|  | 105 | } | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 106 |  | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 107 | // If the caller specified an exact size then use a block size adapter. | 
|  | 108 | if (mBlockAdapter != nullptr) { | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 109 | int32_t byteCount = audioBuffer->frameCount * getBytesPerDeviceFrame(); | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 110 | callbackResult = mBlockAdapter->processVariableBlock( | 
|  | 111 | (uint8_t *) audioBuffer->raw, byteCount); | 
|  | 112 | } else { | 
|  | 113 | // Call using the AAudio callback interface. | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 114 | callbackResult = callDataCallbackFrames((uint8_t *)audioBuffer->raw, | 
|  | 115 | audioBuffer->frameCount); | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 116 | } | 
|  | 117 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) { | 
| Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 118 | audioBuffer->size = audioBuffer->frameCount * getBytesPerDeviceFrame(); | 
| Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 119 | } else { | 
|  | 120 | if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) { | 
|  | 121 | ALOGD("%s() callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__); | 
|  | 122 | } else { | 
|  | 123 | ALOGW("%s() callback returned invalid result = %d", | 
|  | 124 | __func__, callbackResult); | 
|  | 125 | } | 
|  | 126 | audioBuffer->size = 0; | 
|  | 127 | systemStopFromCallback(); | 
|  | 128 | // Disable the callback just in case the system keeps trying to call us. | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 129 | mCallbackEnabled.store(false); | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 130 | } | 
| Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 131 |  | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 132 | if (updateStateMachine() != AAUDIO_OK) { | 
|  | 133 | forceDisconnect(); | 
|  | 134 | mCallbackEnabled.store(false); | 
| Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 135 | } | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 136 | } | 
|  | 137 | } | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 138 | break; | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 139 |  | 
| Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 140 | // Stream got rerouted so we disconnect. | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 141 | case AAUDIO_CALLBACK_OPERATION_DISCONNECTED: | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 142 | ALOGD("processCallbackCommon() stream disconnected"); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 143 | forceDisconnect(); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 144 | mCallbackEnabled.store(false); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 145 | break; | 
|  | 146 |  | 
|  | 147 | default: | 
|  | 148 | break; | 
|  | 149 | } | 
|  | 150 | } | 
| Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 151 |  | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 152 | aaudio_result_t AudioStreamLegacy::checkForDisconnectRequest(bool errorCallbackEnabled) { | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 153 | if (mRequestDisconnect.isRequested()) { | 
|  | 154 | ALOGD("checkForDisconnectRequest() mRequestDisconnect acknowledged"); | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 155 | forceDisconnect(errorCallbackEnabled); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 156 | mRequestDisconnect.acknowledge(); | 
|  | 157 | mCallbackEnabled.store(false); | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 158 | return AAUDIO_ERROR_DISCONNECTED; | 
|  | 159 | } else { | 
|  | 160 | return AAUDIO_OK; | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 161 | } | 
|  | 162 | } | 
|  | 163 |  | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 164 | void AudioStreamLegacy::forceDisconnect(bool errorCallbackEnabled) { | 
| Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 165 | // There is no need to disconnect if already in these states. | 
|  | 166 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED | 
|  | 167 | && getState() != AAUDIO_STREAM_STATE_CLOSING | 
|  | 168 | && getState() != AAUDIO_STREAM_STATE_CLOSED | 
|  | 169 | ) { | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 170 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 171 | if (errorCallbackEnabled) { | 
|  | 172 | maybeCallErrorCallback(AAUDIO_ERROR_DISCONNECTED); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 173 | } | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
| Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 177 | aaudio_result_t AudioStreamLegacy::getBestTimestamp(clockid_t clockId, | 
|  | 178 | int64_t *framePosition, | 
|  | 179 | int64_t *timeNanoseconds, | 
|  | 180 | ExtendedTimestamp *extendedTimestamp) { | 
|  | 181 | int timebase; | 
|  | 182 | switch (clockId) { | 
|  | 183 | case CLOCK_BOOTTIME: | 
|  | 184 | timebase = ExtendedTimestamp::TIMEBASE_BOOTTIME; | 
|  | 185 | break; | 
|  | 186 | case CLOCK_MONOTONIC: | 
|  | 187 | timebase = ExtendedTimestamp::TIMEBASE_MONOTONIC; | 
|  | 188 | break; | 
|  | 189 | default: | 
|  | 190 | ALOGE("getTimestamp() - Unrecognized clock type %d", (int) clockId); | 
| Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 191 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; | 
| Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 192 | break; | 
|  | 193 | } | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 194 | ExtendedTimestamp::Location location = ExtendedTimestamp::Location::LOCATION_INVALID; | 
|  | 195 | int64_t localPosition; | 
|  | 196 | status_t status = extendedTimestamp->getBestTimestamp(&localPosition, timeNanoseconds, | 
|  | 197 | timebase, &location); | 
| Phil Burk | c095964 | 2018-04-05 18:11:01 -0700 | [diff] [blame] | 198 | if (status == OK) { | 
|  | 199 | // use MonotonicCounter to prevent retrograde motion. | 
|  | 200 | mTimestampPosition.update32((int32_t) localPosition); | 
|  | 201 | *framePosition = mTimestampPosition.get(); | 
|  | 202 | } | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 203 |  | 
|  | 204 | //    ALOGD("getBestTimestamp() fposition: server = %6lld, kernel = %6lld, location = %d", | 
|  | 205 | //          (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_SERVER], | 
|  | 206 | //          (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_KERNEL], | 
|  | 207 | //          (int)location); | 
| Phil Burk | c095964 | 2018-04-05 18:11:01 -0700 | [diff] [blame] | 208 | return AAudioConvert_androidToAAudioResult(status); | 
| Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 209 | } | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 210 |  | 
| Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 211 | void AudioStreamLegacy::onAudioDeviceUpdate(audio_io_handle_t /* audioIo */, | 
|  | 212 | audio_port_handle_t deviceId) { | 
| Phil Burk | 7ba4655 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 213 | // Device routing is a common source of errors and DISCONNECTS. | 
| Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 214 | // Please leave this log in place. If there is a bug then this might | 
|  | 215 | // get called after the stream has been deleted so log before we | 
|  | 216 | // touch the stream object. | 
|  | 217 | ALOGD("%s(deviceId = %d)", __func__, (int)deviceId); | 
|  | 218 | if (getDeviceId() != AAUDIO_UNSPECIFIED | 
|  | 219 | && getDeviceId() != deviceId | 
|  | 220 | && getState() != AAUDIO_STREAM_STATE_DISCONNECTED | 
|  | 221 | ) { | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 222 | // Note that isDataCallbackActive() is affected by state so call it before DISCONNECTING. | 
|  | 223 | // If we have a data callback and the stream is active, then ask the data callback | 
|  | 224 | // to DISCONNECT and call the error callback. | 
|  | 225 | if (isDataCallbackActive()) { | 
| Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 226 | ALOGD("%s() request DISCONNECT in data callback, device %d => %d", | 
|  | 227 | __func__, (int) getDeviceId(), (int) deviceId); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 228 | // If the stream is stopped before the data callback has a chance to handle the | 
|  | 229 | // request then the requestStop() and requestPause() methods will handle it after | 
|  | 230 | // the callback has stopped. | 
|  | 231 | mRequestDisconnect.request(); | 
|  | 232 | } else { | 
| Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 233 | ALOGD("%s() DISCONNECT the stream now, device %d => %d", | 
|  | 234 | __func__, (int) getDeviceId(), (int) deviceId); | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 235 | forceDisconnect(); | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 236 | } | 
|  | 237 | } | 
|  | 238 | setDeviceId(deviceId); | 
|  | 239 | } |