blob: 4d1b1871f1b273a9099f961998288d6edc803e7c [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 Burk4e1af9f2018-01-03 15:54:35 -0800137 static_assert(AAUDIO_UNSPECIFIED == AUDIO_SESSION_ALLOCATE, "Session IDs should match");
138
139 aaudio_session_id_t requestedSessionId = builder.getSessionId();
140 audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
141
Phil Burkee995392017-12-11 11:44:36 -0800142 mAudioTrack = new AudioTrack();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700143 mAudioTrack->set(
Phil Burkd4ccc622017-12-20 15:32:44 -0800144 AUDIO_STREAM_DEFAULT, // ignored because we pass attributes below
Phil Burke1ce4912016-11-21 10:40:25 -0800145 getSampleRate(),
146 format,
147 channelMask,
148 frameCount,
149 flags,
150 callback,
Phil Burke4d7bb42017-03-28 11:32:39 -0700151 callbackData,
152 notificationFrames,
Phil Burkee995392017-12-11 11:44:36 -0800153 0, // DEFAULT sharedBuffer*/,
154 false, // DEFAULT threadCanCallJava
Phil Burk4e1af9f2018-01-03 15:54:35 -0800155 sessionId,
Phil Burkee995392017-12-11 11:44:36 -0800156 streamTransferType,
157 NULL, // DEFAULT audio_offload_info_t
158 AUDIO_UID_INVALID, // DEFAULT uid
159 -1, // DEFAULT pid
Phil Burkd4ccc622017-12-20 15:32:44 -0800160 &attributes,
Phil Burkee995392017-12-11 11:44:36 -0800161 // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging
162 // headphones a few times.
163 false, // DEFAULT doNotReconnect,
164 1.0f, // DEFAULT maxRequiredSpeed
165 selectedDeviceId
166 );
Phil Burke1ce4912016-11-21 10:40:25 -0800167
168 // Did we get a valid track?
169 status_t status = mAudioTrack->initCheck();
Phil Burkdec33ab2017-01-17 14:48:16 -0800170 if (status != NO_ERROR) {
Phil Burke1ce4912016-11-21 10:40:25 -0800171 close();
Phil Burkfbf031e2017-10-12 15:58:31 -0700172 ALOGE("open(), initCheck() returned %d", status);
Phil Burk5ed503c2017-02-01 09:38:15 -0800173 return AAudioConvert_androidToAAudioResult(status);
Phil Burke1ce4912016-11-21 10:40:25 -0800174 }
175
Phil Burk965650e2017-09-07 21:00:09 -0700176 doSetVolume();
Eric Laurent1d32e9f2017-06-02 14:01:32 -0700177
Phil Burke1ce4912016-11-21 10:40:25 -0800178 // Get the actual values from the AudioTrack.
179 setSamplesPerFrame(mAudioTrack->channelCount());
Phil Burk9dca9822017-05-26 14:27:43 -0700180 aaudio_format_t aaudioFormat =
Phil Burke4d7bb42017-03-28 11:32:39 -0700181 AAudioConvert_androidToAAudioDataFormat(mAudioTrack->format());
182 setFormat(aaudioFormat);
183
Phil Burkcf5f6d22017-05-26 12:35:07 -0700184 int32_t actualSampleRate = mAudioTrack->getSampleRate();
185 ALOGW_IF(actualSampleRate != getSampleRate(),
Phil Burkfbf031e2017-10-12 15:58:31 -0700186 "open() sampleRate changed from %d to %d",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700187 getSampleRate(), actualSampleRate);
188 setSampleRate(actualSampleRate);
189
Phil Burke4d7bb42017-03-28 11:32:39 -0700190 // We may need to pass the data through a block size adapter to guarantee constant size.
191 if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) {
192 int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize;
193 mFixedBlockReader.open(callbackSizeBytes);
194 mBlockAdapter = &mFixedBlockReader;
195 } else {
196 mBlockAdapter = nullptr;
197 }
Phil Burke1ce4912016-11-21 10:40:25 -0800198
Phil Burk5ed503c2017-02-01 09:38:15 -0800199 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800200 setDeviceId(mAudioTrack->getRoutedDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800201
202 aaudio_session_id_t actualSessionId =
203 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
204 ? AAUDIO_SESSION_ID_NONE
205 : (aaudio_session_id_t) mAudioTrack->getSessionId();
206 setSessionId(actualSessionId);
207
Eric Laurentfb00fc72017-05-25 18:17:12 -0700208 mAudioTrack->addAudioDeviceCallback(mDeviceCallback);
Phil Burke1ce4912016-11-21 10:40:25 -0800209
Phil Burk09c27ca2017-08-24 22:12:56 -0700210 // Update performance mode based on the actual stream flags.
Phil Burk30a70772017-05-16 11:20:36 -0700211 // For example, if the sample rate is not allowed then you won't get a FAST track.
212 audio_output_flags_t actualFlags = mAudioTrack->getFlags();
213 aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
Phil Burk09c27ca2017-08-24 22:12:56 -0700214 // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY.
215 if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) {
Phil Burk30a70772017-05-16 11:20:36 -0700216 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
Phil Burk30a70772017-05-16 11:20:36 -0700217 } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
218 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
219 }
220 setPerformanceMode(actualPerformanceMode);
Phil Burkefa56002017-08-02 15:07:21 -0700221
222 setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy
223
Phil Burk30a70772017-05-16 11:20:36 -0700224 // Log warning if we did not get what we asked for.
225 ALOGW_IF(actualFlags != flags,
Phil Burkfbf031e2017-10-12 15:58:31 -0700226 "open() flags changed from 0x%08X to 0x%08X",
Phil Burk30a70772017-05-16 11:20:36 -0700227 flags, actualFlags);
228 ALOGW_IF(actualPerformanceMode != perfMode,
Phil Burkfbf031e2017-10-12 15:58:31 -0700229 "open() perfMode changed from %d to %d",
Phil Burk30a70772017-05-16 11:20:36 -0700230 perfMode, actualPerformanceMode);
231
Phil Burk5ed503c2017-02-01 09:38:15 -0800232 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800233}
234
Phil Burk5ed503c2017-02-01 09:38:15 -0800235aaudio_result_t AudioStreamTrack::close()
Phil Burke1ce4912016-11-21 10:40:25 -0800236{
Phil Burk5ed503c2017-02-01 09:38:15 -0800237 if (getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk965650e2017-09-07 21:00:09 -0700238 mAudioTrack->removeAudioDeviceCallback(mDeviceCallback);
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800240 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700241 mFixedBlockReader.close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800242 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800243}
244
Phil Burke4d7bb42017-03-28 11:32:39 -0700245void AudioStreamTrack::processCallback(int event, void *info) {
246
247 switch (event) {
248 case AudioTrack::EVENT_MORE_DATA:
249 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info);
250 break;
251
252 // Stream got rerouted so we disconnect.
253 case AudioTrack::EVENT_NEW_IAUDIOTRACK:
254 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info);
255 break;
256
257 default:
258 break;
259 }
260 return;
261}
262
Phil Burk965650e2017-09-07 21:00:09 -0700263aaudio_result_t AudioStreamTrack::requestStart() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800264 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700265 ALOGE("requestStart() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800266 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800267 }
268 // Get current position so we can detect when the track is playing.
269 status_t err = mAudioTrack->getPosition(&mPositionWhenStarting);
270 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800271 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800272 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700273
Phil Burk9e9a95b2018-01-18 12:14:15 -0800274 // Enable callback before starting AudioTrack to avoid shutting
275 // down because of a race condition.
276 mCallbackEnabled.store(true);
Phil Burk965650e2017-09-07 21:00:09 -0700277 err = mAudioTrack->start();
Phil Burke1ce4912016-11-21 10:40:25 -0800278 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800279 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800280 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800281 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burke1ce4912016-11-21 10:40:25 -0800282 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800283 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800284}
285
Phil Burk965650e2017-09-07 21:00:09 -0700286aaudio_result_t AudioStreamTrack::requestPause() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800287 if (mAudioTrack.get() == nullptr) {
Phil Burk965650e2017-09-07 21:00:09 -0700288 ALOGE("requestPause() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800289 return AAUDIO_ERROR_INVALID_STATE;
290 } else if (getState() != AAUDIO_STREAM_STATE_STARTING
291 && getState() != AAUDIO_STREAM_STATE_STARTED) {
Phil Burk134f1972017-12-08 13:06:11 -0800292 // TODO What about DISCONNECTED?
Phil Burke4d7bb42017-03-28 11:32:39 -0700293 ALOGE("requestPause(), called when state is %s",
294 AAudio_convertStreamStateToText(getState()));
Phil Burk5ed503c2017-02-01 09:38:15 -0800295 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800296 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800297 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burk965650e2017-09-07 21:00:09 -0700298 mAudioTrack->pause();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800299 mCallbackEnabled.store(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800300 status_t err = mAudioTrack->getPosition(&mPositionWhenPausing);
301 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800302 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800303 }
Phil Burk134f1972017-12-08 13:06:11 -0800304 return checkForDisconnectRequest(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800305}
306
Phil Burk5ed503c2017-02-01 09:38:15 -0800307aaudio_result_t AudioStreamTrack::requestFlush() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800308 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700309 ALOGE("requestFlush() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800310 return AAUDIO_ERROR_INVALID_STATE;
311 } else if (getState() != AAUDIO_STREAM_STATE_PAUSED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700312 ALOGE("requestFlush() not paused");
Phil Burk5ed503c2017-02-01 09:38:15 -0800313 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800314 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800315 setState(AAUDIO_STREAM_STATE_FLUSHING);
Phil Burke1ce4912016-11-21 10:40:25 -0800316 incrementFramesRead(getFramesWritten() - getFramesRead());
317 mAudioTrack->flush();
318 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700319 mTimestampPosition.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800320 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800321}
322
Phil Burk5ed503c2017-02-01 09:38:15 -0800323aaudio_result_t AudioStreamTrack::requestStop() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800324 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700325 ALOGE("requestStop() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800326 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800327 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800328 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burke1ce4912016-11-21 10:40:25 -0800329 incrementFramesRead(getFramesWritten() - getFramesRead()); // TODO review
Phil Burk7328a802017-08-30 09:29:48 -0700330 mTimestampPosition.set(getFramesWritten());
Phil Burke1ce4912016-11-21 10:40:25 -0800331 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700332 mTimestampPosition.reset32();
Phil Burk965650e2017-09-07 21:00:09 -0700333 mAudioTrack->stop();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800334 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800335 return checkForDisconnectRequest(false);;
Phil Burke1ce4912016-11-21 10:40:25 -0800336}
337
Phil Burk0befec62017-07-28 15:12:13 -0700338aaudio_result_t AudioStreamTrack::updateStateMachine()
Phil Burke1ce4912016-11-21 10:40:25 -0800339{
340 status_t err;
Phil Burk5ed503c2017-02-01 09:38:15 -0800341 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800342 switch (getState()) {
343 // TODO add better state visibility to AudioTrack
Phil Burk5ed503c2017-02-01 09:38:15 -0800344 case AAUDIO_STREAM_STATE_STARTING:
Phil Burke1ce4912016-11-21 10:40:25 -0800345 if (mAudioTrack->hasStarted()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800346 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burke1ce4912016-11-21 10:40:25 -0800347 }
348 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800349 case AAUDIO_STREAM_STATE_PAUSING:
Phil Burke1ce4912016-11-21 10:40:25 -0800350 if (mAudioTrack->stopped()) {
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 == mPositionWhenPausing) {
355 // Has stream really stopped advancing?
Phil Burk5ed503c2017-02-01 09:38:15 -0800356 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800357 }
358 mPositionWhenPausing = position;
359 }
360 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800361 case AAUDIO_STREAM_STATE_FLUSHING:
Phil Burke1ce4912016-11-21 10:40:25 -0800362 {
363 err = mAudioTrack->getPosition(&position);
364 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800365 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800366 } else if (position == 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700367 // TODO Advance frames read to match written.
Phil Burk5ed503c2017-02-01 09:38:15 -0800368 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burke1ce4912016-11-21 10:40:25 -0800369 }
370 }
371 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800372 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burke1ce4912016-11-21 10:40:25 -0800373 if (mAudioTrack->stopped()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800374 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burke1ce4912016-11-21 10:40:25 -0800375 }
376 break;
377 default:
378 break;
379 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800380 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800381}
382
Phil Burk5ed503c2017-02-01 09:38:15 -0800383aaudio_result_t AudioStreamTrack::write(const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800384 int32_t numFrames,
385 int64_t timeoutNanoseconds)
Phil Burke1ce4912016-11-21 10:40:25 -0800386{
Phil Burk3316d5e2017-02-15 11:23:01 -0800387 int32_t bytesPerFrame = getBytesPerFrame();
388 int32_t numBytes;
Phil Burk5ed503c2017-02-01 09:38:15 -0800389 aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes);
390 if (result != AAUDIO_OK) {
Phil Burke1ce4912016-11-21 10:40:25 -0800391 return result;
392 }
393
Eric Laurentfb00fc72017-05-25 18:17:12 -0700394 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
395 return AAUDIO_ERROR_DISCONNECTED;
396 }
397
Phil Burke1ce4912016-11-21 10:40:25 -0800398 // TODO add timeout to AudioTrack
399 bool blocking = timeoutNanoseconds > 0;
400 ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking);
401 if (bytesWritten == WOULD_BLOCK) {
402 return 0;
403 } else if (bytesWritten < 0) {
404 ALOGE("invalid write, returned %d", (int)bytesWritten);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700405 // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to
406 // AudioTrack invalidation
407 if (bytesWritten == DEAD_OBJECT) {
408 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
409 return AAUDIO_ERROR_DISCONNECTED;
410 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800411 return AAudioConvert_androidToAAudioResult(bytesWritten);
Phil Burke1ce4912016-11-21 10:40:25 -0800412 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800413 int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -0800414 incrementFramesWritten(framesWritten);
Phil Burk0befec62017-07-28 15:12:13 -0700415
416 result = updateStateMachine();
417 if (result != AAUDIO_OK) {
418 return result;
419 }
420
Phil Burke1ce4912016-11-21 10:40:25 -0800421 return framesWritten;
422}
423
Phil Burk3316d5e2017-02-15 11:23:01 -0800424aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames)
Phil Burke1ce4912016-11-21 10:40:25 -0800425{
426 ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800427 if (result < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800428 return AAudioConvert_androidToAAudioResult(result);
Phil Burke1ce4912016-11-21 10:40:25 -0800429 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800430 return result;
Phil Burke1ce4912016-11-21 10:40:25 -0800431 }
432}
433
Phil Burk3316d5e2017-02-15 11:23:01 -0800434int32_t AudioStreamTrack::getBufferSize() const
Phil Burke1ce4912016-11-21 10:40:25 -0800435{
Phil Burk3316d5e2017-02-15 11:23:01 -0800436 return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800437}
438
Phil Burk3316d5e2017-02-15 11:23:01 -0800439int32_t AudioStreamTrack::getBufferCapacity() const
Phil Burke1ce4912016-11-21 10:40:25 -0800440{
Phil Burk3316d5e2017-02-15 11:23:01 -0800441 return static_cast<int32_t>(mAudioTrack->frameCount());
Phil Burke1ce4912016-11-21 10:40:25 -0800442}
443
444int32_t AudioStreamTrack::getXRunCount() const
445{
446 return static_cast<int32_t>(mAudioTrack->getUnderrunCount());
447}
448
449int32_t AudioStreamTrack::getFramesPerBurst() const
450{
Phil Burkb5884022017-03-27 15:26:14 -0700451 return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800452}
453
Phil Burk3316d5e2017-02-15 11:23:01 -0800454int64_t AudioStreamTrack::getFramesRead() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800455 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800456 status_t result;
457 switch (getState()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800458 case AAUDIO_STREAM_STATE_STARTING:
459 case AAUDIO_STREAM_STATE_STARTED:
460 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burk4c5129b2017-04-28 15:17:32 -0700461 case AAUDIO_STREAM_STATE_PAUSING:
462 case AAUDIO_STREAM_STATE_PAUSED:
Phil Burke1ce4912016-11-21 10:40:25 -0800463 result = mAudioTrack->getPosition(&position);
464 if (result == OK) {
465 mFramesRead.update32(position);
466 }
467 break;
468 default:
469 break;
470 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700471 return AudioStreamLegacy::getFramesRead();
Phil Burke1ce4912016-11-21 10:40:25 -0800472}
Phil Burk35e80f32017-03-28 10:25:21 -0700473
474aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId,
475 int64_t *framePosition,
476 int64_t *timeNanoseconds) {
477 ExtendedTimestamp extendedTimestamp;
478 status_t status = mAudioTrack->getTimestamp(&extendedTimestamp);
Phil Burkc75d97f2017-09-08 15:48:36 -0700479 if (status == WOULD_BLOCK) {
480 return AAUDIO_ERROR_INVALID_STATE;
481 } if (status != NO_ERROR) {
Phil Burk35e80f32017-03-28 10:25:21 -0700482 return AAudioConvert_androidToAAudioResult(status);
483 }
Phil Burk7328a802017-08-30 09:29:48 -0700484 int64_t position = 0;
485 int64_t nanoseconds = 0;
486 aaudio_result_t result = getBestTimestamp(clockId, &position,
487 &nanoseconds, &extendedTimestamp);
488 if (result == AAUDIO_OK) {
489 if (position < getFramesWritten()) {
490 *framePosition = position;
491 *timeNanoseconds = nanoseconds;
492 return result;
493 } else {
494 return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent
495 }
496 }
497 return result;
Phil Burk35e80f32017-03-28 10:25:21 -0700498}
Phil Burk965650e2017-09-07 21:00:09 -0700499
500status_t AudioStreamTrack::doSetVolume() {
501 status_t status = NO_INIT;
502 if (mAudioTrack.get() != nullptr) {
503 float volume = getDuckAndMuteVolume();
504 mAudioTrack->setVolume(volume, volume);
505 status = NO_ERROR;
506 }
507 return status;
508}
509
510#if AAUDIO_USE_VOLUME_SHAPER
Phil Burk8a8a9e52017-09-12 16:25:15 -0700511
512using namespace android::media::VolumeShaper;
513
Phil Burk965650e2017-09-07 21:00:09 -0700514binder::Status AudioStreamTrack::applyVolumeShaper(
515 const VolumeShaper::Configuration& configuration,
516 const VolumeShaper::Operation& operation) {
517
518 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
519 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
520
521 if (mAudioTrack.get() != nullptr) {
522 ALOGD("applyVolumeShaper() from IPlayer");
Phil Burk8a8a9e52017-09-12 16:25:15 -0700523 binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Phil Burk965650e2017-09-07 21:00:09 -0700524 if (status < 0) { // a non-negative value is the volume shaper id.
525 ALOGE("applyVolumeShaper() failed with status %d", status);
526 }
527 return binder::Status::fromStatusT(status);
528 } else {
529 ALOGD("applyVolumeShaper()"
530 " no AudioTrack for volume control from IPlayer");
531 return binder::Status::ok();
532 }
533}
534#endif