Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "AudioTrack" |
| 21 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <sys/resource.h> |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 23 | #include <audio_utils/primitives.h> |
| 24 | #include <binder/IPCThreadState.h> |
| 25 | #include <media/AudioTrack.h> |
| 26 | #include <utils/Log.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <private/media/AudioTrackShared.h> |
| 28 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 29 | #define WAIT_PERIOD_MS 10 |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | namespace android { |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 32 | // --------------------------------------------------------------------------- |
| 33 | |
| 34 | // static |
| 35 | status_t AudioTrack::getMinFrameCount( |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 36 | size_t* frameCount, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 37 | audio_stream_type_t streamType, |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 38 | uint32_t sampleRate) |
| 39 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 40 | if (frameCount == NULL) { |
| 41 | return BAD_VALUE; |
| 42 | } |
Glenn Kasten | 04cd018 | 2012-06-25 11:49:27 -0700 | [diff] [blame] | 43 | |
| 44 | // default to 0 in case of error |
| 45 | *frameCount = 0; |
| 46 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 47 | // FIXME merge with similar code in createTrack_l(), except we're missing |
| 48 | // some information here that is available in createTrack_l(): |
| 49 | // audio_io_handle_t output |
| 50 | // audio_format_t format |
| 51 | // audio_channel_mask_t channelMask |
| 52 | // audio_output_flags_t flags |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 53 | uint32_t afSampleRate; |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 54 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 55 | return NO_INIT; |
| 56 | } |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 57 | size_t afFrameCount; |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 58 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 59 | return NO_INIT; |
| 60 | } |
| 61 | uint32_t afLatency; |
| 62 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 63 | return NO_INIT; |
| 64 | } |
| 65 | |
| 66 | // Ensure that buffer depth covers at least audio hardware latency |
| 67 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 68 | if (minBufCount < 2) { |
| 69 | minBufCount = 2; |
| 70 | } |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 71 | |
| 72 | *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 73 | afFrameCount * minBufCount * sampleRate / afSampleRate; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 74 | ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d", |
| 75 | *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency); |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 76 | return NO_ERROR; |
| 77 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
| 79 | // --------------------------------------------------------------------------- |
| 80 | |
| 81 | AudioTrack::AudioTrack() |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 82 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 83 | mIsTimed(false), |
| 84 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 85 | mPreviousSchedulingGroup(SP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
| 89 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 90 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 92 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 93 | audio_channel_mask_t channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | int frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 95 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | callback_t cbf, |
| 97 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 98 | int notificationFrames, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 99 | int sessionId, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 100 | transfer_type transferType, |
| 101 | const audio_offload_info_t *offloadInfo) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 102 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 103 | mIsTimed(false), |
| 104 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 105 | mPreviousSchedulingGroup(SP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 107 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 108 | frameCount, flags, cbf, user, notificationFrames, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 109 | 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 112 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 113 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 115 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 116 | audio_channel_mask_t channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 118 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | callback_t cbf, |
| 120 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 121 | int notificationFrames, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 122 | int sessionId, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 123 | transfer_type transferType, |
| 124 | const audio_offload_info_t *offloadInfo) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 125 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 126 | mIsTimed(false), |
| 127 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 128 | mPreviousSchedulingGroup(SP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 130 | mStatus = set(streamType, sampleRate, format, channelMask, |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 131 | 0 /*frameCount*/, flags, cbf, user, notificationFrames, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 132 | sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | AudioTrack::~AudioTrack() |
| 136 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | if (mStatus == NO_ERROR) { |
| 138 | // Make sure that callback function exits in the case where |
| 139 | // it is looping on buffer full condition in obtainBuffer(). |
| 140 | // Otherwise the callback thread will never exit. |
| 141 | stop(); |
| 142 | if (mAudioTrackThread != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 143 | mAudioTrackThread->requestExit(); // see comment in AudioTrack.h |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mAudioTrackThread->requestExitAndWait(); |
| 145 | mAudioTrackThread.clear(); |
| 146 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 147 | if (mAudioTrack != 0) { |
| 148 | mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this); |
| 149 | mAudioTrack.clear(); |
| 150 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | IPCThreadState::self()->flushCommands(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 152 | AudioSystem::releaseAudioSessionId(mSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | status_t AudioTrack::set( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 157 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 159 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 160 | audio_channel_mask_t channelMask, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 161 | int frameCountInt, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 162 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | callback_t cbf, |
| 164 | void* user, |
| 165 | int notificationFrames, |
| 166 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 167 | bool threadCanCallJava, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 168 | int sessionId, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 169 | transfer_type transferType, |
| 170 | const audio_offload_info_t *offloadInfo) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 172 | switch (transferType) { |
| 173 | case TRANSFER_DEFAULT: |
| 174 | if (sharedBuffer != 0) { |
| 175 | transferType = TRANSFER_SHARED; |
| 176 | } else if (cbf == NULL || threadCanCallJava) { |
| 177 | transferType = TRANSFER_SYNC; |
| 178 | } else { |
| 179 | transferType = TRANSFER_CALLBACK; |
| 180 | } |
| 181 | break; |
| 182 | case TRANSFER_CALLBACK: |
| 183 | if (cbf == NULL || sharedBuffer != 0) { |
| 184 | ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0"); |
| 185 | return BAD_VALUE; |
| 186 | } |
| 187 | break; |
| 188 | case TRANSFER_OBTAIN: |
| 189 | case TRANSFER_SYNC: |
| 190 | if (sharedBuffer != 0) { |
| 191 | ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0"); |
| 192 | return BAD_VALUE; |
| 193 | } |
| 194 | break; |
| 195 | case TRANSFER_SHARED: |
| 196 | if (sharedBuffer == 0) { |
| 197 | ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0"); |
| 198 | return BAD_VALUE; |
| 199 | } |
| 200 | break; |
| 201 | default: |
| 202 | ALOGE("Invalid transfer type %d", transferType); |
| 203 | return BAD_VALUE; |
| 204 | } |
| 205 | mTransfer = transferType; |
| 206 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 207 | // FIXME "int" here is legacy and will be replaced by size_t later |
| 208 | if (frameCountInt < 0) { |
| 209 | ALOGE("Invalid frame count %d", frameCountInt); |
| 210 | return BAD_VALUE; |
| 211 | } |
| 212 | size_t frameCount = frameCountInt; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 214 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), |
| 215 | sharedBuffer->size()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 217 | ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 218 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 219 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 220 | |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 221 | if (mAudioTrack != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 222 | ALOGE("Track already in use"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | return INVALID_OPERATION; |
| 224 | } |
| 225 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | // handle default values first. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 227 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 228 | streamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 230 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | if (sampleRate == 0) { |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 232 | uint32_t afSampleRate; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 233 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 234 | return NO_INIT; |
| 235 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | sampleRate = afSampleRate; |
| 237 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 238 | mSampleRate = sampleRate; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | // these below should probably come from the audioFlinger too... |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 241 | if (format == AUDIO_FORMAT_DEFAULT) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 242 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 244 | if (channelMask == 0) { |
| 245 | channelMask = AUDIO_CHANNEL_OUT_STEREO; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // validate parameters |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 249 | if (!audio_is_valid_format(format)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 250 | ALOGE("Invalid format %d", format); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | return BAD_VALUE; |
| 252 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 253 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 254 | // AudioFlinger does not currently support 8-bit data in shared memory |
| 255 | if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) { |
| 256 | ALOGE("8-bit data in shared memory is not supported"); |
| 257 | return BAD_VALUE; |
| 258 | } |
| 259 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 260 | // force direct flag if format is not linear PCM |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 261 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 262 | flags = (audio_output_flags_t) |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 263 | // FIXME why can't we allow direct AND fast? |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 264 | ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 265 | } |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 266 | // only allow deep buffering for music stream type |
| 267 | if (streamType != AUDIO_STREAM_MUSIC) { |
| 268 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 269 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 270 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 271 | if (!audio_is_output_channel(channelMask)) { |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 272 | ALOGE("Invalid channel mask %#x", channelMask); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 273 | return BAD_VALUE; |
| 274 | } |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 275 | mChannelMask = channelMask; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 276 | uint32_t channelCount = popcount(channelMask); |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 277 | mChannelCount = channelCount; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 278 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 279 | if (audio_is_linear_pcm(format)) { |
| 280 | mFrameSize = channelCount * audio_bytes_per_sample(format); |
| 281 | mFrameSizeAF = channelCount * sizeof(int16_t); |
| 282 | } else { |
| 283 | mFrameSize = sizeof(uint8_t); |
| 284 | mFrameSizeAF = sizeof(uint8_t); |
| 285 | } |
| 286 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 287 | audio_io_handle_t output = AudioSystem::getOutput( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 288 | streamType, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 289 | sampleRate, format, channelMask, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 290 | flags, |
| 291 | offloadInfo); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 292 | |
| 293 | if (output == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 294 | ALOGE("Could not get audio output for stream type %d", streamType); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | return BAD_VALUE; |
| 296 | } |
| 297 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 298 | mVolume[LEFT] = 1.0f; |
| 299 | mVolume[RIGHT] = 1.0f; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 300 | mSendLevel = 0.0f; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 301 | mFrameCount = frameCount; |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 302 | mReqFrameCount = frameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 303 | mNotificationFramesReq = notificationFrames; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 304 | mNotificationFramesAct = 0; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 305 | mSessionId = sessionId; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 306 | mAuxEffectId = 0; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 307 | mFlags = flags; |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 308 | mCbf = cbf; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 309 | |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 310 | if (cbf != NULL) { |
Eric Laurent | 896adcd | 2012-09-13 11:18:23 -0700 | [diff] [blame] | 311 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 312 | mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/); |
| 313 | } |
| 314 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 315 | // create the IAudioTrack |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 316 | status_t status = createTrack_l(streamType, |
| 317 | sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 318 | format, |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 319 | frameCount, |
| 320 | flags, |
| 321 | sharedBuffer, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 322 | output, |
| 323 | 0 /*epoch*/); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 324 | |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 325 | if (status != NO_ERROR) { |
| 326 | if (mAudioTrackThread != 0) { |
| 327 | mAudioTrackThread->requestExit(); |
| 328 | mAudioTrackThread.clear(); |
| 329 | } |
| 330 | return status; |
Glenn Kasten | 5d464eb | 2012-06-22 17:19:53 -0700 | [diff] [blame] | 331 | } |
| 332 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | mStatus = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | mStreamType = streamType; |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 335 | mFormat = format; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | mSharedBuffer = sharedBuffer; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 337 | mState = STATE_STOPPED; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | mUserData = user; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 339 | mLoopPeriod = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | mMarkerPosition = 0; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 341 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | mNewPosition = 0; |
| 343 | mUpdatePeriod = 0; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 344 | AudioSystem::acquireAudioSessionId(mSessionId); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 345 | mSequence = 1; |
| 346 | mObservedSequence = mSequence; |
| 347 | mInUnderrun = false; |
| 348 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | return NO_ERROR; |
| 350 | } |
| 351 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | // ------------------------------------------------------------------------- |
| 353 | |
| 354 | void AudioTrack::start() |
| 355 | { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 356 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 357 | if (mState == STATE_ACTIVE) { |
| 358 | return; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 361 | mInUnderrun = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 363 | State previousState = mState; |
| 364 | mState = STATE_ACTIVE; |
| 365 | if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) { |
| 366 | // reset current position as seen by client to 0 |
| 367 | mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition()); |
| 368 | } |
| 369 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
| 370 | int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->flags); |
| 371 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | sp<AudioTrackThread> t = mAudioTrackThread; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 373 | if (t != 0) { |
| 374 | t->resume(); |
| 375 | } else { |
| 376 | mPreviousPriority = getpriority(PRIO_PROCESS, 0); |
| 377 | get_sched_policy(0, &mPreviousSchedulingGroup); |
| 378 | androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); |
| 379 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 381 | status_t status = NO_ERROR; |
| 382 | if (!(flags & CBLK_INVALID)) { |
| 383 | status = mAudioTrack->start(); |
| 384 | if (status == DEAD_OBJECT) { |
| 385 | flags |= CBLK_INVALID; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 387 | } |
| 388 | if (flags & CBLK_INVALID) { |
| 389 | status = restoreTrack_l("start"); |
| 390 | } |
| 391 | |
| 392 | if (status != NO_ERROR) { |
| 393 | ALOGE("start() status %d", status); |
| 394 | mState = previousState; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | if (t != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 396 | t->pause(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 398 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 399 | set_sched_policy(0, mPreviousSchedulingGroup); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 403 | // FIXME discarding status |
| 404 | } |
| 405 | |
| 406 | void AudioTrack::stop() |
| 407 | { |
| 408 | AutoMutex lock(mLock); |
| 409 | // FIXME pause then stop should not be a nop |
| 410 | if (mState != STATE_ACTIVE) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | mState = STATE_STOPPED; |
| 415 | mProxy->interrupt(); |
| 416 | mAudioTrack->stop(); |
| 417 | // the playback head position will reset to 0, so if a marker is set, we need |
| 418 | // to activate it again |
| 419 | mMarkerReached = false; |
| 420 | #if 0 |
| 421 | // Force flush if a shared buffer is used otherwise audioflinger |
| 422 | // will not stop before end of buffer is reached. |
| 423 | // It may be needed to make sure that we stop playback, likely in case looping is on. |
| 424 | if (mSharedBuffer != 0) { |
| 425 | flush_l(); |
| 426 | } |
| 427 | #endif |
| 428 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 429 | if (t != 0) { |
| 430 | t->pause(); |
| 431 | } else { |
| 432 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
| 433 | set_sched_policy(0, mPreviousSchedulingGroup); |
| 434 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | bool AudioTrack::stopped() const |
| 438 | { |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 439 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 440 | return mState != STATE_ACTIVE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void AudioTrack::flush() |
| 444 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 445 | if (mSharedBuffer != 0) { |
| 446 | return; |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 447 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 448 | AutoMutex lock(mLock); |
| 449 | if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) { |
| 450 | return; |
| 451 | } |
| 452 | flush_l(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 455 | void AudioTrack::flush_l() |
| 456 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 457 | ALOG_ASSERT(mState != STATE_ACTIVE); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 458 | |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 459 | // clear playback marker and periodic update counter |
| 460 | mMarkerPosition = 0; |
| 461 | mMarkerReached = false; |
| 462 | mUpdatePeriod = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 463 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 464 | mState = STATE_FLUSHED; |
| 465 | mProxy->flush(); |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 466 | mAudioTrack->flush(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void AudioTrack::pause() |
| 470 | { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 471 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 472 | if (mState != STATE_ACTIVE) { |
| 473 | return; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 475 | mState = STATE_PAUSED; |
| 476 | mProxy->interrupt(); |
| 477 | mAudioTrack->pause(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 480 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | { |
Glenn Kasten | f0c4950 | 2011-11-30 09:46:04 -0800 | [diff] [blame] | 482 | if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 483 | return BAD_VALUE; |
| 484 | } |
| 485 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 486 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | mVolume[LEFT] = left; |
| 488 | mVolume[RIGHT] = right; |
| 489 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 490 | mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000)); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 491 | |
| 492 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Glenn Kasten | b1c0993 | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 495 | status_t AudioTrack::setVolume(float volume) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | { |
Glenn Kasten | b1c0993 | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 497 | return setVolume(volume, volume); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 500 | status_t AudioTrack::setAuxEffectSendLevel(float level) |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 501 | { |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 502 | if (level < 0.0f || level > 1.0f) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 503 | return BAD_VALUE; |
| 504 | } |
| 505 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 506 | AutoMutex lock(mLock); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 507 | mSendLevel = level; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 508 | mProxy->setSendLevel(level); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 509 | |
| 510 | return NO_ERROR; |
| 511 | } |
| 512 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 513 | void AudioTrack::getAuxEffectSendLevel(float* level) const |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 514 | { |
| 515 | if (level != NULL) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 516 | *level = mSendLevel; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 517 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 520 | status_t AudioTrack::setSampleRate(uint32_t rate) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 522 | if (mIsTimed) { |
| 523 | return INVALID_OPERATION; |
| 524 | } |
| 525 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 526 | uint32_t afSamplingRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) { |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 528 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | } |
| 530 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 531 | if (rate == 0 || rate > afSamplingRate*2 ) { |
| 532 | return BAD_VALUE; |
| 533 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 535 | AutoMutex lock(mLock); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 536 | mSampleRate = rate; |
| 537 | mProxy->setSampleRate(rate); |
| 538 | |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 539 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 542 | uint32_t AudioTrack::getSampleRate() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 544 | if (mIsTimed) { |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 545 | return 0; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 548 | AutoMutex lock(mLock); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 549 | return mSampleRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 553 | { |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 554 | if (mSharedBuffer == 0 || mIsTimed) { |
| 555 | return INVALID_OPERATION; |
| 556 | } |
| 557 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | if (loopCount == 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 559 | ; |
| 560 | } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount && |
| 561 | loopEnd - loopStart >= MIN_LOOP) { |
| 562 | ; |
| 563 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | return BAD_VALUE; |
| 565 | } |
| 566 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 567 | AutoMutex lock(mLock); |
| 568 | // See setPosition() regarding setting parameters such as loop points or position while active |
| 569 | if (mState == STATE_ACTIVE) { |
| 570 | return INVALID_OPERATION; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 571 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 572 | setLoop_l(loopStart, loopEnd, loopCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | return NO_ERROR; |
| 574 | } |
| 575 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 576 | void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 577 | { |
| 578 | // FIXME If setting a loop also sets position to start of loop, then |
| 579 | // this is correct. Otherwise it should be removed. |
| 580 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
| 581 | mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0; |
| 582 | mStaticProxy->setLoop(loopStart, loopEnd, loopCount); |
| 583 | } |
| 584 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 586 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 587 | if (mCbf == NULL) { |
| 588 | return INVALID_OPERATION; |
| 589 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 591 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | mMarkerPosition = marker; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 593 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | |
| 595 | return NO_ERROR; |
| 596 | } |
| 597 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 598 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 600 | if (marker == NULL) { |
| 601 | return BAD_VALUE; |
| 602 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 604 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | *marker = mMarkerPosition; |
| 606 | |
| 607 | return NO_ERROR; |
| 608 | } |
| 609 | |
| 610 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 611 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 612 | if (mCbf == NULL) { |
| 613 | return INVALID_OPERATION; |
| 614 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 616 | AutoMutex lock(mLock); |
| 617 | mNewPosition = mProxy->getPosition() + updatePeriod; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | mUpdatePeriod = updatePeriod; |
| 619 | |
| 620 | return NO_ERROR; |
| 621 | } |
| 622 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 623 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 625 | if (updatePeriod == NULL) { |
| 626 | return BAD_VALUE; |
| 627 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 629 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | *updatePeriod = mUpdatePeriod; |
| 631 | |
| 632 | return NO_ERROR; |
| 633 | } |
| 634 | |
| 635 | status_t AudioTrack::setPosition(uint32_t position) |
| 636 | { |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 637 | if (mSharedBuffer == 0 || mIsTimed) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 638 | return INVALID_OPERATION; |
| 639 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 640 | if (position > mFrameCount) { |
| 641 | return BAD_VALUE; |
| 642 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 643 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 644 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 645 | // Currently we require that the player is inactive before setting parameters such as position |
| 646 | // or loop points. Otherwise, there could be a race condition: the application could read the |
| 647 | // current position, compute a new position or loop parameters, and then set that position or |
| 648 | // loop parameters but it would do the "wrong" thing since the position has continued to advance |
| 649 | // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app |
| 650 | // to specify how it wants to handle such scenarios. |
| 651 | if (mState == STATE_ACTIVE) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 652 | return INVALID_OPERATION; |
| 653 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 654 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
| 655 | mLoopPeriod = 0; |
| 656 | // FIXME Check whether loops and setting position are incompatible in old code. |
| 657 | // If we use setLoop for both purposes we lose the capability to set the position while looping. |
| 658 | mStaticProxy->setLoop(position, mFrameCount, 0); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 659 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | return NO_ERROR; |
| 661 | } |
| 662 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 663 | status_t AudioTrack::getPosition(uint32_t *position) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 665 | if (position == NULL) { |
| 666 | return BAD_VALUE; |
| 667 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 668 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 669 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 670 | // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes |
| 671 | *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 : |
| 672 | mProxy->getPosition(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | |
| 674 | return NO_ERROR; |
| 675 | } |
| 676 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 677 | status_t AudioTrack::getBufferPosition(size_t *position) |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 678 | { |
| 679 | if (mSharedBuffer == 0 || mIsTimed) { |
| 680 | return INVALID_OPERATION; |
| 681 | } |
| 682 | if (position == NULL) { |
| 683 | return BAD_VALUE; |
| 684 | } |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 685 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 686 | AutoMutex lock(mLock); |
| 687 | *position = mStaticProxy->getBufferPosition(); |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 688 | return NO_ERROR; |
| 689 | } |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 690 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | status_t AudioTrack::reload() |
| 692 | { |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 693 | if (mSharedBuffer == 0 || mIsTimed) { |
| 694 | return INVALID_OPERATION; |
| 695 | } |
| 696 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 697 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 698 | // See setPosition() regarding setting parameters such as loop points or position while active |
| 699 | if (mState == STATE_ACTIVE) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 700 | return INVALID_OPERATION; |
| 701 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 702 | mNewPosition = mUpdatePeriod; |
| 703 | mLoopPeriod = 0; |
| 704 | // FIXME The new code cannot reload while keeping a loop specified. |
| 705 | // Need to check how the old code handled this, and whether it's a significant change. |
| 706 | mStaticProxy->setLoop(0, mFrameCount, 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | return NO_ERROR; |
| 708 | } |
| 709 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 710 | audio_io_handle_t AudioTrack::getOutput() |
| 711 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 712 | AutoMutex lock(mLock); |
| 713 | return getOutput_l(); |
| 714 | } |
| 715 | |
| 716 | // must be called with mLock held |
| 717 | audio_io_handle_t AudioTrack::getOutput_l() |
| 718 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 719 | return AudioSystem::getOutput(mStreamType, |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 720 | mSampleRate, mFormat, mChannelMask, mFlags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 723 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 724 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 725 | AutoMutex lock(mLock); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 726 | status_t status = mAudioTrack->attachAuxEffect(effectId); |
| 727 | if (status == NO_ERROR) { |
| 728 | mAuxEffectId = effectId; |
| 729 | } |
| 730 | return status; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 731 | } |
| 732 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | // ------------------------------------------------------------------------- |
| 734 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 735 | // must be called with mLock held |
| 736 | status_t AudioTrack::createTrack_l( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 737 | audio_stream_type_t streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 738 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 739 | audio_format_t format, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 740 | size_t frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 741 | audio_output_flags_t flags, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 742 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 743 | audio_io_handle_t output, |
| 744 | size_t epoch) |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 745 | { |
| 746 | status_t status; |
| 747 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 748 | if (audioFlinger == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 749 | ALOGE("Could not get audioflinger"); |
| 750 | return NO_INIT; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 753 | uint32_t afLatency; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 754 | if ((status = AudioSystem::getLatency(output, streamType, &afLatency)) != NO_ERROR) { |
| 755 | ALOGE("getLatency(%d) failed status %d", output, status); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 756 | return NO_INIT; |
| 757 | } |
| 758 | |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 759 | // Client decides whether the track is TIMED (see below), but can only express a preference |
| 760 | // for FAST. Server will perform additional tests. |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 761 | if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !( |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 762 | // either of these use cases: |
| 763 | // use case 1: shared buffer |
| 764 | (sharedBuffer != 0) || |
| 765 | // use case 2: callback handler |
| 766 | (mCbf != NULL))) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 767 | ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client"); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 768 | // once denied, do not request again if IAudioTrack is re-created |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 769 | flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 770 | mFlags = flags; |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 771 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 772 | ALOGV("createTrack_l() output %d afLatency %d", output, afLatency); |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 773 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 774 | mNotificationFramesAct = mNotificationFramesReq; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 775 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 776 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 777 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 778 | if (sharedBuffer != 0) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 779 | // Same comment as below about ignoring frameCount parameter for set() |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 780 | frameCount = sharedBuffer->size(); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 781 | } else if (frameCount == 0) { |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 782 | size_t afFrameCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 783 | status = AudioSystem::getFrameCount(output, streamType, &afFrameCount); |
| 784 | if (status != NO_ERROR) { |
| 785 | ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, |
| 786 | status); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 787 | return NO_INIT; |
| 788 | } |
| 789 | frameCount = afFrameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 790 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 791 | |
| 792 | } else if (sharedBuffer != 0) { |
| 793 | |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 794 | // Ensure that buffer alignment matches channel count |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 795 | // 8-bit data in shared memory is not currently supported by AudioFlinger |
| 796 | size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2; |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 797 | if (mChannelCount > 1) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 798 | // More than 2 channels does not require stronger alignment than stereo |
| 799 | alignment <<= 1; |
| 800 | } |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 801 | if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) { |
| 802 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
| 803 | sharedBuffer->pointer(), mChannelCount); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 804 | return BAD_VALUE; |
| 805 | } |
| 806 | |
| 807 | // When initializing a shared buffer AudioTrack via constructors, |
| 808 | // there's no frameCount parameter. |
| 809 | // But when initializing a shared buffer AudioTrack via set(), |
| 810 | // there _is_ a frameCount parameter. We silently ignore it. |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 811 | frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 812 | |
| 813 | } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 814 | |
| 815 | // FIXME move these calculations and associated checks to server |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 816 | uint32_t afSampleRate; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 817 | status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate); |
| 818 | if (status != NO_ERROR) { |
| 819 | ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, |
| 820 | status); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 821 | return NO_INIT; |
| 822 | } |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 823 | size_t afFrameCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 824 | status = AudioSystem::getFrameCount(output, streamType, &afFrameCount); |
| 825 | if (status != NO_ERROR) { |
| 826 | ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 827 | return NO_INIT; |
| 828 | } |
| 829 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 830 | // Ensure that buffer depth covers at least audio hardware latency |
| 831 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
Glenn Kasten | bb6f0a0 | 2013-06-03 15:00:29 -0700 | [diff] [blame] | 832 | ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d", |
| 833 | afFrameCount, minBufCount, afSampleRate, afLatency); |
| 834 | if (minBufCount <= 2) { |
| 835 | minBufCount = sampleRate == afSampleRate ? 2 : 3; |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 836 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 837 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 838 | size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; |
| 839 | ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u" |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 840 | ", afLatency=%d", |
| 841 | minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 842 | |
| 843 | if (frameCount == 0) { |
| 844 | frameCount = minFrameCount; |
| 845 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 846 | // Make sure that application is notified with sufficient margin |
| 847 | // before underrun |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 848 | if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 849 | mNotificationFramesAct = frameCount/2; |
| 850 | } |
| 851 | if (frameCount < minFrameCount) { |
| 852 | // not ALOGW because it happens all the time when playing key clicks over A2DP |
| 853 | ALOGV("Minimum buffer size corrected from %d to %d", |
| 854 | frameCount, minFrameCount); |
| 855 | frameCount = minFrameCount; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 856 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 857 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 858 | } else { |
| 859 | // For fast tracks, the frame count calculations and checks are done by server |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 862 | IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT; |
| 863 | if (mIsTimed) { |
| 864 | trackFlags |= IAudioFlinger::TRACK_TIMED; |
| 865 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 866 | |
| 867 | pid_t tid = -1; |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 868 | if (flags & AUDIO_OUTPUT_FLAG_FAST) { |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 869 | trackFlags |= IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 870 | if (mAudioTrackThread != 0) { |
| 871 | tid = mAudioTrackThread->getTid(); |
| 872 | } |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 875 | sp<IAudioTrack> track = audioFlinger->createTrack(streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 876 | sampleRate, |
Glenn Kasten | 60a8392 | 2012-06-21 12:56:37 -0700 | [diff] [blame] | 877 | // AudioFlinger only sees 16-bit PCM |
| 878 | format == AUDIO_FORMAT_PCM_8_BIT ? |
| 879 | AUDIO_FORMAT_PCM_16_BIT : format, |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 880 | mChannelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 881 | frameCount, |
Glenn Kasten | e0b0717 | 2012-11-06 15:03:34 -0800 | [diff] [blame] | 882 | &trackFlags, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 883 | sharedBuffer, |
| 884 | output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 885 | tid, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 886 | &mSessionId, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 887 | &status); |
| 888 | |
| 889 | if (track == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 890 | ALOGE("AudioFlinger could not create track, status: %d", status); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 891 | return status; |
| 892 | } |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 893 | sp<IMemory> iMem = track->getCblk(); |
| 894 | if (iMem == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 895 | ALOGE("Could not get control block"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 896 | return NO_INIT; |
| 897 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 898 | if (mAudioTrack != 0) { |
| 899 | mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this); |
| 900 | mDeathNotifier.clear(); |
| 901 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 902 | mAudioTrack = track; |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 903 | mCblkMemory = iMem; |
| 904 | audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer()); |
| 905 | mCblk = cblk; |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 906 | size_t temp = cblk->frameCount_; |
| 907 | if (temp < frameCount || (frameCount == 0 && temp == 0)) { |
| 908 | // In current design, AudioTrack client checks and ensures frame count validity before |
| 909 | // passing it to AudioFlinger so AudioFlinger should not return a different value except |
| 910 | // for fast track as it uses a special method of assigning frame count. |
| 911 | ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp); |
| 912 | } |
| 913 | frameCount = temp; |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 914 | mAwaitBoost = false; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 915 | if (flags & AUDIO_OUTPUT_FLAG_FAST) { |
Glenn Kasten | e0b0717 | 2012-11-06 15:03:34 -0800 | [diff] [blame] | 916 | if (trackFlags & IAudioFlinger::TRACK_FAST) { |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 917 | ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount); |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 918 | mAwaitBoost = true; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 919 | if (sharedBuffer == 0) { |
| 920 | // double-buffering is not required for fast tracks, due to tighter scheduling |
| 921 | if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) { |
| 922 | mNotificationFramesAct = frameCount; |
| 923 | } |
| 924 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 925 | } else { |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 926 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 927 | // once denied, do not request again if IAudioTrack is re-created |
| 928 | flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 929 | mFlags = flags; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 930 | if (sharedBuffer == 0) { |
| 931 | if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) { |
| 932 | mNotificationFramesAct = frameCount/2; |
| 933 | } |
| 934 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 935 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 936 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 937 | mRefreshRemaining = true; |
| 938 | |
| 939 | // Starting address of buffers in shared memory. If there is a shared buffer, buffers |
| 940 | // is the value of pointer() for the shared buffer, otherwise buffers points |
| 941 | // immediately after the control block. This address is for the mapping within client |
| 942 | // address space. AudioFlinger::TrackBase::mBuffer is for the server address space. |
| 943 | void* buffers; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 944 | if (sharedBuffer == 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 945 | buffers = (char*)cblk + sizeof(audio_track_cblk_t); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 946 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 947 | buffers = sharedBuffer->pointer(); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 950 | mAudioTrack->attachAuxEffect(mAuxEffectId); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 951 | // FIXME don't believe this lie |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 952 | mLatency = afLatency + (1000*frameCount) / sampleRate; |
| 953 | mFrameCount = frameCount; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 954 | // If IAudioTrack is re-created, don't let the requested frameCount |
| 955 | // decrease. This can confuse clients that cache frameCount(). |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 956 | if (frameCount > mReqFrameCount) { |
| 957 | mReqFrameCount = frameCount; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 958 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 959 | |
| 960 | // update proxy |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 961 | if (sharedBuffer == 0) { |
| 962 | mStaticProxy.clear(); |
| 963 | mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF); |
| 964 | } else { |
| 965 | mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF); |
| 966 | mProxy = mStaticProxy; |
| 967 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 968 | mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | |
| 969 | uint16_t(mVolume[LEFT] * 0x1000)); |
| 970 | mProxy->setSendLevel(mSendLevel); |
| 971 | mProxy->setSampleRate(mSampleRate); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 972 | mProxy->setEpoch(epoch); |
| 973 | mProxy->setMinimum(mNotificationFramesAct); |
| 974 | |
| 975 | mDeathNotifier = new DeathNotifier(this); |
| 976 | mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 977 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 978 | return NO_ERROR; |
| 979 | } |
| 980 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 982 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 983 | if (audioBuffer == NULL) { |
| 984 | return BAD_VALUE; |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 985 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 986 | if (mTransfer != TRANSFER_OBTAIN) { |
| 987 | audioBuffer->frameCount = 0; |
| 988 | audioBuffer->size = 0; |
| 989 | audioBuffer->raw = NULL; |
| 990 | return INVALID_OPERATION; |
| 991 | } |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 992 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 993 | const struct timespec *requested; |
| 994 | if (waitCount == -1) { |
| 995 | requested = &ClientProxy::kForever; |
| 996 | } else if (waitCount == 0) { |
| 997 | requested = &ClientProxy::kNonBlocking; |
| 998 | } else if (waitCount > 0) { |
| 999 | long long ms = WAIT_PERIOD_MS * (long long) waitCount; |
| 1000 | struct timespec timeout; |
| 1001 | timeout.tv_sec = ms / 1000; |
| 1002 | timeout.tv_nsec = (int) (ms % 1000) * 1000000; |
| 1003 | requested = &timeout; |
| 1004 | } else { |
| 1005 | ALOGE("%s invalid waitCount %d", __func__, waitCount); |
| 1006 | requested = NULL; |
| 1007 | } |
| 1008 | return obtainBuffer(audioBuffer, requested); |
| 1009 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1010 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1011 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested, |
| 1012 | struct timespec *elapsed, size_t *nonContig) |
| 1013 | { |
| 1014 | // previous and new IAudioTrack sequence numbers are used to detect track re-creation |
| 1015 | uint32_t oldSequence = 0; |
| 1016 | uint32_t newSequence; |
| 1017 | |
| 1018 | Proxy::Buffer buffer; |
| 1019 | status_t status = NO_ERROR; |
| 1020 | |
| 1021 | static const int32_t kMaxTries = 5; |
| 1022 | int32_t tryCounter = kMaxTries; |
| 1023 | |
| 1024 | do { |
| 1025 | // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to |
| 1026 | // keep them from going away if another thread re-creates the track during obtainBuffer() |
| 1027 | sp<AudioTrackClientProxy> proxy; |
| 1028 | sp<IMemory> iMem; |
| 1029 | |
| 1030 | { // start of lock scope |
| 1031 | AutoMutex lock(mLock); |
| 1032 | |
| 1033 | newSequence = mSequence; |
| 1034 | // did previous obtainBuffer() fail due to media server death or voluntary invalidation? |
| 1035 | if (status == DEAD_OBJECT) { |
| 1036 | // re-create track, unless someone else has already done so |
| 1037 | if (newSequence == oldSequence) { |
| 1038 | status = restoreTrack_l("obtainBuffer"); |
| 1039 | if (status != NO_ERROR) { |
| 1040 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | } |
| 1043 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1044 | oldSequence = newSequence; |
| 1045 | |
| 1046 | // Keep the extra references |
| 1047 | proxy = mProxy; |
| 1048 | iMem = mCblkMemory; |
| 1049 | |
| 1050 | // Non-blocking if track is stopped or paused |
| 1051 | if (mState != STATE_ACTIVE) { |
| 1052 | requested = &ClientProxy::kNonBlocking; |
| 1053 | } |
| 1054 | |
| 1055 | } // end of lock scope |
| 1056 | |
| 1057 | buffer.mFrameCount = audioBuffer->frameCount; |
| 1058 | // FIXME starts the requested timeout and elapsed over from scratch |
| 1059 | status = proxy->obtainBuffer(&buffer, requested, elapsed); |
| 1060 | |
| 1061 | } while ((status == DEAD_OBJECT) && (tryCounter-- > 0)); |
| 1062 | |
| 1063 | audioBuffer->frameCount = buffer.mFrameCount; |
| 1064 | audioBuffer->size = buffer.mFrameCount * mFrameSizeAF; |
| 1065 | audioBuffer->raw = buffer.mRaw; |
| 1066 | if (nonContig != NULL) { |
| 1067 | *nonContig = buffer.mNonContig; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1069 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 1073 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1074 | if (mTransfer == TRANSFER_SHARED) { |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | size_t stepCount = audioBuffer->size / mFrameSizeAF; |
| 1079 | if (stepCount == 0) { |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | Proxy::Buffer buffer; |
| 1084 | buffer.mFrameCount = stepCount; |
| 1085 | buffer.mRaw = audioBuffer->raw; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1086 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1087 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1088 | mInUnderrun = false; |
| 1089 | mProxy->releaseBuffer(&buffer); |
| 1090 | |
| 1091 | // restart track if it was disabled by audioflinger due to previous underrun |
| 1092 | if (mState == STATE_ACTIVE) { |
| 1093 | audio_track_cblk_t* cblk = mCblk; |
| 1094 | if (android_atomic_and(~CBLK_DISABLED, &cblk->flags) & CBLK_DISABLED) { |
| 1095 | ALOGW("releaseBuffer() track %p name=%#x disabled due to previous underrun, restarting", |
| 1096 | this, cblk->mName); |
| 1097 | // FIXME ignoring status |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1098 | mAudioTrack->start(); |
| 1099 | } |
| 1100 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | // ------------------------------------------------------------------------- |
| 1104 | |
| 1105 | ssize_t AudioTrack::write(const void* buffer, size_t userSize) |
| 1106 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1107 | if (mTransfer != TRANSFER_SYNC || mIsTimed) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1108 | return INVALID_OPERATION; |
| 1109 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1111 | if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) { |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1112 | // Sanity-check: user is most-likely passing an error code, and it would |
| 1113 | // make the return value ambiguous (actualSize vs error). |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1114 | ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | return BAD_VALUE; |
| 1116 | } |
| 1117 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1118 | size_t written = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | Buffer audioBuffer; |
| 1120 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1121 | while (userSize >= mFrameSize) { |
| 1122 | audioBuffer.frameCount = userSize / mFrameSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1123 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1124 | status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | if (err < 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1126 | if (written > 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | break; |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1128 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | return ssize_t(err); |
| 1130 | } |
| 1131 | |
| 1132 | size_t toWrite; |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1133 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | // Divide capacity by 2 to take expansion into account |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1135 | toWrite = audioBuffer.size >> 1; |
| 1136 | memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite); |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1137 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | toWrite = audioBuffer.size; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1139 | memcpy(audioBuffer.i8, buffer, toWrite); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1141 | buffer = ((const char *) buffer) + toWrite; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | userSize -= toWrite; |
| 1143 | written += toWrite; |
| 1144 | |
| 1145 | releaseBuffer(&audioBuffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1146 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | |
| 1148 | return written; |
| 1149 | } |
| 1150 | |
| 1151 | // ------------------------------------------------------------------------- |
| 1152 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1153 | TimedAudioTrack::TimedAudioTrack() { |
| 1154 | mIsTimed = true; |
| 1155 | } |
| 1156 | |
| 1157 | status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer) |
| 1158 | { |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 1159 | AutoMutex lock(mLock); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1160 | status_t result = UNKNOWN_ERROR; |
| 1161 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1162 | #if 1 |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 1163 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1164 | // while we are accessing the cblk |
| 1165 | sp<IAudioTrack> audioTrack = mAudioTrack; |
| 1166 | sp<IMemory> iMem = mCblkMemory; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1167 | #endif |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 1168 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1169 | // If the track is not invalid already, try to allocate a buffer. alloc |
| 1170 | // fails indicating that the server is dead, flag the track as invalid so |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 1171 | // we can attempt to restore in just a bit. |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1172 | audio_track_cblk_t* cblk = mCblk; |
| 1173 | if (!(cblk->flags & CBLK_INVALID)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1174 | result = mAudioTrack->allocateTimedBuffer(size, buffer); |
| 1175 | if (result == DEAD_OBJECT) { |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1176 | android_atomic_or(CBLK_INVALID, &cblk->flags); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | // If the track is invalid at this point, attempt to restore it. and try the |
| 1181 | // allocation one more time. |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1182 | if (cblk->flags & CBLK_INVALID) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1183 | result = restoreTrack_l("allocateTimedBuffer"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1184 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1185 | if (result == NO_ERROR) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1186 | result = mAudioTrack->allocateTimedBuffer(size, buffer); |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1187 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | return result; |
| 1191 | } |
| 1192 | |
| 1193 | status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer, |
| 1194 | int64_t pts) |
| 1195 | { |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1196 | status_t status = mAudioTrack->queueTimedBuffer(buffer, pts); |
| 1197 | { |
| 1198 | AutoMutex lock(mLock); |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1199 | audio_track_cblk_t* cblk = mCblk; |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1200 | // restart track if it was disabled by audioflinger due to previous underrun |
| 1201 | if (buffer->size() != 0 && status == NO_ERROR && |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1202 | (mState == STATE_ACTIVE) && (cblk->flags & CBLK_DISABLED)) { |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1203 | android_atomic_and(~CBLK_DISABLED, &cblk->flags); |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1204 | ALOGW("queueTimedBuffer() track %p disabled, restarting", this); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1205 | // FIXME ignoring status |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1206 | mAudioTrack->start(); |
| 1207 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1208 | } |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1209 | return status; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform, |
| 1213 | TargetTimeline target) |
| 1214 | { |
| 1215 | return mAudioTrack->setMediaTimeTransform(xform, target); |
| 1216 | } |
| 1217 | |
| 1218 | // ------------------------------------------------------------------------- |
| 1219 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1220 | nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1222 | mLock.lock(); |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 1223 | if (mAwaitBoost) { |
| 1224 | mAwaitBoost = false; |
| 1225 | mLock.unlock(); |
| 1226 | static const int32_t kMaxTries = 5; |
| 1227 | int32_t tryCounter = kMaxTries; |
| 1228 | uint32_t pollUs = 10000; |
| 1229 | do { |
| 1230 | int policy = sched_getscheduler(0); |
| 1231 | if (policy == SCHED_FIFO || policy == SCHED_RR) { |
| 1232 | break; |
| 1233 | } |
| 1234 | usleep(pollUs); |
| 1235 | pollUs <<= 1; |
| 1236 | } while (tryCounter-- > 0); |
| 1237 | if (tryCounter < 0) { |
| 1238 | ALOGE("did not receive expected priority boost on time"); |
| 1239 | } |
| 1240 | return true; |
| 1241 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1242 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1243 | // Can only reference mCblk while locked |
| 1244 | int32_t flags = android_atomic_and( |
| 1245 | ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->flags); |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1246 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1247 | // Check for track invalidation |
| 1248 | if (flags & CBLK_INVALID) { |
| 1249 | (void) restoreTrack_l("processAudioBuffer"); |
| 1250 | mLock.unlock(); |
| 1251 | // Run again immediately, but with a new IAudioTrack |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | bool active = mState == STATE_ACTIVE; |
| 1256 | |
| 1257 | // Manage underrun callback, must be done under lock to avoid race with releaseBuffer() |
| 1258 | bool newUnderrun = false; |
| 1259 | if (flags & CBLK_UNDERRUN) { |
| 1260 | #if 0 |
| 1261 | // Currently in shared buffer mode, when the server reaches the end of buffer, |
| 1262 | // the track stays active in continuous underrun state. It's up to the application |
| 1263 | // to pause or stop the track, or set the position to a new offset within buffer. |
| 1264 | // This was some experimental code to auto-pause on underrun. Keeping it here |
| 1265 | // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content. |
| 1266 | if (mTransfer == TRANSFER_SHARED) { |
| 1267 | mState = STATE_PAUSED; |
| 1268 | active = false; |
| 1269 | } |
| 1270 | #endif |
| 1271 | if (!mInUnderrun) { |
| 1272 | mInUnderrun = true; |
| 1273 | newUnderrun = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | } |
| 1275 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1276 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1277 | // Get current position of server |
| 1278 | size_t position = mProxy->getPosition(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | |
| 1280 | // Manage marker callback |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1281 | bool markerReached = false; |
| 1282 | size_t markerPosition = mMarkerPosition; |
| 1283 | // FIXME fails for wraparound, need 64 bits |
| 1284 | if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) { |
| 1285 | mMarkerReached = markerReached = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1288 | // Determine number of new position callback(s) that will be needed, while locked |
| 1289 | size_t newPosCount = 0; |
| 1290 | size_t newPosition = mNewPosition; |
| 1291 | size_t updatePeriod = mUpdatePeriod; |
| 1292 | // FIXME fails for wraparound, need 64 bits |
| 1293 | if (updatePeriod > 0 && position >= newPosition) { |
| 1294 | newPosCount = ((position - newPosition) / updatePeriod) + 1; |
| 1295 | mNewPosition += updatePeriod * newPosCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1298 | // Cache other fields that will be needed soon |
| 1299 | uint32_t loopPeriod = mLoopPeriod; |
| 1300 | uint32_t sampleRate = mSampleRate; |
| 1301 | size_t notificationFrames = mNotificationFramesAct; |
| 1302 | if (mRefreshRemaining) { |
| 1303 | mRefreshRemaining = false; |
| 1304 | mRemainingFrames = notificationFrames; |
| 1305 | mRetryOnPartialBuffer = false; |
| 1306 | } |
| 1307 | size_t misalignment = mProxy->getMisalignment(); |
| 1308 | int32_t sequence = mSequence; |
| 1309 | |
| 1310 | // These fields don't need to be cached, because they are assigned only by set(): |
| 1311 | // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags |
| 1312 | // mFlags is also assigned by createTrack_l(), but not the bit we care about. |
| 1313 | |
| 1314 | mLock.unlock(); |
| 1315 | |
| 1316 | // perform callbacks while unlocked |
| 1317 | if (newUnderrun) { |
| 1318 | mCbf(EVENT_UNDERRUN, mUserData, NULL); |
| 1319 | } |
| 1320 | // FIXME we will miss loops if loop cycle was signaled several times since last call |
| 1321 | // to processAudioBuffer() |
| 1322 | if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) { |
| 1323 | mCbf(EVENT_LOOP_END, mUserData, NULL); |
| 1324 | } |
| 1325 | if (flags & CBLK_BUFFER_END) { |
| 1326 | mCbf(EVENT_BUFFER_END, mUserData, NULL); |
| 1327 | } |
| 1328 | if (markerReached) { |
| 1329 | mCbf(EVENT_MARKER, mUserData, &markerPosition); |
| 1330 | } |
| 1331 | while (newPosCount > 0) { |
| 1332 | size_t temp = newPosition; |
| 1333 | mCbf(EVENT_NEW_POS, mUserData, &temp); |
| 1334 | newPosition += updatePeriod; |
| 1335 | newPosCount--; |
| 1336 | } |
| 1337 | if (mObservedSequence != sequence) { |
| 1338 | mObservedSequence = sequence; |
| 1339 | mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1342 | // if inactive, then don't run me again until re-started |
| 1343 | if (!active) { |
| 1344 | return NS_INACTIVE; |
Eric Laurent | 2267ba1 | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1347 | // Compute the estimated time until the next timed event (position, markers, loops) |
| 1348 | // FIXME only for non-compressed audio |
| 1349 | uint32_t minFrames = ~0; |
| 1350 | if (!markerReached && position < markerPosition) { |
| 1351 | minFrames = markerPosition - position; |
| 1352 | } |
| 1353 | if (loopPeriod > 0 && loopPeriod < minFrames) { |
| 1354 | minFrames = loopPeriod; |
| 1355 | } |
| 1356 | if (updatePeriod > 0 && updatePeriod < minFrames) { |
| 1357 | minFrames = updatePeriod; |
| 1358 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1360 | // If > 0, poll periodically to recover from a stuck server. A good value is 2. |
| 1361 | static const uint32_t kPoll = 0; |
| 1362 | if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) { |
| 1363 | minFrames = kPoll * notificationFrames; |
| 1364 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1365 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1366 | // Convert frame units to time units |
| 1367 | nsecs_t ns = NS_WHENEVER; |
| 1368 | if (minFrames != (uint32_t) ~0) { |
| 1369 | // This "fudge factor" avoids soaking CPU, and compensates for late progress by server |
| 1370 | static const nsecs_t kFudgeNs = 10000000LL; // 10 ms |
| 1371 | ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs; |
| 1372 | } |
| 1373 | |
| 1374 | // If not supplying data by EVENT_MORE_DATA, then we're done |
| 1375 | if (mTransfer != TRANSFER_CALLBACK) { |
| 1376 | return ns; |
| 1377 | } |
| 1378 | |
| 1379 | struct timespec timeout; |
| 1380 | const struct timespec *requested = &ClientProxy::kForever; |
| 1381 | if (ns != NS_WHENEVER) { |
| 1382 | timeout.tv_sec = ns / 1000000000LL; |
| 1383 | timeout.tv_nsec = ns % 1000000000LL; |
| 1384 | ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000); |
| 1385 | requested = &timeout; |
| 1386 | } |
| 1387 | |
| 1388 | while (mRemainingFrames > 0) { |
| 1389 | |
| 1390 | Buffer audioBuffer; |
| 1391 | audioBuffer.frameCount = mRemainingFrames; |
| 1392 | size_t nonContig; |
| 1393 | status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig); |
| 1394 | LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0), |
| 1395 | "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount); |
| 1396 | requested = &ClientProxy::kNonBlocking; |
| 1397 | size_t avail = audioBuffer.frameCount + nonContig; |
| 1398 | ALOGV("obtainBuffer(%u) returned %u = %u + %u", |
| 1399 | mRemainingFrames, avail, audioBuffer.frameCount, nonContig); |
| 1400 | if (err != NO_ERROR) { |
| 1401 | if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR) { |
| 1402 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1404 | ALOGE("Error %d obtaining an audio buffer, giving up.", err); |
| 1405 | return NS_NEVER; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1407 | |
| 1408 | if (mRetryOnPartialBuffer) { |
| 1409 | mRetryOnPartialBuffer = false; |
| 1410 | if (avail < mRemainingFrames) { |
| 1411 | int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate; |
| 1412 | if (ns < 0 || myns < ns) { |
| 1413 | ns = myns; |
| 1414 | } |
| 1415 | return ns; |
| 1416 | } |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1417 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | |
| 1419 | // Divide buffer size by 2 to take into account the expansion |
| 1420 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 1421 | // of the destination buffer |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1422 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | audioBuffer.size >>= 1; |
| 1424 | } |
| 1425 | |
| 1426 | size_t reqSize = audioBuffer.size; |
| 1427 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1428 | size_t writtenSize = audioBuffer.size; |
| 1429 | size_t writtenFrames = writtenSize / mFrameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | |
| 1431 | // Sanity check on returned size |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1432 | if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) { |
| 1433 | ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes", |
| 1434 | reqSize, (int) writtenSize); |
| 1435 | return NS_NEVER; |
| 1436 | } |
| 1437 | |
| 1438 | if (writtenSize == 0) { |
The Android Open Source Project | 8555d08 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1439 | // The callback is done filling buffers |
| 1440 | // Keep this thread going to handle timed events and |
| 1441 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 1442 | // but don't just loop and block the CPU, so wait |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1443 | return WAIT_PERIOD_MS * 1000000LL; |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1444 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1446 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 1447 | // 8 to 16 bit conversion, note that source and destination are the same address |
| 1448 | memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1449 | audioBuffer.size <<= 1; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1452 | size_t releasedFrames = audioBuffer.size / mFrameSizeAF; |
| 1453 | audioBuffer.frameCount = releasedFrames; |
| 1454 | mRemainingFrames -= releasedFrames; |
| 1455 | if (misalignment >= releasedFrames) { |
| 1456 | misalignment -= releasedFrames; |
| 1457 | } else { |
| 1458 | misalignment = 0; |
| 1459 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | |
| 1461 | releaseBuffer(&audioBuffer); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1463 | // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer |
| 1464 | // if callback doesn't like to accept the full chunk |
| 1465 | if (writtenSize < reqSize) { |
| 1466 | continue; |
| 1467 | } |
| 1468 | |
| 1469 | // There could be enough non-contiguous frames available to satisfy the remaining request |
| 1470 | if (mRemainingFrames <= nonContig) { |
| 1471 | continue; |
| 1472 | } |
| 1473 | |
| 1474 | #if 0 |
| 1475 | // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a |
| 1476 | // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA |
| 1477 | // that total to a sum == notificationFrames. |
| 1478 | if (0 < misalignment && misalignment <= mRemainingFrames) { |
| 1479 | mRemainingFrames = misalignment; |
| 1480 | return (mRemainingFrames * 1100000000LL) / sampleRate; |
| 1481 | } |
| 1482 | #endif |
| 1483 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1485 | mRemainingFrames = notificationFrames; |
| 1486 | mRetryOnPartialBuffer = true; |
| 1487 | |
| 1488 | // A lot has transpired since ns was calculated, so run again immediately and re-calculate |
| 1489 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1492 | status_t AudioTrack::restoreTrack_l(const char *from) |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1493 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1494 | ALOGW("dead IAudioTrack, creating a new one from %s()", from); |
| 1495 | ++mSequence; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1496 | status_t result; |
| 1497 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1498 | // refresh the audio configuration cache in this process to make sure we get new |
| 1499 | // output parameters in getOutput_l() and createTrack_l() |
| 1500 | AudioSystem::clearAudioConfigCache(); |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1501 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1502 | // if the new IAudioTrack is created, createTrack_l() will modify the |
| 1503 | // following member variables: mAudioTrack, mCblkMemory and mCblk. |
| 1504 | // It will also delete the strong references on previous IAudioTrack and IMemory |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1505 | size_t position = mProxy->getPosition(); |
| 1506 | mNewPosition = position + mUpdatePeriod; |
| 1507 | size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0; |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1508 | result = createTrack_l(mStreamType, |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1509 | mSampleRate, |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1510 | mFormat, |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1511 | mReqFrameCount, // so that frame count never goes down |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1512 | mFlags, |
| 1513 | mSharedBuffer, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1514 | getOutput_l(), |
| 1515 | position /*epoch*/); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1516 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1517 | if (result == NO_ERROR) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1518 | // continue playback from last known position, but |
| 1519 | // don't attempt to restore loop after invalidation; it's difficult and not worthwhile |
| 1520 | if (mStaticProxy != NULL) { |
| 1521 | mLoopPeriod = 0; |
| 1522 | mStaticProxy->setLoop(bufferPosition, mFrameCount, 0); |
| 1523 | } |
| 1524 | // FIXME How do we simulate the fact that all frames present in the buffer at the time of |
| 1525 | // track destruction have been played? This is critical for SoundPool implementation |
| 1526 | // This must be broken, and needs to be tested/debugged. |
| 1527 | #if 0 |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1528 | // restore write index and set other indexes to reflect empty buffer status |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1529 | if (!strcmp(from, "start")) { |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1530 | // Make sure that a client relying on callback events indicating underrun or |
| 1531 | // the actual amount of audio frames played (e.g SoundPool) receives them. |
| 1532 | if (mSharedBuffer == 0) { |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1533 | // restart playback even if buffer is not completely filled. |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1534 | android_atomic_or(CBLK_FORCEREADY, &mCblk->flags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1535 | } |
| 1536 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1537 | #endif |
| 1538 | if (mState == STATE_ACTIVE) { |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1539 | result = mAudioTrack->start(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1540 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1541 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1542 | if (result != NO_ERROR) { |
| 1543 | ALOGW("restoreTrack_l() failed status %d", result); |
| 1544 | mState = STATE_STOPPED; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1545 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1546 | |
| 1547 | return result; |
| 1548 | } |
| 1549 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1550 | status_t AudioTrack::setParameters(const String8& keyValuePairs) |
| 1551 | { |
| 1552 | AutoMutex lock(mLock); |
| 1553 | if (mAudioTrack != 0) { |
| 1554 | return mAudioTrack->setParameters(keyValuePairs); |
| 1555 | } else { |
| 1556 | return NO_INIT; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | String8 AudioTrack::getParameters(const String8& keys) |
| 1561 | { |
| 1562 | return String8::empty(); |
| 1563 | } |
| 1564 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | status_t AudioTrack::dump(int fd, const Vector<String16>& args) const |
| 1566 | { |
| 1567 | |
| 1568 | const size_t SIZE = 256; |
| 1569 | char buffer[SIZE]; |
| 1570 | String8 result; |
| 1571 | |
| 1572 | result.append(" AudioTrack::dump\n"); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1573 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, |
| 1574 | mVolume[0], mVolume[1]); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | result.append(buffer); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1576 | snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1577 | mChannelCount, mFrameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1578 | result.append(buffer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1579 | snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1580 | result.append(buffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1581 | snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | result.append(buffer); |
| 1583 | ::write(fd, result.string(), result.size()); |
| 1584 | return NO_ERROR; |
| 1585 | } |
| 1586 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1587 | uint32_t AudioTrack::getUnderrunFrames() const |
| 1588 | { |
| 1589 | AutoMutex lock(mLock); |
| 1590 | return mProxy->getUnderrunFrames(); |
| 1591 | } |
| 1592 | |
| 1593 | // ========================================================================= |
| 1594 | |
| 1595 | void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who) |
| 1596 | { |
| 1597 | sp<AudioTrack> audioTrack = mAudioTrack.promote(); |
| 1598 | if (audioTrack != 0) { |
| 1599 | AutoMutex lock(audioTrack->mLock); |
| 1600 | audioTrack->mProxy->binderDied(); |
| 1601 | } |
| 1602 | } |
| 1603 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | // ========================================================================= |
| 1605 | |
| 1606 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1607 | : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mResumeLatch(false) |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1608 | { |
| 1609 | } |
| 1610 | |
| 1611 | AudioTrack::AudioTrackThread::~AudioTrackThread() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | { |
| 1613 | } |
| 1614 | |
| 1615 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 1616 | { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1617 | { |
| 1618 | AutoMutex _l(mMyLock); |
| 1619 | if (mPaused) { |
| 1620 | mMyCond.wait(mMyLock); |
| 1621 | // caller will check for exitPending() |
| 1622 | return true; |
| 1623 | } |
| 1624 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1625 | nsecs_t ns = mReceiver.processAudioBuffer(this); |
| 1626 | switch (ns) { |
| 1627 | case 0: |
| 1628 | return true; |
| 1629 | case NS_WHENEVER: |
| 1630 | sleep(1); |
| 1631 | return true; |
| 1632 | case NS_INACTIVE: |
| 1633 | pauseConditional(); |
| 1634 | return true; |
| 1635 | case NS_NEVER: |
| 1636 | return false; |
| 1637 | default: |
| 1638 | LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns); |
| 1639 | struct timespec req; |
| 1640 | req.tv_sec = ns / 1000000000LL; |
| 1641 | req.tv_nsec = ns % 1000000000LL; |
| 1642 | nanosleep(&req, NULL /*rem*/); |
| 1643 | return true; |
Glenn Kasten | ca8b280 | 2012-04-23 13:58:16 -0700 | [diff] [blame] | 1644 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1647 | void AudioTrack::AudioTrackThread::requestExit() |
| 1648 | { |
| 1649 | // must be in this order to avoid a race condition |
| 1650 | Thread::requestExit(); |
Glenn Kasten | f4022f9 | 2012-05-02 10:34:59 -0700 | [diff] [blame] | 1651 | resume(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | void AudioTrack::AudioTrackThread::pause() |
| 1655 | { |
| 1656 | AutoMutex _l(mMyLock); |
| 1657 | mPaused = true; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1658 | mResumeLatch = false; |
| 1659 | } |
| 1660 | |
| 1661 | void AudioTrack::AudioTrackThread::pauseConditional() |
| 1662 | { |
| 1663 | AutoMutex _l(mMyLock); |
| 1664 | if (mResumeLatch) { |
| 1665 | mResumeLatch = false; |
| 1666 | } else { |
| 1667 | mPaused = true; |
| 1668 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | void AudioTrack::AudioTrackThread::resume() |
| 1672 | { |
| 1673 | AutoMutex _l(mMyLock); |
| 1674 | if (mPaused) { |
| 1675 | mPaused = false; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1676 | mResumeLatch = false; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1677 | mMyCond.signal(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1678 | } else { |
| 1679 | mResumeLatch = true; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | }; // namespace android |