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