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