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