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