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