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" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 23 | #include <math.h> |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 24 | #include <sys/syscall.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <private/media/AudioTrackShared.h> |
| 28 | |
| 29 | #include <common_time/cc_helper.h> |
| 30 | #include <common_time/local_clock.h> |
| 31 | |
| 32 | #include "AudioMixer.h" |
| 33 | #include "AudioFlinger.h" |
| 34 | #include "ServiceUtilities.h" |
| 35 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 36 | #include <media/nbaio/Pipe.h> |
| 37 | #include <media/nbaio/PipeReader.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 38 | #include <audio_utils/minifloat.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 39 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | // Note: the following macro is used for extremely verbose logging message. In |
| 43 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 44 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 45 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 46 | // turned on. Do not uncomment the #def below unless you really know what you |
| 47 | // are doing and want to see all of the extremely verbose messages. |
| 48 | //#define VERY_VERY_VERBOSE_LOGGING |
| 49 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 50 | #define ALOGVV ALOGV |
| 51 | #else |
| 52 | #define ALOGVV(a...) do { } while(0) |
| 53 | #endif |
| 54 | |
| 55 | namespace android { |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // TrackBase |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 61 | static volatile int32_t nextTrackId = 55; |
| 62 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 63 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 64 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 65 | ThreadBase *thread, |
| 66 | const sp<Client>& client, |
| 67 | uint32_t sampleRate, |
| 68 | audio_format_t format, |
| 69 | audio_channel_mask_t channelMask, |
| 70 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 71 | void *buffer, |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 72 | int sessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 73 | int clientUid, |
Glenn Kasten | 755b0a6 | 2014-05-13 11:30:28 -0700 | [diff] [blame] | 74 | IAudioFlinger::track_flags_t flags, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 75 | bool isOut, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 76 | alloc_type alloc, |
| 77 | track_type type) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 78 | : RefBase(), |
| 79 | mThread(thread), |
| 80 | mClient(client), |
| 81 | mCblk(NULL), |
| 82 | // mBuffer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 83 | mState(IDLE), |
| 84 | mSampleRate(sampleRate), |
| 85 | mFormat(format), |
| 86 | mChannelMask(channelMask), |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 87 | mChannelCount(isOut ? |
| 88 | audio_channel_count_from_out_mask(channelMask) : |
| 89 | audio_channel_count_from_in_mask(channelMask)), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 90 | mFrameSize(audio_is_linear_pcm(format) ? |
| 91 | mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)), |
| 92 | mFrameCount(frameCount), |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 93 | mSessionId(sessionId), |
Glenn Kasten | 755b0a6 | 2014-05-13 11:30:28 -0700 | [diff] [blame] | 94 | mFlags(flags), |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 95 | mIsOut(isOut), |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 96 | mServerProxy(NULL), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 97 | mId(android_atomic_inc(&nextTrackId)), |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 98 | mTerminated(false), |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 99 | mType(type), |
| 100 | mThreadIoHandle(thread->id()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 101 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 102 | // if the caller is us, trust the specified uid |
| 103 | if (IPCThreadState::self()->getCallingPid() != getpid_cached || clientUid == -1) { |
| 104 | int newclientUid = IPCThreadState::self()->getCallingUid(); |
| 105 | if (clientUid != -1 && clientUid != newclientUid) { |
| 106 | ALOGW("uid %d tried to pass itself off as %d", newclientUid, clientUid); |
| 107 | } |
| 108 | clientUid = newclientUid; |
| 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 | eaa3969 | 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 | eaa3969 | 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) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 140 | ALOGE("not enough memory for AudioTrack size=%u", size); |
| 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 | 1159ffd | 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 |
| 240 | delete mServerProxy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 241 | if (mCblk != NULL) { |
Andy Hung | 1159ffd | 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 | 1159ffd | 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; |
| 262 | // This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack |
| 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 | |
| 328 | status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size, |
| 329 | sp<IMemory>* buffer) { |
| 330 | if (!mTrack->isTimedTrack()) |
| 331 | return INVALID_OPERATION; |
| 332 | |
| 333 | PlaybackThread::TimedTrack* tt = |
| 334 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 335 | return tt->allocateTimedBuffer(size, buffer); |
| 336 | } |
| 337 | |
| 338 | status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer, |
| 339 | int64_t pts) { |
| 340 | if (!mTrack->isTimedTrack()) |
| 341 | return INVALID_OPERATION; |
| 342 | |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 343 | if (buffer == 0 || buffer->pointer() == NULL) { |
| 344 | ALOGE("queueTimedBuffer() buffer is 0 or has NULL pointer()"); |
| 345 | return BAD_VALUE; |
| 346 | } |
| 347 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 348 | PlaybackThread::TimedTrack* tt = |
| 349 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 350 | return tt->queueTimedBuffer(buffer, pts); |
| 351 | } |
| 352 | |
| 353 | status_t AudioFlinger::TrackHandle::setMediaTimeTransform( |
| 354 | const LinearTransform& xform, int target) { |
| 355 | |
| 356 | if (!mTrack->isTimedTrack()) |
| 357 | return INVALID_OPERATION; |
| 358 | |
| 359 | PlaybackThread::TimedTrack* tt = |
| 360 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 361 | return tt->setMediaTimeTransform( |
| 362 | xform, static_cast<TimedAudioTrack::TargetTimeline>(target)); |
| 363 | } |
| 364 | |
Glenn Kasten | 3dcd00d | 2013-07-17 10:10:23 -0700 | [diff] [blame] | 365 | status_t AudioFlinger::TrackHandle::setParameters(const String8& keyValuePairs) { |
| 366 | return mTrack->setParameters(keyValuePairs); |
| 367 | } |
| 368 | |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 369 | status_t AudioFlinger::TrackHandle::getTimestamp(AudioTimestamp& timestamp) |
| 370 | { |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 371 | return mTrack->getTimestamp(timestamp); |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 374 | |
| 375 | void AudioFlinger::TrackHandle::signal() |
| 376 | { |
| 377 | return mTrack->signal(); |
| 378 | } |
| 379 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 380 | status_t AudioFlinger::TrackHandle::onTransact( |
| 381 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 382 | { |
| 383 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 384 | } |
| 385 | |
| 386 | // ---------------------------------------------------------------------------- |
| 387 | |
| 388 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 389 | AudioFlinger::PlaybackThread::Track::Track( |
| 390 | PlaybackThread *thread, |
| 391 | const sp<Client>& client, |
| 392 | audio_stream_type_t streamType, |
| 393 | uint32_t sampleRate, |
| 394 | audio_format_t format, |
| 395 | audio_channel_mask_t channelMask, |
| 396 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 397 | void *buffer, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 398 | const sp<IMemory>& sharedBuffer, |
| 399 | int sessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 400 | int uid, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 401 | IAudioFlinger::track_flags_t flags, |
| 402 | track_type type) |
| 403 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, |
| 404 | (sharedBuffer != 0) ? sharedBuffer->pointer() : buffer, |
| 405 | sessionId, uid, flags, true /*isOut*/, |
| 406 | (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK, |
| 407 | type), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 408 | mFillingUpStatus(FS_INVALID), |
| 409 | // mRetryCount initialized later when needed |
| 410 | mSharedBuffer(sharedBuffer), |
| 411 | mStreamType(streamType), |
| 412 | mName(-1), // see note below |
| 413 | mMainBuffer(thread->mixBuffer()), |
| 414 | mAuxBuffer(NULL), |
| 415 | mAuxEffectId(0), mHasVolumeController(false), |
| 416 | mPresentationCompleteFrames(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 417 | mFastIndex(-1), |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 418 | mCachedVolume(1.0), |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 419 | mIsInvalid(false), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 420 | mAudioTrackServerProxy(NULL), |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 421 | mResumeToStopping(false), |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 422 | mFlushHwPending(false), |
| 423 | mPreviousValid(false), |
| 424 | mPreviousFramesWritten(0) |
| 425 | // mPreviousTimestamp |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 426 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 427 | // client == 0 implies sharedBuffer == 0 |
| 428 | ALOG_ASSERT(!(client == 0 && sharedBuffer != 0)); |
| 429 | |
| 430 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), |
| 431 | sharedBuffer->size()); |
| 432 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 433 | if (mCblk == NULL) { |
| 434 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 435 | } |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 436 | |
| 437 | if (sharedBuffer == 0) { |
| 438 | mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 439 | mFrameSize, !isExternalTrack(), sampleRate); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 440 | } else { |
Andy Hung | f4aeab2 | 2017-06-12 17:22:46 -0700 | [diff] [blame] | 441 | // Is the shared buffer of sufficient size? |
| 442 | // (frameCount * mFrameSize) is <= SIZE_MAX, checked in TrackBase. |
| 443 | if (sharedBuffer->size() < frameCount * mFrameSize) { |
| 444 | // Workaround: clear out mCblk to indicate track hasn't been properly created. |
| 445 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 446 | if (mClient == 0) { |
| 447 | free(mCblk); |
| 448 | } |
| 449 | mCblk = NULL; |
| 450 | |
| 451 | mSharedBuffer.clear(); // release shared buffer early |
| 452 | android_errorWriteLog(0x534e4554, "38340117"); |
| 453 | return; |
| 454 | } |
| 455 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 456 | mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount, |
| 457 | mFrameSize); |
| 458 | } |
| 459 | mServerProxy = mAudioTrackServerProxy; |
| 460 | |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 461 | mName = thread->getTrackName_l(channelMask, format, sessionId); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 462 | if (mName < 0) { |
| 463 | ALOGE("no more track names available"); |
| 464 | return; |
| 465 | } |
| 466 | // only allocate a fast track index if we were able to allocate a normal track name |
| 467 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 468 | mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads(); |
| 469 | ALOG_ASSERT(thread->mFastTrackAvailMask != 0); |
| 470 | int i = __builtin_ctz(thread->mFastTrackAvailMask); |
| 471 | ALOG_ASSERT(0 < i && i < (int)FastMixerState::kMaxFastTracks); |
| 472 | // FIXME This is too eager. We allocate a fast track index before the |
| 473 | // fast track becomes active. Since fast tracks are a scarce resource, |
| 474 | // this means we are potentially denying other more important fast tracks from |
| 475 | // being created. It would be better to allocate the index dynamically. |
| 476 | mFastIndex = i; |
| 477 | // Read the initial underruns because this field is never cleared by the fast mixer |
| 478 | mObservedUnderruns = thread->getFastTrackUnderruns(i); |
| 479 | thread->mFastTrackAvailMask &= ~(1 << i); |
| 480 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | AudioFlinger::PlaybackThread::Track::~Track() |
| 484 | { |
| 485 | ALOGV("PlaybackThread::Track destructor"); |
Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 486 | |
| 487 | // The destructor would clear mSharedBuffer, |
| 488 | // but it will not push the decremented reference count, |
| 489 | // leaving the client's IMemory dangling indefinitely. |
| 490 | // This prevents that leak. |
| 491 | if (mSharedBuffer != 0) { |
| 492 | mSharedBuffer.clear(); |
Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 493 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 496 | status_t AudioFlinger::PlaybackThread::Track::initCheck() const |
| 497 | { |
| 498 | status_t status = TrackBase::initCheck(); |
| 499 | if (status == NO_ERROR && mName < 0) { |
| 500 | status = NO_MEMORY; |
| 501 | } |
| 502 | return status; |
| 503 | } |
| 504 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 505 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 506 | { |
| 507 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 508 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 509 | // destructor is called. As the destructor needs to lock mLock, |
| 510 | // we must acquire a strong reference on this Track before locking mLock |
| 511 | // here so that the destructor is called only when exiting this function. |
| 512 | // On the other hand, as long as Track::destroy() is only called by |
| 513 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 514 | // this Track with its member mTrack. |
| 515 | sp<Track> keep(this); |
| 516 | { // scope for mLock |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 517 | bool wasActive = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 518 | sp<ThreadBase> thread = mThread.promote(); |
| 519 | if (thread != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 520 | Mutex::Autolock _l(thread->mLock); |
| 521 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 522 | wasActive = playbackThread->destroyTrack_l(this); |
| 523 | } |
| 524 | if (isExternalTrack() && !wasActive) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 525 | AudioSystem::releaseOutput(mThreadIoHandle, mStreamType, (audio_session_t)mSessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) |
| 531 | { |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 532 | result.append(" Name Active Client Type Fmt Chn mask Session fCount S F SRate " |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 533 | "L dB R dB Server Main buf Aux Buf Flags UndFrmCnt\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 536 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size, bool active) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 537 | { |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 538 | gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 539 | if (isFastTrack()) { |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 540 | sprintf(buffer, " F %2d", mFastIndex); |
| 541 | } else if (mName >= AudioMixer::TRACK0) { |
| 542 | sprintf(buffer, " %4d", mName - AudioMixer::TRACK0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 543 | } else { |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 544 | sprintf(buffer, " none"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 545 | } |
| 546 | track_state state = mState; |
| 547 | char stateChar; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 548 | if (isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 549 | stateChar = 'T'; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 550 | } else { |
| 551 | switch (state) { |
| 552 | case IDLE: |
| 553 | stateChar = 'I'; |
| 554 | break; |
| 555 | case STOPPING_1: |
| 556 | stateChar = 's'; |
| 557 | break; |
| 558 | case STOPPING_2: |
| 559 | stateChar = '5'; |
| 560 | break; |
| 561 | case STOPPED: |
| 562 | stateChar = 'S'; |
| 563 | break; |
| 564 | case RESUMING: |
| 565 | stateChar = 'R'; |
| 566 | break; |
| 567 | case ACTIVE: |
| 568 | stateChar = 'A'; |
| 569 | break; |
| 570 | case PAUSING: |
| 571 | stateChar = 'p'; |
| 572 | break; |
| 573 | case PAUSED: |
| 574 | stateChar = 'P'; |
| 575 | break; |
| 576 | case FLUSHED: |
| 577 | stateChar = 'F'; |
| 578 | break; |
| 579 | default: |
| 580 | stateChar = '?'; |
| 581 | break; |
| 582 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 583 | } |
| 584 | char nowInUnderrun; |
| 585 | switch (mObservedUnderruns.mBitFields.mMostRecent) { |
| 586 | case UNDERRUN_FULL: |
| 587 | nowInUnderrun = ' '; |
| 588 | break; |
| 589 | case UNDERRUN_PARTIAL: |
| 590 | nowInUnderrun = '<'; |
| 591 | break; |
| 592 | case UNDERRUN_EMPTY: |
| 593 | nowInUnderrun = '*'; |
| 594 | break; |
| 595 | default: |
| 596 | nowInUnderrun = '?'; |
| 597 | break; |
| 598 | } |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 599 | snprintf(&buffer[8], size-8, " %6s %6u %4u %08X %08X %7u %6zu %1c %1d %5u %5.2g %5.2g " |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 600 | "%08X %p %p 0x%03X %9u%c\n", |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 601 | active ? "yes" : "no", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 602 | (mClient == 0) ? getpid_cached : mClient->pid(), |
| 603 | mStreamType, |
| 604 | mFormat, |
| 605 | mChannelMask, |
| 606 | mSessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 607 | mFrameCount, |
| 608 | stateChar, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 609 | mFillingUpStatus, |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 610 | mAudioTrackServerProxy->getSampleRate(), |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 611 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))), |
| 612 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))), |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 613 | mCblk->mServer, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 614 | mMainBuffer, |
| 615 | mAuxBuffer, |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 616 | mCblk->mFlags, |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 617 | mAudioTrackServerProxy->getUnderrunFrames(), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 618 | nowInUnderrun); |
| 619 | } |
| 620 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 621 | uint32_t AudioFlinger::PlaybackThread::Track::sampleRate() const { |
| 622 | return mAudioTrackServerProxy->getSampleRate(); |
| 623 | } |
| 624 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 625 | // AudioBufferProvider interface |
| 626 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 627 | AudioBufferProvider::Buffer* buffer, int64_t pts __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 628 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 629 | ServerProxy::Buffer buf; |
| 630 | size_t desiredFrames = buffer->frameCount; |
| 631 | buf.mFrameCount = desiredFrames; |
| 632 | status_t status = mServerProxy->obtainBuffer(&buf); |
| 633 | buffer->frameCount = buf.mFrameCount; |
| 634 | buffer->raw = buf.mRaw; |
| 635 | if (buf.mFrameCount == 0) { |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 636 | mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 637 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 638 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 641 | // releaseBuffer() is not overridden |
| 642 | |
| 643 | // ExtendedAudioBufferProvider interface |
| 644 | |
Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 645 | // framesReady() may return an approximation of the number of frames if called |
| 646 | // from a different thread than the one calling Proxy->obtainBuffer() and |
| 647 | // Proxy->releaseBuffer(). Also note there is no mutual exclusion in the |
| 648 | // AudioTrackServerProxy so be especially careful calling with FastTracks. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 649 | size_t AudioFlinger::PlaybackThread::Track::framesReady() const { |
Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 650 | if (mSharedBuffer != 0 && (isStopped() || isStopping())) { |
| 651 | // Static tracks return zero frames immediately upon stopping (for FastTracks). |
| 652 | // The remainder of the buffer is not drained. |
| 653 | return 0; |
| 654 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 655 | return mAudioTrackServerProxy->framesReady(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 658 | size_t AudioFlinger::PlaybackThread::Track::framesReleased() const |
| 659 | { |
| 660 | return mAudioTrackServerProxy->framesReleased(); |
| 661 | } |
| 662 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 663 | // Don't call for fast tracks; the framesReady() could result in priority inversion |
| 664 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 665 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) { |
| 666 | return true; |
| 667 | } |
| 668 | |
Eric Laurent | 1649851 | 2014-03-17 17:22:08 -0700 | [diff] [blame] | 669 | if (isStopping()) { |
| 670 | if (framesReady() > 0) { |
| 671 | mFillingUpStatus = FS_FILLED; |
| 672 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 673 | return true; |
| 674 | } |
| 675 | |
| 676 | if (framesReady() >= mFrameCount || |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 677 | (mCblk->mFlags & CBLK_FORCEREADY)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 678 | mFillingUpStatus = FS_FILLED; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 679 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 680 | return true; |
| 681 | } |
| 682 | return false; |
| 683 | } |
| 684 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 685 | status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event __unused, |
| 686 | int triggerSession __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 687 | { |
| 688 | status_t status = NO_ERROR; |
| 689 | ALOGV("start(%d), calling pid %d session %d", |
| 690 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
| 691 | |
| 692 | sp<ThreadBase> thread = mThread.promote(); |
| 693 | if (thread != 0) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 694 | if (isOffloaded()) { |
| 695 | Mutex::Autolock _laf(thread->mAudioFlinger->mLock); |
| 696 | Mutex::Autolock _lth(thread->mLock); |
| 697 | sp<EffectChain> ec = thread->getEffectChain_l(mSessionId); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 698 | if (thread->mAudioFlinger->isNonOffloadableGlobalEffectEnabled_l() || |
| 699 | (ec != 0 && ec->isNonOffloadableEnabled())) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 700 | invalidate(); |
| 701 | return PERMISSION_DENIED; |
| 702 | } |
| 703 | } |
| 704 | Mutex::Autolock _lth(thread->mLock); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 705 | track_state state = mState; |
| 706 | // here the track could be either new, or restarted |
| 707 | // in both cases "unstop" the track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 708 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 709 | // initial state-stopping. next state-pausing. |
| 710 | // What if resume is called ? |
| 711 | |
| 712 | if (state == PAUSED || state == PAUSING) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 713 | if (mResumeToStopping) { |
| 714 | // happened we need to resume to STOPPING_1 |
| 715 | mState = TrackBase::STOPPING_1; |
| 716 | ALOGV("PAUSED => STOPPING_1 (%d) on thread %p", mName, this); |
| 717 | } else { |
| 718 | mState = TrackBase::RESUMING; |
| 719 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
| 720 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 721 | } else { |
| 722 | mState = TrackBase::ACTIVE; |
| 723 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
| 724 | } |
| 725 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 726 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 727 | status = playbackThread->addTrack_l(this); |
| 728 | if (status == INVALID_OPERATION || status == PERMISSION_DENIED) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 729 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 730 | // restore previous state if start was rejected by policy manager |
| 731 | if (status == PERMISSION_DENIED) { |
| 732 | mState = state; |
| 733 | } |
| 734 | } |
| 735 | // track was already in the active list, not a problem |
| 736 | if (status == ALREADY_EXISTS) { |
| 737 | status = NO_ERROR; |
Glenn Kasten | 12022ff | 2013-10-17 11:32:39 -0700 | [diff] [blame] | 738 | } else { |
| 739 | // Acknowledge any pending flush(), so that subsequent new data isn't discarded. |
| 740 | // It is usually unsafe to access the server proxy from a binder thread. |
| 741 | // But in this case we know the mixer thread (whether normal mixer or fast mixer) |
| 742 | // isn't looking at this track yet: we still hold the normal mixer thread lock, |
| 743 | // and for fast tracks the track is not yet in the fast mixer thread's active set. |
| 744 | ServerProxy::Buffer buffer; |
| 745 | buffer.mFrameCount = 1; |
Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 746 | (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 747 | } |
| 748 | } else { |
| 749 | status = BAD_VALUE; |
| 750 | } |
| 751 | return status; |
| 752 | } |
| 753 | |
| 754 | void AudioFlinger::PlaybackThread::Track::stop() |
| 755 | { |
| 756 | ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
| 757 | sp<ThreadBase> thread = mThread.promote(); |
| 758 | if (thread != 0) { |
| 759 | Mutex::Autolock _l(thread->mLock); |
| 760 | track_state state = mState; |
| 761 | if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) { |
| 762 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 763 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 764 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 765 | reset(); |
| 766 | mState = STOPPED; |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 767 | } else if (!isFastTrack() && !isOffloaded() && !isDirect()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 768 | mState = STOPPED; |
| 769 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 770 | // For fast tracks prepareTracks_l() will set state to STOPPING_2 |
| 771 | // presentation is complete |
| 772 | // For an offloaded track this starts a drain and state will |
| 773 | // move to STOPPING_2 when drain completes and then STOPPED |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 774 | mState = STOPPING_1; |
| 775 | } |
| 776 | ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName, |
| 777 | playbackThread); |
| 778 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | void AudioFlinger::PlaybackThread::Track::pause() |
| 783 | { |
| 784 | ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
| 785 | sp<ThreadBase> thread = mThread.promote(); |
| 786 | if (thread != 0) { |
| 787 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 788 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 789 | switch (mState) { |
| 790 | case STOPPING_1: |
| 791 | case STOPPING_2: |
| 792 | if (!isOffloaded()) { |
| 793 | /* nothing to do if track is not offloaded */ |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | // Offloaded track was draining, we need to carry on draining when resumed |
| 798 | mResumeToStopping = true; |
| 799 | // fall through... |
| 800 | case ACTIVE: |
| 801 | case RESUMING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 802 | mState = PAUSING; |
| 803 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 804 | playbackThread->broadcast_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 805 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 806 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 807 | default: |
| 808 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | void AudioFlinger::PlaybackThread::Track::flush() |
| 814 | { |
| 815 | ALOGV("flush(%d)", mName); |
| 816 | sp<ThreadBase> thread = mThread.promote(); |
| 817 | if (thread != 0) { |
| 818 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 819 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 820 | |
| 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 | |
| 838 | if (mState == ACTIVE) { |
| 839 | ALOGV("flush called in active state, resetting buffer time out retry count"); |
| 840 | mRetryCount = PlaybackThread::kMaxTrackRetriesOffload; |
| 841 | } |
| 842 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 843 | mFlushHwPending = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 844 | mResumeToStopping = false; |
| 845 | } else { |
| 846 | if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && |
| 847 | mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) { |
| 848 | return; |
| 849 | } |
| 850 | // No point remaining in PAUSED state after a flush => go to |
| 851 | // FLUSHED state |
| 852 | mState = FLUSHED; |
| 853 | // do not reset the track if it is still in the process of being stopped or paused. |
| 854 | // this will be done by prepareTracks_l() when the track is stopped. |
| 855 | // prepareTracks_l() will see mState == FLUSHED, then |
| 856 | // remove from active track list, reset(), and trigger presentation complete |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 857 | if (isDirect()) { |
| 858 | mFlushHwPending = true; |
| 859 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 860 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 861 | reset(); |
| 862 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 863 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 864 | // Prevent flush being lost if the track is flushed and then resumed |
| 865 | // before mixer thread can run. This is important when offloading |
| 866 | // because the hardware buffer could hold a large amount of audio |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 867 | playbackThread->broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 871 | // must be called with thread lock held |
| 872 | void AudioFlinger::PlaybackThread::Track::flushAck() |
| 873 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 874 | if (!isOffloaded() && !isDirect()) |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 875 | return; |
| 876 | |
| 877 | mFlushHwPending = false; |
| 878 | } |
| 879 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 880 | void AudioFlinger::PlaybackThread::Track::reset() |
| 881 | { |
| 882 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 883 | // the audioflinger thread detects the track is stopped. |
| 884 | if (!mResetDone) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 885 | // Force underrun condition to avoid false underrun callback until first data is |
| 886 | // written to buffer |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 887 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 888 | mFillingUpStatus = FS_FILLING; |
| 889 | mResetDone = true; |
| 890 | if (mState == FLUSHED) { |
| 891 | mState = IDLE; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 896 | status_t AudioFlinger::PlaybackThread::Track::setParameters(const String8& keyValuePairs) |
| 897 | { |
| 898 | sp<ThreadBase> thread = mThread.promote(); |
| 899 | if (thread == 0) { |
| 900 | ALOGE("thread is dead"); |
| 901 | return FAILED_TRANSACTION; |
| 902 | } else if ((thread->type() == ThreadBase::DIRECT) || |
| 903 | (thread->type() == ThreadBase::OFFLOAD)) { |
| 904 | return thread->setParameters(keyValuePairs); |
| 905 | } else { |
| 906 | return PERMISSION_DENIED; |
| 907 | } |
| 908 | } |
| 909 | |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 910 | status_t AudioFlinger::PlaybackThread::Track::getTimestamp(AudioTimestamp& timestamp) |
| 911 | { |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 912 | // Client should implement this using SSQ; the unpresented frame count in latch is irrelevant |
| 913 | if (isFastTrack()) { |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 914 | // FIXME no lock held to set mPreviousValid = false |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 915 | return INVALID_OPERATION; |
| 916 | } |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 917 | sp<ThreadBase> thread = mThread.promote(); |
| 918 | if (thread == 0) { |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 919 | // FIXME no lock held to set mPreviousValid = false |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 920 | return INVALID_OPERATION; |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 921 | } |
| 922 | Mutex::Autolock _l(thread->mLock); |
| 923 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 924 | if (!isOffloaded() && !isDirect()) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 925 | if (!playbackThread->mLatchQValid) { |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 926 | mPreviousValid = false; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 927 | return INVALID_OPERATION; |
| 928 | } |
| 929 | uint32_t unpresentedFrames = |
| 930 | ((int64_t) playbackThread->mLatchQ.mUnpresentedFrames * mSampleRate) / |
| 931 | playbackThread->mSampleRate; |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 932 | // FIXME Since we're using a raw pointer as the key, it is theoretically possible |
| 933 | // for a brand new track to share the same address as a recently destroyed |
| 934 | // track, and thus for us to get the frames released of the wrong track. |
| 935 | // It is unlikely that we would be able to call getTimestamp() so quickly |
| 936 | // right after creating a new track. Nevertheless, the index here should |
| 937 | // be changed to something that is unique. Or use a completely different strategy. |
| 938 | ssize_t i = playbackThread->mLatchQ.mFramesReleased.indexOfKey(this); |
| 939 | uint32_t framesWritten = i >= 0 ? |
| 940 | playbackThread->mLatchQ.mFramesReleased[i] : |
| 941 | mAudioTrackServerProxy->framesReleased(); |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 942 | bool checkPreviousTimestamp = mPreviousValid && framesWritten >= mPreviousFramesWritten; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 943 | if (framesWritten < unpresentedFrames) { |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 944 | mPreviousValid = false; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 945 | return INVALID_OPERATION; |
| 946 | } |
Glenn Kasten | ced6e74 | 2014-06-09 17:12:32 -0700 | [diff] [blame] | 947 | mPreviousFramesWritten = framesWritten; |
| 948 | uint32_t position = framesWritten - unpresentedFrames; |
| 949 | struct timespec time = playbackThread->mLatchQ.mTimestamp.mTime; |
| 950 | if (checkPreviousTimestamp) { |
| 951 | if (time.tv_sec < mPreviousTimestamp.mTime.tv_sec || |
| 952 | (time.tv_sec == mPreviousTimestamp.mTime.tv_sec && |
| 953 | time.tv_nsec < mPreviousTimestamp.mTime.tv_nsec)) { |
| 954 | ALOGW("Time is going backwards"); |
| 955 | } |
| 956 | // position can bobble slightly as an artifact; this hides the bobble |
| 957 | static const uint32_t MINIMUM_POSITION_DELTA = 8u; |
| 958 | if ((position <= mPreviousTimestamp.mPosition) || |
| 959 | (position - mPreviousTimestamp.mPosition) < MINIMUM_POSITION_DELTA) { |
| 960 | position = mPreviousTimestamp.mPosition; |
| 961 | time = mPreviousTimestamp.mTime; |
| 962 | } |
| 963 | } |
| 964 | timestamp.mPosition = position; |
| 965 | timestamp.mTime = time; |
| 966 | mPreviousTimestamp = timestamp; |
| 967 | mPreviousValid = true; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 968 | return NO_ERROR; |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 969 | } |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 970 | |
| 971 | return playbackThread->getTimestamp_l(timestamp); |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 974 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 975 | { |
| 976 | status_t status = DEAD_OBJECT; |
| 977 | sp<ThreadBase> thread = mThread.promote(); |
| 978 | if (thread != 0) { |
| 979 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 980 | sp<AudioFlinger> af = mClient->audioFlinger(); |
| 981 | |
| 982 | Mutex::Autolock _l(af->mLock); |
| 983 | |
| 984 | sp<PlaybackThread> srcThread = af->getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 985 | |
| 986 | if (EffectId != 0 && srcThread != 0 && playbackThread != srcThread.get()) { |
| 987 | Mutex::Autolock _dl(playbackThread->mLock); |
| 988 | Mutex::Autolock _sl(srcThread->mLock); |
| 989 | sp<EffectChain> chain = srcThread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 990 | if (chain == 0) { |
| 991 | return INVALID_OPERATION; |
| 992 | } |
| 993 | |
| 994 | sp<EffectModule> effect = chain->getEffectFromId_l(EffectId); |
| 995 | if (effect == 0) { |
| 996 | return INVALID_OPERATION; |
| 997 | } |
| 998 | srcThread->removeEffect_l(effect); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 999 | status = playbackThread->addEffect_l(effect); |
| 1000 | if (status != NO_ERROR) { |
| 1001 | srcThread->addEffect_l(effect); |
| 1002 | return INVALID_OPERATION; |
| 1003 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1004 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 1005 | if (effect->state() == EffectModule::ACTIVE || |
| 1006 | effect->state() == EffectModule::STOPPING) { |
| 1007 | effect->start(); |
| 1008 | } |
| 1009 | |
| 1010 | sp<EffectChain> dstChain = effect->chain().promote(); |
| 1011 | if (dstChain == 0) { |
| 1012 | srcThread->addEffect_l(effect); |
| 1013 | return INVALID_OPERATION; |
| 1014 | } |
| 1015 | AudioSystem::unregisterEffect(effect->id()); |
| 1016 | AudioSystem::registerEffect(&effect->desc(), |
| 1017 | srcThread->id(), |
| 1018 | dstChain->strategy(), |
| 1019 | AUDIO_SESSION_OUTPUT_MIX, |
| 1020 | effect->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 1021 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1022 | } |
| 1023 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 1024 | } |
| 1025 | return status; |
| 1026 | } |
| 1027 | |
| 1028 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 1029 | { |
| 1030 | mAuxEffectId = EffectId; |
| 1031 | mAuxBuffer = buffer; |
| 1032 | } |
| 1033 | |
| 1034 | bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten, |
| 1035 | size_t audioHalFrames) |
| 1036 | { |
| 1037 | // a track is considered presented when the total number of frames written to audio HAL |
| 1038 | // corresponds to the number of frames written when presentationComplete() is called for the |
| 1039 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1040 | // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used |
| 1041 | // to detect when all frames have been played. In this case framesWritten isn't |
| 1042 | // useful because it doesn't always reflect whether there is data in the h/w |
| 1043 | // buffers, particularly if a track has been paused and resumed during draining |
| 1044 | ALOGV("presentationComplete() mPresentationCompleteFrames %d framesWritten %d", |
| 1045 | mPresentationCompleteFrames, framesWritten); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1046 | if (mPresentationCompleteFrames == 0) { |
| 1047 | mPresentationCompleteFrames = framesWritten + audioHalFrames; |
| 1048 | ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d", |
| 1049 | mPresentationCompleteFrames, audioHalFrames); |
| 1050 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1051 | |
| 1052 | if (framesWritten >= mPresentationCompleteFrames || isOffloaded()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1053 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1054 | mAudioTrackServerProxy->setStreamEndDone(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1055 | return true; |
| 1056 | } |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) |
| 1061 | { |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 1062 | for (size_t i = 0; i < mSyncEvents.size(); i++) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1063 | if (mSyncEvents[i]->type() == type) { |
| 1064 | mSyncEvents[i]->trigger(); |
| 1065 | mSyncEvents.removeAt(i); |
| 1066 | i--; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | // implement VolumeBufferProvider interface |
| 1072 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1073 | gain_minifloat_packed_t AudioFlinger::PlaybackThread::Track::getVolumeLR() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1074 | { |
| 1075 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs |
| 1076 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1077 | gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); |
| 1078 | float vl = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 1079 | float vr = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1080 | // 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] | 1081 | if (vl > GAIN_FLOAT_UNITY) { |
| 1082 | vl = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1083 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1084 | if (vr > GAIN_FLOAT_UNITY) { |
| 1085 | vr = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1086 | } |
| 1087 | // now apply the cached master volume and stream type volume; |
| 1088 | // this is trusted but lacks any synchronization or barrier so may be stale |
| 1089 | float v = mCachedVolume; |
| 1090 | vl *= v; |
| 1091 | vr *= v; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1092 | // re-combine into packed minifloat |
| 1093 | vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1094 | // FIXME look at mute, pause, and stop flags |
| 1095 | return vlr; |
| 1096 | } |
| 1097 | |
| 1098 | status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event) |
| 1099 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1100 | if (isTerminated() || mState == PAUSED || |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1101 | ((framesReady() == 0) && ((mSharedBuffer != 0) || |
| 1102 | (mState == STOPPED)))) { |
| 1103 | ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %d ", |
| 1104 | mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady()); |
| 1105 | event->cancel(); |
| 1106 | return INVALID_OPERATION; |
| 1107 | } |
| 1108 | (void) TrackBase::setSyncEvent(event); |
| 1109 | return NO_ERROR; |
| 1110 | } |
| 1111 | |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1112 | void AudioFlinger::PlaybackThread::Track::invalidate() |
| 1113 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1114 | // FIXME should use proxy, and needs work |
| 1115 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1116 | android_atomic_or(CBLK_INVALID, &cblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1117 | android_atomic_release_store(0x40000000, &cblk->mFutex); |
| 1118 | // 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] | 1119 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1120 | mIsInvalid = true; |
| 1121 | } |
| 1122 | |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1123 | void AudioFlinger::PlaybackThread::Track::signal() |
| 1124 | { |
| 1125 | sp<ThreadBase> thread = mThread.promote(); |
| 1126 | if (thread != 0) { |
| 1127 | PlaybackThread *t = (PlaybackThread *)thread.get(); |
| 1128 | Mutex::Autolock _l(t->mLock); |
| 1129 | t->broadcast_l(); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1133 | //To be called with thread lock held |
| 1134 | bool AudioFlinger::PlaybackThread::Track::isResumePending() { |
| 1135 | |
| 1136 | if (mState == RESUMING) |
| 1137 | return true; |
| 1138 | /* Resume is pending if track was stopping before pause was called */ |
| 1139 | if (mState == STOPPING_1 && |
| 1140 | mResumeToStopping) |
| 1141 | return true; |
| 1142 | |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
| 1146 | //To be called with thread lock held |
| 1147 | void AudioFlinger::PlaybackThread::Track::resumeAck() { |
| 1148 | |
| 1149 | |
| 1150 | if (mState == RESUMING) |
| 1151 | mState = ACTIVE; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1152 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1153 | // Other possibility of pending resume is stopping_1 state |
| 1154 | // Do not update the state from stopping as this prevents |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1155 | // drain being called. |
| 1156 | if (mState == STOPPING_1) { |
| 1157 | mResumeToStopping = false; |
| 1158 | } |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1159 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1160 | // ---------------------------------------------------------------------------- |
| 1161 | |
| 1162 | sp<AudioFlinger::PlaybackThread::TimedTrack> |
| 1163 | AudioFlinger::PlaybackThread::TimedTrack::create( |
| 1164 | PlaybackThread *thread, |
| 1165 | const sp<Client>& client, |
| 1166 | audio_stream_type_t streamType, |
| 1167 | uint32_t sampleRate, |
| 1168 | audio_format_t format, |
| 1169 | audio_channel_mask_t channelMask, |
| 1170 | size_t frameCount, |
| 1171 | const sp<IMemory>& sharedBuffer, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1172 | int sessionId, |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 1173 | int uid) |
| 1174 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1175 | if (!client->reserveTimedTrack()) |
| 1176 | return 0; |
| 1177 | |
| 1178 | return new TimedTrack( |
| 1179 | thread, client, streamType, sampleRate, format, channelMask, frameCount, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1180 | sharedBuffer, sessionId, uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | AudioFlinger::PlaybackThread::TimedTrack::TimedTrack( |
| 1184 | PlaybackThread *thread, |
| 1185 | const sp<Client>& client, |
| 1186 | audio_stream_type_t streamType, |
| 1187 | uint32_t sampleRate, |
| 1188 | audio_format_t format, |
| 1189 | audio_channel_mask_t channelMask, |
| 1190 | size_t frameCount, |
| 1191 | const sp<IMemory>& sharedBuffer, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1192 | int sessionId, |
| 1193 | int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1194 | : Track(thread, client, streamType, sampleRate, format, channelMask, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1195 | frameCount, (sharedBuffer != 0) ? sharedBuffer->pointer() : NULL, sharedBuffer, |
| 1196 | sessionId, uid, IAudioFlinger::TRACK_TIMED, TYPE_TIMED), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1197 | mQueueHeadInFlight(false), |
| 1198 | mTrimQueueHeadOnRelease(false), |
| 1199 | mFramesPendingInQueue(0), |
| 1200 | mTimedSilenceBuffer(NULL), |
| 1201 | mTimedSilenceBufferSize(0), |
| 1202 | mTimedAudioOutputOnTime(false), |
| 1203 | mMediaTimeTransformValid(false) |
| 1204 | { |
| 1205 | LocalClock lc; |
| 1206 | mLocalTimeFreq = lc.getLocalFreq(); |
| 1207 | |
| 1208 | mLocalTimeToSampleTransform.a_zero = 0; |
| 1209 | mLocalTimeToSampleTransform.b_zero = 0; |
| 1210 | mLocalTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 1211 | mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq; |
| 1212 | LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer, |
| 1213 | &mLocalTimeToSampleTransform.a_to_b_denom); |
| 1214 | |
| 1215 | mMediaTimeToSampleTransform.a_zero = 0; |
| 1216 | mMediaTimeToSampleTransform.b_zero = 0; |
| 1217 | mMediaTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 1218 | mMediaTimeToSampleTransform.a_to_b_denom = 1000000; |
| 1219 | LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer, |
| 1220 | &mMediaTimeToSampleTransform.a_to_b_denom); |
| 1221 | } |
| 1222 | |
| 1223 | AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() { |
| 1224 | mClient->releaseTimedTrack(); |
| 1225 | delete [] mTimedSilenceBuffer; |
| 1226 | } |
| 1227 | |
| 1228 | status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer( |
| 1229 | size_t size, sp<IMemory>* buffer) { |
| 1230 | |
| 1231 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 1232 | |
| 1233 | trimTimedBufferQueue_l(); |
| 1234 | |
| 1235 | // lazily initialize the shared memory heap for timed buffers |
| 1236 | if (mTimedMemoryDealer == NULL) { |
| 1237 | const int kTimedBufferHeapSize = 512 << 10; |
| 1238 | |
| 1239 | mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize, |
| 1240 | "AudioFlingerTimed"); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 1241 | if (mTimedMemoryDealer == NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1242 | return NO_MEMORY; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 1243 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size); |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1247 | if (newBuffer == 0 || newBuffer->pointer() == NULL) { |
Glenn Kasten | 30ff92c | 2013-11-20 11:57:08 -0800 | [diff] [blame] | 1248 | return NO_MEMORY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | *buffer = newBuffer; |
| 1252 | return NO_ERROR; |
| 1253 | } |
| 1254 | |
| 1255 | // caller must hold mTimedBufferQueueLock |
| 1256 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() { |
| 1257 | int64_t mediaTimeNow; |
| 1258 | { |
| 1259 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 1260 | if (!mMediaTimeTransformValid) |
| 1261 | return; |
| 1262 | |
| 1263 | int64_t targetTimeNow; |
| 1264 | status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) |
| 1265 | ? mCCHelper.getCommonTime(&targetTimeNow) |
| 1266 | : mCCHelper.getLocalTime(&targetTimeNow); |
| 1267 | |
| 1268 | if (OK != res) |
| 1269 | return; |
| 1270 | |
| 1271 | if (!mMediaTimeTransform.doReverseTransform(targetTimeNow, |
| 1272 | &mediaTimeNow)) { |
| 1273 | return; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | size_t trimEnd; |
| 1278 | for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) { |
| 1279 | int64_t bufEnd; |
| 1280 | |
| 1281 | if ((trimEnd + 1) < mTimedBufferQueue.size()) { |
| 1282 | // We have a next buffer. Just use its PTS as the PTS of the frame |
| 1283 | // following the last frame in this buffer. If the stream is sparse |
| 1284 | // (ie, there are deliberate gaps left in the stream which should be |
| 1285 | // filled with silence by the TimedAudioTrack), then this can result |
| 1286 | // in one extra buffer being left un-trimmed when it could have |
| 1287 | // been. In general, this is not typical, and we would rather |
| 1288 | // optimized away the TS calculation below for the more common case |
| 1289 | // where PTSes are contiguous. |
| 1290 | bufEnd = mTimedBufferQueue[trimEnd + 1].pts(); |
| 1291 | } else { |
| 1292 | // We have no next buffer. Compute the PTS of the frame following |
| 1293 | // the last frame in this buffer by computing the duration of of |
| 1294 | // this frame in media time units and adding it to the PTS of the |
| 1295 | // buffer. |
| 1296 | int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size() |
| 1297 | / mFrameSize; |
| 1298 | |
| 1299 | if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount, |
| 1300 | &bufEnd)) { |
| 1301 | ALOGE("Failed to convert frame count of %lld to media time" |
| 1302 | " duration" " (scale factor %d/%u) in %s", |
| 1303 | frameCount, |
| 1304 | mMediaTimeToSampleTransform.a_to_b_numer, |
| 1305 | mMediaTimeToSampleTransform.a_to_b_denom, |
| 1306 | __PRETTY_FUNCTION__); |
| 1307 | break; |
| 1308 | } |
| 1309 | bufEnd += mTimedBufferQueue[trimEnd].pts(); |
| 1310 | } |
| 1311 | |
| 1312 | if (bufEnd > mediaTimeNow) |
| 1313 | break; |
| 1314 | |
| 1315 | // Is the buffer we want to use in the middle of a mix operation right |
| 1316 | // now? If so, don't actually trim it. Just wait for the releaseBuffer |
| 1317 | // from the mixer which should be coming back shortly. |
| 1318 | if (!trimEnd && mQueueHeadInFlight) { |
| 1319 | mTrimQueueHeadOnRelease = true; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0; |
| 1324 | if (trimStart < trimEnd) { |
| 1325 | // Update the bookkeeping for framesReady() |
| 1326 | for (size_t i = trimStart; i < trimEnd; ++i) { |
| 1327 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim"); |
| 1328 | } |
| 1329 | |
| 1330 | // Now actually remove the buffers from the queue. |
| 1331 | mTimedBufferQueue.removeItemsAt(trimStart, trimEnd); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l( |
| 1336 | const char* logTag) { |
| 1337 | ALOG_ASSERT(mTimedBufferQueue.size() > 0, |
| 1338 | "%s called (reason \"%s\"), but timed buffer queue has no" |
| 1339 | " elements to trim.", __FUNCTION__, logTag); |
| 1340 | |
| 1341 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag); |
| 1342 | mTimedBufferQueue.removeAt(0); |
| 1343 | } |
| 1344 | |
| 1345 | void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l( |
| 1346 | const TimedBuffer& buf, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1347 | const char* logTag __unused) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1348 | uint32_t bufBytes = buf.buffer()->size(); |
| 1349 | uint32_t consumedAlready = buf.position(); |
| 1350 | |
| 1351 | ALOG_ASSERT(consumedAlready <= bufBytes, |
| 1352 | "Bad bookkeeping while updating frames pending. Timed buffer is" |
| 1353 | " only %u bytes long, but claims to have consumed %u" |
| 1354 | " bytes. (update reason: \"%s\")", |
| 1355 | bufBytes, consumedAlready, logTag); |
| 1356 | |
| 1357 | uint32_t bufFrames = (bufBytes - consumedAlready) / mFrameSize; |
| 1358 | ALOG_ASSERT(mFramesPendingInQueue >= bufFrames, |
| 1359 | "Bad bookkeeping while updating frames pending. Should have at" |
| 1360 | " least %u queued frames, but we think we have only %u. (update" |
| 1361 | " reason: \"%s\")", |
| 1362 | bufFrames, mFramesPendingInQueue, logTag); |
| 1363 | |
| 1364 | mFramesPendingInQueue -= bufFrames; |
| 1365 | } |
| 1366 | |
| 1367 | status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer( |
| 1368 | const sp<IMemory>& buffer, int64_t pts) { |
| 1369 | |
| 1370 | { |
| 1371 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 1372 | if (!mMediaTimeTransformValid) |
| 1373 | return INVALID_OPERATION; |
| 1374 | } |
| 1375 | |
| 1376 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 1377 | |
| 1378 | uint32_t bufFrames = buffer->size() / mFrameSize; |
| 1379 | mFramesPendingInQueue += bufFrames; |
| 1380 | mTimedBufferQueue.add(TimedBuffer(buffer, pts)); |
| 1381 | |
| 1382 | return NO_ERROR; |
| 1383 | } |
| 1384 | |
| 1385 | status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform( |
| 1386 | const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) { |
| 1387 | |
| 1388 | ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d", |
| 1389 | xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom, |
| 1390 | target); |
| 1391 | |
| 1392 | if (!(target == TimedAudioTrack::LOCAL_TIME || |
| 1393 | target == TimedAudioTrack::COMMON_TIME)) { |
| 1394 | return BAD_VALUE; |
| 1395 | } |
| 1396 | |
| 1397 | Mutex::Autolock lock(mMediaTimeTransformLock); |
| 1398 | mMediaTimeTransform = xform; |
| 1399 | mMediaTimeTransformTarget = target; |
| 1400 | mMediaTimeTransformValid = true; |
| 1401 | |
| 1402 | return NO_ERROR; |
| 1403 | } |
| 1404 | |
| 1405 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 1406 | |
| 1407 | // implementation of getNextBuffer for tracks whose buffers have timestamps |
| 1408 | status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer( |
| 1409 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 1410 | { |
| 1411 | if (pts == AudioBufferProvider::kInvalidPTS) { |
| 1412 | buffer->raw = NULL; |
| 1413 | buffer->frameCount = 0; |
| 1414 | mTimedAudioOutputOnTime = false; |
| 1415 | return INVALID_OPERATION; |
| 1416 | } |
| 1417 | |
| 1418 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 1419 | |
| 1420 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 1421 | "getNextBuffer called without releaseBuffer!"); |
| 1422 | |
| 1423 | while (true) { |
| 1424 | |
| 1425 | // if we have no timed buffers, then fail |
| 1426 | if (mTimedBufferQueue.isEmpty()) { |
| 1427 | buffer->raw = NULL; |
| 1428 | buffer->frameCount = 0; |
| 1429 | return NOT_ENOUGH_DATA; |
| 1430 | } |
| 1431 | |
| 1432 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
| 1433 | |
| 1434 | // calculate the PTS of the head of the timed buffer queue expressed in |
| 1435 | // local time |
| 1436 | int64_t headLocalPTS; |
| 1437 | { |
| 1438 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 1439 | |
| 1440 | ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid"); |
| 1441 | |
| 1442 | if (mMediaTimeTransform.a_to_b_denom == 0) { |
| 1443 | // the transform represents a pause, so yield silence |
| 1444 | timedYieldSilence_l(buffer->frameCount, buffer); |
| 1445 | return NO_ERROR; |
| 1446 | } |
| 1447 | |
| 1448 | int64_t transformedPTS; |
| 1449 | if (!mMediaTimeTransform.doForwardTransform(head.pts(), |
| 1450 | &transformedPTS)) { |
| 1451 | // the transform failed. this shouldn't happen, but if it does |
| 1452 | // then just drop this buffer |
| 1453 | ALOGW("timedGetNextBuffer transform failed"); |
| 1454 | buffer->raw = NULL; |
| 1455 | buffer->frameCount = 0; |
| 1456 | trimTimedBufferQueueHead_l("getNextBuffer; no transform"); |
| 1457 | return NO_ERROR; |
| 1458 | } |
| 1459 | |
| 1460 | if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) { |
| 1461 | if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS, |
| 1462 | &headLocalPTS)) { |
| 1463 | buffer->raw = NULL; |
| 1464 | buffer->frameCount = 0; |
| 1465 | return INVALID_OPERATION; |
| 1466 | } |
| 1467 | } else { |
| 1468 | headLocalPTS = transformedPTS; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 1472 | uint32_t sr = sampleRate(); |
| 1473 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1474 | // adjust the head buffer's PTS to reflect the portion of the head buffer |
| 1475 | // that has already been consumed |
| 1476 | int64_t effectivePTS = headLocalPTS + |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 1477 | ((head.position() / mFrameSize) * mLocalTimeFreq / sr); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1478 | |
| 1479 | // Calculate the delta in samples between the head of the input buffer |
| 1480 | // queue and the start of the next output buffer that will be written. |
| 1481 | // If the transformation fails because of over or underflow, it means |
| 1482 | // that the sample's position in the output stream is so far out of |
| 1483 | // whack that it should just be dropped. |
| 1484 | int64_t sampleDelta; |
| 1485 | if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) { |
| 1486 | ALOGV("*** head buffer is too far from PTS: dropped buffer"); |
| 1487 | trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from" |
| 1488 | " mix"); |
| 1489 | continue; |
| 1490 | } |
| 1491 | if (!mLocalTimeToSampleTransform.doForwardTransform( |
| 1492 | (effectivePTS - pts) << 32, &sampleDelta)) { |
| 1493 | ALOGV("*** too late during sample rate transform: dropped buffer"); |
| 1494 | trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample"); |
| 1495 | continue; |
| 1496 | } |
| 1497 | |
| 1498 | ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld" |
| 1499 | " sampleDelta=[%d.%08x]", |
| 1500 | head.pts(), head.position(), pts, |
| 1501 | static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) |
| 1502 | + (sampleDelta >> 32)), |
| 1503 | static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF)); |
| 1504 | |
| 1505 | // if the delta between the ideal placement for the next input sample and |
| 1506 | // the current output position is within this threshold, then we will |
| 1507 | // concatenate the next input samples to the previous output |
| 1508 | const int64_t kSampleContinuityThreshold = |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 1509 | (static_cast<int64_t>(sr) << 32) / 250; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1510 | |
| 1511 | // if this is the first buffer of audio that we're emitting from this track |
| 1512 | // then it should be almost exactly on time. |
| 1513 | const int64_t kSampleStartupThreshold = 1LL << 32; |
| 1514 | |
| 1515 | if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) || |
| 1516 | (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) { |
| 1517 | // the next input is close enough to being on time, so concatenate it |
| 1518 | // with the last output |
| 1519 | timedYieldSamples_l(buffer); |
| 1520 | |
| 1521 | ALOGVV("*** on time: head.pos=%d frameCount=%u", |
| 1522 | head.position(), buffer->frameCount); |
| 1523 | return NO_ERROR; |
| 1524 | } |
| 1525 | |
| 1526 | // Looks like our output is not on time. Reset our on timed status. |
| 1527 | // Next time we mix samples from our input queue, then should be within |
| 1528 | // the StartupThreshold. |
| 1529 | mTimedAudioOutputOnTime = false; |
| 1530 | if (sampleDelta > 0) { |
| 1531 | // the gap between the current output position and the proper start of |
| 1532 | // the next input sample is too big, so fill it with silence |
| 1533 | uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32; |
| 1534 | |
| 1535 | timedYieldSilence_l(framesUntilNextInput, buffer); |
| 1536 | ALOGV("*** silence: frameCount=%u", buffer->frameCount); |
| 1537 | return NO_ERROR; |
| 1538 | } else { |
| 1539 | // the next input sample is late |
| 1540 | uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32)); |
| 1541 | size_t onTimeSamplePosition = |
| 1542 | head.position() + lateFrames * mFrameSize; |
| 1543 | |
| 1544 | if (onTimeSamplePosition > head.buffer()->size()) { |
| 1545 | // all the remaining samples in the head are too late, so |
| 1546 | // drop it and move on |
| 1547 | ALOGV("*** too late: dropped buffer"); |
| 1548 | trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer"); |
| 1549 | continue; |
| 1550 | } else { |
| 1551 | // skip over the late samples |
| 1552 | head.setPosition(onTimeSamplePosition); |
| 1553 | |
| 1554 | // yield the available samples |
| 1555 | timedYieldSamples_l(buffer); |
| 1556 | |
| 1557 | ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); |
| 1558 | return NO_ERROR; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | // Yield samples from the timed buffer queue head up to the given output |
| 1565 | // buffer's capacity. |
| 1566 | // |
| 1567 | // Caller must hold mTimedBufferQueueLock |
| 1568 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l( |
| 1569 | AudioBufferProvider::Buffer* buffer) { |
| 1570 | |
| 1571 | const TimedBuffer& head = mTimedBufferQueue[0]; |
| 1572 | |
| 1573 | buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) + |
| 1574 | head.position()); |
| 1575 | |
| 1576 | uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) / |
| 1577 | mFrameSize); |
| 1578 | size_t framesRequested = buffer->frameCount; |
| 1579 | buffer->frameCount = min(framesLeftInHead, framesRequested); |
| 1580 | |
| 1581 | mQueueHeadInFlight = true; |
| 1582 | mTimedAudioOutputOnTime = true; |
| 1583 | } |
| 1584 | |
| 1585 | // Yield samples of silence up to the given output buffer's capacity |
| 1586 | // |
| 1587 | // Caller must hold mTimedBufferQueueLock |
| 1588 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l( |
| 1589 | uint32_t numFrames, AudioBufferProvider::Buffer* buffer) { |
| 1590 | |
| 1591 | // lazily allocate a buffer filled with silence |
| 1592 | if (mTimedSilenceBufferSize < numFrames * mFrameSize) { |
| 1593 | delete [] mTimedSilenceBuffer; |
| 1594 | mTimedSilenceBufferSize = numFrames * mFrameSize; |
| 1595 | mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize]; |
| 1596 | memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize); |
| 1597 | } |
| 1598 | |
| 1599 | buffer->raw = mTimedSilenceBuffer; |
| 1600 | size_t framesRequested = buffer->frameCount; |
| 1601 | buffer->frameCount = min(numFrames, framesRequested); |
| 1602 | |
| 1603 | mTimedAudioOutputOnTime = false; |
| 1604 | } |
| 1605 | |
| 1606 | // AudioBufferProvider interface |
| 1607 | void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer( |
| 1608 | AudioBufferProvider::Buffer* buffer) { |
| 1609 | |
| 1610 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 1611 | |
| 1612 | // If the buffer which was just released is part of the buffer at the head |
| 1613 | // of the queue, be sure to update the amt of the buffer which has been |
| 1614 | // consumed. If the buffer being returned is not part of the head of the |
| 1615 | // queue, its either because the buffer is part of the silence buffer, or |
| 1616 | // because the head of the timed queue was trimmed after the mixer called |
| 1617 | // getNextBuffer but before the mixer called releaseBuffer. |
| 1618 | if (buffer->raw == mTimedSilenceBuffer) { |
| 1619 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 1620 | "Queue head in flight during release of silence buffer!"); |
| 1621 | goto done; |
| 1622 | } |
| 1623 | |
| 1624 | ALOG_ASSERT(mQueueHeadInFlight, |
| 1625 | "TimedTrack::releaseBuffer of non-silence buffer, but no queue" |
| 1626 | " head in flight."); |
| 1627 | |
| 1628 | if (mTimedBufferQueue.size()) { |
| 1629 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
| 1630 | |
| 1631 | void* start = head.buffer()->pointer(); |
| 1632 | void* end = reinterpret_cast<void*>( |
| 1633 | reinterpret_cast<uint8_t*>(head.buffer()->pointer()) |
| 1634 | + head.buffer()->size()); |
| 1635 | |
| 1636 | ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end), |
| 1637 | "released buffer not within the head of the timed buffer" |
| 1638 | " queue; qHead = [%p, %p], released buffer = %p", |
| 1639 | start, end, buffer->raw); |
| 1640 | |
| 1641 | head.setPosition(head.position() + |
| 1642 | (buffer->frameCount * mFrameSize)); |
| 1643 | mQueueHeadInFlight = false; |
| 1644 | |
| 1645 | ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount, |
| 1646 | "Bad bookkeeping during releaseBuffer! Should have at" |
| 1647 | " least %u queued frames, but we think we have only %u", |
| 1648 | buffer->frameCount, mFramesPendingInQueue); |
| 1649 | |
| 1650 | mFramesPendingInQueue -= buffer->frameCount; |
| 1651 | |
| 1652 | if ((static_cast<size_t>(head.position()) >= head.buffer()->size()) |
| 1653 | || mTrimQueueHeadOnRelease) { |
| 1654 | trimTimedBufferQueueHead_l("releaseBuffer"); |
| 1655 | mTrimQueueHeadOnRelease = false; |
| 1656 | } |
| 1657 | } else { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1658 | LOG_ALWAYS_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 | " buffers in the timed buffer queue"); |
| 1660 | } |
| 1661 | |
| 1662 | done: |
| 1663 | buffer->raw = 0; |
| 1664 | buffer->frameCount = 0; |
| 1665 | } |
| 1666 | |
| 1667 | size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const { |
| 1668 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 1669 | return mFramesPendingInQueue; |
| 1670 | } |
| 1671 | |
| 1672 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer() |
| 1673 | : mPTS(0), mPosition(0) {} |
| 1674 | |
| 1675 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer( |
| 1676 | const sp<IMemory>& buffer, int64_t pts) |
| 1677 | : mBuffer(buffer), mPTS(pts), mPosition(0) {} |
| 1678 | |
| 1679 | |
| 1680 | // ---------------------------------------------------------------------------- |
| 1681 | |
| 1682 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 1683 | PlaybackThread *playbackThread, |
| 1684 | DuplicatingThread *sourceThread, |
| 1685 | uint32_t sampleRate, |
| 1686 | audio_format_t format, |
| 1687 | audio_channel_mask_t channelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1688 | size_t frameCount, |
| 1689 | int uid) |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1690 | : Track(playbackThread, NULL, AUDIO_STREAM_PATCH, |
| 1691 | sampleRate, format, channelMask, frameCount, |
| 1692 | NULL, 0, 0, uid, IAudioFlinger::TRACK_DEFAULT, TYPE_OUTPUT), |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1693 | mActive(false), mSourceThread(sourceThread), mClientProxy(NULL) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1694 | { |
| 1695 | |
| 1696 | if (mCblk != NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1697 | mOutBuffer.frameCount = 0; |
| 1698 | playbackThread->mTracks.add(this); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1699 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, " |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1700 | "frameCount %u, mChannelMask 0x%08x", |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1701 | mCblk, mBuffer, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1702 | frameCount, mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1703 | // since client and server are in the same process, |
| 1704 | // the buffer has the same virtual address on both sides |
Glenn Kasten | 529c61b | 2014-07-18 15:31:02 -0700 | [diff] [blame] | 1705 | mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize, |
| 1706 | true /*clientInServer*/); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1707 | mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY); |
Eric Laurent | 8d2d493 | 2013-04-25 12:56:18 -0700 | [diff] [blame] | 1708 | mClientProxy->setSendLevel(0.0); |
| 1709 | mClientProxy->setSampleRate(sampleRate); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1710 | } else { |
| 1711 | ALOGW("Error creating output track on thread %p", playbackThread); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 1716 | { |
| 1717 | clearBufferQueue(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1718 | delete mClientProxy; |
| 1719 | // 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] | 1720 | } |
| 1721 | |
| 1722 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, |
| 1723 | int triggerSession) |
| 1724 | { |
| 1725 | status_t status = Track::start(event, triggerSession); |
| 1726 | if (status != NO_ERROR) { |
| 1727 | return status; |
| 1728 | } |
| 1729 | |
| 1730 | mActive = true; |
| 1731 | mRetryCount = 127; |
| 1732 | return status; |
| 1733 | } |
| 1734 | |
| 1735 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 1736 | { |
| 1737 | Track::stop(); |
| 1738 | clearBufferQueue(); |
| 1739 | mOutBuffer.frameCount = 0; |
| 1740 | mActive = false; |
| 1741 | } |
| 1742 | |
| 1743 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 1744 | { |
| 1745 | Buffer *pInBuffer; |
| 1746 | Buffer inBuffer; |
| 1747 | uint32_t channelCount = mChannelCount; |
| 1748 | bool outputBufferFull = false; |
| 1749 | inBuffer.frameCount = frames; |
| 1750 | inBuffer.i16 = data; |
| 1751 | |
| 1752 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 1753 | |
| 1754 | if (!mActive && frames != 0) { |
| 1755 | start(); |
| 1756 | sp<ThreadBase> thread = mThread.promote(); |
| 1757 | if (thread != 0) { |
| 1758 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 1759 | if (mFrameCount > frames) { |
| 1760 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 1761 | uint32_t startFrames = (mFrameCount - frames); |
| 1762 | pInBuffer = new Buffer; |
| 1763 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 1764 | pInBuffer->frameCount = startFrames; |
| 1765 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 1766 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 1767 | mBufferQueue.add(pInBuffer); |
| 1768 | } else { |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 1769 | ALOGW("OutputTrack::write() %p no more buffers in queue", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | while (waitTimeLeftMs) { |
| 1776 | // First write pending buffers, then new data |
| 1777 | if (mBufferQueue.size()) { |
| 1778 | pInBuffer = mBufferQueue.itemAt(0); |
| 1779 | } else { |
| 1780 | pInBuffer = &inBuffer; |
| 1781 | } |
| 1782 | |
| 1783 | if (pInBuffer->frameCount == 0) { |
| 1784 | break; |
| 1785 | } |
| 1786 | |
| 1787 | if (mOutBuffer.frameCount == 0) { |
| 1788 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 1789 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1790 | status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs); |
| 1791 | if (status != NO_ERROR) { |
| 1792 | ALOGV("OutputTrack::write() %p thread %p no more output buffers; status %d", this, |
| 1793 | mThread.unsafe_get(), status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1794 | outputBufferFull = true; |
| 1795 | break; |
| 1796 | } |
| 1797 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 1798 | if (waitTimeLeftMs >= waitTimeMs) { |
| 1799 | waitTimeLeftMs -= waitTimeMs; |
| 1800 | } else { |
| 1801 | waitTimeLeftMs = 0; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : |
| 1806 | pInBuffer->frameCount; |
| 1807 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1808 | Proxy::Buffer buf; |
| 1809 | buf.mFrameCount = outFrames; |
| 1810 | buf.mRaw = NULL; |
| 1811 | mClientProxy->releaseBuffer(&buf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1812 | pInBuffer->frameCount -= outFrames; |
| 1813 | pInBuffer->i16 += outFrames * channelCount; |
| 1814 | mOutBuffer.frameCount -= outFrames; |
| 1815 | mOutBuffer.i16 += outFrames * channelCount; |
| 1816 | |
| 1817 | if (pInBuffer->frameCount == 0) { |
| 1818 | if (mBufferQueue.size()) { |
| 1819 | mBufferQueue.removeAt(0); |
| 1820 | delete [] pInBuffer->mBuffer; |
| 1821 | delete pInBuffer; |
| 1822 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, |
| 1823 | mThread.unsafe_get(), mBufferQueue.size()); |
| 1824 | } else { |
| 1825 | break; |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 1831 | if (inBuffer.frameCount) { |
| 1832 | sp<ThreadBase> thread = mThread.promote(); |
| 1833 | if (thread != 0 && !thread->standby()) { |
| 1834 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 1835 | pInBuffer = new Buffer; |
| 1836 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 1837 | pInBuffer->frameCount = inBuffer.frameCount; |
| 1838 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 1839 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * |
| 1840 | sizeof(int16_t)); |
| 1841 | mBufferQueue.add(pInBuffer); |
| 1842 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, |
| 1843 | mThread.unsafe_get(), mBufferQueue.size()); |
| 1844 | } else { |
| 1845 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", |
| 1846 | mThread.unsafe_get(), this); |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 1852 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 1853 | // by output mixer. |
| 1854 | if (frames == 0 && mBufferQueue.size() == 0) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1855 | // FIXME borken, replace by getting framesReady() from proxy |
| 1856 | size_t user = 0; // was mCblk->user |
| 1857 | if (user < mFrameCount) { |
| 1858 | frames = mFrameCount - user; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1859 | pInBuffer = new Buffer; |
| 1860 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 1861 | pInBuffer->frameCount = frames; |
| 1862 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 1863 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 1864 | mBufferQueue.add(pInBuffer); |
| 1865 | } else if (mActive) { |
| 1866 | stop(); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | return outputBufferFull; |
| 1871 | } |
| 1872 | |
| 1873 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer( |
| 1874 | AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 1875 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1876 | ClientProxy::Buffer buf; |
| 1877 | buf.mFrameCount = buffer->frameCount; |
| 1878 | struct timespec timeout; |
| 1879 | timeout.tv_sec = waitTimeMs / 1000; |
| 1880 | timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000; |
| 1881 | status_t status = mClientProxy->obtainBuffer(&buf, &timeout); |
| 1882 | buffer->frameCount = buf.mFrameCount; |
| 1883 | buffer->raw = buf.mRaw; |
| 1884 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1885 | } |
| 1886 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1887 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 1888 | { |
| 1889 | size_t size = mBufferQueue.size(); |
| 1890 | |
| 1891 | for (size_t i = 0; i < size; i++) { |
| 1892 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
| 1893 | delete [] pBuffer->mBuffer; |
| 1894 | delete pBuffer; |
| 1895 | } |
| 1896 | mBufferQueue.clear(); |
| 1897 | } |
| 1898 | |
| 1899 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1900 | AudioFlinger::PlaybackThread::PatchTrack::PatchTrack(PlaybackThread *playbackThread, |
| 1901 | uint32_t sampleRate, |
| 1902 | audio_channel_mask_t channelMask, |
| 1903 | audio_format_t format, |
| 1904 | size_t frameCount, |
| 1905 | void *buffer, |
| 1906 | IAudioFlinger::track_flags_t flags) |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1907 | : Track(playbackThread, NULL, AUDIO_STREAM_PATCH, |
| 1908 | sampleRate, format, channelMask, frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1909 | buffer, 0, 0, getuid(), flags, TYPE_PATCH), |
| 1910 | mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true)) |
| 1911 | { |
| 1912 | uint64_t mixBufferNs = ((uint64_t)2 * playbackThread->frameCount() * 1000000000) / |
| 1913 | playbackThread->sampleRate(); |
| 1914 | mPeerTimeout.tv_sec = mixBufferNs / 1000000000; |
| 1915 | mPeerTimeout.tv_nsec = (int) (mixBufferNs % 1000000000); |
| 1916 | |
| 1917 | ALOGV("PatchTrack %p sampleRate %d mPeerTimeout %d.%03d sec", |
| 1918 | this, sampleRate, |
| 1919 | (int)mPeerTimeout.tv_sec, |
| 1920 | (int)(mPeerTimeout.tv_nsec / 1000000)); |
| 1921 | } |
| 1922 | |
| 1923 | AudioFlinger::PlaybackThread::PatchTrack::~PatchTrack() |
| 1924 | { |
| 1925 | } |
| 1926 | |
| 1927 | // AudioBufferProvider interface |
| 1928 | status_t AudioFlinger::PlaybackThread::PatchTrack::getNextBuffer( |
| 1929 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 1930 | { |
| 1931 | ALOG_ASSERT(mPeerProxy != 0, "PatchTrack::getNextBuffer() called without peer proxy"); |
| 1932 | Proxy::Buffer buf; |
| 1933 | buf.mFrameCount = buffer->frameCount; |
| 1934 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); |
| 1935 | ALOGV_IF(status != NO_ERROR, "PatchTrack() %p getNextBuffer status %d", this, status); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 1936 | buffer->frameCount = buf.mFrameCount; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1937 | if (buf.mFrameCount == 0) { |
| 1938 | return WOULD_BLOCK; |
| 1939 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1940 | status = Track::getNextBuffer(buffer, pts); |
| 1941 | return status; |
| 1942 | } |
| 1943 | |
| 1944 | void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 1945 | { |
| 1946 | ALOG_ASSERT(mPeerProxy != 0, "PatchTrack::releaseBuffer() called without peer proxy"); |
| 1947 | Proxy::Buffer buf; |
| 1948 | buf.mFrameCount = buffer->frameCount; |
| 1949 | buf.mRaw = buffer->raw; |
| 1950 | mPeerProxy->releaseBuffer(&buf); |
| 1951 | TrackBase::releaseBuffer(buffer); |
| 1952 | } |
| 1953 | |
| 1954 | status_t AudioFlinger::PlaybackThread::PatchTrack::obtainBuffer(Proxy::Buffer* buffer, |
| 1955 | const struct timespec *timeOut) |
| 1956 | { |
| 1957 | return mProxy->obtainBuffer(buffer, timeOut); |
| 1958 | } |
| 1959 | |
| 1960 | void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(Proxy::Buffer* buffer) |
| 1961 | { |
| 1962 | mProxy->releaseBuffer(buffer); |
| 1963 | if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) { |
| 1964 | ALOGW("PatchTrack::releaseBuffer() disabled due to previous underrun, restarting"); |
| 1965 | start(); |
| 1966 | } |
| 1967 | android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags); |
| 1968 | } |
| 1969 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1970 | // ---------------------------------------------------------------------------- |
| 1971 | // Record |
| 1972 | // ---------------------------------------------------------------------------- |
| 1973 | |
| 1974 | AudioFlinger::RecordHandle::RecordHandle( |
| 1975 | const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 1976 | : BnAudioRecord(), |
| 1977 | mRecordTrack(recordTrack) |
| 1978 | { |
| 1979 | } |
| 1980 | |
| 1981 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 1982 | stop_nonvirtual(); |
| 1983 | mRecordTrack->destroy(); |
| 1984 | } |
| 1985 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1986 | status_t AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event, |
| 1987 | int triggerSession) { |
| 1988 | ALOGV("RecordHandle::start()"); |
| 1989 | return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession); |
| 1990 | } |
| 1991 | |
| 1992 | void AudioFlinger::RecordHandle::stop() { |
| 1993 | stop_nonvirtual(); |
| 1994 | } |
| 1995 | |
| 1996 | void AudioFlinger::RecordHandle::stop_nonvirtual() { |
| 1997 | ALOGV("RecordHandle::stop()"); |
| 1998 | mRecordTrack->stop(); |
| 1999 | } |
| 2000 | |
| 2001 | status_t AudioFlinger::RecordHandle::onTransact( |
| 2002 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 2003 | { |
| 2004 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 2005 | } |
| 2006 | |
| 2007 | // ---------------------------------------------------------------------------- |
| 2008 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 2009 | // RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2010 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 2011 | RecordThread *thread, |
| 2012 | const sp<Client>& client, |
| 2013 | uint32_t sampleRate, |
| 2014 | audio_format_t format, |
| 2015 | audio_channel_mask_t channelMask, |
| 2016 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2017 | void *buffer, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2018 | int sessionId, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 2019 | int uid, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2020 | IAudioFlinger::track_flags_t flags, |
| 2021 | track_type type) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2022 | : TrackBase(thread, client, sampleRate, format, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2023 | channelMask, frameCount, buffer, sessionId, uid, |
Glenn Kasten | 755b0a6 | 2014-05-13 11:30:28 -0700 | [diff] [blame] | 2024 | flags, false /*isOut*/, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2025 | (type == TYPE_DEFAULT) ? |
| 2026 | ((flags & IAudioFlinger::TRACK_FAST) ? ALLOC_PIPE : ALLOC_CBLK) : |
| 2027 | ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE), |
| 2028 | type), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2029 | mOverflow(false), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpOutFrameCount(0), |
| 2030 | // See real initialization of mRsmpInFront at RecordThread::start() |
| 2031 | mRsmpInUnrel(0), mRsmpInFront(0), mFramesToDrop(0), mResamplerBufferProvider(NULL) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2032 | { |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 2033 | if (mCblk == NULL) { |
| 2034 | return; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2035 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2036 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2037 | mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount, |
| 2038 | mFrameSize, !isExternalTrack()); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 2039 | |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2040 | uint32_t channelCount = audio_channel_count_from_in_mask(channelMask); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2041 | // FIXME I don't understand either of the channel count checks |
| 2042 | if (thread->mSampleRate != sampleRate && thread->mChannelCount <= FCC_2 && |
| 2043 | channelCount <= FCC_2) { |
| 2044 | // sink SR |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 2045 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_16_BIT, |
| 2046 | thread->mChannelCount, sampleRate); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2047 | // source SR |
| 2048 | mResampler->setSampleRate(thread->mSampleRate); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 2049 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2050 | mResamplerBufferProvider = new ResamplerBufferProvider(this); |
| 2051 | } |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 2052 | |
| 2053 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 2054 | ALOG_ASSERT(thread->mFastTrackAvail); |
| 2055 | thread->mFastTrackAvail = false; |
| 2056 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 2060 | { |
| 2061 | ALOGV("%s", __func__); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2062 | delete mResampler; |
| 2063 | delete[] mRsmpOutBuffer; |
| 2064 | delete mResamplerBufferProvider; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | // AudioBufferProvider interface |
| 2068 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2069 | int64_t pts __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2070 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2071 | ServerProxy::Buffer buf; |
| 2072 | buf.mFrameCount = buffer->frameCount; |
| 2073 | status_t status = mServerProxy->obtainBuffer(&buf); |
| 2074 | buffer->frameCount = buf.mFrameCount; |
| 2075 | buffer->raw = buf.mRaw; |
| 2076 | if (buf.mFrameCount == 0) { |
| 2077 | // FIXME also wake futex so that overrun is noticed more quickly |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 2078 | (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2079 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2080 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, |
| 2084 | int triggerSession) |
| 2085 | { |
| 2086 | sp<ThreadBase> thread = mThread.promote(); |
| 2087 | if (thread != 0) { |
| 2088 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 2089 | return recordThread->start(this, event, triggerSession); |
| 2090 | } else { |
| 2091 | return BAD_VALUE; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 2096 | { |
| 2097 | sp<ThreadBase> thread = mThread.promote(); |
| 2098 | if (thread != 0) { |
| 2099 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2100 | if (recordThread->stop(this) && isExternalTrack()) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2101 | AudioSystem::stopInput(mThreadIoHandle, (audio_session_t)mSessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | void AudioFlinger::RecordThread::RecordTrack::destroy() |
| 2107 | { |
| 2108 | // see comments at AudioFlinger::PlaybackThread::Track::destroy() |
| 2109 | sp<RecordTrack> keep(this); |
| 2110 | { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2111 | if (isExternalTrack()) { |
| 2112 | if (mState == ACTIVE || mState == RESUMING) { |
| 2113 | AudioSystem::stopInput(mThreadIoHandle, (audio_session_t)mSessionId); |
| 2114 | } |
| 2115 | AudioSystem::releaseInput(mThreadIoHandle, (audio_session_t)mSessionId); |
| 2116 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2117 | sp<ThreadBase> thread = mThread.promote(); |
| 2118 | if (thread != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2119 | Mutex::Autolock _l(thread->mLock); |
| 2120 | RecordThread *recordThread = (RecordThread *) thread.get(); |
| 2121 | recordThread->destroyTrack_l(this); |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2126 | void AudioFlinger::RecordThread::RecordTrack::invalidate() |
| 2127 | { |
| 2128 | // FIXME should use proxy, and needs work |
| 2129 | audio_track_cblk_t* cblk = mCblk; |
| 2130 | android_atomic_or(CBLK_INVALID, &cblk->mFlags); |
| 2131 | android_atomic_release_store(0x40000000, &cblk->mFutex); |
| 2132 | // 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] | 2133 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2136 | |
| 2137 | /*static*/ void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result) |
| 2138 | { |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 2139 | 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] | 2140 | } |
| 2141 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2142 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size, bool active) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2143 | { |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 2144 | 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] | 2145 | active ? "yes" : "no", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2146 | (mClient == 0) ? getpid_cached : mClient->pid(), |
| 2147 | mFormat, |
| 2148 | mChannelMask, |
| 2149 | mSessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2150 | mState, |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 2151 | mCblk->mServer, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2152 | mFrameCount, |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 2153 | mSampleRate); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2154 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2155 | } |
| 2156 | |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2157 | void AudioFlinger::RecordThread::RecordTrack::handleSyncStartEvent(const sp<SyncEvent>& event) |
| 2158 | { |
| 2159 | if (event == mSyncStartEvent) { |
| 2160 | ssize_t framesToDrop = 0; |
| 2161 | sp<ThreadBase> threadBase = mThread.promote(); |
| 2162 | if (threadBase != 0) { |
| 2163 | // TODO: use actual buffer filling status instead of 2 buffers when info is available |
| 2164 | // from audio HAL |
| 2165 | framesToDrop = threadBase->mFrameCount * 2; |
| 2166 | } |
| 2167 | mFramesToDrop = framesToDrop; |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | void AudioFlinger::RecordThread::RecordTrack::clearSyncStartEvent() |
| 2172 | { |
| 2173 | if (mSyncStartEvent != 0) { |
| 2174 | mSyncStartEvent->cancel(); |
| 2175 | mSyncStartEvent.clear(); |
| 2176 | } |
| 2177 | mFramesToDrop = 0; |
| 2178 | } |
| 2179 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2180 | |
| 2181 | AudioFlinger::RecordThread::PatchRecord::PatchRecord(RecordThread *recordThread, |
| 2182 | uint32_t sampleRate, |
| 2183 | audio_channel_mask_t channelMask, |
| 2184 | audio_format_t format, |
| 2185 | size_t frameCount, |
| 2186 | void *buffer, |
| 2187 | IAudioFlinger::track_flags_t flags) |
| 2188 | : RecordTrack(recordThread, NULL, sampleRate, format, channelMask, frameCount, |
| 2189 | buffer, 0, getuid(), flags, TYPE_PATCH), |
| 2190 | mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true)) |
| 2191 | { |
| 2192 | uint64_t mixBufferNs = ((uint64_t)2 * recordThread->frameCount() * 1000000000) / |
| 2193 | recordThread->sampleRate(); |
| 2194 | mPeerTimeout.tv_sec = mixBufferNs / 1000000000; |
| 2195 | mPeerTimeout.tv_nsec = (int) (mixBufferNs % 1000000000); |
| 2196 | |
| 2197 | ALOGV("PatchRecord %p sampleRate %d mPeerTimeout %d.%03d sec", |
| 2198 | this, sampleRate, |
| 2199 | (int)mPeerTimeout.tv_sec, |
| 2200 | (int)(mPeerTimeout.tv_nsec / 1000000)); |
| 2201 | } |
| 2202 | |
| 2203 | AudioFlinger::RecordThread::PatchRecord::~PatchRecord() |
| 2204 | { |
| 2205 | } |
| 2206 | |
| 2207 | // AudioBufferProvider interface |
| 2208 | status_t AudioFlinger::RecordThread::PatchRecord::getNextBuffer( |
| 2209 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 2210 | { |
| 2211 | ALOG_ASSERT(mPeerProxy != 0, "PatchRecord::getNextBuffer() called without peer proxy"); |
| 2212 | Proxy::Buffer buf; |
| 2213 | buf.mFrameCount = buffer->frameCount; |
| 2214 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); |
| 2215 | ALOGV_IF(status != NO_ERROR, |
| 2216 | "PatchRecord() %p mPeerProxy->obtainBuffer status %d", this, status); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2217 | buffer->frameCount = buf.mFrameCount; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2218 | if (buf.mFrameCount == 0) { |
| 2219 | return WOULD_BLOCK; |
| 2220 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2221 | status = RecordTrack::getNextBuffer(buffer, pts); |
| 2222 | return status; |
| 2223 | } |
| 2224 | |
| 2225 | void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 2226 | { |
| 2227 | ALOG_ASSERT(mPeerProxy != 0, "PatchRecord::releaseBuffer() called without peer proxy"); |
| 2228 | Proxy::Buffer buf; |
| 2229 | buf.mFrameCount = buffer->frameCount; |
| 2230 | buf.mRaw = buffer->raw; |
| 2231 | mPeerProxy->releaseBuffer(&buf); |
| 2232 | TrackBase::releaseBuffer(buffer); |
| 2233 | } |
| 2234 | |
| 2235 | status_t AudioFlinger::RecordThread::PatchRecord::obtainBuffer(Proxy::Buffer* buffer, |
| 2236 | const struct timespec *timeOut) |
| 2237 | { |
| 2238 | return mProxy->obtainBuffer(buffer, timeOut); |
| 2239 | } |
| 2240 | |
| 2241 | void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(Proxy::Buffer* buffer) |
| 2242 | { |
| 2243 | mProxy->releaseBuffer(buffer); |
| 2244 | } |
| 2245 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2246 | }; // namespace android |