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