blob: 8ea868335e5afd2a9a4b3e113891a1750b75ee53 [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
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
22class ThreadBase : public Thread {
23public:
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 Laurentbfb1b832013-01-07 09:53:42 -080031 RECORD, // Thread class is RecordThread
32 OFFLOAD // Thread class is OffloadThread
Eric Laurent81784c32012-11-19 14:55:58 -080033 };
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 Kastencf04c2c2013-08-06 07:41:16 -070039 virtual status_t readyToRun();
40
Eric Laurent81784c32012-11-19 14:55:58 -080041 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 Kasten49debee2014-01-31 16:54:54 -080068 ConfigEvent(CFG_EVENT_IO), mEvent(event), mParam(param) {}
Eric Laurent81784c32012-11-19 14:55:58 -080069 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 Kasten9b58f632013-07-16 11:37:48 -0700131 // and returns the [normal mix] buffer's frame count.
132 virtual size_t frameCount() const = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800133 size_t frameSize() const { return mFrameSize; }
Eric Laurent81784c32012-11-19 14:55:58 -0800134
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 Kastenf7773312013-08-13 16:00:42 -0700146 void processConfigEvents_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800147
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 Kasten9156ef32013-08-06 15:39:08 -0700162 status_t *status /*non-NULL*/);
Eric Laurent81784c32012-11-19 14:55:58 -0800163 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 Laurentbfb1b832013-01-07 09:53:42 -0800190 // get a copy of mEffectChains vector
191 Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
Eric Laurent81784c32012-11-19 14:55:58 -0800192 // 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
Glenn Kasten0f11b512014-01-31 16:18:54 -0800204 virtual void detachAuxEffect_l(int effectId __unused) {}
Eric Laurent81784c32012-11-19 14:55:58 -0800205 // 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
Glenn Kasten0f11b512014-01-31 16:18:54 -0800210 virtual uint32_t getStrategyForSession_l(int sessionId __unused) { return 0; }
Eric Laurent81784c32012-11-19 14:55:58 -0800211
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
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700229 // Return a reference to a per-thread heap which can be used to allocate IMemory
230 // objects that will be read-only to client processes, read/write to mediaserver,
231 // and shared by all client processes of the thread.
232 // The heap is per-thread rather than common across all threads, because
233 // clients can't be trusted not to modify the offset of the IMemory they receive.
234 // If a thread does not have such a heap, this method returns 0.
235 virtual sp<MemoryDealer> readOnlyHeap() const { return 0; }
Eric Laurent81784c32012-11-19 14:55:58 -0800236
237 mutable Mutex mLock;
238
239protected:
240
241 // entry describing an effect being suspended in mSuspendedSessions keyed vector
242 class SuspendedSessionDesc : public RefBase {
243 public:
244 SuspendedSessionDesc() : mRefCount(0) {}
245
246 int mRefCount; // number of active suspend requests
247 effect_uuid_t mType; // effect type UUID
248 };
249
Marco Nelissene14a5d62013-10-03 08:51:24 -0700250 void acquireWakeLock(int uid = -1);
251 void acquireWakeLock_l(int uid = -1);
Eric Laurent81784c32012-11-19 14:55:58 -0800252 void releaseWakeLock();
253 void releaseWakeLock_l();
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800254 void updateWakeLockUids(const SortedVector<int> &uids);
255 void updateWakeLockUids_l(const SortedVector<int> &uids);
256 void getPowerManager_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800257 void setEffectSuspended_l(const effect_uuid_t *type,
258 bool suspend,
259 int sessionId);
260 // updated mSuspendedSessions when an effect suspended or restored
261 void updateSuspendedSessions_l(const effect_uuid_t *type,
262 bool suspend,
263 int sessionId);
264 // check if some effects must be suspended when an effect chain is added
265 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
266
Narayan Kamath014e7fa2013-10-14 15:03:38 +0100267 String16 getWakeLockTag();
268
Eric Laurent81784c32012-11-19 14:55:58 -0800269 virtual void preExit() { }
270
271 friend class AudioFlinger; // for mEffectChains
272
273 const type_t mType;
274
275 // Used by parameters, config events, addTrack_l, exit
276 Condition mWaitWorkCV;
277
278 const sp<AudioFlinger> mAudioFlinger;
Glenn Kasten9b58f632013-07-16 11:37:48 -0700279
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800280 // updated by PlaybackThread::readOutputParameters_l() or
281 // RecordThread::readInputParameters_l()
Eric Laurent81784c32012-11-19 14:55:58 -0800282 uint32_t mSampleRate;
283 size_t mFrameCount; // output HAL, direct output, record
Eric Laurent81784c32012-11-19 14:55:58 -0800284 audio_channel_mask_t mChannelMask;
Glenn Kastenf6ed4232013-07-16 11:16:27 -0700285 uint32_t mChannelCount;
Eric Laurent81784c32012-11-19 14:55:58 -0800286 size_t mFrameSize;
287 audio_format_t mFormat;
Glenn Kasten70949c42013-08-06 07:40:12 -0700288 size_t mBufferSize; // HAL buffer size for read() or write()
Eric Laurent81784c32012-11-19 14:55:58 -0800289
290 // Parameter sequence by client: binder thread calling setParameters():
291 // 1. Lock mLock
292 // 2. Append to mNewParameters
293 // 3. mWaitWorkCV.signal
294 // 4. mParamCond.waitRelative with timeout
295 // 5. read mParamStatus
296 // 6. mWaitWorkCV.signal
297 // 7. Unlock
298 //
299 // Parameter sequence by server: threadLoop calling checkForNewParameters_l():
300 // 1. Lock mLock
301 // 2. If there is an entry in mNewParameters proceed ...
302 // 2. Read first entry in mNewParameters
303 // 3. Process
304 // 4. Remove first entry from mNewParameters
305 // 5. Set mParamStatus
306 // 6. mParamCond.signal
307 // 7. mWaitWorkCV.wait with timeout (this is to avoid overwriting mParamStatus)
308 // 8. Unlock
309 Condition mParamCond;
310 Vector<String8> mNewParameters;
311 status_t mParamStatus;
312
Glenn Kastenc6ae3c82013-07-17 09:08:51 -0700313 // vector owns each ConfigEvent *, so must delete after removing
Eric Laurent81784c32012-11-19 14:55:58 -0800314 Vector<ConfigEvent *> mConfigEvents;
315
316 // These fields are written and read by thread itself without lock or barrier,
Glenn Kasten4944acb2013-08-19 08:39:20 -0700317 // and read by other threads without lock or barrier via standby(), outDevice()
Eric Laurent81784c32012-11-19 14:55:58 -0800318 // and inDevice().
319 // Because of the absence of a lock or barrier, any other thread that reads
320 // these fields must use the information in isolation, or be prepared to deal
321 // with possibility that it might be inconsistent with other information.
Glenn Kasten4944acb2013-08-19 08:39:20 -0700322 bool mStandby; // Whether thread is currently in standby.
Eric Laurent81784c32012-11-19 14:55:58 -0800323 audio_devices_t mOutDevice; // output device
324 audio_devices_t mInDevice; // input device
325 audio_source_t mAudioSource; // (see audio.h, audio_source_t)
326
327 const audio_io_handle_t mId;
328 Vector< sp<EffectChain> > mEffectChains;
329
330 static const int kNameLength = 16; // prctl(PR_SET_NAME) limit
331 char mName[kNameLength];
332 sp<IPowerManager> mPowerManager;
333 sp<IBinder> mWakeLockToken;
334 const sp<PMDeathRecipient> mDeathRecipient;
335 // list of suspended effects per session and per type. The first vector is
336 // keyed by session ID, the second by type UUID timeLow field
337 KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > >
338 mSuspendedSessions;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800339 static const size_t kLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800340 sp<NBLog::Writer> mNBLogWriter;
Eric Laurent81784c32012-11-19 14:55:58 -0800341};
342
343// --- PlaybackThread ---
344class PlaybackThread : public ThreadBase {
345public:
346
347#include "PlaybackTracks.h"
348
349 enum mixer_state {
350 MIXER_IDLE, // no active tracks
351 MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready
Eric Laurentbfb1b832013-01-07 09:53:42 -0800352 MIXER_TRACKS_READY, // at least one active track, and at least one track has data
353 MIXER_DRAIN_TRACK, // drain currently playing track
354 MIXER_DRAIN_ALL, // fully drain the hardware
Eric Laurent81784c32012-11-19 14:55:58 -0800355 // standby mode does not have an enum value
356 // suspend by audio policy manager is orthogonal to mixer state
357 };
358
Eric Laurentbfb1b832013-01-07 09:53:42 -0800359 // retry count before removing active track in case of underrun on offloaded thread:
360 // we need to make sure that AudioTrack client has enough time to send large buffers
361//FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled
362 // for offloaded tracks
363 static const int8_t kMaxTrackRetriesOffload = 20;
364
Eric Laurent81784c32012-11-19 14:55:58 -0800365 PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
366 audio_io_handle_t id, audio_devices_t device, type_t type);
367 virtual ~PlaybackThread();
368
369 void dump(int fd, const Vector<String16>& args);
370
371 // Thread virtuals
Eric Laurent81784c32012-11-19 14:55:58 -0800372 virtual bool threadLoop();
373
374 // RefBase
375 virtual void onFirstRef();
376
377protected:
378 // Code snippets that were lifted up out of threadLoop()
379 virtual void threadLoop_mix() = 0;
380 virtual void threadLoop_sleepTime() = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800381 virtual ssize_t threadLoop_write();
382 virtual void threadLoop_drain();
Eric Laurent81784c32012-11-19 14:55:58 -0800383 virtual void threadLoop_standby();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800384 virtual void threadLoop_exit();
Eric Laurent81784c32012-11-19 14:55:58 -0800385 virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
386
387 // prepareTracks_l reads and writes mActiveTracks, and returns
388 // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller
389 // is responsible for clearing or destroying this Vector later on, when it
390 // is safe to do so. That will drop the final ref count and destroy the tracks.
391 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800392 void removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
393
394 void writeCallback();
Eric Laurent3b4529e2013-09-05 18:09:19 -0700395 void resetWriteBlocked(uint32_t sequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800396 void drainCallback();
Eric Laurent3b4529e2013-09-05 18:09:19 -0700397 void resetDraining(uint32_t sequence);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800398
399 static int asyncCallback(stream_callback_event_t event, void *param, void *cookie);
400
401 virtual bool waitingAsyncCallback();
402 virtual bool waitingAsyncCallback_l();
403 virtual bool shouldStandby_l();
Haynes Mathew George4c6a4332014-01-15 12:31:39 -0800404 virtual void onAddNewTrack_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800405
406 // ThreadBase virtuals
407 virtual void preExit();
408
409public:
410
411 virtual status_t initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
412
413 // return estimated latency in milliseconds, as reported by HAL
414 uint32_t latency() const;
415 // same, but lock must already be held
416 uint32_t latency_l() const;
417
418 void setMasterVolume(float value);
419 void setMasterMute(bool muted);
420
421 void setStreamVolume(audio_stream_type_t stream, float value);
422 void setStreamMute(audio_stream_type_t stream, bool muted);
423
424 float streamVolume(audio_stream_type_t stream) const;
425
426 sp<Track> createTrack_l(
427 const sp<AudioFlinger::Client>& client,
428 audio_stream_type_t streamType,
429 uint32_t sampleRate,
430 audio_format_t format,
431 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800432 size_t *pFrameCount,
Eric Laurent81784c32012-11-19 14:55:58 -0800433 const sp<IMemory>& sharedBuffer,
434 int sessionId,
435 IAudioFlinger::track_flags_t *flags,
436 pid_t tid,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800437 int uid,
Glenn Kasten9156ef32013-08-06 15:39:08 -0700438 status_t *status /*non-NULL*/);
Eric Laurent81784c32012-11-19 14:55:58 -0800439
440 AudioStreamOut* getOutput() const;
441 AudioStreamOut* clearOutput();
442 virtual audio_stream_t* stream() const;
443
444 // a very large number of suspend() will eventually wraparound, but unlikely
445 void suspend() { (void) android_atomic_inc(&mSuspended); }
446 void restore()
447 {
448 // if restore() is done without suspend(), get back into
449 // range so that the next suspend() will operate correctly
450 if (android_atomic_dec(&mSuspended) <= 0) {
451 android_atomic_release_store(0, &mSuspended);
452 }
453 }
454 bool isSuspended() const
455 { return android_atomic_acquire_load(&mSuspended) > 0; }
456
457 virtual String8 getParameters(const String8& keys);
458 virtual void audioConfigChanged_l(int event, int param = 0);
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000459 status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Andy Hung010a1a12014-03-13 13:57:33 -0700460 // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency.
461 // Consider also removing and passing an explicit mMainBuffer initialization
462 // parameter to AF::PlaybackThread::Track::Track().
463 int16_t *mixBuffer() const {
464 return reinterpret_cast<int16_t *>(mSinkBuffer); };
Eric Laurent81784c32012-11-19 14:55:58 -0800465
466 virtual void detachAuxEffect_l(int effectId);
467 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
468 int EffectId);
469 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
470 int EffectId);
471
472 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
473 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
474 virtual uint32_t hasAudioSession(int sessionId) const;
475 virtual uint32_t getStrategyForSession_l(int sessionId);
476
477
478 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
479 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700480
481 // called with AudioFlinger lock held
Eric Laurent81784c32012-11-19 14:55:58 -0800482 void invalidateTracks(audio_stream_type_t streamType);
483
Glenn Kasten9b58f632013-07-16 11:37:48 -0700484 virtual size_t frameCount() const { return mNormalFrameCount; }
485
486 // Return's the HAL's frame count i.e. fast mixer buffer size.
487 size_t frameCountHAL() const { return mFrameCount; }
Eric Laurent81784c32012-11-19 14:55:58 -0800488
Eric Laurentaccc1472013-09-20 09:36:34 -0700489 status_t getTimestamp_l(AudioTimestamp& timestamp);
490
Eric Laurent81784c32012-11-19 14:55:58 -0800491protected:
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800492 // updated by readOutputParameters_l()
Glenn Kasten9b58f632013-07-16 11:37:48 -0700493 size_t mNormalFrameCount; // normal mixer and effects
494
Andy Hung010a1a12014-03-13 13:57:33 -0700495 void* mSinkBuffer; // frame size aligned sink buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800496
Andy Hung98ef9782014-03-04 14:46:50 -0800497 // TODO:
498 // Rearrange the buffer info into a struct/class with
499 // clear, copy, construction, destruction methods.
500 //
501 // mSinkBuffer also has associated with it:
502 //
503 // mSinkBufferSize: Sink Buffer Size
504 // mFormat: Sink Buffer Format
505
Andy Hung69aed5f2014-02-25 17:24:40 -0800506 // Mixer Buffer (mMixerBuffer*)
507 //
508 // In the case of floating point or multichannel data, which is not in the
509 // sink format, it is required to accumulate in a higher precision or greater channel count
510 // buffer before downmixing or data conversion to the sink buffer.
511
512 // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
513 bool mMixerBufferEnabled;
514
515 // Storage, 32 byte aligned (may make this alignment a requirement later).
516 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
517 void* mMixerBuffer;
518
519 // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
520 size_t mMixerBufferSize;
521
522 // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
523 audio_format_t mMixerBufferFormat;
524
525 // An internal flag set to true by MixerThread::prepareTracks_l()
526 // when mMixerBuffer contains valid data after mixing.
527 bool mMixerBufferValid;
528
Andy Hung98ef9782014-03-04 14:46:50 -0800529 // Effects Buffer (mEffectsBuffer*)
530 //
531 // In the case of effects data, which is not in the sink format,
532 // it is required to accumulate in a different buffer before data conversion
533 // to the sink buffer.
534
535 // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
536 bool mEffectBufferEnabled;
537
538 // Storage, 32 byte aligned (may make this alignment a requirement later).
539 // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
540 void* mEffectBuffer;
541
542 // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
543 size_t mEffectBufferSize;
544
545 // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
546 audio_format_t mEffectBufferFormat;
547
548 // An internal flag set to true by MixerThread::prepareTracks_l()
549 // when mEffectsBuffer contains valid data after mixing.
550 //
551 // When this is set, all mixer data is routed into the effects buffer
552 // for any processing (including output processing).
553 bool mEffectBufferValid;
554
Eric Laurent81784c32012-11-19 14:55:58 -0800555 // suspend count, > 0 means suspended. While suspended, the thread continues to pull from
556 // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle
557 // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
558 // workaround that restriction.
559 // 'volatile' means accessed via atomic operations and no lock.
560 volatile int32_t mSuspended;
561
562 // FIXME overflows every 6+ hours at 44.1 kHz stereo 16-bit samples
563 // mFramesWritten would be better, or 64-bit even better
564 size_t mBytesWritten;
565private:
566 // mMasterMute is in both PlaybackThread and in AudioFlinger. When a
567 // PlaybackThread needs to find out if master-muted, it checks it's local
568 // copy rather than the one in AudioFlinger. This optimization saves a lock.
569 bool mMasterMute;
570 void setMasterMute_l(bool muted) { mMasterMute = muted; }
571protected:
572 SortedVector< wp<Track> > mActiveTracks; // FIXME check if this could be sp<>
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800573 SortedVector<int> mWakeLockUids;
574 int mActiveTracksGeneration;
Eric Laurentfd477972013-10-25 18:10:40 -0700575 wp<Track> mLatestActiveTrack; // latest track added to mActiveTracks
Eric Laurent81784c32012-11-19 14:55:58 -0800576
577 // Allocate a track name for a given channel mask.
578 // Returns name >= 0 if successful, -1 on failure.
579 virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId) = 0;
580 virtual void deleteTrackName_l(int name) = 0;
581
582 // Time to sleep between cycles when:
583 virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED
584 virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE
585 virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us
586 // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
587 // No sleep in standby mode; waits on a condition
588
589 // Code snippets that are temporarily lifted up out of threadLoop() until the merge
590 void checkSilentMode_l();
591
592 // Non-trivial for DUPLICATING only
593 virtual void saveOutputTracks() { }
594 virtual void clearOutputTracks() { }
595
596 // Cache various calculated values, at threadLoop() entry and after a parameter change
597 virtual void cacheParameters_l();
598
599 virtual uint32_t correctLatency_l(uint32_t latency) const;
600
601private:
602
603 friend class AudioFlinger; // for numerous
604
605 PlaybackThread(const Client&);
606 PlaybackThread& operator = (const PlaybackThread&);
607
608 status_t addTrack_l(const sp<Track>& track);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800609 bool destroyTrack_l(const sp<Track>& track);
Eric Laurent81784c32012-11-19 14:55:58 -0800610 void removeTrack_l(const sp<Track>& track);
Eric Laurentede6c3b2013-09-19 14:37:46 -0700611 void broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800612
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800613 void readOutputParameters_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800614
615 virtual void dumpInternals(int fd, const Vector<String16>& args);
616 void dumpTracks(int fd, const Vector<String16>& args);
617
618 SortedVector< sp<Track> > mTracks;
619 // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by
620 // DuplicatingThread
621 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
622 AudioStreamOut *mOutput;
623
624 float mMasterVolume;
625 nsecs_t mLastWriteTime;
626 int mNumWrites;
627 int mNumDelayedWrites;
628 bool mInWrite;
629
630 // FIXME rename these former local variables of threadLoop to standard "m" names
631 nsecs_t standbyTime;
Andy Hung25c2dac2014-02-27 14:56:00 -0800632 size_t mSinkBufferSize;
Eric Laurent81784c32012-11-19 14:55:58 -0800633
634 // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
635 uint32_t activeSleepTime;
636 uint32_t idleSleepTime;
637
638 uint32_t sleepTime;
639
640 // mixer status returned by prepareTracks_l()
641 mixer_state mMixerStatus; // current cycle
642 // previous cycle when in prepareTracks_l()
643 mixer_state mMixerStatusIgnoringFastTracks;
644 // FIXME or a separate ready state per track
645
646 // FIXME move these declarations into the specific sub-class that needs them
647 // MIXER only
648 uint32_t sleepTimeShift;
649
650 // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
651 nsecs_t standbyDelay;
652
653 // MIXER only
654 nsecs_t maxPeriod;
655
656 // DUPLICATING only
657 uint32_t writeFrames;
658
Eric Laurentbfb1b832013-01-07 09:53:42 -0800659 size_t mBytesRemaining;
660 size_t mCurrentWriteLength;
661 bool mUseAsyncWrite;
Eric Laurent3b4529e2013-09-05 18:09:19 -0700662 // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
663 // incremented each time a write(), a flush() or a standby() occurs.
664 // Bit 0 is set when a write blocks and indicates a callback is expected.
665 // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
666 // callbacks are ignored.
667 uint32_t mWriteAckSequence;
668 // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
669 // incremented each time a drain is requested or a flush() or standby() occurs.
670 // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
671 // expected.
672 // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
673 // callbacks are ignored.
674 uint32_t mDrainSequence;
Eric Laurentede6c3b2013-09-19 14:37:46 -0700675 // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait
676 // for async write callback in the thread loop before evaluating it
Eric Laurentbfb1b832013-01-07 09:53:42 -0800677 bool mSignalPending;
678 sp<AsyncCallbackThread> mCallbackThread;
679
Eric Laurent81784c32012-11-19 14:55:58 -0800680private:
681 // The HAL output sink is treated as non-blocking, but current implementation is blocking
682 sp<NBAIO_Sink> mOutputSink;
683 // If a fast mixer is present, the blocking pipe sink, otherwise clear
684 sp<NBAIO_Sink> mPipeSink;
685 // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
686 sp<NBAIO_Sink> mNormalSink;
Glenn Kasten46909e72013-02-26 09:20:22 -0800687#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -0800688 // For dumpsys
689 sp<NBAIO_Sink> mTeeSink;
690 sp<NBAIO_Source> mTeeSource;
Glenn Kasten46909e72013-02-26 09:20:22 -0800691#endif
Eric Laurent81784c32012-11-19 14:55:58 -0800692 uint32_t mScreenState; // cached copy of gScreenState
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800693 static const size_t kFastMixerLogSize = 4 * 1024;
Glenn Kasten9e58b552013-01-18 15:09:48 -0800694 sp<NBLog::Writer> mFastMixerNBLogWriter;
Eric Laurent81784c32012-11-19 14:55:58 -0800695public:
696 virtual bool hasFastMixer() const = 0;
Glenn Kasten0f11b512014-01-31 16:18:54 -0800697 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
Eric Laurent81784c32012-11-19 14:55:58 -0800698 { FastTrackUnderruns dummy; return dummy; }
699
700protected:
701 // accessed by both binder threads and within threadLoop(), lock on mutex needed
702 unsigned mFastTrackAvailMask; // bit i set if fast track [i] is available
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700703
704private:
705 // timestamp latch:
706 // D input is written by threadLoop_write while mutex is unlocked, and read while locked
707 // Q output is written while locked, and read while locked
708 struct {
709 AudioTimestamp mTimestamp;
710 uint32_t mUnpresentedFrames;
711 } mLatchD, mLatchQ;
712 bool mLatchDValid; // true means mLatchD is valid, and clock it into latch at next opportunity
713 bool mLatchQValid; // true means mLatchQ is valid
Eric Laurent81784c32012-11-19 14:55:58 -0800714};
715
716class MixerThread : public PlaybackThread {
717public:
718 MixerThread(const sp<AudioFlinger>& audioFlinger,
719 AudioStreamOut* output,
720 audio_io_handle_t id,
721 audio_devices_t device,
722 type_t type = MIXER);
723 virtual ~MixerThread();
724
725 // Thread virtuals
726
727 virtual bool checkForNewParameters_l();
728 virtual void dumpInternals(int fd, const Vector<String16>& args);
729
730protected:
731 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
732 virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
733 virtual void deleteTrackName_l(int name);
734 virtual uint32_t idleSleepTimeUs() const;
735 virtual uint32_t suspendSleepTimeUs() const;
736 virtual void cacheParameters_l();
737
738 // threadLoop snippets
Eric Laurentbfb1b832013-01-07 09:53:42 -0800739 virtual ssize_t threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -0800740 virtual void threadLoop_standby();
741 virtual void threadLoop_mix();
742 virtual void threadLoop_sleepTime();
743 virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
744 virtual uint32_t correctLatency_l(uint32_t latency) const;
745
746 AudioMixer* mAudioMixer; // normal mixer
747private:
748 // one-time initialization, no locks required
749 FastMixer* mFastMixer; // non-NULL if there is also a fast mixer
750 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
751
752 // contents are not guaranteed to be consistent, no locks required
753 FastMixerDumpState mFastMixerDumpState;
754#ifdef STATE_QUEUE_DUMP
755 StateQueueObserverDump mStateQueueObserverDump;
756 StateQueueMutatorDump mStateQueueMutatorDump;
757#endif
758 AudioWatchdogDump mAudioWatchdogDump;
759
760 // accessible only within the threadLoop(), no locks required
761 // mFastMixer->sq() // for mutating and pushing state
762 int32_t mFastMixerFutex; // for cold idle
763
764public:
765 virtual bool hasFastMixer() const { return mFastMixer != NULL; }
766 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
767 ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks);
768 return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
769 }
770};
771
772class DirectOutputThread : public PlaybackThread {
773public:
774
775 DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
776 audio_io_handle_t id, audio_devices_t device);
777 virtual ~DirectOutputThread();
778
779 // Thread virtuals
780
781 virtual bool checkForNewParameters_l();
782
783protected:
784 virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
785 virtual void deleteTrackName_l(int name);
786 virtual uint32_t activeSleepTimeUs() const;
787 virtual uint32_t idleSleepTimeUs() const;
788 virtual uint32_t suspendSleepTimeUs() const;
789 virtual void cacheParameters_l();
790
791 // threadLoop snippets
792 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
793 virtual void threadLoop_mix();
794 virtual void threadLoop_sleepTime();
795
Eric Laurent81784c32012-11-19 14:55:58 -0800796 // volumes last sent to audio HAL with stream->set_volume()
797 float mLeftVolFloat;
798 float mRightVolFloat;
799
Eric Laurentbfb1b832013-01-07 09:53:42 -0800800 DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
801 audio_io_handle_t id, uint32_t device, ThreadBase::type_t type);
802 void processVolume_l(Track *track, bool lastTrack);
803
Eric Laurent81784c32012-11-19 14:55:58 -0800804 // prepareTracks_l() tells threadLoop_mix() the name of the single active track
805 sp<Track> mActiveTrack;
806public:
807 virtual bool hasFastMixer() const { return false; }
808};
809
Eric Laurentbfb1b832013-01-07 09:53:42 -0800810class OffloadThread : public DirectOutputThread {
811public:
812
813 OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
814 audio_io_handle_t id, uint32_t device);
Eric Laurent6a51d7e2013-10-17 18:59:26 -0700815 virtual ~OffloadThread() {};
Eric Laurentbfb1b832013-01-07 09:53:42 -0800816
817protected:
818 // threadLoop snippets
819 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
820 virtual void threadLoop_exit();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800821
822 virtual bool waitingAsyncCallback();
823 virtual bool waitingAsyncCallback_l();
824 virtual bool shouldStandby_l();
Haynes Mathew George4c6a4332014-01-15 12:31:39 -0800825 virtual void onAddNewTrack_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800826
827private:
828 void flushHw_l();
829
830private:
831 bool mHwPaused;
832 bool mFlushPending;
833 size_t mPausedWriteLength; // length in bytes of write interrupted by pause
834 size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume
Eric Laurentd7e59222013-11-15 12:02:28 -0800835 wp<Track> mPreviousTrack; // used to detect track switch
Eric Laurentbfb1b832013-01-07 09:53:42 -0800836};
837
838class AsyncCallbackThread : public Thread {
839public:
840
Eric Laurent4de95592013-09-26 15:28:21 -0700841 AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800842
843 virtual ~AsyncCallbackThread();
844
845 // Thread virtuals
846 virtual bool threadLoop();
847
848 // RefBase
849 virtual void onFirstRef();
850
851 void exit();
Eric Laurent3b4529e2013-09-05 18:09:19 -0700852 void setWriteBlocked(uint32_t sequence);
853 void resetWriteBlocked();
854 void setDraining(uint32_t sequence);
855 void resetDraining();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800856
857private:
Eric Laurent4de95592013-09-26 15:28:21 -0700858 const wp<PlaybackThread> mPlaybackThread;
Eric Laurent3b4529e2013-09-05 18:09:19 -0700859 // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
860 // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
861 // to indicate that the callback has been received via resetWriteBlocked()
Eric Laurent4de95592013-09-26 15:28:21 -0700862 uint32_t mWriteAckSequence;
Eric Laurent3b4529e2013-09-05 18:09:19 -0700863 // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
864 // setDraining(). The sequence is shifted one bit to the left and the lsb is used
865 // to indicate that the callback has been received via resetDraining()
Eric Laurent4de95592013-09-26 15:28:21 -0700866 uint32_t mDrainSequence;
867 Condition mWaitWorkCV;
868 Mutex mLock;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800869};
870
Eric Laurent81784c32012-11-19 14:55:58 -0800871class DuplicatingThread : public MixerThread {
872public:
873 DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
874 audio_io_handle_t id);
875 virtual ~DuplicatingThread();
876
877 // Thread virtuals
878 void addOutputTrack(MixerThread* thread);
879 void removeOutputTrack(MixerThread* thread);
880 uint32_t waitTimeMs() const { return mWaitTimeMs; }
881protected:
882 virtual uint32_t activeSleepTimeUs() const;
883
884private:
885 bool outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
886protected:
887 // threadLoop snippets
888 virtual void threadLoop_mix();
889 virtual void threadLoop_sleepTime();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800890 virtual ssize_t threadLoop_write();
Eric Laurent81784c32012-11-19 14:55:58 -0800891 virtual void threadLoop_standby();
892 virtual void cacheParameters_l();
893
894private:
895 // called from threadLoop, addOutputTrack, removeOutputTrack
896 virtual void updateWaitTime_l();
897protected:
898 virtual void saveOutputTracks();
899 virtual void clearOutputTracks();
900private:
901
902 uint32_t mWaitTimeMs;
903 SortedVector < sp<OutputTrack> > outputTracks;
904 SortedVector < sp<OutputTrack> > mOutputTracks;
905public:
906 virtual bool hasFastMixer() const { return false; }
907};
908
909
910// record thread
Glenn Kasten6dd62fb2013-12-05 16:35:58 -0800911class RecordThread : public ThreadBase
Eric Laurent81784c32012-11-19 14:55:58 -0800912{
913public:
914
Glenn Kasten6dd62fb2013-12-05 16:35:58 -0800915 class RecordTrack;
916 class ResamplerBufferProvider : public AudioBufferProvider
917 // derives from AudioBufferProvider interface for use by resampler
918 {
919 public:
920 ResamplerBufferProvider(RecordTrack* recordTrack) : mRecordTrack(recordTrack) { }
921 virtual ~ResamplerBufferProvider() { }
922 // AudioBufferProvider interface
923 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
924 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
925 private:
926 RecordTrack * const mRecordTrack;
927 };
928
Eric Laurent81784c32012-11-19 14:55:58 -0800929#include "RecordTracks.h"
930
931 RecordThread(const sp<AudioFlinger>& audioFlinger,
932 AudioStreamIn *input,
Eric Laurent81784c32012-11-19 14:55:58 -0800933 audio_io_handle_t id,
Eric Laurentd3922f72013-02-01 17:57:04 -0800934 audio_devices_t outDevice,
Glenn Kasten46909e72013-02-26 09:20:22 -0800935 audio_devices_t inDevice
936#ifdef TEE_SINK
937 , const sp<NBAIO_Sink>& teeSink
938#endif
939 );
Eric Laurent81784c32012-11-19 14:55:58 -0800940 virtual ~RecordThread();
941
942 // no addTrack_l ?
943 void destroyTrack_l(const sp<RecordTrack>& track);
944 void removeTrack_l(const sp<RecordTrack>& track);
945
946 void dumpInternals(int fd, const Vector<String16>& args);
947 void dumpTracks(int fd, const Vector<String16>& args);
948
949 // Thread virtuals
950 virtual bool threadLoop();
Eric Laurent81784c32012-11-19 14:55:58 -0800951
952 // RefBase
953 virtual void onFirstRef();
954
955 virtual status_t initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
Glenn Kastene198c362013-08-13 09:13:36 -0700956
Glenn Kastenb880f5e2014-05-07 08:43:45 -0700957 virtual sp<MemoryDealer> readOnlyHeap() const { return mReadOnlyHeap; }
958
Eric Laurent81784c32012-11-19 14:55:58 -0800959 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
960 const sp<AudioFlinger::Client>& client,
961 uint32_t sampleRate,
962 audio_format_t format,
963 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800964 size_t *pFrameCount,
Eric Laurent81784c32012-11-19 14:55:58 -0800965 int sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800966 int uid,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -0700967 IAudioFlinger::track_flags_t *flags,
Eric Laurent81784c32012-11-19 14:55:58 -0800968 pid_t tid,
Glenn Kasten9156ef32013-08-06 15:39:08 -0700969 status_t *status /*non-NULL*/);
Eric Laurent81784c32012-11-19 14:55:58 -0800970
971 status_t start(RecordTrack* recordTrack,
972 AudioSystem::sync_event_t event,
973 int triggerSession);
974
975 // ask the thread to stop the specified track, and
976 // return true if the caller should then do it's part of the stopping process
Glenn Kastena8356f62013-07-25 14:37:52 -0700977 bool stop(RecordTrack* recordTrack);
Eric Laurent81784c32012-11-19 14:55:58 -0800978
979 void dump(int fd, const Vector<String16>& args);
980 AudioStreamIn* clearInput();
981 virtual audio_stream_t* stream() const;
982
Eric Laurent81784c32012-11-19 14:55:58 -0800983
984 virtual bool checkForNewParameters_l();
985 virtual String8 getParameters(const String8& keys);
986 virtual void audioConfigChanged_l(int event, int param = 0);
Glenn Kastendeca2ae2014-02-07 10:25:56 -0800987 void readInputParameters_l();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800988 virtual uint32_t getInputFramesLost();
Eric Laurent81784c32012-11-19 14:55:58 -0800989
990 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
991 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
992 virtual uint32_t hasAudioSession(int sessionId) const;
993
994 // Return the set of unique session IDs across all tracks.
995 // The keys are the session IDs, and the associated values are meaningless.
996 // FIXME replace by Set [and implement Bag/Multiset for other uses].
997 KeyedVector<int, bool> sessionIds() const;
998
999 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
1000 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
1001
1002 static void syncStartEventCallback(const wp<SyncEvent>& event);
Eric Laurent81784c32012-11-19 14:55:58 -08001003
Glenn Kasten9b58f632013-07-16 11:37:48 -07001004 virtual size_t frameCount() const { return mFrameCount; }
Glenn Kasten3a6c90a2014-03-13 15:07:51 -07001005 bool hasFastCapture() const { return false; }
Glenn Kasten9b58f632013-07-16 11:37:48 -07001006
Eric Laurent81784c32012-11-19 14:55:58 -08001007private:
Eric Laurent81784c32012-11-19 14:55:58 -08001008 // Enter standby if not already in standby, and set mStandby flag
Glenn Kasten93e471f2013-08-19 08:40:07 -07001009 void standbyIfNotAlreadyInStandby();
Eric Laurent81784c32012-11-19 14:55:58 -08001010
1011 // Call the HAL standby method unconditionally, and don't change mStandby flag
Glenn Kastene198c362013-08-13 09:13:36 -07001012 void inputStandBy();
Eric Laurent81784c32012-11-19 14:55:58 -08001013
1014 AudioStreamIn *mInput;
1015 SortedVector < sp<RecordTrack> > mTracks;
Glenn Kasten2b806402013-11-20 16:37:38 -08001016 // mActiveTracks has dual roles: it indicates the current active track(s), and
Eric Laurent81784c32012-11-19 14:55:58 -08001017 // is used together with mStartStopCond to indicate start()/stop() progress
Glenn Kasten2b806402013-11-20 16:37:38 -08001018 SortedVector< sp<RecordTrack> > mActiveTracks;
1019 // generation counter for mActiveTracks
1020 int mActiveTracksGen;
Eric Laurent81784c32012-11-19 14:55:58 -08001021 Condition mStartStopCond;
Glenn Kasten9b58f632013-07-16 11:37:48 -07001022
Glenn Kasten85948432013-08-19 12:09:05 -07001023 // resampler converts input at HAL Hz to output at AudioRecord client Hz
1024 int16_t *mRsmpInBuffer; // see new[] for details on the size
1025 size_t mRsmpInFrames; // size of resampler input in frames
1026 size_t mRsmpInFramesP2;// size rounded up to a power-of-2
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001027
1028 // rolling index that is never cleared
Glenn Kasten85948432013-08-19 12:09:05 -07001029 int32_t mRsmpInRear; // last filled frame + 1
Glenn Kasten85948432013-08-19 12:09:05 -07001030
Eric Laurent81784c32012-11-19 14:55:58 -08001031 // For dumpsys
1032 const sp<NBAIO_Sink> mTeeSink;
Glenn Kastenb880f5e2014-05-07 08:43:45 -07001033
1034 const sp<MemoryDealer> mReadOnlyHeap;
Eric Laurent81784c32012-11-19 14:55:58 -08001035};