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