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