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