blob: 96536013df51465aba7b165fbe74980e468a9e42 [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
62 // Try to create an AudioTrack
Phil Burk5204d312017-05-04 17:16:13 -070063 // Use stereo if unspecified.
Phil Burk5ed503c2017-02-01 09:38:15 -080064 int32_t samplesPerFrame = (getSamplesPerFrame() == AAUDIO_UNSPECIFIED)
Phil Burke1ce4912016-11-21 10:40:25 -080065 ? 2 : getSamplesPerFrame();
66 audio_channel_mask_t channelMask = audio_channel_out_mask_from_count(samplesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -080067
Phil Burke2fbb592017-05-01 15:05:52 -070068 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
Phil Burk30a70772017-05-16 11:20:36 -070069 aaudio_performance_mode_t perfMode = getPerformanceMode();
70 switch(perfMode) {
Phil Burke2fbb592017-05-01 15:05:52 -070071 case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY:
72 // Bypass the normal mixer and go straight to the FAST mixer.
73 flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW);
74 break;
75
76 case AAUDIO_PERFORMANCE_MODE_POWER_SAVING:
77 // This uses a mixer that wakes up less often than the FAST mixer.
78 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
79 break;
80
81 case AAUDIO_PERFORMANCE_MODE_NONE:
82 default:
83 // No flags. Use a normal mixer in front of the FAST mixer.
84 break;
85 }
Phil Burke4d7bb42017-03-28 11:32:39 -070086
Phil Burkcf5f6d22017-05-26 12:35:07 -070087 size_t frameCount = (size_t)builder.getBufferCapacity();
Phil Burke4d7bb42017-03-28 11:32:39 -070088
89 int32_t notificationFrames = 0;
90
Phil Burke4d7bb42017-03-28 11:32:39 -070091 audio_format_t format = (getFormat() == AAUDIO_FORMAT_UNSPECIFIED)
Phil Burke1ce4912016-11-21 10:40:25 -080092 ? AUDIO_FORMAT_PCM_FLOAT
Phil Burk5ed503c2017-02-01 09:38:15 -080093 : AAudioConvert_aaudioToAndroidDataFormat(getFormat());
Phil Burke1ce4912016-11-21 10:40:25 -080094
Phil Burke4d7bb42017-03-28 11:32:39 -070095 // Setup the callback if there is one.
96 AudioTrack::callback_t callback = nullptr;
97 void *callbackData = nullptr;
98 // Note that TRANSFER_SYNC does not allow FAST track
99 AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC;
100 if (builder.getDataCallbackProc() != nullptr) {
101 streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK;
102 callback = getLegacyCallback();
103 callbackData = this;
104
Phil Burke4d7bb42017-03-28 11:32:39 -0700105 // If the total buffer size is unspecified then base the size on the burst size.
Phil Burk4485d412017-05-09 15:55:02 -0700106 if (frameCount == 0
107 && ((flags & AUDIO_OUTPUT_FLAG_FAST) != 0)) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700108 // Take advantage of a special trick that allows us to create a buffer
109 // that is some multiple of the burst size.
110 notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY;
Phil Burk4485d412017-05-09 15:55:02 -0700111 } else {
112 notificationFrames = builder.getFramesPerDataCallback();
Phil Burke4d7bb42017-03-28 11:32:39 -0700113 }
114 }
115 mCallbackBufferSize = builder.getFramesPerDataCallback();
116
Phil Burkfbf031e2017-10-12 15:58:31 -0700117 ALOGD("open(), request notificationFrames = %d, frameCount = %u",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700118 notificationFrames, (uint)frameCount);
Phil Burkee995392017-12-11 11:44:36 -0800119
120 // Don't call mAudioTrack->setDeviceId() because it will be overwritten by set()!
121 audio_port_handle_t selectedDeviceId = (getDeviceId() == AAUDIO_UNSPECIFIED)
122 ? AUDIO_PORT_HANDLE_NONE
123 : getDeviceId();
124
Phil Burkd4ccc622017-12-20 15:32:44 -0800125 const audio_content_type_t contentType =
126 AAudioConvert_contentTypeToInternal(builder.getContentType());
127 const audio_usage_t usage =
128 AAudioConvert_usageToInternal(builder.getUsage());
129
130 const audio_attributes_t attributes = {
131 .content_type = contentType,
132 .usage = usage,
133 .source = AUDIO_SOURCE_DEFAULT, // only used for recording
Phil Burked816012018-02-06 12:40:50 -0800134 .flags = AUDIO_FLAG_NONE, // Different than the AUDIO_OUTPUT_FLAGS
Phil Burkd4ccc622017-12-20 15:32:44 -0800135 .tags = ""
136 };
137
Phil Burk4e1af9f2018-01-03 15:54:35 -0800138 static_assert(AAUDIO_UNSPECIFIED == AUDIO_SESSION_ALLOCATE, "Session IDs should match");
139
140 aaudio_session_id_t requestedSessionId = builder.getSessionId();
141 audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
142
Phil Burkee995392017-12-11 11:44:36 -0800143 mAudioTrack = new AudioTrack();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700144 mAudioTrack->set(
Phil Burkd4ccc622017-12-20 15:32:44 -0800145 AUDIO_STREAM_DEFAULT, // ignored because we pass attributes below
Phil Burke1ce4912016-11-21 10:40:25 -0800146 getSampleRate(),
147 format,
148 channelMask,
149 frameCount,
150 flags,
151 callback,
Phil Burke4d7bb42017-03-28 11:32:39 -0700152 callbackData,
153 notificationFrames,
Phil Burkee995392017-12-11 11:44:36 -0800154 0, // DEFAULT sharedBuffer*/,
155 false, // DEFAULT threadCanCallJava
Phil Burk4e1af9f2018-01-03 15:54:35 -0800156 sessionId,
Phil Burkee995392017-12-11 11:44:36 -0800157 streamTransferType,
158 NULL, // DEFAULT audio_offload_info_t
159 AUDIO_UID_INVALID, // DEFAULT uid
160 -1, // DEFAULT pid
Phil Burkd4ccc622017-12-20 15:32:44 -0800161 &attributes,
Phil Burkee995392017-12-11 11:44:36 -0800162 // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging
163 // headphones a few times.
164 false, // DEFAULT doNotReconnect,
165 1.0f, // DEFAULT maxRequiredSpeed
166 selectedDeviceId
167 );
Phil Burke1ce4912016-11-21 10:40:25 -0800168
169 // Did we get a valid track?
170 status_t status = mAudioTrack->initCheck();
Phil Burkdec33ab2017-01-17 14:48:16 -0800171 if (status != NO_ERROR) {
Phil Burke1ce4912016-11-21 10:40:25 -0800172 close();
Phil Burkfbf031e2017-10-12 15:58:31 -0700173 ALOGE("open(), initCheck() returned %d", status);
Phil Burk5ed503c2017-02-01 09:38:15 -0800174 return AAudioConvert_androidToAAudioResult(status);
Phil Burke1ce4912016-11-21 10:40:25 -0800175 }
176
Phil Burk965650e2017-09-07 21:00:09 -0700177 doSetVolume();
Eric Laurent1d32e9f2017-06-02 14:01:32 -0700178
Phil Burke1ce4912016-11-21 10:40:25 -0800179 // Get the actual values from the AudioTrack.
180 setSamplesPerFrame(mAudioTrack->channelCount());
Phil Burk9dca9822017-05-26 14:27:43 -0700181 aaudio_format_t aaudioFormat =
Phil Burke4d7bb42017-03-28 11:32:39 -0700182 AAudioConvert_androidToAAudioDataFormat(mAudioTrack->format());
183 setFormat(aaudioFormat);
Phil Burk3d786cb2018-04-09 11:58:09 -0700184 setDeviceFormat(aaudioFormat);
Phil Burke4d7bb42017-03-28 11:32:39 -0700185
Phil Burkcf5f6d22017-05-26 12:35:07 -0700186 int32_t actualSampleRate = mAudioTrack->getSampleRate();
187 ALOGW_IF(actualSampleRate != getSampleRate(),
Phil Burkfbf031e2017-10-12 15:58:31 -0700188 "open() sampleRate changed from %d to %d",
Phil Burkcf5f6d22017-05-26 12:35:07 -0700189 getSampleRate(), actualSampleRate);
190 setSampleRate(actualSampleRate);
191
Phil Burke4d7bb42017-03-28 11:32:39 -0700192 // We may need to pass the data through a block size adapter to guarantee constant size.
193 if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) {
194 int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize;
195 mFixedBlockReader.open(callbackSizeBytes);
196 mBlockAdapter = &mFixedBlockReader;
197 } else {
198 mBlockAdapter = nullptr;
199 }
Phil Burke1ce4912016-11-21 10:40:25 -0800200
Phil Burk5ed503c2017-02-01 09:38:15 -0800201 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 setDeviceId(mAudioTrack->getRoutedDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800203
204 aaudio_session_id_t actualSessionId =
205 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
206 ? AAUDIO_SESSION_ID_NONE
207 : (aaudio_session_id_t) mAudioTrack->getSessionId();
208 setSessionId(actualSessionId);
209
Eric Laurentfb00fc72017-05-25 18:17:12 -0700210 mAudioTrack->addAudioDeviceCallback(mDeviceCallback);
Phil Burke1ce4912016-11-21 10:40:25 -0800211
Phil Burk09c27ca2017-08-24 22:12:56 -0700212 // Update performance mode based on the actual stream flags.
Phil Burk30a70772017-05-16 11:20:36 -0700213 // For example, if the sample rate is not allowed then you won't get a FAST track.
214 audio_output_flags_t actualFlags = mAudioTrack->getFlags();
215 aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
Phil Burk09c27ca2017-08-24 22:12:56 -0700216 // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY.
217 if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) {
Phil Burk30a70772017-05-16 11:20:36 -0700218 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
Phil Burk30a70772017-05-16 11:20:36 -0700219 } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
220 actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
221 }
222 setPerformanceMode(actualPerformanceMode);
Phil Burkefa56002017-08-02 15:07:21 -0700223
224 setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy
225
Phil Burk30a70772017-05-16 11:20:36 -0700226 // Log warning if we did not get what we asked for.
227 ALOGW_IF(actualFlags != flags,
Phil Burkfbf031e2017-10-12 15:58:31 -0700228 "open() flags changed from 0x%08X to 0x%08X",
Phil Burk30a70772017-05-16 11:20:36 -0700229 flags, actualFlags);
230 ALOGW_IF(actualPerformanceMode != perfMode,
Phil Burkfbf031e2017-10-12 15:58:31 -0700231 "open() perfMode changed from %d to %d",
Phil Burk30a70772017-05-16 11:20:36 -0700232 perfMode, actualPerformanceMode);
233
Phil Burk5ed503c2017-02-01 09:38:15 -0800234 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800235}
236
Phil Burk5ed503c2017-02-01 09:38:15 -0800237aaudio_result_t AudioStreamTrack::close()
Phil Burke1ce4912016-11-21 10:40:25 -0800238{
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 if (getState() != AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk965650e2017-09-07 21:00:09 -0700240 mAudioTrack->removeAudioDeviceCallback(mDeviceCallback);
Phil Burk5ed503c2017-02-01 09:38:15 -0800241 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800242 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700243 mFixedBlockReader.close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800244 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800245}
246
Phil Burke4d7bb42017-03-28 11:32:39 -0700247void AudioStreamTrack::processCallback(int event, void *info) {
248
249 switch (event) {
250 case AudioTrack::EVENT_MORE_DATA:
251 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info);
252 break;
253
254 // Stream got rerouted so we disconnect.
255 case AudioTrack::EVENT_NEW_IAUDIOTRACK:
256 processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info);
257 break;
258
259 default:
260 break;
261 }
262 return;
263}
264
Phil Burk965650e2017-09-07 21:00:09 -0700265aaudio_result_t AudioStreamTrack::requestStart() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800266 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700267 ALOGE("requestStart() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800268 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800269 }
270 // Get current position so we can detect when the track is playing.
271 status_t err = mAudioTrack->getPosition(&mPositionWhenStarting);
272 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800273 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800274 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700275
Phil Burk9e9a95b2018-01-18 12:14:15 -0800276 // Enable callback before starting AudioTrack to avoid shutting
277 // down because of a race condition.
278 mCallbackEnabled.store(true);
Phil Burk965650e2017-09-07 21:00:09 -0700279 err = mAudioTrack->start();
Phil Burke1ce4912016-11-21 10:40:25 -0800280 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800281 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800282 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800283 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burke1ce4912016-11-21 10:40:25 -0800284 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800285 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800286}
287
Phil Burk965650e2017-09-07 21:00:09 -0700288aaudio_result_t AudioStreamTrack::requestPause() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800289 if (mAudioTrack.get() == nullptr) {
Phil Burk965650e2017-09-07 21:00:09 -0700290 ALOGE("requestPause() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800291 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800292 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800293
Phil Burk5ed503c2017-02-01 09:38:15 -0800294 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burk965650e2017-09-07 21:00:09 -0700295 mAudioTrack->pause();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800296 mCallbackEnabled.store(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800297 status_t err = mAudioTrack->getPosition(&mPositionWhenPausing);
298 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800299 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800300 }
Phil Burk134f1972017-12-08 13:06:11 -0800301 return checkForDisconnectRequest(false);
Phil Burke1ce4912016-11-21 10:40:25 -0800302}
303
Phil Burk5ed503c2017-02-01 09:38:15 -0800304aaudio_result_t AudioStreamTrack::requestFlush() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800305 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700306 ALOGE("requestFlush() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800307 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800308 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800309
Phil Burk5ed503c2017-02-01 09:38:15 -0800310 setState(AAUDIO_STREAM_STATE_FLUSHING);
Phil Burke1ce4912016-11-21 10:40:25 -0800311 incrementFramesRead(getFramesWritten() - getFramesRead());
312 mAudioTrack->flush();
313 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700314 mTimestampPosition.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800315 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800316}
317
Phil Burk5ed503c2017-02-01 09:38:15 -0800318aaudio_result_t AudioStreamTrack::requestStop() {
Phil Burkd8bdcab2017-01-03 17:20:30 -0800319 if (mAudioTrack.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700320 ALOGE("requestStop() no AudioTrack");
Phil Burk5ed503c2017-02-01 09:38:15 -0800321 return AAUDIO_ERROR_INVALID_STATE;
Phil Burke1ce4912016-11-21 10:40:25 -0800322 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800323
Phil Burk5ed503c2017-02-01 09:38:15 -0800324 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burke1ce4912016-11-21 10:40:25 -0800325 incrementFramesRead(getFramesWritten() - getFramesRead()); // TODO review
Phil Burk7328a802017-08-30 09:29:48 -0700326 mTimestampPosition.set(getFramesWritten());
Phil Burke1ce4912016-11-21 10:40:25 -0800327 mFramesWritten.reset32();
Phil Burk7328a802017-08-30 09:29:48 -0700328 mTimestampPosition.reset32();
Phil Burk965650e2017-09-07 21:00:09 -0700329 mAudioTrack->stop();
Phil Burk9e9a95b2018-01-18 12:14:15 -0800330 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800331 return checkForDisconnectRequest(false);;
Phil Burke1ce4912016-11-21 10:40:25 -0800332}
333
Phil Burk0befec62017-07-28 15:12:13 -0700334aaudio_result_t AudioStreamTrack::updateStateMachine()
Phil Burke1ce4912016-11-21 10:40:25 -0800335{
336 status_t err;
Phil Burk5ed503c2017-02-01 09:38:15 -0800337 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800338 switch (getState()) {
339 // TODO add better state visibility to AudioTrack
Phil Burk5ed503c2017-02-01 09:38:15 -0800340 case AAUDIO_STREAM_STATE_STARTING:
Phil Burke1ce4912016-11-21 10:40:25 -0800341 if (mAudioTrack->hasStarted()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800342 setState(AAUDIO_STREAM_STATE_STARTED);
Phil Burke1ce4912016-11-21 10:40:25 -0800343 }
344 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800345 case AAUDIO_STREAM_STATE_PAUSING:
Phil Burke1ce4912016-11-21 10:40:25 -0800346 if (mAudioTrack->stopped()) {
347 err = mAudioTrack->getPosition(&position);
348 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800349 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800350 } else if (position == mPositionWhenPausing) {
351 // Has stream really stopped advancing?
Phil Burk5ed503c2017-02-01 09:38:15 -0800352 setState(AAUDIO_STREAM_STATE_PAUSED);
Phil Burke1ce4912016-11-21 10:40:25 -0800353 }
354 mPositionWhenPausing = position;
355 }
356 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800357 case AAUDIO_STREAM_STATE_FLUSHING:
Phil Burke1ce4912016-11-21 10:40:25 -0800358 {
359 err = mAudioTrack->getPosition(&position);
360 if (err != OK) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800361 return AAudioConvert_androidToAAudioResult(err);
Phil Burke1ce4912016-11-21 10:40:25 -0800362 } else if (position == 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700363 // TODO Advance frames read to match written.
Phil Burk5ed503c2017-02-01 09:38:15 -0800364 setState(AAUDIO_STREAM_STATE_FLUSHED);
Phil Burke1ce4912016-11-21 10:40:25 -0800365 }
366 }
367 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800368 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burke1ce4912016-11-21 10:40:25 -0800369 if (mAudioTrack->stopped()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800370 setState(AAUDIO_STREAM_STATE_STOPPED);
Phil Burke1ce4912016-11-21 10:40:25 -0800371 }
372 break;
373 default:
374 break;
375 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800376 return AAUDIO_OK;
Phil Burke1ce4912016-11-21 10:40:25 -0800377}
378
Phil Burk5ed503c2017-02-01 09:38:15 -0800379aaudio_result_t AudioStreamTrack::write(const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800380 int32_t numFrames,
381 int64_t timeoutNanoseconds)
Phil Burke1ce4912016-11-21 10:40:25 -0800382{
Phil Burk3316d5e2017-02-15 11:23:01 -0800383 int32_t bytesPerFrame = getBytesPerFrame();
384 int32_t numBytes;
Phil Burk5ed503c2017-02-01 09:38:15 -0800385 aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes);
386 if (result != AAUDIO_OK) {
Phil Burke1ce4912016-11-21 10:40:25 -0800387 return result;
388 }
389
Eric Laurentfb00fc72017-05-25 18:17:12 -0700390 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
391 return AAUDIO_ERROR_DISCONNECTED;
392 }
393
Phil Burke1ce4912016-11-21 10:40:25 -0800394 // TODO add timeout to AudioTrack
395 bool blocking = timeoutNanoseconds > 0;
396 ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking);
397 if (bytesWritten == WOULD_BLOCK) {
398 return 0;
399 } else if (bytesWritten < 0) {
400 ALOGE("invalid write, returned %d", (int)bytesWritten);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700401 // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to
402 // AudioTrack invalidation
403 if (bytesWritten == DEAD_OBJECT) {
404 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
405 return AAUDIO_ERROR_DISCONNECTED;
406 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800407 return AAudioConvert_androidToAAudioResult(bytesWritten);
Phil Burke1ce4912016-11-21 10:40:25 -0800408 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800409 int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
Phil Burke1ce4912016-11-21 10:40:25 -0800410 incrementFramesWritten(framesWritten);
Phil Burk0befec62017-07-28 15:12:13 -0700411
412 result = updateStateMachine();
413 if (result != AAUDIO_OK) {
414 return result;
415 }
416
Phil Burke1ce4912016-11-21 10:40:25 -0800417 return framesWritten;
418}
419
Phil Burk3316d5e2017-02-15 11:23:01 -0800420aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames)
Phil Burke1ce4912016-11-21 10:40:25 -0800421{
422 ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800423 if (result < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800424 return AAudioConvert_androidToAAudioResult(result);
Phil Burke1ce4912016-11-21 10:40:25 -0800425 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800426 return result;
Phil Burke1ce4912016-11-21 10:40:25 -0800427 }
428}
429
Phil Burk3316d5e2017-02-15 11:23:01 -0800430int32_t AudioStreamTrack::getBufferSize() const
Phil Burke1ce4912016-11-21 10:40:25 -0800431{
Phil Burk3316d5e2017-02-15 11:23:01 -0800432 return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800433}
434
Phil Burk3316d5e2017-02-15 11:23:01 -0800435int32_t AudioStreamTrack::getBufferCapacity() const
Phil Burke1ce4912016-11-21 10:40:25 -0800436{
Phil Burk3316d5e2017-02-15 11:23:01 -0800437 return static_cast<int32_t>(mAudioTrack->frameCount());
Phil Burke1ce4912016-11-21 10:40:25 -0800438}
439
440int32_t AudioStreamTrack::getXRunCount() const
441{
442 return static_cast<int32_t>(mAudioTrack->getUnderrunCount());
443}
444
445int32_t AudioStreamTrack::getFramesPerBurst() const
446{
Phil Burkb5884022017-03-27 15:26:14 -0700447 return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames());
Phil Burke1ce4912016-11-21 10:40:25 -0800448}
449
Phil Burk3316d5e2017-02-15 11:23:01 -0800450int64_t AudioStreamTrack::getFramesRead() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800451 aaudio_wrapping_frames_t position;
Phil Burke1ce4912016-11-21 10:40:25 -0800452 status_t result;
453 switch (getState()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800454 case AAUDIO_STREAM_STATE_STARTING:
455 case AAUDIO_STREAM_STATE_STARTED:
456 case AAUDIO_STREAM_STATE_STOPPING:
Phil Burk4c5129b2017-04-28 15:17:32 -0700457 case AAUDIO_STREAM_STATE_PAUSING:
458 case AAUDIO_STREAM_STATE_PAUSED:
Phil Burke1ce4912016-11-21 10:40:25 -0800459 result = mAudioTrack->getPosition(&position);
460 if (result == OK) {
461 mFramesRead.update32(position);
462 }
463 break;
464 default:
465 break;
466 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700467 return AudioStreamLegacy::getFramesRead();
Phil Burke1ce4912016-11-21 10:40:25 -0800468}
Phil Burk35e80f32017-03-28 10:25:21 -0700469
470aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId,
471 int64_t *framePosition,
472 int64_t *timeNanoseconds) {
473 ExtendedTimestamp extendedTimestamp;
474 status_t status = mAudioTrack->getTimestamp(&extendedTimestamp);
Phil Burkc75d97f2017-09-08 15:48:36 -0700475 if (status == WOULD_BLOCK) {
476 return AAUDIO_ERROR_INVALID_STATE;
477 } if (status != NO_ERROR) {
Phil Burk35e80f32017-03-28 10:25:21 -0700478 return AAudioConvert_androidToAAudioResult(status);
479 }
Phil Burk7328a802017-08-30 09:29:48 -0700480 int64_t position = 0;
481 int64_t nanoseconds = 0;
482 aaudio_result_t result = getBestTimestamp(clockId, &position,
483 &nanoseconds, &extendedTimestamp);
484 if (result == AAUDIO_OK) {
485 if (position < getFramesWritten()) {
486 *framePosition = position;
487 *timeNanoseconds = nanoseconds;
488 return result;
489 } else {
490 return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent
491 }
492 }
493 return result;
Phil Burk35e80f32017-03-28 10:25:21 -0700494}
Phil Burk965650e2017-09-07 21:00:09 -0700495
496status_t AudioStreamTrack::doSetVolume() {
497 status_t status = NO_INIT;
498 if (mAudioTrack.get() != nullptr) {
499 float volume = getDuckAndMuteVolume();
500 mAudioTrack->setVolume(volume, volume);
501 status = NO_ERROR;
502 }
503 return status;
504}
505
506#if AAUDIO_USE_VOLUME_SHAPER
Phil Burk8a8a9e52017-09-12 16:25:15 -0700507
508using namespace android::media::VolumeShaper;
509
Phil Burk965650e2017-09-07 21:00:09 -0700510binder::Status AudioStreamTrack::applyVolumeShaper(
511 const VolumeShaper::Configuration& configuration,
512 const VolumeShaper::Operation& operation) {
513
514 sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
515 sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
516
517 if (mAudioTrack.get() != nullptr) {
518 ALOGD("applyVolumeShaper() from IPlayer");
Phil Burk8a8a9e52017-09-12 16:25:15 -0700519 binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
Phil Burk965650e2017-09-07 21:00:09 -0700520 if (status < 0) { // a non-negative value is the volume shaper id.
521 ALOGE("applyVolumeShaper() failed with status %d", status);
522 }
523 return binder::Status::fromStatusT(status);
524 } else {
525 ALOGD("applyVolumeShaper()"
526 " no AudioTrack for volume control from IPlayer");
527 return binder::Status::ok();
528 }
529}
530#endif