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 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 22 | #include <math.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <sys/resource.h> |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 24 | #include <audio_utils/primitives.h> |
| 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <media/AudioTrack.h> |
| 27 | #include <utils/Log.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <private/media/AudioTrackShared.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 29 | #include <media/IAudioFlinger.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 31 | #define WAIT_PERIOD_MS 10 |
| 32 | #define WAIT_STREAM_END_TIMEOUT_SEC 120 |
| 33 | |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 34 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | namespace android { |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 36 | // --------------------------------------------------------------------------- |
| 37 | |
| 38 | // static |
| 39 | status_t AudioTrack::getMinFrameCount( |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 40 | size_t* frameCount, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 41 | audio_stream_type_t streamType, |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 42 | uint32_t sampleRate) |
| 43 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 44 | if (frameCount == NULL) { |
| 45 | return BAD_VALUE; |
| 46 | } |
Glenn Kasten | 04cd018 | 2012-06-25 11:49:27 -0700 | [diff] [blame] | 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; |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 55 | status_t status; |
| 56 | status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType); |
| 57 | if (status != NO_ERROR) { |
Glenn Kasten | 70c0bfb | 2014-01-14 15:47:01 -0800 | [diff] [blame] | 58 | ALOGE("Unable to query output sample rate for stream type %d; status %d", |
| 59 | streamType, status); |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 60 | return status; |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 61 | } |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 62 | size_t afFrameCount; |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 63 | status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType); |
| 64 | if (status != NO_ERROR) { |
Glenn Kasten | 70c0bfb | 2014-01-14 15:47:01 -0800 | [diff] [blame] | 65 | ALOGE("Unable to query output frame count for stream type %d; status %d", |
| 66 | streamType, status); |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 67 | return status; |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 68 | } |
| 69 | uint32_t afLatency; |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 70 | status = AudioSystem::getOutputLatency(&afLatency, streamType); |
| 71 | if (status != NO_ERROR) { |
Glenn Kasten | 70c0bfb | 2014-01-14 15:47:01 -0800 | [diff] [blame] | 72 | ALOGE("Unable to query output latency for stream type %d; status %d", |
| 73 | streamType, status); |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 74 | return status; |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Ensure that buffer depth covers at least audio hardware latency |
| 78 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 79 | if (minBufCount < 2) { |
| 80 | minBufCount = 2; |
| 81 | } |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 82 | |
| 83 | *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 84 | afFrameCount * minBufCount * sampleRate / afSampleRate; |
Glenn Kasten | 66a0467 | 2014-01-08 08:53:44 -0800 | [diff] [blame] | 85 | // The formula above should always produce a non-zero value, but return an error |
| 86 | // in the unlikely event that it does not, as that's part of the API contract. |
| 87 | if (*frameCount == 0) { |
| 88 | ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d", |
| 89 | streamType, sampleRate); |
| 90 | return BAD_VALUE; |
| 91 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 92 | ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d", |
| 93 | *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency); |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 94 | return NO_ERROR; |
| 95 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
| 97 | // --------------------------------------------------------------------------- |
| 98 | |
| 99 | AudioTrack::AudioTrack() |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 100 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 101 | mIsTimed(false), |
| 102 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Haynes Mathew George | 7064fd2 | 2014-01-08 13:59:53 -0800 | [diff] [blame] | 103 | mPreviousSchedulingGroup(SP_DEFAULT), |
| 104 | mPausedPosition(0) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | { |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 106 | mAttributes.content_type = AUDIO_CONTENT_TYPE_UNKNOWN; |
| 107 | mAttributes.usage = AUDIO_USAGE_UNKNOWN; |
| 108 | mAttributes.flags = 0x0; |
| 109 | strcpy(mAttributes.tags, ""); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 113 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 115 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 116 | audio_channel_mask_t channelMask, |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 117 | size_t frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 118 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | callback_t cbf, |
| 120 | void* user, |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 121 | uint32_t notificationFrames, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 122 | int sessionId, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 123 | transfer_type transferType, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 124 | const audio_offload_info_t *offloadInfo, |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 125 | int uid, |
| 126 | pid_t pid) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 127 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 128 | mIsTimed(false), |
| 129 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Haynes Mathew George | 7064fd2 | 2014-01-08 13:59:53 -0800 | [diff] [blame] | 130 | mPreviousSchedulingGroup(SP_DEFAULT), |
| 131 | mPausedPosition(0) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 133 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 134 | frameCount, flags, cbf, user, notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 135 | 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType, |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 136 | offloadInfo, uid, pid, NULL /*no audio attributes*/); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 139 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 140 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 142 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 143 | audio_channel_mask_t channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 145 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | callback_t cbf, |
| 147 | void* user, |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 148 | uint32_t notificationFrames, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 149 | int sessionId, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 150 | transfer_type transferType, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 151 | const audio_offload_info_t *offloadInfo, |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 152 | int uid, |
| 153 | pid_t pid) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 154 | : mStatus(NO_INIT), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 155 | mIsTimed(false), |
| 156 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), |
Haynes Mathew George | 7064fd2 | 2014-01-08 13:59:53 -0800 | [diff] [blame] | 157 | mPreviousSchedulingGroup(SP_DEFAULT), |
| 158 | mPausedPosition(0) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 160 | mStatus = set(streamType, sampleRate, format, channelMask, |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 161 | 0 /*frameCount*/, flags, cbf, user, notificationFrames, |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 162 | sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 163 | uid, pid, NULL /*no audio attributes*/); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | AudioTrack::~AudioTrack() |
| 167 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | if (mStatus == NO_ERROR) { |
| 169 | // Make sure that callback function exits in the case where |
| 170 | // it is looping on buffer full condition in obtainBuffer(). |
| 171 | // Otherwise the callback thread will never exit. |
| 172 | stop(); |
| 173 | if (mAudioTrackThread != 0) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 174 | mProxy->interrupt(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 175 | mAudioTrackThread->requestExit(); // see comment in AudioTrack.h |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | mAudioTrackThread->requestExitAndWait(); |
| 177 | mAudioTrackThread.clear(); |
| 178 | } |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 179 | mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this); |
| 180 | mAudioTrack.clear(); |
Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 181 | mCblkMemory.clear(); |
| 182 | mSharedBuffer.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | IPCThreadState::self()->flushCommands(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 184 | ALOGV("~AudioTrack, releasing session id from %d on behalf of %d", |
| 185 | IPCThreadState::self()->getCallingPid(), mClientPid); |
| 186 | AudioSystem::releaseAudioSessionId(mSessionId, mClientPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | status_t AudioTrack::set( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 191 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 193 | audio_format_t format, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 194 | audio_channel_mask_t channelMask, |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 195 | size_t frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 196 | audio_output_flags_t flags, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | callback_t cbf, |
| 198 | void* user, |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 199 | uint32_t notificationFrames, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 201 | bool threadCanCallJava, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 202 | int sessionId, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 203 | transfer_type transferType, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 204 | const audio_offload_info_t *offloadInfo, |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 205 | int uid, |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 206 | pid_t pid, |
| 207 | audio_attributes_t* pAttributes) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | { |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 209 | ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, " |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 210 | "flags #%x, notificationFrames %u, sessionId %d, transferType %d", |
Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 211 | streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames, |
Glenn Kasten | 86f0466 | 2014-02-24 15:13:05 -0800 | [diff] [blame] | 212 | sessionId, transferType); |
| 213 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 214 | switch (transferType) { |
| 215 | case TRANSFER_DEFAULT: |
| 216 | if (sharedBuffer != 0) { |
| 217 | transferType = TRANSFER_SHARED; |
| 218 | } else if (cbf == NULL || threadCanCallJava) { |
| 219 | transferType = TRANSFER_SYNC; |
| 220 | } else { |
| 221 | transferType = TRANSFER_CALLBACK; |
| 222 | } |
| 223 | break; |
| 224 | case TRANSFER_CALLBACK: |
| 225 | if (cbf == NULL || sharedBuffer != 0) { |
| 226 | ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0"); |
| 227 | return BAD_VALUE; |
| 228 | } |
| 229 | break; |
| 230 | case TRANSFER_OBTAIN: |
| 231 | case TRANSFER_SYNC: |
| 232 | if (sharedBuffer != 0) { |
| 233 | ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0"); |
| 234 | return BAD_VALUE; |
| 235 | } |
| 236 | break; |
| 237 | case TRANSFER_SHARED: |
| 238 | if (sharedBuffer == 0) { |
| 239 | ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0"); |
| 240 | return BAD_VALUE; |
| 241 | } |
| 242 | break; |
| 243 | default: |
| 244 | ALOGE("Invalid transfer type %d", transferType); |
| 245 | return BAD_VALUE; |
| 246 | } |
Glenn Kasten | dd5f4c8 | 2014-01-13 10:26:32 -0800 | [diff] [blame] | 247 | mSharedBuffer = sharedBuffer; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 248 | mTransfer = transferType; |
| 249 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 250 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), |
| 251 | sharedBuffer->size()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 253 | ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 254 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 255 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 256 | |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 257 | // invariant that mAudioTrack != 0 is true only after set() returns successfully |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 258 | if (mAudioTrack != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 259 | ALOGE("Track already in use"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | return INVALID_OPERATION; |
| 261 | } |
| 262 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | // handle default values first. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 264 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 265 | streamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 267 | |
| 268 | if (pAttributes == NULL) { |
| 269 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
| 270 | ALOGE("Invalid stream type %d", streamType); |
| 271 | return BAD_VALUE; |
| 272 | } |
| 273 | setAttributesFromStreamType(streamType); |
| 274 | mStreamType = streamType; |
| 275 | } else { |
| 276 | if (!isValidAttributes(pAttributes)) { |
| 277 | ALOGE("Invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 278 | pAttributes->usage, pAttributes->content_type, pAttributes->flags, |
| 279 | pAttributes->tags); |
| 280 | } |
| 281 | // stream type shouldn't be looked at, this track has audio attributes |
| 282 | memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t)); |
| 283 | setStreamTypeFromAttributes(mAttributes); |
| 284 | ALOGV("Building AudioTrack with attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 285 | mAttributes.usage, mAttributes.content_type, mAttributes.flags, mAttributes.tags); |
Glenn Kasten | dd5f4c8 | 2014-01-13 10:26:32 -0800 | [diff] [blame] | 286 | } |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 287 | |
Glenn Kasten | b1bef51 | 2014-01-13 10:25:53 -0800 | [diff] [blame] | 288 | status_t status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | if (sampleRate == 0) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame^] | 290 | status = AudioSystem::getOutputSamplingRateForAttr(&sampleRate, &mAttributes); |
Glenn Kasten | b1bef51 | 2014-01-13 10:25:53 -0800 | [diff] [blame] | 291 | if (status != NO_ERROR) { |
| 292 | ALOGE("Could not get output sample rate for stream type %d; status %d", |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 293 | mStreamType, status); |
Glenn Kasten | b1bef51 | 2014-01-13 10:25:53 -0800 | [diff] [blame] | 294 | return status; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 295 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 297 | mSampleRate = sampleRate; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 298 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | // these below should probably come from the audioFlinger too... |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 300 | if (format == AUDIO_FORMAT_DEFAULT) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 301 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | |
| 304 | // validate parameters |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 305 | if (!audio_is_valid_format(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 306 | ALOGE("Invalid format %#x", format); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | return BAD_VALUE; |
| 308 | } |
Glenn Kasten | dd5f4c8 | 2014-01-13 10:26:32 -0800 | [diff] [blame] | 309 | mFormat = format; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 310 | |
Glenn Kasten | 8ba9032 | 2013-10-30 11:29:27 -0700 | [diff] [blame] | 311 | if (!audio_is_output_channel(channelMask)) { |
| 312 | ALOGE("Invalid channel mask %#x", channelMask); |
| 313 | return BAD_VALUE; |
| 314 | } |
Glenn Kasten | e3247bf | 2014-02-24 15:19:07 -0800 | [diff] [blame] | 315 | mChannelMask = channelMask; |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 316 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
Glenn Kasten | e3247bf | 2014-02-24 15:19:07 -0800 | [diff] [blame] | 317 | mChannelCount = channelCount; |
Glenn Kasten | 8ba9032 | 2013-10-30 11:29:27 -0700 | [diff] [blame] | 318 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 319 | // AudioFlinger does not currently support 8-bit data in shared memory |
| 320 | if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) { |
| 321 | ALOGE("8-bit data in shared memory is not supported"); |
| 322 | return BAD_VALUE; |
| 323 | } |
| 324 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 325 | // force direct flag if format is not linear PCM |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 326 | // or offload was requested |
| 327 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 328 | || !audio_is_linear_pcm(format)) { |
| 329 | ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 330 | ? "Offload request, forcing to Direct Output" |
| 331 | : "Not linear PCM, forcing to Direct Output"); |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 332 | flags = (audio_output_flags_t) |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 333 | // FIXME why can't we allow direct AND fast? |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 334 | ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 335 | } |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 336 | // only allow deep buffering for music stream type |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 337 | if (mStreamType != AUDIO_STREAM_MUSIC) { |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 338 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 339 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 340 | |
Glenn Kasten | b773038 | 2014-04-30 15:50:31 -0700 | [diff] [blame] | 341 | if (flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 342 | if (audio_is_linear_pcm(format)) { |
| 343 | mFrameSize = channelCount * audio_bytes_per_sample(format); |
| 344 | } else { |
| 345 | mFrameSize = sizeof(uint8_t); |
| 346 | } |
| 347 | mFrameSizeAF = mFrameSize; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 348 | } else { |
Glenn Kasten | b773038 | 2014-04-30 15:50:31 -0700 | [diff] [blame] | 349 | ALOG_ASSERT(audio_is_linear_pcm(format)); |
| 350 | mFrameSize = channelCount * audio_bytes_per_sample(format); |
| 351 | mFrameSizeAF = channelCount * audio_bytes_per_sample( |
| 352 | format == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : format); |
| 353 | // createTrack will return an error if PCM format is not supported by server, |
| 354 | // so no need to check for specific PCM formats here |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Glenn Kasten | b5ccb2d | 2014-01-13 14:42:43 -0800 | [diff] [blame] | 357 | // Make copy of input parameter offloadInfo so that in the future: |
| 358 | // (a) createTrack_l doesn't need it as an input parameter |
| 359 | // (b) we can support re-creation of offloaded tracks |
| 360 | if (offloadInfo != NULL) { |
| 361 | mOffloadInfoCopy = *offloadInfo; |
| 362 | mOffloadInfo = &mOffloadInfoCopy; |
| 363 | } else { |
| 364 | mOffloadInfo = NULL; |
| 365 | } |
| 366 | |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 367 | mVolume[AUDIO_INTERLEAVE_LEFT] = 1.0f; |
| 368 | mVolume[AUDIO_INTERLEAVE_RIGHT] = 1.0f; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 369 | mSendLevel = 0.0f; |
Glenn Kasten | 396fabd | 2014-01-08 08:54:23 -0800 | [diff] [blame] | 370 | // mFrameCount is initialized in createTrack_l |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 371 | mReqFrameCount = frameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 372 | mNotificationFramesReq = notificationFrames; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 373 | mNotificationFramesAct = 0; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 374 | mSessionId = sessionId; |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 375 | int callingpid = IPCThreadState::self()->getCallingPid(); |
| 376 | int mypid = getpid(); |
| 377 | if (uid == -1 || (callingpid != mypid)) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 378 | mClientUid = IPCThreadState::self()->getCallingUid(); |
| 379 | } else { |
| 380 | mClientUid = uid; |
| 381 | } |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 382 | if (pid == -1 || (callingpid != mypid)) { |
| 383 | mClientPid = callingpid; |
| 384 | } else { |
| 385 | mClientPid = pid; |
| 386 | } |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 387 | mAuxEffectId = 0; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 388 | mFlags = flags; |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 389 | mCbf = cbf; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 390 | |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 391 | if (cbf != NULL) { |
Eric Laurent | 896adcd | 2012-09-13 11:18:23 -0700 | [diff] [blame] | 392 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 393 | mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/); |
| 394 | } |
| 395 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 396 | // create the IAudioTrack |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 397 | status = createTrack_l(0 /*epoch*/); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 398 | |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 399 | if (status != NO_ERROR) { |
| 400 | if (mAudioTrackThread != 0) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 401 | mAudioTrackThread->requestExit(); // see comment in AudioTrack.h |
| 402 | mAudioTrackThread->requestExitAndWait(); |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 403 | mAudioTrackThread.clear(); |
| 404 | } |
| 405 | return status; |
Glenn Kasten | 5d464eb | 2012-06-22 17:19:53 -0700 | [diff] [blame] | 406 | } |
| 407 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | mStatus = NO_ERROR; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 409 | mState = STATE_STOPPED; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | mUserData = user; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 411 | mLoopPeriod = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | mMarkerPosition = 0; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 413 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | mNewPosition = 0; |
| 415 | mUpdatePeriod = 0; |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 416 | AudioSystem::acquireAudioSessionId(mSessionId, mClientPid); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 417 | mSequence = 1; |
| 418 | mObservedSequence = mSequence; |
| 419 | mInUnderrun = false; |
| 420 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return NO_ERROR; |
| 422 | } |
| 423 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | // ------------------------------------------------------------------------- |
| 425 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 426 | status_t AudioTrack::start() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 428 | AutoMutex lock(mLock); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 429 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 430 | if (mState == STATE_ACTIVE) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 431 | return INVALID_OPERATION; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 434 | mInUnderrun = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 436 | State previousState = mState; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 437 | if (previousState == STATE_PAUSED_STOPPING) { |
| 438 | mState = STATE_STOPPING; |
| 439 | } else { |
| 440 | mState = STATE_ACTIVE; |
| 441 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 442 | if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) { |
| 443 | // reset current position as seen by client to 0 |
| 444 | mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition()); |
Eric Laurent | ec9a032 | 2013-08-28 10:23:01 -0700 | [diff] [blame] | 445 | // force refresh of remaining frames by processAudioBuffer() as last |
| 446 | // write before stop could be partial. |
| 447 | mRefreshRemaining = true; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 448 | } |
| 449 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 450 | int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 451 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | sp<AudioTrackThread> t = mAudioTrackThread; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 453 | if (t != 0) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 454 | if (previousState == STATE_STOPPING) { |
| 455 | mProxy->interrupt(); |
| 456 | } else { |
| 457 | t->resume(); |
| 458 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 459 | } else { |
| 460 | mPreviousPriority = getpriority(PRIO_PROCESS, 0); |
| 461 | get_sched_policy(0, &mPreviousSchedulingGroup); |
| 462 | androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); |
| 463 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 465 | status_t status = NO_ERROR; |
| 466 | if (!(flags & CBLK_INVALID)) { |
| 467 | status = mAudioTrack->start(); |
| 468 | if (status == DEAD_OBJECT) { |
| 469 | flags |= CBLK_INVALID; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 471 | } |
| 472 | if (flags & CBLK_INVALID) { |
| 473 | status = restoreTrack_l("start"); |
| 474 | } |
| 475 | |
| 476 | if (status != NO_ERROR) { |
| 477 | ALOGE("start() status %d", status); |
| 478 | mState = previousState; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | if (t != 0) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 480 | if (previousState != STATE_STOPPING) { |
| 481 | t->pause(); |
| 482 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 484 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 485 | set_sched_policy(0, mPreviousSchedulingGroup); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 489 | return status; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void AudioTrack::stop() |
| 493 | { |
| 494 | AutoMutex lock(mLock); |
Glenn Kasten | 397edb3 | 2013-08-30 15:10:13 -0700 | [diff] [blame] | 495 | if (mState != STATE_ACTIVE && mState != STATE_PAUSED) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 496 | return; |
| 497 | } |
| 498 | |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 499 | if (isOffloaded_l()) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 500 | mState = STATE_STOPPING; |
| 501 | } else { |
| 502 | mState = STATE_STOPPED; |
| 503 | } |
| 504 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 505 | mProxy->interrupt(); |
| 506 | mAudioTrack->stop(); |
| 507 | // the playback head position will reset to 0, so if a marker is set, we need |
| 508 | // to activate it again |
| 509 | mMarkerReached = false; |
| 510 | #if 0 |
| 511 | // Force flush if a shared buffer is used otherwise audioflinger |
| 512 | // will not stop before end of buffer is reached. |
| 513 | // It may be needed to make sure that we stop playback, likely in case looping is on. |
| 514 | if (mSharedBuffer != 0) { |
| 515 | flush_l(); |
| 516 | } |
| 517 | #endif |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 518 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 519 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 520 | if (t != 0) { |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 521 | if (!isOffloaded_l()) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 522 | t->pause(); |
| 523 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 524 | } else { |
| 525 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
| 526 | set_sched_policy(0, mPreviousSchedulingGroup); |
| 527 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | bool AudioTrack::stopped() const |
| 531 | { |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 532 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 533 | return mState != STATE_ACTIVE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void AudioTrack::flush() |
| 537 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 538 | if (mSharedBuffer != 0) { |
| 539 | return; |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 540 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 541 | AutoMutex lock(mLock); |
| 542 | if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) { |
| 543 | return; |
| 544 | } |
| 545 | flush_l(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 548 | void AudioTrack::flush_l() |
| 549 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 550 | ALOG_ASSERT(mState != STATE_ACTIVE); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 551 | |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 552 | // clear playback marker and periodic update counter |
| 553 | mMarkerPosition = 0; |
| 554 | mMarkerReached = false; |
| 555 | mUpdatePeriod = 0; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 556 | mRefreshRemaining = true; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 557 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 558 | mState = STATE_FLUSHED; |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 559 | if (isOffloaded_l()) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 560 | mProxy->interrupt(); |
| 561 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 562 | mProxy->flush(); |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 563 | mAudioTrack->flush(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void AudioTrack::pause() |
| 567 | { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 568 | AutoMutex lock(mLock); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 569 | if (mState == STATE_ACTIVE) { |
| 570 | mState = STATE_PAUSED; |
| 571 | } else if (mState == STATE_STOPPING) { |
| 572 | mState = STATE_PAUSED_STOPPING; |
| 573 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 574 | return; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 576 | mProxy->interrupt(); |
| 577 | mAudioTrack->pause(); |
Haynes Mathew George | 7064fd2 | 2014-01-08 13:59:53 -0800 | [diff] [blame] | 578 | |
Marco Nelissen | 3a90f28 | 2014-03-10 11:21:43 -0700 | [diff] [blame] | 579 | if (isOffloaded_l()) { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 580 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
Haynes Mathew George | 7064fd2 | 2014-01-08 13:59:53 -0800 | [diff] [blame] | 581 | uint32_t halFrames; |
| 582 | // OffloadThread sends HAL pause in its threadLoop.. time saved |
| 583 | // here can be slightly off |
| 584 | AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition); |
| 585 | ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition); |
| 586 | } |
| 587 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 590 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | { |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 592 | // This duplicates a test by AudioTrack JNI, but that is not the only caller |
| 593 | if (isnanf(left) || left < GAIN_FLOAT_ZERO || left > GAIN_FLOAT_UNITY || |
| 594 | isnanf(right) || right < GAIN_FLOAT_ZERO || right > GAIN_FLOAT_UNITY) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 595 | return BAD_VALUE; |
| 596 | } |
| 597 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 598 | AutoMutex lock(mLock); |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 599 | mVolume[AUDIO_INTERLEAVE_LEFT] = left; |
| 600 | mVolume[AUDIO_INTERLEAVE_RIGHT] = right; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 602 | mProxy->setVolumeLR(gain_minifloat_pack(gain_from_float(left), gain_from_float(right))); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 603 | |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 604 | if (isOffloaded_l()) { |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 605 | mAudioTrack->signal(); |
| 606 | } |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 607 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Glenn Kasten | b1c0993 | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 610 | status_t AudioTrack::setVolume(float volume) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | { |
Glenn Kasten | b1c0993 | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 612 | return setVolume(volume, volume); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 615 | status_t AudioTrack::setAuxEffectSendLevel(float level) |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 616 | { |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 617 | // This duplicates a test by AudioTrack JNI, but that is not the only caller |
| 618 | if (isnanf(level) || level < GAIN_FLOAT_ZERO || level > GAIN_FLOAT_UNITY) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 619 | return BAD_VALUE; |
| 620 | } |
| 621 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 622 | AutoMutex lock(mLock); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 623 | mSendLevel = level; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 624 | mProxy->setSendLevel(level); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 625 | |
| 626 | return NO_ERROR; |
| 627 | } |
| 628 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 629 | void AudioTrack::getAuxEffectSendLevel(float* level) const |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 630 | { |
| 631 | if (level != NULL) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 632 | *level = mSendLevel; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 633 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 636 | status_t AudioTrack::setSampleRate(uint32_t rate) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 638 | if (mIsTimed || isOffloaded()) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 639 | return INVALID_OPERATION; |
| 640 | } |
| 641 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 642 | uint32_t afSamplingRate; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame^] | 643 | if (AudioSystem::getOutputSamplingRateForAttr(&afSamplingRate, &mAttributes) != NO_ERROR) { |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 644 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | } |
| 646 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 647 | if (rate == 0 || rate > afSamplingRate*2 ) { |
| 648 | return BAD_VALUE; |
| 649 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 651 | AutoMutex lock(mLock); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 652 | mSampleRate = rate; |
| 653 | mProxy->setSampleRate(rate); |
| 654 | |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 655 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 658 | uint32_t AudioTrack::getSampleRate() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 660 | if (mIsTimed) { |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 661 | return 0; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 662 | } |
| 663 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 664 | AutoMutex lock(mLock); |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 665 | |
| 666 | // sample rate can be updated during playback by the offloaded decoder so we need to |
| 667 | // query the HAL and update if needed. |
| 668 | // FIXME use Proxy return channel to update the rate from server and avoid polling here |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 669 | if (isOffloaded_l()) { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 670 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 671 | uint32_t sampleRate = 0; |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 672 | status_t status = AudioSystem::getSamplingRate(mOutput, &sampleRate); |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 673 | if (status == NO_ERROR) { |
| 674 | mSampleRate = sampleRate; |
| 675 | } |
| 676 | } |
| 677 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 678 | return mSampleRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 682 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 683 | if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) { |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 684 | return INVALID_OPERATION; |
| 685 | } |
| 686 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | if (loopCount == 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 688 | ; |
| 689 | } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount && |
| 690 | loopEnd - loopStart >= MIN_LOOP) { |
| 691 | ; |
| 692 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | return BAD_VALUE; |
| 694 | } |
| 695 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 696 | AutoMutex lock(mLock); |
| 697 | // See setPosition() regarding setting parameters such as loop points or position while active |
| 698 | if (mState == STATE_ACTIVE) { |
| 699 | return INVALID_OPERATION; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 700 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 701 | setLoop_l(loopStart, loopEnd, loopCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | return NO_ERROR; |
| 703 | } |
| 704 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 705 | void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 706 | { |
| 707 | // FIXME If setting a loop also sets position to start of loop, then |
| 708 | // this is correct. Otherwise it should be removed. |
| 709 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
| 710 | mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0; |
| 711 | mStaticProxy->setLoop(loopStart, loopEnd, loopCount); |
| 712 | } |
| 713 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 715 | { |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 716 | // The only purpose of setting marker position is to get a callback |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 717 | if (mCbf == NULL || isOffloaded()) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 718 | return INVALID_OPERATION; |
| 719 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 721 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | mMarkerPosition = marker; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 723 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | |
| 725 | return NO_ERROR; |
| 726 | } |
| 727 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 728 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 730 | if (isOffloaded()) { |
| 731 | return INVALID_OPERATION; |
| 732 | } |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 733 | if (marker == NULL) { |
| 734 | return BAD_VALUE; |
| 735 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 737 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | *marker = mMarkerPosition; |
| 739 | |
| 740 | return NO_ERROR; |
| 741 | } |
| 742 | |
| 743 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 744 | { |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 745 | // The only purpose of setting position update period is to get a callback |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 746 | if (mCbf == NULL || isOffloaded()) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 747 | return INVALID_OPERATION; |
| 748 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 750 | AutoMutex lock(mLock); |
| 751 | mNewPosition = mProxy->getPosition() + updatePeriod; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | mUpdatePeriod = updatePeriod; |
Glenn Kasten | 2b2165c | 2014-01-13 08:53:36 -0800 | [diff] [blame] | 753 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | return NO_ERROR; |
| 755 | } |
| 756 | |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 757 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 759 | if (isOffloaded()) { |
| 760 | return INVALID_OPERATION; |
| 761 | } |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 762 | if (updatePeriod == NULL) { |
| 763 | return BAD_VALUE; |
| 764 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 766 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | *updatePeriod = mUpdatePeriod; |
| 768 | |
| 769 | return NO_ERROR; |
| 770 | } |
| 771 | |
| 772 | status_t AudioTrack::setPosition(uint32_t position) |
| 773 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 774 | if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 775 | return INVALID_OPERATION; |
| 776 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 777 | if (position > mFrameCount) { |
| 778 | return BAD_VALUE; |
| 779 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 780 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 781 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 782 | // Currently we require that the player is inactive before setting parameters such as position |
| 783 | // or loop points. Otherwise, there could be a race condition: the application could read the |
| 784 | // current position, compute a new position or loop parameters, and then set that position or |
| 785 | // loop parameters but it would do the "wrong" thing since the position has continued to advance |
| 786 | // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app |
| 787 | // to specify how it wants to handle such scenarios. |
| 788 | if (mState == STATE_ACTIVE) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 789 | return INVALID_OPERATION; |
| 790 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 791 | mNewPosition = mProxy->getPosition() + mUpdatePeriod; |
| 792 | mLoopPeriod = 0; |
| 793 | // FIXME Check whether loops and setting position are incompatible in old code. |
| 794 | // If we use setLoop for both purposes we lose the capability to set the position while looping. |
| 795 | mStaticProxy->setLoop(position, mFrameCount, 0); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 796 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | return NO_ERROR; |
| 798 | } |
| 799 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 800 | status_t AudioTrack::getPosition(uint32_t *position) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 802 | if (position == NULL) { |
| 803 | return BAD_VALUE; |
| 804 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 805 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 806 | AutoMutex lock(mLock); |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 807 | if (isOffloaded_l()) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 808 | uint32_t dspFrames = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | |
Haynes Mathew George | 7064fd2 | 2014-01-08 13:59:53 -0800 | [diff] [blame] | 810 | if ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING)) { |
| 811 | ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition); |
| 812 | *position = mPausedPosition; |
| 813 | return NO_ERROR; |
| 814 | } |
| 815 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 816 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 817 | uint32_t halFrames; |
| 818 | AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames); |
| 819 | } |
| 820 | *position = dspFrames; |
| 821 | } else { |
| 822 | // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes |
| 823 | *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 : |
| 824 | mProxy->getPosition(); |
| 825 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | return NO_ERROR; |
| 827 | } |
| 828 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 829 | status_t AudioTrack::getBufferPosition(uint32_t *position) |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 830 | { |
| 831 | if (mSharedBuffer == 0 || mIsTimed) { |
| 832 | return INVALID_OPERATION; |
| 833 | } |
| 834 | if (position == NULL) { |
| 835 | return BAD_VALUE; |
| 836 | } |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 837 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 838 | AutoMutex lock(mLock); |
| 839 | *position = mStaticProxy->getBufferPosition(); |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 840 | return NO_ERROR; |
| 841 | } |
Glenn Kasten | 9c6745f | 2012-11-30 13:35:29 -0800 | [diff] [blame] | 842 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | status_t AudioTrack::reload() |
| 844 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 845 | if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) { |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 846 | return INVALID_OPERATION; |
| 847 | } |
| 848 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 849 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 850 | // See setPosition() regarding setting parameters such as loop points or position while active |
| 851 | if (mState == STATE_ACTIVE) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 852 | return INVALID_OPERATION; |
| 853 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 854 | mNewPosition = mUpdatePeriod; |
| 855 | mLoopPeriod = 0; |
| 856 | // FIXME The new code cannot reload while keeping a loop specified. |
| 857 | // Need to check how the old code handled this, and whether it's a significant change. |
| 858 | mStaticProxy->setLoop(0, mFrameCount, 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | return NO_ERROR; |
| 860 | } |
| 861 | |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 862 | audio_io_handle_t AudioTrack::getOutput() const |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 863 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 864 | AutoMutex lock(mLock); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 865 | return mOutput; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 868 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 869 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 870 | AutoMutex lock(mLock); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 871 | status_t status = mAudioTrack->attachAuxEffect(effectId); |
| 872 | if (status == NO_ERROR) { |
| 873 | mAuxEffectId = effectId; |
| 874 | } |
| 875 | return status; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 876 | } |
| 877 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | // ------------------------------------------------------------------------- |
| 879 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 880 | // must be called with mLock held |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 881 | status_t AudioTrack::createTrack_l(size_t epoch) |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 882 | { |
| 883 | status_t status; |
| 884 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 885 | if (audioFlinger == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 886 | ALOGE("Could not get audioflinger"); |
| 887 | return NO_INIT; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 888 | } |
| 889 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame^] | 890 | audio_io_handle_t output = AudioSystem::getOutputForAttr(&mAttributes, mSampleRate, mFormat, |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 891 | mChannelMask, mFlags, mOffloadInfo); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 892 | if (output == AUDIO_IO_HANDLE_NONE) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame^] | 893 | ALOGE("Could not get audio output for stream type %d, usage %d, sample rate %u, format %#x," |
| 894 | " channel mask %#x, flags %#x", |
| 895 | mStreamType, mAttributes.usage, mSampleRate, mFormat, mChannelMask, mFlags); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 896 | return BAD_VALUE; |
| 897 | } |
| 898 | { |
| 899 | // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger, |
| 900 | // we must release it ourselves if anything goes wrong. |
| 901 | |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 902 | // Not all of these values are needed under all conditions, but it is easier to get them all |
| 903 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 904 | uint32_t afLatency; |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 905 | status = AudioSystem::getLatency(output, &afLatency); |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 906 | if (status != NO_ERROR) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 907 | ALOGE("getLatency(%d) failed status %d", output, status); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 908 | goto release; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 911 | size_t afFrameCount; |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 912 | status = AudioSystem::getFrameCount(output, &afFrameCount); |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 913 | if (status != NO_ERROR) { |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 914 | ALOGE("getFrameCount(output=%d) status %d", output, status); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 915 | goto release; |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | uint32_t afSampleRate; |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 919 | status = AudioSystem::getSamplingRate(output, &afSampleRate); |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 920 | if (status != NO_ERROR) { |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 921 | ALOGE("getSamplingRate(output=%d) status %d", output, status); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 922 | goto release; |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 925 | // Client decides whether the track is TIMED (see below), but can only express a preference |
| 926 | // for FAST. Server will perform additional tests. |
Glenn Kasten | 43bdc1d | 2014-02-10 09:53:55 -0800 | [diff] [blame] | 927 | if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !(( |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 928 | // either of these use cases: |
| 929 | // use case 1: shared buffer |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 930 | (mSharedBuffer != 0) || |
Glenn Kasten | c6ba823 | 2014-02-27 13:34:29 -0800 | [diff] [blame] | 931 | // use case 2: callback transfer mode |
| 932 | (mTransfer == TRANSFER_CALLBACK)) && |
Glenn Kasten | 43bdc1d | 2014-02-10 09:53:55 -0800 | [diff] [blame] | 933 | // matching sample rate |
| 934 | (mSampleRate == afSampleRate))) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 935 | ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client"); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 936 | // once denied, do not request again if IAudioTrack is re-created |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 937 | mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST); |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 938 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 939 | ALOGV("createTrack_l() output %d afLatency %d", output, afLatency); |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 940 | |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 941 | // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 942 | // n = 1 fast track with single buffering; nBuffering is ignored |
| 943 | // n = 2 fast track with double buffering |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 944 | // n = 2 normal track, no sample rate conversion |
| 945 | // n = 3 normal track, with sample rate conversion |
| 946 | // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering) |
| 947 | // n > 3 very high latency or very small notification interval; nBuffering is ignored |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 948 | const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3; |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 949 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 950 | mNotificationFramesAct = mNotificationFramesReq; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 951 | |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 952 | size_t frameCount = mReqFrameCount; |
| 953 | if (!audio_is_linear_pcm(mFormat)) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 954 | |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 955 | if (mSharedBuffer != 0) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 956 | // Same comment as below about ignoring frameCount parameter for set() |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 957 | frameCount = mSharedBuffer->size(); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 958 | } else if (frameCount == 0) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 959 | frameCount = afFrameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 960 | } |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 961 | if (mNotificationFramesAct != frameCount) { |
| 962 | mNotificationFramesAct = frameCount; |
| 963 | } |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 964 | } else if (mSharedBuffer != 0) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 965 | |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 966 | // Ensure that buffer alignment matches channel count |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 967 | // 8-bit data in shared memory is not currently supported by AudioFlinger |
Glenn Kasten | b773038 | 2014-04-30 15:50:31 -0700 | [diff] [blame] | 968 | size_t alignment = audio_bytes_per_sample( |
| 969 | mFormat == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : mFormat); |
| 970 | if (alignment & 1) { |
| 971 | alignment = 1; |
| 972 | } |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 973 | if (mChannelCount > 1) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 974 | // More than 2 channels does not require stronger alignment than stereo |
| 975 | alignment <<= 1; |
| 976 | } |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 977 | if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) { |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 978 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 979 | mSharedBuffer->pointer(), mChannelCount); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 980 | status = BAD_VALUE; |
| 981 | goto release; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // When initializing a shared buffer AudioTrack via constructors, |
| 985 | // there's no frameCount parameter. |
| 986 | // But when initializing a shared buffer AudioTrack via set(), |
| 987 | // there _is_ a frameCount parameter. We silently ignore it. |
Glenn Kasten | b773038 | 2014-04-30 15:50:31 -0700 | [diff] [blame] | 988 | frameCount = mSharedBuffer->size() / mFrameSizeAF; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 989 | |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 990 | } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 991 | |
| 992 | // FIXME move these calculations and associated checks to server |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 993 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 994 | // Ensure that buffer depth covers at least audio hardware latency |
| 995 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
Glenn Kasten | bb6f0a0 | 2013-06-03 15:00:29 -0700 | [diff] [blame] | 996 | ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d", |
| 997 | afFrameCount, minBufCount, afSampleRate, afLatency); |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 998 | if (minBufCount <= nBuffering) { |
| 999 | minBufCount = nBuffering; |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 1000 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1001 | |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1002 | size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate; |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1003 | ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u" |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1004 | ", afLatency=%d", |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1005 | minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1006 | |
| 1007 | if (frameCount == 0) { |
| 1008 | frameCount = minFrameCount; |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 1009 | } else if (frameCount < minFrameCount) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1010 | // not ALOGW because it happens all the time when playing key clicks over A2DP |
| 1011 | ALOGV("Minimum buffer size corrected from %d to %d", |
| 1012 | frameCount, minFrameCount); |
| 1013 | frameCount = minFrameCount; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1014 | } |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 1015 | // Make sure that application is notified with sufficient margin before underrun |
| 1016 | if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) { |
| 1017 | mNotificationFramesAct = frameCount/nBuffering; |
| 1018 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1019 | |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1020 | } else { |
| 1021 | // 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] | 1022 | } |
| 1023 | |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 1024 | IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT; |
| 1025 | if (mIsTimed) { |
| 1026 | trackFlags |= IAudioFlinger::TRACK_TIMED; |
| 1027 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1028 | |
| 1029 | pid_t tid = -1; |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1030 | if (mFlags & AUDIO_OUTPUT_FLAG_FAST) { |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 1031 | trackFlags |= IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1032 | if (mAudioTrackThread != 0) { |
| 1033 | tid = mAudioTrackThread->getTid(); |
| 1034 | } |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1037 | if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1038 | trackFlags |= IAudioFlinger::TRACK_OFFLOAD; |
| 1039 | } |
| 1040 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1041 | size_t temp = frameCount; // temp may be replaced by a revised value of frameCount, |
| 1042 | // but we will still need the original value also |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1043 | sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType, |
| 1044 | mSampleRate, |
Glenn Kasten | 60a8392 | 2012-06-21 12:56:37 -0700 | [diff] [blame] | 1045 | // AudioFlinger only sees 16-bit PCM |
Glenn Kasten | c4b88a8 | 2014-04-30 16:54:30 -0700 | [diff] [blame] | 1046 | mFormat == AUDIO_FORMAT_PCM_8_BIT && |
| 1047 | !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT) ? |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1048 | AUDIO_FORMAT_PCM_16_BIT : mFormat, |
Glenn Kasten | a42ff00 | 2012-11-14 12:47:55 -0800 | [diff] [blame] | 1049 | mChannelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1050 | &temp, |
Glenn Kasten | e0b0717 | 2012-11-06 15:03:34 -0800 | [diff] [blame] | 1051 | &trackFlags, |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1052 | mSharedBuffer, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1053 | output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1054 | tid, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1055 | &mSessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1056 | mClientUid, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1057 | &status); |
| 1058 | |
Glenn Kasten | c08d20b | 2014-02-24 15:21:10 -0800 | [diff] [blame] | 1059 | if (status != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1060 | ALOGE("AudioFlinger could not create track, status: %d", status); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1061 | goto release; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1062 | } |
Glenn Kasten | c08d20b | 2014-02-24 15:21:10 -0800 | [diff] [blame] | 1063 | ALOG_ASSERT(track != 0); |
| 1064 | |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1065 | // AudioFlinger now owns the reference to the I/O handle, |
| 1066 | // so we are no longer responsible for releasing it. |
| 1067 | |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1068 | sp<IMemory> iMem = track->getCblk(); |
| 1069 | if (iMem == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1070 | ALOGE("Could not get control block"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1071 | return NO_INIT; |
| 1072 | } |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1073 | void *iMemPointer = iMem->pointer(); |
| 1074 | if (iMemPointer == NULL) { |
| 1075 | ALOGE("Could not get control block pointer"); |
| 1076 | return NO_INIT; |
| 1077 | } |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 1078 | // invariant that mAudioTrack != 0 is true only after set() returns successfully |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1079 | if (mAudioTrack != 0) { |
| 1080 | mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this); |
| 1081 | mDeathNotifier.clear(); |
| 1082 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1083 | mAudioTrack = track; |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1084 | mCblkMemory = iMem; |
Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 1085 | IPCThreadState::self()->flushCommands(); |
| 1086 | |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1087 | audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer); |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1088 | mCblk = cblk; |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1089 | // note that temp is the (possibly revised) value of frameCount |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1090 | if (temp < frameCount || (frameCount == 0 && temp == 0)) { |
| 1091 | // In current design, AudioTrack client checks and ensures frame count validity before |
| 1092 | // passing it to AudioFlinger so AudioFlinger should not return a different value except |
| 1093 | // for fast track as it uses a special method of assigning frame count. |
| 1094 | ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp); |
| 1095 | } |
| 1096 | frameCount = temp; |
Glenn Kasten | 5f63151 | 2014-02-24 15:16:07 -0800 | [diff] [blame] | 1097 | |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 1098 | mAwaitBoost = false; |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1099 | if (mFlags & AUDIO_OUTPUT_FLAG_FAST) { |
Glenn Kasten | e0b0717 | 2012-11-06 15:03:34 -0800 | [diff] [blame] | 1100 | if (trackFlags & IAudioFlinger::TRACK_FAST) { |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1101 | ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount); |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 1102 | mAwaitBoost = true; |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1103 | if (mSharedBuffer == 0) { |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1104 | // Theoretically double-buffering is not required for fast tracks, |
| 1105 | // due to tighter scheduling. But in practice, to accommodate kernels with |
| 1106 | // scheduling jitter, and apps with computation jitter, we use double-buffering. |
| 1107 | if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) { |
| 1108 | mNotificationFramesAct = frameCount/nBuffering; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1109 | } |
| 1110 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1111 | } else { |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1112 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount); |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 1113 | // once denied, do not request again if IAudioTrack is re-created |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1114 | mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 1115 | if (mSharedBuffer == 0) { |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 1116 | if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) { |
| 1117 | mNotificationFramesAct = frameCount/nBuffering; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1120 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1121 | } |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1122 | if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1123 | if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) { |
| 1124 | ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful"); |
| 1125 | } else { |
| 1126 | ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server"); |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1127 | mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1128 | // FIXME This is a warning, not an error, so don't return error status |
| 1129 | //return NO_INIT; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1133 | // We retain a copy of the I/O handle, but don't own the reference |
| 1134 | mOutput = output; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1135 | mRefreshRemaining = true; |
| 1136 | |
| 1137 | // Starting address of buffers in shared memory. If there is a shared buffer, buffers |
| 1138 | // is the value of pointer() for the shared buffer, otherwise buffers points |
| 1139 | // immediately after the control block. This address is for the mapping within client |
| 1140 | // address space. AudioFlinger::TrackBase::mBuffer is for the server address space. |
| 1141 | void* buffers; |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1142 | if (mSharedBuffer == 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1143 | buffers = (char*)cblk + sizeof(audio_track_cblk_t); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1144 | } else { |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1145 | buffers = mSharedBuffer->pointer(); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1148 | mAudioTrack->attachAuxEffect(mAuxEffectId); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1149 | // FIXME don't believe this lie |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1150 | mLatency = afLatency + (1000*frameCount) / mSampleRate; |
Glenn Kasten | 5f63151 | 2014-02-24 15:16:07 -0800 | [diff] [blame] | 1151 | |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1152 | mFrameCount = frameCount; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 1153 | // If IAudioTrack is re-created, don't let the requested frameCount |
| 1154 | // decrease. This can confuse clients that cache frameCount(). |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1155 | if (frameCount > mReqFrameCount) { |
| 1156 | mReqFrameCount = frameCount; |
Glenn Kasten | 093000f | 2012-05-03 09:35:36 -0700 | [diff] [blame] | 1157 | } |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1158 | |
| 1159 | // update proxy |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1160 | if (mSharedBuffer == 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1161 | mStaticProxy.clear(); |
| 1162 | mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF); |
| 1163 | } else { |
| 1164 | mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF); |
| 1165 | mProxy = mStaticProxy; |
| 1166 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1167 | mProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1168 | mProxy->setSendLevel(mSendLevel); |
| 1169 | mProxy->setSampleRate(mSampleRate); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1170 | mProxy->setEpoch(epoch); |
| 1171 | mProxy->setMinimum(mNotificationFramesAct); |
| 1172 | |
| 1173 | mDeathNotifier = new DeathNotifier(this); |
| 1174 | mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1175 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1176 | return NO_ERROR; |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | release: |
| 1180 | AudioSystem::releaseOutput(output); |
| 1181 | if (status == NO_ERROR) { |
| 1182 | status = NO_INIT; |
| 1183 | } |
| 1184 | return status; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 1188 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1189 | if (audioBuffer == NULL) { |
| 1190 | return BAD_VALUE; |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1191 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1192 | if (mTransfer != TRANSFER_OBTAIN) { |
| 1193 | audioBuffer->frameCount = 0; |
| 1194 | audioBuffer->size = 0; |
| 1195 | audioBuffer->raw = NULL; |
| 1196 | return INVALID_OPERATION; |
| 1197 | } |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1198 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1199 | const struct timespec *requested; |
Eric Laurent | df57699 | 2014-01-27 18:13:39 -0800 | [diff] [blame] | 1200 | struct timespec timeout; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1201 | if (waitCount == -1) { |
| 1202 | requested = &ClientProxy::kForever; |
| 1203 | } else if (waitCount == 0) { |
| 1204 | requested = &ClientProxy::kNonBlocking; |
| 1205 | } else if (waitCount > 0) { |
| 1206 | long long ms = WAIT_PERIOD_MS * (long long) waitCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1207 | timeout.tv_sec = ms / 1000; |
| 1208 | timeout.tv_nsec = (int) (ms % 1000) * 1000000; |
| 1209 | requested = &timeout; |
| 1210 | } else { |
| 1211 | ALOGE("%s invalid waitCount %d", __func__, waitCount); |
| 1212 | requested = NULL; |
| 1213 | } |
| 1214 | return obtainBuffer(audioBuffer, requested); |
| 1215 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1216 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1217 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested, |
| 1218 | struct timespec *elapsed, size_t *nonContig) |
| 1219 | { |
| 1220 | // previous and new IAudioTrack sequence numbers are used to detect track re-creation |
| 1221 | uint32_t oldSequence = 0; |
| 1222 | uint32_t newSequence; |
| 1223 | |
| 1224 | Proxy::Buffer buffer; |
| 1225 | status_t status = NO_ERROR; |
| 1226 | |
| 1227 | static const int32_t kMaxTries = 5; |
| 1228 | int32_t tryCounter = kMaxTries; |
| 1229 | |
| 1230 | do { |
| 1231 | // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to |
| 1232 | // keep them from going away if another thread re-creates the track during obtainBuffer() |
| 1233 | sp<AudioTrackClientProxy> proxy; |
| 1234 | sp<IMemory> iMem; |
| 1235 | |
| 1236 | { // start of lock scope |
| 1237 | AutoMutex lock(mLock); |
| 1238 | |
| 1239 | newSequence = mSequence; |
| 1240 | // did previous obtainBuffer() fail due to media server death or voluntary invalidation? |
| 1241 | if (status == DEAD_OBJECT) { |
| 1242 | // re-create track, unless someone else has already done so |
| 1243 | if (newSequence == oldSequence) { |
| 1244 | status = restoreTrack_l("obtainBuffer"); |
| 1245 | if (status != NO_ERROR) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1246 | buffer.mFrameCount = 0; |
| 1247 | buffer.mRaw = NULL; |
| 1248 | buffer.mNonContig = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1249 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | } |
| 1252 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1253 | oldSequence = newSequence; |
| 1254 | |
| 1255 | // Keep the extra references |
| 1256 | proxy = mProxy; |
| 1257 | iMem = mCblkMemory; |
| 1258 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1259 | if (mState == STATE_STOPPING) { |
| 1260 | status = -EINTR; |
| 1261 | buffer.mFrameCount = 0; |
| 1262 | buffer.mRaw = NULL; |
| 1263 | buffer.mNonContig = 0; |
| 1264 | break; |
| 1265 | } |
| 1266 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1267 | // Non-blocking if track is stopped or paused |
| 1268 | if (mState != STATE_ACTIVE) { |
| 1269 | requested = &ClientProxy::kNonBlocking; |
| 1270 | } |
| 1271 | |
| 1272 | } // end of lock scope |
| 1273 | |
| 1274 | buffer.mFrameCount = audioBuffer->frameCount; |
| 1275 | // FIXME starts the requested timeout and elapsed over from scratch |
| 1276 | status = proxy->obtainBuffer(&buffer, requested, elapsed); |
| 1277 | |
| 1278 | } while ((status == DEAD_OBJECT) && (tryCounter-- > 0)); |
| 1279 | |
| 1280 | audioBuffer->frameCount = buffer.mFrameCount; |
| 1281 | audioBuffer->size = buffer.mFrameCount * mFrameSizeAF; |
| 1282 | audioBuffer->raw = buffer.mRaw; |
| 1283 | if (nonContig != NULL) { |
| 1284 | *nonContig = buffer.mNonContig; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1286 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 1290 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1291 | if (mTransfer == TRANSFER_SHARED) { |
| 1292 | return; |
| 1293 | } |
| 1294 | |
| 1295 | size_t stepCount = audioBuffer->size / mFrameSizeAF; |
| 1296 | if (stepCount == 0) { |
| 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | Proxy::Buffer buffer; |
| 1301 | buffer.mFrameCount = stepCount; |
| 1302 | buffer.mRaw = audioBuffer->raw; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1303 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1304 | AutoMutex lock(mLock); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1305 | mInUnderrun = false; |
| 1306 | mProxy->releaseBuffer(&buffer); |
| 1307 | |
| 1308 | // restart track if it was disabled by audioflinger due to previous underrun |
| 1309 | if (mState == STATE_ACTIVE) { |
| 1310 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1311 | if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) { |
Glenn Kasten | c5a1742 | 2014-03-13 14:59:59 -0700 | [diff] [blame] | 1312 | ALOGW("releaseBuffer() track %p disabled due to previous underrun, restarting", this); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1313 | // FIXME ignoring status |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1314 | mAudioTrack->start(); |
| 1315 | } |
| 1316 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | // ------------------------------------------------------------------------- |
| 1320 | |
Jean-Michel Trivi | 720ad9d | 2014-02-04 11:00:59 -0800 | [diff] [blame] | 1321 | ssize_t AudioTrack::write(const void* buffer, size_t userSize, bool blocking) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1323 | if (mTransfer != TRANSFER_SYNC || mIsTimed) { |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1324 | return INVALID_OPERATION; |
| 1325 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1327 | if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) { |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1328 | // Sanity-check: user is most-likely passing an error code, and it would |
| 1329 | // make the return value ambiguous (actualSize vs error). |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1330 | ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | return BAD_VALUE; |
| 1332 | } |
| 1333 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1334 | size_t written = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | Buffer audioBuffer; |
| 1336 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1337 | while (userSize >= mFrameSize) { |
| 1338 | audioBuffer.frameCount = userSize / mFrameSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1339 | |
Jean-Michel Trivi | 720ad9d | 2014-02-04 11:00:59 -0800 | [diff] [blame] | 1340 | status_t err = obtainBuffer(&audioBuffer, |
| 1341 | blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | if (err < 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1343 | if (written > 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | break; |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1345 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | return ssize_t(err); |
| 1347 | } |
| 1348 | |
| 1349 | size_t toWrite; |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1350 | 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] | 1351 | // Divide capacity by 2 to take expansion into account |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1352 | toWrite = audioBuffer.size >> 1; |
| 1353 | memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite); |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1354 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | toWrite = audioBuffer.size; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1356 | memcpy(audioBuffer.i8, buffer, toWrite); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1358 | buffer = ((const char *) buffer) + toWrite; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | userSize -= toWrite; |
| 1360 | written += toWrite; |
| 1361 | |
| 1362 | releaseBuffer(&audioBuffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1363 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | |
| 1365 | return written; |
| 1366 | } |
| 1367 | |
| 1368 | // ------------------------------------------------------------------------- |
| 1369 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1370 | TimedAudioTrack::TimedAudioTrack() { |
| 1371 | mIsTimed = true; |
| 1372 | } |
| 1373 | |
| 1374 | status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer) |
| 1375 | { |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 1376 | AutoMutex lock(mLock); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1377 | status_t result = UNKNOWN_ERROR; |
| 1378 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1379 | #if 1 |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 1380 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1381 | // while we are accessing the cblk |
| 1382 | sp<IAudioTrack> audioTrack = mAudioTrack; |
| 1383 | sp<IMemory> iMem = mCblkMemory; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1384 | #endif |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 1385 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1386 | // If the track is not invalid already, try to allocate a buffer. alloc |
| 1387 | // 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] | 1388 | // we can attempt to restore in just a bit. |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1389 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1390 | if (!(cblk->mFlags & CBLK_INVALID)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1391 | result = mAudioTrack->allocateTimedBuffer(size, buffer); |
| 1392 | if (result == DEAD_OBJECT) { |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1393 | android_atomic_or(CBLK_INVALID, &cblk->mFlags); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | // If the track is invalid at this point, attempt to restore it. and try the |
| 1398 | // allocation one more time. |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1399 | if (cblk->mFlags & CBLK_INVALID) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1400 | result = restoreTrack_l("allocateTimedBuffer"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1401 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1402 | if (result == NO_ERROR) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1403 | result = mAudioTrack->allocateTimedBuffer(size, buffer); |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1404 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | return result; |
| 1408 | } |
| 1409 | |
| 1410 | status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer, |
| 1411 | int64_t pts) |
| 1412 | { |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1413 | status_t status = mAudioTrack->queueTimedBuffer(buffer, pts); |
| 1414 | { |
| 1415 | AutoMutex lock(mLock); |
Glenn Kasten | d2c38fc | 2012-11-01 14:58:02 -0700 | [diff] [blame] | 1416 | audio_track_cblk_t* cblk = mCblk; |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1417 | // restart track if it was disabled by audioflinger due to previous underrun |
| 1418 | if (buffer->size() != 0 && status == NO_ERROR && |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1419 | (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) { |
| 1420 | android_atomic_and(~CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1421 | ALOGW("queueTimedBuffer() track %p disabled, restarting", this); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1422 | // FIXME ignoring status |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1423 | mAudioTrack->start(); |
| 1424 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1425 | } |
Eric Laurent | df83984 | 2012-05-31 14:27:14 -0700 | [diff] [blame] | 1426 | return status; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform, |
| 1430 | TargetTimeline target) |
| 1431 | { |
| 1432 | return mAudioTrack->setMediaTimeTransform(xform, target); |
| 1433 | } |
| 1434 | |
| 1435 | // ------------------------------------------------------------------------- |
| 1436 | |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 1437 | nsecs_t AudioTrack::processAudioBuffer() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | { |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 1439 | // Currently the AudioTrack thread is not created if there are no callbacks. |
| 1440 | // Would it ever make sense to run the thread, even without callbacks? |
| 1441 | // If so, then replace this by checks at each use for mCbf != NULL. |
| 1442 | LOG_ALWAYS_FATAL_IF(mCblk == NULL); |
| 1443 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1444 | mLock.lock(); |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 1445 | if (mAwaitBoost) { |
| 1446 | mAwaitBoost = false; |
| 1447 | mLock.unlock(); |
| 1448 | static const int32_t kMaxTries = 5; |
| 1449 | int32_t tryCounter = kMaxTries; |
| 1450 | uint32_t pollUs = 10000; |
| 1451 | do { |
| 1452 | int policy = sched_getscheduler(0); |
| 1453 | if (policy == SCHED_FIFO || policy == SCHED_RR) { |
| 1454 | break; |
| 1455 | } |
| 1456 | usleep(pollUs); |
| 1457 | pollUs <<= 1; |
| 1458 | } while (tryCounter-- > 0); |
| 1459 | if (tryCounter < 0) { |
| 1460 | ALOGE("did not receive expected priority boost on time"); |
| 1461 | } |
Glenn Kasten | b0dfd46 | 2013-07-10 16:52:47 -0700 | [diff] [blame] | 1462 | // Run again immediately |
| 1463 | return 0; |
Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 1464 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1465 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1466 | // Can only reference mCblk while locked |
| 1467 | int32_t flags = android_atomic_and( |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1468 | ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags); |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1469 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1470 | // Check for track invalidation |
| 1471 | if (flags & CBLK_INVALID) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1472 | // for offloaded tracks restoreTrack_l() will just update the sequence and clear |
| 1473 | // AudioSystem cache. We should not exit here but after calling the callback so |
| 1474 | // that the upper layers can recreate the track |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 1475 | if (!isOffloaded_l() || (mSequence == mObservedSequence)) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1476 | status_t status = restoreTrack_l("processAudioBuffer"); |
| 1477 | mLock.unlock(); |
| 1478 | // Run again immediately, but with a new IAudioTrack |
| 1479 | return 0; |
| 1480 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1483 | bool waitStreamEnd = mState == STATE_STOPPING; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1484 | bool active = mState == STATE_ACTIVE; |
| 1485 | |
| 1486 | // Manage underrun callback, must be done under lock to avoid race with releaseBuffer() |
| 1487 | bool newUnderrun = false; |
| 1488 | if (flags & CBLK_UNDERRUN) { |
| 1489 | #if 0 |
| 1490 | // Currently in shared buffer mode, when the server reaches the end of buffer, |
| 1491 | // the track stays active in continuous underrun state. It's up to the application |
| 1492 | // to pause or stop the track, or set the position to a new offset within buffer. |
| 1493 | // This was some experimental code to auto-pause on underrun. Keeping it here |
| 1494 | // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content. |
| 1495 | if (mTransfer == TRANSFER_SHARED) { |
| 1496 | mState = STATE_PAUSED; |
| 1497 | active = false; |
| 1498 | } |
| 1499 | #endif |
| 1500 | if (!mInUnderrun) { |
| 1501 | mInUnderrun = true; |
| 1502 | newUnderrun = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | } |
| 1504 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1505 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1506 | // Get current position of server |
| 1507 | size_t position = mProxy->getPosition(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | |
| 1509 | // Manage marker callback |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1510 | bool markerReached = false; |
| 1511 | size_t markerPosition = mMarkerPosition; |
| 1512 | // FIXME fails for wraparound, need 64 bits |
| 1513 | if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) { |
| 1514 | mMarkerReached = markerReached = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1517 | // Determine number of new position callback(s) that will be needed, while locked |
| 1518 | size_t newPosCount = 0; |
| 1519 | size_t newPosition = mNewPosition; |
| 1520 | size_t updatePeriod = mUpdatePeriod; |
| 1521 | // FIXME fails for wraparound, need 64 bits |
| 1522 | if (updatePeriod > 0 && position >= newPosition) { |
| 1523 | newPosCount = ((position - newPosition) / updatePeriod) + 1; |
| 1524 | mNewPosition += updatePeriod * newPosCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1527 | // Cache other fields that will be needed soon |
| 1528 | uint32_t loopPeriod = mLoopPeriod; |
| 1529 | uint32_t sampleRate = mSampleRate; |
Glenn Kasten | 838b3d8 | 2014-02-27 15:30:41 -0800 | [diff] [blame] | 1530 | uint32_t notificationFrames = mNotificationFramesAct; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1531 | if (mRefreshRemaining) { |
| 1532 | mRefreshRemaining = false; |
| 1533 | mRemainingFrames = notificationFrames; |
| 1534 | mRetryOnPartialBuffer = false; |
| 1535 | } |
| 1536 | size_t misalignment = mProxy->getMisalignment(); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1537 | uint32_t sequence = mSequence; |
Glenn Kasten | 96f0488 | 2013-09-20 09:28:56 -0700 | [diff] [blame] | 1538 | sp<AudioTrackClientProxy> proxy = mProxy; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1539 | |
| 1540 | // These fields don't need to be cached, because they are assigned only by set(): |
| 1541 | // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags |
| 1542 | // mFlags is also assigned by createTrack_l(), but not the bit we care about. |
| 1543 | |
| 1544 | mLock.unlock(); |
| 1545 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1546 | if (waitStreamEnd) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1547 | struct timespec timeout; |
| 1548 | timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC; |
| 1549 | timeout.tv_nsec = 0; |
| 1550 | |
Glenn Kasten | 96f0488 | 2013-09-20 09:28:56 -0700 | [diff] [blame] | 1551 | status_t status = proxy->waitStreamEndDone(&timeout); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1552 | switch (status) { |
| 1553 | case NO_ERROR: |
| 1554 | case DEAD_OBJECT: |
| 1555 | case TIMED_OUT: |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1556 | mCbf(EVENT_STREAM_END, mUserData, NULL); |
Glenn Kasten | 96f0488 | 2013-09-20 09:28:56 -0700 | [diff] [blame] | 1557 | { |
| 1558 | AutoMutex lock(mLock); |
| 1559 | // The previously assigned value of waitStreamEnd is no longer valid, |
| 1560 | // since the mutex has been unlocked and either the callback handler |
| 1561 | // or another thread could have re-started the AudioTrack during that time. |
| 1562 | waitStreamEnd = mState == STATE_STOPPING; |
| 1563 | if (waitStreamEnd) { |
| 1564 | mState = STATE_STOPPED; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1565 | } |
| 1566 | } |
Glenn Kasten | 96f0488 | 2013-09-20 09:28:56 -0700 | [diff] [blame] | 1567 | if (waitStreamEnd && status != DEAD_OBJECT) { |
| 1568 | return NS_INACTIVE; |
| 1569 | } |
| 1570 | break; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1571 | } |
Glenn Kasten | 96f0488 | 2013-09-20 09:28:56 -0700 | [diff] [blame] | 1572 | return 0; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1575 | // perform callbacks while unlocked |
| 1576 | if (newUnderrun) { |
| 1577 | mCbf(EVENT_UNDERRUN, mUserData, NULL); |
| 1578 | } |
| 1579 | // FIXME we will miss loops if loop cycle was signaled several times since last call |
| 1580 | // to processAudioBuffer() |
| 1581 | if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) { |
| 1582 | mCbf(EVENT_LOOP_END, mUserData, NULL); |
| 1583 | } |
| 1584 | if (flags & CBLK_BUFFER_END) { |
| 1585 | mCbf(EVENT_BUFFER_END, mUserData, NULL); |
| 1586 | } |
| 1587 | if (markerReached) { |
| 1588 | mCbf(EVENT_MARKER, mUserData, &markerPosition); |
| 1589 | } |
| 1590 | while (newPosCount > 0) { |
| 1591 | size_t temp = newPosition; |
| 1592 | mCbf(EVENT_NEW_POS, mUserData, &temp); |
| 1593 | newPosition += updatePeriod; |
| 1594 | newPosCount--; |
| 1595 | } |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1596 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1597 | if (mObservedSequence != sequence) { |
| 1598 | mObservedSequence = sequence; |
| 1599 | mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1600 | // for offloaded tracks, just wait for the upper layers to recreate the track |
| 1601 | if (isOffloaded()) { |
| 1602 | return NS_INACTIVE; |
| 1603 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1606 | // if inactive, then don't run me again until re-started |
| 1607 | if (!active) { |
| 1608 | return NS_INACTIVE; |
Eric Laurent | 2267ba1 | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1611 | // Compute the estimated time until the next timed event (position, markers, loops) |
| 1612 | // FIXME only for non-compressed audio |
| 1613 | uint32_t minFrames = ~0; |
| 1614 | if (!markerReached && position < markerPosition) { |
| 1615 | minFrames = markerPosition - position; |
| 1616 | } |
| 1617 | if (loopPeriod > 0 && loopPeriod < minFrames) { |
| 1618 | minFrames = loopPeriod; |
| 1619 | } |
| 1620 | if (updatePeriod > 0 && updatePeriod < minFrames) { |
| 1621 | minFrames = updatePeriod; |
| 1622 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1624 | // If > 0, poll periodically to recover from a stuck server. A good value is 2. |
| 1625 | static const uint32_t kPoll = 0; |
| 1626 | if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) { |
| 1627 | minFrames = kPoll * notificationFrames; |
| 1628 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1629 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1630 | // Convert frame units to time units |
| 1631 | nsecs_t ns = NS_WHENEVER; |
| 1632 | if (minFrames != (uint32_t) ~0) { |
| 1633 | // This "fudge factor" avoids soaking CPU, and compensates for late progress by server |
| 1634 | static const nsecs_t kFudgeNs = 10000000LL; // 10 ms |
| 1635 | ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs; |
| 1636 | } |
| 1637 | |
| 1638 | // If not supplying data by EVENT_MORE_DATA, then we're done |
| 1639 | if (mTransfer != TRANSFER_CALLBACK) { |
| 1640 | return ns; |
| 1641 | } |
| 1642 | |
| 1643 | struct timespec timeout; |
| 1644 | const struct timespec *requested = &ClientProxy::kForever; |
| 1645 | if (ns != NS_WHENEVER) { |
| 1646 | timeout.tv_sec = ns / 1000000000LL; |
| 1647 | timeout.tv_nsec = ns % 1000000000LL; |
| 1648 | ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000); |
| 1649 | requested = &timeout; |
| 1650 | } |
| 1651 | |
| 1652 | while (mRemainingFrames > 0) { |
| 1653 | |
| 1654 | Buffer audioBuffer; |
| 1655 | audioBuffer.frameCount = mRemainingFrames; |
| 1656 | size_t nonContig; |
| 1657 | status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig); |
| 1658 | LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0), |
| 1659 | "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount); |
| 1660 | requested = &ClientProxy::kNonBlocking; |
| 1661 | size_t avail = audioBuffer.frameCount + nonContig; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1662 | ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d", |
| 1663 | mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1664 | if (err != NO_ERROR) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1665 | if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR || |
| 1666 | (isOffloaded() && (err == DEAD_OBJECT))) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1667 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1669 | ALOGE("Error %d obtaining an audio buffer, giving up.", err); |
| 1670 | return NS_NEVER; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1672 | |
Eric Laurent | 42a6f42 | 2013-08-29 14:35:05 -0700 | [diff] [blame] | 1673 | if (mRetryOnPartialBuffer && !isOffloaded()) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1674 | mRetryOnPartialBuffer = false; |
| 1675 | if (avail < mRemainingFrames) { |
| 1676 | int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate; |
| 1677 | if (ns < 0 || myns < ns) { |
| 1678 | ns = myns; |
| 1679 | } |
| 1680 | return ns; |
| 1681 | } |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1682 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | |
| 1684 | // Divide buffer size by 2 to take into account the expansion |
| 1685 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 1686 | // of the destination buffer |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1687 | 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] | 1688 | audioBuffer.size >>= 1; |
| 1689 | } |
| 1690 | |
| 1691 | size_t reqSize = audioBuffer.size; |
| 1692 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1693 | size_t writtenSize = audioBuffer.size; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | |
| 1695 | // Sanity check on returned size |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1696 | if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) { |
| 1697 | ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes", |
| 1698 | reqSize, (int) writtenSize); |
| 1699 | return NS_NEVER; |
| 1700 | } |
| 1701 | |
| 1702 | if (writtenSize == 0) { |
The Android Open Source Project | 8555d08 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1703 | // The callback is done filling buffers |
| 1704 | // Keep this thread going to handle timed events and |
| 1705 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 1706 | // but don't just loop and block the CPU, so wait |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1707 | return WAIT_PERIOD_MS * 1000000LL; |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 1708 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1710 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) { |
Glenn Kasten | 511754b | 2012-01-11 09:52:19 -0800 | [diff] [blame] | 1711 | // 8 to 16 bit conversion, note that source and destination are the same address |
| 1712 | 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] | 1713 | audioBuffer.size <<= 1; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1716 | size_t releasedFrames = audioBuffer.size / mFrameSizeAF; |
| 1717 | audioBuffer.frameCount = releasedFrames; |
| 1718 | mRemainingFrames -= releasedFrames; |
| 1719 | if (misalignment >= releasedFrames) { |
| 1720 | misalignment -= releasedFrames; |
| 1721 | } else { |
| 1722 | misalignment = 0; |
| 1723 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | |
| 1725 | releaseBuffer(&audioBuffer); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1727 | // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer |
| 1728 | // if callback doesn't like to accept the full chunk |
| 1729 | if (writtenSize < reqSize) { |
| 1730 | continue; |
| 1731 | } |
| 1732 | |
| 1733 | // There could be enough non-contiguous frames available to satisfy the remaining request |
| 1734 | if (mRemainingFrames <= nonContig) { |
| 1735 | continue; |
| 1736 | } |
| 1737 | |
| 1738 | #if 0 |
| 1739 | // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a |
| 1740 | // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA |
| 1741 | // that total to a sum == notificationFrames. |
| 1742 | if (0 < misalignment && misalignment <= mRemainingFrames) { |
| 1743 | mRemainingFrames = misalignment; |
| 1744 | return (mRemainingFrames * 1100000000LL) / sampleRate; |
| 1745 | } |
| 1746 | #endif |
| 1747 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1749 | mRemainingFrames = notificationFrames; |
| 1750 | mRetryOnPartialBuffer = true; |
| 1751 | |
| 1752 | // A lot has transpired since ns was calculated, so run again immediately and re-calculate |
| 1753 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | } |
| 1755 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1756 | status_t AudioTrack::restoreTrack_l(const char *from) |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1757 | { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1758 | ALOGW("dead IAudioTrack, %s, creating a new one from %s()", |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 1759 | isOffloaded_l() ? "Offloaded" : "PCM", from); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1760 | ++mSequence; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1761 | status_t result; |
| 1762 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1763 | // refresh the audio configuration cache in this process to make sure we get new |
Glenn Kasten | 38e905b | 2014-01-13 10:21:48 -0800 | [diff] [blame] | 1764 | // output parameters in createTrack_l() |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1765 | AudioSystem::clearAudioConfigCache(); |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1766 | |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 1767 | if (isOffloaded_l()) { |
| 1768 | // FIXME re-creation of offloaded tracks is not yet implemented |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1769 | return DEAD_OBJECT; |
| 1770 | } |
| 1771 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1772 | // if the new IAudioTrack is created, createTrack_l() will modify the |
| 1773 | // following member variables: mAudioTrack, mCblkMemory and mCblk. |
| 1774 | // It will also delete the strong references on previous IAudioTrack and IMemory |
Eric Laurent | cc21e4f | 2013-10-16 15:12:32 -0700 | [diff] [blame] | 1775 | |
| 1776 | // take the frames that will be lost by track recreation into account in saved position |
| 1777 | size_t position = mProxy->getPosition() + mProxy->getFramesFilled(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1778 | size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0; |
Glenn Kasten | 363fb75 | 2014-01-15 12:27:31 -0800 | [diff] [blame] | 1779 | result = createTrack_l(position /*epoch*/); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1780 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1781 | if (result == NO_ERROR) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1782 | // continue playback from last known position, but |
| 1783 | // don't attempt to restore loop after invalidation; it's difficult and not worthwhile |
| 1784 | if (mStaticProxy != NULL) { |
| 1785 | mLoopPeriod = 0; |
| 1786 | mStaticProxy->setLoop(bufferPosition, mFrameCount, 0); |
| 1787 | } |
| 1788 | // FIXME How do we simulate the fact that all frames present in the buffer at the time of |
| 1789 | // track destruction have been played? This is critical for SoundPool implementation |
| 1790 | // This must be broken, and needs to be tested/debugged. |
| 1791 | #if 0 |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1792 | // restore write index and set other indexes to reflect empty buffer status |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1793 | if (!strcmp(from, "start")) { |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1794 | // Make sure that a client relying on callback events indicating underrun or |
| 1795 | // the actual amount of audio frames played (e.g SoundPool) receives them. |
| 1796 | if (mSharedBuffer == 0) { |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1797 | // restart playback even if buffer is not completely filled. |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1798 | android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1799 | } |
| 1800 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1801 | #endif |
| 1802 | if (mState == STATE_ACTIVE) { |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 1803 | result = mAudioTrack->start(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1804 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1805 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1806 | if (result != NO_ERROR) { |
| 1807 | ALOGW("restoreTrack_l() failed status %d", result); |
| 1808 | mState = STATE_STOPPED; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1809 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1810 | |
| 1811 | return result; |
| 1812 | } |
| 1813 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1814 | status_t AudioTrack::setParameters(const String8& keyValuePairs) |
| 1815 | { |
| 1816 | AutoMutex lock(mLock); |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 1817 | return mAudioTrack->setParameters(keyValuePairs); |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Glenn Kasten | ce70374 | 2013-07-19 16:33:58 -0700 | [diff] [blame] | 1820 | status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp) |
| 1821 | { |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 1822 | AutoMutex lock(mLock); |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 1823 | // FIXME not implemented for fast tracks; should use proxy and SSQ |
| 1824 | if (mFlags & AUDIO_OUTPUT_FLAG_FAST) { |
| 1825 | return INVALID_OPERATION; |
| 1826 | } |
| 1827 | if (mState != STATE_ACTIVE && mState != STATE_PAUSED) { |
| 1828 | return INVALID_OPERATION; |
| 1829 | } |
| 1830 | status_t status = mAudioTrack->getTimestamp(timestamp); |
| 1831 | if (status == NO_ERROR) { |
| 1832 | timestamp.mPosition += mProxy->getEpoch(); |
| 1833 | } |
| 1834 | return status; |
Glenn Kasten | ce70374 | 2013-07-19 16:33:58 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1837 | String8 AudioTrack::getParameters(const String8& keys) |
| 1838 | { |
Glenn Kasten | 2c6c529 | 2014-01-13 10:29:08 -0800 | [diff] [blame] | 1839 | audio_io_handle_t output = getOutput(); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1840 | if (output != AUDIO_IO_HANDLE_NONE) { |
Glenn Kasten | 2c6c529 | 2014-01-13 10:29:08 -0800 | [diff] [blame] | 1841 | return AudioSystem::getParameters(output, keys); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 1842 | } else { |
| 1843 | return String8::empty(); |
| 1844 | } |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Glenn Kasten | 23a7545 | 2014-01-13 10:37:17 -0800 | [diff] [blame] | 1847 | bool AudioTrack::isOffloaded() const |
| 1848 | { |
| 1849 | AutoMutex lock(mLock); |
| 1850 | return isOffloaded_l(); |
| 1851 | } |
| 1852 | |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 1853 | status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1854 | { |
| 1855 | |
| 1856 | const size_t SIZE = 256; |
| 1857 | char buffer[SIZE]; |
| 1858 | String8 result; |
| 1859 | |
| 1860 | result.append(" AudioTrack::dump\n"); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1861 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, |
Glenn Kasten | 877a0ac | 2014-04-30 17:04:13 -0700 | [diff] [blame] | 1862 | mVolume[AUDIO_INTERLEAVE_LEFT], mVolume[AUDIO_INTERLEAVE_RIGHT]); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | result.append(buffer); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1864 | snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat, |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 1865 | mChannelCount, mFrameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1866 | result.append(buffer); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1867 | 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] | 1868 | result.append(buffer); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1869 | 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] | 1870 | result.append(buffer); |
| 1871 | ::write(fd, result.string(), result.size()); |
| 1872 | return NO_ERROR; |
| 1873 | } |
| 1874 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1875 | uint32_t AudioTrack::getUnderrunFrames() const |
| 1876 | { |
| 1877 | AutoMutex lock(mLock); |
| 1878 | return mProxy->getUnderrunFrames(); |
| 1879 | } |
| 1880 | |
Jean-Michel Trivi | faabb51 | 2014-06-11 16:55:06 -0700 | [diff] [blame] | 1881 | void AudioTrack::setAttributesFromStreamType(audio_stream_type_t streamType) { |
| 1882 | mAttributes.flags = 0x0; |
| 1883 | |
| 1884 | switch(streamType) { |
| 1885 | case AUDIO_STREAM_DEFAULT: |
| 1886 | case AUDIO_STREAM_MUSIC: |
| 1887 | mAttributes.content_type = AUDIO_CONTENT_TYPE_MUSIC; |
| 1888 | mAttributes.usage = AUDIO_USAGE_MEDIA; |
| 1889 | break; |
| 1890 | case AUDIO_STREAM_VOICE_CALL: |
| 1891 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1892 | mAttributes.usage = AUDIO_USAGE_VOICE_COMMUNICATION; |
| 1893 | break; |
| 1894 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 1895 | mAttributes.flags |= AUDIO_FLAG_AUDIBILITY_ENFORCED; |
| 1896 | // intended fall through, attributes in common with STREAM_SYSTEM |
| 1897 | case AUDIO_STREAM_SYSTEM: |
| 1898 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1899 | mAttributes.usage = AUDIO_USAGE_ASSISTANCE_SONIFICATION; |
| 1900 | break; |
| 1901 | case AUDIO_STREAM_RING: |
| 1902 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1903 | mAttributes.usage = AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE; |
| 1904 | break; |
| 1905 | case AUDIO_STREAM_ALARM: |
| 1906 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1907 | mAttributes.usage = AUDIO_USAGE_ALARM; |
| 1908 | break; |
| 1909 | case AUDIO_STREAM_NOTIFICATION: |
| 1910 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1911 | mAttributes.usage = AUDIO_USAGE_NOTIFICATION; |
| 1912 | break; |
| 1913 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 1914 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1915 | mAttributes.usage = AUDIO_USAGE_VOICE_COMMUNICATION; |
| 1916 | mAttributes.flags |= AUDIO_FLAG_SCO; |
| 1917 | break; |
| 1918 | case AUDIO_STREAM_DTMF: |
| 1919 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1920 | mAttributes.usage = AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING; |
| 1921 | break; |
| 1922 | case AUDIO_STREAM_TTS: |
| 1923 | mAttributes.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1924 | mAttributes.usage = AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY; |
| 1925 | break; |
| 1926 | default: |
| 1927 | ALOGE("invalid stream type %d when converting to attributes", streamType); |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | void AudioTrack::setStreamTypeFromAttributes(audio_attributes_t& aa) { |
| 1932 | // flags to stream type mapping |
| 1933 | if ((aa.flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 1934 | mStreamType = AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 1935 | return; |
| 1936 | } |
| 1937 | if ((aa.flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 1938 | mStreamType = AUDIO_STREAM_BLUETOOTH_SCO; |
| 1939 | return; |
| 1940 | } |
| 1941 | |
| 1942 | // usage to stream type mapping |
| 1943 | switch (aa.usage) { |
| 1944 | case AUDIO_USAGE_MEDIA: |
| 1945 | case AUDIO_USAGE_GAME: |
| 1946 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 1947 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 1948 | mStreamType = AUDIO_STREAM_MUSIC; |
| 1949 | return; |
| 1950 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 1951 | mStreamType = AUDIO_STREAM_SYSTEM; |
| 1952 | return; |
| 1953 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 1954 | mStreamType = AUDIO_STREAM_VOICE_CALL; |
| 1955 | return; |
| 1956 | |
| 1957 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 1958 | mStreamType = AUDIO_STREAM_DTMF; |
| 1959 | return; |
| 1960 | |
| 1961 | case AUDIO_USAGE_ALARM: |
| 1962 | mStreamType = AUDIO_STREAM_ALARM; |
| 1963 | return; |
| 1964 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 1965 | mStreamType = AUDIO_STREAM_RING; |
| 1966 | return; |
| 1967 | |
| 1968 | case AUDIO_USAGE_NOTIFICATION: |
| 1969 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 1970 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 1971 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 1972 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 1973 | mStreamType = AUDIO_STREAM_NOTIFICATION; |
| 1974 | return; |
| 1975 | |
| 1976 | case AUDIO_USAGE_UNKNOWN: |
| 1977 | default: |
| 1978 | mStreamType = AUDIO_STREAM_MUSIC; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | bool AudioTrack::isValidAttributes(const audio_attributes_t *paa) { |
| 1983 | // has flags that map to a strategy? |
| 1984 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO)) != 0) { |
| 1985 | return true; |
| 1986 | } |
| 1987 | |
| 1988 | // has known usage? |
| 1989 | switch (paa->usage) { |
| 1990 | case AUDIO_USAGE_UNKNOWN: |
| 1991 | case AUDIO_USAGE_MEDIA: |
| 1992 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 1993 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 1994 | case AUDIO_USAGE_ALARM: |
| 1995 | case AUDIO_USAGE_NOTIFICATION: |
| 1996 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 1997 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 1998 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 1999 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 2000 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 2001 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 2002 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 2003 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 2004 | case AUDIO_USAGE_GAME: |
| 2005 | break; |
| 2006 | default: |
| 2007 | return false; |
| 2008 | } |
| 2009 | return true; |
| 2010 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2011 | // ========================================================================= |
| 2012 | |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 2013 | void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused) |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2014 | { |
| 2015 | sp<AudioTrack> audioTrack = mAudioTrack.promote(); |
| 2016 | if (audioTrack != 0) { |
| 2017 | AutoMutex lock(audioTrack->mLock); |
| 2018 | audioTrack->mProxy->binderDied(); |
| 2019 | } |
| 2020 | } |
| 2021 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2022 | // ========================================================================= |
| 2023 | |
| 2024 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
Glenn Kasten | 598de6c | 2013-10-16 17:02:13 -0700 | [diff] [blame] | 2025 | : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL), |
| 2026 | mIgnoreNextPausedInt(false) |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2027 | { |
| 2028 | } |
| 2029 | |
| 2030 | AudioTrack::AudioTrackThread::~AudioTrackThread() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | { |
| 2032 | } |
| 2033 | |
| 2034 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 2035 | { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2036 | { |
| 2037 | AutoMutex _l(mMyLock); |
| 2038 | if (mPaused) { |
| 2039 | mMyCond.wait(mMyLock); |
| 2040 | // caller will check for exitPending() |
| 2041 | return true; |
| 2042 | } |
Glenn Kasten | 598de6c | 2013-10-16 17:02:13 -0700 | [diff] [blame] | 2043 | if (mIgnoreNextPausedInt) { |
| 2044 | mIgnoreNextPausedInt = false; |
| 2045 | mPausedInt = false; |
| 2046 | } |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2047 | if (mPausedInt) { |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2048 | if (mPausedNs > 0) { |
| 2049 | (void) mMyCond.waitRelative(mMyLock, mPausedNs); |
| 2050 | } else { |
| 2051 | mMyCond.wait(mMyLock); |
| 2052 | } |
Eric Laurent | 9d2c78c | 2013-09-23 12:29:42 -0700 | [diff] [blame] | 2053 | mPausedInt = false; |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2054 | return true; |
| 2055 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2056 | } |
Glenn Kasten | 7c7be1e | 2013-12-19 16:34:04 -0800 | [diff] [blame] | 2057 | nsecs_t ns = mReceiver.processAudioBuffer(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2058 | switch (ns) { |
| 2059 | case 0: |
| 2060 | return true; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2061 | case NS_INACTIVE: |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2062 | pauseInternal(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2063 | return true; |
| 2064 | case NS_NEVER: |
| 2065 | return false; |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2066 | case NS_WHENEVER: |
| 2067 | // FIXME increase poll interval, or make event-driven |
| 2068 | ns = 1000000000LL; |
| 2069 | // fall through |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2070 | default: |
| 2071 | LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns); |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2072 | pauseInternal(ns); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2073 | return true; |
Glenn Kasten | ca8b280 | 2012-04-23 13:58:16 -0700 | [diff] [blame] | 2074 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2077 | void AudioTrack::AudioTrackThread::requestExit() |
| 2078 | { |
| 2079 | // must be in this order to avoid a race condition |
| 2080 | Thread::requestExit(); |
Glenn Kasten | 598de6c | 2013-10-16 17:02:13 -0700 | [diff] [blame] | 2081 | resume(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | void AudioTrack::AudioTrackThread::pause() |
| 2085 | { |
| 2086 | AutoMutex _l(mMyLock); |
| 2087 | mPaused = true; |
| 2088 | } |
| 2089 | |
| 2090 | void AudioTrack::AudioTrackThread::resume() |
| 2091 | { |
| 2092 | AutoMutex _l(mMyLock); |
Glenn Kasten | 598de6c | 2013-10-16 17:02:13 -0700 | [diff] [blame] | 2093 | mIgnoreNextPausedInt = true; |
Eric Laurent | 9d2c78c | 2013-09-23 12:29:42 -0700 | [diff] [blame] | 2094 | if (mPaused || mPausedInt) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2095 | mPaused = false; |
Eric Laurent | 9d2c78c | 2013-09-23 12:29:42 -0700 | [diff] [blame] | 2096 | mPausedInt = false; |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 2097 | mMyCond.signal(); |
| 2098 | } |
| 2099 | } |
| 2100 | |
Glenn Kasten | 5a6cd22 | 2013-09-20 09:20:45 -0700 | [diff] [blame] | 2101 | void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns) |
| 2102 | { |
| 2103 | AutoMutex _l(mMyLock); |
| 2104 | mPausedInt = true; |
| 2105 | mPausedNs = ns; |
| 2106 | } |
| 2107 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | }; // namespace android |