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 | |
| 17 | #define LOG_TAG "AudioStreamTrack" |
| 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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include "utility/AudioClock.h" |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 26 | #include "legacy/AudioStreamLegacy.h" |
| 27 | #include "legacy/AudioStreamTrack.h" |
| 28 | #include "utility/FixedBlockReader.h" |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace android; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | using namespace aaudio; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 32 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 33 | // Arbitrary and somewhat generous number of bursts. |
| 34 | #define DEFAULT_BURSTS_PER_BUFFER_CAPACITY 8 |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 35 | static const bool FAST_TRACKS_ENABLED = true; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 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 | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 62 | ALOGD("AudioStreamTrack::open = %p", this); |
| 63 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 64 | // Try to create an AudioTrack |
| 65 | // TODO Support UNSPECIFIED in AudioTrack. For now, use stereo if unspecified. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 66 | int32_t samplesPerFrame = (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 67 | ? 2 : getSamplesPerFrame(); |
| 68 | audio_channel_mask_t channelMask = audio_channel_out_mask_from_count(samplesPerFrame); |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 69 | ALOGD("AudioStreamTrack::open(), samplesPerFrame = %d, channelMask = 0x%08x", |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 70 | samplesPerFrame, channelMask); |
| 71 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 72 | // TODO add more performance options |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 73 | audio_output_flags_t flags = FAST_TRACKS_ENABLED |
| 74 | ? AUDIO_OUTPUT_FLAG_FAST |
| 75 | : AUDIO_OUTPUT_FLAG_NONE; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 76 | |
| 77 | int32_t frameCount = builder.getBufferCapacity(); |
| 78 | ALOGD("AudioStreamTrack::open(), requested buffer capacity %d", frameCount); |
| 79 | |
| 80 | int32_t notificationFrames = 0; |
| 81 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 82 | // TODO implement an unspecified AudioTrack format then use that. |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 83 | audio_format_t format = (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 84 | ? AUDIO_FORMAT_PCM_FLOAT |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 85 | : AAudioConvert_aaudioToAndroidDataFormat(getFormat()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 86 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 87 | // Setup the callback if there is one. |
| 88 | AudioTrack::callback_t callback = nullptr; |
| 89 | void *callbackData = nullptr; |
| 90 | // Note that TRANSFER_SYNC does not allow FAST track |
| 91 | AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC; |
| 92 | if (builder.getDataCallbackProc() != nullptr) { |
| 93 | streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK; |
| 94 | callback = getLegacyCallback(); |
| 95 | callbackData = this; |
| 96 | |
| 97 | notificationFrames = builder.getFramesPerDataCallback(); |
| 98 | // If the total buffer size is unspecified then base the size on the burst size. |
| 99 | if (frameCount == AAUDIO_UNSPECIFIED) { |
| 100 | // Take advantage of a special trick that allows us to create a buffer |
| 101 | // that is some multiple of the burst size. |
| 102 | notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY; |
| 103 | } |
| 104 | } |
| 105 | mCallbackBufferSize = builder.getFramesPerDataCallback(); |
| 106 | |
| 107 | ALOGD("AudioStreamTrack::open(), notificationFrames = %d", notificationFrames); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 108 | mAudioTrack = new AudioTrack( |
| 109 | (audio_stream_type_t) AUDIO_STREAM_MUSIC, |
| 110 | getSampleRate(), |
| 111 | format, |
| 112 | channelMask, |
| 113 | frameCount, |
| 114 | flags, |
| 115 | callback, |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 116 | callbackData, |
| 117 | notificationFrames, |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 118 | AUDIO_SESSION_ALLOCATE, |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 119 | streamTransferType |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 120 | ); |
| 121 | |
| 122 | // Did we get a valid track? |
| 123 | status_t status = mAudioTrack->initCheck(); |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 124 | ALOGD("AudioStreamTrack::open(), initCheck() returned %d", status); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 125 | if (status != NO_ERROR) { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 126 | close(); |
| 127 | ALOGE("AudioStreamTrack::open(), initCheck() returned %d", status); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 128 | return AAudioConvert_androidToAAudioResult(status); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Get the actual values from the AudioTrack. |
| 132 | setSamplesPerFrame(mAudioTrack->channelCount()); |
| 133 | setSampleRate(mAudioTrack->getSampleRate()); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 134 | aaudio_audio_format_t aaudioFormat = |
| 135 | AAudioConvert_androidToAAudioDataFormat(mAudioTrack->format()); |
| 136 | setFormat(aaudioFormat); |
| 137 | |
| 138 | // We may need to pass the data through a block size adapter to guarantee constant size. |
| 139 | if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) { |
| 140 | int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize; |
| 141 | mFixedBlockReader.open(callbackSizeBytes); |
| 142 | mBlockAdapter = &mFixedBlockReader; |
| 143 | } else { |
| 144 | mBlockAdapter = nullptr; |
| 145 | } |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 146 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 147 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 148 | setDeviceId(mAudioTrack->getRoutedDeviceId()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 149 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 150 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 153 | aaudio_result_t AudioStreamTrack::close() |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 154 | { |
| 155 | // TODO maybe add close() or release() to AudioTrack API then call it from here |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 156 | if (getState() != AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 157 | mAudioTrack.clear(); // TODO is this right? |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 158 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 159 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 160 | mFixedBlockReader.close(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 161 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 164 | void AudioStreamTrack::processCallback(int event, void *info) { |
| 165 | |
| 166 | switch (event) { |
| 167 | case AudioTrack::EVENT_MORE_DATA: |
| 168 | processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info); |
| 169 | break; |
| 170 | |
| 171 | // Stream got rerouted so we disconnect. |
| 172 | case AudioTrack::EVENT_NEW_IAUDIOTRACK: |
| 173 | processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info); |
| 174 | break; |
| 175 | |
| 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | return; |
| 180 | } |
| 181 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 182 | aaudio_result_t AudioStreamTrack::requestStart() |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 183 | { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 184 | std::lock_guard<std::mutex> lock(mStreamMutex); |
| 185 | |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 186 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 187 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 188 | } |
| 189 | // Get current position so we can detect when the track is playing. |
| 190 | status_t err = mAudioTrack->getPosition(&mPositionWhenStarting); |
| 191 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 192 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 193 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 194 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 195 | err = mAudioTrack->start(); |
| 196 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 197 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 198 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 199 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 200 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 201 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 204 | aaudio_result_t AudioStreamTrack::requestPause() |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 205 | { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 206 | std::lock_guard<std::mutex> lock(mStreamMutex); |
| 207 | |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 208 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 209 | return AAUDIO_ERROR_INVALID_STATE; |
| 210 | } else if (getState() != AAUDIO_STREAM_STATE_STARTING |
| 211 | && getState() != AAUDIO_STREAM_STATE_STARTED) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 212 | ALOGE("requestPause(), called when state is %s", |
| 213 | AAudio_convertStreamStateToText(getState())); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 214 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 215 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 216 | setState(AAUDIO_STREAM_STATE_PAUSING); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 217 | mAudioTrack->pause(); |
| 218 | status_t err = mAudioTrack->getPosition(&mPositionWhenPausing); |
| 219 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 220 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 221 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 222 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 225 | aaudio_result_t AudioStreamTrack::requestFlush() { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 226 | std::lock_guard<std::mutex> lock(mStreamMutex); |
| 227 | |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 228 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 229 | return AAUDIO_ERROR_INVALID_STATE; |
| 230 | } else if (getState() != AAUDIO_STREAM_STATE_PAUSED) { |
| 231 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 232 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 233 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 234 | incrementFramesRead(getFramesWritten() - getFramesRead()); |
| 235 | mAudioTrack->flush(); |
| 236 | mFramesWritten.reset32(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 237 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 240 | aaudio_result_t AudioStreamTrack::requestStop() { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 241 | std::lock_guard<std::mutex> lock(mStreamMutex); |
| 242 | |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 243 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 244 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 245 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 246 | setState(AAUDIO_STREAM_STATE_STOPPING); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 247 | incrementFramesRead(getFramesWritten() - getFramesRead()); // TODO review |
| 248 | mAudioTrack->stop(); |
| 249 | mFramesWritten.reset32(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 250 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 253 | aaudio_result_t AudioStreamTrack::updateStateWhileWaiting() |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 254 | { |
| 255 | status_t err; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 256 | aaudio_wrapping_frames_t position; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 257 | switch (getState()) { |
| 258 | // TODO add better state visibility to AudioTrack |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 259 | case AAUDIO_STREAM_STATE_STARTING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 260 | if (mAudioTrack->hasStarted()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 261 | setState(AAUDIO_STREAM_STATE_STARTED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 262 | } |
| 263 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 264 | case AAUDIO_STREAM_STATE_PAUSING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 265 | if (mAudioTrack->stopped()) { |
| 266 | err = mAudioTrack->getPosition(&position); |
| 267 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 268 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 269 | } else if (position == mPositionWhenPausing) { |
| 270 | // Has stream really stopped advancing? |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 271 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 272 | } |
| 273 | mPositionWhenPausing = position; |
| 274 | } |
| 275 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 276 | case AAUDIO_STREAM_STATE_FLUSHING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 277 | { |
| 278 | err = mAudioTrack->getPosition(&position); |
| 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 if (position == 0) { |
| 282 | // Advance frames read to match written. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 283 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 287 | case AAUDIO_STREAM_STATE_STOPPING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 288 | if (mAudioTrack->stopped()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 289 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 290 | } |
| 291 | break; |
| 292 | default: |
| 293 | break; |
| 294 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 295 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 298 | aaudio_result_t AudioStreamTrack::write(const void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 299 | int32_t numFrames, |
| 300 | int64_t timeoutNanoseconds) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 301 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 302 | int32_t bytesPerFrame = getBytesPerFrame(); |
| 303 | int32_t numBytes; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 304 | aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes); |
| 305 | if (result != AAUDIO_OK) { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 306 | return result; |
| 307 | } |
| 308 | |
| 309 | // TODO add timeout to AudioTrack |
| 310 | bool blocking = timeoutNanoseconds > 0; |
| 311 | ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking); |
| 312 | if (bytesWritten == WOULD_BLOCK) { |
| 313 | return 0; |
| 314 | } else if (bytesWritten < 0) { |
| 315 | ALOGE("invalid write, returned %d", (int)bytesWritten); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 316 | return AAudioConvert_androidToAAudioResult(bytesWritten); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 317 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 318 | int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 319 | incrementFramesWritten(framesWritten); |
| 320 | return framesWritten; |
| 321 | } |
| 322 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 323 | aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 324 | { |
| 325 | ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 326 | if (result < 0) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 327 | return AAudioConvert_androidToAAudioResult(result); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 328 | } else { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 329 | return result; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 333 | int32_t AudioStreamTrack::getBufferSize() const |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 334 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 335 | return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 338 | int32_t AudioStreamTrack::getBufferCapacity() const |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 339 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 340 | return static_cast<int32_t>(mAudioTrack->frameCount()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | int32_t AudioStreamTrack::getXRunCount() const |
| 344 | { |
| 345 | return static_cast<int32_t>(mAudioTrack->getUnderrunCount()); |
| 346 | } |
| 347 | |
| 348 | int32_t AudioStreamTrack::getFramesPerBurst() const |
| 349 | { |
Phil Burk | b588402 | 2017-03-27 15:26:14 -0700 | [diff] [blame] | 350 | return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 353 | int64_t AudioStreamTrack::getFramesRead() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 354 | aaudio_wrapping_frames_t position; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 355 | status_t result; |
| 356 | switch (getState()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 357 | case AAUDIO_STREAM_STATE_STARTING: |
| 358 | case AAUDIO_STREAM_STATE_STARTED: |
| 359 | case AAUDIO_STREAM_STATE_STOPPING: |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 360 | case AAUDIO_STREAM_STATE_PAUSING: |
| 361 | case AAUDIO_STREAM_STATE_PAUSED: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 362 | result = mAudioTrack->getPosition(&position); |
| 363 | if (result == OK) { |
| 364 | mFramesRead.update32(position); |
| 365 | } |
| 366 | break; |
| 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | return AudioStream::getFramesRead(); |
| 371 | } |
Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 372 | |
| 373 | aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId, |
| 374 | int64_t *framePosition, |
| 375 | int64_t *timeNanoseconds) { |
| 376 | ExtendedTimestamp extendedTimestamp; |
| 377 | status_t status = mAudioTrack->getTimestamp(&extendedTimestamp); |
| 378 | if (status != NO_ERROR) { |
| 379 | return AAudioConvert_androidToAAudioResult(status); |
| 380 | } |
| 381 | // TODO Merge common code into AudioStreamLegacy after rebasing. |
| 382 | int timebase; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 383 | switch (clockId) { |
Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 384 | case CLOCK_BOOTTIME: |
| 385 | timebase = ExtendedTimestamp::TIMEBASE_BOOTTIME; |
| 386 | break; |
| 387 | case CLOCK_MONOTONIC: |
| 388 | timebase = ExtendedTimestamp::TIMEBASE_MONOTONIC; |
| 389 | break; |
| 390 | default: |
| 391 | ALOGE("getTimestamp() - Unrecognized clock type %d", (int) clockId); |
| 392 | return AAUDIO_ERROR_UNEXPECTED_VALUE; |
| 393 | break; |
| 394 | } |
| 395 | status = extendedTimestamp.getBestTimestamp(framePosition, timeNanoseconds, timebase); |
| 396 | return AAudioConvert_androidToAAudioResult(status); |
| 397 | } |