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