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