| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 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 Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioStreamTrack" | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 | #include <utils/Log.h> | 
|  | 20 |  | 
|  | 21 | #include <stdint.h> | 
|  | 22 | #include <media/AudioTrack.h> | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 23 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 24 | #include <aaudio/AAudio.h> | 
| Phil Burk | ed81601 | 2018-02-06 12:40:50 -0800 | [diff] [blame] | 25 | #include <system/audio.h> | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 26 | #include "utility/AudioClock.h" | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 27 | #include "legacy/AudioStreamLegacy.h" | 
|  | 28 | #include "legacy/AudioStreamTrack.h" | 
|  | 29 | #include "utility/FixedBlockReader.h" | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | using namespace android; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | using namespace aaudio; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 33 |  | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 34 | using media::permission::Identity; | 
|  | 35 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 36 | // Arbitrary and somewhat generous number of bursts. | 
|  | 37 | #define DEFAULT_BURSTS_PER_BUFFER_CAPACITY     8 | 
|  | 38 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 39 | /* | 
|  | 40 | * Create a stream that uses the AudioTrack. | 
|  | 41 | */ | 
|  | 42 | AudioStreamTrack::AudioStreamTrack() | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 43 | : AudioStreamLegacy() | 
|  | 44 | , mFixedBlockReader(*this) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 45 | { | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | AudioStreamTrack::~AudioStreamTrack() | 
|  | 49 | { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | const aaudio_stream_state_t state = getState(); | 
|  | 51 | bool bad = !(state == AAUDIO_STREAM_STATE_UNINITIALIZED || state == AAUDIO_STREAM_STATE_CLOSED); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 52 | ALOGE_IF(bad, "stream not closed, in state %d", state); | 
|  | 53 | } | 
|  | 54 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 55 | aaudio_result_t AudioStreamTrack::open(const AudioStreamBuilder& builder) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 56 | { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 57 | aaudio_result_t result = AAUDIO_OK; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 58 |  | 
|  | 59 | result = AudioStream::open(builder); | 
|  | 60 | if (result != OK) { | 
|  | 61 | return result; | 
|  | 62 | } | 
|  | 63 |  | 
| Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 64 | const aaudio_session_id_t requestedSessionId = builder.getSessionId(); | 
|  | 65 | const audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId); | 
|  | 66 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 67 | // Try to create an AudioTrack | 
| Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 68 | // Use stereo if unspecified. | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 69 | int32_t samplesPerFrame = (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 70 | ? 2 : getSamplesPerFrame(); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 71 | audio_channel_mask_t channelMask = samplesPerFrame <= 2 ? | 
|  | 72 | audio_channel_out_mask_from_count(samplesPerFrame) : | 
|  | 73 | audio_channel_mask_for_index_assignment_from_count(samplesPerFrame); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 74 |  | 
| Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 75 | audio_output_flags_t flags; | 
| Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 76 | aaudio_performance_mode_t perfMode = getPerformanceMode(); | 
|  | 77 | switch(perfMode) { | 
| Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 78 | case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY: | 
|  | 79 | // Bypass the normal mixer and go straight to the FAST mixer. | 
| Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 80 | // If the app asks for a sessionId then it means they want to use effects. | 
|  | 81 | // So don't use RAW flag. | 
|  | 82 | flags = (audio_output_flags_t) ((requestedSessionId == AAUDIO_SESSION_ID_NONE) | 
|  | 83 | ? (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW) | 
|  | 84 | : (AUDIO_OUTPUT_FLAG_FAST)); | 
| Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 85 | break; | 
|  | 86 |  | 
|  | 87 | case AAUDIO_PERFORMANCE_MODE_POWER_SAVING: | 
|  | 88 | // This uses a mixer that wakes up less often than the FAST mixer. | 
|  | 89 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
|  | 90 | break; | 
|  | 91 |  | 
|  | 92 | case AAUDIO_PERFORMANCE_MODE_NONE: | 
|  | 93 | default: | 
|  | 94 | // No flags. Use a normal mixer in front of the FAST mixer. | 
| Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 95 | flags = AUDIO_OUTPUT_FLAG_NONE; | 
| Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 96 | break; | 
|  | 97 | } | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 98 |  | 
| Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 99 | size_t frameCount = (size_t)builder.getBufferCapacity(); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 100 |  | 
| Phil Burk | e69bfcb | 2020-08-20 00:10:35 +0000 | [diff] [blame] | 101 | // To avoid glitching, let AudioFlinger pick the optimal burst size. | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 102 | int32_t notificationFrames = 0; | 
|  | 103 |  | 
| Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 104 | const audio_format_t format = (getFormat() == AUDIO_FORMAT_DEFAULT) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 105 | ? AUDIO_FORMAT_PCM_FLOAT | 
| Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 106 | : getFormat(); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 107 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 108 | // Setup the callback if there is one. | 
|  | 109 | AudioTrack::callback_t callback = nullptr; | 
|  | 110 | void *callbackData = nullptr; | 
|  | 111 | // Note that TRANSFER_SYNC does not allow FAST track | 
|  | 112 | AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC; | 
|  | 113 | if (builder.getDataCallbackProc() != nullptr) { | 
|  | 114 | streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK; | 
|  | 115 | callback = getLegacyCallback(); | 
|  | 116 | callbackData = this; | 
|  | 117 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 118 | // If the total buffer size is unspecified then base the size on the burst size. | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 119 | if (frameCount == 0 | 
|  | 120 | && ((flags & AUDIO_OUTPUT_FLAG_FAST) != 0)) { | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 121 | // Take advantage of a special trick that allows us to create a buffer | 
|  | 122 | // that is some multiple of the burst size. | 
|  | 123 | notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY; | 
|  | 124 | } | 
|  | 125 | } | 
|  | 126 | mCallbackBufferSize = builder.getFramesPerDataCallback(); | 
|  | 127 |  | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 128 | ALOGD("open(), request notificationFrames = %d, frameCount = %u", | 
| Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 129 | notificationFrames, (uint)frameCount); | 
| Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 130 |  | 
|  | 131 | // Don't call mAudioTrack->setDeviceId() because it will be overwritten by set()! | 
|  | 132 | audio_port_handle_t selectedDeviceId = (getDeviceId() == AAUDIO_UNSPECIFIED) | 
|  | 133 | ? AUDIO_PORT_HANDLE_NONE | 
|  | 134 | : getDeviceId(); | 
|  | 135 |  | 
| Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 136 | const audio_content_type_t contentType = | 
|  | 137 | AAudioConvert_contentTypeToInternal(builder.getContentType()); | 
|  | 138 | const audio_usage_t usage = | 
|  | 139 | AAudioConvert_usageToInternal(builder.getUsage()); | 
| Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 140 | const audio_flags_mask_t attributesFlags = | 
|  | 141 | AAudioConvert_allowCapturePolicyToAudioFlagsMask(builder.getAllowedCapturePolicy()); | 
| Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 142 |  | 
|  | 143 | const audio_attributes_t attributes = { | 
|  | 144 | .content_type = contentType, | 
|  | 145 | .usage = usage, | 
|  | 146 | .source = AUDIO_SOURCE_DEFAULT, // only used for recording | 
| Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 147 | .flags = attributesFlags, | 
| Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 148 | .tags = "" | 
|  | 149 | }; | 
|  | 150 |  | 
| Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 151 | mAudioTrack = new AudioTrack(); | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 152 | // TODO b/182392769: use identity util | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 153 | mAudioTrack->set( | 
| Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 154 | AUDIO_STREAM_DEFAULT,  // ignored because we pass attributes below | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 155 | getSampleRate(), | 
|  | 156 | format, | 
|  | 157 | channelMask, | 
|  | 158 | frameCount, | 
|  | 159 | flags, | 
|  | 160 | callback, | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 161 | callbackData, | 
|  | 162 | notificationFrames, | 
| Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 163 | 0,       // DEFAULT sharedBuffer*/, | 
|  | 164 | false,   // DEFAULT threadCanCallJava | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 165 | sessionId, | 
| Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 166 | streamTransferType, | 
|  | 167 | NULL,    // DEFAULT audio_offload_info_t | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 168 | Identity(), // DEFAULT uid and pid | 
| Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 169 | &attributes, | 
| Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 170 | // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging | 
|  | 171 | // headphones a few times. | 
|  | 172 | false,   // DEFAULT doNotReconnect, | 
|  | 173 | 1.0f,    // DEFAULT maxRequiredSpeed | 
|  | 174 | selectedDeviceId | 
|  | 175 | ); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 176 |  | 
| Phil Burk | d3813f3 | 2020-04-23 16:26:15 -0700 | [diff] [blame] | 177 | // Set it here so it can be logged by the destructor if the open failed. | 
|  | 178 | mAudioTrack->setCallerName(kCallerName); | 
|  | 179 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 180 | // Did we get a valid track? | 
|  | 181 | status_t status = mAudioTrack->initCheck(); | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 182 | if (status != NO_ERROR) { | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 183 | safeReleaseClose(); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 184 | ALOGE("open(), initCheck() returned %d", status); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 185 | return AAudioConvert_androidToAAudioResult(status); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 188 | mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_TRACK) | 
|  | 189 | + std::to_string(mAudioTrack->getPortId()); | 
|  | 190 |  | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 191 | doSetVolume(); | 
| Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 192 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 193 | // Get the actual values from the AudioTrack. | 
|  | 194 | setSamplesPerFrame(mAudioTrack->channelCount()); | 
| Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 195 | setFormat(mAudioTrack->format()); | 
|  | 196 | setDeviceFormat(mAudioTrack->format()); | 
| Phil Burk | 8d97b8e | 2020-09-25 23:18:14 +0000 | [diff] [blame] | 197 | setSampleRate(mAudioTrack->getSampleRate()); | 
|  | 198 | setBufferCapacity(getBufferCapacityFromDevice()); | 
|  | 199 | setFramesPerBurst(getFramesPerBurstFromDevice()); | 
| Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 200 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 201 | // We may need to pass the data through a block size adapter to guarantee constant size. | 
|  | 202 | if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) { | 
| Phil Burk | 4b86749 | 2020-02-12 10:58:05 -0800 | [diff] [blame] | 203 | // This may need to change if we add format conversion before | 
|  | 204 | // the block size adaptation. | 
|  | 205 | mBlockAdapterBytesPerFrame = getBytesPerFrame(); | 
|  | 206 | int callbackSizeBytes = mBlockAdapterBytesPerFrame * mCallbackBufferSize; | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 207 | mFixedBlockReader.open(callbackSizeBytes); | 
|  | 208 | mBlockAdapter = &mFixedBlockReader; | 
|  | 209 | } else { | 
|  | 210 | mBlockAdapter = nullptr; | 
|  | 211 | } | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 212 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 213 | setState(AAUDIO_STREAM_STATE_OPEN); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 214 | setDeviceId(mAudioTrack->getRoutedDeviceId()); | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 215 |  | 
|  | 216 | aaudio_session_id_t actualSessionId = | 
|  | 217 | (requestedSessionId == AAUDIO_SESSION_ID_NONE) | 
|  | 218 | ? AAUDIO_SESSION_ID_NONE | 
|  | 219 | : (aaudio_session_id_t) mAudioTrack->getSessionId(); | 
|  | 220 | setSessionId(actualSessionId); | 
|  | 221 |  | 
| Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 222 | mAudioTrack->addAudioDeviceCallback(this); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 223 |  | 
| Phil Burk | 09c27ca | 2017-08-24 22:12:56 -0700 | [diff] [blame] | 224 | // Update performance mode based on the actual stream flags. | 
| Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 225 | // For example, if the sample rate is not allowed then you won't get a FAST track. | 
|  | 226 | audio_output_flags_t actualFlags = mAudioTrack->getFlags(); | 
|  | 227 | aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; | 
| Phil Burk | 09c27ca | 2017-08-24 22:12:56 -0700 | [diff] [blame] | 228 | // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY. | 
|  | 229 | if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) { | 
| Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 230 | actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; | 
| Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 231 | } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
|  | 232 | actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; | 
|  | 233 | } | 
|  | 234 | setPerformanceMode(actualPerformanceMode); | 
| Phil Burk | efa5600 | 2017-08-02 15:07:21 -0700 | [diff] [blame] | 235 |  | 
|  | 236 | setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy | 
|  | 237 |  | 
| Phil Burk | 7216b3f | 2020-06-23 23:29:18 +0000 | [diff] [blame] | 238 | // Log if we did not get what we asked for. | 
|  | 239 | ALOGD_IF(actualFlags != flags, | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 240 | "open() flags changed from 0x%08X to 0x%08X", | 
| Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 241 | flags, actualFlags); | 
| Phil Burk | 7216b3f | 2020-06-23 23:29:18 +0000 | [diff] [blame] | 242 | ALOGD_IF(actualPerformanceMode != perfMode, | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 243 | "open() perfMode changed from %d to %d", | 
| Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 244 | perfMode, actualPerformanceMode); | 
|  | 245 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 246 | return AAUDIO_OK; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 249 | aaudio_result_t AudioStreamTrack::release_l() { | 
|  | 250 | if (getState() != AAUDIO_STREAM_STATE_CLOSING) { | 
| Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 251 | status_t err = mAudioTrack->removeAudioDeviceCallback(this); | 
|  | 252 | ALOGE_IF(err, "%s() removeAudioDeviceCallback returned %d", __func__, err); | 
| Phil Burk | 64e16a7 | 2020-06-01 13:25:51 -0700 | [diff] [blame] | 253 | logReleaseBufferState(); | 
| Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 254 | // Data callbacks may still be running! | 
| Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 255 | return AudioStream::release_l(); | 
|  | 256 | } else { | 
|  | 257 | return AAUDIO_OK; // already released | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 258 | } | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
| Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 261 | void AudioStreamTrack::close_l() { | 
|  | 262 | // Stop callbacks before deleting mFixedBlockReader memory. | 
|  | 263 | mAudioTrack.clear(); | 
| Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 264 | // Do not close mFixedBlockReader because a data callback | 
|  | 265 | // thread might still be running if someone else has a reference | 
|  | 266 | // to mAudioRecord. | 
|  | 267 | // It has a unique_ptr to its buffer so it will clean up by itself. | 
| Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 268 | AudioStream::close_l(); | 
|  | 269 | } | 
|  | 270 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 271 | void AudioStreamTrack::processCallback(int event, void *info) { | 
|  | 272 |  | 
|  | 273 | switch (event) { | 
|  | 274 | case AudioTrack::EVENT_MORE_DATA: | 
|  | 275 | processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info); | 
|  | 276 | break; | 
|  | 277 |  | 
|  | 278 | // Stream got rerouted so we disconnect. | 
|  | 279 | case AudioTrack::EVENT_NEW_IAUDIOTRACK: | 
| Eric Laurent | 68eff05 | 2020-04-10 18:28:41 -0700 | [diff] [blame] | 280 | // request stream disconnect if the restored AudioTrack has properties not matching | 
|  | 281 | // what was requested initially | 
|  | 282 | if (mAudioTrack->channelCount() != getSamplesPerFrame() | 
|  | 283 | || mAudioTrack->format() != getFormat() | 
|  | 284 | || mAudioTrack->getSampleRate() != getSampleRate() | 
|  | 285 | || mAudioTrack->getRoutedDeviceId() != getDeviceId() | 
| Phil Burk | 8d97b8e | 2020-09-25 23:18:14 +0000 | [diff] [blame] | 286 | || getBufferCapacityFromDevice() != getBufferCapacity() | 
|  | 287 | || getFramesPerBurstFromDevice() != getFramesPerBurst()) { | 
| Eric Laurent | 68eff05 | 2020-04-10 18:28:41 -0700 | [diff] [blame] | 288 | processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info); | 
|  | 289 | } | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 290 | break; | 
|  | 291 |  | 
|  | 292 | default: | 
|  | 293 | break; | 
|  | 294 | } | 
|  | 295 | return; | 
|  | 296 | } | 
|  | 297 |  | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 298 | aaudio_result_t AudioStreamTrack::requestStart_l() { | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 299 | if (mAudioTrack.get() == nullptr) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 300 | ALOGE("requestStart() no AudioTrack"); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 301 | return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 302 | } | 
|  | 303 | // Get current position so we can detect when the track is playing. | 
|  | 304 | status_t err = mAudioTrack->getPosition(&mPositionWhenStarting); | 
|  | 305 | if (err != OK) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 306 | return AAudioConvert_androidToAAudioResult(err); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 307 | } | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 308 |  | 
| Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 309 | // Enable callback before starting AudioTrack to avoid shutting | 
|  | 310 | // down because of a race condition. | 
|  | 311 | mCallbackEnabled.store(true); | 
| Phil Burk | 4c377ef | 2020-06-25 10:03:23 -0700 | [diff] [blame] | 312 | aaudio_stream_state_t originalState = getState(); | 
|  | 313 | // Set before starting the callback so that we are in the correct state | 
|  | 314 | // before updateStateMachine() can be called by the callback. | 
|  | 315 | setState(AAUDIO_STREAM_STATE_STARTING); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 316 | err = mAudioTrack->start(); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 317 | if (err != OK) { | 
| Phil Burk | 4c377ef | 2020-06-25 10:03:23 -0700 | [diff] [blame] | 318 | mCallbackEnabled.store(false); | 
|  | 319 | setState(originalState); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 320 | return AAudioConvert_androidToAAudioResult(err); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 321 | } | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 322 | return AAUDIO_OK; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 325 | aaudio_result_t AudioStreamTrack::requestPause_l() { | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 326 | if (mAudioTrack.get() == nullptr) { | 
| Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 327 | ALOGE("%s() no AudioTrack", __func__); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 328 | return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 329 | } | 
| Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 330 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 331 | setState(AAUDIO_STREAM_STATE_PAUSING); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 332 | mAudioTrack->pause(); | 
| Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 333 | mCallbackEnabled.store(false); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 334 | status_t err = mAudioTrack->getPosition(&mPositionWhenPausing); | 
|  | 335 | if (err != OK) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 336 | return AAudioConvert_androidToAAudioResult(err); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 337 | } | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 338 | return checkForDisconnectRequest(false); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 339 | } | 
|  | 340 |  | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 341 | aaudio_result_t AudioStreamTrack::requestFlush_l() { | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 342 | if (mAudioTrack.get() == nullptr) { | 
| Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 343 | ALOGE("%s() no AudioTrack", __func__); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 344 | return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 345 | } | 
| Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 346 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 347 | setState(AAUDIO_STREAM_STATE_FLUSHING); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 348 | incrementFramesRead(getFramesWritten() - getFramesRead()); | 
|  | 349 | mAudioTrack->flush(); | 
| Phil Burk | 2b6f128 | 2018-05-10 15:26:30 -0700 | [diff] [blame] | 350 | mFramesRead.reset32(); // service reads frames, service position reset on flush | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 351 | mTimestampPosition.reset32(); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 352 | return AAUDIO_OK; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
| Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 355 | aaudio_result_t AudioStreamTrack::requestStop_l() { | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 356 | if (mAudioTrack.get() == nullptr) { | 
| Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 357 | ALOGE("%s() no AudioTrack", __func__); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 358 | return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 359 | } | 
| Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 360 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 361 | setState(AAUDIO_STREAM_STATE_STOPPING); | 
| Phil Burk | 18c8476 | 2018-12-18 12:15:35 -0800 | [diff] [blame] | 362 | mFramesRead.catchUpTo(getFramesWritten()); | 
|  | 363 | mTimestampPosition.catchUpTo(getFramesWritten()); | 
| Phil Burk | 2b6f128 | 2018-05-10 15:26:30 -0700 | [diff] [blame] | 364 | mFramesRead.reset32(); // service reads frames, service position reset on stop | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 365 | mTimestampPosition.reset32(); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 366 | mAudioTrack->stop(); | 
| Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 367 | mCallbackEnabled.store(false); | 
| Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 368 | return checkForDisconnectRequest(false);; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 369 | } | 
|  | 370 |  | 
| Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 371 | aaudio_result_t AudioStreamTrack::updateStateMachine() | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 372 | { | 
|  | 373 | status_t err; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 374 | aaudio_wrapping_frames_t position; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 375 | switch (getState()) { | 
|  | 376 | // TODO add better state visibility to AudioTrack | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 377 | case AAUDIO_STREAM_STATE_STARTING: | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 378 | if (mAudioTrack->hasStarted()) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 379 | setState(AAUDIO_STREAM_STATE_STARTED); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 380 | } | 
|  | 381 | break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 382 | case AAUDIO_STREAM_STATE_PAUSING: | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 383 | if (mAudioTrack->stopped()) { | 
|  | 384 | err = mAudioTrack->getPosition(&position); | 
|  | 385 | if (err != OK) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 386 | return AAudioConvert_androidToAAudioResult(err); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 387 | } else if (position == mPositionWhenPausing) { | 
|  | 388 | // Has stream really stopped advancing? | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 389 | setState(AAUDIO_STREAM_STATE_PAUSED); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 390 | } | 
|  | 391 | mPositionWhenPausing = position; | 
|  | 392 | } | 
|  | 393 | break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 394 | case AAUDIO_STREAM_STATE_FLUSHING: | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 395 | { | 
|  | 396 | err = mAudioTrack->getPosition(&position); | 
|  | 397 | if (err != OK) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 398 | return AAudioConvert_androidToAAudioResult(err); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 399 | } else if (position == 0) { | 
| Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 400 | // TODO Advance frames read to match written. | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 401 | setState(AAUDIO_STREAM_STATE_FLUSHED); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 402 | } | 
|  | 403 | } | 
|  | 404 | break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 405 | case AAUDIO_STREAM_STATE_STOPPING: | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 406 | if (mAudioTrack->stopped()) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 407 | setState(AAUDIO_STREAM_STATE_STOPPED); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 408 | } | 
|  | 409 | break; | 
|  | 410 | default: | 
|  | 411 | break; | 
|  | 412 | } | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 413 | return AAUDIO_OK; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 414 | } | 
|  | 415 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 416 | aaudio_result_t AudioStreamTrack::write(const void *buffer, | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 417 | int32_t numFrames, | 
|  | 418 | int64_t timeoutNanoseconds) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 419 | { | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 420 | int32_t bytesPerFrame = getBytesPerFrame(); | 
|  | 421 | int32_t numBytes; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 422 | aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes); | 
|  | 423 | if (result != AAUDIO_OK) { | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 424 | return result; | 
|  | 425 | } | 
|  | 426 |  | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 427 | if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) { | 
|  | 428 | return AAUDIO_ERROR_DISCONNECTED; | 
|  | 429 | } | 
|  | 430 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 431 | // TODO add timeout to AudioTrack | 
|  | 432 | bool blocking = timeoutNanoseconds > 0; | 
|  | 433 | ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking); | 
|  | 434 | if (bytesWritten == WOULD_BLOCK) { | 
|  | 435 | return 0; | 
|  | 436 | } else if (bytesWritten < 0) { | 
|  | 437 | ALOGE("invalid write, returned %d", (int)bytesWritten); | 
| Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 438 | // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to | 
|  | 439 | // AudioTrack invalidation | 
|  | 440 | if (bytesWritten == DEAD_OBJECT) { | 
|  | 441 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); | 
|  | 442 | return AAUDIO_ERROR_DISCONNECTED; | 
|  | 443 | } | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 444 | return AAudioConvert_androidToAAudioResult(bytesWritten); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 445 | } | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 446 | int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 447 | incrementFramesWritten(framesWritten); | 
| Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 448 |  | 
|  | 449 | result = updateStateMachine(); | 
|  | 450 | if (result != AAUDIO_OK) { | 
|  | 451 | return result; | 
|  | 452 | } | 
|  | 453 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 454 | return framesWritten; | 
|  | 455 | } | 
|  | 456 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 457 | aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 458 | { | 
| Phil Burk | 4185268 | 2019-03-29 10:58:15 -0700 | [diff] [blame] | 459 | // Do not ask for less than one burst. | 
|  | 460 | if (requestedFrames < getFramesPerBurst()) { | 
|  | 461 | requestedFrames = getFramesPerBurst(); | 
|  | 462 | } | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 463 | ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames); | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 464 | if (result < 0) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 465 | return AAudioConvert_androidToAAudioResult(result); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 466 | } else { | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 467 | return result; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 468 | } | 
|  | 469 | } | 
|  | 470 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 471 | int32_t AudioStreamTrack::getBufferSize() const | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 472 | { | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 473 | return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames()); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 474 | } | 
|  | 475 |  | 
| Phil Burk | 8d97b8e | 2020-09-25 23:18:14 +0000 | [diff] [blame] | 476 | int32_t AudioStreamTrack::getBufferCapacityFromDevice() const | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 477 | { | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 478 | return static_cast<int32_t>(mAudioTrack->frameCount()); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 479 | } | 
|  | 480 |  | 
|  | 481 | int32_t AudioStreamTrack::getXRunCount() const | 
|  | 482 | { | 
|  | 483 | return static_cast<int32_t>(mAudioTrack->getUnderrunCount()); | 
|  | 484 | } | 
|  | 485 |  | 
| Phil Burk | 8d97b8e | 2020-09-25 23:18:14 +0000 | [diff] [blame] | 486 | int32_t AudioStreamTrack::getFramesPerBurstFromDevice() const { | 
| Phil Burk | b588402 | 2017-03-27 15:26:14 -0700 | [diff] [blame] | 487 | return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames()); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 488 | } | 
|  | 489 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 490 | int64_t AudioStreamTrack::getFramesRead() { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 491 | aaudio_wrapping_frames_t position; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 492 | status_t result; | 
|  | 493 | switch (getState()) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 494 | case AAUDIO_STREAM_STATE_STARTING: | 
|  | 495 | case AAUDIO_STREAM_STATE_STARTED: | 
|  | 496 | case AAUDIO_STREAM_STATE_STOPPING: | 
| Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 497 | case AAUDIO_STREAM_STATE_PAUSING: | 
|  | 498 | case AAUDIO_STREAM_STATE_PAUSED: | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 499 | result = mAudioTrack->getPosition(&position); | 
|  | 500 | if (result == OK) { | 
|  | 501 | mFramesRead.update32(position); | 
|  | 502 | } | 
|  | 503 | break; | 
|  | 504 | default: | 
|  | 505 | break; | 
|  | 506 | } | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 507 | return AudioStreamLegacy::getFramesRead(); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 508 | } | 
| Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 509 |  | 
|  | 510 | aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId, | 
|  | 511 | int64_t *framePosition, | 
|  | 512 | int64_t *timeNanoseconds) { | 
|  | 513 | ExtendedTimestamp extendedTimestamp; | 
|  | 514 | status_t status = mAudioTrack->getTimestamp(&extendedTimestamp); | 
| Phil Burk | c75d97f | 2017-09-08 15:48:36 -0700 | [diff] [blame] | 515 | if (status == WOULD_BLOCK) { | 
|  | 516 | return AAUDIO_ERROR_INVALID_STATE; | 
|  | 517 | } if (status != NO_ERROR) { | 
| Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 518 | return AAudioConvert_androidToAAudioResult(status); | 
|  | 519 | } | 
| Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 520 | int64_t position = 0; | 
|  | 521 | int64_t nanoseconds = 0; | 
|  | 522 | aaudio_result_t result = getBestTimestamp(clockId, &position, | 
|  | 523 | &nanoseconds, &extendedTimestamp); | 
|  | 524 | if (result == AAUDIO_OK) { | 
|  | 525 | if (position < getFramesWritten()) { | 
|  | 526 | *framePosition = position; | 
|  | 527 | *timeNanoseconds = nanoseconds; | 
|  | 528 | return result; | 
|  | 529 | } else { | 
|  | 530 | return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent | 
|  | 531 | } | 
|  | 532 | } | 
|  | 533 | return result; | 
| Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 534 | } | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 535 |  | 
|  | 536 | status_t AudioStreamTrack::doSetVolume() { | 
|  | 537 | status_t status = NO_INIT; | 
|  | 538 | if (mAudioTrack.get() != nullptr) { | 
|  | 539 | float volume = getDuckAndMuteVolume(); | 
|  | 540 | mAudioTrack->setVolume(volume, volume); | 
|  | 541 | status = NO_ERROR; | 
|  | 542 | } | 
|  | 543 | return status; | 
|  | 544 | } | 
|  | 545 |  | 
|  | 546 | #if AAUDIO_USE_VOLUME_SHAPER | 
| Phil Burk | 8a8a9e5 | 2017-09-12 16:25:15 -0700 | [diff] [blame] | 547 |  | 
|  | 548 | using namespace android::media::VolumeShaper; | 
|  | 549 |  | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 550 | binder::Status AudioStreamTrack::applyVolumeShaper( | 
|  | 551 | const VolumeShaper::Configuration& configuration, | 
|  | 552 | const VolumeShaper::Operation& operation) { | 
|  | 553 |  | 
|  | 554 | sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration); | 
|  | 555 | sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation); | 
|  | 556 |  | 
|  | 557 | if (mAudioTrack.get() != nullptr) { | 
|  | 558 | ALOGD("applyVolumeShaper() from IPlayer"); | 
| Phil Burk | 8a8a9e5 | 2017-09-12 16:25:15 -0700 | [diff] [blame] | 559 | binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 560 | if (status < 0) { // a non-negative value is the volume shaper id. | 
|  | 561 | ALOGE("applyVolumeShaper() failed with status %d", status); | 
|  | 562 | } | 
| Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 563 | return aidl_utils::binderStatusFromStatusT(status); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 564 | } else { | 
|  | 565 | ALOGD("applyVolumeShaper()" | 
|  | 566 | " no AudioTrack for volume control from IPlayer"); | 
|  | 567 | return binder::Status::ok(); | 
|  | 568 | } | 
|  | 569 | } | 
|  | 570 | #endif |