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