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