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