blob: 6bac5951b917f85402605a5cedeb9bf5fcf00707 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/* //device/include/server/AudioFlinger/AudioFlinger.h
2**
3** Copyright 2007, 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 ANDROID_AUDIO_FLINGER_H
19#define ANDROID_AUDIO_FLINGER_H
20
21#include <stdint.h>
22#include <sys/types.h>
23#include <limits.h>
24
25#include <media/IAudioFlinger.h>
26#include <media/IAudioFlingerClient.h>
27#include <media/IAudioTrack.h>
28#include <media/IAudioRecord.h>
29#include <media/AudioTrack.h>
30
31#include <utils/Atomic.h>
32#include <utils/Errors.h>
33#include <utils/threads.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/SortedVector.h>
Dima Zavin799a70e2011-04-18 16:57:27 -070035#include <utils/TypeHelpers.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <utils/Vector.h>
37
Mathias Agopian5462fc92010-07-14 18:41:18 -070038#include <binder/BinderService.h>
39#include <binder/MemoryDealer.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioBufferProvider.h"
45
Eric Laurentfeb0db62011-07-22 09:04:31 -070046#include <powermanager/IPowerManager.h>
47
Mathias Agopian65ab4712010-07-14 17:59:35 -070048namespace android {
49
50class audio_track_cblk_t;
51class effect_param_cblk_t;
52class AudioMixer;
53class AudioBuffer;
54class AudioResampler;
55
Mathias Agopian65ab4712010-07-14 17:59:35 -070056// ----------------------------------------------------------------------------
57
58#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
59#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
60
61
62// ----------------------------------------------------------------------------
63
64static const nsecs_t kStandbyTimeInNsecs = seconds(3);
65
Mathias Agopian5462fc92010-07-14 18:41:18 -070066class AudioFlinger :
67 public BinderService<AudioFlinger>,
68 public BnAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -070069{
Mathias Agopian5462fc92010-07-14 18:41:18 -070070 friend class BinderService<AudioFlinger>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070071public:
Mathias Agopian5462fc92010-07-14 18:41:18 -070072 static char const* getServiceName() { return "media.audio_flinger"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
74 virtual status_t dump(int fd, const Vector<String16>& args);
75
76 // IAudioFlinger interface
77 virtual sp<IAudioTrack> createTrack(
78 pid_t pid,
79 int streamType,
80 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070081 uint32_t format,
82 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -070083 int frameCount,
84 uint32_t flags,
85 const sp<IMemory>& sharedBuffer,
86 int output,
Mike J. Chen500e5102011-08-15 13:28:26 -070087 bool isTimed,
Mathias Agopian65ab4712010-07-14 17:59:35 -070088 int *sessionId,
89 status_t *status);
90
91 virtual uint32_t sampleRate(int output) const;
92 virtual int channelCount(int output) const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070093 virtual uint32_t format(int output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 virtual size_t frameCount(int output) const;
95 virtual uint32_t latency(int output) const;
96
97 virtual status_t setMasterVolume(float value);
98 virtual status_t setMasterMute(bool muted);
99
100 virtual float masterVolume() const;
Mike J. Chen500e5102011-08-15 13:28:26 -0700101 virtual float masterVolumeSW() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102 virtual bool masterMute() const;
103
104 virtual status_t setStreamVolume(int stream, float value, int output);
105 virtual status_t setStreamMute(int stream, bool muted);
106
107 virtual float streamVolume(int stream, int output) const;
108 virtual bool streamMute(int stream) const;
109
110 virtual status_t setMode(int mode);
111
112 virtual status_t setMicMute(bool state);
113 virtual bool getMicMute() const;
114
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
116 virtual String8 getParameters(int ioHandle, const String8& keys);
117
118 virtual void registerClient(const sp<IAudioFlingerClient>& client);
119
120 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
121 virtual unsigned int getInputFramesLost(int ioHandle);
122
123 virtual int openOutput(uint32_t *pDevices,
124 uint32_t *pSamplingRate,
125 uint32_t *pFormat,
126 uint32_t *pChannels,
127 uint32_t *pLatencyMs,
128 uint32_t flags);
129
130 virtual int openDuplicateOutput(int output1, int output2);
131
132 virtual status_t closeOutput(int output);
133
134 virtual status_t suspendOutput(int output);
135
136 virtual status_t restoreOutput(int output);
137
138 virtual int openInput(uint32_t *pDevices,
139 uint32_t *pSamplingRate,
140 uint32_t *pFormat,
141 uint32_t *pChannels,
142 uint32_t acoustics);
143
144 virtual status_t closeInput(int input);
145
146 virtual status_t setStreamOutput(uint32_t stream, int output);
147
148 virtual status_t setVoiceVolume(float volume);
149
150 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
151
152 virtual int newAudioSessionId();
153
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700154 virtual void acquireAudioSessionId(int audioSession);
155
156 virtual void releaseAudioSessionId(int audioSession);
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 virtual status_t queryNumberEffects(uint32_t *numEffects);
159
160 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
161
162 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
163
164 virtual sp<IEffect> createEffect(pid_t pid,
165 effect_descriptor_t *pDesc,
166 const sp<IEffectClient>& effectClient,
167 int32_t priority,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700168 int io,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 int sessionId,
170 status_t *status,
171 int *id,
172 int *enabled);
173
Eric Laurent59255e42011-07-27 19:49:51 -0700174 virtual status_t moveEffects(int sessionId, int srcOutput, int dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175
176 enum hardware_call_state {
177 AUDIO_HW_IDLE = 0,
178 AUDIO_HW_INIT,
179 AUDIO_HW_OUTPUT_OPEN,
180 AUDIO_HW_OUTPUT_CLOSE,
181 AUDIO_HW_INPUT_OPEN,
182 AUDIO_HW_INPUT_CLOSE,
183 AUDIO_HW_STANDBY,
184 AUDIO_HW_SET_MASTER_VOLUME,
185 AUDIO_HW_GET_ROUTING,
186 AUDIO_HW_SET_ROUTING,
187 AUDIO_HW_GET_MODE,
188 AUDIO_HW_SET_MODE,
189 AUDIO_HW_GET_MIC_MUTE,
190 AUDIO_HW_SET_MIC_MUTE,
191 AUDIO_SET_VOICE_VOLUME,
192 AUDIO_SET_PARAMETER,
Mike J. Chen500e5102011-08-15 13:28:26 -0700193 AUDIO_HW_GET_MASTER_VOLUME,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 };
195
196 // record interface
197 virtual sp<IAudioRecord> openRecord(
198 pid_t pid,
199 int input,
200 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700201 uint32_t format,
202 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 int frameCount,
204 uint32_t flags,
205 int *sessionId,
206 status_t *status);
207
208 virtual status_t onTransact(
209 uint32_t code,
210 const Parcel& data,
211 Parcel* reply,
212 uint32_t flags);
213
214 uint32_t getMode() { return mMode; }
215
Eric Laurentbee53372011-08-29 12:42:48 -0700216 bool btNrecIsOff() { return mBtNrecIsOff; }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700217
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218private:
219 AudioFlinger();
220 virtual ~AudioFlinger();
221
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700222 status_t initCheck() const;
223 virtual void onFirstRef();
Dima Zavin799a70e2011-04-18 16:57:27 -0700224 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700225 void purgeStaleEffects_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226
227 // Internal dump utilites.
228 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
229 status_t dumpClients(int fd, const Vector<String16>& args);
230 status_t dumpInternals(int fd, const Vector<String16>& args);
231
232 // --- Client ---
233 class Client : public RefBase {
234 public:
235 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
236 virtual ~Client();
237 const sp<MemoryDealer>& heap() const;
238 pid_t pid() const { return mPid; }
239 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
240
Mike J. Chen500e5102011-08-15 13:28:26 -0700241 bool reserveTimedTrack();
242 void releaseTimedTrack();
243
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 private:
245 Client(const Client&);
246 Client& operator = (const Client&);
247 sp<AudioFlinger> mAudioFlinger;
248 sp<MemoryDealer> mMemoryDealer;
249 pid_t mPid;
Mike J. Chen500e5102011-08-15 13:28:26 -0700250
251 Mutex mTimedTrackLock;
252 int mTimedTrackCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 };
254
255 // --- Notification Client ---
256 class NotificationClient : public IBinder::DeathRecipient {
257 public:
258 NotificationClient(const sp<AudioFlinger>& audioFlinger,
259 const sp<IAudioFlingerClient>& client,
260 pid_t pid);
261 virtual ~NotificationClient();
262
263 sp<IAudioFlingerClient> client() { return mClient; }
264
265 // IBinder::DeathRecipient
266 virtual void binderDied(const wp<IBinder>& who);
267
268 private:
269 NotificationClient(const NotificationClient&);
270 NotificationClient& operator = (const NotificationClient&);
271
272 sp<AudioFlinger> mAudioFlinger;
273 pid_t mPid;
274 sp<IAudioFlingerClient> mClient;
275 };
276
277 class TrackHandle;
278 class RecordHandle;
279 class RecordThread;
280 class PlaybackThread;
281 class MixerThread;
282 class DirectOutputThread;
283 class DuplicatingThread;
284 class Track;
285 class RecordTrack;
286 class EffectModule;
287 class EffectHandle;
288 class EffectChain;
Dima Zavin799a70e2011-04-18 16:57:27 -0700289 struct AudioStreamOut;
290 struct AudioStreamIn;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291
292 class ThreadBase : public Thread {
293 public:
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700294 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 virtual ~ThreadBase();
296
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700297
298 enum type {
299 MIXER, // Thread class is MixerThread
300 DIRECT, // Thread class is DirectOutputThread
301 DUPLICATING, // Thread class is DuplicatingThread
302 RECORD // Thread class is RecordThread
303 };
304
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 status_t dumpBase(int fd, const Vector<String16>& args);
Eric Laurent1d2bff02011-07-24 17:49:51 -0700306 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307
Eric Laurentfeb0db62011-07-22 09:04:31 -0700308 void clearPowerManager();
309
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 // base for record and playback
311 class TrackBase : public AudioBufferProvider, public RefBase {
312
313 public:
314 enum track_state {
315 IDLE,
316 TERMINATED,
317 STOPPED,
318 RESUMING,
319 ACTIVE,
320 PAUSING,
321 PAUSED
322 };
323
324 enum track_flags {
325 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
326 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
327 // The upper 16 bits are used for track-specific flags.
328 };
329
330 TrackBase(const wp<ThreadBase>& thread,
331 const sp<Client>& client,
332 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700333 uint32_t format,
334 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 int frameCount,
336 uint32_t flags,
337 const sp<IMemory>& sharedBuffer,
338 int sessionId);
339 ~TrackBase();
340
341 virtual status_t start() = 0;
342 virtual void stop() = 0;
343 sp<IMemory> getCblk() const;
344 audio_track_cblk_t* cblk() const { return mCblk; }
345 int sessionId() { return mSessionId; }
346
347 protected:
348 friend class ThreadBase;
349 friend class RecordHandle;
350 friend class PlaybackThread;
351 friend class RecordThread;
352 friend class MixerThread;
353 friend class DirectOutputThread;
354
355 TrackBase(const TrackBase&);
356 TrackBase& operator = (const TrackBase&);
357
Mike J. Chen500e5102011-08-15 13:28:26 -0700358 virtual status_t getNextBuffer(
359 AudioBufferProvider::Buffer* buffer,
360 int64_t pts) = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
362
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700363 uint32_t format() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 return mFormat;
365 }
366
367 int channelCount() const ;
368
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700369 uint32_t channelMask() const;
370
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 int sampleRate() const;
372
373 void* getBuffer(uint32_t offset, uint32_t frames) const;
374
375 bool isStopped() const {
376 return mState == STOPPED;
377 }
378
379 bool isTerminated() const {
380 return mState == TERMINATED;
381 }
382
383 bool step();
384 void reset();
385
386 wp<ThreadBase> mThread;
387 sp<Client> mClient;
388 sp<IMemory> mCblkMemory;
389 audio_track_cblk_t* mCblk;
390 void* mBuffer;
391 void* mBufferEnd;
392 uint32_t mFrameCount;
393 // we don't really need a lock for these
394 int mState;
395 int mClientTid;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700396 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 uint32_t mFlags;
398 int mSessionId;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700399 uint8_t mChannelCount;
400 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 };
402
403 class ConfigEvent {
404 public:
405 ConfigEvent() : mEvent(0), mParam(0) {}
406
407 int mEvent;
408 int mParam;
409 };
410
Eric Laurentfeb0db62011-07-22 09:04:31 -0700411 class PMDeathRecipient : public IBinder::DeathRecipient {
412 public:
413 PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
414 virtual ~PMDeathRecipient() {}
415
416 // IBinder::DeathRecipient
417 virtual void binderDied(const wp<IBinder>& who);
418
419 private:
420 PMDeathRecipient(const PMDeathRecipient&);
421 PMDeathRecipient& operator = (const PMDeathRecipient&);
422
423 wp<ThreadBase> mThread;
424 };
425
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700426 virtual status_t initCheck() const = 0;
427 int type() const { return mType; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 uint32_t sampleRate() const;
429 int channelCount() const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700430 uint32_t format() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 size_t frameCount() const;
432 void wakeUp() { mWaitWorkCV.broadcast(); }
433 void exit();
434 virtual bool checkForNewParameters_l() = 0;
435 virtual status_t setParameters(const String8& keyValuePairs);
436 virtual String8 getParameters(const String8& keys) = 0;
437 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
438 void sendConfigEvent(int event, int param = 0);
439 void sendConfigEvent_l(int event, int param = 0);
440 void processConfigEvents();
441 int id() const { return mId;}
442 bool standby() { return mStandby; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700443 uint32_t device() { return mDevice; }
444 virtual audio_stream_t* stream() = 0;
445
446 sp<EffectHandle> createEffect_l(
447 const sp<AudioFlinger::Client>& client,
448 const sp<IEffectClient>& effectClient,
449 int32_t priority,
450 int sessionId,
451 effect_descriptor_t *desc,
452 int *enabled,
453 status_t *status);
454 void disconnectEffect(const sp< EffectModule>& effect,
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700455 const wp<EffectHandle>& handle,
456 bool unpiniflast);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700457
458 // return values for hasAudioSession (bit field)
459 enum effect_state {
460 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
461 // effect
462 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
463 // track
464 };
465
466 // get effect chain corresponding to session Id.
467 sp<EffectChain> getEffectChain(int sessionId);
468 // same as getEffectChain() but must be called with ThreadBase mutex locked
469 sp<EffectChain> getEffectChain_l(int sessionId);
470 // add an effect chain to the chain list (mEffectChains)
471 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
472 // remove an effect chain from the chain list (mEffectChains)
473 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
474 // lock mall effect chains Mutexes. Must be called before releasing the
475 // ThreadBase mutex before processing the mixer and effects. This guarantees the
476 // integrity of the chains during the process.
477 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
478 // unlock effect chains after process
479 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
480 // set audio mode to all effect chains
481 void setMode(uint32_t mode);
482 // get effect module with corresponding ID on specified audio session
483 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
484 // add and effect module. Also creates the effect chain is none exists for
485 // the effects audio session
486 status_t addEffect_l(const sp< EffectModule>& effect);
487 // remove and effect module. Also removes the effect chain is this was the last
488 // effect
489 void removeEffect_l(const sp< EffectModule>& effect);
490 // detach all tracks connected to an auxiliary effect
491 virtual void detachAuxEffect_l(int effectId) {}
492 // returns either EFFECT_SESSION if effects on this audio session exist in one
493 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
494 virtual uint32_t hasAudioSession(int sessionId) = 0;
495 // the value returned by default implementation is not important as the
496 // strategy is only meaningful for PlaybackThread which implements this method
497 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498
Eric Laurent59255e42011-07-27 19:49:51 -0700499 // suspend or restore effect according to the type of effect passed. a NULL
500 // type pointer means suspend all effects in the session
501 void setEffectSuspended(const effect_uuid_t *type,
502 bool suspend,
503 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
504 // check if some effects must be suspended/restored when an effect is enabled
505 // or disabled
Eric Laurenta85a74a2011-10-19 11:44:54 -0700506 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
Eric Laurent59255e42011-07-27 19:49:51 -0700507 bool enabled,
508 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
Eric Laurenta85a74a2011-10-19 11:44:54 -0700509 void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
510 bool enabled,
511 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 mutable Mutex mLock;
513
514 protected:
515
Eric Laurent59255e42011-07-27 19:49:51 -0700516 // entry describing an effect being suspended in mSuspendedSessions keyed vector
517 class SuspendedSessionDesc : public RefBase {
518 public:
519 SuspendedSessionDesc() : mRefCount(0) {}
520
521 int mRefCount; // number of active suspend requests
522 effect_uuid_t mType; // effect type UUID
523 };
524
Eric Laurentfeb0db62011-07-22 09:04:31 -0700525 void acquireWakeLock();
526 void acquireWakeLock_l();
527 void releaseWakeLock();
528 void releaseWakeLock_l();
Eric Laurent59255e42011-07-27 19:49:51 -0700529 void setEffectSuspended_l(const effect_uuid_t *type,
530 bool suspend,
531 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
532 // updated mSuspendedSessions when an effect suspended or restored
533 void updateSuspendedSessions_l(const effect_uuid_t *type,
534 bool suspend,
535 int sessionId);
536 // check if some effects must be suspended when an effect chain is added
537 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
Eric Laurentfeb0db62011-07-22 09:04:31 -0700538
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700539 friend class AudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540 friend class Track;
541 friend class TrackBase;
542 friend class PlaybackThread;
543 friend class MixerThread;
544 friend class DirectOutputThread;
545 friend class DuplicatingThread;
546 friend class RecordThread;
547 friend class RecordTrack;
548
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700549 int mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 Condition mWaitWorkCV;
551 sp<AudioFlinger> mAudioFlinger;
552 uint32_t mSampleRate;
553 size_t mFrameCount;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700554 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555 uint16_t mChannelCount;
556 uint16_t mFrameSize;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700557 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 Condition mParamCond;
559 Vector<String8> mNewParameters;
560 status_t mParamStatus;
561 Vector<ConfigEvent *> mConfigEvents;
562 bool mStandby;
563 int mId;
564 bool mExiting;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700565 Vector< sp<EffectChain> > mEffectChains;
566 uint32_t mDevice; // output device for PlaybackThread
567 // input + output devices for RecordThread
Eric Laurentfeb0db62011-07-22 09:04:31 -0700568 static const int kNameLength = 32;
569 char mName[kNameLength];
570 sp<IPowerManager> mPowerManager;
571 sp<IBinder> mWakeLockToken;
572 sp<PMDeathRecipient> mDeathRecipient;
Eric Laurent59255e42011-07-27 19:49:51 -0700573 // list of suspended effects per session and per type. The first vector is
574 // keyed by session ID, the second by type UUID timeLow field
575 KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > > mSuspendedSessions;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 };
577
578 // --- PlaybackThread ---
579 class PlaybackThread : public ThreadBase {
580 public:
581
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 enum mixer_state {
583 MIXER_IDLE,
584 MIXER_TRACKS_ENABLED,
585 MIXER_TRACKS_READY
586 };
587
588 // playback track
589 class Track : public TrackBase {
590 public:
591 Track( const wp<ThreadBase>& thread,
592 const sp<Client>& client,
593 int streamType,
594 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700595 uint32_t format,
596 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 int frameCount,
598 const sp<IMemory>& sharedBuffer,
599 int sessionId);
600 ~Track();
601
602 void dump(char* buffer, size_t size);
603 virtual status_t start();
604 virtual void stop();
605 void pause();
606
607 void flush();
608 void destroy();
609 void mute(bool);
610 void setVolume(float left, float right);
611 int name() const {
612 return mName;
613 }
614
615 int type() const {
616 return mStreamType;
617 }
618 status_t attachAuxEffect(int EffectId);
619 void setAuxBuffer(int EffectId, int32_t *buffer);
620 int32_t *auxBuffer() { return mAuxBuffer; }
621 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
622 int16_t *mainBuffer() { return mMainBuffer; }
623 int auxEffectId() { return mAuxEffectId; }
624
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 protected:
626 friend class ThreadBase;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627 friend class TrackHandle;
628 friend class PlaybackThread;
629 friend class MixerThread;
630 friend class DirectOutputThread;
631
632 Track(const Track&);
633 Track& operator = (const Track&);
634
Mike J. Chen500e5102011-08-15 13:28:26 -0700635 virtual status_t getNextBuffer(
636 AudioBufferProvider::Buffer* buffer,
637 int64_t pts);
638 virtual uint32_t framesReady() const;
639
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 bool isMuted() { return mMute; }
641 bool isPausing() const {
642 return mState == PAUSING;
643 }
644 bool isPaused() const {
645 return mState == PAUSED;
646 }
647 bool isReady() const;
648 void setPaused() { mState = PAUSED; }
649 void reset();
650
651 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700652 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 }
654
Mike J. Chen500e5102011-08-15 13:28:26 -0700655 virtual bool isTimedTrack() const { return false; }
656
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 // we don't really need a lock for these
658 float mVolume[2];
659 volatile bool mMute;
660 // FILLED state is used for suppressing volume ramp at begin of playing
661 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
662 mutable uint8_t mFillingUpStatus;
663 int8_t mRetryCount;
664 sp<IMemory> mSharedBuffer;
665 bool mResetDone;
666 int mStreamType;
667 int mName;
668 int16_t *mMainBuffer;
669 int32_t *mAuxBuffer;
670 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700671 bool mHasVolumeController;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 }; // end of Track
673
Mike J. Chen500e5102011-08-15 13:28:26 -0700674 class TimedTrack : public Track {
675 public:
676 static sp<TimedTrack> create(const wp<ThreadBase>& thread,
677 const sp<Client>& client,
678 int streamType,
679 uint32_t sampleRate,
680 uint32_t format,
681 uint32_t channelMask,
682 int frameCount,
683 const sp<IMemory>& sharedBuffer,
684 int sessionId);
685 ~TimedTrack();
686
687 class TimedBuffer {
688 public:
689 TimedBuffer();
690 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
691 const sp<IMemory>& buffer() const { return mBuffer; }
692 int64_t pts() const { return mPTS; }
693 int position() const { return mPosition; }
694 void setPosition(int pos) { mPosition = pos; }
695 private:
696 sp<IMemory> mBuffer;
697 int64_t mPTS;
698 int mPosition;
699 };
700
701 virtual bool isTimedTrack() const { return true; }
702
703 virtual uint32_t framesReady() const;
704
705 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
706 int64_t pts);
707 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
708 void timedYieldSamples(AudioBufferProvider::Buffer* buffer);
709 void timedYieldSilence(uint32_t numFrames,
710 AudioBufferProvider::Buffer* buffer);
711
712 status_t allocateTimedBuffer(size_t size,
713 sp<IMemory>* buffer);
714 status_t queueTimedBuffer(const sp<IMemory>& buffer,
715 int64_t pts);
716 status_t setMediaTimeTransform(const LinearTransform& xform,
717 TimedAudioTrack::TargetTimeline target);
718 void trimTimedBufferQueue_l();
719
720 private:
721 TimedTrack(const wp<ThreadBase>& thread,
722 const sp<Client>& client,
723 int streamType,
724 uint32_t sampleRate,
725 uint32_t format,
726 uint32_t channelMask,
727 int frameCount,
728 const sp<IMemory>& sharedBuffer,
729 int sessionId);
730
731 uint64_t mLocalTimeFreq;
732 LinearTransform mLocalTimeToSampleTransform;
733 sp<MemoryDealer> mTimedMemoryDealer;
734 Vector<TimedBuffer> mTimedBufferQueue;
735 uint8_t* mTimedSilenceBuffer;
736 uint32_t mTimedSilenceBufferSize;
737 mutable Mutex mTimedBufferQueueLock;
738 bool mTimedAudioOutputOnTime;
739
740 Mutex mMediaTimeTransformLock;
741 LinearTransform mMediaTimeTransform;
742 bool mMediaTimeTransformValid;
743 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
744 };
745
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746
747 // playback track
748 class OutputTrack : public Track {
749 public:
750
751 class Buffer: public AudioBufferProvider::Buffer {
752 public:
753 int16_t *mBuffer;
754 };
755
756 OutputTrack( const wp<ThreadBase>& thread,
757 DuplicatingThread *sourceThread,
758 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700759 uint32_t format,
760 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 int frameCount);
762 ~OutputTrack();
763
764 virtual status_t start();
765 virtual void stop();
766 bool write(int16_t* data, uint32_t frames);
767 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
768 bool isActive() { return mActive; }
769 wp<ThreadBase>& thread() { return mThread; }
770
771 private:
772
773 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
774 void clearBufferQueue();
775
776 // Maximum number of pending buffers allocated by OutputTrack::write()
777 static const uint8_t kMaxOverFlowBuffers = 10;
778
779 Vector < Buffer* > mBufferQueue;
780 AudioBufferProvider::Buffer mOutBuffer;
781 bool mActive;
782 DuplicatingThread* mSourceThread;
783 }; // end of OutputTrack
784
Dima Zavin799a70e2011-04-18 16:57:27 -0700785 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786 virtual ~PlaybackThread();
787
788 virtual status_t dump(int fd, const Vector<String16>& args);
789
790 // Thread virtuals
791 virtual status_t readyToRun();
792 virtual void onFirstRef();
793
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700794 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
795
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 virtual uint32_t latency() const;
797
798 virtual status_t setMasterVolume(float value);
799 virtual status_t setMasterMute(bool muted);
800
801 virtual float masterVolume() const;
802 virtual bool masterMute() const;
803
804 virtual status_t setStreamVolume(int stream, float value);
805 virtual status_t setStreamMute(int stream, bool muted);
806
807 virtual float streamVolume(int stream) const;
808 virtual bool streamMute(int stream) const;
809
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 sp<Track> createTrack_l(
811 const sp<AudioFlinger::Client>& client,
812 int streamType,
813 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700814 uint32_t format,
815 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 int frameCount,
817 const sp<IMemory>& sharedBuffer,
818 int sessionId,
Mike J. Chen500e5102011-08-15 13:28:26 -0700819 bool isTimed,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 status_t *status);
821
Eric Laurentb8ba0a92011-08-07 16:32:26 -0700822 AudioStreamOut* getOutput();
823 AudioStreamOut* clearOutput();
824 virtual audio_stream_t* stream();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 void suspend() { mSuspended++; }
827 void restore() { if (mSuspended) mSuspended--; }
828 bool isSuspended() { return (mSuspended != 0); }
829 virtual String8 getParameters(const String8& keys);
830 virtual void audioConfigChanged_l(int event, int param = 0);
831 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
832 int16_t *mixBuffer() { return mMixBuffer; };
833
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700834 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -0700835 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
836 int EffectId);
837 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
838 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700840 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
841 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
842 virtual uint32_t hasAudioSession(int sessionId);
843 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -0700844
Eric Laurent9f6530f2011-08-30 10:18:54 -0700845 void setStreamValid(int streamType, bool valid);
846
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 struct stream_type_t {
848 stream_type_t()
849 : volume(1.0f),
Eric Laurent9f6530f2011-08-30 10:18:54 -0700850 mute(false),
851 valid(true)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 {
853 }
854 float volume;
855 bool mute;
Eric Laurent9f6530f2011-08-30 10:18:54 -0700856 bool valid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 };
858
859 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 int16_t* mMixBuffer;
861 int mSuspended;
862 int mBytesWritten;
863 bool mMasterMute;
864 SortedVector< wp<Track> > mActiveTracks;
865
866 virtual int getTrackName_l() = 0;
867 virtual void deleteTrackName_l(int name) = 0;
868 virtual uint32_t activeSleepTimeUs() = 0;
869 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700870 virtual uint32_t suspendSleepTimeUs() = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871
872 private:
873
874 friend class AudioFlinger;
875 friend class OutputTrack;
876 friend class Track;
877 friend class TrackBase;
878 friend class MixerThread;
879 friend class DirectOutputThread;
880 friend class DuplicatingThread;
881
882 PlaybackThread(const Client&);
883 PlaybackThread& operator = (const PlaybackThread&);
884
885 status_t addTrack_l(const sp<Track>& track);
886 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -0700887 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888
889 void readOutputParameters();
890
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
892 status_t dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893
894 SortedVector< sp<Track> > mTracks;
895 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -0700896 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin799a70e2011-04-18 16:57:27 -0700897 AudioStreamOut* mOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898 float mMasterVolume;
899 nsecs_t mLastWriteTime;
900 int mNumWrites;
901 int mNumDelayedWrites;
902 bool mInWrite;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 };
904
905 class MixerThread : public PlaybackThread {
906 public:
Eric Laurentde070132010-07-13 04:45:46 -0700907 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700908 AudioStreamOut* output,
Eric Laurentde070132010-07-13 04:45:46 -0700909 int id,
910 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 virtual ~MixerThread();
912
913 // Thread virtuals
914 virtual bool threadLoop();
915
916 void invalidateTracks(int streamType);
917 virtual bool checkForNewParameters_l();
918 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
919
920 protected:
Eric Laurentde070132010-07-13 04:45:46 -0700921 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
922 Vector< sp<Track> > *tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 virtual int getTrackName_l();
924 virtual void deleteTrackName_l(int name);
925 virtual uint32_t activeSleepTimeUs();
926 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700927 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928
929 AudioMixer* mAudioMixer;
930 };
931
932 class DirectOutputThread : public PlaybackThread {
933 public:
934
Dima Zavin799a70e2011-04-18 16:57:27 -0700935 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 ~DirectOutputThread();
937
938 // Thread virtuals
939 virtual bool threadLoop();
940
941 virtual bool checkForNewParameters_l();
942
943 protected:
944 virtual int getTrackName_l();
945 virtual void deleteTrackName_l(int name);
946 virtual uint32_t activeSleepTimeUs();
947 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700948 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949
950 private:
951 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
952
953 float mLeftVolFloat;
954 float mRightVolFloat;
955 uint16_t mLeftVolShort;
956 uint16_t mRightVolShort;
957 };
958
959 class DuplicatingThread : public MixerThread {
960 public:
961 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
962 ~DuplicatingThread();
963
964 // Thread virtuals
965 virtual bool threadLoop();
966 void addOutputTrack(MixerThread* thread);
967 void removeOutputTrack(MixerThread* thread);
968 uint32_t waitTimeMs() { return mWaitTimeMs; }
969 protected:
970 virtual uint32_t activeSleepTimeUs();
971
972 private:
973 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
974 void updateWaitTime();
975
976 SortedVector < sp<OutputTrack> > mOutputTracks;
977 uint32_t mWaitTimeMs;
978 };
979
980 PlaybackThread *checkPlaybackThread_l(int output) const;
981 MixerThread *checkMixerThread_l(int output) const;
982 RecordThread *checkRecordThread_l(int input) const;
983 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
984 void audioConfigChanged_l(int event, int ioHandle, void *param2);
985
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700986 uint32_t nextUniqueId();
Eric Laurent59255e42011-07-27 19:49:51 -0700987 status_t moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -0700988 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -0700989 AudioFlinger::PlaybackThread *dstThread,
990 bool reRegister);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700991 PlaybackThread *primaryPlaybackThread_l();
992 uint32_t primaryOutputDevice_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993
994 friend class AudioBuffer;
995
996 class TrackHandle : public android::BnAudioTrack {
997 public:
998 TrackHandle(const sp<PlaybackThread::Track>& track);
999 virtual ~TrackHandle();
1000 virtual status_t start();
1001 virtual void stop();
1002 virtual void flush();
1003 virtual void mute(bool);
1004 virtual void pause();
1005 virtual void setVolume(float left, float right);
1006 virtual sp<IMemory> getCblk() const;
1007 virtual status_t attachAuxEffect(int effectId);
Mike J. Chen500e5102011-08-15 13:28:26 -07001008 virtual status_t allocateTimedBuffer(size_t size,
1009 sp<IMemory>* buffer);
1010 virtual status_t queueTimedBuffer(const sp<IMemory>& buffer,
1011 int64_t pts);
1012 virtual status_t setMediaTimeTransform(const LinearTransform& xform,
1013 int target);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014 virtual status_t onTransact(
1015 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1016 private:
1017 sp<PlaybackThread::Track> mTrack;
1018 };
1019
1020 friend class Client;
1021 friend class PlaybackThread::Track;
1022
1023
1024 void removeClient_l(pid_t pid);
1025 void removeNotificationClient(pid_t pid);
1026
1027
1028 // record thread
1029 class RecordThread : public ThreadBase, public AudioBufferProvider
1030 {
1031 public:
1032
1033 // record track
1034 class RecordTrack : public TrackBase {
1035 public:
1036 RecordTrack(const wp<ThreadBase>& thread,
1037 const sp<Client>& client,
1038 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001039 uint32_t format,
1040 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 int frameCount,
1042 uint32_t flags,
1043 int sessionId);
1044 ~RecordTrack();
1045
1046 virtual status_t start();
1047 virtual void stop();
1048
1049 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
1050 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
1051
1052 void dump(char* buffer, size_t size);
Eric Laurent59255e42011-07-27 19:49:51 -07001053
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054 private:
1055 friend class AudioFlinger;
1056 friend class RecordThread;
1057
1058 RecordTrack(const RecordTrack&);
1059 RecordTrack& operator = (const RecordTrack&);
1060
Mike J. Chen500e5102011-08-15 13:28:26 -07001061 virtual status_t getNextBuffer(
1062 AudioBufferProvider::Buffer* buffer,
1063 int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064
1065 bool mOverflow;
1066 };
1067
1068
1069 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -07001070 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 uint32_t sampleRate,
1072 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001073 int id,
1074 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 ~RecordThread();
1076
1077 virtual bool threadLoop();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001078 virtual status_t readyToRun();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 virtual void onFirstRef();
1080
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001081 virtual status_t initCheck() const { return (mInput == 0) ? NO_INIT : NO_ERROR; }
1082 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
1083 const sp<AudioFlinger::Client>& client,
1084 uint32_t sampleRate,
1085 int format,
1086 int channelMask,
1087 int frameCount,
1088 uint32_t flags,
1089 int sessionId,
1090 status_t *status);
1091
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092 status_t start(RecordTrack* recordTrack);
1093 void stop(RecordTrack* recordTrack);
1094 status_t dump(int fd, const Vector<String16>& args);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001095 AudioStreamIn* getInput();
1096 AudioStreamIn* clearInput();
1097 virtual audio_stream_t* stream();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098
Mike J. Chen500e5102011-08-15 13:28:26 -07001099 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
1100 int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
1102 virtual bool checkForNewParameters_l();
1103 virtual String8 getParameters(const String8& keys);
1104 virtual void audioConfigChanged_l(int event, int param = 0);
1105 void readInputParameters();
1106 virtual unsigned int getInputFramesLost();
1107
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001108 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1109 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
1110 virtual uint32_t hasAudioSession(int sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001111 RecordTrack* track();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001112
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 private:
1114 RecordThread();
Dima Zavin799a70e2011-04-18 16:57:27 -07001115 AudioStreamIn *mInput;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001116 RecordTrack* mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 sp<RecordTrack> mActiveTrack;
1118 Condition mStartStopCond;
1119 AudioResampler *mResampler;
1120 int32_t *mRsmpOutBuffer;
1121 int16_t *mRsmpInBuffer;
1122 size_t mRsmpInIndex;
1123 size_t mInputBytes;
1124 int mReqChannelCount;
1125 uint32_t mReqSampleRate;
1126 ssize_t mBytesRead;
1127 };
1128
1129 class RecordHandle : public android::BnAudioRecord {
1130 public:
1131 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
1132 virtual ~RecordHandle();
1133 virtual status_t start();
1134 virtual void stop();
1135 virtual sp<IMemory> getCblk() const;
1136 virtual status_t onTransact(
1137 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1138 private:
1139 sp<RecordThread::RecordTrack> mRecordTrack;
1140 };
1141
1142 //--- Audio Effect Management
1143
1144 // EffectModule and EffectChain classes both have their own mutex to protect
1145 // state changes or resource modifications. Always respect the following order
1146 // if multiple mutexes must be acquired to avoid cross deadlock:
1147 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1148
1149 // The EffectModule class is a wrapper object controlling the effect engine implementation
1150 // in the effect library. It prevents concurrent calls to process() and command() functions
1151 // from different client threads. It keeps a list of EffectHandle objects corresponding
1152 // to all client applications using this effect and notifies applications of effect state,
1153 // control or parameter changes. It manages the activation state machine to send appropriate
1154 // reset, enable, disable commands to effect engine and provide volume
1155 // ramping when effects are activated/deactivated.
1156 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1157 // the attached track(s) to accumulate their auxiliary channel.
1158 class EffectModule: public RefBase {
1159 public:
1160 EffectModule(const wp<ThreadBase>& wThread,
1161 const wp<AudioFlinger::EffectChain>& chain,
1162 effect_descriptor_t *desc,
1163 int id,
1164 int sessionId);
1165 ~EffectModule();
1166
1167 enum effect_state {
1168 IDLE,
1169 RESTART,
1170 STARTING,
1171 ACTIVE,
1172 STOPPING,
Eric Laurentec437d82011-07-26 20:54:46 -07001173 STOPPED,
1174 DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 };
1176
1177 int id() { return mId; }
1178 void process();
1179 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001180 status_t command(uint32_t cmdCode,
1181 uint32_t cmdSize,
1182 void *pCmdData,
1183 uint32_t *replySize,
1184 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185
1186 void reset_l();
1187 status_t configure();
1188 status_t init();
1189 uint32_t state() {
1190 return mState;
1191 }
1192 uint32_t status() {
1193 return mStatus;
1194 }
Eric Laurentde070132010-07-13 04:45:46 -07001195 int sessionId() {
1196 return mSessionId;
1197 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198 status_t setEnabled(bool enabled);
1199 bool isEnabled();
Eric Laurent8f45bd72010-08-31 13:50:07 -07001200 bool isProcessEnabled();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201
1202 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1203 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1204 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1205 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001206 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1207 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent59255e42011-07-27 19:49:51 -07001208 wp<ThreadBase>& thread() { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209
1210 status_t addHandle(sp<EffectHandle>& handle);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001211 void disconnect(const wp<EffectHandle>& handle, bool unpiniflast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212 size_t removeHandle (const wp<EffectHandle>& handle);
1213
1214 effect_descriptor_t& desc() { return mDescriptor; }
1215 wp<EffectChain>& chain() { return mChain; }
1216
1217 status_t setDevice(uint32_t device);
1218 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
1219 status_t setMode(uint32_t mode);
Eric Laurentec35a142011-10-05 17:42:25 -07001220 status_t start();
Eric Laurentec437d82011-07-26 20:54:46 -07001221 status_t stop();
Eric Laurent59255e42011-07-27 19:49:51 -07001222 void setSuspended(bool suspended);
1223 bool suspended();
1224
1225 sp<EffectHandle> controlHandle();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001227 bool isPinned() { return mPinned; }
1228 void unPin() { mPinned = false; }
1229
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 status_t dump(int fd, const Vector<String16>& args);
1231
1232 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001233 friend class EffectHandle;
1234 friend class AudioFlinger;
1235 bool mPinned;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236
1237 // Maximum time allocated to effect engines to complete the turn off sequence
1238 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1239
1240 EffectModule(const EffectModule&);
1241 EffectModule& operator = (const EffectModule&);
1242
1243 status_t start_l();
1244 status_t stop_l();
1245
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246 Mutex mLock; // mutex for process, commands and handles list protection
1247 wp<ThreadBase> mThread; // parent thread
1248 wp<EffectChain> mChain; // parent effect chain
1249 int mId; // this instance unique ID
1250 int mSessionId; // audio session ID
1251 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1252 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001253 effect_handle_t mEffectInterface; // Effect module C API
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254 status_t mStatus; // initialization status
1255 uint32_t mState; // current activation state (effect_state)
1256 Vector< wp<EffectHandle> > mHandles; // list of client handles
1257 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1258 // sending disable command.
1259 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent59255e42011-07-27 19:49:51 -07001260 bool mSuspended; // effect is suspended: temporarily disabled by framework
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 };
1262
1263 // The EffectHandle class implements the IEffect interface. It provides resources
1264 // to receive parameter updates, keeps track of effect control
1265 // ownership and state and has a pointer to the EffectModule object it is controlling.
1266 // There is one EffectHandle object for each application controlling (or using)
1267 // an effect module.
1268 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1269 class EffectHandle: public android::BnEffect {
1270 public:
1271
1272 EffectHandle(const sp<EffectModule>& effect,
1273 const sp<AudioFlinger::Client>& client,
1274 const sp<IEffectClient>& effectClient,
1275 int32_t priority);
1276 virtual ~EffectHandle();
1277
1278 // IEffect
1279 virtual status_t enable();
1280 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001281 virtual status_t command(uint32_t cmdCode,
1282 uint32_t cmdSize,
1283 void *pCmdData,
1284 uint32_t *replySize,
1285 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 virtual void disconnect();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001287 virtual void disconnect(bool unpiniflast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 virtual sp<IMemory> getCblk() const;
1289 virtual status_t onTransact(uint32_t code, const Parcel& data,
1290 Parcel* reply, uint32_t flags);
1291
1292
1293 // Give or take control of effect module
Eric Laurent59255e42011-07-27 19:49:51 -07001294 // - hasControl: true if control is given, false if removed
1295 // - signal: true client app should be signaled of change, false otherwise
1296 // - enabled: state of the effect when control is passed
1297 void setControl(bool hasControl, bool signal, bool enabled);
Eric Laurent25f43952010-07-28 05:40:18 -07001298 void commandExecuted(uint32_t cmdCode,
1299 uint32_t cmdSize,
1300 void *pCmdData,
1301 uint32_t replySize,
1302 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303 void setEnabled(bool enabled);
Eric Laurent59255e42011-07-27 19:49:51 -07001304 bool enabled() { return mEnabled; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305
1306 // Getters
1307 int id() { return mEffect->id(); }
1308 int priority() { return mPriority; }
1309 bool hasControl() { return mHasControl; }
1310 sp<EffectModule> effect() { return mEffect; }
1311
1312 void dump(char* buffer, size_t size);
1313
1314 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001315 friend class AudioFlinger;
1316 friend class EffectModule;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001317 EffectHandle(const EffectHandle&);
1318 EffectHandle& operator =(const EffectHandle&);
1319
1320 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1321 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1322 sp<Client> mClient; // client for shared memory allocation
1323 sp<IMemory> mCblkMemory; // shared memory for control block
1324 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1325 uint8_t* mBuffer; // pointer to parameter area in shared memory
1326 int mPriority; // client application priority to control the effect
1327 bool mHasControl; // true if this handle is controlling the effect
Eric Laurent59255e42011-07-27 19:49:51 -07001328 bool mEnabled; // cached enable state: needed when the effect is
1329 // restored after being suspended
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 };
1331
1332 // the EffectChain class represents a group of effects associated to one audio session.
1333 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1334 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1335 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1336 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1337 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1338 // input buffer used by the track as accumulation buffer.
1339 class EffectChain: public RefBase {
1340 public:
1341 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1342 ~EffectChain();
1343
Eric Laurent59255e42011-07-27 19:49:51 -07001344 // special key used for an entry in mSuspendedEffects keyed vector
1345 // corresponding to a suspend all request.
1346 static const int kKeyForSuspendAll = 0;
1347
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348 void process_l();
1349
1350 void lock() {
1351 mLock.lock();
1352 }
1353 void unlock() {
1354 mLock.unlock();
1355 }
1356
Eric Laurentde070132010-07-13 04:45:46 -07001357 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001358 size_t removeEffect_l(const sp<EffectModule>& handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001360 int sessionId() { return mSessionId; }
1361 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001362
Eric Laurentcab11242010-07-15 12:50:15 -07001363 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1364 sp<EffectModule> getEffectFromId_l(int id);
Eric Laurent59255e42011-07-27 19:49:51 -07001365 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurentcab11242010-07-15 12:50:15 -07001366 bool setVolume_l(uint32_t *left, uint32_t *right);
1367 void setDevice_l(uint32_t device);
1368 void setMode_l(uint32_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1371 mInBuffer = buffer;
1372 mOwnInBuffer = ownsBuffer;
1373 }
1374 int16_t *inBuffer() {
1375 return mInBuffer;
1376 }
1377 void setOutBuffer(int16_t *buffer) {
1378 mOutBuffer = buffer;
1379 }
1380 int16_t *outBuffer() {
1381 return mOutBuffer;
1382 }
1383
Eric Laurentb469b942011-05-09 12:09:06 -07001384 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1385 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1386 int32_t trackCnt() { return mTrackCnt;}
1387
1388 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1389 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1390 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391
Eric Laurentde070132010-07-13 04:45:46 -07001392 uint32_t strategy() { return mStrategy; }
1393 void setStrategy(uint32_t strategy)
1394 { mStrategy = strategy; }
1395
Eric Laurent59255e42011-07-27 19:49:51 -07001396 // suspend effect of the given type
1397 void setEffectSuspended_l(const effect_uuid_t *type,
1398 bool suspend);
1399 // suspend all eligible effects
1400 void setEffectSuspendedAll_l(bool suspend);
1401 // check if effects should be suspend or restored when a given effect is enable or disabled
Eric Laurenta85a74a2011-10-19 11:44:54 -07001402 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
Eric Laurent59255e42011-07-27 19:49:51 -07001403 bool enabled);
1404
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 status_t dump(int fd, const Vector<String16>& args);
1406
1407 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001408 friend class AudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 EffectChain(const EffectChain&);
1410 EffectChain& operator =(const EffectChain&);
1411
Eric Laurent59255e42011-07-27 19:49:51 -07001412 class SuspendedEffectDesc : public RefBase {
1413 public:
1414 SuspendedEffectDesc() : mRefCount(0) {}
1415
1416 int mRefCount;
1417 effect_uuid_t mType;
1418 wp<EffectModule> mEffect;
1419 };
1420
1421 // get a list of effect modules to suspend when an effect of the type
1422 // passed is enabled.
1423 Vector< sp<EffectModule> > getSuspendEligibleEffects();
1424 // get an effect module if it is currently enable
1425 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurentdb7c0792011-08-10 10:37:50 -07001426 // true if the effect whose descriptor is passed can be suspended
1427 // OEMs can modify the rules implemented in this method to exclude specific effect
1428 // types or implementations from the suspend/restore mechanism.
1429 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
Eric Laurent59255e42011-07-27 19:49:51 -07001430
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431 wp<ThreadBase> mThread; // parent mixer thread
1432 Mutex mLock; // mutex protecting effect list
1433 Vector<sp<EffectModule> > mEffects; // list of effect modules
1434 int mSessionId; // audio session ID
1435 int16_t *mInBuffer; // chain input buffer
1436 int16_t *mOutBuffer; // chain output buffer
Eric Laurentb469b942011-05-09 12:09:06 -07001437 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1438 volatile int32_t mTrackCnt; // number of tracks connected
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07001440 int mVolumeCtrlIdx; // index of insert effect having control over volume
1441 uint32_t mLeftVolume; // previous volume on left channel
1442 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07001443 uint32_t mNewLeftVolume; // new volume on left channel
1444 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07001445 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent59255e42011-07-27 19:49:51 -07001446 // mSuspendedEffects lists all effect currently suspended in the chain
1447 // use effect type UUID timelow field as key. There is no real risk of identical
1448 // timeLow fields among effect type UUIDs.
1449 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450 };
1451
Dima Zavin799a70e2011-04-18 16:57:27 -07001452 struct AudioStreamOut {
1453 audio_hw_device_t *hwDev;
1454 audio_stream_out_t *stream;
1455
1456 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1457 hwDev(dev), stream(out) {}
1458 };
1459
1460 struct AudioStreamIn {
1461 audio_hw_device_t *hwDev;
1462 audio_stream_in_t *stream;
1463
1464 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1465 hwDev(dev), stream(in) {}
1466 };
1467
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001468 struct AudioSessionRef {
1469 int sessionid;
1470 pid_t pid;
1471 int cnt;
1472 };
1473
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 friend class RecordThread;
1475 friend class PlaybackThread;
1476
Mike J. Chen500e5102011-08-15 13:28:26 -07001477 enum master_volume_support {
1478 MVS_NONE,
1479 MVS_SETONLY,
1480 MVS_FULL,
1481 };
1482
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483 mutable Mutex mLock;
1484
1485 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1486
1487 mutable Mutex mHardwareLock;
Dima Zavin799a70e2011-04-18 16:57:27 -07001488 audio_hw_device_t* mPrimaryHardwareDev;
1489 Vector<audio_hw_device_t*> mAudioHwDevs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 mutable int mHardwareStatus;
1491
1492
1493 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavinfce7a472011-04-19 22:30:36 -07001494 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495 float mMasterVolume;
Mike J. Chen500e5102011-08-15 13:28:26 -07001496 float mMasterVolumeSW;
1497 master_volume_support mMasterVolumeSupportLvl;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 bool mMasterMute;
1499
1500 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
1501
1502 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
1503 volatile int32_t mNextUniqueId;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001504 uint32_t mMode;
Eric Laurentbee53372011-08-29 12:42:48 -07001505 bool mBtNrecIsOff;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001507 Vector<AudioSessionRef*> mAudioSessionRefs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508};
1509
Dima Zavin799a70e2011-04-18 16:57:27 -07001510
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511// ----------------------------------------------------------------------------
1512
1513}; // namespace android
1514
1515#endif // ANDROID_AUDIO_FLINGER_H