blob: 17a8d5262081fbbf8976b35fda461a9adfbf0010 [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 Burk965650e2017-09-07 21:00:09 -0700262 err = mAudioTrack->start();
Phil Burke1ce4912016-11-21 10:40:25 -0800263 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800264 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800265 } else {
Eric Laurentfb00fc72017-05-25 18:17:12 -0700266 onStart();
Phil Burk5ed503c2017-02-01 09:38:15 -0800267 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burke1ce4912016-11-21 10:40:25 -0800268 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800269 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800270}
271
Phil Burk965650e2017-09-07 21:00:09 -0700272aaudio_result_t AudioStreamTrack::requestPause() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800273 if (mAudioTrack.get() == nullptr) {
Phil Burk965650e2017-09-07 21:00:09 -0700274 ALOGE("requestPause() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800275 return AAUDIO_ERROR_INVALID_STATE;
276 } else if (getState() != AAUDIO_STREAM_STATE_STARTING
277 && getState() != AAUDIO_STREAM_STATE_STARTED) {
Phil Burk134f1972017-12-08 13:06:11 -0800278 // TODO What about DISCONNECTED?
Phil Burke4d7bb42017-03-28 11:32:39 -0700279 ALOGE("requestPause(), called when state is %s",
280 AAudio_convertStreamStateToText(getState()));
Phil Burk5ed503c2017-02-01 09:38:15 -0800281 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800282 }
Eric Laurentfb00fc72017-05-25 18:17:12 -0700283 onStop();
Phil Burk5ed503c2017-02-01 09:38:15 -0800284 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burk965650e2017-09-07 21:00:09 -0700285 mAudioTrack->pause();
Phil Burke1ce4912016-11-21 10:40:25 -0800286 status_t err = mAudioTrack->getPosition(&mPositionWhenPausing);
287 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800288 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800289 }
Phil Burk134f1972017-12-08 13:06:11 -0800290 return checkForDisconnectRequest(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800291}
292
Phil Burk5ed503c2017-02-01 09:38:15 -0800293aaudio_result_t AudioStreamTrack::requestFlush() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800294 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700295 ALOGE("requestFlush() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800296 return AAUDIO_ERROR_INVALID_STATE;
297 } else if (getState() != AAUDIO_STREAM_STATE_PAUSED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700298 ALOGE("requestFlush() not paused");
Phil Burk5ed503c2017-02-01 09:38:15 -0800299 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800300 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800301 setState(AAUDIO_STREAM_STATE_FLUSHING);
Phil Burke1ce4912016-11-21 10:40:25 -0800302 incrementFramesRead(getFramesWritten() - getFramesRead());
303 mAudioTrack->flush();
304 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700305 mTimestampPosition.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800306 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800307}
308
Phil Burk5ed503c2017-02-01 09:38:15 -0800309aaudio_result_t AudioStreamTrack::requestStop() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800310 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700311 ALOGE("requestStop() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800312 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800313 }
Eric Laurentfb00fc72017-05-25 18:17:12 -0700314 onStop();
Phil Burk5ed503c2017-02-01 09:38:15 -0800315 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burke1ce4912016-11-21 10:40:25 -0800316 incrementFramesRead(getFramesWritten() - getFramesRead()); // TODO review
Phil Burk7328a802017-08-30 09:29:48 -0700317 mTimestampPosition.set(getFramesWritten());
Phil Burke1ce4912016-11-21 10:40:25 -0800318 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700319 mTimestampPosition.reset32();
Phil Burk965650e2017-09-07 21:00:09 -0700320 mAudioTrack->stop();
Phil Burk134f1972017-12-08 13:06:11 -0800321 return checkForDisconnectRequest(false);;
Phil Burke1ce4912016-11-21 10:40:25 -0800322}
323
Phil Burk0befec62017-07-28 15:12:13 -0700324aaudio_result_t AudioStreamTrack::updateStateMachine()
Phil Burke1ce4912016-11-21 10:40:25 -0800325{
326 status_t err;
Phil Burk5ed503c2017-02-01 09:38:15 -0800327 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800328 switch (getState()) {
329 // TODO add better state visibility to AudioTrack
Phil Burk5ed503c2017-02-01 09:38:15 -0800330 case AAUDIO_STREAM_STATE_STARTING:
Phil Burke1ce4912016-11-21 10:40:25 -0800331 if (mAudioTrack->hasStarted()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800332 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burke1ce4912016-11-21 10:40:25 -0800333 }
334 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800335 case AAUDIO_STREAM_STATE_PAUSING:
Phil Burke1ce4912016-11-21 10:40:25 -0800336 if (mAudioTrack->stopped()) {
337 err = mAudioTrack->getPosition(&position);
338 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800339 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800340 } else if (position == mPositionWhenPausing) {
341 // Has stream really stopped advancing?
Phil Burk5ed503c2017-02-01 09:38:15 -0800342 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800343 }
344 mPositionWhenPausing = position;
345 }
346 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800347 case AAUDIO_STREAM_STATE_FLUSHING:
Phil Burke1ce4912016-11-21 10:40:25 -0800348 {
349 err = mAudioTrack->getPosition(&position);
350 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800351 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800352 } else if (position == 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700353 // TODO Advance frames read to match written.
Phil Burk5ed503c2017-02-01 09:38:15 -0800354 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burke1ce4912016-11-21 10:40:25 -0800355 }
356 }
357 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800358 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burke1ce4912016-11-21 10:40:25 -0800359 if (mAudioTrack->stopped()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800360 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burke1ce4912016-11-21 10:40:25 -0800361 }
362 break;
363 default:
364 break;
365 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800366 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800367}
368
Phil Burk5ed503c2017-02-01 09:38:15 -0800369aaudio_result_t AudioStreamTrack::write(const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800370 int32_t numFrames,
371 int64_t timeoutNanoseconds)
Phil Burke1ce4912016-11-21 10:40:25 -0800372{
Phil Burk3316d5e2017-02-15 11:23:01 -0800373 int32_t bytesPerFrame = getBytesPerFrame();
374 int32_t numBytes;
Phil Burk5ed503c2017-02-01 09:38:15 -0800375 aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes);
376 if (result != AAUDIO_OK) {
Phil Burke1ce4912016-11-21 10:40:25 -0800377 return result;
378 }
379
Eric Laurentfb00fc72017-05-25 18:17:12 -0700380 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
381 return AAUDIO_ERROR_DISCONNECTED;
382 }
383
Phil Burke1ce4912016-11-21 10:40:25 -0800384 // TODO add timeout to AudioTrack
385 bool blocking = timeoutNanoseconds > 0;
386 ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking);
387 if (bytesWritten == WOULD_BLOCK) {
388 return 0;
389 } else if (bytesWritten < 0) {
390 ALOGE("invalid write, returned %d", (int)bytesWritten);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700391 // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to
392 // AudioTrack invalidation
393 if (bytesWritten == DEAD_OBJECT) {
394 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
395 return AAUDIO_ERROR_DISCONNECTED;
396 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800397 return AAudioConvert_androidToAAudioResult(bytesWritten);
Phil Burke1ce4912016-11-21 10:40:25 -0800398 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800399 int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -0800400 incrementFramesWritten(framesWritten);
Phil Burk0befec62017-07-28 15:12:13 -0700401
402 result = updateStateMachine();
403 if (result != AAUDIO_OK) {
404 return result;
405 }
406
Phil Burke1ce4912016-11-21 10:40:25 -0800407 return framesWritten;
408}
409
Phil Burk3316d5e2017-02-15 11:23:01 -0800410aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames)
Phil Burke1ce4912016-11-21 10:40:25 -0800411{
412 ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800413 if (result < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800414 return AAudioConvert_androidToAAudioResult(result);
Phil Burke1ce4912016-11-21 10:40:25 -0800415 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800416 return result;
Phil Burke1ce4912016-11-21 10:40:25 -0800417 }
418}
419
Phil Burk3316d5e2017-02-15 11:23:01 -0800420int32_t AudioStreamTrack::getBufferSize() const
Phil Burke1ce4912016-11-21 10:40:25 -0800421{
Phil Burk3316d5e2017-02-15 11:23:01 -0800422 return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800423}
424
Phil Burk3316d5e2017-02-15 11:23:01 -0800425int32_t AudioStreamTrack::getBufferCapacity() const
Phil Burke1ce4912016-11-21 10:40:25 -0800426{
Phil Burk3316d5e2017-02-15 11:23:01 -0800427 return static_cast<int32_t>(mAudioTrack->frameCount());
Phil Burke1ce4912016-11-21 10:40:25 -0800428}
429
430int32_t AudioStreamTrack::getXRunCount() const
431{
432 return static_cast<int32_t>(mAudioTrack->getUnderrunCount());
433}
434
435int32_t AudioStreamTrack::getFramesPerBurst() const
436{
Phil Burkb5884022017-03-27 15:26:14 -0700437 return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800438}
439
Phil Burk3316d5e2017-02-15 11:23:01 -0800440int64_t AudioStreamTrack::getFramesRead() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800441 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800442 status_t result;
443 switch (getState()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800444 case AAUDIO_STREAM_STATE_STARTING:
445 case AAUDIO_STREAM_STATE_STARTED:
446 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burk4c5129b2017-04-28 15:17:32 -0700447 case AAUDIO_STREAM_STATE_PAUSING:
448 case AAUDIO_STREAM_STATE_PAUSED:
Phil Burke1ce4912016-11-21 10:40:25 -0800449 result = mAudioTrack->getPosition(&position);
450 if (result == OK) {
451 mFramesRead.update32(position);
452 }
453 break;
454 default:
455 break;
456 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700457 return AudioStreamLegacy::getFramesRead();
Phil Burke1ce4912016-11-21 10:40:25 -0800458}
Phil Burk35e80f32017-03-28 10:25:21 -0700459
460aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId,
461 int64_t *framePosition,
462 int64_t *timeNanoseconds) {
463 ExtendedTimestamp extendedTimestamp;
464 status_t status = mAudioTrack->getTimestamp(&extendedTimestamp);
Phil Burkc75d97f2017-09-08 15:48:36 -0700465 if (status == WOULD_BLOCK) {
466 return AAUDIO_ERROR_INVALID_STATE;
467 } if (status != NO_ERROR) {
Phil Burk35e80f32017-03-28 10:25:21 -0700468 return AAudioConvert_androidToAAudioResult(status);
469 }
Phil Burk7328a802017-08-30 09:29:48 -0700470 int64_t position = 0;
471 int64_t nanoseconds = 0;
472 aaudio_result_t result = getBestTimestamp(clockId, &position,
473 &nanoseconds, &extendedTimestamp);
474 if (result == AAUDIO_OK) {
475 if (position < getFramesWritten()) {
476 *framePosition = position;
477 *timeNanoseconds = nanoseconds;
478 return result;
479 } else {
480 return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent
481 }
482 }
483 return result;
Phil Burk35e80f32017-03-28 10:25:21 -0700484}
Phil Burk965650e2017-09-07 21:00:09 -0700485
486status_t AudioStreamTrack::doSetVolume() {
487 status_t status = NO_INIT;
488 if (mAudioTrack.get() != nullptr) {
489 float volume = getDuckAndMuteVolume();
490 mAudioTrack->setVolume(volume, volume);
491 status = NO_ERROR;
492 }
493 return status;
494}
495
496#if AAUDIO_USE_VOLUME_SHAPER
Phil Burk8a8a9e52017-09-12 16:25:15 -0700497
498using namespace android::media::VolumeShaper;
499
Phil Burk965650e2017-09-07 21:00:09 -0700500binder::Status AudioStreamTrack::applyVolumeShaper(
501 const VolumeShaper::Configuration& configuration,
502 const VolumeShaper::Operation& operation) {
503
504 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
505 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
506
507 if (mAudioTrack.get() != nullptr) {
508 ALOGD("applyVolumeShaper() from IPlayer");
Phil Burk8a8a9e52017-09-12 16:25:15 -0700509 binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Phil Burk965650e2017-09-07 21:00:09 -0700510 if (status < 0) { // a non-negative value is the volume shaper id.
511 ALOGE("applyVolumeShaper() failed with status %d", status);
512 }
513 return binder::Status::fromStatusT(status);
514 } else {
515 ALOGD("applyVolumeShaper()"
516 " no AudioTrack for volume control from IPlayer");
517 return binder::Status::ok();
518 }
519}
520#endif