blob: db07925b8ae3dd60e82ef8699bd3393631db1398 [file] [log] [blame]
Phil Burke1ce4912016-11-21 10:40:25 -08001/*
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 Burk965650e2017-09-07 21:00:09 -070017#define LOG_TAG "AudioStreamTrack"
Phil Burke1ce4912016-11-21 10:40:25 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <media/AudioTrack.h>
Phil Burke1ce4912016-11-21 10:40:25 -080023
Phil Burke4d7bb42017-03-28 11:32:39 -070024#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include "utility/AudioClock.h"
Phil Burke4d7bb42017-03-28 11:32:39 -070026#include "legacy/AudioStreamLegacy.h"
27#include "legacy/AudioStreamTrack.h"
28#include "utility/FixedBlockReader.h"
Phil Burke1ce4912016-11-21 10:40:25 -080029
30using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080031using namespace aaudio;
Phil Burke1ce4912016-11-21 10:40:25 -080032
Phil Burke4d7bb42017-03-28 11:32:39 -070033// Arbitrary and somewhat generous number of bursts.
34#define DEFAULT_BURSTS_PER_BUFFER_CAPACITY 8
35
Phil Burke1ce4912016-11-21 10:40:25 -080036/*
37 * Create a stream that uses the AudioTrack.
38 */
39AudioStreamTrack::AudioStreamTrack()
Phil Burke4d7bb42017-03-28 11:32:39 -070040 : AudioStreamLegacy()
41 , mFixedBlockReader(*this)
Phil Burke1ce4912016-11-21 10:40:25 -080042{
43}
44
45AudioStreamTrack::~AudioStreamTrack()
46{
Phil Burk5ed503c2017-02-01 09:38:15 -080047 const aaudio_stream_state_t state = getState();
48 bool bad = !(state == AAUDIO_STREAM_STATE_UNINITIALIZED || state == AAUDIO_STREAM_STATE_CLOSED);
Phil Burke1ce4912016-11-21 10:40:25 -080049 ALOGE_IF(bad, "stream not closed, in state %d", state);
50}
51
Phil Burk5ed503c2017-02-01 09:38:15 -080052aaudio_result_t AudioStreamTrack::open(const AudioStreamBuilder& builder)
Phil Burke1ce4912016-11-21 10:40:25 -080053{
Phil Burk5ed503c2017-02-01 09:38:15 -080054 aaudio_result_t result = AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -080055
56 result = AudioStream::open(builder);
57 if (result != OK) {
58 return result;
59 }
60
61 // Try to create an AudioTrack
Phil Burk5204d312017-05-04 17:16:13 -070062 // Use stereo if unspecified.
Phil Burk5ed503c2017-02-01 09:38:15 -080063 int32_t samplesPerFrame = (getSamplesPerFrame() == AAUDIO_UNSPECIFIED)
Phil Burke1ce4912016-11-21 10:40:25 -080064 ? 2 : getSamplesPerFrame();
65 audio_channel_mask_t channelMask = audio_channel_out_mask_from_count(samplesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -080066
Phil Burke2fbb592017-05-01 15:05:52 -070067 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
Phil Burk30a70772017-05-16 11:20:36 -070068 aaudio_performance_mode_t perfMode = getPerformanceMode();
69 switch(perfMode) {
Phil Burke2fbb592017-05-01 15:05:52 -070070 case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY:
71 // Bypass the normal mixer and go straight to the FAST mixer.
72 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW);
73 break;
74
75 case AAUDIO_PERFORMANCE_MODE_POWER_SAVING:
76 // This uses a mixer that wakes up less often than the FAST mixer.
77 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
78 break;
79
80 case AAUDIO_PERFORMANCE_MODE_NONE:
81 default:
82 // No flags. Use a normal mixer in front of the FAST mixer.
83 break;
84 }
Phil Burke4d7bb42017-03-28 11:32:39 -070085
Phil Burkcf5f6d22017-05-26 12:35:07 -070086 size_t frameCount = (size_t)builder.getBufferCapacity();
Phil Burke4d7bb42017-03-28 11:32:39 -070087
88 int32_t notificationFrames = 0;
89
Phil Burke4d7bb42017-03-28 11:32:39 -070090 audio_format_t format = (getFormat() == AAUDIO_FORMAT_UNSPECIFIED)
Phil Burke1ce4912016-11-21 10:40:25 -080091 ? AUDIO_FORMAT_PCM_FLOAT
Phil Burk5ed503c2017-02-01 09:38:15 -080092 : AAudioConvert_aaudioToAndroidDataFormat(getFormat());
Phil Burke1ce4912016-11-21 10:40:25 -080093
Phil Burke4d7bb42017-03-28 11:32:39 -070094 // Setup the callback if there is one.
95 AudioTrack::callback_t callback = nullptr;
96 void *callbackData = nullptr;
97 // Note that TRANSFER_SYNC does not allow FAST track
98 AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC;
99 if (builder.getDataCallbackProc() != nullptr) {
100 streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK;
101 callback = getLegacyCallback();
102 callbackData = this;
103
Phil Burke4d7bb42017-03-28 11:32:39 -0700104 // If the total buffer size is unspecified then base the size on the burst size.
Phil Burk4485d412017-05-09 15:55:02 -0700105 if (frameCount == 0
106 && ((flags & AUDIO_OUTPUT_FLAG_FAST) != 0)) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700107 // Take advantage of a special trick that allows us to create a buffer
108 // that is some multiple of the burst size.
109 notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY;
Phil Burk4485d412017-05-09 15:55:02 -0700110 } else {
111 notificationFrames = builder.getFramesPerDataCallback();
Phil Burke4d7bb42017-03-28 11:32:39 -0700112 }
113 }
114 mCallbackBufferSize = builder.getFramesPerDataCallback();
115
Phil Burkfbf031e2017-10-12 15:58:31 -0700116 ALOGD("open(), request notificationFrames = %d, frameCount = %u",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700117 notificationFrames, (uint)frameCount);
Phil Burkee995392017-12-11 11:44:36 -0800118
119 // Don't call mAudioTrack->setDeviceId() because it will be overwritten by set()!
120 audio_port_handle_t selectedDeviceId = (getDeviceId() == AAUDIO_UNSPECIFIED)
121 ? AUDIO_PORT_HANDLE_NONE
122 : getDeviceId();
123
Phil Burkd4ccc622017-12-20 15:32:44 -0800124 const audio_content_type_t contentType =
125 AAudioConvert_contentTypeToInternal(builder.getContentType());
126 const audio_usage_t usage =
127 AAudioConvert_usageToInternal(builder.getUsage());
128
129 const audio_attributes_t attributes = {
130 .content_type = contentType,
131 .usage = usage,
132 .source = AUDIO_SOURCE_DEFAULT, // only used for recording
133 .flags = flags, // If attributes are set then the other flags parameter is ignored.
134 .tags = ""
135 };
136
Phil Burkee995392017-12-11 11:44:36 -0800137 mAudioTrack = new AudioTrack();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700138 mAudioTrack->set(
Phil Burkd4ccc622017-12-20 15:32:44 -0800139 AUDIO_STREAM_DEFAULT, // ignored because we pass attributes below
Phil Burke1ce4912016-11-21 10:40:25 -0800140 getSampleRate(),
141 format,
142 channelMask,
143 frameCount,
144 flags,
145 callback,
Phil Burke4d7bb42017-03-28 11:32:39 -0700146 callbackData,
147 notificationFrames,
Phil Burkee995392017-12-11 11:44:36 -0800148 0, // DEFAULT sharedBuffer*/,
149 false, // DEFAULT threadCanCallJava
Phil Burke1ce4912016-11-21 10:40:25 -0800150 AUDIO_SESSION_ALLOCATE,
Phil Burkee995392017-12-11 11:44:36 -0800151 streamTransferType,
152 NULL, // DEFAULT audio_offload_info_t
153 AUDIO_UID_INVALID, // DEFAULT uid
154 -1, // DEFAULT pid
Phil Burkd4ccc622017-12-20 15:32:44 -0800155 &attributes,
Phil Burkee995392017-12-11 11:44:36 -0800156 // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging
157 // headphones a few times.
158 false, // DEFAULT doNotReconnect,
159 1.0f, // DEFAULT maxRequiredSpeed
160 selectedDeviceId
161 );
Phil Burke1ce4912016-11-21 10:40:25 -0800162
163 // Did we get a valid track?
164 status_t status = mAudioTrack->initCheck();
Phil Burkdec33ab2017-01-17 14:48:16 -0800165 if (status != NO_ERROR) {
Phil Burke1ce4912016-11-21 10:40:25 -0800166 close();
Phil Burkfbf031e2017-10-12 15:58:31 -0700167 ALOGE("open(), initCheck() returned %d", status);
Phil Burk5ed503c2017-02-01 09:38:15 -0800168 return AAudioConvert_androidToAAudioResult(status);
Phil Burke1ce4912016-11-21 10:40:25 -0800169 }
170
Phil Burk965650e2017-09-07 21:00:09 -0700171 doSetVolume();
Eric Laurent1d32e9f2017-06-02 14:01:32 -0700172
Phil Burke1ce4912016-11-21 10:40:25 -0800173 // Get the actual values from the AudioTrack.
174 setSamplesPerFrame(mAudioTrack->channelCount());
Phil Burk9dca9822017-05-26 14:27:43 -0700175 aaudio_format_t aaudioFormat =
Phil Burke4d7bb42017-03-28 11:32:39 -0700176 AAudioConvert_androidToAAudioDataFormat(mAudioTrack->format());
177 setFormat(aaudioFormat);
178
Phil Burkcf5f6d22017-05-26 12:35:07 -0700179 int32_t actualSampleRate = mAudioTrack->getSampleRate();
180 ALOGW_IF(actualSampleRate != getSampleRate(),
Phil Burkfbf031e2017-10-12 15:58:31 -0700181 "open() sampleRate changed from %d to %d",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700182 getSampleRate(), actualSampleRate);
183 setSampleRate(actualSampleRate);
184
Phil Burke4d7bb42017-03-28 11:32:39 -0700185 // We may need to pass the data through a block size adapter to guarantee constant size.
186 if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) {
187 int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize;
188 mFixedBlockReader.open(callbackSizeBytes);
189 mBlockAdapter = &mFixedBlockReader;
190 } else {
191 mBlockAdapter = nullptr;
192 }
Phil Burke1ce4912016-11-21 10:40:25 -0800193
Phil Burk5ed503c2017-02-01 09:38:15 -0800194 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800195 setDeviceId(mAudioTrack->getRoutedDeviceId());
Eric Laurentfb00fc72017-05-25 18:17:12 -0700196 mAudioTrack->addAudioDeviceCallback(mDeviceCallback);
Phil Burke1ce4912016-11-21 10:40:25 -0800197
Phil Burk09c27ca2017-08-24 22:12:56 -0700198 // Update performance mode based on the actual stream flags.
Phil Burk30a70772017-05-16 11:20:36 -0700199 // For example, if the sample rate is not allowed then you won't get a FAST track.
200 audio_output_flags_t actualFlags = mAudioTrack->getFlags();
201 aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
Phil Burk09c27ca2017-08-24 22:12:56 -0700202 // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY.
203 if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) {
Phil Burk30a70772017-05-16 11:20:36 -0700204 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
Phil Burk30a70772017-05-16 11:20:36 -0700205 } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
206 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
207 }
208 setPerformanceMode(actualPerformanceMode);
Phil Burkefa56002017-08-02 15:07:21 -0700209
210 setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy
211
Phil Burk30a70772017-05-16 11:20:36 -0700212 // Log warning if we did not get what we asked for.
213 ALOGW_IF(actualFlags != flags,
Phil Burkfbf031e2017-10-12 15:58:31 -0700214 "open() flags changed from 0x%08X to 0x%08X",
Phil Burk30a70772017-05-16 11:20:36 -0700215 flags, actualFlags);
216 ALOGW_IF(actualPerformanceMode != perfMode,
Phil Burkfbf031e2017-10-12 15:58:31 -0700217 "open() perfMode changed from %d to %d",
Phil Burk30a70772017-05-16 11:20:36 -0700218 perfMode, actualPerformanceMode);
219
Phil Burk5ed503c2017-02-01 09:38:15 -0800220 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800221}
222
Phil Burk5ed503c2017-02-01 09:38:15 -0800223aaudio_result_t AudioStreamTrack::close()
Phil Burke1ce4912016-11-21 10:40:25 -0800224{
Phil Burk5ed503c2017-02-01 09:38:15 -0800225 if (getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk965650e2017-09-07 21:00:09 -0700226 mAudioTrack->removeAudioDeviceCallback(mDeviceCallback);
Phil Burk5ed503c2017-02-01 09:38:15 -0800227 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800228 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700229 mFixedBlockReader.close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800230 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800231}
232
Phil Burke4d7bb42017-03-28 11:32:39 -0700233void AudioStreamTrack::processCallback(int event, void *info) {
234
235 switch (event) {
236 case AudioTrack::EVENT_MORE_DATA:
237 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info);
238 break;
239
240 // Stream got rerouted so we disconnect.
241 case AudioTrack::EVENT_NEW_IAUDIOTRACK:
242 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info);
243 break;
244
245 default:
246 break;
247 }
248 return;
249}
250
Phil Burk965650e2017-09-07 21:00:09 -0700251aaudio_result_t AudioStreamTrack::requestStart() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800252 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700253 ALOGE("requestStart() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800254 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800255 }
256 // Get current position so we can detect when the track is playing.
257 status_t err = mAudioTrack->getPosition(&mPositionWhenStarting);
258 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800259 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800260 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700261
Phil Burk9e9a95b2018-01-18 12:14:15 -0800262 // Enable callback before starting AudioTrack to avoid shutting
263 // down because of a race condition.
264 mCallbackEnabled.store(true);
Phil Burk965650e2017-09-07 21:00:09 -0700265 err = mAudioTrack->start();
Phil Burke1ce4912016-11-21 10:40:25 -0800266 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800267 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800268 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800269 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burke1ce4912016-11-21 10:40:25 -0800270 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800271 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800272}
273
Phil Burk965650e2017-09-07 21:00:09 -0700274aaudio_result_t AudioStreamTrack::requestPause() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800275 if (mAudioTrack.get() == nullptr) {
Phil Burk965650e2017-09-07 21:00:09 -0700276 ALOGE("requestPause() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800277 return AAUDIO_ERROR_INVALID_STATE;
278 } else if (getState() != AAUDIO_STREAM_STATE_STARTING
279 && getState() != AAUDIO_STREAM_STATE_STARTED) {
Phil Burk134f1972017-12-08 13:06:11 -0800280 // TODO What about DISCONNECTED?
Phil Burke4d7bb42017-03-28 11:32:39 -0700281 ALOGE("requestPause(), called when state is %s",
282 AAudio_convertStreamStateToText(getState()));
Phil Burk5ed503c2017-02-01 09:38:15 -0800283 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800284 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800285 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burk965650e2017-09-07 21:00:09 -0700286 mAudioTrack->pause();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800287 mCallbackEnabled.store(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800288 status_t err = mAudioTrack->getPosition(&mPositionWhenPausing);
289 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800290 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800291 }
Phil Burk134f1972017-12-08 13:06:11 -0800292 return checkForDisconnectRequest(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800293}
294
Phil Burk5ed503c2017-02-01 09:38:15 -0800295aaudio_result_t AudioStreamTrack::requestFlush() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800296 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700297 ALOGE("requestFlush() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800298 return AAUDIO_ERROR_INVALID_STATE;
299 } else if (getState() != AAUDIO_STREAM_STATE_PAUSED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700300 ALOGE("requestFlush() not paused");
Phil Burk5ed503c2017-02-01 09:38:15 -0800301 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800302 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800303 setState(AAUDIO_STREAM_STATE_FLUSHING);
Phil Burke1ce4912016-11-21 10:40:25 -0800304 incrementFramesRead(getFramesWritten() - getFramesRead());
305 mAudioTrack->flush();
306 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700307 mTimestampPosition.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800308 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800309}
310
Phil Burk5ed503c2017-02-01 09:38:15 -0800311aaudio_result_t AudioStreamTrack::requestStop() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800312 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700313 ALOGE("requestStop() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800314 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800315 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800316 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burke1ce4912016-11-21 10:40:25 -0800317 incrementFramesRead(getFramesWritten() - getFramesRead()); // TODO review
Phil Burk7328a802017-08-30 09:29:48 -0700318 mTimestampPosition.set(getFramesWritten());
Phil Burke1ce4912016-11-21 10:40:25 -0800319 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700320 mTimestampPosition.reset32();
Phil Burk965650e2017-09-07 21:00:09 -0700321 mAudioTrack->stop();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800322 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800323 return checkForDisconnectRequest(false);;
Phil Burke1ce4912016-11-21 10:40:25 -0800324}
325
Phil Burk0befec62017-07-28 15:12:13 -0700326aaudio_result_t AudioStreamTrack::updateStateMachine()
Phil Burke1ce4912016-11-21 10:40:25 -0800327{
328 status_t err;
Phil Burk5ed503c2017-02-01 09:38:15 -0800329 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800330 switch (getState()) {
331 // TODO add better state visibility to AudioTrack
Phil Burk5ed503c2017-02-01 09:38:15 -0800332 case AAUDIO_STREAM_STATE_STARTING:
Phil Burke1ce4912016-11-21 10:40:25 -0800333 if (mAudioTrack->hasStarted()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800334 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burke1ce4912016-11-21 10:40:25 -0800335 }
336 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800337 case AAUDIO_STREAM_STATE_PAUSING:
Phil Burke1ce4912016-11-21 10:40:25 -0800338 if (mAudioTrack->stopped()) {
339 err = mAudioTrack->getPosition(&position);
340 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800341 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800342 } else if (position == mPositionWhenPausing) {
343 // Has stream really stopped advancing?
Phil Burk5ed503c2017-02-01 09:38:15 -0800344 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800345 }
346 mPositionWhenPausing = position;
347 }
348 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800349 case AAUDIO_STREAM_STATE_FLUSHING:
Phil Burke1ce4912016-11-21 10:40:25 -0800350 {
351 err = mAudioTrack->getPosition(&position);
352 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800353 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800354 } else if (position == 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700355 // TODO Advance frames read to match written.
Phil Burk5ed503c2017-02-01 09:38:15 -0800356 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burke1ce4912016-11-21 10:40:25 -0800357 }
358 }
359 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800360 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burke1ce4912016-11-21 10:40:25 -0800361 if (mAudioTrack->stopped()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800362 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burke1ce4912016-11-21 10:40:25 -0800363 }
364 break;
365 default:
366 break;
367 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800368 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800369}
370
Phil Burk5ed503c2017-02-01 09:38:15 -0800371aaudio_result_t AudioStreamTrack::write(const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800372 int32_t numFrames,
373 int64_t timeoutNanoseconds)
Phil Burke1ce4912016-11-21 10:40:25 -0800374{
Phil Burk3316d5e2017-02-15 11:23:01 -0800375 int32_t bytesPerFrame = getBytesPerFrame();
376 int32_t numBytes;
Phil Burk5ed503c2017-02-01 09:38:15 -0800377 aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes);
378 if (result != AAUDIO_OK) {
Phil Burke1ce4912016-11-21 10:40:25 -0800379 return result;
380 }
381
Eric Laurentfb00fc72017-05-25 18:17:12 -0700382 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
383 return AAUDIO_ERROR_DISCONNECTED;
384 }
385
Phil Burke1ce4912016-11-21 10:40:25 -0800386 // TODO add timeout to AudioTrack
387 bool blocking = timeoutNanoseconds > 0;
388 ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking);
389 if (bytesWritten == WOULD_BLOCK) {
390 return 0;
391 } else if (bytesWritten < 0) {
392 ALOGE("invalid write, returned %d", (int)bytesWritten);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700393 // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to
394 // AudioTrack invalidation
395 if (bytesWritten == DEAD_OBJECT) {
396 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
397 return AAUDIO_ERROR_DISCONNECTED;
398 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800399 return AAudioConvert_androidToAAudioResult(bytesWritten);
Phil Burke1ce4912016-11-21 10:40:25 -0800400 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800401 int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -0800402 incrementFramesWritten(framesWritten);
Phil Burk0befec62017-07-28 15:12:13 -0700403
404 result = updateStateMachine();
405 if (result != AAUDIO_OK) {
406 return result;
407 }
408
Phil Burke1ce4912016-11-21 10:40:25 -0800409 return framesWritten;
410}
411
Phil Burk3316d5e2017-02-15 11:23:01 -0800412aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames)
Phil Burke1ce4912016-11-21 10:40:25 -0800413{
414 ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800415 if (result < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800416 return AAudioConvert_androidToAAudioResult(result);
Phil Burke1ce4912016-11-21 10:40:25 -0800417 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800418 return result;
Phil Burke1ce4912016-11-21 10:40:25 -0800419 }
420}
421
Phil Burk3316d5e2017-02-15 11:23:01 -0800422int32_t AudioStreamTrack::getBufferSize() const
Phil Burke1ce4912016-11-21 10:40:25 -0800423{
Phil Burk3316d5e2017-02-15 11:23:01 -0800424 return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800425}
426
Phil Burk3316d5e2017-02-15 11:23:01 -0800427int32_t AudioStreamTrack::getBufferCapacity() const
Phil Burke1ce4912016-11-21 10:40:25 -0800428{
Phil Burk3316d5e2017-02-15 11:23:01 -0800429 return static_cast<int32_t>(mAudioTrack->frameCount());
Phil Burke1ce4912016-11-21 10:40:25 -0800430}
431
432int32_t AudioStreamTrack::getXRunCount() const
433{
434 return static_cast<int32_t>(mAudioTrack->getUnderrunCount());
435}
436
437int32_t AudioStreamTrack::getFramesPerBurst() const
438{
Phil Burkb5884022017-03-27 15:26:14 -0700439 return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800440}
441
Phil Burk3316d5e2017-02-15 11:23:01 -0800442int64_t AudioStreamTrack::getFramesRead() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800443 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800444 status_t result;
445 switch (getState()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800446 case AAUDIO_STREAM_STATE_STARTING:
447 case AAUDIO_STREAM_STATE_STARTED:
448 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burk4c5129b2017-04-28 15:17:32 -0700449 case AAUDIO_STREAM_STATE_PAUSING:
450 case AAUDIO_STREAM_STATE_PAUSED:
Phil Burke1ce4912016-11-21 10:40:25 -0800451 result = mAudioTrack->getPosition(&position);
452 if (result == OK) {
453 mFramesRead.update32(position);
454 }
455 break;
456 default:
457 break;
458 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700459 return AudioStreamLegacy::getFramesRead();
Phil Burke1ce4912016-11-21 10:40:25 -0800460}
Phil Burk35e80f32017-03-28 10:25:21 -0700461
462aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId,
463 int64_t *framePosition,
464 int64_t *timeNanoseconds) {
465 ExtendedTimestamp extendedTimestamp;
466 status_t status = mAudioTrack->getTimestamp(&extendedTimestamp);
Phil Burkc75d97f2017-09-08 15:48:36 -0700467 if (status == WOULD_BLOCK) {
468 return AAUDIO_ERROR_INVALID_STATE;
469 } if (status != NO_ERROR) {
Phil Burk35e80f32017-03-28 10:25:21 -0700470 return AAudioConvert_androidToAAudioResult(status);
471 }
Phil Burk7328a802017-08-30 09:29:48 -0700472 int64_t position = 0;
473 int64_t nanoseconds = 0;
474 aaudio_result_t result = getBestTimestamp(clockId, &position,
475 &nanoseconds, &extendedTimestamp);
476 if (result == AAUDIO_OK) {
477 if (position < getFramesWritten()) {
478 *framePosition = position;
479 *timeNanoseconds = nanoseconds;
480 return result;
481 } else {
482 return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent
483 }
484 }
485 return result;
Phil Burk35e80f32017-03-28 10:25:21 -0700486}
Phil Burk965650e2017-09-07 21:00:09 -0700487
488status_t AudioStreamTrack::doSetVolume() {
489 status_t status = NO_INIT;
490 if (mAudioTrack.get() != nullptr) {
491 float volume = getDuckAndMuteVolume();
492 mAudioTrack->setVolume(volume, volume);
493 status = NO_ERROR;
494 }
495 return status;
496}
497
498#if AAUDIO_USE_VOLUME_SHAPER
Phil Burk8a8a9e52017-09-12 16:25:15 -0700499
500using namespace android::media::VolumeShaper;
501
Phil Burk965650e2017-09-07 21:00:09 -0700502binder::Status AudioStreamTrack::applyVolumeShaper(
503 const VolumeShaper::Configuration& configuration,
504 const VolumeShaper::Operation& operation) {
505
506 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
507 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
508
509 if (mAudioTrack.get() != nullptr) {
510 ALOGD("applyVolumeShaper() from IPlayer");
Phil Burk8a8a9e52017-09-12 16:25:15 -0700511 binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Phil Burk965650e2017-09-07 21:00:09 -0700512 if (status < 0) { // a non-negative value is the volume shaper id.
513 ALOGE("applyVolumeShaper() failed with status %d", status);
514 }
515 return binder::Status::fromStatusT(status);
516 } else {
517 ALOGD("applyVolumeShaper()"
518 " no AudioTrack for volume control from IPlayer");
519 return binder::Status::ok();
520 }
521}
522#endif