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