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> |
| 22 | #include <utils/String16.h> |
| 23 | #include <media/AudioTrack.h> |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 24 | #include <media/AudioTimestamp.h> |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 25 | #include <aaudio/AAudio.h> |
| 26 | |
| 27 | #include "core/AudioStream.h" |
| 28 | #include "legacy/AudioStreamLegacy.h" |
| 29 | |
| 30 | using namespace android; |
| 31 | using namespace aaudio; |
| 32 | |
| 33 | AudioStreamLegacy::AudioStreamLegacy() |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 34 | : AudioStream(), mDeviceCallback(new StreamDeviceCallback(this)) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | AudioStreamLegacy::~AudioStreamLegacy() { |
| 38 | } |
| 39 | |
| 40 | // Called from AudioTrack.cpp or AudioRecord.cpp |
| 41 | static void AudioStreamLegacy_callback(int event, void* userData, void *info) { |
| 42 | AudioStreamLegacy *streamLegacy = (AudioStreamLegacy *) userData; |
| 43 | streamLegacy->processCallback(event, info); |
| 44 | } |
| 45 | |
| 46 | aaudio_legacy_callback_t AudioStreamLegacy::getLegacyCallback() { |
| 47 | return AudioStreamLegacy_callback; |
| 48 | } |
| 49 | |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 50 | int32_t AudioStreamLegacy::callDataCallbackFrames(uint8_t *buffer, int32_t numFrames) { |
| 51 | if (getDirection() == AAUDIO_DIRECTION_INPUT) { |
| 52 | // Increment before because we already got the data from the device. |
| 53 | incrementFramesRead(numFrames); |
| 54 | } |
| 55 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 56 | // Call using the AAudio callback interface. |
| 57 | AAudioStream_dataCallback appCallback = getDataCallbackProc(); |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 58 | aaudio_data_callback_result_t callbackResult = (*appCallback)( |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 59 | (AAudioStream *) this, |
| 60 | getDataCallbackUserData(), |
| 61 | buffer, |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 62 | numFrames); |
| 63 | |
| 64 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE |
| 65 | && getDirection() == AAUDIO_DIRECTION_OUTPUT) { |
| 66 | // Increment after because we are going to write the data to the device. |
| 67 | incrementFramesWritten(numFrames); |
| 68 | } |
| 69 | return callbackResult; |
| 70 | } |
| 71 | |
| 72 | // Implement FixedBlockProcessor |
| 73 | int32_t AudioStreamLegacy::onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) { |
| 74 | int32_t numFrames = numBytes / getBytesPerFrame(); |
| 75 | return callDataCallbackFrames(buffer, numFrames); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void AudioStreamLegacy::processCallbackCommon(aaudio_callback_operation_t opcode, void *info) { |
| 79 | aaudio_data_callback_result_t callbackResult; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 80 | |
| 81 | if (!mCallbackEnabled.load()) { |
| 82 | return; |
| 83 | } |
| 84 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 85 | switch (opcode) { |
| 86 | case AAUDIO_CALLBACK_OPERATION_PROCESS_DATA: { |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 87 | if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 88 | // Note that this code assumes an AudioTrack::Buffer is the same as |
| 89 | // AudioRecord::Buffer |
| 90 | // TODO define our own AudioBuffer and pass it from the subclasses. |
| 91 | AudioTrack::Buffer *audioBuffer = static_cast<AudioTrack::Buffer *>(info); |
| 92 | if (audioBuffer->frameCount == 0) return; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 93 | |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 94 | // If the caller specified an exact size then use a block size adapter. |
| 95 | if (mBlockAdapter != nullptr) { |
| 96 | int32_t byteCount = audioBuffer->frameCount * getBytesPerFrame(); |
| 97 | callbackResult = mBlockAdapter->processVariableBlock( |
| 98 | (uint8_t *) audioBuffer->raw, byteCount); |
| 99 | } else { |
| 100 | // Call using the AAudio callback interface. |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 101 | callbackResult = callDataCallbackFrames((uint8_t *)audioBuffer->raw, |
| 102 | audioBuffer->frameCount); |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 103 | } |
| 104 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) { |
| 105 | audioBuffer->size = audioBuffer->frameCount * getBytesPerFrame(); |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 106 | } else { |
| 107 | audioBuffer->size = 0; |
| 108 | } |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 109 | |
| 110 | if (updateStateMachine() == AAUDIO_OK) { |
| 111 | break; // don't fall through |
| 112 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 115 | /// FALL THROUGH |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 116 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 117 | // Stream got rerouted so we disconnect. |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 118 | case AAUDIO_CALLBACK_OPERATION_DISCONNECTED: { |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 119 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
| 120 | ALOGD("processCallbackCommon() stream disconnected"); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 121 | if (getErrorCallbackProc() != nullptr) { |
| 122 | (*getErrorCallbackProc())( |
| 123 | (AAudioStream *) this, |
| 124 | getErrorCallbackUserData(), |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 125 | AAUDIO_ERROR_DISCONNECTED |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 126 | ); |
| 127 | } |
| 128 | mCallbackEnabled.store(false); |
| 129 | } |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 136 | |
| 137 | aaudio_result_t AudioStreamLegacy::getBestTimestamp(clockid_t clockId, |
| 138 | int64_t *framePosition, |
| 139 | int64_t *timeNanoseconds, |
| 140 | ExtendedTimestamp *extendedTimestamp) { |
| 141 | int timebase; |
| 142 | switch (clockId) { |
| 143 | case CLOCK_BOOTTIME: |
| 144 | timebase = ExtendedTimestamp::TIMEBASE_BOOTTIME; |
| 145 | break; |
| 146 | case CLOCK_MONOTONIC: |
| 147 | timebase = ExtendedTimestamp::TIMEBASE_MONOTONIC; |
| 148 | break; |
| 149 | default: |
| 150 | ALOGE("getTimestamp() - Unrecognized clock type %d", (int) clockId); |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 151 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 152 | break; |
| 153 | } |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 154 | ExtendedTimestamp::Location location = ExtendedTimestamp::Location::LOCATION_INVALID; |
| 155 | int64_t localPosition; |
| 156 | status_t status = extendedTimestamp->getBestTimestamp(&localPosition, timeNanoseconds, |
| 157 | timebase, &location); |
| 158 | // use MonotonicCounter to prevent retrograde motion. |
| 159 | mTimestampPosition.update32((int32_t)localPosition); |
| 160 | *framePosition = mTimestampPosition.get(); |
| 161 | |
| 162 | // ALOGD("getBestTimestamp() fposition: server = %6lld, kernel = %6lld, location = %d", |
| 163 | // (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_SERVER], |
| 164 | // (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_KERNEL], |
| 165 | // (int)location); |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 166 | return AAudioConvert_androidToAAudioResult(status); |
| 167 | } |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 168 | |
| 169 | void AudioStreamLegacy::onAudioDeviceUpdate(audio_port_handle_t deviceId) |
| 170 | { |
| 171 | ALOGD("onAudioDeviceUpdate() deviceId %d", (int)deviceId); |
| 172 | if (getDeviceId() != AAUDIO_UNSPECIFIED && getDeviceId() != deviceId && |
| 173 | getState() != AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 174 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
| 175 | // if we have a data callback and the stream is active, send the error callback from |
| 176 | // data callback thread when it sees the DISCONNECTED state |
| 177 | if (!isDataCallbackActive() && getErrorCallbackProc() != nullptr) { |
| 178 | (*getErrorCallbackProc())( |
| 179 | (AAudioStream *) this, |
| 180 | getErrorCallbackUserData(), |
| 181 | AAUDIO_ERROR_DISCONNECTED |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 | setDeviceId(deviceId); |
| 186 | } |