blob: 00963d624a6786722c0d2fb0f45d7ca06c4fed39 [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 Burked816012018-02-06 12:40:50 -080025#include <system/audio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080026#include "utility/AudioClock.h"
Phil Burke4d7bb42017-03-28 11:32:39 -070027#include "legacy/AudioStreamLegacy.h"
28#include "legacy/AudioStreamTrack.h"
29#include "utility/FixedBlockReader.h"
Phil Burke1ce4912016-11-21 10:40:25 -080030
31using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080032using namespace aaudio;
Phil Burke1ce4912016-11-21 10:40:25 -080033
Phil Burke4d7bb42017-03-28 11:32:39 -070034// Arbitrary and somewhat generous number of bursts.
35#define DEFAULT_BURSTS_PER_BUFFER_CAPACITY 8
36
Phil Burke1ce4912016-11-21 10:40:25 -080037/*
38 * Create a stream that uses the AudioTrack.
39 */
40AudioStreamTrack::AudioStreamTrack()
Phil Burke4d7bb42017-03-28 11:32:39 -070041 : AudioStreamLegacy()
42 , mFixedBlockReader(*this)
Phil Burke1ce4912016-11-21 10:40:25 -080043{
44}
45
46AudioStreamTrack::~AudioStreamTrack()
47{
Phil Burk5ed503c2017-02-01 09:38:15 -080048 const aaudio_stream_state_t state = getState();
49 bool bad = !(state == AAUDIO_STREAM_STATE_UNINITIALIZED || state == AAUDIO_STREAM_STATE_CLOSED);
Phil Burke1ce4912016-11-21 10:40:25 -080050 ALOGE_IF(bad, "stream not closed, in state %d", state);
51}
52
Phil Burk5ed503c2017-02-01 09:38:15 -080053aaudio_result_t AudioStreamTrack::open(const AudioStreamBuilder& builder)
Phil Burke1ce4912016-11-21 10:40:25 -080054{
Phil Burk5ed503c2017-02-01 09:38:15 -080055 aaudio_result_t result = AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -080056
57 result = AudioStream::open(builder);
58 if (result != OK) {
59 return result;
60 }
61
Phil Burk8ffcf612018-05-23 14:38:07 -070062 const aaudio_session_id_t requestedSessionId = builder.getSessionId();
63 const audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
64
Phil Burke1ce4912016-11-21 10:40:25 -080065 // Try to create an AudioTrack
Phil Burk5204d312017-05-04 17:16:13 -070066 // Use stereo if unspecified.
Phil Burk5ed503c2017-02-01 09:38:15 -080067 int32_t samplesPerFrame = (getSamplesPerFrame() == AAUDIO_UNSPECIFIED)
Phil Burke1ce4912016-11-21 10:40:25 -080068 ? 2 : getSamplesPerFrame();
Eric Laurent16c66dd2019-05-01 17:54:10 -070069 audio_channel_mask_t channelMask = samplesPerFrame <= 2 ?
70 audio_channel_out_mask_from_count(samplesPerFrame) :
71 audio_channel_mask_for_index_assignment_from_count(samplesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -080072
Phil Burk8ffcf612018-05-23 14:38:07 -070073 audio_output_flags_t flags;
Phil Burk30a70772017-05-16 11:20:36 -070074 aaudio_performance_mode_t perfMode = getPerformanceMode();
75 switch(perfMode) {
Phil Burke2fbb592017-05-01 15:05:52 -070076 case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY:
77 // Bypass the normal mixer and go straight to the FAST mixer.
Phil Burk8ffcf612018-05-23 14:38:07 -070078 // If the app asks for a sessionId then it means they want to use effects.
79 // So don't use RAW flag.
80 flags = (audio_output_flags_t) ((requestedSessionId == AAUDIO_SESSION_ID_NONE)
81 ? (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW)
82 : (AUDIO_OUTPUT_FLAG_FAST));
Phil Burke2fbb592017-05-01 15:05:52 -070083 break;
84
85 case AAUDIO_PERFORMANCE_MODE_POWER_SAVING:
86 // This uses a mixer that wakes up less often than the FAST mixer.
87 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
88 break;
89
90 case AAUDIO_PERFORMANCE_MODE_NONE:
91 default:
92 // No flags. Use a normal mixer in front of the FAST mixer.
Phil Burk8ffcf612018-05-23 14:38:07 -070093 flags = AUDIO_OUTPUT_FLAG_NONE;
Phil Burke2fbb592017-05-01 15:05:52 -070094 break;
95 }
Phil Burke4d7bb42017-03-28 11:32:39 -070096
Phil Burkcf5f6d22017-05-26 12:35:07 -070097 size_t frameCount = (size_t)builder.getBufferCapacity();
Phil Burke4d7bb42017-03-28 11:32:39 -070098
99 int32_t notificationFrames = 0;
100
Phil Burk0127c1b2018-03-29 13:48:06 -0700101 const audio_format_t format = (getFormat() == AUDIO_FORMAT_DEFAULT)
Phil Burke1ce4912016-11-21 10:40:25 -0800102 ? AUDIO_FORMAT_PCM_FLOAT
Phil Burk0127c1b2018-03-29 13:48:06 -0700103 : getFormat();
Phil Burke1ce4912016-11-21 10:40:25 -0800104
Phil Burke4d7bb42017-03-28 11:32:39 -0700105 // Setup the callback if there is one.
106 AudioTrack::callback_t callback = nullptr;
107 void *callbackData = nullptr;
108 // Note that TRANSFER_SYNC does not allow FAST track
109 AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC;
110 if (builder.getDataCallbackProc() != nullptr) {
111 streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK;
112 callback = getLegacyCallback();
113 callbackData = this;
114
Phil Burke4d7bb42017-03-28 11:32:39 -0700115 // If the total buffer size is unspecified then base the size on the burst size.
Phil Burk4485d412017-05-09 15:55:02 -0700116 if (frameCount == 0
117 && ((flags & AUDIO_OUTPUT_FLAG_FAST) != 0)) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700118 // Take advantage of a special trick that allows us to create a buffer
119 // that is some multiple of the burst size.
120 notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY;
Phil Burk4485d412017-05-09 15:55:02 -0700121 } else {
122 notificationFrames = builder.getFramesPerDataCallback();
Phil Burke4d7bb42017-03-28 11:32:39 -0700123 }
124 }
125 mCallbackBufferSize = builder.getFramesPerDataCallback();
126
Phil Burkfbf031e2017-10-12 15:58:31 -0700127 ALOGD("open(), request notificationFrames = %d, frameCount = %u",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700128 notificationFrames, (uint)frameCount);
Phil Burkee995392017-12-11 11:44:36 -0800129
130 // Don't call mAudioTrack->setDeviceId() because it will be overwritten by set()!
131 audio_port_handle_t selectedDeviceId = (getDeviceId() == AAUDIO_UNSPECIFIED)
132 ? AUDIO_PORT_HANDLE_NONE
133 : getDeviceId();
134
Phil Burkd4ccc622017-12-20 15:32:44 -0800135 const audio_content_type_t contentType =
136 AAudioConvert_contentTypeToInternal(builder.getContentType());
137 const audio_usage_t usage =
138 AAudioConvert_usageToInternal(builder.getUsage());
Kevin Rocard68646ba2019-03-20 13:26:49 -0700139 const audio_flags_mask_t attributesFlags =
140 AAudioConvert_allowCapturePolicyToAudioFlagsMask(builder.getAllowedCapturePolicy());
Phil Burkd4ccc622017-12-20 15:32:44 -0800141
142 const audio_attributes_t attributes = {
143 .content_type = contentType,
144 .usage = usage,
145 .source = AUDIO_SOURCE_DEFAULT, // only used for recording
Kevin Rocard68646ba2019-03-20 13:26:49 -0700146 .flags = attributesFlags,
Phil Burkd4ccc622017-12-20 15:32:44 -0800147 .tags = ""
148 };
149
Phil Burkee995392017-12-11 11:44:36 -0800150 mAudioTrack = new AudioTrack();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700151 mAudioTrack->set(
Phil Burkd4ccc622017-12-20 15:32:44 -0800152 AUDIO_STREAM_DEFAULT, // ignored because we pass attributes below
Phil Burke1ce4912016-11-21 10:40:25 -0800153 getSampleRate(),
154 format,
155 channelMask,
156 frameCount,
157 flags,
158 callback,
Phil Burke4d7bb42017-03-28 11:32:39 -0700159 callbackData,
160 notificationFrames,
Phil Burkee995392017-12-11 11:44:36 -0800161 0, // DEFAULT sharedBuffer*/,
162 false, // DEFAULT threadCanCallJava
Phil Burk4e1af9f2018-01-03 15:54:35 -0800163 sessionId,
Phil Burkee995392017-12-11 11:44:36 -0800164 streamTransferType,
165 NULL, // DEFAULT audio_offload_info_t
166 AUDIO_UID_INVALID, // DEFAULT uid
167 -1, // DEFAULT pid
Phil Burkd4ccc622017-12-20 15:32:44 -0800168 &attributes,
Phil Burkee995392017-12-11 11:44:36 -0800169 // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging
170 // headphones a few times.
171 false, // DEFAULT doNotReconnect,
172 1.0f, // DEFAULT maxRequiredSpeed
173 selectedDeviceId
174 );
Phil Burke1ce4912016-11-21 10:40:25 -0800175
176 // Did we get a valid track?
177 status_t status = mAudioTrack->initCheck();
Phil Burkdec33ab2017-01-17 14:48:16 -0800178 if (status != NO_ERROR) {
Phil Burk8b4e05e2019-12-17 12:12:09 -0800179 releaseCloseFinal();
Phil Burkfbf031e2017-10-12 15:58:31 -0700180 ALOGE("open(), initCheck() returned %d", status);
Phil Burk5ed503c2017-02-01 09:38:15 -0800181 return AAudioConvert_androidToAAudioResult(status);
Phil Burke1ce4912016-11-21 10:40:25 -0800182 }
183
Phil Burk965650e2017-09-07 21:00:09 -0700184 doSetVolume();
Eric Laurent1d32e9f2017-06-02 14:01:32 -0700185
Phil Burke1ce4912016-11-21 10:40:25 -0800186 // Get the actual values from the AudioTrack.
187 setSamplesPerFrame(mAudioTrack->channelCount());
Phil Burk0127c1b2018-03-29 13:48:06 -0700188 setFormat(mAudioTrack->format());
189 setDeviceFormat(mAudioTrack->format());
Phil Burke4d7bb42017-03-28 11:32:39 -0700190
Phil Burkcf5f6d22017-05-26 12:35:07 -0700191 int32_t actualSampleRate = mAudioTrack->getSampleRate();
192 ALOGW_IF(actualSampleRate != getSampleRate(),
Phil Burkfbf031e2017-10-12 15:58:31 -0700193 "open() sampleRate changed from %d to %d",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700194 getSampleRate(), actualSampleRate);
195 setSampleRate(actualSampleRate);
196
Phil Burke4d7bb42017-03-28 11:32:39 -0700197 // We may need to pass the data through a block size adapter to guarantee constant size.
198 if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) {
199 int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize;
200 mFixedBlockReader.open(callbackSizeBytes);
201 mBlockAdapter = &mFixedBlockReader;
202 } else {
203 mBlockAdapter = nullptr;
204 }
Phil Burke1ce4912016-11-21 10:40:25 -0800205
Phil Burk5ed503c2017-02-01 09:38:15 -0800206 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 setDeviceId(mAudioTrack->getRoutedDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800208
209 aaudio_session_id_t actualSessionId =
210 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
211 ? AAUDIO_SESSION_ID_NONE
212 : (aaudio_session_id_t) mAudioTrack->getSessionId();
213 setSessionId(actualSessionId);
214
Eric Laurentfb00fc72017-05-25 18:17:12 -0700215 mAudioTrack->addAudioDeviceCallback(mDeviceCallback);
Phil Burke1ce4912016-11-21 10:40:25 -0800216
Phil Burk09c27ca2017-08-24 22:12:56 -0700217 // Update performance mode based on the actual stream flags.
Phil Burk30a70772017-05-16 11:20:36 -0700218 // For example, if the sample rate is not allowed then you won't get a FAST track.
219 audio_output_flags_t actualFlags = mAudioTrack->getFlags();
220 aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
Phil Burk09c27ca2017-08-24 22:12:56 -0700221 // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY.
222 if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) {
Phil Burk30a70772017-05-16 11:20:36 -0700223 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
Phil Burk30a70772017-05-16 11:20:36 -0700224 } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
225 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
226 }
227 setPerformanceMode(actualPerformanceMode);
Phil Burkefa56002017-08-02 15:07:21 -0700228
229 setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy
230
Phil Burk30a70772017-05-16 11:20:36 -0700231 // Log warning if we did not get what we asked for.
232 ALOGW_IF(actualFlags != flags,
Phil Burkfbf031e2017-10-12 15:58:31 -0700233 "open() flags changed from 0x%08X to 0x%08X",
Phil Burk30a70772017-05-16 11:20:36 -0700234 flags, actualFlags);
235 ALOGW_IF(actualPerformanceMode != perfMode,
Phil Burkfbf031e2017-10-12 15:58:31 -0700236 "open() perfMode changed from %d to %d",
Phil Burk30a70772017-05-16 11:20:36 -0700237 perfMode, actualPerformanceMode);
238
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800240}
241
Phil Burk8b4e05e2019-12-17 12:12:09 -0800242aaudio_result_t AudioStreamTrack::release_l() {
243 if (getState() != AAUDIO_STREAM_STATE_CLOSING) {
Phil Burk965650e2017-09-07 21:00:09 -0700244 mAudioTrack->removeAudioDeviceCallback(mDeviceCallback);
Phil Burk8b4e05e2019-12-17 12:12:09 -0800245 // TODO Investigate why clear() causes a hang in test_various.cpp
246 // if I call close() from a data callback.
247 // But the same thing in AudioRecord is OK!
248 // mAudioTrack.clear();
249 mFixedBlockReader.close();
250 return AudioStream::release_l();
251 } else {
252 return AAUDIO_OK; // already released
Phil Burke1ce4912016-11-21 10:40:25 -0800253 }
Phil Burke1ce4912016-11-21 10:40:25 -0800254}
255
Phil Burke4d7bb42017-03-28 11:32:39 -0700256void AudioStreamTrack::processCallback(int event, void *info) {
257
258 switch (event) {
259 case AudioTrack::EVENT_MORE_DATA:
260 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info);
261 break;
262
263 // Stream got rerouted so we disconnect.
264 case AudioTrack::EVENT_NEW_IAUDIOTRACK:
265 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info);
266 break;
267
268 default:
269 break;
270 }
271 return;
272}
273
Phil Burk965650e2017-09-07 21:00:09 -0700274aaudio_result_t AudioStreamTrack::requestStart() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800275 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700276 ALOGE("requestStart() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800277 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800278 }
279 // Get current position so we can detect when the track is playing.
280 status_t err = mAudioTrack->getPosition(&mPositionWhenStarting);
281 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800282 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800283 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700284
Phil Burk9e9a95b2018-01-18 12:14:15 -0800285 // Enable callback before starting AudioTrack to avoid shutting
286 // down because of a race condition.
287 mCallbackEnabled.store(true);
Phil Burk965650e2017-09-07 21:00:09 -0700288 err = mAudioTrack->start();
Phil Burke1ce4912016-11-21 10:40:25 -0800289 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800290 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800291 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800292 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burke1ce4912016-11-21 10:40:25 -0800293 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800294 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800295}
296
Phil Burk965650e2017-09-07 21:00:09 -0700297aaudio_result_t AudioStreamTrack::requestPause() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800298 if (mAudioTrack.get() == nullptr) {
Phil Burk1e83bee2018-12-17 14:15:20 -0800299 ALOGE("%s() no AudioTrack", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800300 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800301 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800302
Phil Burk5ed503c2017-02-01 09:38:15 -0800303 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burk965650e2017-09-07 21:00:09 -0700304 mAudioTrack->pause();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800305 mCallbackEnabled.store(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800306 status_t err = mAudioTrack->getPosition(&mPositionWhenPausing);
307 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800308 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800309 }
Phil Burk134f1972017-12-08 13:06:11 -0800310 return checkForDisconnectRequest(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800311}
312
Phil Burk5ed503c2017-02-01 09:38:15 -0800313aaudio_result_t AudioStreamTrack::requestFlush() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800314 if (mAudioTrack.get() == nullptr) {
Phil Burk1e83bee2018-12-17 14:15:20 -0800315 ALOGE("%s() no AudioTrack", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800316 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800317 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800318
Phil Burk5ed503c2017-02-01 09:38:15 -0800319 setState(AAUDIO_STREAM_STATE_FLUSHING);
Phil Burke1ce4912016-11-21 10:40:25 -0800320 incrementFramesRead(getFramesWritten() - getFramesRead());
321 mAudioTrack->flush();
Phil Burk2b6f1282018-05-10 15:26:30 -0700322 mFramesRead.reset32(); // service reads frames, service position reset on flush
Phil Burk7328a802017-08-30 09:29:48 -0700323 mTimestampPosition.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800324 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800325}
326
Phil Burk5ed503c2017-02-01 09:38:15 -0800327aaudio_result_t AudioStreamTrack::requestStop() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800328 if (mAudioTrack.get() == nullptr) {
Phil Burk1e83bee2018-12-17 14:15:20 -0800329 ALOGE("%s() no AudioTrack", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800330 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800331 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800332
Phil Burk5ed503c2017-02-01 09:38:15 -0800333 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burk18c84762018-12-18 12:15:35 -0800334 mFramesRead.catchUpTo(getFramesWritten());
335 mTimestampPosition.catchUpTo(getFramesWritten());
Phil Burk2b6f1282018-05-10 15:26:30 -0700336 mFramesRead.reset32(); // service reads frames, service position reset on stop
Phil Burk7328a802017-08-30 09:29:48 -0700337 mTimestampPosition.reset32();
Phil Burk965650e2017-09-07 21:00:09 -0700338 mAudioTrack->stop();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800339 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800340 return checkForDisconnectRequest(false);;
Phil Burke1ce4912016-11-21 10:40:25 -0800341}
342
Phil Burk0befec62017-07-28 15:12:13 -0700343aaudio_result_t AudioStreamTrack::updateStateMachine()
Phil Burke1ce4912016-11-21 10:40:25 -0800344{
345 status_t err;
Phil Burk5ed503c2017-02-01 09:38:15 -0800346 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800347 switch (getState()) {
348 // TODO add better state visibility to AudioTrack
Phil Burk5ed503c2017-02-01 09:38:15 -0800349 case AAUDIO_STREAM_STATE_STARTING:
Phil Burke1ce4912016-11-21 10:40:25 -0800350 if (mAudioTrack->hasStarted()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800351 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burke1ce4912016-11-21 10:40:25 -0800352 }
353 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800354 case AAUDIO_STREAM_STATE_PAUSING:
Phil Burke1ce4912016-11-21 10:40:25 -0800355 if (mAudioTrack->stopped()) {
356 err = mAudioTrack->getPosition(&position);
357 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800358 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800359 } else if (position == mPositionWhenPausing) {
360 // Has stream really stopped advancing?
Phil Burk5ed503c2017-02-01 09:38:15 -0800361 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800362 }
363 mPositionWhenPausing = position;
364 }
365 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800366 case AAUDIO_STREAM_STATE_FLUSHING:
Phil Burke1ce4912016-11-21 10:40:25 -0800367 {
368 err = mAudioTrack->getPosition(&position);
369 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800370 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800371 } else if (position == 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700372 // TODO Advance frames read to match written.
Phil Burk5ed503c2017-02-01 09:38:15 -0800373 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burke1ce4912016-11-21 10:40:25 -0800374 }
375 }
376 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800377 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burke1ce4912016-11-21 10:40:25 -0800378 if (mAudioTrack->stopped()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800379 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burke1ce4912016-11-21 10:40:25 -0800380 }
381 break;
382 default:
383 break;
384 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800385 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800386}
387
Phil Burk5ed503c2017-02-01 09:38:15 -0800388aaudio_result_t AudioStreamTrack::write(const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800389 int32_t numFrames,
390 int64_t timeoutNanoseconds)
Phil Burke1ce4912016-11-21 10:40:25 -0800391{
Phil Burk3316d5e2017-02-15 11:23:01 -0800392 int32_t bytesPerFrame = getBytesPerFrame();
393 int32_t numBytes;
Phil Burk5ed503c2017-02-01 09:38:15 -0800394 aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes);
395 if (result != AAUDIO_OK) {
Phil Burke1ce4912016-11-21 10:40:25 -0800396 return result;
397 }
398
Eric Laurentfb00fc72017-05-25 18:17:12 -0700399 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
400 return AAUDIO_ERROR_DISCONNECTED;
401 }
402
Phil Burke1ce4912016-11-21 10:40:25 -0800403 // TODO add timeout to AudioTrack
404 bool blocking = timeoutNanoseconds > 0;
405 ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking);
406 if (bytesWritten == WOULD_BLOCK) {
407 return 0;
408 } else if (bytesWritten < 0) {
409 ALOGE("invalid write, returned %d", (int)bytesWritten);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700410 // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to
411 // AudioTrack invalidation
412 if (bytesWritten == DEAD_OBJECT) {
413 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
414 return AAUDIO_ERROR_DISCONNECTED;
415 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800416 return AAudioConvert_androidToAAudioResult(bytesWritten);
Phil Burke1ce4912016-11-21 10:40:25 -0800417 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800418 int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -0800419 incrementFramesWritten(framesWritten);
Phil Burk0befec62017-07-28 15:12:13 -0700420
421 result = updateStateMachine();
422 if (result != AAUDIO_OK) {
423 return result;
424 }
425
Phil Burke1ce4912016-11-21 10:40:25 -0800426 return framesWritten;
427}
428
Phil Burk3316d5e2017-02-15 11:23:01 -0800429aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames)
Phil Burke1ce4912016-11-21 10:40:25 -0800430{
Phil Burk41852682019-03-29 10:58:15 -0700431 // Do not ask for less than one burst.
432 if (requestedFrames < getFramesPerBurst()) {
433 requestedFrames = getFramesPerBurst();
434 }
Phil Burke1ce4912016-11-21 10:40:25 -0800435 ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800436 if (result < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800437 return AAudioConvert_androidToAAudioResult(result);
Phil Burke1ce4912016-11-21 10:40:25 -0800438 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800439 return result;
Phil Burke1ce4912016-11-21 10:40:25 -0800440 }
441}
442
Phil Burk3316d5e2017-02-15 11:23:01 -0800443int32_t AudioStreamTrack::getBufferSize() const
Phil Burke1ce4912016-11-21 10:40:25 -0800444{
Phil Burk3316d5e2017-02-15 11:23:01 -0800445 return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800446}
447
Phil Burk3316d5e2017-02-15 11:23:01 -0800448int32_t AudioStreamTrack::getBufferCapacity() const
Phil Burke1ce4912016-11-21 10:40:25 -0800449{
Phil Burk3316d5e2017-02-15 11:23:01 -0800450 return static_cast<int32_t>(mAudioTrack->frameCount());
Phil Burke1ce4912016-11-21 10:40:25 -0800451}
452
453int32_t AudioStreamTrack::getXRunCount() const
454{
455 return static_cast<int32_t>(mAudioTrack->getUnderrunCount());
456}
457
458int32_t AudioStreamTrack::getFramesPerBurst() const
459{
Phil Burkb5884022017-03-27 15:26:14 -0700460 return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800461}
462
Phil Burk3316d5e2017-02-15 11:23:01 -0800463int64_t AudioStreamTrack::getFramesRead() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800464 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800465 status_t result;
466 switch (getState()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800467 case AAUDIO_STREAM_STATE_STARTING:
468 case AAUDIO_STREAM_STATE_STARTED:
469 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burk4c5129b2017-04-28 15:17:32 -0700470 case AAUDIO_STREAM_STATE_PAUSING:
471 case AAUDIO_STREAM_STATE_PAUSED:
Phil Burke1ce4912016-11-21 10:40:25 -0800472 result = mAudioTrack->getPosition(&position);
473 if (result == OK) {
474 mFramesRead.update32(position);
475 }
476 break;
477 default:
478 break;
479 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700480 return AudioStreamLegacy::getFramesRead();
Phil Burke1ce4912016-11-21 10:40:25 -0800481}
Phil Burk35e80f32017-03-28 10:25:21 -0700482
483aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId,
484 int64_t *framePosition,
485 int64_t *timeNanoseconds) {
486 ExtendedTimestamp extendedTimestamp;
487 status_t status = mAudioTrack->getTimestamp(&extendedTimestamp);
Phil Burkc75d97f2017-09-08 15:48:36 -0700488 if (status == WOULD_BLOCK) {
489 return AAUDIO_ERROR_INVALID_STATE;
490 } if (status != NO_ERROR) {
Phil Burk35e80f32017-03-28 10:25:21 -0700491 return AAudioConvert_androidToAAudioResult(status);
492 }
Phil Burk7328a802017-08-30 09:29:48 -0700493 int64_t position = 0;
494 int64_t nanoseconds = 0;
495 aaudio_result_t result = getBestTimestamp(clockId, &position,
496 &nanoseconds, &extendedTimestamp);
497 if (result == AAUDIO_OK) {
498 if (position < getFramesWritten()) {
499 *framePosition = position;
500 *timeNanoseconds = nanoseconds;
501 return result;
502 } else {
503 return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent
504 }
505 }
506 return result;
Phil Burk35e80f32017-03-28 10:25:21 -0700507}
Phil Burk965650e2017-09-07 21:00:09 -0700508
509status_t AudioStreamTrack::doSetVolume() {
510 status_t status = NO_INIT;
511 if (mAudioTrack.get() != nullptr) {
512 float volume = getDuckAndMuteVolume();
513 mAudioTrack->setVolume(volume, volume);
514 status = NO_ERROR;
515 }
516 return status;
517}
518
519#if AAUDIO_USE_VOLUME_SHAPER
Phil Burk8a8a9e52017-09-12 16:25:15 -0700520
521using namespace android::media::VolumeShaper;
522
Phil Burk965650e2017-09-07 21:00:09 -0700523binder::Status AudioStreamTrack::applyVolumeShaper(
524 const VolumeShaper::Configuration& configuration,
525 const VolumeShaper::Operation& operation) {
526
527 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
528 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
529
530 if (mAudioTrack.get() != nullptr) {
531 ALOGD("applyVolumeShaper() from IPlayer");
Phil Burk8a8a9e52017-09-12 16:25:15 -0700532 binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Phil Burk965650e2017-09-07 21:00:09 -0700533 if (status < 0) { // a non-negative value is the volume shaper id.
534 ALOGE("applyVolumeShaper() failed with status %d", status);
535 }
536 return binder::Status::fromStatusT(status);
537 } else {
538 ALOGD("applyVolumeShaper()"
539 " no AudioTrack for volume control from IPlayer");
540 return binder::Status::ok();
541 }
542}
543#endif