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