blob: ff95aedcb60312f851c501e2889a9e944a27582b [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();
69 audio_channel_mask_t channelMask = audio_channel_out_mask_from_count(samplesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -080070
Phil Burk8ffcf612018-05-23 14:38:07 -070071 audio_output_flags_t flags;
Phil Burk30a70772017-05-16 11:20:36 -070072 aaudio_performance_mode_t perfMode = getPerformanceMode();
73 switch(perfMode) {
Phil Burke2fbb592017-05-01 15:05:52 -070074 case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY:
75 // Bypass the normal mixer and go straight to the FAST mixer.
Phil Burk8ffcf612018-05-23 14:38:07 -070076 // If the app asks for a sessionId then it means they want to use effects.
77 // So don't use RAW flag.
78 flags = (audio_output_flags_t) ((requestedSessionId == AAUDIO_SESSION_ID_NONE)
79 ? (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW)
80 : (AUDIO_OUTPUT_FLAG_FAST));
Phil Burke2fbb592017-05-01 15:05:52 -070081 break;
82
83 case AAUDIO_PERFORMANCE_MODE_POWER_SAVING:
84 // This uses a mixer that wakes up less often than the FAST mixer.
85 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
86 break;
87
88 case AAUDIO_PERFORMANCE_MODE_NONE:
89 default:
90 // No flags. Use a normal mixer in front of the FAST mixer.
Phil Burk8ffcf612018-05-23 14:38:07 -070091 flags = AUDIO_OUTPUT_FLAG_NONE;
Phil Burke2fbb592017-05-01 15:05:52 -070092 break;
93 }
Phil Burke4d7bb42017-03-28 11:32:39 -070094
Phil Burkcf5f6d22017-05-26 12:35:07 -070095 size_t frameCount = (size_t)builder.getBufferCapacity();
Phil Burke4d7bb42017-03-28 11:32:39 -070096
97 int32_t notificationFrames = 0;
98
Phil Burk0127c1b2018-03-29 13:48:06 -070099 const audio_format_t format = (getFormat() == AUDIO_FORMAT_DEFAULT)
Phil Burke1ce4912016-11-21 10:40:25 -0800100 ? AUDIO_FORMAT_PCM_FLOAT
Phil Burk0127c1b2018-03-29 13:48:06 -0700101 : getFormat();
Phil Burke1ce4912016-11-21 10:40:25 -0800102
Phil Burke4d7bb42017-03-28 11:32:39 -0700103 // Setup the callback if there is one.
104 AudioTrack::callback_t callback = nullptr;
105 void *callbackData = nullptr;
106 // Note that TRANSFER_SYNC does not allow FAST track
107 AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC;
108 if (builder.getDataCallbackProc() != nullptr) {
109 streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK;
110 callback = getLegacyCallback();
111 callbackData = this;
112
Phil Burke4d7bb42017-03-28 11:32:39 -0700113 // If the total buffer size is unspecified then base the size on the burst size.
Phil Burk4485d412017-05-09 15:55:02 -0700114 if (frameCount == 0
115 && ((flags & AUDIO_OUTPUT_FLAG_FAST) != 0)) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700116 // Take advantage of a special trick that allows us to create a buffer
117 // that is some multiple of the burst size.
118 notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY;
Phil Burk4485d412017-05-09 15:55:02 -0700119 } else {
120 notificationFrames = builder.getFramesPerDataCallback();
Phil Burke4d7bb42017-03-28 11:32:39 -0700121 }
122 }
123 mCallbackBufferSize = builder.getFramesPerDataCallback();
124
Phil Burkfbf031e2017-10-12 15:58:31 -0700125 ALOGD("open(), request notificationFrames = %d, frameCount = %u",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700126 notificationFrames, (uint)frameCount);
Phil Burkee995392017-12-11 11:44:36 -0800127
128 // Don't call mAudioTrack->setDeviceId() because it will be overwritten by set()!
129 audio_port_handle_t selectedDeviceId = (getDeviceId() == AAUDIO_UNSPECIFIED)
130 ? AUDIO_PORT_HANDLE_NONE
131 : getDeviceId();
132
Phil Burkd4ccc622017-12-20 15:32:44 -0800133 const audio_content_type_t contentType =
134 AAudioConvert_contentTypeToInternal(builder.getContentType());
135 const audio_usage_t usage =
136 AAudioConvert_usageToInternal(builder.getUsage());
137
138 const audio_attributes_t attributes = {
139 .content_type = contentType,
140 .usage = usage,
141 .source = AUDIO_SOURCE_DEFAULT, // only used for recording
Phil Burked816012018-02-06 12:40:50 -0800142 .flags = AUDIO_FLAG_NONE, // Different than the AUDIO_OUTPUT_FLAGS
Phil Burkd4ccc622017-12-20 15:32:44 -0800143 .tags = ""
144 };
145
Phil Burkee995392017-12-11 11:44:36 -0800146 mAudioTrack = new AudioTrack();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700147 mAudioTrack->set(
Phil Burkd4ccc622017-12-20 15:32:44 -0800148 AUDIO_STREAM_DEFAULT, // ignored because we pass attributes below
Phil Burke1ce4912016-11-21 10:40:25 -0800149 getSampleRate(),
150 format,
151 channelMask,
152 frameCount,
153 flags,
154 callback,
Phil Burke4d7bb42017-03-28 11:32:39 -0700155 callbackData,
156 notificationFrames,
Phil Burkee995392017-12-11 11:44:36 -0800157 0, // DEFAULT sharedBuffer*/,
158 false, // DEFAULT threadCanCallJava
Phil Burk4e1af9f2018-01-03 15:54:35 -0800159 sessionId,
Phil Burkee995392017-12-11 11:44:36 -0800160 streamTransferType,
161 NULL, // DEFAULT audio_offload_info_t
162 AUDIO_UID_INVALID, // DEFAULT uid
163 -1, // DEFAULT pid
Phil Burkd4ccc622017-12-20 15:32:44 -0800164 &attributes,
Phil Burkee995392017-12-11 11:44:36 -0800165 // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging
166 // headphones a few times.
167 false, // DEFAULT doNotReconnect,
168 1.0f, // DEFAULT maxRequiredSpeed
169 selectedDeviceId
170 );
Phil Burke1ce4912016-11-21 10:40:25 -0800171
172 // Did we get a valid track?
173 status_t status = mAudioTrack->initCheck();
Phil Burkdec33ab2017-01-17 14:48:16 -0800174 if (status != NO_ERROR) {
Phil Burke1ce4912016-11-21 10:40:25 -0800175 close();
Phil Burkfbf031e2017-10-12 15:58:31 -0700176 ALOGE("open(), initCheck() returned %d", status);
Phil Burk5ed503c2017-02-01 09:38:15 -0800177 return AAudioConvert_androidToAAudioResult(status);
Phil Burke1ce4912016-11-21 10:40:25 -0800178 }
179
Phil Burk965650e2017-09-07 21:00:09 -0700180 doSetVolume();
Eric Laurent1d32e9f2017-06-02 14:01:32 -0700181
Phil Burke1ce4912016-11-21 10:40:25 -0800182 // Get the actual values from the AudioTrack.
183 setSamplesPerFrame(mAudioTrack->channelCount());
Phil Burk0127c1b2018-03-29 13:48:06 -0700184 setFormat(mAudioTrack->format());
185 setDeviceFormat(mAudioTrack->format());
Phil Burke4d7bb42017-03-28 11:32:39 -0700186
Phil Burkcf5f6d22017-05-26 12:35:07 -0700187 int32_t actualSampleRate = mAudioTrack->getSampleRate();
188 ALOGW_IF(actualSampleRate != getSampleRate(),
Phil Burkfbf031e2017-10-12 15:58:31 -0700189 "open() sampleRate changed from %d to %d",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700190 getSampleRate(), actualSampleRate);
191 setSampleRate(actualSampleRate);
192
Phil Burke4d7bb42017-03-28 11:32:39 -0700193 // We may need to pass the data through a block size adapter to guarantee constant size.
194 if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) {
195 int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize;
196 mFixedBlockReader.open(callbackSizeBytes);
197 mBlockAdapter = &mFixedBlockReader;
198 } else {
199 mBlockAdapter = nullptr;
200 }
Phil Burke1ce4912016-11-21 10:40:25 -0800201
Phil Burk5ed503c2017-02-01 09:38:15 -0800202 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 setDeviceId(mAudioTrack->getRoutedDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800204
205 aaudio_session_id_t actualSessionId =
206 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
207 ? AAUDIO_SESSION_ID_NONE
208 : (aaudio_session_id_t) mAudioTrack->getSessionId();
209 setSessionId(actualSessionId);
210
Eric Laurentfb00fc72017-05-25 18:17:12 -0700211 mAudioTrack->addAudioDeviceCallback(mDeviceCallback);
Phil Burke1ce4912016-11-21 10:40:25 -0800212
Phil Burk09c27ca2017-08-24 22:12:56 -0700213 // Update performance mode based on the actual stream flags.
Phil Burk30a70772017-05-16 11:20:36 -0700214 // For example, if the sample rate is not allowed then you won't get a FAST track.
215 audio_output_flags_t actualFlags = mAudioTrack->getFlags();
216 aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
Phil Burk09c27ca2017-08-24 22:12:56 -0700217 // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY.
218 if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) {
Phil Burk30a70772017-05-16 11:20:36 -0700219 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
Phil Burk30a70772017-05-16 11:20:36 -0700220 } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
221 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
222 }
223 setPerformanceMode(actualPerformanceMode);
Phil Burkefa56002017-08-02 15:07:21 -0700224
225 setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy
226
Phil Burk30a70772017-05-16 11:20:36 -0700227 // Log warning if we did not get what we asked for.
228 ALOGW_IF(actualFlags != flags,
Phil Burkfbf031e2017-10-12 15:58:31 -0700229 "open() flags changed from 0x%08X to 0x%08X",
Phil Burk30a70772017-05-16 11:20:36 -0700230 flags, actualFlags);
231 ALOGW_IF(actualPerformanceMode != perfMode,
Phil Burkfbf031e2017-10-12 15:58:31 -0700232 "open() perfMode changed from %d to %d",
Phil Burk30a70772017-05-16 11:20:36 -0700233 perfMode, actualPerformanceMode);
234
Phil Burk5ed503c2017-02-01 09:38:15 -0800235 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800236}
237
Phil Burk5ed503c2017-02-01 09:38:15 -0800238aaudio_result_t AudioStreamTrack::close()
Phil Burke1ce4912016-11-21 10:40:25 -0800239{
Phil Burk5ed503c2017-02-01 09:38:15 -0800240 if (getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk965650e2017-09-07 21:00:09 -0700241 mAudioTrack->removeAudioDeviceCallback(mDeviceCallback);
Phil Burk5ed503c2017-02-01 09:38:15 -0800242 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800243 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700244 mFixedBlockReader.close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800245 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800246}
247
Phil Burke4d7bb42017-03-28 11:32:39 -0700248void AudioStreamTrack::processCallback(int event, void *info) {
249
250 switch (event) {
251 case AudioTrack::EVENT_MORE_DATA:
252 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info);
253 break;
254
255 // Stream got rerouted so we disconnect.
256 case AudioTrack::EVENT_NEW_IAUDIOTRACK:
257 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info);
258 break;
259
260 default:
261 break;
262 }
263 return;
264}
265
Phil Burk965650e2017-09-07 21:00:09 -0700266aaudio_result_t AudioStreamTrack::requestStart() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800267 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700268 ALOGE("requestStart() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800269 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800270 }
271 // Get current position so we can detect when the track is playing.
272 status_t err = mAudioTrack->getPosition(&mPositionWhenStarting);
273 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800274 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800275 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700276
Phil Burk9e9a95b2018-01-18 12:14:15 -0800277 // Enable callback before starting AudioTrack to avoid shutting
278 // down because of a race condition.
279 mCallbackEnabled.store(true);
Phil Burk965650e2017-09-07 21:00:09 -0700280 err = mAudioTrack->start();
Phil Burke1ce4912016-11-21 10:40:25 -0800281 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800282 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800283 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800284 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burke1ce4912016-11-21 10:40:25 -0800285 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800286 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800287}
288
Phil Burk965650e2017-09-07 21:00:09 -0700289aaudio_result_t AudioStreamTrack::requestPause() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800290 if (mAudioTrack.get() == nullptr) {
Phil Burk1e83bee2018-12-17 14:15:20 -0800291 ALOGE("%s() no AudioTrack", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800292 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800293 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800294
Phil Burk5ed503c2017-02-01 09:38:15 -0800295 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burk965650e2017-09-07 21:00:09 -0700296 mAudioTrack->pause();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800297 mCallbackEnabled.store(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800298 status_t err = mAudioTrack->getPosition(&mPositionWhenPausing);
299 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800300 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800301 }
Phil Burk134f1972017-12-08 13:06:11 -0800302 return checkForDisconnectRequest(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800303}
304
Phil Burk5ed503c2017-02-01 09:38:15 -0800305aaudio_result_t AudioStreamTrack::requestFlush() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800306 if (mAudioTrack.get() == nullptr) {
Phil Burk1e83bee2018-12-17 14:15:20 -0800307 ALOGE("%s() no AudioTrack", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800308 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800309 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800310
Phil Burk5ed503c2017-02-01 09:38:15 -0800311 setState(AAUDIO_STREAM_STATE_FLUSHING);
Phil Burke1ce4912016-11-21 10:40:25 -0800312 incrementFramesRead(getFramesWritten() - getFramesRead());
313 mAudioTrack->flush();
Phil Burk2b6f1282018-05-10 15:26:30 -0700314 mFramesRead.reset32(); // service reads frames, service position reset on flush
Phil Burk7328a802017-08-30 09:29:48 -0700315 mTimestampPosition.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800316 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800317}
318
Phil Burk5ed503c2017-02-01 09:38:15 -0800319aaudio_result_t AudioStreamTrack::requestStop() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800320 if (mAudioTrack.get() == nullptr) {
Phil Burk1e83bee2018-12-17 14:15:20 -0800321 ALOGE("%s() no AudioTrack", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800322 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800323 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800324
Phil Burk5ed503c2017-02-01 09:38:15 -0800325 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burk18c84762018-12-18 12:15:35 -0800326 mFramesRead.catchUpTo(getFramesWritten());
327 mTimestampPosition.catchUpTo(getFramesWritten());
Phil Burk2b6f1282018-05-10 15:26:30 -0700328 mFramesRead.reset32(); // service reads frames, service position reset on stop
Phil Burk7328a802017-08-30 09:29:48 -0700329 mTimestampPosition.reset32();
Phil Burk965650e2017-09-07 21:00:09 -0700330 mAudioTrack->stop();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800331 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800332 return checkForDisconnectRequest(false);;
Phil Burke1ce4912016-11-21 10:40:25 -0800333}
334
Phil Burk0befec62017-07-28 15:12:13 -0700335aaudio_result_t AudioStreamTrack::updateStateMachine()
Phil Burke1ce4912016-11-21 10:40:25 -0800336{
337 status_t err;
Phil Burk5ed503c2017-02-01 09:38:15 -0800338 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800339 switch (getState()) {
340 // TODO add better state visibility to AudioTrack
Phil Burk5ed503c2017-02-01 09:38:15 -0800341 case AAUDIO_STREAM_STATE_STARTING:
Phil Burke1ce4912016-11-21 10:40:25 -0800342 if (mAudioTrack->hasStarted()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800343 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burke1ce4912016-11-21 10:40:25 -0800344 }
345 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800346 case AAUDIO_STREAM_STATE_PAUSING:
Phil Burke1ce4912016-11-21 10:40:25 -0800347 if (mAudioTrack->stopped()) {
348 err = mAudioTrack->getPosition(&position);
349 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800350 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800351 } else if (position == mPositionWhenPausing) {
352 // Has stream really stopped advancing?
Phil Burk5ed503c2017-02-01 09:38:15 -0800353 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800354 }
355 mPositionWhenPausing = position;
356 }
357 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800358 case AAUDIO_STREAM_STATE_FLUSHING:
Phil Burke1ce4912016-11-21 10:40:25 -0800359 {
360 err = mAudioTrack->getPosition(&position);
361 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800362 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800363 } else if (position == 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700364 // TODO Advance frames read to match written.
Phil Burk5ed503c2017-02-01 09:38:15 -0800365 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burke1ce4912016-11-21 10:40:25 -0800366 }
367 }
368 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800369 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burke1ce4912016-11-21 10:40:25 -0800370 if (mAudioTrack->stopped()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800371 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burke1ce4912016-11-21 10:40:25 -0800372 }
373 break;
374 default:
375 break;
376 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800377 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800378}
379
Phil Burk5ed503c2017-02-01 09:38:15 -0800380aaudio_result_t AudioStreamTrack::write(const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800381 int32_t numFrames,
382 int64_t timeoutNanoseconds)
Phil Burke1ce4912016-11-21 10:40:25 -0800383{
Phil Burk3316d5e2017-02-15 11:23:01 -0800384 int32_t bytesPerFrame = getBytesPerFrame();
385 int32_t numBytes;
Phil Burk5ed503c2017-02-01 09:38:15 -0800386 aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes);
387 if (result != AAUDIO_OK) {
Phil Burke1ce4912016-11-21 10:40:25 -0800388 return result;
389 }
390
Eric Laurentfb00fc72017-05-25 18:17:12 -0700391 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
392 return AAUDIO_ERROR_DISCONNECTED;
393 }
394
Phil Burke1ce4912016-11-21 10:40:25 -0800395 // TODO add timeout to AudioTrack
396 bool blocking = timeoutNanoseconds > 0;
397 ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking);
398 if (bytesWritten == WOULD_BLOCK) {
399 return 0;
400 } else if (bytesWritten < 0) {
401 ALOGE("invalid write, returned %d", (int)bytesWritten);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700402 // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to
403 // AudioTrack invalidation
404 if (bytesWritten == DEAD_OBJECT) {
405 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
406 return AAUDIO_ERROR_DISCONNECTED;
407 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800408 return AAudioConvert_androidToAAudioResult(bytesWritten);
Phil Burke1ce4912016-11-21 10:40:25 -0800409 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800410 int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -0800411 incrementFramesWritten(framesWritten);
Phil Burk0befec62017-07-28 15:12:13 -0700412
413 result = updateStateMachine();
414 if (result != AAUDIO_OK) {
415 return result;
416 }
417
Phil Burke1ce4912016-11-21 10:40:25 -0800418 return framesWritten;
419}
420
Phil Burk3316d5e2017-02-15 11:23:01 -0800421aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames)
Phil Burke1ce4912016-11-21 10:40:25 -0800422{
423 ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800424 if (result < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800425 return AAudioConvert_androidToAAudioResult(result);
Phil Burke1ce4912016-11-21 10:40:25 -0800426 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800427 return result;
Phil Burke1ce4912016-11-21 10:40:25 -0800428 }
429}
430
Phil Burk3316d5e2017-02-15 11:23:01 -0800431int32_t AudioStreamTrack::getBufferSize() const
Phil Burke1ce4912016-11-21 10:40:25 -0800432{
Phil Burk3316d5e2017-02-15 11:23:01 -0800433 return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800434}
435
Phil Burk3316d5e2017-02-15 11:23:01 -0800436int32_t AudioStreamTrack::getBufferCapacity() const
Phil Burke1ce4912016-11-21 10:40:25 -0800437{
Phil Burk3316d5e2017-02-15 11:23:01 -0800438 return static_cast<int32_t>(mAudioTrack->frameCount());
Phil Burke1ce4912016-11-21 10:40:25 -0800439}
440
441int32_t AudioStreamTrack::getXRunCount() const
442{
443 return static_cast<int32_t>(mAudioTrack->getUnderrunCount());
444}
445
446int32_t AudioStreamTrack::getFramesPerBurst() const
447{
Phil Burkb5884022017-03-27 15:26:14 -0700448 return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800449}
450
Phil Burk3316d5e2017-02-15 11:23:01 -0800451int64_t AudioStreamTrack::getFramesRead() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800452 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800453 status_t result;
454 switch (getState()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800455 case AAUDIO_STREAM_STATE_STARTING:
456 case AAUDIO_STREAM_STATE_STARTED:
457 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burk4c5129b2017-04-28 15:17:32 -0700458 case AAUDIO_STREAM_STATE_PAUSING:
459 case AAUDIO_STREAM_STATE_PAUSED:
Phil Burke1ce4912016-11-21 10:40:25 -0800460 result = mAudioTrack->getPosition(&position);
461 if (result == OK) {
462 mFramesRead.update32(position);
463 }
464 break;
465 default:
466 break;
467 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700468 return AudioStreamLegacy::getFramesRead();
Phil Burke1ce4912016-11-21 10:40:25 -0800469}
Phil Burk35e80f32017-03-28 10:25:21 -0700470
471aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId,
472 int64_t *framePosition,
473 int64_t *timeNanoseconds) {
474 ExtendedTimestamp extendedTimestamp;
475 status_t status = mAudioTrack->getTimestamp(&extendedTimestamp);
Phil Burkc75d97f2017-09-08 15:48:36 -0700476 if (status == WOULD_BLOCK) {
477 return AAUDIO_ERROR_INVALID_STATE;
478 } if (status != NO_ERROR) {
Phil Burk35e80f32017-03-28 10:25:21 -0700479 return AAudioConvert_androidToAAudioResult(status);
480 }
Phil Burk7328a802017-08-30 09:29:48 -0700481 int64_t position = 0;
482 int64_t nanoseconds = 0;
483 aaudio_result_t result = getBestTimestamp(clockId, &position,
484 &nanoseconds, &extendedTimestamp);
485 if (result == AAUDIO_OK) {
486 if (position < getFramesWritten()) {
487 *framePosition = position;
488 *timeNanoseconds = nanoseconds;
489 return result;
490 } else {
491 return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent
492 }
493 }
494 return result;
Phil Burk35e80f32017-03-28 10:25:21 -0700495}
Phil Burk965650e2017-09-07 21:00:09 -0700496
497status_t AudioStreamTrack::doSetVolume() {
498 status_t status = NO_INIT;
499 if (mAudioTrack.get() != nullptr) {
500 float volume = getDuckAndMuteVolume();
501 mAudioTrack->setVolume(volume, volume);
502 status = NO_ERROR;
503 }
504 return status;
505}
506
507#if AAUDIO_USE_VOLUME_SHAPER
Phil Burk8a8a9e52017-09-12 16:25:15 -0700508
509using namespace android::media::VolumeShaper;
510
Phil Burk965650e2017-09-07 21:00:09 -0700511binder::Status AudioStreamTrack::applyVolumeShaper(
512 const VolumeShaper::Configuration& configuration,
513 const VolumeShaper::Operation& operation) {
514
515 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
516 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
517
518 if (mAudioTrack.get() != nullptr) {
519 ALOGD("applyVolumeShaper() from IPlayer");
Phil Burk8a8a9e52017-09-12 16:25:15 -0700520 binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Phil Burk965650e2017-09-07 21:00:09 -0700521 if (status < 0) { // a non-negative value is the volume shaper id.
522 ALOGE("applyVolumeShaper() failed with status %d", status);
523 }
524 return binder::Status::fromStatusT(status);
525 } else {
526 ALOGD("applyVolumeShaper()"
527 " no AudioTrack for volume control from IPlayer");
528 return binder::Status::ok();
529 }
530}
531#endif