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