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