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