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