| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | ** | 
|  | 3 | ** Copyright 2012, The Android Open Source Project | 
|  | 4 | ** | 
|  | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | ** you may not use this file except in compliance with the License. | 
|  | 7 | ** You may obtain a copy of the License at | 
|  | 8 | ** | 
|  | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | ** | 
|  | 11 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | ** See the License for the specific language governing permissions and | 
|  | 15 | ** limitations under the License. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | #define LOG_TAG "AudioFlinger" | 
|  | 20 | //#define LOG_NDEBUG 0 | 
|  | 21 |  | 
| Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 22 | #include "Configuration.h" | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 23 | #include <linux/futex.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 24 | #include <math.h> | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 25 | #include <sys/syscall.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> | 
|  | 27 |  | 
|  | 28 | #include <private/media/AudioTrackShared.h> | 
|  | 29 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 30 | #include "AudioMixer.h" | 
|  | 31 | #include "AudioFlinger.h" | 
|  | 32 | #include "ServiceUtilities.h" | 
|  | 33 |  | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 34 | #include <media/nbaio/Pipe.h> | 
|  | 35 | #include <media/nbaio/PipeReader.h> | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 36 | #include <audio_utils/minifloat.h> | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 37 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- | 
|  | 39 |  | 
|  | 40 | // Note: the following macro is used for extremely verbose logging message.  In | 
|  | 41 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
|  | 42 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
|  | 43 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
|  | 44 | // turned on.  Do not uncomment the #def below unless you really know what you | 
|  | 45 | // are doing and want to see all of the extremely verbose messages. | 
|  | 46 | //#define VERY_VERY_VERBOSE_LOGGING | 
|  | 47 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
|  | 48 | #define ALOGVV ALOGV | 
|  | 49 | #else | 
|  | 50 | #define ALOGVV(a...) do { } while(0) | 
|  | 51 | #endif | 
|  | 52 |  | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 53 | // TODO move to a common header  (Also shared with AudioTrack.cpp) | 
|  | 54 | #define NANOS_PER_SECOND    1000000000 | 
|  | 55 | #define TIME_TO_NANOS(time) ((uint64_t)time.tv_sec * NANOS_PER_SECOND + time.tv_nsec) | 
|  | 56 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 57 | namespace android { | 
|  | 58 |  | 
|  | 59 | // ---------------------------------------------------------------------------- | 
|  | 60 | //      TrackBase | 
|  | 61 | // ---------------------------------------------------------------------------- | 
|  | 62 |  | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 63 | static volatile int32_t nextTrackId = 55; | 
|  | 64 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 65 | // TrackBase constructor must be called with AudioFlinger::mLock held | 
|  | 66 | AudioFlinger::ThreadBase::TrackBase::TrackBase( | 
|  | 67 | ThreadBase *thread, | 
|  | 68 | const sp<Client>& client, | 
|  | 69 | uint32_t sampleRate, | 
|  | 70 | audio_format_t format, | 
|  | 71 | audio_channel_mask_t channelMask, | 
|  | 72 | size_t frameCount, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 73 | void *buffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 74 | audio_session_t sessionId, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 75 | int clientUid, | 
| Glenn Kasten | 755b0a6 | 2014-05-13 11:30:28 -0700 | [diff] [blame] | 76 | IAudioFlinger::track_flags_t flags, | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 77 | bool isOut, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 78 | alloc_type alloc, | 
|  | 79 | track_type type) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 80 | :   RefBase(), | 
|  | 81 | mThread(thread), | 
|  | 82 | mClient(client), | 
|  | 83 | mCblk(NULL), | 
|  | 84 | // mBuffer | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 85 | mState(IDLE), | 
|  | 86 | mSampleRate(sampleRate), | 
|  | 87 | mFormat(format), | 
|  | 88 | mChannelMask(channelMask), | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 89 | mChannelCount(isOut ? | 
|  | 90 | audio_channel_count_from_out_mask(channelMask) : | 
|  | 91 | audio_channel_count_from_in_mask(channelMask)), | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 92 | mFrameSize(audio_has_proportional_frames(format) ? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 93 | mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)), | 
|  | 94 | mFrameCount(frameCount), | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 95 | mSessionId(sessionId), | 
| Glenn Kasten | 755b0a6 | 2014-05-13 11:30:28 -0700 | [diff] [blame] | 96 | mFlags(flags), | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 97 | mIsOut(isOut), | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 98 | mServerProxy(NULL), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 99 | mId(android_atomic_inc(&nextTrackId)), | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 100 | mTerminated(false), | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 101 | mType(type), | 
|  | 102 | mThreadIoHandle(thread->id()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 103 | { | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 104 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
|  | 105 | if (!isTrustedCallingUid(callingUid) || clientUid == -1) { | 
|  | 106 | ALOGW_IF(clientUid != -1 && clientUid != (int)callingUid, | 
|  | 107 | "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid); | 
|  | 108 | clientUid = (int)callingUid; | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 109 | } | 
|  | 110 | // clientUid contains the uid of the app that is responsible for this track, so we can blame | 
|  | 111 | // battery usage on it. | 
|  | 112 | mUid = clientUid; | 
|  | 113 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 114 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); | 
|  | 115 | size_t size = sizeof(audio_track_cblk_t); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 116 | size_t bufferSize = (buffer == NULL ? roundup(frameCount) : frameCount) * mFrameSize; | 
|  | 117 | if (buffer == NULL && alloc == ALLOC_CBLK) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 118 | size += bufferSize; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | if (client != 0) { | 
|  | 122 | mCblkMemory = client->heap()->allocate(size); | 
| Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 123 | if (mCblkMemory == 0 || | 
|  | 124 | (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer())) == NULL) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 125 | ALOGE("not enough memory for AudioTrack size=%zu", size); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 126 | client->heap()->dump("AudioTrack"); | 
| Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 127 | mCblkMemory.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 128 | return; | 
|  | 129 | } | 
|  | 130 | } else { | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 131 | // this syntax avoids calling the audio_track_cblk_t constructor twice | 
|  | 132 | mCblk = (audio_track_cblk_t *) new uint8_t[size]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 133 | // assume mCblk != NULL | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | // construct the shared structure in-place. | 
|  | 137 | if (mCblk != NULL) { | 
|  | 138 | new(mCblk) audio_track_cblk_t(); | 
| Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 139 | switch (alloc) { | 
|  | 140 | case ALLOC_READONLY: { | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 141 | const sp<MemoryDealer> roHeap(thread->readOnlyHeap()); | 
|  | 142 | if (roHeap == 0 || | 
|  | 143 | (mBufferMemory = roHeap->allocate(bufferSize)) == 0 || | 
|  | 144 | (mBuffer = mBufferMemory->pointer()) == NULL) { | 
|  | 145 | ALOGE("not enough memory for read-only buffer size=%zu", bufferSize); | 
|  | 146 | if (roHeap != 0) { | 
|  | 147 | roHeap->dump("buffer"); | 
|  | 148 | } | 
|  | 149 | mCblkMemory.clear(); | 
|  | 150 | mBufferMemory.clear(); | 
|  | 151 | return; | 
|  | 152 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 153 | memset(mBuffer, 0, bufferSize); | 
| Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 154 | } break; | 
|  | 155 | case ALLOC_PIPE: | 
|  | 156 | mBufferMemory = thread->pipeMemory(); | 
|  | 157 | // mBuffer is the virtual address as seen from current process (mediaserver), | 
|  | 158 | // and should normally be coming from mBufferMemory->pointer(). | 
|  | 159 | // However in this case the TrackBase does not reference the buffer directly. | 
|  | 160 | // It should references the buffer via the pipe. | 
|  | 161 | // Therefore, to detect incorrect usage of the buffer, we set mBuffer to NULL. | 
|  | 162 | mBuffer = NULL; | 
|  | 163 | break; | 
|  | 164 | case ALLOC_CBLK: | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 165 | // clear all buffers | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 166 | if (buffer == NULL) { | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 167 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); | 
|  | 168 | memset(mBuffer, 0, bufferSize); | 
|  | 169 | } else { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 170 | mBuffer = buffer; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 171 | #if 0 | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 172 | mCblk->mFlags = CBLK_FORCEREADY;    // FIXME hack, need to fix the track ready logic | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 173 | #endif | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 174 | } | 
| Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 175 | break; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 176 | case ALLOC_LOCAL: | 
|  | 177 | mBuffer = calloc(1, bufferSize); | 
|  | 178 | break; | 
|  | 179 | case ALLOC_NONE: | 
|  | 180 | mBuffer = buffer; | 
|  | 181 | break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 182 | } | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 183 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 184 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 185 | if (mTeeSinkTrackEnabled) { | 
| Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 186 | NBAIO_Format pipeFormat = Format_from_SR_C(mSampleRate, mChannelCount, mFormat); | 
| Glenn Kasten | 6e0d67d | 2014-01-31 09:41:08 -0800 | [diff] [blame] | 187 | if (Format_isValid(pipeFormat)) { | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 188 | Pipe *pipe = new Pipe(mTeeSinkTrackFrames, pipeFormat); | 
|  | 189 | size_t numCounterOffers = 0; | 
|  | 190 | const NBAIO_Format offers[1] = {pipeFormat}; | 
|  | 191 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); | 
|  | 192 | ALOG_ASSERT(index == 0); | 
|  | 193 | PipeReader *pipeReader = new PipeReader(*pipe); | 
|  | 194 | numCounterOffers = 0; | 
|  | 195 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); | 
|  | 196 | ALOG_ASSERT(index == 0); | 
|  | 197 | mTeeSink = pipe; | 
|  | 198 | mTeeSource = pipeReader; | 
|  | 199 | } | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 200 | } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 201 | #endif | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 202 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 203 | } | 
|  | 204 | } | 
|  | 205 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 206 | status_t AudioFlinger::ThreadBase::TrackBase::initCheck() const | 
|  | 207 | { | 
|  | 208 | status_t status; | 
|  | 209 | if (mType == TYPE_OUTPUT || mType == TYPE_PATCH) { | 
|  | 210 | status = cblk() != NULL ? NO_ERROR : NO_MEMORY; | 
|  | 211 | } else { | 
|  | 212 | status = getCblk() != 0 ? NO_ERROR : NO_MEMORY; | 
|  | 213 | } | 
|  | 214 | return status; | 
|  | 215 | } | 
|  | 216 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 217 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() | 
|  | 218 | { | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 219 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 220 | dumpTee(-1, mTeeSource, mId); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 221 | #endif | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 222 | // delete the proxy before deleting the shared memory it refers to, to avoid dangling reference | 
|  | 223 | delete mServerProxy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 224 | if (mCblk != NULL) { | 
|  | 225 | if (mClient == 0) { | 
|  | 226 | delete mCblk; | 
|  | 227 | } else { | 
|  | 228 | mCblk->~audio_track_cblk_t();   // destroy our shared-structure. | 
|  | 229 | } | 
|  | 230 | } | 
|  | 231 | mCblkMemory.clear();    // free the shared memory before releasing the heap it belongs to | 
|  | 232 | if (mClient != 0) { | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 233 | // Client destructor must run with AudioFlinger client mutex locked | 
|  | 234 | Mutex::Autolock _l(mClient->audioFlinger()->mClientLock); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 235 | // If the client's reference count drops to zero, the associated destructor | 
|  | 236 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than | 
|  | 237 | // relying on the automatic clear() at end of scope. | 
|  | 238 | mClient.clear(); | 
|  | 239 | } | 
| Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 240 | // flush the binder command buffer | 
|  | 241 | IPCThreadState::self()->flushCommands(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
|  | 244 | // AudioBufferProvider interface | 
|  | 245 | // getNextBuffer() = 0; | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 246 | // This implementation of releaseBuffer() is used by Track and RecordTrack | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 247 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 248 | { | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 249 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 250 | if (mTeeSink != 0) { | 
|  | 251 | (void) mTeeSink->write(buffer->raw, buffer->frameCount); | 
|  | 252 | } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 253 | #endif | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 254 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 255 | ServerProxy::Buffer buf; | 
|  | 256 | buf.mFrameCount = buffer->frameCount; | 
|  | 257 | buf.mRaw = buffer->raw; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 258 | buffer->frameCount = 0; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 259 | buffer->raw = NULL; | 
|  | 260 | mServerProxy->releaseBuffer(&buf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 263 | status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event) | 
|  | 264 | { | 
|  | 265 | mSyncEvents.add(event); | 
|  | 266 | return NO_ERROR; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | // ---------------------------------------------------------------------------- | 
|  | 270 | //      Playback | 
|  | 271 | // ---------------------------------------------------------------------------- | 
|  | 272 |  | 
|  | 273 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) | 
|  | 274 | : BnAudioTrack(), | 
|  | 275 | mTrack(track) | 
|  | 276 | { | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | AudioFlinger::TrackHandle::~TrackHandle() { | 
|  | 280 | // just stop the track on deletion, associated resources | 
|  | 281 | // will be freed from the main thread once all pending buffers have | 
|  | 282 | // been played. Unless it's not in the active track list, in which | 
|  | 283 | // case we free everything now... | 
|  | 284 | mTrack->destroy(); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { | 
|  | 288 | return mTrack->getCblk(); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | status_t AudioFlinger::TrackHandle::start() { | 
|  | 292 | return mTrack->start(); | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | void AudioFlinger::TrackHandle::stop() { | 
|  | 296 | mTrack->stop(); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | void AudioFlinger::TrackHandle::flush() { | 
|  | 300 | mTrack->flush(); | 
|  | 301 | } | 
|  | 302 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 303 | void AudioFlinger::TrackHandle::pause() { | 
|  | 304 | mTrack->pause(); | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) | 
|  | 308 | { | 
|  | 309 | return mTrack->attachAuxEffect(EffectId); | 
|  | 310 | } | 
|  | 311 |  | 
| Glenn Kasten | 3dcd00d | 2013-07-17 10:10:23 -0700 | [diff] [blame] | 312 | status_t AudioFlinger::TrackHandle::setParameters(const String8& keyValuePairs) { | 
|  | 313 | return mTrack->setParameters(keyValuePairs); | 
|  | 314 | } | 
|  | 315 |  | 
| Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 316 | status_t AudioFlinger::TrackHandle::getTimestamp(AudioTimestamp& timestamp) | 
|  | 317 | { | 
| Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 318 | return mTrack->getTimestamp(timestamp); | 
| Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 319 | } | 
|  | 320 |  | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 321 |  | 
|  | 322 | void AudioFlinger::TrackHandle::signal() | 
|  | 323 | { | 
|  | 324 | return mTrack->signal(); | 
|  | 325 | } | 
|  | 326 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 327 | status_t AudioFlinger::TrackHandle::onTransact( | 
|  | 328 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 329 | { | 
|  | 330 | return BnAudioTrack::onTransact(code, data, reply, flags); | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | // ---------------------------------------------------------------------------- | 
|  | 334 |  | 
|  | 335 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held | 
|  | 336 | AudioFlinger::PlaybackThread::Track::Track( | 
|  | 337 | PlaybackThread *thread, | 
|  | 338 | const sp<Client>& client, | 
|  | 339 | audio_stream_type_t streamType, | 
|  | 340 | uint32_t sampleRate, | 
|  | 341 | audio_format_t format, | 
|  | 342 | audio_channel_mask_t channelMask, | 
|  | 343 | size_t frameCount, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 344 | void *buffer, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 345 | const sp<IMemory>& sharedBuffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 346 | audio_session_t sessionId, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 347 | int uid, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 348 | IAudioFlinger::track_flags_t flags, | 
|  | 349 | track_type type) | 
|  | 350 | :   TrackBase(thread, client, sampleRate, format, channelMask, frameCount, | 
|  | 351 | (sharedBuffer != 0) ? sharedBuffer->pointer() : buffer, | 
|  | 352 | sessionId, uid, flags, true /*isOut*/, | 
|  | 353 | (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK, | 
|  | 354 | type), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 355 | mFillingUpStatus(FS_INVALID), | 
|  | 356 | // mRetryCount initialized later when needed | 
|  | 357 | mSharedBuffer(sharedBuffer), | 
|  | 358 | mStreamType(streamType), | 
|  | 359 | mName(-1),  // see note below | 
|  | 360 | mMainBuffer(thread->mixBuffer()), | 
|  | 361 | mAuxBuffer(NULL), | 
|  | 362 | mAuxEffectId(0), mHasVolumeController(false), | 
|  | 363 | mPresentationCompleteFrames(0), | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 364 | mFrameMap(16 /* sink-frame-to-track-frame map memory */), | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 365 | // mSinkTimestamp | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 366 | mFastIndex(-1), | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 367 | mCachedVolume(1.0), | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 368 | mIsInvalid(false), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 369 | mAudioTrackServerProxy(NULL), | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 370 | mResumeToStopping(false), | 
| Phil Burk | 1b42097 | 2015-04-22 10:52:21 -0700 | [diff] [blame] | 371 | mFlushHwPending(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 372 | { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 373 | // client == 0 implies sharedBuffer == 0 | 
|  | 374 | ALOG_ASSERT(!(client == 0 && sharedBuffer != 0)); | 
|  | 375 |  | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 376 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %zu", sharedBuffer->pointer(), | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 377 | sharedBuffer->size()); | 
|  | 378 |  | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 379 | if (mCblk == NULL) { | 
|  | 380 | return; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 381 | } | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 382 |  | 
|  | 383 | if (sharedBuffer == 0) { | 
|  | 384 | mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 385 | mFrameSize, !isExternalTrack(), sampleRate); | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 386 | } else { | 
|  | 387 | mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount, | 
|  | 388 | mFrameSize); | 
|  | 389 | } | 
|  | 390 | mServerProxy = mAudioTrackServerProxy; | 
|  | 391 |  | 
| Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 392 | mName = thread->getTrackName_l(channelMask, format, sessionId); | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 393 | if (mName < 0) { | 
|  | 394 | ALOGE("no more track names available"); | 
|  | 395 | return; | 
|  | 396 | } | 
|  | 397 | // only allocate a fast track index if we were able to allocate a normal track name | 
|  | 398 | if (flags & IAudioFlinger::TRACK_FAST) { | 
| Andy Hung | a542782 | 2015-09-11 16:15:35 -0700 | [diff] [blame] | 399 | // FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential | 
|  | 400 | // race with setSyncEvent(). However, if we call it, we cannot properly start | 
|  | 401 | // static fast tracks (SoundPool) immediately after stopping. | 
|  | 402 | //mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads(); | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 403 | ALOG_ASSERT(thread->mFastTrackAvailMask != 0); | 
|  | 404 | int i = __builtin_ctz(thread->mFastTrackAvailMask); | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 405 | ALOG_ASSERT(0 < i && i < (int)FastMixerState::sMaxFastTracks); | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 406 | // FIXME This is too eager.  We allocate a fast track index before the | 
|  | 407 | //       fast track becomes active.  Since fast tracks are a scarce resource, | 
|  | 408 | //       this means we are potentially denying other more important fast tracks from | 
|  | 409 | //       being created.  It would be better to allocate the index dynamically. | 
|  | 410 | mFastIndex = i; | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 411 | thread->mFastTrackAvailMask &= ~(1 << i); | 
|  | 412 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
|  | 415 | AudioFlinger::PlaybackThread::Track::~Track() | 
|  | 416 | { | 
|  | 417 | ALOGV("PlaybackThread::Track destructor"); | 
| Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 418 |  | 
|  | 419 | // The destructor would clear mSharedBuffer, | 
|  | 420 | // but it will not push the decremented reference count, | 
|  | 421 | // leaving the client's IMemory dangling indefinitely. | 
|  | 422 | // This prevents that leak. | 
|  | 423 | if (mSharedBuffer != 0) { | 
|  | 424 | mSharedBuffer.clear(); | 
| Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 425 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 426 | } | 
|  | 427 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 428 | status_t AudioFlinger::PlaybackThread::Track::initCheck() const | 
|  | 429 | { | 
|  | 430 | status_t status = TrackBase::initCheck(); | 
|  | 431 | if (status == NO_ERROR && mName < 0) { | 
|  | 432 | status = NO_MEMORY; | 
|  | 433 | } | 
|  | 434 | return status; | 
|  | 435 | } | 
|  | 436 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 437 | void AudioFlinger::PlaybackThread::Track::destroy() | 
|  | 438 | { | 
|  | 439 | // NOTE: destroyTrack_l() can remove a strong reference to this Track | 
|  | 440 | // by removing it from mTracks vector, so there is a risk that this Tracks's | 
|  | 441 | // destructor is called. As the destructor needs to lock mLock, | 
|  | 442 | // we must acquire a strong reference on this Track before locking mLock | 
|  | 443 | // here so that the destructor is called only when exiting this function. | 
|  | 444 | // On the other hand, as long as Track::destroy() is only called by | 
|  | 445 | // TrackHandle destructor, the TrackHandle still holds a strong ref on | 
|  | 446 | // this Track with its member mTrack. | 
|  | 447 | sp<Track> keep(this); | 
|  | 448 | { // scope for mLock | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 449 | bool wasActive = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 450 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 451 | if (thread != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 452 | Mutex::Autolock _l(thread->mLock); | 
|  | 453 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 454 | wasActive = playbackThread->destroyTrack_l(this); | 
|  | 455 | } | 
|  | 456 | if (isExternalTrack() && !wasActive) { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 457 | AudioSystem::releaseOutput(mThreadIoHandle, mStreamType, mSessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 458 | } | 
|  | 459 | } | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | /*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) | 
|  | 463 | { | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 464 | result.append("    Name Active Client Type      Fmt Chn mask Session fCount S F SRate  " | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 465 | "L dB  R dB    Server Main buf  Aux Buf Flags UndFrmCnt\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 468 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size, bool active) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 469 | { | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 470 | gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 471 | if (isFastTrack()) { | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 472 | sprintf(buffer, "    F %2d", mFastIndex); | 
|  | 473 | } else if (mName >= AudioMixer::TRACK0) { | 
|  | 474 | sprintf(buffer, "    %4d", mName - AudioMixer::TRACK0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 475 | } else { | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 476 | sprintf(buffer, "    none"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 477 | } | 
|  | 478 | track_state state = mState; | 
|  | 479 | char stateChar; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 480 | if (isTerminated()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 481 | stateChar = 'T'; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 482 | } else { | 
|  | 483 | switch (state) { | 
|  | 484 | case IDLE: | 
|  | 485 | stateChar = 'I'; | 
|  | 486 | break; | 
|  | 487 | case STOPPING_1: | 
|  | 488 | stateChar = 's'; | 
|  | 489 | break; | 
|  | 490 | case STOPPING_2: | 
|  | 491 | stateChar = '5'; | 
|  | 492 | break; | 
|  | 493 | case STOPPED: | 
|  | 494 | stateChar = 'S'; | 
|  | 495 | break; | 
|  | 496 | case RESUMING: | 
|  | 497 | stateChar = 'R'; | 
|  | 498 | break; | 
|  | 499 | case ACTIVE: | 
|  | 500 | stateChar = 'A'; | 
|  | 501 | break; | 
|  | 502 | case PAUSING: | 
|  | 503 | stateChar = 'p'; | 
|  | 504 | break; | 
|  | 505 | case PAUSED: | 
|  | 506 | stateChar = 'P'; | 
|  | 507 | break; | 
|  | 508 | case FLUSHED: | 
|  | 509 | stateChar = 'F'; | 
|  | 510 | break; | 
|  | 511 | default: | 
|  | 512 | stateChar = '?'; | 
|  | 513 | break; | 
|  | 514 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 515 | } | 
|  | 516 | char nowInUnderrun; | 
|  | 517 | switch (mObservedUnderruns.mBitFields.mMostRecent) { | 
|  | 518 | case UNDERRUN_FULL: | 
|  | 519 | nowInUnderrun = ' '; | 
|  | 520 | break; | 
|  | 521 | case UNDERRUN_PARTIAL: | 
|  | 522 | nowInUnderrun = '<'; | 
|  | 523 | break; | 
|  | 524 | case UNDERRUN_EMPTY: | 
|  | 525 | nowInUnderrun = '*'; | 
|  | 526 | break; | 
|  | 527 | default: | 
|  | 528 | nowInUnderrun = '?'; | 
|  | 529 | break; | 
|  | 530 | } | 
| Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 531 | snprintf(&buffer[8], size-8, " %6s %6u %4u %08X %08X %7u %6zu %1c %1d %5u %5.2g %5.2g  " | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 532 | "%08X %p %p 0x%03X %9u%c\n", | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 533 | active ? "yes" : "no", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 534 | (mClient == 0) ? getpid_cached : mClient->pid(), | 
|  | 535 | mStreamType, | 
|  | 536 | mFormat, | 
|  | 537 | mChannelMask, | 
|  | 538 | mSessionId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 539 | mFrameCount, | 
|  | 540 | stateChar, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 541 | mFillingUpStatus, | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 542 | mAudioTrackServerProxy->getSampleRate(), | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 543 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))), | 
|  | 544 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))), | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 545 | mCblk->mServer, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 546 | mMainBuffer, | 
|  | 547 | mAuxBuffer, | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 548 | mCblk->mFlags, | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 549 | mAudioTrackServerProxy->getUnderrunFrames(), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 550 | nowInUnderrun); | 
|  | 551 | } | 
|  | 552 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 553 | uint32_t AudioFlinger::PlaybackThread::Track::sampleRate() const { | 
|  | 554 | return mAudioTrackServerProxy->getSampleRate(); | 
|  | 555 | } | 
|  | 556 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 557 | // AudioBufferProvider interface | 
|  | 558 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 559 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 560 | { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 561 | ServerProxy::Buffer buf; | 
|  | 562 | size_t desiredFrames = buffer->frameCount; | 
|  | 563 | buf.mFrameCount = desiredFrames; | 
|  | 564 | status_t status = mServerProxy->obtainBuffer(&buf); | 
|  | 565 | buffer->frameCount = buf.mFrameCount; | 
|  | 566 | buffer->raw = buf.mRaw; | 
|  | 567 | if (buf.mFrameCount == 0) { | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 568 | mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 569 | } else { | 
|  | 570 | mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 571 | } | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 572 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 573 | return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 574 | } | 
|  | 575 |  | 
| Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 576 | // releaseBuffer() is not overridden | 
|  | 577 |  | 
|  | 578 | // ExtendedAudioBufferProvider interface | 
|  | 579 |  | 
| Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 580 | // framesReady() may return an approximation of the number of frames if called | 
|  | 581 | // from a different thread than the one calling Proxy->obtainBuffer() and | 
|  | 582 | // Proxy->releaseBuffer(). Also note there is no mutual exclusion in the | 
|  | 583 | // AudioTrackServerProxy so be especially careful calling with FastTracks. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 584 | size_t AudioFlinger::PlaybackThread::Track::framesReady() const { | 
| Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 585 | if (mSharedBuffer != 0 && (isStopped() || isStopping())) { | 
|  | 586 | // Static tracks return zero frames immediately upon stopping (for FastTracks). | 
|  | 587 | // The remainder of the buffer is not drained. | 
|  | 588 | return 0; | 
|  | 589 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 590 | return mAudioTrackServerProxy->framesReady(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 591 | } | 
|  | 592 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 593 | int64_t AudioFlinger::PlaybackThread::Track::framesReleased() const | 
| Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 594 | { | 
|  | 595 | return mAudioTrackServerProxy->framesReleased(); | 
|  | 596 | } | 
|  | 597 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 598 | void AudioFlinger::PlaybackThread::Track::onTimestamp(const ExtendedTimestamp ×tamp) | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 599 | { | 
|  | 600 | // This call comes from a FastTrack and should be kept lockless. | 
|  | 601 | // The server side frames are already translated to client frames. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 602 | mAudioTrackServerProxy->setTimestamp(timestamp); | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 603 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 604 | // We do not set drained here, as FastTrack timestamp may not go to very last frame. | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 605 | } | 
|  | 606 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 607 | // Don't call for fast tracks; the framesReady() could result in priority inversion | 
|  | 608 | bool AudioFlinger::PlaybackThread::Track::isReady() const { | 
| Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 609 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) { | 
|  | 610 | return true; | 
|  | 611 | } | 
|  | 612 |  | 
| Eric Laurent | 1649851 | 2014-03-17 17:22:08 -0700 | [diff] [blame] | 613 | if (isStopping()) { | 
|  | 614 | if (framesReady() > 0) { | 
|  | 615 | mFillingUpStatus = FS_FILLED; | 
|  | 616 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 617 | return true; | 
|  | 618 | } | 
|  | 619 |  | 
| Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 620 | if (framesReady() >= mServerProxy->getBufferSizeInFrames() || | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 621 | (mCblk->mFlags & CBLK_FORCEREADY)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 622 | mFillingUpStatus = FS_FILLED; | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 623 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 624 | return true; | 
|  | 625 | } | 
|  | 626 | return false; | 
|  | 627 | } | 
|  | 628 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 629 | status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event __unused, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 630 | audio_session_t triggerSession __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 631 | { | 
|  | 632 | status_t status = NO_ERROR; | 
|  | 633 | ALOGV("start(%d), calling pid %d session %d", | 
|  | 634 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); | 
|  | 635 |  | 
|  | 636 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 637 | if (thread != 0) { | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 638 | if (isOffloaded()) { | 
|  | 639 | Mutex::Autolock _laf(thread->mAudioFlinger->mLock); | 
|  | 640 | Mutex::Autolock _lth(thread->mLock); | 
|  | 641 | sp<EffectChain> ec = thread->getEffectChain_l(mSessionId); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 642 | if (thread->mAudioFlinger->isNonOffloadableGlobalEffectEnabled_l() || | 
|  | 643 | (ec != 0 && ec->isNonOffloadableEnabled())) { | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 644 | invalidate(); | 
|  | 645 | return PERMISSION_DENIED; | 
|  | 646 | } | 
|  | 647 | } | 
|  | 648 | Mutex::Autolock _lth(thread->mLock); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 649 | track_state state = mState; | 
|  | 650 | // here the track could be either new, or restarted | 
|  | 651 | // in both cases "unstop" the track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 652 |  | 
| Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 653 | // initial state-stopping. next state-pausing. | 
|  | 654 | // What if resume is called ? | 
|  | 655 |  | 
|  | 656 | if (state == PAUSED || state == PAUSING) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 657 | if (mResumeToStopping) { | 
|  | 658 | // happened we need to resume to STOPPING_1 | 
|  | 659 | mState = TrackBase::STOPPING_1; | 
|  | 660 | ALOGV("PAUSED => STOPPING_1 (%d) on thread %p", mName, this); | 
|  | 661 | } else { | 
|  | 662 | mState = TrackBase::RESUMING; | 
|  | 663 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); | 
|  | 664 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 665 | } else { | 
|  | 666 | mState = TrackBase::ACTIVE; | 
|  | 667 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); | 
|  | 668 | } | 
|  | 669 |  | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 670 | // states to reset position info for non-offloaded/direct tracks | 
|  | 671 | if (!isOffloaded() && !isDirect() | 
|  | 672 | && (state == IDLE || state == STOPPED || state == FLUSHED)) { | 
|  | 673 | mFrameMap.reset(); | 
|  | 674 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 675 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
| Haynes Mathew George | 240934b | 2015-03-11 18:25:50 -0700 | [diff] [blame] | 676 | if (isFastTrack()) { | 
|  | 677 | // refresh fast track underruns on start because that field is never cleared | 
|  | 678 | // by the fast mixer; furthermore, the same track can be recycled, i.e. start | 
|  | 679 | // after stop. | 
|  | 680 | mObservedUnderruns = playbackThread->getFastTrackUnderruns(mFastIndex); | 
|  | 681 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 682 | status = playbackThread->addTrack_l(this); | 
|  | 683 | if (status == INVALID_OPERATION || status == PERMISSION_DENIED) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 684 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 685 | //  restore previous state if start was rejected by policy manager | 
|  | 686 | if (status == PERMISSION_DENIED) { | 
|  | 687 | mState = state; | 
|  | 688 | } | 
|  | 689 | } | 
|  | 690 | // track was already in the active list, not a problem | 
|  | 691 | if (status == ALREADY_EXISTS) { | 
|  | 692 | status = NO_ERROR; | 
| Glenn Kasten | 12022ff | 2013-10-17 11:32:39 -0700 | [diff] [blame] | 693 | } else { | 
|  | 694 | // Acknowledge any pending flush(), so that subsequent new data isn't discarded. | 
|  | 695 | // It is usually unsafe to access the server proxy from a binder thread. | 
|  | 696 | // But in this case we know the mixer thread (whether normal mixer or fast mixer) | 
|  | 697 | // isn't looking at this track yet:  we still hold the normal mixer thread lock, | 
|  | 698 | // and for fast tracks the track is not yet in the fast mixer thread's active set. | 
| Andy Hung | e6fb82a | 2015-09-09 14:39:02 -0700 | [diff] [blame] | 699 | // For static tracks, this is used to acknowledge change in position or loop. | 
| Eric Laurent | 564d144 | 2015-09-09 12:26:52 -0700 | [diff] [blame] | 700 | ServerProxy::Buffer buffer; | 
|  | 701 | buffer.mFrameCount = 1; | 
|  | 702 | (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 703 | } | 
|  | 704 | } else { | 
|  | 705 | status = BAD_VALUE; | 
|  | 706 | } | 
|  | 707 | return status; | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | void AudioFlinger::PlaybackThread::Track::stop() | 
|  | 711 | { | 
|  | 712 | ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); | 
|  | 713 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 714 | if (thread != 0) { | 
|  | 715 | Mutex::Autolock _l(thread->mLock); | 
|  | 716 | track_state state = mState; | 
|  | 717 | if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) { | 
|  | 718 | // If the track is not active (PAUSED and buffers full), flush buffers | 
|  | 719 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 720 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { | 
|  | 721 | reset(); | 
|  | 722 | mState = STOPPED; | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 723 | } else if (!isFastTrack() && !isOffloaded() && !isDirect()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 724 | mState = STOPPED; | 
|  | 725 | } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 726 | // For fast tracks prepareTracks_l() will set state to STOPPING_2 | 
|  | 727 | // presentation is complete | 
|  | 728 | // For an offloaded track this starts a drain and state will | 
|  | 729 | // move to STOPPING_2 when drain completes and then STOPPED | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 730 | mState = STOPPING_1; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 731 | if (isOffloaded()) { | 
|  | 732 | mRetryCount = PlaybackThread::kMaxTrackStopRetriesOffload; | 
|  | 733 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 734 | } | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 735 | playbackThread->broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 736 | ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName, | 
|  | 737 | playbackThread); | 
|  | 738 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 739 | } | 
|  | 740 | } | 
|  | 741 |  | 
|  | 742 | void AudioFlinger::PlaybackThread::Track::pause() | 
|  | 743 | { | 
|  | 744 | ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); | 
|  | 745 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 746 | if (thread != 0) { | 
|  | 747 | Mutex::Autolock _l(thread->mLock); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 748 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 749 | switch (mState) { | 
|  | 750 | case STOPPING_1: | 
|  | 751 | case STOPPING_2: | 
|  | 752 | if (!isOffloaded()) { | 
|  | 753 | /* nothing to do if track is not offloaded */ | 
|  | 754 | break; | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | // Offloaded track was draining, we need to carry on draining when resumed | 
|  | 758 | mResumeToStopping = true; | 
|  | 759 | // fall through... | 
|  | 760 | case ACTIVE: | 
|  | 761 | case RESUMING: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 762 | mState = PAUSING; | 
|  | 763 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 764 | playbackThread->broadcast_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 765 | break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 766 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 767 | default: | 
|  | 768 | break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 769 | } | 
|  | 770 | } | 
|  | 771 | } | 
|  | 772 |  | 
|  | 773 | void AudioFlinger::PlaybackThread::Track::flush() | 
|  | 774 | { | 
|  | 775 | ALOGV("flush(%d)", mName); | 
|  | 776 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 777 | if (thread != 0) { | 
|  | 778 | Mutex::Autolock _l(thread->mLock); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 779 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 780 |  | 
|  | 781 | if (isOffloaded()) { | 
|  | 782 | // If offloaded we allow flush during any state except terminated | 
|  | 783 | // and keep the track active to avoid problems if user is seeking | 
|  | 784 | // rapidly and underlying hardware has a significant delay handling | 
|  | 785 | // a pause | 
|  | 786 | if (isTerminated()) { | 
|  | 787 | return; | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | ALOGV("flush: offload flush"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 791 | reset(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 792 |  | 
|  | 793 | if (mState == STOPPING_1 || mState == STOPPING_2) { | 
|  | 794 | ALOGV("flushed in STOPPING_1 or 2 state, change state to ACTIVE"); | 
|  | 795 | mState = ACTIVE; | 
|  | 796 | } | 
|  | 797 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 798 | mFlushHwPending = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 799 | mResumeToStopping = false; | 
|  | 800 | } else { | 
|  | 801 | if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && | 
|  | 802 | mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) { | 
|  | 803 | return; | 
|  | 804 | } | 
|  | 805 | // No point remaining in PAUSED state after a flush => go to | 
|  | 806 | // FLUSHED state | 
|  | 807 | mState = FLUSHED; | 
|  | 808 | // do not reset the track if it is still in the process of being stopped or paused. | 
|  | 809 | // this will be done by prepareTracks_l() when the track is stopped. | 
|  | 810 | // prepareTracks_l() will see mState == FLUSHED, then | 
|  | 811 | // remove from active track list, reset(), and trigger presentation complete | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 812 | if (isDirect()) { | 
|  | 813 | mFlushHwPending = true; | 
|  | 814 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 815 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { | 
|  | 816 | reset(); | 
|  | 817 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 818 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 819 | // Prevent flush being lost if the track is flushed and then resumed | 
|  | 820 | // before mixer thread can run. This is important when offloading | 
|  | 821 | // because the hardware buffer could hold a large amount of audio | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 822 | playbackThread->broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 823 | } | 
|  | 824 | } | 
|  | 825 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 826 | // must be called with thread lock held | 
|  | 827 | void AudioFlinger::PlaybackThread::Track::flushAck() | 
|  | 828 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 829 | if (!isOffloaded() && !isDirect()) | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 830 | return; | 
|  | 831 |  | 
|  | 832 | mFlushHwPending = false; | 
|  | 833 | } | 
|  | 834 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 835 | void AudioFlinger::PlaybackThread::Track::reset() | 
|  | 836 | { | 
|  | 837 | // Do not reset twice to avoid discarding data written just after a flush and before | 
|  | 838 | // the audioflinger thread detects the track is stopped. | 
|  | 839 | if (!mResetDone) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 840 | // Force underrun condition to avoid false underrun callback until first data is | 
|  | 841 | // written to buffer | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 842 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 843 | mFillingUpStatus = FS_FILLING; | 
|  | 844 | mResetDone = true; | 
|  | 845 | if (mState == FLUSHED) { | 
|  | 846 | mState = IDLE; | 
|  | 847 | } | 
|  | 848 | } | 
|  | 849 | } | 
|  | 850 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 851 | status_t AudioFlinger::PlaybackThread::Track::setParameters(const String8& keyValuePairs) | 
|  | 852 | { | 
|  | 853 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 854 | if (thread == 0) { | 
|  | 855 | ALOGE("thread is dead"); | 
|  | 856 | return FAILED_TRANSACTION; | 
|  | 857 | } else if ((thread->type() == ThreadBase::DIRECT) || | 
|  | 858 | (thread->type() == ThreadBase::OFFLOAD)) { | 
|  | 859 | return thread->setParameters(keyValuePairs); | 
|  | 860 | } else { | 
|  | 861 | return PERMISSION_DENIED; | 
|  | 862 | } | 
|  | 863 | } | 
|  | 864 |  | 
| Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 865 | status_t AudioFlinger::PlaybackThread::Track::getTimestamp(AudioTimestamp& timestamp) | 
|  | 866 | { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 867 | if (!isOffloaded() && !isDirect()) { | 
|  | 868 | return INVALID_OPERATION; // normal tracks handled through SSQ | 
| Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 869 | } | 
| Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 870 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 871 | if (thread == 0) { | 
| Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 872 | return INVALID_OPERATION; | 
| Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 873 | } | 
| Phil Burk | 6140c79 | 2015-03-19 14:30:21 -0700 | [diff] [blame] | 874 |  | 
| Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 875 | Mutex::Autolock _l(thread->mLock); | 
|  | 876 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 877 | return playbackThread->getTimestamp_l(timestamp); | 
| Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 878 | } | 
|  | 879 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 880 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) | 
|  | 881 | { | 
|  | 882 | status_t status = DEAD_OBJECT; | 
|  | 883 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 884 | if (thread != 0) { | 
|  | 885 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 886 | sp<AudioFlinger> af = mClient->audioFlinger(); | 
|  | 887 |  | 
|  | 888 | Mutex::Autolock _l(af->mLock); | 
|  | 889 |  | 
|  | 890 | sp<PlaybackThread> srcThread = af->getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
|  | 891 |  | 
|  | 892 | if (EffectId != 0 && srcThread != 0 && playbackThread != srcThread.get()) { | 
|  | 893 | Mutex::Autolock _dl(playbackThread->mLock); | 
|  | 894 | Mutex::Autolock _sl(srcThread->mLock); | 
|  | 895 | sp<EffectChain> chain = srcThread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
|  | 896 | if (chain == 0) { | 
|  | 897 | return INVALID_OPERATION; | 
|  | 898 | } | 
|  | 899 |  | 
|  | 900 | sp<EffectModule> effect = chain->getEffectFromId_l(EffectId); | 
|  | 901 | if (effect == 0) { | 
|  | 902 | return INVALID_OPERATION; | 
|  | 903 | } | 
|  | 904 | srcThread->removeEffect_l(effect); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 905 | status = playbackThread->addEffect_l(effect); | 
|  | 906 | if (status != NO_ERROR) { | 
|  | 907 | srcThread->addEffect_l(effect); | 
|  | 908 | return INVALID_OPERATION; | 
|  | 909 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 910 | // removeEffect_l() has stopped the effect if it was active so it must be restarted | 
|  | 911 | if (effect->state() == EffectModule::ACTIVE || | 
|  | 912 | effect->state() == EffectModule::STOPPING) { | 
|  | 913 | effect->start(); | 
|  | 914 | } | 
|  | 915 |  | 
|  | 916 | sp<EffectChain> dstChain = effect->chain().promote(); | 
|  | 917 | if (dstChain == 0) { | 
|  | 918 | srcThread->addEffect_l(effect); | 
|  | 919 | return INVALID_OPERATION; | 
|  | 920 | } | 
|  | 921 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 922 | AudioSystem::registerEffect(&effect->desc(), | 
|  | 923 | srcThread->id(), | 
|  | 924 | dstChain->strategy(), | 
|  | 925 | AUDIO_SESSION_OUTPUT_MIX, | 
|  | 926 | effect->id()); | 
| Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 927 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 928 | } | 
|  | 929 | status = playbackThread->attachAuxEffect(this, EffectId); | 
|  | 930 | } | 
|  | 931 | return status; | 
|  | 932 | } | 
|  | 933 |  | 
|  | 934 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) | 
|  | 935 | { | 
|  | 936 | mAuxEffectId = EffectId; | 
|  | 937 | mAuxBuffer = buffer; | 
|  | 938 | } | 
|  | 939 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 940 | bool AudioFlinger::PlaybackThread::Track::presentationComplete( | 
|  | 941 | int64_t framesWritten, size_t audioHalFrames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 942 | { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 943 | // TODO: improve this based on FrameMap if it exists, to ensure full drain. | 
|  | 944 | // This assists in proper timestamp computation as well as wakelock management. | 
|  | 945 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 946 | // a track is considered presented when the total number of frames written to audio HAL | 
|  | 947 | // corresponds to the number of frames written when presentationComplete() is called for the | 
|  | 948 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 949 | // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used | 
|  | 950 | // to detect when all frames have been played. In this case framesWritten isn't | 
|  | 951 | // useful because it doesn't always reflect whether there is data in the h/w | 
|  | 952 | // buffers, particularly if a track has been paused and resumed during draining | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 953 | ALOGV("presentationComplete() mPresentationCompleteFrames %lld framesWritten %lld", | 
|  | 954 | (long long)mPresentationCompleteFrames, (long long)framesWritten); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 955 | if (mPresentationCompleteFrames == 0) { | 
|  | 956 | mPresentationCompleteFrames = framesWritten + audioHalFrames; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 957 | ALOGV("presentationComplete() reset: mPresentationCompleteFrames %lld audioHalFrames %zu", | 
|  | 958 | (long long)mPresentationCompleteFrames, audioHalFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 959 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 960 |  | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 961 | bool complete; | 
|  | 962 | if (isOffloaded()) { | 
|  | 963 | complete = true; | 
|  | 964 | } else if (isDirect() || isFastTrack()) { // these do not go through linear map | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 965 | complete = framesWritten >= (int64_t) mPresentationCompleteFrames; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 966 | } else {  // Normal tracks, OutputTracks, and PatchTracks | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 967 | complete = framesWritten >= (int64_t) mPresentationCompleteFrames | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 968 | && mAudioTrackServerProxy->isDrained(); | 
|  | 969 | } | 
|  | 970 |  | 
|  | 971 | if (complete) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 972 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 973 | mAudioTrackServerProxy->setStreamEndDone(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 974 | return true; | 
|  | 975 | } | 
|  | 976 | return false; | 
|  | 977 | } | 
|  | 978 |  | 
|  | 979 | void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) | 
|  | 980 | { | 
| Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 981 | for (size_t i = 0; i < mSyncEvents.size(); i++) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 982 | if (mSyncEvents[i]->type() == type) { | 
|  | 983 | mSyncEvents[i]->trigger(); | 
|  | 984 | mSyncEvents.removeAt(i); | 
|  | 985 | i--; | 
|  | 986 | } | 
|  | 987 | } | 
|  | 988 | } | 
|  | 989 |  | 
|  | 990 | // implement VolumeBufferProvider interface | 
|  | 991 |  | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 992 | gain_minifloat_packed_t AudioFlinger::PlaybackThread::Track::getVolumeLR() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 993 | { | 
|  | 994 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs | 
|  | 995 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 996 | gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); | 
|  | 997 | float vl = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
|  | 998 | float vr = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 999 | // track volumes come from shared memory, so can't be trusted and must be clamped | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1000 | if (vl > GAIN_FLOAT_UNITY) { | 
|  | 1001 | vl = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1002 | } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1003 | if (vr > GAIN_FLOAT_UNITY) { | 
|  | 1004 | vr = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1005 | } | 
|  | 1006 | // now apply the cached master volume and stream type volume; | 
|  | 1007 | // this is trusted but lacks any synchronization or barrier so may be stale | 
|  | 1008 | float v = mCachedVolume; | 
|  | 1009 | vl *= v; | 
|  | 1010 | vr *= v; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1011 | // re-combine into packed minifloat | 
|  | 1012 | vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1013 | // FIXME look at mute, pause, and stop flags | 
|  | 1014 | return vlr; | 
|  | 1015 | } | 
|  | 1016 |  | 
|  | 1017 | status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event) | 
|  | 1018 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1019 | if (isTerminated() || mState == PAUSED || | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1020 | ((framesReady() == 0) && ((mSharedBuffer != 0) || | 
|  | 1021 | (mState == STOPPED)))) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1022 | ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1023 | mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady()); | 
|  | 1024 | event->cancel(); | 
|  | 1025 | return INVALID_OPERATION; | 
|  | 1026 | } | 
|  | 1027 | (void) TrackBase::setSyncEvent(event); | 
|  | 1028 | return NO_ERROR; | 
|  | 1029 | } | 
|  | 1030 |  | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1031 | void AudioFlinger::PlaybackThread::Track::invalidate() | 
|  | 1032 | { | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1033 | signalClientFlag(CBLK_INVALID); | 
|  | 1034 | mIsInvalid = true; | 
|  | 1035 | } | 
|  | 1036 |  | 
|  | 1037 | void AudioFlinger::PlaybackThread::Track::disable() | 
|  | 1038 | { | 
|  | 1039 | signalClientFlag(CBLK_DISABLED); | 
|  | 1040 | } | 
|  | 1041 |  | 
|  | 1042 | void AudioFlinger::PlaybackThread::Track::signalClientFlag(int32_t flag) | 
|  | 1043 | { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1044 | // FIXME should use proxy, and needs work | 
|  | 1045 | audio_track_cblk_t* cblk = mCblk; | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1046 | android_atomic_or(flag, &cblk->mFlags); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1047 | android_atomic_release_store(0x40000000, &cblk->mFutex); | 
|  | 1048 | // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 1049 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1050 | } | 
|  | 1051 |  | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1052 | void AudioFlinger::PlaybackThread::Track::signal() | 
|  | 1053 | { | 
|  | 1054 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1055 | if (thread != 0) { | 
|  | 1056 | PlaybackThread *t = (PlaybackThread *)thread.get(); | 
|  | 1057 | Mutex::Autolock _l(t->mLock); | 
|  | 1058 | t->broadcast_l(); | 
|  | 1059 | } | 
|  | 1060 | } | 
|  | 1061 |  | 
| Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1062 | //To be called with thread lock held | 
|  | 1063 | bool AudioFlinger::PlaybackThread::Track::isResumePending() { | 
|  | 1064 |  | 
|  | 1065 | if (mState == RESUMING) | 
|  | 1066 | return true; | 
|  | 1067 | /* Resume is pending if track was stopping before pause was called */ | 
|  | 1068 | if (mState == STOPPING_1 && | 
|  | 1069 | mResumeToStopping) | 
|  | 1070 | return true; | 
|  | 1071 |  | 
|  | 1072 | return false; | 
|  | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | //To be called with thread lock held | 
|  | 1076 | void AudioFlinger::PlaybackThread::Track::resumeAck() { | 
|  | 1077 |  | 
|  | 1078 |  | 
|  | 1079 | if (mState == RESUMING) | 
|  | 1080 | mState = ACTIVE; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1081 |  | 
| Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1082 | // Other possibility of  pending resume is stopping_1 state | 
|  | 1083 | // Do not update the state from stopping as this prevents | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1084 | // drain being called. | 
|  | 1085 | if (mState == STOPPING_1) { | 
|  | 1086 | mResumeToStopping = false; | 
|  | 1087 | } | 
| Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1088 | } | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1089 |  | 
|  | 1090 | //To be called with thread lock held | 
|  | 1091 | void AudioFlinger::PlaybackThread::Track::updateTrackFrameInfo( | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1092 | int64_t trackFramesReleased, int64_t sinkFramesWritten, | 
|  | 1093 | const ExtendedTimestamp &timeStamp) { | 
|  | 1094 | //update frame map | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1095 | mFrameMap.push(trackFramesReleased, sinkFramesWritten); | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1096 |  | 
|  | 1097 | // adjust server times and set drained state. | 
|  | 1098 | // | 
|  | 1099 | // Our timestamps are only updated when the track is on the Thread active list. | 
|  | 1100 | // We need to ensure that tracks are not removed before full drain. | 
|  | 1101 | ExtendedTimestamp local = timeStamp; | 
|  | 1102 | bool checked = false; | 
|  | 1103 | for (int i = ExtendedTimestamp::LOCATION_MAX - 1; | 
|  | 1104 | i >= ExtendedTimestamp::LOCATION_SERVER; --i) { | 
|  | 1105 | // Lookup the track frame corresponding to the sink frame position. | 
|  | 1106 | if (local.mTimeNs[i] > 0) { | 
|  | 1107 | local.mPosition[i] = mFrameMap.findX(local.mPosition[i]); | 
|  | 1108 | // check drain state from the latest stage in the pipeline. | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 1109 | if (!checked && i <= ExtendedTimestamp::LOCATION_KERNEL) { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1110 | mAudioTrackServerProxy->setDrained( | 
|  | 1111 | local.mPosition[i] >= mAudioTrackServerProxy->framesReleased()); | 
|  | 1112 | checked = true; | 
|  | 1113 | } | 
|  | 1114 | } | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1115 | } | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1116 | if (!checked) { // no server info, assume drained. | 
|  | 1117 | mAudioTrackServerProxy->setDrained(true); | 
|  | 1118 | } | 
| Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 1119 | // Set correction for flushed frames that are not accounted for in released. | 
| Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 1120 | local.mFlushed = mAudioTrackServerProxy->framesFlushed(); | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1121 | mServerProxy->setTimestamp(local); | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1122 | } | 
|  | 1123 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1124 | // ---------------------------------------------------------------------------- | 
|  | 1125 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1126 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( | 
|  | 1127 | PlaybackThread *playbackThread, | 
|  | 1128 | DuplicatingThread *sourceThread, | 
|  | 1129 | uint32_t sampleRate, | 
|  | 1130 | audio_format_t format, | 
|  | 1131 | audio_channel_mask_t channelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1132 | size_t frameCount, | 
|  | 1133 | int uid) | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1134 | :   Track(playbackThread, NULL, AUDIO_STREAM_PATCH, | 
|  | 1135 | sampleRate, format, channelMask, frameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1136 | NULL, 0, AUDIO_SESSION_NONE, uid, IAudioFlinger::TRACK_DEFAULT, | 
|  | 1137 | TYPE_OUTPUT), | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1138 | mActive(false), mSourceThread(sourceThread), mClientProxy(NULL) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1139 | { | 
|  | 1140 |  | 
|  | 1141 | if (mCblk != NULL) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1142 | mOutBuffer.frameCount = 0; | 
|  | 1143 | playbackThread->mTracks.add(this); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1144 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, " | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1145 | "frameCount %zu, mChannelMask 0x%08x", | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1146 | mCblk, mBuffer, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1147 | frameCount, mChannelMask); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1148 | // since client and server are in the same process, | 
|  | 1149 | // the buffer has the same virtual address on both sides | 
| Glenn Kasten | 529c61b | 2014-07-18 15:31:02 -0700 | [diff] [blame] | 1150 | mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize, | 
|  | 1151 | true /*clientInServer*/); | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1152 | mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY); | 
| Eric Laurent | 8d2d493 | 2013-04-25 12:56:18 -0700 | [diff] [blame] | 1153 | mClientProxy->setSendLevel(0.0); | 
|  | 1154 | mClientProxy->setSampleRate(sampleRate); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1155 | } else { | 
|  | 1156 | ALOGW("Error creating output track on thread %p", playbackThread); | 
|  | 1157 | } | 
|  | 1158 | } | 
|  | 1159 |  | 
|  | 1160 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() | 
|  | 1161 | { | 
|  | 1162 | clearBufferQueue(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1163 | delete mClientProxy; | 
|  | 1164 | // superclass destructor will now delete the server proxy and shared memory both refer to | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1165 | } | 
|  | 1166 |  | 
|  | 1167 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1168 | audio_session_t triggerSession) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1169 | { | 
|  | 1170 | status_t status = Track::start(event, triggerSession); | 
|  | 1171 | if (status != NO_ERROR) { | 
|  | 1172 | return status; | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | mActive = true; | 
|  | 1176 | mRetryCount = 127; | 
|  | 1177 | return status; | 
|  | 1178 | } | 
|  | 1179 |  | 
|  | 1180 | void AudioFlinger::PlaybackThread::OutputTrack::stop() | 
|  | 1181 | { | 
|  | 1182 | Track::stop(); | 
|  | 1183 | clearBufferQueue(); | 
|  | 1184 | mOutBuffer.frameCount = 0; | 
|  | 1185 | mActive = false; | 
|  | 1186 | } | 
|  | 1187 |  | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1188 | bool AudioFlinger::PlaybackThread::OutputTrack::write(void* data, uint32_t frames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1189 | { | 
|  | 1190 | Buffer *pInBuffer; | 
|  | 1191 | Buffer inBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1192 | bool outputBufferFull = false; | 
|  | 1193 | inBuffer.frameCount = frames; | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1194 | inBuffer.raw = data; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1195 |  | 
|  | 1196 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); | 
|  | 1197 |  | 
|  | 1198 | if (!mActive && frames != 0) { | 
| Andy Hung | 5bedff6 | 2015-01-16 11:05:32 -0800 | [diff] [blame] | 1199 | (void) start(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1200 | } | 
|  | 1201 |  | 
|  | 1202 | while (waitTimeLeftMs) { | 
|  | 1203 | // First write pending buffers, then new data | 
|  | 1204 | if (mBufferQueue.size()) { | 
|  | 1205 | pInBuffer = mBufferQueue.itemAt(0); | 
|  | 1206 | } else { | 
|  | 1207 | pInBuffer = &inBuffer; | 
|  | 1208 | } | 
|  | 1209 |  | 
|  | 1210 | if (pInBuffer->frameCount == 0) { | 
|  | 1211 | break; | 
|  | 1212 | } | 
|  | 1213 |  | 
|  | 1214 | if (mOutBuffer.frameCount == 0) { | 
|  | 1215 | mOutBuffer.frameCount = pInBuffer->frameCount; | 
|  | 1216 | nsecs_t startTime = systemTime(); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1217 | status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs); | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1218 | if (status != NO_ERROR && status != NOT_ENOUGH_DATA) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1219 | ALOGV("OutputTrack::write() %p thread %p no more output buffers; status %d", this, | 
|  | 1220 | mThread.unsafe_get(), status); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1221 | outputBufferFull = true; | 
|  | 1222 | break; | 
|  | 1223 | } | 
|  | 1224 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); | 
|  | 1225 | if (waitTimeLeftMs >= waitTimeMs) { | 
|  | 1226 | waitTimeLeftMs -= waitTimeMs; | 
|  | 1227 | } else { | 
|  | 1228 | waitTimeLeftMs = 0; | 
|  | 1229 | } | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1230 | if (status == NOT_ENOUGH_DATA) { | 
|  | 1231 | restartIfDisabled(); | 
|  | 1232 | continue; | 
|  | 1233 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1234 | } | 
|  | 1235 |  | 
|  | 1236 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : | 
|  | 1237 | pInBuffer->frameCount; | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1238 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * mFrameSize); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1239 | Proxy::Buffer buf; | 
|  | 1240 | buf.mFrameCount = outFrames; | 
|  | 1241 | buf.mRaw = NULL; | 
|  | 1242 | mClientProxy->releaseBuffer(&buf); | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1243 | restartIfDisabled(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1244 | pInBuffer->frameCount -= outFrames; | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1245 | pInBuffer->raw = (int8_t *)pInBuffer->raw + outFrames * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1246 | mOutBuffer.frameCount -= outFrames; | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1247 | mOutBuffer.raw = (int8_t *)mOutBuffer.raw + outFrames * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1248 |  | 
|  | 1249 | if (pInBuffer->frameCount == 0) { | 
|  | 1250 | if (mBufferQueue.size()) { | 
|  | 1251 | mBufferQueue.removeAt(0); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1252 | free(pInBuffer->mBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1253 | delete pInBuffer; | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1254 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %zu", this, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1255 | mThread.unsafe_get(), mBufferQueue.size()); | 
|  | 1256 | } else { | 
|  | 1257 | break; | 
|  | 1258 | } | 
|  | 1259 | } | 
|  | 1260 | } | 
|  | 1261 |  | 
|  | 1262 | // If we could not write all frames, allocate a buffer and queue it for next time. | 
|  | 1263 | if (inBuffer.frameCount) { | 
|  | 1264 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1265 | if (thread != 0 && !thread->standby()) { | 
|  | 1266 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { | 
|  | 1267 | pInBuffer = new Buffer; | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1268 | pInBuffer->mBuffer = malloc(inBuffer.frameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1269 | pInBuffer->frameCount = inBuffer.frameCount; | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1270 | pInBuffer->raw = pInBuffer->mBuffer; | 
|  | 1271 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1272 | mBufferQueue.add(pInBuffer); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1273 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %zu", this, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1274 | mThread.unsafe_get(), mBufferQueue.size()); | 
|  | 1275 | } else { | 
|  | 1276 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", | 
|  | 1277 | mThread.unsafe_get(), this); | 
|  | 1278 | } | 
|  | 1279 | } | 
|  | 1280 | } | 
|  | 1281 |  | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1282 | // Calling write() with a 0 length buffer means that no more data will be written: | 
|  | 1283 | // We rely on stop() to set the appropriate flags to allow the remaining frames to play out. | 
|  | 1284 | if (frames == 0 && mBufferQueue.size() == 0 && mActive) { | 
|  | 1285 | stop(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | return outputBufferFull; | 
|  | 1289 | } | 
|  | 1290 |  | 
|  | 1291 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer( | 
|  | 1292 | AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) | 
|  | 1293 | { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1294 | ClientProxy::Buffer buf; | 
|  | 1295 | buf.mFrameCount = buffer->frameCount; | 
|  | 1296 | struct timespec timeout; | 
|  | 1297 | timeout.tv_sec = waitTimeMs / 1000; | 
|  | 1298 | timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000; | 
|  | 1299 | status_t status = mClientProxy->obtainBuffer(&buf, &timeout); | 
|  | 1300 | buffer->frameCount = buf.mFrameCount; | 
|  | 1301 | buffer->raw = buf.mRaw; | 
|  | 1302 | return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1303 | } | 
|  | 1304 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1305 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() | 
|  | 1306 | { | 
|  | 1307 | size_t size = mBufferQueue.size(); | 
|  | 1308 |  | 
|  | 1309 | for (size_t i = 0; i < size; i++) { | 
|  | 1310 | Buffer *pBuffer = mBufferQueue.itemAt(i); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1311 | free(pBuffer->mBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1312 | delete pBuffer; | 
|  | 1313 | } | 
|  | 1314 | mBufferQueue.clear(); | 
|  | 1315 | } | 
|  | 1316 |  | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1317 | void AudioFlinger::PlaybackThread::OutputTrack::restartIfDisabled() | 
|  | 1318 | { | 
|  | 1319 | int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); | 
|  | 1320 | if (mActive && (flags & CBLK_DISABLED)) { | 
|  | 1321 | start(); | 
|  | 1322 | } | 
|  | 1323 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1325 | AudioFlinger::PlaybackThread::PatchTrack::PatchTrack(PlaybackThread *playbackThread, | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 1326 | audio_stream_type_t streamType, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1327 | uint32_t sampleRate, | 
|  | 1328 | audio_channel_mask_t channelMask, | 
|  | 1329 | audio_format_t format, | 
|  | 1330 | size_t frameCount, | 
|  | 1331 | void *buffer, | 
|  | 1332 | IAudioFlinger::track_flags_t flags) | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 1333 | :   Track(playbackThread, NULL, streamType, | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1334 | sampleRate, format, channelMask, frameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1335 | buffer, 0, AUDIO_SESSION_NONE, getuid(), flags, TYPE_PATCH), | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1336 | mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true)) | 
|  | 1337 | { | 
|  | 1338 | uint64_t mixBufferNs = ((uint64_t)2 * playbackThread->frameCount() * 1000000000) / | 
|  | 1339 | playbackThread->sampleRate(); | 
|  | 1340 | mPeerTimeout.tv_sec = mixBufferNs / 1000000000; | 
|  | 1341 | mPeerTimeout.tv_nsec = (int) (mixBufferNs % 1000000000); | 
|  | 1342 |  | 
|  | 1343 | ALOGV("PatchTrack %p sampleRate %d mPeerTimeout %d.%03d sec", | 
|  | 1344 | this, sampleRate, | 
|  | 1345 | (int)mPeerTimeout.tv_sec, | 
|  | 1346 | (int)(mPeerTimeout.tv_nsec / 1000000)); | 
|  | 1347 | } | 
|  | 1348 |  | 
|  | 1349 | AudioFlinger::PlaybackThread::PatchTrack::~PatchTrack() | 
|  | 1350 | { | 
|  | 1351 | } | 
|  | 1352 |  | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1353 | status_t AudioFlinger::PlaybackThread::PatchTrack::start(AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1354 | audio_session_t triggerSession) | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1355 | { | 
|  | 1356 | status_t status = Track::start(event, triggerSession); | 
|  | 1357 | if (status != NO_ERROR) { | 
|  | 1358 | return status; | 
|  | 1359 | } | 
|  | 1360 | android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); | 
|  | 1361 | return status; | 
|  | 1362 | } | 
|  | 1363 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1364 | // AudioBufferProvider interface | 
|  | 1365 | status_t AudioFlinger::PlaybackThread::PatchTrack::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1366 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1367 | { | 
|  | 1368 | ALOG_ASSERT(mPeerProxy != 0, "PatchTrack::getNextBuffer() called without peer proxy"); | 
|  | 1369 | Proxy::Buffer buf; | 
|  | 1370 | buf.mFrameCount = buffer->frameCount; | 
|  | 1371 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); | 
|  | 1372 | ALOGV_IF(status != NO_ERROR, "PatchTrack() %p getNextBuffer status %d", this, status); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 1373 | buffer->frameCount = buf.mFrameCount; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1374 | if (buf.mFrameCount == 0) { | 
|  | 1375 | return WOULD_BLOCK; | 
|  | 1376 | } | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1377 | status = Track::getNextBuffer(buffer); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1378 | return status; | 
|  | 1379 | } | 
|  | 1380 |  | 
|  | 1381 | void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 1382 | { | 
|  | 1383 | ALOG_ASSERT(mPeerProxy != 0, "PatchTrack::releaseBuffer() called without peer proxy"); | 
|  | 1384 | Proxy::Buffer buf; | 
|  | 1385 | buf.mFrameCount = buffer->frameCount; | 
|  | 1386 | buf.mRaw = buffer->raw; | 
|  | 1387 | mPeerProxy->releaseBuffer(&buf); | 
|  | 1388 | TrackBase::releaseBuffer(buffer); | 
|  | 1389 | } | 
|  | 1390 |  | 
|  | 1391 | status_t AudioFlinger::PlaybackThread::PatchTrack::obtainBuffer(Proxy::Buffer* buffer, | 
|  | 1392 | const struct timespec *timeOut) | 
|  | 1393 | { | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1394 | status_t status = NO_ERROR; | 
|  | 1395 | static const int32_t kMaxTries = 5; | 
|  | 1396 | int32_t tryCounter = kMaxTries; | 
|  | 1397 | do { | 
|  | 1398 | if (status == NOT_ENOUGH_DATA) { | 
|  | 1399 | restartIfDisabled(); | 
|  | 1400 | } | 
|  | 1401 | status = mProxy->obtainBuffer(buffer, timeOut); | 
|  | 1402 | } while ((status == NOT_ENOUGH_DATA) && (tryCounter-- > 0)); | 
|  | 1403 | return status; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1404 | } | 
|  | 1405 |  | 
|  | 1406 | void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(Proxy::Buffer* buffer) | 
|  | 1407 | { | 
|  | 1408 | mProxy->releaseBuffer(buffer); | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1409 | restartIfDisabled(); | 
|  | 1410 | android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags); | 
|  | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | void AudioFlinger::PlaybackThread::PatchTrack::restartIfDisabled() | 
|  | 1414 | { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1415 | if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) { | 
|  | 1416 | ALOGW("PatchTrack::releaseBuffer() disabled due to previous underrun, restarting"); | 
|  | 1417 | start(); | 
|  | 1418 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1419 | } | 
|  | 1420 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1421 | // ---------------------------------------------------------------------------- | 
|  | 1422 | //      Record | 
|  | 1423 | // ---------------------------------------------------------------------------- | 
|  | 1424 |  | 
|  | 1425 | AudioFlinger::RecordHandle::RecordHandle( | 
|  | 1426 | const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) | 
|  | 1427 | : BnAudioRecord(), | 
|  | 1428 | mRecordTrack(recordTrack) | 
|  | 1429 | { | 
|  | 1430 | } | 
|  | 1431 |  | 
|  | 1432 | AudioFlinger::RecordHandle::~RecordHandle() { | 
|  | 1433 | stop_nonvirtual(); | 
|  | 1434 | mRecordTrack->destroy(); | 
|  | 1435 | } | 
|  | 1436 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1437 | status_t AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1438 | audio_session_t triggerSession) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1439 | ALOGV("RecordHandle::start()"); | 
|  | 1440 | return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession); | 
|  | 1441 | } | 
|  | 1442 |  | 
|  | 1443 | void AudioFlinger::RecordHandle::stop() { | 
|  | 1444 | stop_nonvirtual(); | 
|  | 1445 | } | 
|  | 1446 |  | 
|  | 1447 | void AudioFlinger::RecordHandle::stop_nonvirtual() { | 
|  | 1448 | ALOGV("RecordHandle::stop()"); | 
|  | 1449 | mRecordTrack->stop(); | 
|  | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | status_t AudioFlinger::RecordHandle::onTransact( | 
|  | 1453 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 1454 | { | 
|  | 1455 | return BnAudioRecord::onTransact(code, data, reply, flags); | 
|  | 1456 | } | 
|  | 1457 |  | 
|  | 1458 | // ---------------------------------------------------------------------------- | 
|  | 1459 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 1460 | // RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( | 
|  | 1462 | RecordThread *thread, | 
|  | 1463 | const sp<Client>& client, | 
|  | 1464 | uint32_t sampleRate, | 
|  | 1465 | audio_format_t format, | 
|  | 1466 | audio_channel_mask_t channelMask, | 
|  | 1467 | size_t frameCount, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1468 | void *buffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1469 | audio_session_t sessionId, | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1470 | int uid, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1471 | IAudioFlinger::track_flags_t flags, | 
|  | 1472 | track_type type) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1473 | :   TrackBase(thread, client, sampleRate, format, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1474 | channelMask, frameCount, buffer, sessionId, uid, | 
| Glenn Kasten | 755b0a6 | 2014-05-13 11:30:28 -0700 | [diff] [blame] | 1475 | flags, false /*isOut*/, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1476 | (type == TYPE_DEFAULT) ? | 
|  | 1477 | ((flags & IAudioFlinger::TRACK_FAST) ? ALLOC_PIPE : ALLOC_CBLK) : | 
|  | 1478 | ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE), | 
|  | 1479 | type), | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 1480 | mOverflow(false), | 
| Andy Hung | 4c6afaf | 2015-06-12 18:23:35 -0700 | [diff] [blame] | 1481 | mFramesToDrop(0), | 
|  | 1482 | mResamplerBufferProvider(NULL), // initialize in case of early constructor exit | 
|  | 1483 | mRecordBufferConverter(NULL) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1484 | { | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 1485 | if (mCblk == NULL) { | 
|  | 1486 | return; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1487 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 1488 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 1489 | mRecordBufferConverter = new RecordBufferConverter( | 
|  | 1490 | thread->mChannelMask, thread->mFormat, thread->mSampleRate, | 
|  | 1491 | channelMask, format, sampleRate); | 
|  | 1492 | // Check if the RecordBufferConverter construction was successful. | 
|  | 1493 | // If not, don't continue with construction. | 
|  | 1494 | // | 
|  | 1495 | // NOTE: It would be extremely rare that the record track cannot be created | 
|  | 1496 | // for the current device, but a pending or future device change would make | 
|  | 1497 | // the record track configuration valid. | 
|  | 1498 | if (mRecordBufferConverter->initCheck() != NO_ERROR) { | 
|  | 1499 | ALOGE("RecordTrack unable to create record buffer converter"); | 
|  | 1500 | return; | 
|  | 1501 | } | 
|  | 1502 |  | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 1503 | mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount, | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1504 | mFrameSize, !isExternalTrack()); | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1505 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 1506 | mResamplerBufferProvider = new ResamplerBufferProvider(this); | 
| Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 1507 |  | 
|  | 1508 | if (flags & IAudioFlinger::TRACK_FAST) { | 
|  | 1509 | ALOG_ASSERT(thread->mFastTrackAvail); | 
|  | 1510 | thread->mFastTrackAvail = false; | 
|  | 1511 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1512 | } | 
|  | 1513 |  | 
|  | 1514 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() | 
|  | 1515 | { | 
|  | 1516 | ALOGV("%s", __func__); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 1517 | delete mRecordBufferConverter; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 1518 | delete mResamplerBufferProvider; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1519 | } | 
|  | 1520 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 1521 | status_t AudioFlinger::RecordThread::RecordTrack::initCheck() const | 
|  | 1522 | { | 
|  | 1523 | status_t status = TrackBase::initCheck(); | 
|  | 1524 | if (status == NO_ERROR && mServerProxy == 0) { | 
|  | 1525 | status = BAD_VALUE; | 
|  | 1526 | } | 
|  | 1527 | return status; | 
|  | 1528 | } | 
|  | 1529 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1530 | // AudioBufferProvider interface | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1531 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1532 | { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1533 | ServerProxy::Buffer buf; | 
|  | 1534 | buf.mFrameCount = buffer->frameCount; | 
|  | 1535 | status_t status = mServerProxy->obtainBuffer(&buf); | 
|  | 1536 | buffer->frameCount = buf.mFrameCount; | 
|  | 1537 | buffer->raw = buf.mRaw; | 
|  | 1538 | if (buf.mFrameCount == 0) { | 
|  | 1539 | // FIXME also wake futex so that overrun is noticed more quickly | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1540 | (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1541 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1542 | return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1543 | } | 
|  | 1544 |  | 
|  | 1545 | status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1546 | audio_session_t triggerSession) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1547 | { | 
|  | 1548 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1549 | if (thread != 0) { | 
|  | 1550 | RecordThread *recordThread = (RecordThread *)thread.get(); | 
|  | 1551 | return recordThread->start(this, event, triggerSession); | 
|  | 1552 | } else { | 
|  | 1553 | return BAD_VALUE; | 
|  | 1554 | } | 
|  | 1555 | } | 
|  | 1556 |  | 
|  | 1557 | void AudioFlinger::RecordThread::RecordTrack::stop() | 
|  | 1558 | { | 
|  | 1559 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1560 | if (thread != 0) { | 
|  | 1561 | RecordThread *recordThread = (RecordThread *)thread.get(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1562 | if (recordThread->stop(this) && isExternalTrack()) { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1563 | AudioSystem::stopInput(mThreadIoHandle, mSessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1564 | } | 
|  | 1565 | } | 
|  | 1566 | } | 
|  | 1567 |  | 
|  | 1568 | void AudioFlinger::RecordThread::RecordTrack::destroy() | 
|  | 1569 | { | 
|  | 1570 | // see comments at AudioFlinger::PlaybackThread::Track::destroy() | 
|  | 1571 | sp<RecordTrack> keep(this); | 
|  | 1572 | { | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 1573 | if (isExternalTrack()) { | 
|  | 1574 | if (mState == ACTIVE || mState == RESUMING) { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1575 | AudioSystem::stopInput(mThreadIoHandle, mSessionId); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 1576 | } | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1577 | AudioSystem::releaseInput(mThreadIoHandle, mSessionId); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 1578 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1579 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1580 | if (thread != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1581 | Mutex::Autolock _l(thread->mLock); | 
|  | 1582 | RecordThread *recordThread = (RecordThread *) thread.get(); | 
|  | 1583 | recordThread->destroyTrack_l(this); | 
|  | 1584 | } | 
|  | 1585 | } | 
|  | 1586 | } | 
|  | 1587 |  | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 1588 | void AudioFlinger::RecordThread::RecordTrack::invalidate() | 
|  | 1589 | { | 
|  | 1590 | // FIXME should use proxy, and needs work | 
|  | 1591 | audio_track_cblk_t* cblk = mCblk; | 
|  | 1592 | android_atomic_or(CBLK_INVALID, &cblk->mFlags); | 
|  | 1593 | android_atomic_release_store(0x40000000, &cblk->mFutex); | 
|  | 1594 | // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 1595 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 1596 | } | 
|  | 1597 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1598 |  | 
|  | 1599 | /*static*/ void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result) | 
|  | 1600 | { | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 1601 | result.append("    Active Client Fmt Chn mask Session S   Server fCount SRate\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1602 | } | 
|  | 1603 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1604 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size, bool active) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1605 | { | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 1606 | snprintf(buffer, size, "    %6s %6u %3u %08X %7u %1d %08X %6zu %5u\n", | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1607 | active ? "yes" : "no", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1608 | (mClient == 0) ? getpid_cached : mClient->pid(), | 
|  | 1609 | mFormat, | 
|  | 1610 | mChannelMask, | 
|  | 1611 | mSessionId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1612 | mState, | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 1613 | mCblk->mServer, | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 1614 | mFrameCount, | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 1615 | mSampleRate); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 1616 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1617 | } | 
|  | 1618 |  | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 1619 | void AudioFlinger::RecordThread::RecordTrack::handleSyncStartEvent(const sp<SyncEvent>& event) | 
|  | 1620 | { | 
|  | 1621 | if (event == mSyncStartEvent) { | 
|  | 1622 | ssize_t framesToDrop = 0; | 
|  | 1623 | sp<ThreadBase> threadBase = mThread.promote(); | 
|  | 1624 | if (threadBase != 0) { | 
|  | 1625 | // TODO: use actual buffer filling status instead of 2 buffers when info is available | 
|  | 1626 | // from audio HAL | 
|  | 1627 | framesToDrop = threadBase->mFrameCount * 2; | 
|  | 1628 | } | 
|  | 1629 | mFramesToDrop = framesToDrop; | 
|  | 1630 | } | 
|  | 1631 | } | 
|  | 1632 |  | 
|  | 1633 | void AudioFlinger::RecordThread::RecordTrack::clearSyncStartEvent() | 
|  | 1634 | { | 
|  | 1635 | if (mSyncStartEvent != 0) { | 
|  | 1636 | mSyncStartEvent->cancel(); | 
|  | 1637 | mSyncStartEvent.clear(); | 
|  | 1638 | } | 
|  | 1639 | mFramesToDrop = 0; | 
|  | 1640 | } | 
|  | 1641 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1642 | void AudioFlinger::RecordThread::RecordTrack::updateTrackFrameInfo( | 
|  | 1643 | int64_t trackFramesReleased, int64_t sourceFramesRead, | 
|  | 1644 | uint32_t halSampleRate, const ExtendedTimestamp ×tamp) | 
|  | 1645 | { | 
|  | 1646 | ExtendedTimestamp local = timestamp; | 
|  | 1647 |  | 
|  | 1648 | // Convert HAL frames to server-side track frames at track sample rate. | 
|  | 1649 | // We use trackFramesReleased and sourceFramesRead as an anchor point. | 
|  | 1650 | for (int i = ExtendedTimestamp::LOCATION_SERVER; i < ExtendedTimestamp::LOCATION_MAX; ++i) { | 
|  | 1651 | if (local.mTimeNs[i] != 0) { | 
|  | 1652 | const int64_t relativeServerFrames = local.mPosition[i] - sourceFramesRead; | 
|  | 1653 | const int64_t relativeTrackFrames = relativeServerFrames | 
|  | 1654 | * mSampleRate / halSampleRate; // TODO: potential computation overflow | 
|  | 1655 | local.mPosition[i] = relativeTrackFrames + trackFramesReleased; | 
|  | 1656 | } | 
|  | 1657 | } | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 1658 | mServerProxy->setTimestamp(local); | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1659 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1660 |  | 
|  | 1661 | AudioFlinger::RecordThread::PatchRecord::PatchRecord(RecordThread *recordThread, | 
|  | 1662 | uint32_t sampleRate, | 
|  | 1663 | audio_channel_mask_t channelMask, | 
|  | 1664 | audio_format_t format, | 
|  | 1665 | size_t frameCount, | 
|  | 1666 | void *buffer, | 
|  | 1667 | IAudioFlinger::track_flags_t flags) | 
|  | 1668 | :   RecordTrack(recordThread, NULL, sampleRate, format, channelMask, frameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1669 | buffer, AUDIO_SESSION_NONE, getuid(), flags, TYPE_PATCH), | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1670 | mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true)) | 
|  | 1671 | { | 
|  | 1672 | uint64_t mixBufferNs = ((uint64_t)2 * recordThread->frameCount() * 1000000000) / | 
|  | 1673 | recordThread->sampleRate(); | 
|  | 1674 | mPeerTimeout.tv_sec = mixBufferNs / 1000000000; | 
|  | 1675 | mPeerTimeout.tv_nsec = (int) (mixBufferNs % 1000000000); | 
|  | 1676 |  | 
|  | 1677 | ALOGV("PatchRecord %p sampleRate %d mPeerTimeout %d.%03d sec", | 
|  | 1678 | this, sampleRate, | 
|  | 1679 | (int)mPeerTimeout.tv_sec, | 
|  | 1680 | (int)(mPeerTimeout.tv_nsec / 1000000)); | 
|  | 1681 | } | 
|  | 1682 |  | 
|  | 1683 | AudioFlinger::RecordThread::PatchRecord::~PatchRecord() | 
|  | 1684 | { | 
|  | 1685 | } | 
|  | 1686 |  | 
|  | 1687 | // AudioBufferProvider interface | 
|  | 1688 | status_t AudioFlinger::RecordThread::PatchRecord::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1689 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1690 | { | 
|  | 1691 | ALOG_ASSERT(mPeerProxy != 0, "PatchRecord::getNextBuffer() called without peer proxy"); | 
|  | 1692 | Proxy::Buffer buf; | 
|  | 1693 | buf.mFrameCount = buffer->frameCount; | 
|  | 1694 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); | 
|  | 1695 | ALOGV_IF(status != NO_ERROR, | 
|  | 1696 | "PatchRecord() %p mPeerProxy->obtainBuffer status %d", this, status); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 1697 | buffer->frameCount = buf.mFrameCount; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1698 | if (buf.mFrameCount == 0) { | 
|  | 1699 | return WOULD_BLOCK; | 
|  | 1700 | } | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1701 | status = RecordTrack::getNextBuffer(buffer); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1702 | return status; | 
|  | 1703 | } | 
|  | 1704 |  | 
|  | 1705 | void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 1706 | { | 
|  | 1707 | ALOG_ASSERT(mPeerProxy != 0, "PatchRecord::releaseBuffer() called without peer proxy"); | 
|  | 1708 | Proxy::Buffer buf; | 
|  | 1709 | buf.mFrameCount = buffer->frameCount; | 
|  | 1710 | buf.mRaw = buffer->raw; | 
|  | 1711 | mPeerProxy->releaseBuffer(&buf); | 
|  | 1712 | TrackBase::releaseBuffer(buffer); | 
|  | 1713 | } | 
|  | 1714 |  | 
|  | 1715 | status_t AudioFlinger::RecordThread::PatchRecord::obtainBuffer(Proxy::Buffer* buffer, | 
|  | 1716 | const struct timespec *timeOut) | 
|  | 1717 | { | 
|  | 1718 | return mProxy->obtainBuffer(buffer, timeOut); | 
|  | 1719 | } | 
|  | 1720 |  | 
|  | 1721 | void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(Proxy::Buffer* buffer) | 
|  | 1722 | { | 
|  | 1723 | mProxy->releaseBuffer(buffer); | 
|  | 1724 | } | 
|  | 1725 |  | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 1726 | } // namespace android |