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