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