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 | |
| 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <limits.h> |
| 25 | |
| 26 | #include <sched.h> |
| 27 | #include <sys/resource.h> |
| 28 | |
| 29 | #include <private/media/AudioTrackShared.h> |
| 30 | |
| 31 | #include <media/AudioSystem.h> |
| 32 | #include <media/AudioTrack.h> |
| 33 | |
| 34 | #include <utils/Log.h> |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/Parcel.h> |
| 36 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/Timers.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 38 | #include <utils/Atomic.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 40 | #include <cutils/bitops.h> |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 41 | #include <cutils/compiler.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 42 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 43 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 44 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 | |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 46 | #include <audio_utils/primitives.h> |
| 47 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | namespace android { |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 49 | // --------------------------------------------------------------------------- |
| 50 | |
| 51 | // static |
| 52 | status_t AudioTrack::getMinFrameCount( |
| 53 | int* frameCount, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 54 | audio_stream_type_t streamType, |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 55 | uint32_t sampleRate) |
| 56 | { |
Glenn Kasten | 04cd018 | 2012-06-25 11:49:27 -0700 | [diff] [blame] | 57 | if (frameCount == NULL) return BAD_VALUE; |
| 58 | |
| 59 | // default to 0 in case of error |
| 60 | *frameCount = 0; |
| 61 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 62 | // FIXME merge with similar code in createTrack_l(), except we're missing |
| 63 | // some information here that is available in createTrack_l(): |
| 64 | // audio_io_handle_t output |
| 65 | // audio_format_t format |
| 66 | // audio_channel_mask_t channelMask |
| 67 | // audio_output_flags_t flags |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 68 | int afSampleRate; |
| 69 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 70 | return NO_INIT; |
| 71 | } |
| 72 | int afFrameCount; |
| 73 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 74 | return NO_INIT; |
| 75 | } |
| 76 | uint32_t afLatency; |
| 77 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 78 | return NO_INIT; |
| 79 | } |
| 80 | |
| 81 | // Ensure that buffer depth covers at least audio hardware latency |
| 82 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); |
| 83 | if (minBufCount < 2) minBufCount = 2; |
| 84 | |
| 85 | *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 86 | afFrameCount * minBufCount * sampleRate / afSampleRate; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 87 | ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d", |
| 88 | *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency); |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 89 | return NO_ERROR; |
| 90 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
| 92 | // --------------------------------------------------------------------------- |
| 93 | |
| 94 | AudioTrack::AudioTrack() |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 95 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 96 | mIsTimed(false), |
| 97 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 98 | mPreviousSchedulingGroup(SP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
| 102 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 103 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 105 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 106 | audio_channel_mask_t channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | int frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 108 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | callback_t cbf, |
| 110 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 111 | int notificationFrames, |
| 112 | int sessionId) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 113 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 114 | mIsTimed(false), |
| 115 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 116 | mPreviousSchedulingGroup(SP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 118 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 119 | frameCount, flags, cbf, user, notificationFrames, |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 120 | 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 123 | // DEPRECATED |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | AudioTrack::AudioTrack( |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 125 | int streamType, |
| 126 | uint32_t sampleRate, |
| 127 | int format, |
| 128 | int channelMask, |
| 129 | int frameCount, |
| 130 | uint32_t flags, |
| 131 | callback_t cbf, |
| 132 | void* user, |
| 133 | int notificationFrames, |
| 134 | int sessionId) |
| 135 | : mStatus(NO_INIT), |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 136 | mIsTimed(false), |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 137 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT) |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 138 | { |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 139 | mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, |
| 140 | (audio_channel_mask_t) channelMask, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 141 | frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames, |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 142 | 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId); |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 146 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 148 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 149 | audio_channel_mask_t channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 151 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | callback_t cbf, |
| 153 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 154 | int notificationFrames, |
| 155 | int sessionId) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 156 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 157 | mIsTimed(false), |
| 158 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 159 | mPreviousSchedulingGroup(SP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 161 | mStatus = set(streamType, sampleRate, format, channelMask, |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 162 | 0 /*frameCount*/, flags, cbf, user, notificationFrames, |
| 163 | sharedBuffer, false /*threadCanCallJava*/, sessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | AudioTrack::~AudioTrack() |
| 167 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 168 | ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
| 170 | if (mStatus == NO_ERROR) { |
| 171 | // Make sure that callback function exits in the case where |
| 172 | // it is looping on buffer full condition in obtainBuffer(). |
| 173 | // Otherwise the callback thread will never exit. |
| 174 | stop(); |
| 175 | if (mAudioTrackThread != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 176 | mAudioTrackThread->requestExit(); // see comment in AudioTrack.h |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | mAudioTrackThread->requestExitAndWait(); |
| 178 | mAudioTrackThread.clear(); |
| 179 | } |
| 180 | mAudioTrack.clear(); |
| 181 | IPCThreadState::self()->flushCommands(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 182 | AudioSystem::releaseAudioSessionId(mSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | status_t AudioTrack::set( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 187 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 189 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 190 | audio_channel_mask_t channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | int frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 192 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | callback_t cbf, |
| 194 | void* user, |
| 195 | int notificationFrames, |
| 196 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 197 | bool threadCanCallJava, |
| 198 | int sessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | { |
| 200 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 201 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), |
| 202 | sharedBuffer->size()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 204 | ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags); |
| 205 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 206 | AutoMutex lock(mLock); |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 207 | if (mAudioTrack != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 208 | ALOGE("Track already in use"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | return INVALID_OPERATION; |
| 210 | } |
| 211 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | // handle default values first. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 213 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 214 | streamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | if (sampleRate == 0) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 218 | int afSampleRate; |
| 219 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 220 | return NO_INIT; |
| 221 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | sampleRate = afSampleRate; |
| 223 | } |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 224 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // these below should probably come from the audioFlinger too... |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 226 | if (format == AUDIO_FORMAT_DEFAULT) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 227 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | } |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 229 | if (channelMask == 0) { |
| 230 | channelMask = AUDIO_CHANNEL_OUT_STEREO; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // validate parameters |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 234 | if (!audio_is_valid_format(format)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 235 | ALOGE("Invalid format"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | return BAD_VALUE; |
| 237 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 238 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 239 | // AudioFlinger does not currently support 8-bit data in shared memory |
| 240 | if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) { |
| 241 | ALOGE("8-bit data in shared memory is not supported"); |
| 242 | return BAD_VALUE; |
| 243 | } |
| 244 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 245 | // force direct flag if format is not linear PCM |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 246 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 247 | flags = (audio_output_flags_t) |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 248 | // FIXME why can't we allow direct AND fast? |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 249 | ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 250 | } |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 251 | // only allow deep buffering for music stream type |
| 252 | if (streamType != AUDIO_STREAM_MUSIC) { |
| 253 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 254 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 255 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 256 | if (!audio_is_output_channel(channelMask)) { |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 257 | ALOGE("Invalid channel mask %#x", channelMask); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 258 | return BAD_VALUE; |
| 259 | } |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 260 | uint32_t channelCount = popcount(channelMask); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 261 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 262 | audio_io_handle_t output = AudioSystem::getOutput( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 263 | streamType, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 264 | sampleRate, format, channelMask, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 265 | flags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 266 | |
| 267 | if (output == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 268 | 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] | 269 | return BAD_VALUE; |
| 270 | } |
| 271 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 272 | mVolume[LEFT] = 1.0f; |
| 273 | mVolume[RIGHT] = 1.0f; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 274 | mSendLevel = 0.0f; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 275 | mFrameCount = frameCount; |
| 276 | mNotificationFramesReq = notificationFrames; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 277 | mSessionId = sessionId; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 278 | mAuxEffectId = 0; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 279 | mFlags = flags; |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 280 | mCbf = cbf; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 281 | |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 282 | if (cbf != NULL) { |
Eric Laurent | 896adcd | 2012-09-13 11:18:23 -0700 | [diff] [blame] | 283 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 284 | mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/); |
| 285 | } |
| 286 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 287 | // create the IAudioTrack |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 288 | status_t status = createTrack_l(streamType, |
| 289 | sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 290 | format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 291 | channelMask, |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 292 | frameCount, |
| 293 | flags, |
| 294 | sharedBuffer, |
Glenn Kasten | 291f4d5 | 2012-03-19 12:16:56 -0700 | [diff] [blame] | 295 | output); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 296 | |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 297 | if (status != NO_ERROR) { |
| 298 | if (mAudioTrackThread != 0) { |
| 299 | mAudioTrackThread->requestExit(); |
| 300 | mAudioTrackThread.clear(); |
| 301 | } |
| 302 | return status; |
Glenn Kasten | 5d464eb | 2012-06-22 17:19:53 -0700 | [diff] [blame] | 303 | } |
| 304 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | mStatus = NO_ERROR; |
| 306 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | mStreamType = streamType; |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 308 | mFormat = format; |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 309 | mChannelMask = channelMask; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | mChannelCount = channelCount; |
| 311 | mSharedBuffer = sharedBuffer; |
| 312 | mMuted = false; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 313 | mActive = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | mUserData = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | mLoopCount = 0; |
| 316 | mMarkerPosition = 0; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 317 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | mNewPosition = 0; |
| 319 | mUpdatePeriod = 0; |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 320 | mFlushed = false; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 321 | AudioSystem::acquireAudioSessionId(mSessionId); |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 322 | mRestoreStatus = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | return NO_ERROR; |
| 324 | } |
| 325 | |
| 326 | status_t AudioTrack::initCheck() const |
| 327 | { |
| 328 | return mStatus; |
| 329 | } |
| 330 | |
| 331 | // ------------------------------------------------------------------------- |
| 332 | |
| 333 | uint32_t AudioTrack::latency() const |
| 334 | { |
| 335 | return mLatency; |
| 336 | } |
| 337 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 338 | audio_stream_type_t AudioTrack::streamType() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | { |
| 340 | return mStreamType; |
| 341 | } |
| 342 | |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 343 | audio_format_t AudioTrack::format() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | { |
| 345 | return mFormat; |
| 346 | } |
| 347 | |
| 348 | int AudioTrack::channelCount() const |
| 349 | { |
| 350 | return mChannelCount; |
| 351 | } |
| 352 | |
| 353 | uint32_t AudioTrack::frameCount() const |
| 354 | { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 355 | return mCblk->frameCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 358 | size_t AudioTrack::frameSize() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 360 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 671a636 | 2011-06-16 21:30:45 -0700 | [diff] [blame] | 361 | return channelCount()*audio_bytes_per_sample(mFormat); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 362 | } else { |
| 363 | return sizeof(uint8_t); |
| 364 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | sp<IMemory>& AudioTrack::sharedBuffer() |
| 368 | { |
| 369 | return mSharedBuffer; |
| 370 | } |
| 371 | |
| 372 | // ------------------------------------------------------------------------- |
| 373 | |
| 374 | void AudioTrack::start() |
| 375 | { |
| 376 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 377 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 378 | ALOGV("start %p", this); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 380 | AutoMutex lock(mLock); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 381 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 382 | // while we are accessing the cblk |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 383 | sp<IAudioTrack> audioTrack = mAudioTrack; |
| 384 | sp<IMemory> iMem = mCblkMemory; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 385 | audio_track_cblk_t* cblk = mCblk; |
| 386 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 387 | if (!mActive) { |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 388 | mFlushed = false; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 389 | mActive = true; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 390 | mNewPosition = cblk->server + mUpdatePeriod; |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 391 | cblk->lock.lock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 392 | cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 393 | cblk->waitTimeMs = 0; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 394 | android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 395 | if (t != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 396 | t->resume(); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 397 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 398 | mPreviousPriority = getpriority(PRIO_PROCESS, 0); |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 399 | get_sched_policy(0, &mPreviousSchedulingGroup); |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 400 | androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 403 | ALOGV("start %p before lock cblk %p", this, mCblk); |
Glenn Kasten | d3a9ff4 | 2012-06-21 12:11:38 -0700 | [diff] [blame] | 404 | status_t status = NO_ERROR; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 405 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 406 | cblk->lock.unlock(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 407 | ALOGV("mAudioTrack->start()"); |
| 408 | status = mAudioTrack->start(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 409 | cblk->lock.lock(); |
| 410 | if (status == DEAD_OBJECT) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 411 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 412 | } |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 413 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 414 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 415 | status = restoreTrack_l(cblk, true); |
| 416 | } |
| 417 | cblk->lock.unlock(); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 418 | if (status != NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 419 | ALOGV("start() failed"); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 420 | mActive = false; |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 421 | if (t != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 422 | t->pause(); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 423 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 424 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 425 | set_sched_policy(0, mPreviousSchedulingGroup); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 426 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 427 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | } |
| 429 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void AudioTrack::stop() |
| 433 | { |
| 434 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 435 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 436 | ALOGV("stop %p", this); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 438 | AutoMutex lock(mLock); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 439 | if (mActive) { |
| 440 | mActive = false; |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 441 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | mAudioTrack->stop(); |
| 443 | // Cancel loops (If we are in the middle of a loop, playback |
| 444 | // would not stop until loopCount reaches 0). |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 445 | setLoop_l(0, 0, 0); |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 446 | // the playback head position will reset to 0, so if a marker is set, we need |
| 447 | // to activate it again |
| 448 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | // Force flush if a shared buffer is used otherwise audioflinger |
| 450 | // will not stop before end of buffer is reached. |
| 451 | if (mSharedBuffer != 0) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 452 | flush_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
| 454 | if (t != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 455 | t->pause(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 457 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 458 | set_sched_policy(0, mPreviousSchedulingGroup); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | bool AudioTrack::stopped() const |
| 465 | { |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 466 | AutoMutex lock(mLock); |
| 467 | return stopped_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void AudioTrack::flush() |
| 471 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 472 | AutoMutex lock(mLock); |
| 473 | flush_l(); |
| 474 | } |
| 475 | |
| 476 | // must be called with mLock held |
| 477 | void AudioTrack::flush_l() |
| 478 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 479 | ALOGV("flush"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 480 | |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 481 | // clear playback marker and periodic update counter |
| 482 | mMarkerPosition = 0; |
| 483 | mMarkerReached = false; |
| 484 | mUpdatePeriod = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 485 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | if (!mActive) { |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 487 | mFlushed = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | mAudioTrack->flush(); |
| 489 | // Release AudioTrack callback thread in case it was waiting for new buffers |
| 490 | // in AudioTrack::obtainBuffer() |
| 491 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | void AudioTrack::pause() |
| 496 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 497 | ALOGV("pause"); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 498 | AutoMutex lock(mLock); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 499 | if (mActive) { |
| 500 | mActive = false; |
Eric Laurent | 192cbba | 2012-06-13 08:38:36 -0700 | [diff] [blame] | 501 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | mAudioTrack->pause(); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void AudioTrack::mute(bool e) |
| 507 | { |
| 508 | mAudioTrack->mute(e); |
| 509 | mMuted = e; |
| 510 | } |
| 511 | |
| 512 | bool AudioTrack::muted() const |
| 513 | { |
| 514 | return mMuted; |
| 515 | } |
| 516 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 517 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | { |
Glenn Kasten | f0c4950 | 2011-11-30 09:46:04 -0800 | [diff] [blame] | 519 | 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] | 520 | return BAD_VALUE; |
| 521 | } |
| 522 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 523 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | mVolume[LEFT] = left; |
| 525 | mVolume[RIGHT] = right; |
| 526 | |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 527 | mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000)); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 528 | |
| 529 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 532 | void AudioTrack::getVolume(float* left, float* right) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 534 | if (left != NULL) { |
| 535 | *left = mVolume[LEFT]; |
| 536 | } |
| 537 | if (right != NULL) { |
| 538 | *right = mVolume[RIGHT]; |
| 539 | } |
| 540 | } |
| 541 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 542 | status_t AudioTrack::setAuxEffectSendLevel(float level) |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 543 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 544 | ALOGV("setAuxEffectSendLevel(%f)", level); |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 545 | if (level < 0.0f || level > 1.0f) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 546 | return BAD_VALUE; |
| 547 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 548 | AutoMutex lock(mLock); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 549 | |
| 550 | mSendLevel = level; |
| 551 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 552 | mCblk->setSendLevel(level); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 553 | |
| 554 | return NO_ERROR; |
| 555 | } |
| 556 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 557 | void AudioTrack::getAuxEffectSendLevel(float* level) const |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 558 | { |
| 559 | if (level != NULL) { |
| 560 | *level = mSendLevel; |
| 561 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 564 | status_t AudioTrack::setSampleRate(int rate) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | { |
| 566 | int afSamplingRate; |
| 567 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 568 | if (mIsTimed) { |
| 569 | return INVALID_OPERATION; |
| 570 | } |
| 571 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) { |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 573 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } |
| 575 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 576 | if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 578 | AutoMutex lock(mLock); |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 579 | mCblk->sampleRate = rate; |
| 580 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 583 | uint32_t AudioTrack::getSampleRate() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 585 | if (mIsTimed) { |
| 586 | return INVALID_OPERATION; |
| 587 | } |
| 588 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 589 | AutoMutex lock(mLock); |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 590 | return mCblk->sampleRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 594 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 595 | AutoMutex lock(mLock); |
| 596 | return setLoop_l(loopStart, loopEnd, loopCount); |
| 597 | } |
| 598 | |
| 599 | // must be called with mLock held |
| 600 | status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 601 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | audio_track_cblk_t* cblk = mCblk; |
| 603 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | Mutex::Autolock _l(cblk->lock); |
| 605 | |
| 606 | if (loopCount == 0) { |
| 607 | cblk->loopStart = UINT_MAX; |
| 608 | cblk->loopEnd = UINT_MAX; |
| 609 | cblk->loopCount = 0; |
| 610 | mLoopCount = 0; |
| 611 | return NO_ERROR; |
| 612 | } |
| 613 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 614 | if (mIsTimed) { |
| 615 | return INVALID_OPERATION; |
| 616 | } |
| 617 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | if (loopStart >= loopEnd || |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 619 | loopEnd - loopStart > cblk->frameCount || |
| 620 | cblk->server > loopStart) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 621 | ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, " |
| 622 | "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | return BAD_VALUE; |
| 624 | } |
| 625 | |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 626 | if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 627 | ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, " |
| 628 | "framecount %d", |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 629 | loopStart, loopEnd, cblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 631 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | |
| 633 | cblk->loopStart = loopStart; |
| 634 | cblk->loopEnd = loopEnd; |
| 635 | cblk->loopCount = loopCount; |
| 636 | mLoopCount = loopCount; |
| 637 | |
| 638 | return NO_ERROR; |
| 639 | } |
| 640 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 642 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 643 | if (mCbf == NULL) return INVALID_OPERATION; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | |
| 645 | mMarkerPosition = marker; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 646 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | |
| 648 | return NO_ERROR; |
| 649 | } |
| 650 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 651 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 653 | if (marker == NULL) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | |
| 655 | *marker = mMarkerPosition; |
| 656 | |
| 657 | return NO_ERROR; |
| 658 | } |
| 659 | |
| 660 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 661 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 662 | if (mCbf == NULL) return INVALID_OPERATION; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | |
| 664 | uint32_t curPosition; |
| 665 | getPosition(&curPosition); |
| 666 | mNewPosition = curPosition + updatePeriod; |
| 667 | mUpdatePeriod = updatePeriod; |
| 668 | |
| 669 | return NO_ERROR; |
| 670 | } |
| 671 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 672 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 674 | if (updatePeriod == NULL) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | |
| 676 | *updatePeriod = mUpdatePeriod; |
| 677 | |
| 678 | return NO_ERROR; |
| 679 | } |
| 680 | |
| 681 | status_t AudioTrack::setPosition(uint32_t position) |
| 682 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 683 | if (mIsTimed) return INVALID_OPERATION; |
| 684 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 685 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 687 | if (!stopped_l()) return INVALID_OPERATION; |
| 688 | |
| 689 | Mutex::Autolock _l(mCblk->lock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | |
| 691 | if (position > mCblk->user) return BAD_VALUE; |
| 692 | |
| 693 | mCblk->server = position; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 694 | android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 695 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | return NO_ERROR; |
| 697 | } |
| 698 | |
| 699 | status_t AudioTrack::getPosition(uint32_t *position) |
| 700 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 701 | if (position == NULL) return BAD_VALUE; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 702 | AutoMutex lock(mLock); |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 703 | *position = mFlushed ? 0 : mCblk->server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | |
| 705 | return NO_ERROR; |
| 706 | } |
| 707 | |
| 708 | status_t AudioTrack::reload() |
| 709 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 710 | AutoMutex lock(mLock); |
| 711 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 712 | if (!stopped_l()) return INVALID_OPERATION; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 713 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 714 | flush_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 716 | mCblk->stepUser(mCblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | |
| 718 | return NO_ERROR; |
| 719 | } |
| 720 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 721 | audio_io_handle_t AudioTrack::getOutput() |
| 722 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 723 | AutoMutex lock(mLock); |
| 724 | return getOutput_l(); |
| 725 | } |
| 726 | |
| 727 | // must be called with mLock held |
| 728 | audio_io_handle_t AudioTrack::getOutput_l() |
| 729 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 730 | return AudioSystem::getOutput(mStreamType, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 731 | mCblk->sampleRate, mFormat, mChannelMask, mFlags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 734 | int AudioTrack::getSessionId() const |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 735 | { |
| 736 | return mSessionId; |
| 737 | } |
| 738 | |
| 739 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 740 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 741 | ALOGV("attachAuxEffect(%d)", effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 742 | status_t status = mAudioTrack->attachAuxEffect(effectId); |
| 743 | if (status == NO_ERROR) { |
| 744 | mAuxEffectId = effectId; |
| 745 | } |
| 746 | return status; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 747 | } |
| 748 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | // ------------------------------------------------------------------------- |
| 750 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 751 | // must be called with mLock held |
| 752 | status_t AudioTrack::createTrack_l( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 753 | audio_stream_type_t streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 754 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 755 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 756 | audio_channel_mask_t channelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 757 | int frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 758 | audio_output_flags_t flags, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 759 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 291f4d5 | 2012-03-19 12:16:56 -0700 | [diff] [blame] | 760 | audio_io_handle_t output) |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 761 | { |
| 762 | status_t status; |
| 763 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 764 | if (audioFlinger == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 765 | ALOGE("Could not get audioflinger"); |
| 766 | return NO_INIT; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 769 | uint32_t afLatency; |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 770 | if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 771 | return NO_INIT; |
| 772 | } |
| 773 | |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 774 | // Client decides whether the track is TIMED (see below), but can only express a preference |
| 775 | // for FAST. Server will perform additional tests. |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 776 | if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !( |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 777 | // either of these use cases: |
| 778 | // use case 1: shared buffer |
| 779 | (sharedBuffer != 0) || |
| 780 | // use case 2: callback handler |
| 781 | (mCbf != NULL))) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 782 | ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client"); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 783 | // once denied, do not request again if IAudioTrack is re-created |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 784 | flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 785 | mFlags = flags; |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 786 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 787 | ALOGV("createTrack_l() output %d afLatency %d", output, afLatency); |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 788 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 789 | mNotificationFramesAct = mNotificationFramesReq; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 790 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 791 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 792 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 793 | if (sharedBuffer != 0) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 794 | // Same comment as below about ignoring frameCount parameter for set() |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 795 | frameCount = sharedBuffer->size(); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 796 | } else if (frameCount == 0) { |
| 797 | int afFrameCount; |
| 798 | if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) { |
| 799 | return NO_INIT; |
| 800 | } |
| 801 | frameCount = afFrameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 802 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 803 | |
| 804 | } else if (sharedBuffer != 0) { |
| 805 | |
| 806 | // Ensure that buffer alignment matches channelCount |
| 807 | int channelCount = popcount(channelMask); |
| 808 | // 8-bit data in shared memory is not currently supported by AudioFlinger |
| 809 | size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2; |
| 810 | if (channelCount > 1) { |
| 811 | // More than 2 channels does not require stronger alignment than stereo |
| 812 | alignment <<= 1; |
| 813 | } |
| 814 | if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) { |
| 815 | ALOGE("Invalid buffer alignment: address %p, channelCount %d", |
| 816 | sharedBuffer->pointer(), channelCount); |
| 817 | return BAD_VALUE; |
| 818 | } |
| 819 | |
| 820 | // When initializing a shared buffer AudioTrack via constructors, |
| 821 | // there's no frameCount parameter. |
| 822 | // But when initializing a shared buffer AudioTrack via set(), |
| 823 | // there _is_ a frameCount parameter. We silently ignore it. |
| 824 | frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t); |
| 825 | |
| 826 | } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 827 | |
| 828 | // FIXME move these calculations and associated checks to server |
| 829 | int afSampleRate; |
| 830 | if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) { |
| 831 | return NO_INIT; |
| 832 | } |
| 833 | int afFrameCount; |
| 834 | if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) { |
| 835 | return NO_INIT; |
| 836 | } |
| 837 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 838 | // Ensure that buffer depth covers at least audio hardware latency |
| 839 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
| 840 | if (minBufCount < 2) minBufCount = 2; |
| 841 | |
| 842 | int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 843 | ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d" |
| 844 | ", afLatency=%d", |
| 845 | minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 846 | |
| 847 | if (frameCount == 0) { |
| 848 | frameCount = minFrameCount; |
| 849 | } |
| 850 | if (mNotificationFramesAct == 0) { |
| 851 | mNotificationFramesAct = frameCount/2; |
| 852 | } |
| 853 | // Make sure that application is notified with sufficient margin |
| 854 | // before underrun |
| 855 | if (mNotificationFramesAct > (uint32_t)frameCount/2) { |
| 856 | mNotificationFramesAct = frameCount/2; |
| 857 | } |
| 858 | if (frameCount < minFrameCount) { |
| 859 | // not ALOGW because it happens all the time when playing key clicks over A2DP |
| 860 | ALOGV("Minimum buffer size corrected from %d to %d", |
| 861 | frameCount, minFrameCount); |
| 862 | frameCount = minFrameCount; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 863 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 864 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 865 | } else { |
| 866 | // 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] | 867 | } |
| 868 | |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 869 | IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT; |
| 870 | if (mIsTimed) { |
| 871 | trackFlags |= IAudioFlinger::TRACK_TIMED; |
| 872 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 873 | |
| 874 | pid_t tid = -1; |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 875 | if (flags & AUDIO_OUTPUT_FLAG_FAST) { |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 876 | trackFlags |= IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 877 | if (mAudioTrackThread != 0) { |
| 878 | tid = mAudioTrackThread->getTid(); |
| 879 | } |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 882 | sp<IAudioTrack> track = audioFlinger->createTrack(getpid(), |
| 883 | streamType, |
| 884 | sampleRate, |
| 885 | format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 886 | channelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 887 | frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 888 | trackFlags, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 889 | sharedBuffer, |
| 890 | output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 891 | tid, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 892 | &mSessionId, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 893 | &status); |
| 894 | |
| 895 | if (track == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 896 | ALOGE("AudioFlinger could not create track, status: %d", status); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 897 | return status; |
| 898 | } |
| 899 | sp<IMemory> cblk = track->getCblk(); |
| 900 | if (cblk == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 901 | ALOGE("Could not get control block"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 902 | return NO_INIT; |
| 903 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 904 | mAudioTrack = track; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 905 | mCblkMemory = cblk; |
| 906 | mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer()); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 907 | // old has the previous value of mCblk->flags before the "or" operation |
| 908 | int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags); |
| 909 | if (flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 910 | if (old & CBLK_FAST) { |
Glenn Kasten | 31dfd1d | 2012-05-01 11:07:08 -0700 | [diff] [blame] | 911 | ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 912 | } else { |
Glenn Kasten | 31dfd1d | 2012-05-01 11:07:08 -0700 | [diff] [blame] | 913 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 914 | // once denied, do not request again if IAudioTrack is re-created |
| 915 | flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 916 | mFlags = flags; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 917 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 918 | if (sharedBuffer == 0) { |
| 919 | mNotificationFramesAct = mCblk->frameCount/2; |
| 920 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 921 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 922 | if (sharedBuffer == 0) { |
| 923 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 924 | } else { |
| 925 | mCblk->buffers = sharedBuffer->pointer(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 926 | // Force buffer full condition as data is already present in shared memory |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 927 | mCblk->stepUser(mCblk->frameCount); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 928 | } |
| 929 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 930 | mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | |
| 931 | uint16_t(mVolume[LEFT] * 0x1000)); |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 932 | mCblk->setSendLevel(mSendLevel); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 933 | mAudioTrack->attachAuxEffect(mAuxEffectId); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 934 | mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 935 | mCblk->waitTimeMs = 0; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 936 | mRemainingFrames = mNotificationFramesAct; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 937 | // FIXME don't believe this lie |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 938 | mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 939 | // If IAudioTrack is re-created, don't let the requested frameCount |
| 940 | // decrease. This can confuse clients that cache frameCount(). |
| 941 | if (mCblk->frameCount > mFrameCount) { |
| 942 | mFrameCount = mCblk->frameCount; |
| 943 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 944 | return NO_ERROR; |
| 945 | } |
| 946 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 948 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 949 | AutoMutex lock(mLock); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 950 | bool active; |
Glenn Kasten | d0965dd | 2011-06-22 16:18:04 -0700 | [diff] [blame] | 951 | status_t result = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | audio_track_cblk_t* cblk = mCblk; |
| 953 | uint32_t framesReq = audioBuffer->frameCount; |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 954 | uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | |
| 956 | audioBuffer->frameCount = 0; |
| 957 | audioBuffer->size = 0; |
| 958 | |
| 959 | uint32_t framesAvail = cblk->framesAvailable(); |
| 960 | |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 961 | cblk->lock.lock(); |
| 962 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 963 | goto create_new_track; |
| 964 | } |
| 965 | cblk->lock.unlock(); |
| 966 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | if (framesAvail == 0) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 968 | cblk->lock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | goto start_loop_here; |
| 970 | while (framesAvail == 0) { |
| 971 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 972 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 973 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 974 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | return NO_MORE_BUFFERS; |
| 976 | } |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 977 | if (CC_UNLIKELY(!waitCount)) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 978 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | return WOULD_BLOCK; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 980 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 981 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 982 | mLock.unlock(); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 983 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 984 | cblk->lock.unlock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 985 | mLock.lock(); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 986 | if (!mActive) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 987 | return status_t(STOPPED); |
| 988 | } |
| 989 | cblk->lock.lock(); |
| 990 | } |
| 991 | |
| 992 | if (cblk->flags & CBLK_INVALID_MSK) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 993 | goto create_new_track; |
| 994 | } |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 995 | if (CC_UNLIKELY(result != NO_ERROR)) { |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 996 | cblk->waitTimeMs += waitTimeMs; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) { |
| 998 | // timing out when a loop has been set and we have already written upto loop end |
| 999 | // is a normal condition: no need to wake AudioFlinger up. |
| 1000 | if (cblk->user < cblk->loopEnd) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1001 | ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, " |
| 1002 | "server=%08x", this, cblk->mName, cblk->user, cblk->server); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1003 | //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | cblk->lock.unlock(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1005 | result = mAudioTrack->start(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | cblk->lock.lock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1007 | if (result == DEAD_OBJECT) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1008 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1009 | create_new_track: |
| 1010 | result = restoreTrack_l(cblk, false); |
| 1011 | } |
| 1012 | if (result != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1013 | ALOGW("obtainBuffer create Track error %d", result); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1014 | cblk->lock.unlock(); |
| 1015 | return result; |
| 1016 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | } |
| 1018 | cblk->waitTimeMs = 0; |
| 1019 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1020 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | if (--waitCount == 0) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1022 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | return TIMED_OUT; |
| 1024 | } |
| 1025 | } |
| 1026 | // read the server count again |
| 1027 | start_loop_here: |
| 1028 | framesAvail = cblk->framesAvailable_l(); |
| 1029 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1030 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | cblk->waitTimeMs = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1034 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | if (framesReq > framesAvail) { |
| 1036 | framesReq = framesAvail; |
| 1037 | } |
| 1038 | |
| 1039 | uint32_t u = cblk->user; |
| 1040 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 1041 | |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1042 | if (framesReq > bufferEnd - u) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | framesReq = bufferEnd - u; |
| 1044 | } |
| 1045 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1046 | audioBuffer->flags = mMuted ? Buffer::MUTE : 0; |
| 1047 | audioBuffer->channelCount = mChannelCount; |
| 1048 | audioBuffer->frameCount = framesReq; |
| 1049 | audioBuffer->size = framesReq * cblk->frameSize; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1050 | if (audio_is_linear_pcm(mFormat)) { |
| 1051 | audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1052 | } else { |
| 1053 | audioBuffer->format = mFormat; |
| 1054 | } |
| 1055 | audioBuffer->raw = (int8_t *)cblk->buffer(u); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | active = mActive; |
| 1057 | return active ? status_t(NO_ERROR) : status_t(STOPPED); |
| 1058 | } |
| 1059 | |
| 1060 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 1061 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1062 | AutoMutex lock(mLock); |
| 1063 | mCblk->stepUser(audioBuffer->frameCount); |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1064 | if (audioBuffer->frameCount > 0) { |
| 1065 | // restart track if it was disabled by audioflinger due to previous underrun |
| 1066 | if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) { |
| 1067 | android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags); |
Glenn Kasten | 0c9d26d | 2012-05-31 14:35:01 -0700 | [diff] [blame] | 1068 | ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName); |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1069 | mAudioTrack->start(); |
| 1070 | } |
| 1071 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | // ------------------------------------------------------------------------- |
| 1075 | |
| 1076 | ssize_t AudioTrack::write(const void* buffer, size_t userSize) |
| 1077 | { |
| 1078 | |
| 1079 | if (mSharedBuffer != 0) return INVALID_OPERATION; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1080 | if (mIsTimed) return INVALID_OPERATION; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | |
| 1082 | if (ssize_t(userSize) < 0) { |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1083 | // Sanity-check: user is most-likely passing an error code, and it would |
| 1084 | // make the return value ambiguous (actualSize vs error). |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1085 | ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | buffer, userSize, userSize); |
| 1087 | return BAD_VALUE; |
| 1088 | } |
| 1089 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1090 | ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1092 | if (userSize == 0) { |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1096 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1097 | // while we are accessing the cblk |
| 1098 | mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1099 | sp<IAudioTrack> audioTrack = mAudioTrack; |
| 1100 | sp<IMemory> iMem = mCblkMemory; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1101 | mLock.unlock(); |
| 1102 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | ssize_t written = 0; |
| 1104 | const int8_t *src = (const int8_t *)buffer; |
| 1105 | Buffer audioBuffer; |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1106 | size_t frameSz = frameSize(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | |
| 1108 | do { |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1109 | audioBuffer.frameCount = userSize/frameSz; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1110 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | status_t err = obtainBuffer(&audioBuffer, -1); |
| 1112 | if (err < 0) { |
| 1113 | // out of buffers, return #bytes written |
| 1114 | if (err == status_t(NO_MORE_BUFFERS)) |
| 1115 | break; |
| 1116 | return ssize_t(err); |
| 1117 | } |
| 1118 | |
| 1119 | size_t toWrite; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1120 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1121 | 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] | 1122 | // Divide capacity by 2 to take expansion into account |
| 1123 | toWrite = audioBuffer.size>>1; |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 1124 | memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite); |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1125 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | toWrite = audioBuffer.size; |
| 1127 | memcpy(audioBuffer.i8, src, toWrite); |
| 1128 | src += toWrite; |
| 1129 | } |
| 1130 | userSize -= toWrite; |
| 1131 | written += toWrite; |
| 1132 | |
| 1133 | releaseBuffer(&audioBuffer); |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1134 | } while (userSize >= frameSz); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | |
| 1136 | return written; |
| 1137 | } |
| 1138 | |
| 1139 | // ------------------------------------------------------------------------- |
| 1140 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1141 | TimedAudioTrack::TimedAudioTrack() { |
| 1142 | mIsTimed = true; |
| 1143 | } |
| 1144 | |
| 1145 | status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer) |
| 1146 | { |
| 1147 | status_t result = UNKNOWN_ERROR; |
| 1148 | |
| 1149 | // If the track is not invalid already, try to allocate a buffer. alloc |
| 1150 | // 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] | 1151 | // we can attempt to restore in just a bit. |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1152 | if (!(mCblk->flags & CBLK_INVALID_MSK)) { |
| 1153 | result = mAudioTrack->allocateTimedBuffer(size, buffer); |
| 1154 | if (result == DEAD_OBJECT) { |
| 1155 | android_atomic_or(CBLK_INVALID_ON, &mCblk->flags); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // If the track is invalid at this point, attempt to restore it. and try the |
| 1160 | // allocation one more time. |
| 1161 | if (mCblk->flags & CBLK_INVALID_MSK) { |
| 1162 | mCblk->lock.lock(); |
| 1163 | result = restoreTrack_l(mCblk, false); |
| 1164 | mCblk->lock.unlock(); |
| 1165 | |
| 1166 | if (result == OK) |
| 1167 | result = mAudioTrack->allocateTimedBuffer(size, buffer); |
| 1168 | } |
| 1169 | |
| 1170 | return result; |
| 1171 | } |
| 1172 | |
| 1173 | status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer, |
| 1174 | int64_t pts) |
| 1175 | { |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1176 | status_t status = mAudioTrack->queueTimedBuffer(buffer, pts); |
| 1177 | { |
| 1178 | AutoMutex lock(mLock); |
| 1179 | // restart track if it was disabled by audioflinger due to previous underrun |
| 1180 | if (buffer->size() != 0 && status == NO_ERROR && |
| 1181 | mActive && (mCblk->flags & CBLK_DISABLED_MSK)) { |
| 1182 | android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags); |
| 1183 | ALOGW("queueTimedBuffer() track %p disabled, restarting", this); |
| 1184 | mAudioTrack->start(); |
| 1185 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1186 | } |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1187 | return status; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform, |
| 1191 | TargetTimeline target) |
| 1192 | { |
| 1193 | return mAudioTrack->setMediaTimeTransform(xform, target); |
| 1194 | } |
| 1195 | |
| 1196 | // ------------------------------------------------------------------------- |
| 1197 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread) |
| 1199 | { |
| 1200 | Buffer audioBuffer; |
| 1201 | uint32_t frames; |
| 1202 | size_t writtenSize; |
| 1203 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1204 | mLock.lock(); |
| 1205 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1206 | // while we are accessing the cblk |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1207 | sp<IAudioTrack> audioTrack = mAudioTrack; |
| 1208 | sp<IMemory> iMem = mCblkMemory; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1209 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 1210 | bool active = mActive; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1211 | mLock.unlock(); |
| 1212 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | // Manage underrun callback |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 1214 | if (active && (cblk->framesAvailable() == cblk->frameCount)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1215 | ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1216 | if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | mCbf(EVENT_UNDERRUN, mUserData, 0); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1218 | if (cblk->server == cblk->frameCount) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1219 | mCbf(EVENT_BUFFER_END, mUserData, 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | if (mSharedBuffer != 0) return false; |
| 1222 | } |
| 1223 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1224 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | // Manage loop end callback |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1226 | while (mLoopCount > cblk->loopCount) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | int loopCount = -1; |
| 1228 | mLoopCount--; |
| 1229 | if (mLoopCount >= 0) loopCount = mLoopCount; |
| 1230 | |
| 1231 | mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount); |
| 1232 | } |
| 1233 | |
| 1234 | // Manage marker callback |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1235 | if (!mMarkerReached && (mMarkerPosition > 0)) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1236 | if (cblk->server >= mMarkerPosition) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition); |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1238 | mMarkerReached = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | // Manage new position callback |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1243 | if (mUpdatePeriod > 0) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1244 | while (cblk->server >= mNewPosition) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition); |
| 1246 | mNewPosition += mUpdatePeriod; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | // If Shared buffer is used, no data is requested from client. |
| 1251 | if (mSharedBuffer != 0) { |
| 1252 | frames = 0; |
| 1253 | } else { |
| 1254 | frames = mRemainingFrames; |
| 1255 | } |
| 1256 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1257 | // See description of waitCount parameter at declaration of obtainBuffer(). |
| 1258 | // The logic below prevents us from being stuck below at obtainBuffer() |
| 1259 | // not being able to handle timed events (position, markers, loops). |
Eric Laurent | 2267ba1 | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1260 | int32_t waitCount = -1; |
| 1261 | if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) { |
| 1262 | waitCount = 1; |
| 1263 | } |
| 1264 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | do { |
| 1266 | |
| 1267 | audioBuffer.frameCount = frames; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1268 | |
Eric Laurent | 2267ba1 | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1269 | status_t err = obtainBuffer(&audioBuffer, waitCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | if (err < NO_ERROR) { |
| 1271 | if (err != TIMED_OUT) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1272 | ALOGE_IF(err != status_t(NO_MORE_BUFFERS), |
| 1273 | "Error obtaining an audio buffer, giving up."); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | return false; |
| 1275 | } |
| 1276 | break; |
| 1277 | } |
| 1278 | if (err == status_t(STOPPED)) return false; |
| 1279 | |
| 1280 | // Divide buffer size by 2 to take into account the expansion |
| 1281 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 1282 | // of the destination buffer |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1283 | 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] | 1284 | audioBuffer.size >>= 1; |
| 1285 | } |
| 1286 | |
| 1287 | size_t reqSize = audioBuffer.size; |
| 1288 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
| 1289 | writtenSize = audioBuffer.size; |
| 1290 | |
| 1291 | // Sanity check on returned size |
The Android Open Source Project | 8555d08 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1292 | if (ssize_t(writtenSize) <= 0) { |
| 1293 | // The callback is done filling buffers |
| 1294 | // Keep this thread going to handle timed events and |
| 1295 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 1296 | // but don't just loop and block the CPU, so wait |
| 1297 | usleep(WAIT_PERIOD_MS*1000); |
| 1298 | break; |
| 1299 | } |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1300 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | if (writtenSize > reqSize) writtenSize = reqSize; |
| 1302 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1303 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 1304 | // 8 to 16 bit conversion, note that source and destination are the same address |
| 1305 | memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | writtenSize <<= 1; |
| 1307 | } |
| 1308 | |
| 1309 | audioBuffer.size = writtenSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1310 | // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1311 | // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1312 | // 16 bit. |
| 1313 | audioBuffer.frameCount = writtenSize/mCblk->frameSize; |
| 1314 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | frames -= audioBuffer.frameCount; |
| 1316 | |
| 1317 | releaseBuffer(&audioBuffer); |
| 1318 | } |
| 1319 | while (frames); |
| 1320 | |
| 1321 | if (frames == 0) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1322 | mRemainingFrames = mNotificationFramesAct; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } else { |
| 1324 | mRemainingFrames = frames; |
| 1325 | } |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1329 | // must be called with mLock and cblk.lock held. Callers must also hold strong references on |
| 1330 | // the IAudioTrack and IMemory in case they are recreated here. |
| 1331 | // If the IAudioTrack is successfully restored, the cblk pointer is updated |
| 1332 | status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart) |
| 1333 | { |
| 1334 | status_t result; |
| 1335 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1336 | if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1337 | ALOGW("dead IAudioTrack, creating a new one from %s TID %d", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1338 | fromStart ? "start()" : "obtainBuffer()", gettid()); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1339 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1340 | // signal old cblk condition so that other threads waiting for available buffers stop |
| 1341 | // waiting now |
| 1342 | cblk->cv.broadcast(); |
| 1343 | cblk->lock.unlock(); |
| 1344 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1345 | // refresh the audio configuration cache in this process to make sure we get new |
| 1346 | // output parameters in getOutput_l() and createTrack_l() |
| 1347 | AudioSystem::clearAudioConfigCache(); |
| 1348 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1349 | // if the new IAudioTrack is created, createTrack_l() will modify the |
| 1350 | // following member variables: mAudioTrack, mCblkMemory and mCblk. |
| 1351 | // It will also delete the strong references on previous IAudioTrack and IMemory |
| 1352 | result = createTrack_l(mStreamType, |
| 1353 | cblk->sampleRate, |
| 1354 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1355 | mChannelMask, |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1356 | mFrameCount, |
| 1357 | mFlags, |
| 1358 | mSharedBuffer, |
Glenn Kasten | 291f4d5 | 2012-03-19 12:16:56 -0700 | [diff] [blame] | 1359 | getOutput_l()); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1360 | |
| 1361 | if (result == NO_ERROR) { |
Eric Laurent | 408b8dc | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1362 | uint32_t user = cblk->user; |
| 1363 | uint32_t server = cblk->server; |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1364 | // restore write index and set other indexes to reflect empty buffer status |
Eric Laurent | 408b8dc | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1365 | mCblk->user = user; |
| 1366 | mCblk->server = user; |
| 1367 | mCblk->userBase = user; |
| 1368 | mCblk->serverBase = user; |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1369 | // restore loop: this is not guaranteed to succeed if new frame count is not |
| 1370 | // compatible with loop length |
| 1371 | setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1372 | if (!fromStart) { |
| 1373 | mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
Eric Laurent | 408b8dc | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1374 | // Make sure that a client relying on callback events indicating underrun or |
| 1375 | // the actual amount of audio frames played (e.g SoundPool) receives them. |
| 1376 | if (mSharedBuffer == 0) { |
| 1377 | uint32_t frames = 0; |
| 1378 | if (user > server) { |
| 1379 | frames = ((user - server) > mCblk->frameCount) ? |
| 1380 | mCblk->frameCount : (user - server); |
| 1381 | memset(mCblk->buffers, 0, frames * mCblk->frameSize); |
| 1382 | } |
| 1383 | // restart playback even if buffer is not completely filled. |
| 1384 | android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags); |
| 1385 | // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to |
| 1386 | // the client |
| 1387 | mCblk->stepUser(frames); |
| 1388 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1389 | } |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 1390 | if (mSharedBuffer != 0) { |
| 1391 | mCblk->stepUser(mCblk->frameCount); |
| 1392 | } |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1393 | if (mActive) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1394 | result = mAudioTrack->start(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1395 | ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result); |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1396 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1397 | if (fromStart && result == NO_ERROR) { |
| 1398 | mNewPosition = mCblk->server + mUpdatePeriod; |
| 1399 | } |
| 1400 | } |
| 1401 | if (result != NO_ERROR) { |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1402 | android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1403 | ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1404 | } |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1405 | mRestoreStatus = result; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1406 | // signal old cblk condition for other threads waiting for restore completion |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1407 | android_atomic_or(CBLK_RESTORED_ON, &cblk->flags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1408 | cblk->cv.broadcast(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1409 | } else { |
Glenn Kasten | 9a08ebc | 2012-11-02 09:59:51 -0700 | [diff] [blame^] | 1410 | bool haveLogged = false; |
| 1411 | for (;;) { |
| 1412 | if (cblk->flags & CBLK_RESTORED_MSK) { |
| 1413 | ALOGW("dead IAudioTrack restored"); |
| 1414 | result = mRestoreStatus; |
| 1415 | cblk->lock.unlock(); |
| 1416 | break; |
| 1417 | } |
| 1418 | if (!haveLogged) { |
| 1419 | ALOGW("dead IAudioTrack, waiting for a new one"); |
| 1420 | haveLogged = true; |
| 1421 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1422 | mLock.unlock(); |
| 1423 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS)); |
| 1424 | cblk->lock.unlock(); |
| 1425 | mLock.lock(); |
Glenn Kasten | 9a08ebc | 2012-11-02 09:59:51 -0700 | [diff] [blame^] | 1426 | if (result != NO_ERROR) { |
| 1427 | ALOGW("timed out"); |
| 1428 | break; |
| 1429 | } |
| 1430 | cblk->lock.lock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1431 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1432 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1433 | ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1434 | result, mActive, mCblk, cblk, mCblk->flags, cblk->flags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1435 | |
| 1436 | if (result == NO_ERROR) { |
| 1437 | // from now on we switch to the newly created cblk |
| 1438 | cblk = mCblk; |
| 1439 | } |
| 1440 | cblk->lock.lock(); |
| 1441 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1442 | ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid()); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1443 | |
| 1444 | return result; |
| 1445 | } |
| 1446 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | status_t AudioTrack::dump(int fd, const Vector<String16>& args) const |
| 1448 | { |
| 1449 | |
| 1450 | const size_t SIZE = 256; |
| 1451 | char buffer[SIZE]; |
| 1452 | String8 result; |
| 1453 | |
| 1454 | result.append(" AudioTrack::dump\n"); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1455 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, |
| 1456 | mVolume[0], mVolume[1]); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | result.append(buffer); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1458 | snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, |
| 1459 | mChannelCount, mCblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | result.append(buffer); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1461 | snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", |
| 1462 | (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | result.append(buffer); |
| 1464 | snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency); |
| 1465 | result.append(buffer); |
| 1466 | ::write(fd, result.string(), result.size()); |
| 1467 | return NO_ERROR; |
| 1468 | } |
| 1469 | |
| 1470 | // ========================================================================= |
| 1471 | |
| 1472 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1473 | : Thread(bCanCallJava), mReceiver(receiver), mPaused(true) |
| 1474 | { |
| 1475 | } |
| 1476 | |
| 1477 | AudioTrack::AudioTrackThread::~AudioTrackThread() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | { |
| 1479 | } |
| 1480 | |
| 1481 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 1482 | { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1483 | { |
| 1484 | AutoMutex _l(mMyLock); |
| 1485 | if (mPaused) { |
| 1486 | mMyCond.wait(mMyLock); |
| 1487 | // caller will check for exitPending() |
| 1488 | return true; |
| 1489 | } |
| 1490 | } |
Glenn Kasten | ca8b280 | 2012-04-23 13:58:16 -0700 | [diff] [blame] | 1491 | if (!mReceiver.processAudioBuffer(this)) { |
| 1492 | pause(); |
| 1493 | } |
| 1494 | return true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1497 | void AudioTrack::AudioTrackThread::requestExit() |
| 1498 | { |
| 1499 | // must be in this order to avoid a race condition |
| 1500 | Thread::requestExit(); |
Glenn Kasten | f4022f9 | 2012-05-02 10:34:59 -0700 | [diff] [blame] | 1501 | resume(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | void AudioTrack::AudioTrackThread::pause() |
| 1505 | { |
| 1506 | AutoMutex _l(mMyLock); |
| 1507 | mPaused = true; |
| 1508 | } |
| 1509 | |
| 1510 | void AudioTrack::AudioTrackThread::resume() |
| 1511 | { |
| 1512 | AutoMutex _l(mMyLock); |
| 1513 | if (mPaused) { |
| 1514 | mPaused = false; |
| 1515 | mMyCond.signal(); |
| 1516 | } |
| 1517 | } |
| 1518 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | // ========================================================================= |
| 1520 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1521 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | audio_track_cblk_t::audio_track_cblk_t() |
Mathias Agopian | 54b1a05 | 2010-03-19 16:14:13 -0700 | [diff] [blame] | 1523 | : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0), |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1524 | userBase(0), serverBase(0), buffers(NULL), frameCount(0), |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 1525 | loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000), |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 1526 | mSendLevel(0), flags(0) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | { |
| 1528 | } |
| 1529 | |
| 1530 | uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount) |
| 1531 | { |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1532 | ALOGV("stepuser %08x %08x %d", user, server, frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1533 | |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1534 | uint32_t u = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | u += frameCount; |
| 1536 | // Ensure that user is never ahead of server for AudioRecord |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1537 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | // If stepServer() has been called once, switch to normal obtainBuffer() timeout period |
| 1539 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) { |
| 1540 | bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 1541 | } |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1542 | } else if (u > server) { |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1543 | ALOGW("stepUser occurred after track reset"); |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1544 | u = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1547 | uint32_t fc = this->frameCount; |
| 1548 | if (u >= fc) { |
| 1549 | // common case, user didn't just wrap |
| 1550 | if (u - fc >= userBase ) { |
| 1551 | userBase += fc; |
| 1552 | } |
| 1553 | } else if (u >= userBase + fc) { |
| 1554 | // user just wrapped |
| 1555 | userBase += fc; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1558 | user = u; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1559 | |
| 1560 | // Clear flow control error condition as new data has been written/read to/from buffer. |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1561 | if (flags & CBLK_UNDERRUN_MSK) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1562 | android_atomic_and(~CBLK_UNDERRUN_MSK, &flags); |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1563 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1564 | |
| 1565 | return u; |
| 1566 | } |
| 1567 | |
| 1568 | bool audio_track_cblk_t::stepServer(uint32_t frameCount) |
| 1569 | { |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1570 | ALOGV("stepserver %08x %08x %d", user, server, frameCount); |
| 1571 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1572 | if (!tryLock()) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1573 | ALOGW("stepServer() could not lock cblk"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | return false; |
| 1575 | } |
| 1576 | |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1577 | uint32_t s = server; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1578 | bool flushed = (s == user); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | |
| 1580 | s += frameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1581 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | // Mark that we have read the first buffer so that next time stepUser() is called |
| 1583 | // we switch to normal obtainBuffer() timeout period |
| 1584 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1585 | bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1586 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | // It is possible that we receive a flush() |
| 1588 | // while the mixer is processing a block: in this case, |
| 1589 | // stepServer() is called After the flush() has reset u & s and |
| 1590 | // we have s > u |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1591 | if (flushed) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1592 | ALOGW("stepServer occurred after track reset"); |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1593 | s = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | if (s >= loopEnd) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1598 | ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | s = loopStart; |
| 1600 | if (--loopCount == 0) { |
| 1601 | loopEnd = UINT_MAX; |
| 1602 | loopStart = UINT_MAX; |
| 1603 | } |
| 1604 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 1605 | |
| 1606 | uint32_t fc = this->frameCount; |
| 1607 | if (s >= fc) { |
| 1608 | // common case, server didn't just wrap |
| 1609 | if (s - fc >= serverBase ) { |
| 1610 | serverBase += fc; |
| 1611 | } |
| 1612 | } else if (s >= serverBase + fc) { |
| 1613 | // server just wrapped |
| 1614 | serverBase += fc; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1617 | server = s; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1619 | if (!(flags & CBLK_INVALID_MSK)) { |
| 1620 | cv.signal(); |
| 1621 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | lock.unlock(); |
| 1623 | return true; |
| 1624 | } |
| 1625 | |
| 1626 | void* audio_track_cblk_t::buffer(uint32_t offset) const |
| 1627 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1628 | return (int8_t *)buffers + (offset - userBase) * frameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | uint32_t audio_track_cblk_t::framesAvailable() |
| 1632 | { |
| 1633 | Mutex::Autolock _l(lock); |
| 1634 | return framesAvailable_l(); |
| 1635 | } |
| 1636 | |
| 1637 | uint32_t audio_track_cblk_t::framesAvailable_l() |
| 1638 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1639 | uint32_t u = user; |
| 1640 | uint32_t s = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1642 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1643 | uint32_t limit = (s < loopStart) ? s : loopStart; |
| 1644 | return limit + frameCount - u; |
| 1645 | } else { |
| 1646 | return frameCount + u - s; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | uint32_t audio_track_cblk_t::framesReady() |
| 1651 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1652 | uint32_t u = user; |
| 1653 | uint32_t s = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1655 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | if (u < loopEnd) { |
| 1657 | return u - s; |
| 1658 | } else { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1659 | // do not block on mutex shared with client on AudioFlinger side |
| 1660 | if (!tryLock()) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1661 | ALOGW("framesReady() could not lock cblk"); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1662 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | } |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1664 | uint32_t frames = UINT_MAX; |
| 1665 | if (loopCount >= 0) { |
| 1666 | frames = (loopEnd - loopStart)*loopCount + u - s; |
| 1667 | } |
| 1668 | lock.unlock(); |
| 1669 | return frames; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | } |
| 1671 | } else { |
| 1672 | return s - u; |
| 1673 | } |
| 1674 | } |
| 1675 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1676 | bool audio_track_cblk_t::tryLock() |
| 1677 | { |
| 1678 | // the code below simulates lock-with-timeout |
| 1679 | // we MUST do this to protect the AudioFlinger server |
| 1680 | // as this lock is shared with the client. |
| 1681 | status_t err; |
| 1682 | |
| 1683 | err = lock.tryLock(); |
| 1684 | if (err == -EBUSY) { // just wait a bit |
| 1685 | usleep(1000); |
| 1686 | err = lock.tryLock(); |
| 1687 | } |
| 1688 | if (err != NO_ERROR) { |
| 1689 | // probably, the client just died. |
| 1690 | return false; |
| 1691 | } |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | // ------------------------------------------------------------------------- |
| 1696 | |
| 1697 | }; // namespace android |