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 | #ifndef INCLUDING_FROM_AUDIOFLINGER_H |
| 19 | #error This header file should only be included from AudioFlinger.h |
| 20 | #endif |
| 21 | |
| 22 | class ThreadBase : public Thread { |
| 23 | public: |
| 24 | |
| 25 | #include "TrackBase.h" |
| 26 | |
| 27 | enum type_t { |
| 28 | MIXER, // Thread class is MixerThread |
| 29 | DIRECT, // Thread class is DirectOutputThread |
| 30 | DUPLICATING, // Thread class is DuplicatingThread |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 31 | RECORD, // Thread class is RecordThread |
| 32 | OFFLOAD // Thread class is OffloadThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 36 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type); |
| 37 | virtual ~ThreadBase(); |
| 38 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 39 | virtual status_t readyToRun(); |
| 40 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 41 | void dumpBase(int fd, const Vector<String16>& args); |
| 42 | void dumpEffectChains(int fd, const Vector<String16>& args); |
| 43 | |
| 44 | void clearPowerManager(); |
| 45 | |
| 46 | // base for record and playback |
| 47 | enum { |
| 48 | CFG_EVENT_IO, |
| 49 | CFG_EVENT_PRIO |
| 50 | }; |
| 51 | |
| 52 | class ConfigEvent { |
| 53 | public: |
| 54 | ConfigEvent(int type) : mType(type) {} |
| 55 | virtual ~ConfigEvent() {} |
| 56 | |
| 57 | int type() const { return mType; } |
| 58 | |
| 59 | virtual void dump(char *buffer, size_t size) = 0; |
| 60 | |
| 61 | private: |
| 62 | const int mType; |
| 63 | }; |
| 64 | |
| 65 | class IoConfigEvent : public ConfigEvent { |
| 66 | public: |
| 67 | IoConfigEvent(int event, int param) : |
Glenn Kasten | 49debee | 2014-01-31 16:54:54 -0800 | [diff] [blame^] | 68 | ConfigEvent(CFG_EVENT_IO), mEvent(event), mParam(param) {} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 69 | virtual ~IoConfigEvent() {} |
| 70 | |
| 71 | int event() const { return mEvent; } |
| 72 | int param() const { return mParam; } |
| 73 | |
| 74 | virtual void dump(char *buffer, size_t size) { |
| 75 | snprintf(buffer, size, "IO event: event %d, param %d\n", mEvent, mParam); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | const int mEvent; |
| 80 | const int mParam; |
| 81 | }; |
| 82 | |
| 83 | class PrioConfigEvent : public ConfigEvent { |
| 84 | public: |
| 85 | PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) : |
| 86 | ConfigEvent(CFG_EVENT_PRIO), mPid(pid), mTid(tid), mPrio(prio) {} |
| 87 | virtual ~PrioConfigEvent() {} |
| 88 | |
| 89 | pid_t pid() const { return mPid; } |
| 90 | pid_t tid() const { return mTid; } |
| 91 | int32_t prio() const { return mPrio; } |
| 92 | |
| 93 | virtual void dump(char *buffer, size_t size) { |
| 94 | snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | const pid_t mPid; |
| 99 | const pid_t mTid; |
| 100 | const int32_t mPrio; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | class PMDeathRecipient : public IBinder::DeathRecipient { |
| 105 | public: |
| 106 | PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {} |
| 107 | virtual ~PMDeathRecipient() {} |
| 108 | |
| 109 | // IBinder::DeathRecipient |
| 110 | virtual void binderDied(const wp<IBinder>& who); |
| 111 | |
| 112 | private: |
| 113 | PMDeathRecipient(const PMDeathRecipient&); |
| 114 | PMDeathRecipient& operator = (const PMDeathRecipient&); |
| 115 | |
| 116 | wp<ThreadBase> mThread; |
| 117 | }; |
| 118 | |
| 119 | virtual status_t initCheck() const = 0; |
| 120 | |
| 121 | // static externally-visible |
| 122 | type_t type() const { return mType; } |
| 123 | audio_io_handle_t id() const { return mId;} |
| 124 | |
| 125 | // dynamic externally-visible |
| 126 | uint32_t sampleRate() const { return mSampleRate; } |
| 127 | uint32_t channelCount() const { return mChannelCount; } |
| 128 | audio_channel_mask_t channelMask() const { return mChannelMask; } |
| 129 | audio_format_t format() const { return mFormat; } |
| 130 | // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects, |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 131 | // and returns the [normal mix] buffer's frame count. |
| 132 | virtual size_t frameCount() const = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 133 | size_t frameSize() const { return mFrameSize; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 134 | |
| 135 | // Should be "virtual status_t requestExitAndWait()" and override same |
| 136 | // method in Thread, but Thread::requestExitAndWait() is not yet virtual. |
| 137 | void exit(); |
| 138 | virtual bool checkForNewParameters_l() = 0; |
| 139 | virtual status_t setParameters(const String8& keyValuePairs); |
| 140 | virtual String8 getParameters(const String8& keys) = 0; |
| 141 | virtual void audioConfigChanged_l(int event, int param = 0) = 0; |
| 142 | void sendIoConfigEvent(int event, int param = 0); |
| 143 | void sendIoConfigEvent_l(int event, int param = 0); |
| 144 | void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio); |
| 145 | void processConfigEvents(); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 146 | void processConfigEvents_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 147 | |
| 148 | // see note at declaration of mStandby, mOutDevice and mInDevice |
| 149 | bool standby() const { return mStandby; } |
| 150 | audio_devices_t outDevice() const { return mOutDevice; } |
| 151 | audio_devices_t inDevice() const { return mInDevice; } |
| 152 | |
| 153 | virtual audio_stream_t* stream() const = 0; |
| 154 | |
| 155 | sp<EffectHandle> createEffect_l( |
| 156 | const sp<AudioFlinger::Client>& client, |
| 157 | const sp<IEffectClient>& effectClient, |
| 158 | int32_t priority, |
| 159 | int sessionId, |
| 160 | effect_descriptor_t *desc, |
| 161 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 162 | status_t *status /*non-NULL*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 163 | void disconnectEffect(const sp< EffectModule>& effect, |
| 164 | EffectHandle *handle, |
| 165 | bool unpinIfLast); |
| 166 | |
| 167 | // return values for hasAudioSession (bit field) |
| 168 | enum effect_state { |
| 169 | EFFECT_SESSION = 0x1, // the audio session corresponds to at least one |
| 170 | // effect |
| 171 | TRACK_SESSION = 0x2 // the audio session corresponds to at least one |
| 172 | // track |
| 173 | }; |
| 174 | |
| 175 | // get effect chain corresponding to session Id. |
| 176 | sp<EffectChain> getEffectChain(int sessionId); |
| 177 | // same as getEffectChain() but must be called with ThreadBase mutex locked |
| 178 | sp<EffectChain> getEffectChain_l(int sessionId) const; |
| 179 | // add an effect chain to the chain list (mEffectChains) |
| 180 | virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0; |
| 181 | // remove an effect chain from the chain list (mEffectChains) |
| 182 | virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0; |
| 183 | // lock all effect chains Mutexes. Must be called before releasing the |
| 184 | // ThreadBase mutex before processing the mixer and effects. This guarantees the |
| 185 | // integrity of the chains during the process. |
| 186 | // Also sets the parameter 'effectChains' to current value of mEffectChains. |
| 187 | void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains); |
| 188 | // unlock effect chains after process |
| 189 | void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 190 | // get a copy of mEffectChains vector |
| 191 | Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; }; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 192 | // set audio mode to all effect chains |
| 193 | void setMode(audio_mode_t mode); |
| 194 | // get effect module with corresponding ID on specified audio session |
| 195 | sp<AudioFlinger::EffectModule> getEffect(int sessionId, int effectId); |
| 196 | sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId); |
| 197 | // add and effect module. Also creates the effect chain is none exists for |
| 198 | // the effects audio session |
| 199 | status_t addEffect_l(const sp< EffectModule>& effect); |
| 200 | // remove and effect module. Also removes the effect chain is this was the last |
| 201 | // effect |
| 202 | void removeEffect_l(const sp< EffectModule>& effect); |
| 203 | // detach all tracks connected to an auxiliary effect |
| 204 | virtual void detachAuxEffect_l(int effectId) {} |
| 205 | // returns either EFFECT_SESSION if effects on this audio session exist in one |
| 206 | // chain, or TRACK_SESSION if tracks on this audio session exist, or both |
| 207 | virtual uint32_t hasAudioSession(int sessionId) const = 0; |
| 208 | // the value returned by default implementation is not important as the |
| 209 | // strategy is only meaningful for PlaybackThread which implements this method |
| 210 | virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; } |
| 211 | |
| 212 | // suspend or restore effect according to the type of effect passed. a NULL |
| 213 | // type pointer means suspend all effects in the session |
| 214 | void setEffectSuspended(const effect_uuid_t *type, |
| 215 | bool suspend, |
| 216 | int sessionId = AUDIO_SESSION_OUTPUT_MIX); |
| 217 | // check if some effects must be suspended/restored when an effect is enabled |
| 218 | // or disabled |
| 219 | void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 220 | bool enabled, |
| 221 | int sessionId = AUDIO_SESSION_OUTPUT_MIX); |
| 222 | void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 223 | bool enabled, |
| 224 | int sessionId = AUDIO_SESSION_OUTPUT_MIX); |
| 225 | |
| 226 | virtual status_t setSyncEvent(const sp<SyncEvent>& event) = 0; |
| 227 | virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const = 0; |
| 228 | |
| 229 | |
| 230 | mutable Mutex mLock; |
| 231 | |
| 232 | protected: |
| 233 | |
| 234 | // entry describing an effect being suspended in mSuspendedSessions keyed vector |
| 235 | class SuspendedSessionDesc : public RefBase { |
| 236 | public: |
| 237 | SuspendedSessionDesc() : mRefCount(0) {} |
| 238 | |
| 239 | int mRefCount; // number of active suspend requests |
| 240 | effect_uuid_t mType; // effect type UUID |
| 241 | }; |
| 242 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 243 | void acquireWakeLock(int uid = -1); |
| 244 | void acquireWakeLock_l(int uid = -1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 245 | void releaseWakeLock(); |
| 246 | void releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 247 | void updateWakeLockUids(const SortedVector<int> &uids); |
| 248 | void updateWakeLockUids_l(const SortedVector<int> &uids); |
| 249 | void getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 250 | void setEffectSuspended_l(const effect_uuid_t *type, |
| 251 | bool suspend, |
| 252 | int sessionId); |
| 253 | // updated mSuspendedSessions when an effect suspended or restored |
| 254 | void updateSuspendedSessions_l(const effect_uuid_t *type, |
| 255 | bool suspend, |
| 256 | int sessionId); |
| 257 | // check if some effects must be suspended when an effect chain is added |
| 258 | void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain); |
| 259 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 260 | String16 getWakeLockTag(); |
| 261 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 262 | virtual void preExit() { } |
| 263 | |
| 264 | friend class AudioFlinger; // for mEffectChains |
| 265 | |
| 266 | const type_t mType; |
| 267 | |
| 268 | // Used by parameters, config events, addTrack_l, exit |
| 269 | Condition mWaitWorkCV; |
| 270 | |
| 271 | const sp<AudioFlinger> mAudioFlinger; |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 272 | |
| 273 | // updated by PlaybackThread::readOutputParameters() or |
| 274 | // RecordThread::readInputParameters() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 275 | uint32_t mSampleRate; |
| 276 | size_t mFrameCount; // output HAL, direct output, record |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 277 | audio_channel_mask_t mChannelMask; |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 278 | uint32_t mChannelCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 279 | size_t mFrameSize; |
| 280 | audio_format_t mFormat; |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 281 | size_t mBufferSize; // HAL buffer size for read() or write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 282 | |
| 283 | // Parameter sequence by client: binder thread calling setParameters(): |
| 284 | // 1. Lock mLock |
| 285 | // 2. Append to mNewParameters |
| 286 | // 3. mWaitWorkCV.signal |
| 287 | // 4. mParamCond.waitRelative with timeout |
| 288 | // 5. read mParamStatus |
| 289 | // 6. mWaitWorkCV.signal |
| 290 | // 7. Unlock |
| 291 | // |
| 292 | // Parameter sequence by server: threadLoop calling checkForNewParameters_l(): |
| 293 | // 1. Lock mLock |
| 294 | // 2. If there is an entry in mNewParameters proceed ... |
| 295 | // 2. Read first entry in mNewParameters |
| 296 | // 3. Process |
| 297 | // 4. Remove first entry from mNewParameters |
| 298 | // 5. Set mParamStatus |
| 299 | // 6. mParamCond.signal |
| 300 | // 7. mWaitWorkCV.wait with timeout (this is to avoid overwriting mParamStatus) |
| 301 | // 8. Unlock |
| 302 | Condition mParamCond; |
| 303 | Vector<String8> mNewParameters; |
| 304 | status_t mParamStatus; |
| 305 | |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 306 | // vector owns each ConfigEvent *, so must delete after removing |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 307 | Vector<ConfigEvent *> mConfigEvents; |
| 308 | |
| 309 | // These fields are written and read by thread itself without lock or barrier, |
| 310 | // and read by other threads without lock or barrier via standby() , outDevice() |
| 311 | // and inDevice(). |
| 312 | // Because of the absence of a lock or barrier, any other thread that reads |
| 313 | // these fields must use the information in isolation, or be prepared to deal |
| 314 | // with possibility that it might be inconsistent with other information. |
| 315 | bool mStandby; // Whether thread is currently in standby. |
| 316 | audio_devices_t mOutDevice; // output device |
| 317 | audio_devices_t mInDevice; // input device |
| 318 | audio_source_t mAudioSource; // (see audio.h, audio_source_t) |
| 319 | |
| 320 | const audio_io_handle_t mId; |
| 321 | Vector< sp<EffectChain> > mEffectChains; |
| 322 | |
| 323 | static const int kNameLength = 16; // prctl(PR_SET_NAME) limit |
| 324 | char mName[kNameLength]; |
| 325 | sp<IPowerManager> mPowerManager; |
| 326 | sp<IBinder> mWakeLockToken; |
| 327 | const sp<PMDeathRecipient> mDeathRecipient; |
| 328 | // list of suspended effects per session and per type. The first vector is |
| 329 | // keyed by session ID, the second by type UUID timeLow field |
| 330 | KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > > |
| 331 | mSuspendedSessions; |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 332 | static const size_t kLogSize = 4 * 1024; |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 333 | sp<NBLog::Writer> mNBLogWriter; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | // --- PlaybackThread --- |
| 337 | class PlaybackThread : public ThreadBase { |
| 338 | public: |
| 339 | |
| 340 | #include "PlaybackTracks.h" |
| 341 | |
| 342 | enum mixer_state { |
| 343 | MIXER_IDLE, // no active tracks |
| 344 | MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 345 | MIXER_TRACKS_READY, // at least one active track, and at least one track has data |
| 346 | MIXER_DRAIN_TRACK, // drain currently playing track |
| 347 | MIXER_DRAIN_ALL, // fully drain the hardware |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 348 | // standby mode does not have an enum value |
| 349 | // suspend by audio policy manager is orthogonal to mixer state |
| 350 | }; |
| 351 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 352 | // retry count before removing active track in case of underrun on offloaded thread: |
| 353 | // we need to make sure that AudioTrack client has enough time to send large buffers |
| 354 | //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled |
| 355 | // for offloaded tracks |
| 356 | static const int8_t kMaxTrackRetriesOffload = 20; |
| 357 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 358 | PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 359 | audio_io_handle_t id, audio_devices_t device, type_t type); |
| 360 | virtual ~PlaybackThread(); |
| 361 | |
| 362 | void dump(int fd, const Vector<String16>& args); |
| 363 | |
| 364 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 365 | virtual bool threadLoop(); |
| 366 | |
| 367 | // RefBase |
| 368 | virtual void onFirstRef(); |
| 369 | |
| 370 | protected: |
| 371 | // Code snippets that were lifted up out of threadLoop() |
| 372 | virtual void threadLoop_mix() = 0; |
| 373 | virtual void threadLoop_sleepTime() = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 374 | virtual ssize_t threadLoop_write(); |
| 375 | virtual void threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 376 | virtual void threadLoop_standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 377 | virtual void threadLoop_exit(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 378 | virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove); |
| 379 | |
| 380 | // prepareTracks_l reads and writes mActiveTracks, and returns |
| 381 | // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller |
| 382 | // is responsible for clearing or destroying this Vector later on, when it |
| 383 | // is safe to do so. That will drop the final ref count and destroy the tracks. |
| 384 | virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 385 | void removeTracks_l(const Vector< sp<Track> >& tracksToRemove); |
| 386 | |
| 387 | void writeCallback(); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 388 | void resetWriteBlocked(uint32_t sequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 389 | void drainCallback(); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 390 | void resetDraining(uint32_t sequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 391 | |
| 392 | static int asyncCallback(stream_callback_event_t event, void *param, void *cookie); |
| 393 | |
| 394 | virtual bool waitingAsyncCallback(); |
| 395 | virtual bool waitingAsyncCallback_l(); |
| 396 | virtual bool shouldStandby_l(); |
| 397 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 398 | |
| 399 | // ThreadBase virtuals |
| 400 | virtual void preExit(); |
| 401 | |
| 402 | public: |
| 403 | |
| 404 | virtual status_t initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; } |
| 405 | |
| 406 | // return estimated latency in milliseconds, as reported by HAL |
| 407 | uint32_t latency() const; |
| 408 | // same, but lock must already be held |
| 409 | uint32_t latency_l() const; |
| 410 | |
| 411 | void setMasterVolume(float value); |
| 412 | void setMasterMute(bool muted); |
| 413 | |
| 414 | void setStreamVolume(audio_stream_type_t stream, float value); |
| 415 | void setStreamMute(audio_stream_type_t stream, bool muted); |
| 416 | |
| 417 | float streamVolume(audio_stream_type_t stream) const; |
| 418 | |
| 419 | sp<Track> createTrack_l( |
| 420 | const sp<AudioFlinger::Client>& client, |
| 421 | audio_stream_type_t streamType, |
| 422 | uint32_t sampleRate, |
| 423 | audio_format_t format, |
| 424 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 425 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 426 | const sp<IMemory>& sharedBuffer, |
| 427 | int sessionId, |
| 428 | IAudioFlinger::track_flags_t *flags, |
| 429 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 430 | int uid, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 431 | status_t *status /*non-NULL*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 432 | |
| 433 | AudioStreamOut* getOutput() const; |
| 434 | AudioStreamOut* clearOutput(); |
| 435 | virtual audio_stream_t* stream() const; |
| 436 | |
| 437 | // a very large number of suspend() will eventually wraparound, but unlikely |
| 438 | void suspend() { (void) android_atomic_inc(&mSuspended); } |
| 439 | void restore() |
| 440 | { |
| 441 | // if restore() is done without suspend(), get back into |
| 442 | // range so that the next suspend() will operate correctly |
| 443 | if (android_atomic_dec(&mSuspended) <= 0) { |
| 444 | android_atomic_release_store(0, &mSuspended); |
| 445 | } |
| 446 | } |
| 447 | bool isSuspended() const |
| 448 | { return android_atomic_acquire_load(&mSuspended) > 0; } |
| 449 | |
| 450 | virtual String8 getParameters(const String8& keys); |
| 451 | virtual void audioConfigChanged_l(int event, int param = 0); |
| 452 | status_t getRenderPosition(size_t *halFrames, size_t *dspFrames); |
| 453 | int16_t *mixBuffer() const { return mMixBuffer; }; |
| 454 | |
| 455 | virtual void detachAuxEffect_l(int effectId); |
| 456 | status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track, |
| 457 | int EffectId); |
| 458 | status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track, |
| 459 | int EffectId); |
| 460 | |
| 461 | virtual status_t addEffectChain_l(const sp<EffectChain>& chain); |
| 462 | virtual size_t removeEffectChain_l(const sp<EffectChain>& chain); |
| 463 | virtual uint32_t hasAudioSession(int sessionId) const; |
| 464 | virtual uint32_t getStrategyForSession_l(int sessionId); |
| 465 | |
| 466 | |
| 467 | virtual status_t setSyncEvent(const sp<SyncEvent>& event); |
| 468 | virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const; |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 469 | |
| 470 | // called with AudioFlinger lock held |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 471 | void invalidateTracks(audio_stream_type_t streamType); |
| 472 | |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 473 | virtual size_t frameCount() const { return mNormalFrameCount; } |
| 474 | |
| 475 | // Return's the HAL's frame count i.e. fast mixer buffer size. |
| 476 | size_t frameCountHAL() const { return mFrameCount; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 477 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 478 | status_t getTimestamp_l(AudioTimestamp& timestamp); |
| 479 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 480 | protected: |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 481 | // updated by readOutputParameters() |
| 482 | size_t mNormalFrameCount; // normal mixer and effects |
| 483 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 484 | int16_t* mMixBuffer; // frame size aligned mix buffer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 485 | |
| 486 | // suspend count, > 0 means suspended. While suspended, the thread continues to pull from |
| 487 | // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle |
| 488 | // concurrent use of both of them, so Audio Policy Service suspends one of the threads to |
| 489 | // workaround that restriction. |
| 490 | // 'volatile' means accessed via atomic operations and no lock. |
| 491 | volatile int32_t mSuspended; |
| 492 | |
| 493 | // FIXME overflows every 6+ hours at 44.1 kHz stereo 16-bit samples |
| 494 | // mFramesWritten would be better, or 64-bit even better |
| 495 | size_t mBytesWritten; |
| 496 | private: |
| 497 | // mMasterMute is in both PlaybackThread and in AudioFlinger. When a |
| 498 | // PlaybackThread needs to find out if master-muted, it checks it's local |
| 499 | // copy rather than the one in AudioFlinger. This optimization saves a lock. |
| 500 | bool mMasterMute; |
| 501 | void setMasterMute_l(bool muted) { mMasterMute = muted; } |
| 502 | protected: |
| 503 | SortedVector< wp<Track> > mActiveTracks; // FIXME check if this could be sp<> |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 504 | SortedVector<int> mWakeLockUids; |
| 505 | int mActiveTracksGeneration; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 506 | wp<Track> mLatestActiveTrack; // latest track added to mActiveTracks |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 507 | |
| 508 | // Allocate a track name for a given channel mask. |
| 509 | // Returns name >= 0 if successful, -1 on failure. |
| 510 | virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId) = 0; |
| 511 | virtual void deleteTrackName_l(int name) = 0; |
| 512 | |
| 513 | // Time to sleep between cycles when: |
| 514 | virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED |
| 515 | virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE |
| 516 | virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us |
| 517 | // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write() |
| 518 | // No sleep in standby mode; waits on a condition |
| 519 | |
| 520 | // Code snippets that are temporarily lifted up out of threadLoop() until the merge |
| 521 | void checkSilentMode_l(); |
| 522 | |
| 523 | // Non-trivial for DUPLICATING only |
| 524 | virtual void saveOutputTracks() { } |
| 525 | virtual void clearOutputTracks() { } |
| 526 | |
| 527 | // Cache various calculated values, at threadLoop() entry and after a parameter change |
| 528 | virtual void cacheParameters_l(); |
| 529 | |
| 530 | virtual uint32_t correctLatency_l(uint32_t latency) const; |
| 531 | |
| 532 | private: |
| 533 | |
| 534 | friend class AudioFlinger; // for numerous |
| 535 | |
| 536 | PlaybackThread(const Client&); |
| 537 | PlaybackThread& operator = (const PlaybackThread&); |
| 538 | |
| 539 | status_t addTrack_l(const sp<Track>& track); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 540 | bool destroyTrack_l(const sp<Track>& track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 541 | void removeTrack_l(const sp<Track>& track); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 542 | void broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 543 | |
| 544 | void readOutputParameters(); |
| 545 | |
| 546 | virtual void dumpInternals(int fd, const Vector<String16>& args); |
| 547 | void dumpTracks(int fd, const Vector<String16>& args); |
| 548 | |
| 549 | SortedVector< sp<Track> > mTracks; |
| 550 | // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by |
| 551 | // DuplicatingThread |
| 552 | stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1]; |
| 553 | AudioStreamOut *mOutput; |
| 554 | |
| 555 | float mMasterVolume; |
| 556 | nsecs_t mLastWriteTime; |
| 557 | int mNumWrites; |
| 558 | int mNumDelayedWrites; |
| 559 | bool mInWrite; |
| 560 | |
| 561 | // FIXME rename these former local variables of threadLoop to standard "m" names |
| 562 | nsecs_t standbyTime; |
| 563 | size_t mixBufferSize; |
| 564 | |
| 565 | // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l() |
| 566 | uint32_t activeSleepTime; |
| 567 | uint32_t idleSleepTime; |
| 568 | |
| 569 | uint32_t sleepTime; |
| 570 | |
| 571 | // mixer status returned by prepareTracks_l() |
| 572 | mixer_state mMixerStatus; // current cycle |
| 573 | // previous cycle when in prepareTracks_l() |
| 574 | mixer_state mMixerStatusIgnoringFastTracks; |
| 575 | // FIXME or a separate ready state per track |
| 576 | |
| 577 | // FIXME move these declarations into the specific sub-class that needs them |
| 578 | // MIXER only |
| 579 | uint32_t sleepTimeShift; |
| 580 | |
| 581 | // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value |
| 582 | nsecs_t standbyDelay; |
| 583 | |
| 584 | // MIXER only |
| 585 | nsecs_t maxPeriod; |
| 586 | |
| 587 | // DUPLICATING only |
| 588 | uint32_t writeFrames; |
| 589 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 590 | size_t mBytesRemaining; |
| 591 | size_t mCurrentWriteLength; |
| 592 | bool mUseAsyncWrite; |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 593 | // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is |
| 594 | // incremented each time a write(), a flush() or a standby() occurs. |
| 595 | // Bit 0 is set when a write blocks and indicates a callback is expected. |
| 596 | // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence |
| 597 | // callbacks are ignored. |
| 598 | uint32_t mWriteAckSequence; |
| 599 | // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is |
| 600 | // incremented each time a drain is requested or a flush() or standby() occurs. |
| 601 | // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is |
| 602 | // expected. |
| 603 | // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence |
| 604 | // callbacks are ignored. |
| 605 | uint32_t mDrainSequence; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 606 | // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait |
| 607 | // for async write callback in the thread loop before evaluating it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 608 | bool mSignalPending; |
| 609 | sp<AsyncCallbackThread> mCallbackThread; |
| 610 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 611 | private: |
| 612 | // The HAL output sink is treated as non-blocking, but current implementation is blocking |
| 613 | sp<NBAIO_Sink> mOutputSink; |
| 614 | // If a fast mixer is present, the blocking pipe sink, otherwise clear |
| 615 | sp<NBAIO_Sink> mPipeSink; |
| 616 | // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink |
| 617 | sp<NBAIO_Sink> mNormalSink; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 618 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 619 | // For dumpsys |
| 620 | sp<NBAIO_Sink> mTeeSink; |
| 621 | sp<NBAIO_Source> mTeeSource; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 622 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 623 | uint32_t mScreenState; // cached copy of gScreenState |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 624 | static const size_t kFastMixerLogSize = 4 * 1024; |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 625 | sp<NBLog::Writer> mFastMixerNBLogWriter; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 626 | public: |
| 627 | virtual bool hasFastMixer() const = 0; |
| 628 | virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const |
| 629 | { FastTrackUnderruns dummy; return dummy; } |
| 630 | |
| 631 | protected: |
| 632 | // accessed by both binder threads and within threadLoop(), lock on mutex needed |
| 633 | unsigned mFastTrackAvailMask; // bit i set if fast track [i] is available |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 634 | virtual void flushOutput_l(); |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 635 | |
| 636 | private: |
| 637 | // timestamp latch: |
| 638 | // D input is written by threadLoop_write while mutex is unlocked, and read while locked |
| 639 | // Q output is written while locked, and read while locked |
| 640 | struct { |
| 641 | AudioTimestamp mTimestamp; |
| 642 | uint32_t mUnpresentedFrames; |
| 643 | } mLatchD, mLatchQ; |
| 644 | bool mLatchDValid; // true means mLatchD is valid, and clock it into latch at next opportunity |
| 645 | bool mLatchQValid; // true means mLatchQ is valid |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | class MixerThread : public PlaybackThread { |
| 649 | public: |
| 650 | MixerThread(const sp<AudioFlinger>& audioFlinger, |
| 651 | AudioStreamOut* output, |
| 652 | audio_io_handle_t id, |
| 653 | audio_devices_t device, |
| 654 | type_t type = MIXER); |
| 655 | virtual ~MixerThread(); |
| 656 | |
| 657 | // Thread virtuals |
| 658 | |
| 659 | virtual bool checkForNewParameters_l(); |
| 660 | virtual void dumpInternals(int fd, const Vector<String16>& args); |
| 661 | |
| 662 | protected: |
| 663 | virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove); |
| 664 | virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId); |
| 665 | virtual void deleteTrackName_l(int name); |
| 666 | virtual uint32_t idleSleepTimeUs() const; |
| 667 | virtual uint32_t suspendSleepTimeUs() const; |
| 668 | virtual void cacheParameters_l(); |
| 669 | |
| 670 | // threadLoop snippets |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 671 | virtual ssize_t threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 672 | virtual void threadLoop_standby(); |
| 673 | virtual void threadLoop_mix(); |
| 674 | virtual void threadLoop_sleepTime(); |
| 675 | virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove); |
| 676 | virtual uint32_t correctLatency_l(uint32_t latency) const; |
| 677 | |
| 678 | AudioMixer* mAudioMixer; // normal mixer |
| 679 | private: |
| 680 | // one-time initialization, no locks required |
| 681 | FastMixer* mFastMixer; // non-NULL if there is also a fast mixer |
| 682 | sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread |
| 683 | |
| 684 | // contents are not guaranteed to be consistent, no locks required |
| 685 | FastMixerDumpState mFastMixerDumpState; |
| 686 | #ifdef STATE_QUEUE_DUMP |
| 687 | StateQueueObserverDump mStateQueueObserverDump; |
| 688 | StateQueueMutatorDump mStateQueueMutatorDump; |
| 689 | #endif |
| 690 | AudioWatchdogDump mAudioWatchdogDump; |
| 691 | |
| 692 | // accessible only within the threadLoop(), no locks required |
| 693 | // mFastMixer->sq() // for mutating and pushing state |
| 694 | int32_t mFastMixerFutex; // for cold idle |
| 695 | |
| 696 | public: |
| 697 | virtual bool hasFastMixer() const { return mFastMixer != NULL; } |
| 698 | virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const { |
| 699 | ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks); |
| 700 | return mFastMixerDumpState.mTracks[fastIndex].mUnderruns; |
| 701 | } |
| 702 | }; |
| 703 | |
| 704 | class DirectOutputThread : public PlaybackThread { |
| 705 | public: |
| 706 | |
| 707 | DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 708 | audio_io_handle_t id, audio_devices_t device); |
| 709 | virtual ~DirectOutputThread(); |
| 710 | |
| 711 | // Thread virtuals |
| 712 | |
| 713 | virtual bool checkForNewParameters_l(); |
| 714 | |
| 715 | protected: |
| 716 | virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId); |
| 717 | virtual void deleteTrackName_l(int name); |
| 718 | virtual uint32_t activeSleepTimeUs() const; |
| 719 | virtual uint32_t idleSleepTimeUs() const; |
| 720 | virtual uint32_t suspendSleepTimeUs() const; |
| 721 | virtual void cacheParameters_l(); |
| 722 | |
| 723 | // threadLoop snippets |
| 724 | virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove); |
| 725 | virtual void threadLoop_mix(); |
| 726 | virtual void threadLoop_sleepTime(); |
| 727 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 728 | // volumes last sent to audio HAL with stream->set_volume() |
| 729 | float mLeftVolFloat; |
| 730 | float mRightVolFloat; |
| 731 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 732 | DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 733 | audio_io_handle_t id, uint32_t device, ThreadBase::type_t type); |
| 734 | void processVolume_l(Track *track, bool lastTrack); |
| 735 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 736 | // prepareTracks_l() tells threadLoop_mix() the name of the single active track |
| 737 | sp<Track> mActiveTrack; |
| 738 | public: |
| 739 | virtual bool hasFastMixer() const { return false; } |
| 740 | }; |
| 741 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 742 | class OffloadThread : public DirectOutputThread { |
| 743 | public: |
| 744 | |
| 745 | OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 746 | audio_io_handle_t id, uint32_t device); |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 747 | virtual ~OffloadThread() {}; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 748 | |
| 749 | protected: |
| 750 | // threadLoop snippets |
| 751 | virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove); |
| 752 | virtual void threadLoop_exit(); |
| 753 | virtual void flushOutput_l(); |
| 754 | |
| 755 | virtual bool waitingAsyncCallback(); |
| 756 | virtual bool waitingAsyncCallback_l(); |
| 757 | virtual bool shouldStandby_l(); |
| 758 | |
| 759 | private: |
| 760 | void flushHw_l(); |
| 761 | |
| 762 | private: |
| 763 | bool mHwPaused; |
| 764 | bool mFlushPending; |
| 765 | size_t mPausedWriteLength; // length in bytes of write interrupted by pause |
| 766 | size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 767 | wp<Track> mPreviousTrack; // used to detect track switch |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 768 | }; |
| 769 | |
| 770 | class AsyncCallbackThread : public Thread { |
| 771 | public: |
| 772 | |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 773 | AsyncCallbackThread(const wp<PlaybackThread>& playbackThread); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 774 | |
| 775 | virtual ~AsyncCallbackThread(); |
| 776 | |
| 777 | // Thread virtuals |
| 778 | virtual bool threadLoop(); |
| 779 | |
| 780 | // RefBase |
| 781 | virtual void onFirstRef(); |
| 782 | |
| 783 | void exit(); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 784 | void setWriteBlocked(uint32_t sequence); |
| 785 | void resetWriteBlocked(); |
| 786 | void setDraining(uint32_t sequence); |
| 787 | void resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 788 | |
| 789 | private: |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 790 | const wp<PlaybackThread> mPlaybackThread; |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 791 | // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via |
| 792 | // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used |
| 793 | // to indicate that the callback has been received via resetWriteBlocked() |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 794 | uint32_t mWriteAckSequence; |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 795 | // mDrainSequence corresponds to the last drain sequence passed by the offload thread via |
| 796 | // setDraining(). The sequence is shifted one bit to the left and the lsb is used |
| 797 | // to indicate that the callback has been received via resetDraining() |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 798 | uint32_t mDrainSequence; |
| 799 | Condition mWaitWorkCV; |
| 800 | Mutex mLock; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 801 | }; |
| 802 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 803 | class DuplicatingThread : public MixerThread { |
| 804 | public: |
| 805 | DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, |
| 806 | audio_io_handle_t id); |
| 807 | virtual ~DuplicatingThread(); |
| 808 | |
| 809 | // Thread virtuals |
| 810 | void addOutputTrack(MixerThread* thread); |
| 811 | void removeOutputTrack(MixerThread* thread); |
| 812 | uint32_t waitTimeMs() const { return mWaitTimeMs; } |
| 813 | protected: |
| 814 | virtual uint32_t activeSleepTimeUs() const; |
| 815 | |
| 816 | private: |
| 817 | bool outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks); |
| 818 | protected: |
| 819 | // threadLoop snippets |
| 820 | virtual void threadLoop_mix(); |
| 821 | virtual void threadLoop_sleepTime(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 822 | virtual ssize_t threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 823 | virtual void threadLoop_standby(); |
| 824 | virtual void cacheParameters_l(); |
| 825 | |
| 826 | private: |
| 827 | // called from threadLoop, addOutputTrack, removeOutputTrack |
| 828 | virtual void updateWaitTime_l(); |
| 829 | protected: |
| 830 | virtual void saveOutputTracks(); |
| 831 | virtual void clearOutputTracks(); |
| 832 | private: |
| 833 | |
| 834 | uint32_t mWaitTimeMs; |
| 835 | SortedVector < sp<OutputTrack> > outputTracks; |
| 836 | SortedVector < sp<OutputTrack> > mOutputTracks; |
| 837 | public: |
| 838 | virtual bool hasFastMixer() const { return false; } |
| 839 | }; |
| 840 | |
| 841 | |
| 842 | // record thread |
| 843 | class RecordThread : public ThreadBase, public AudioBufferProvider |
| 844 | // derives from AudioBufferProvider interface for use by resampler |
| 845 | { |
| 846 | public: |
| 847 | |
| 848 | #include "RecordTracks.h" |
| 849 | |
| 850 | RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 851 | AudioStreamIn *input, |
| 852 | uint32_t sampleRate, |
| 853 | audio_channel_mask_t channelMask, |
| 854 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 855 | audio_devices_t outDevice, |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 856 | audio_devices_t inDevice |
| 857 | #ifdef TEE_SINK |
| 858 | , const sp<NBAIO_Sink>& teeSink |
| 859 | #endif |
| 860 | ); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 861 | virtual ~RecordThread(); |
| 862 | |
| 863 | // no addTrack_l ? |
| 864 | void destroyTrack_l(const sp<RecordTrack>& track); |
| 865 | void removeTrack_l(const sp<RecordTrack>& track); |
| 866 | |
| 867 | void dumpInternals(int fd, const Vector<String16>& args); |
| 868 | void dumpTracks(int fd, const Vector<String16>& args); |
| 869 | |
| 870 | // Thread virtuals |
| 871 | virtual bool threadLoop(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 | |
| 873 | // RefBase |
| 874 | virtual void onFirstRef(); |
| 875 | |
| 876 | virtual status_t initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 877 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 878 | sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l( |
| 879 | const sp<AudioFlinger::Client>& client, |
| 880 | uint32_t sampleRate, |
| 881 | audio_format_t format, |
| 882 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 883 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 884 | int sessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 885 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 886 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 887 | pid_t tid, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 888 | status_t *status /*non-NULL*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 889 | |
| 890 | status_t start(RecordTrack* recordTrack, |
| 891 | AudioSystem::sync_event_t event, |
| 892 | int triggerSession); |
| 893 | |
| 894 | // ask the thread to stop the specified track, and |
| 895 | // return true if the caller should then do it's part of the stopping process |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 896 | bool stop(RecordTrack* recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 897 | |
| 898 | void dump(int fd, const Vector<String16>& args); |
| 899 | AudioStreamIn* clearInput(); |
| 900 | virtual audio_stream_t* stream() const; |
| 901 | |
| 902 | // AudioBufferProvider interface |
| 903 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts); |
| 904 | virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); |
| 905 | |
| 906 | virtual bool checkForNewParameters_l(); |
| 907 | virtual String8 getParameters(const String8& keys); |
| 908 | virtual void audioConfigChanged_l(int event, int param = 0); |
| 909 | void readInputParameters(); |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 910 | virtual uint32_t getInputFramesLost(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 911 | |
| 912 | virtual status_t addEffectChain_l(const sp<EffectChain>& chain); |
| 913 | virtual size_t removeEffectChain_l(const sp<EffectChain>& chain); |
| 914 | virtual uint32_t hasAudioSession(int sessionId) const; |
| 915 | |
| 916 | // Return the set of unique session IDs across all tracks. |
| 917 | // The keys are the session IDs, and the associated values are meaningless. |
| 918 | // FIXME replace by Set [and implement Bag/Multiset for other uses]. |
| 919 | KeyedVector<int, bool> sessionIds() const; |
| 920 | |
| 921 | virtual status_t setSyncEvent(const sp<SyncEvent>& event); |
| 922 | virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const; |
| 923 | |
| 924 | static void syncStartEventCallback(const wp<SyncEvent>& event); |
| 925 | void handleSyncStartEvent(const sp<SyncEvent>& event); |
| 926 | |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 927 | virtual size_t frameCount() const { return mFrameCount; } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 928 | bool hasFastRecorder() const { return false; } |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 929 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 930 | private: |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 931 | void clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 932 | |
| 933 | // Enter standby if not already in standby, and set mStandby flag |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 934 | void standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 935 | |
| 936 | // Call the HAL standby method unconditionally, and don't change mStandby flag |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 937 | void inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 938 | |
| 939 | AudioStreamIn *mInput; |
| 940 | SortedVector < sp<RecordTrack> > mTracks; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 941 | // mActiveTracks has dual roles: it indicates the current active track(s), and |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 942 | // is used together with mStartStopCond to indicate start()/stop() progress |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 943 | SortedVector< sp<RecordTrack> > mActiveTracks; |
| 944 | // generation counter for mActiveTracks |
| 945 | int mActiveTracksGen; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 946 | Condition mStartStopCond; |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 947 | |
| 948 | // updated by RecordThread::readInputParameters() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 949 | AudioResampler *mResampler; |
Glenn Kasten | 34af026 | 2013-07-30 11:52:39 -0700 | [diff] [blame] | 950 | // interleaved stereo pairs of fixed-point signed Q19.12 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 951 | int32_t *mRsmpOutBuffer; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 952 | |
| 953 | // resampler converts input at HAL Hz to output at AudioRecord client Hz |
| 954 | int16_t *mRsmpInBuffer; // see new[] for details on the size |
| 955 | size_t mRsmpInFrames; // size of resampler input in frames |
| 956 | size_t mRsmpInFramesP2;// size rounded up to a power-of-2 |
| 957 | size_t mRsmpInUnrel; // unreleased frames remaining from |
| 958 | // most recent getNextBuffer |
| 959 | // these are rolling counters that are never cleared |
| 960 | int32_t mRsmpInFront; // next available frame |
| 961 | int32_t mRsmpInRear; // last filled frame + 1 |
| 962 | size_t mRsmpInIndex; // FIXME legacy |
| 963 | |
| 964 | // client's requested configuration, which may differ from the HAL configuration |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 965 | const uint32_t mReqChannelCount; |
| 966 | const uint32_t mReqSampleRate; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 967 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 968 | ssize_t mBytesRead; |
| 969 | // sync event triggering actual audio capture. Frames read before this event will |
| 970 | // be dropped and therefore not read by the application. |
| 971 | sp<SyncEvent> mSyncStartEvent; |
| 972 | // number of captured frames to drop after the start sync event has been received. |
| 973 | // when < 0, maximum frames to drop before starting capture even if sync event is |
| 974 | // not received |
| 975 | ssize_t mFramestoDrop; |
| 976 | |
| 977 | // For dumpsys |
| 978 | const sp<NBAIO_Sink> mTeeSink; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 979 | }; |