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