blob: 440cd345d3086d730e0ee600512c5516388e5535 [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,
87 int *sessionId,
88 status_t *status);
89
90 virtual uint32_t sampleRate(int output) const;
91 virtual int channelCount(int output) const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070092 virtual uint32_t format(int output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -070093 virtual size_t frameCount(int output) const;
94 virtual uint32_t latency(int output) const;
95
96 virtual status_t setMasterVolume(float value);
97 virtual status_t setMasterMute(bool muted);
98
99 virtual float masterVolume() const;
100 virtual bool masterMute() const;
101
102 virtual status_t setStreamVolume(int stream, float value, int output);
103 virtual status_t setStreamMute(int stream, bool muted);
104
105 virtual float streamVolume(int stream, int output) const;
106 virtual bool streamMute(int stream) const;
107
108 virtual status_t setMode(int mode);
109
110 virtual status_t setMicMute(bool state);
111 virtual bool getMicMute() const;
112
Mathias Agopian65ab4712010-07-14 17:59:35 -0700113 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
114 virtual String8 getParameters(int ioHandle, const String8& keys);
115
116 virtual void registerClient(const sp<IAudioFlingerClient>& client);
117
118 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
119 virtual unsigned int getInputFramesLost(int ioHandle);
120
121 virtual int openOutput(uint32_t *pDevices,
122 uint32_t *pSamplingRate,
123 uint32_t *pFormat,
124 uint32_t *pChannels,
125 uint32_t *pLatencyMs,
126 uint32_t flags);
127
128 virtual int openDuplicateOutput(int output1, int output2);
129
130 virtual status_t closeOutput(int output);
131
132 virtual status_t suspendOutput(int output);
133
134 virtual status_t restoreOutput(int output);
135
136 virtual int openInput(uint32_t *pDevices,
137 uint32_t *pSamplingRate,
138 uint32_t *pFormat,
139 uint32_t *pChannels,
140 uint32_t acoustics);
141
142 virtual status_t closeInput(int input);
143
144 virtual status_t setStreamOutput(uint32_t stream, int output);
145
146 virtual status_t setVoiceVolume(float volume);
147
148 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
149
150 virtual int newAudioSessionId();
151
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 virtual status_t queryNumberEffects(uint32_t *numEffects);
153
154 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
155
156 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
157
158 virtual sp<IEffect> createEffect(pid_t pid,
159 effect_descriptor_t *pDesc,
160 const sp<IEffectClient>& effectClient,
161 int32_t priority,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700162 int io,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163 int sessionId,
164 status_t *status,
165 int *id,
166 int *enabled);
167
Eric Laurent59255e42011-07-27 19:49:51 -0700168 virtual status_t moveEffects(int sessionId, int srcOutput, int dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169
170 enum hardware_call_state {
171 AUDIO_HW_IDLE = 0,
172 AUDIO_HW_INIT,
173 AUDIO_HW_OUTPUT_OPEN,
174 AUDIO_HW_OUTPUT_CLOSE,
175 AUDIO_HW_INPUT_OPEN,
176 AUDIO_HW_INPUT_CLOSE,
177 AUDIO_HW_STANDBY,
178 AUDIO_HW_SET_MASTER_VOLUME,
179 AUDIO_HW_GET_ROUTING,
180 AUDIO_HW_SET_ROUTING,
181 AUDIO_HW_GET_MODE,
182 AUDIO_HW_SET_MODE,
183 AUDIO_HW_GET_MIC_MUTE,
184 AUDIO_HW_SET_MIC_MUTE,
185 AUDIO_SET_VOICE_VOLUME,
186 AUDIO_SET_PARAMETER,
187 };
188
189 // record interface
190 virtual sp<IAudioRecord> openRecord(
191 pid_t pid,
192 int input,
193 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700194 uint32_t format,
195 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 int frameCount,
197 uint32_t flags,
198 int *sessionId,
199 status_t *status);
200
201 virtual status_t onTransact(
202 uint32_t code,
203 const Parcel& data,
204 Parcel* reply,
205 uint32_t flags);
206
207 uint32_t getMode() { return mMode; }
208
209private:
210 AudioFlinger();
211 virtual ~AudioFlinger();
212
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700213 status_t initCheck() const;
214 virtual void onFirstRef();
Dima Zavin799a70e2011-04-18 16:57:27 -0700215 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216
217 // Internal dump utilites.
218 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
219 status_t dumpClients(int fd, const Vector<String16>& args);
220 status_t dumpInternals(int fd, const Vector<String16>& args);
221
222 // --- Client ---
223 class Client : public RefBase {
224 public:
225 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
226 virtual ~Client();
227 const sp<MemoryDealer>& heap() const;
228 pid_t pid() const { return mPid; }
229 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
230
231 private:
232 Client(const Client&);
233 Client& operator = (const Client&);
234 sp<AudioFlinger> mAudioFlinger;
235 sp<MemoryDealer> mMemoryDealer;
236 pid_t mPid;
237 };
238
239 // --- Notification Client ---
240 class NotificationClient : public IBinder::DeathRecipient {
241 public:
242 NotificationClient(const sp<AudioFlinger>& audioFlinger,
243 const sp<IAudioFlingerClient>& client,
244 pid_t pid);
245 virtual ~NotificationClient();
246
247 sp<IAudioFlingerClient> client() { return mClient; }
248
249 // IBinder::DeathRecipient
250 virtual void binderDied(const wp<IBinder>& who);
251
252 private:
253 NotificationClient(const NotificationClient&);
254 NotificationClient& operator = (const NotificationClient&);
255
256 sp<AudioFlinger> mAudioFlinger;
257 pid_t mPid;
258 sp<IAudioFlingerClient> mClient;
259 };
260
261 class TrackHandle;
262 class RecordHandle;
263 class RecordThread;
264 class PlaybackThread;
265 class MixerThread;
266 class DirectOutputThread;
267 class DuplicatingThread;
268 class Track;
269 class RecordTrack;
270 class EffectModule;
271 class EffectHandle;
272 class EffectChain;
Dima Zavin799a70e2011-04-18 16:57:27 -0700273 struct AudioStreamOut;
274 struct AudioStreamIn;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275
276 class ThreadBase : public Thread {
277 public:
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700278 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279 virtual ~ThreadBase();
280
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700281
282 enum type {
283 MIXER, // Thread class is MixerThread
284 DIRECT, // Thread class is DirectOutputThread
285 DUPLICATING, // Thread class is DuplicatingThread
286 RECORD // Thread class is RecordThread
287 };
288
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 status_t dumpBase(int fd, const Vector<String16>& args);
Eric Laurent1d2bff02011-07-24 17:49:51 -0700290 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291
Eric Laurentfeb0db62011-07-22 09:04:31 -0700292 void clearPowerManager();
293
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 // base for record and playback
295 class TrackBase : public AudioBufferProvider, public RefBase {
296
297 public:
298 enum track_state {
299 IDLE,
300 TERMINATED,
301 STOPPED,
302 RESUMING,
303 ACTIVE,
304 PAUSING,
305 PAUSED
306 };
307
308 enum track_flags {
309 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
310 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
311 // The upper 16 bits are used for track-specific flags.
312 };
313
314 TrackBase(const wp<ThreadBase>& thread,
315 const sp<Client>& client,
316 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700317 uint32_t format,
318 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 int frameCount,
320 uint32_t flags,
321 const sp<IMemory>& sharedBuffer,
322 int sessionId);
323 ~TrackBase();
324
325 virtual status_t start() = 0;
326 virtual void stop() = 0;
327 sp<IMemory> getCblk() const;
328 audio_track_cblk_t* cblk() const { return mCblk; }
329 int sessionId() { return mSessionId; }
330
331 protected:
332 friend class ThreadBase;
333 friend class RecordHandle;
334 friend class PlaybackThread;
335 friend class RecordThread;
336 friend class MixerThread;
337 friend class DirectOutputThread;
338
339 TrackBase(const TrackBase&);
340 TrackBase& operator = (const TrackBase&);
341
342 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
343 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
344
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700345 uint32_t format() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 return mFormat;
347 }
348
349 int channelCount() const ;
350
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700351 uint32_t channelMask() const;
352
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 int sampleRate() const;
354
355 void* getBuffer(uint32_t offset, uint32_t frames) const;
356
357 bool isStopped() const {
358 return mState == STOPPED;
359 }
360
361 bool isTerminated() const {
362 return mState == TERMINATED;
363 }
364
365 bool step();
366 void reset();
367
368 wp<ThreadBase> mThread;
369 sp<Client> mClient;
370 sp<IMemory> mCblkMemory;
371 audio_track_cblk_t* mCblk;
372 void* mBuffer;
373 void* mBufferEnd;
374 uint32_t mFrameCount;
375 // we don't really need a lock for these
376 int mState;
377 int mClientTid;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700378 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 uint32_t mFlags;
380 int mSessionId;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700381 uint8_t mChannelCount;
382 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 };
384
385 class ConfigEvent {
386 public:
387 ConfigEvent() : mEvent(0), mParam(0) {}
388
389 int mEvent;
390 int mParam;
391 };
392
Eric Laurentfeb0db62011-07-22 09:04:31 -0700393 class PMDeathRecipient : public IBinder::DeathRecipient {
394 public:
395 PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
396 virtual ~PMDeathRecipient() {}
397
398 // IBinder::DeathRecipient
399 virtual void binderDied(const wp<IBinder>& who);
400
401 private:
402 PMDeathRecipient(const PMDeathRecipient&);
403 PMDeathRecipient& operator = (const PMDeathRecipient&);
404
405 wp<ThreadBase> mThread;
406 };
407
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700408 virtual status_t initCheck() const = 0;
409 int type() const { return mType; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 uint32_t sampleRate() const;
411 int channelCount() const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700412 uint32_t format() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 size_t frameCount() const;
414 void wakeUp() { mWaitWorkCV.broadcast(); }
415 void exit();
416 virtual bool checkForNewParameters_l() = 0;
417 virtual status_t setParameters(const String8& keyValuePairs);
418 virtual String8 getParameters(const String8& keys) = 0;
419 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
420 void sendConfigEvent(int event, int param = 0);
421 void sendConfigEvent_l(int event, int param = 0);
422 void processConfigEvents();
423 int id() const { return mId;}
424 bool standby() { return mStandby; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700425 uint32_t device() { return mDevice; }
426 virtual audio_stream_t* stream() = 0;
427
428 sp<EffectHandle> createEffect_l(
429 const sp<AudioFlinger::Client>& client,
430 const sp<IEffectClient>& effectClient,
431 int32_t priority,
432 int sessionId,
433 effect_descriptor_t *desc,
434 int *enabled,
435 status_t *status);
436 void disconnectEffect(const sp< EffectModule>& effect,
437 const wp<EffectHandle>& handle);
438
439 // return values for hasAudioSession (bit field)
440 enum effect_state {
441 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
442 // effect
443 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
444 // track
445 };
446
447 // get effect chain corresponding to session Id.
448 sp<EffectChain> getEffectChain(int sessionId);
449 // same as getEffectChain() but must be called with ThreadBase mutex locked
450 sp<EffectChain> getEffectChain_l(int sessionId);
451 // add an effect chain to the chain list (mEffectChains)
452 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
453 // remove an effect chain from the chain list (mEffectChains)
454 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
455 // lock mall effect chains Mutexes. Must be called before releasing the
456 // ThreadBase mutex before processing the mixer and effects. This guarantees the
457 // integrity of the chains during the process.
458 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
459 // unlock effect chains after process
460 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
461 // set audio mode to all effect chains
462 void setMode(uint32_t mode);
463 // get effect module with corresponding ID on specified audio session
464 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
465 // add and effect module. Also creates the effect chain is none exists for
466 // the effects audio session
467 status_t addEffect_l(const sp< EffectModule>& effect);
468 // remove and effect module. Also removes the effect chain is this was the last
469 // effect
470 void removeEffect_l(const sp< EffectModule>& effect);
471 // detach all tracks connected to an auxiliary effect
472 virtual void detachAuxEffect_l(int effectId) {}
473 // returns either EFFECT_SESSION if effects on this audio session exist in one
474 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
475 virtual uint32_t hasAudioSession(int sessionId) = 0;
476 // the value returned by default implementation is not important as the
477 // strategy is only meaningful for PlaybackThread which implements this method
478 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479
Eric Laurent59255e42011-07-27 19:49:51 -0700480 // suspend or restore effect according to the type of effect passed. a NULL
481 // type pointer means suspend all effects in the session
482 void setEffectSuspended(const effect_uuid_t *type,
483 bool suspend,
484 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
485 // check if some effects must be suspended/restored when an effect is enabled
486 // or disabled
487 virtual void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
488 bool enabled,
489 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
490
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 mutable Mutex mLock;
492
493 protected:
494
Eric Laurent59255e42011-07-27 19:49:51 -0700495 // entry describing an effect being suspended in mSuspendedSessions keyed vector
496 class SuspendedSessionDesc : public RefBase {
497 public:
498 SuspendedSessionDesc() : mRefCount(0) {}
499
500 int mRefCount; // number of active suspend requests
501 effect_uuid_t mType; // effect type UUID
502 };
503
Eric Laurentfeb0db62011-07-22 09:04:31 -0700504 void acquireWakeLock();
505 void acquireWakeLock_l();
506 void releaseWakeLock();
507 void releaseWakeLock_l();
Eric Laurent59255e42011-07-27 19:49:51 -0700508 void setEffectSuspended_l(const effect_uuid_t *type,
509 bool suspend,
510 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
511 // updated mSuspendedSessions when an effect suspended or restored
512 void updateSuspendedSessions_l(const effect_uuid_t *type,
513 bool suspend,
514 int sessionId);
515 // check if some effects must be suspended when an effect chain is added
516 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
517 // updated mSuspendedSessions when an effect chain is removed
518 void updateSuspendedSessionsOnRemoveEffectChain_l(const sp<EffectChain>& chain);
Eric Laurentfeb0db62011-07-22 09:04:31 -0700519
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 friend class Track;
521 friend class TrackBase;
522 friend class PlaybackThread;
523 friend class MixerThread;
524 friend class DirectOutputThread;
525 friend class DuplicatingThread;
526 friend class RecordThread;
527 friend class RecordTrack;
528
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700529 int mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530 Condition mWaitWorkCV;
531 sp<AudioFlinger> mAudioFlinger;
532 uint32_t mSampleRate;
533 size_t mFrameCount;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700534 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535 uint16_t mChannelCount;
536 uint16_t mFrameSize;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700537 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 Condition mParamCond;
539 Vector<String8> mNewParameters;
540 status_t mParamStatus;
541 Vector<ConfigEvent *> mConfigEvents;
542 bool mStandby;
543 int mId;
544 bool mExiting;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700545 Vector< sp<EffectChain> > mEffectChains;
546 uint32_t mDevice; // output device for PlaybackThread
547 // input + output devices for RecordThread
Eric Laurentfeb0db62011-07-22 09:04:31 -0700548 static const int kNameLength = 32;
549 char mName[kNameLength];
550 sp<IPowerManager> mPowerManager;
551 sp<IBinder> mWakeLockToken;
552 sp<PMDeathRecipient> mDeathRecipient;
Eric Laurent59255e42011-07-27 19:49:51 -0700553 // list of suspended effects per session and per type. The first vector is
554 // keyed by session ID, the second by type UUID timeLow field
555 KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > > mSuspendedSessions;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 };
557
558 // --- PlaybackThread ---
559 class PlaybackThread : public ThreadBase {
560 public:
561
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 enum mixer_state {
563 MIXER_IDLE,
564 MIXER_TRACKS_ENABLED,
565 MIXER_TRACKS_READY
566 };
567
568 // playback track
569 class Track : public TrackBase {
570 public:
571 Track( const wp<ThreadBase>& thread,
572 const sp<Client>& client,
573 int streamType,
574 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700575 uint32_t format,
576 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 int frameCount,
578 const sp<IMemory>& sharedBuffer,
579 int sessionId);
580 ~Track();
581
582 void dump(char* buffer, size_t size);
583 virtual status_t start();
584 virtual void stop();
585 void pause();
586
587 void flush();
588 void destroy();
589 void mute(bool);
590 void setVolume(float left, float right);
591 int name() const {
592 return mName;
593 }
594
595 int type() const {
596 return mStreamType;
597 }
598 status_t attachAuxEffect(int EffectId);
599 void setAuxBuffer(int EffectId, int32_t *buffer);
600 int32_t *auxBuffer() { return mAuxBuffer; }
601 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
602 int16_t *mainBuffer() { return mMainBuffer; }
603 int auxEffectId() { return mAuxEffectId; }
604
605
606 protected:
607 friend class ThreadBase;
608 friend class AudioFlinger;
609 friend class TrackHandle;
610 friend class PlaybackThread;
611 friend class MixerThread;
612 friend class DirectOutputThread;
613
614 Track(const Track&);
615 Track& operator = (const Track&);
616
617 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
618 bool isMuted() { return mMute; }
619 bool isPausing() const {
620 return mState == PAUSING;
621 }
622 bool isPaused() const {
623 return mState == PAUSED;
624 }
625 bool isReady() const;
626 void setPaused() { mState = PAUSED; }
627 void reset();
628
629 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700630 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 }
632
633 // we don't really need a lock for these
634 float mVolume[2];
635 volatile bool mMute;
636 // FILLED state is used for suppressing volume ramp at begin of playing
637 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
638 mutable uint8_t mFillingUpStatus;
639 int8_t mRetryCount;
640 sp<IMemory> mSharedBuffer;
641 bool mResetDone;
642 int mStreamType;
643 int mName;
644 int16_t *mMainBuffer;
645 int32_t *mAuxBuffer;
646 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700647 bool mHasVolumeController;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 }; // end of Track
649
650
651 // playback track
652 class OutputTrack : public Track {
653 public:
654
655 class Buffer: public AudioBufferProvider::Buffer {
656 public:
657 int16_t *mBuffer;
658 };
659
660 OutputTrack( const wp<ThreadBase>& thread,
661 DuplicatingThread *sourceThread,
662 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700663 uint32_t format,
664 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 int frameCount);
666 ~OutputTrack();
667
668 virtual status_t start();
669 virtual void stop();
670 bool write(int16_t* data, uint32_t frames);
671 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
672 bool isActive() { return mActive; }
673 wp<ThreadBase>& thread() { return mThread; }
674
675 private:
676
677 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
678 void clearBufferQueue();
679
680 // Maximum number of pending buffers allocated by OutputTrack::write()
681 static const uint8_t kMaxOverFlowBuffers = 10;
682
683 Vector < Buffer* > mBufferQueue;
684 AudioBufferProvider::Buffer mOutBuffer;
685 bool mActive;
686 DuplicatingThread* mSourceThread;
687 }; // end of OutputTrack
688
Dima Zavin799a70e2011-04-18 16:57:27 -0700689 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 virtual ~PlaybackThread();
691
692 virtual status_t dump(int fd, const Vector<String16>& args);
693
694 // Thread virtuals
695 virtual status_t readyToRun();
696 virtual void onFirstRef();
697
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700698 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
699
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 virtual uint32_t latency() const;
701
702 virtual status_t setMasterVolume(float value);
703 virtual status_t setMasterMute(bool muted);
704
705 virtual float masterVolume() const;
706 virtual bool masterMute() const;
707
708 virtual status_t setStreamVolume(int stream, float value);
709 virtual status_t setStreamMute(int stream, bool muted);
710
711 virtual float streamVolume(int stream) const;
712 virtual bool streamMute(int stream) const;
713
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 sp<Track> createTrack_l(
715 const sp<AudioFlinger::Client>& client,
716 int streamType,
717 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700718 uint32_t format,
719 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 int frameCount,
721 const sp<IMemory>& sharedBuffer,
722 int sessionId,
723 status_t *status);
724
Dima Zavin799a70e2011-04-18 16:57:27 -0700725 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700726 virtual audio_stream_t* stream() { return &mOutput->stream->common; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 void suspend() { mSuspended++; }
729 void restore() { if (mSuspended) mSuspended--; }
730 bool isSuspended() { return (mSuspended != 0); }
731 virtual String8 getParameters(const String8& keys);
732 virtual void audioConfigChanged_l(int event, int param = 0);
733 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
734 int16_t *mixBuffer() { return mMixBuffer; };
735
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700736 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -0700737 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
738 int EffectId);
739 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
740 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700742 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
743 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
744 virtual uint32_t hasAudioSession(int sessionId);
745 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -0700746
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 struct stream_type_t {
748 stream_type_t()
749 : volume(1.0f),
750 mute(false)
751 {
752 }
753 float volume;
754 bool mute;
755 };
756
757 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 int16_t* mMixBuffer;
759 int mSuspended;
760 int mBytesWritten;
761 bool mMasterMute;
762 SortedVector< wp<Track> > mActiveTracks;
763
764 virtual int getTrackName_l() = 0;
765 virtual void deleteTrackName_l(int name) = 0;
766 virtual uint32_t activeSleepTimeUs() = 0;
767 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700768 virtual uint32_t suspendSleepTimeUs() = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769
770 private:
771
772 friend class AudioFlinger;
773 friend class OutputTrack;
774 friend class Track;
775 friend class TrackBase;
776 friend class MixerThread;
777 friend class DirectOutputThread;
778 friend class DuplicatingThread;
779
780 PlaybackThread(const Client&);
781 PlaybackThread& operator = (const PlaybackThread&);
782
783 status_t addTrack_l(const sp<Track>& track);
784 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -0700785 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786
787 void readOutputParameters();
788
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
790 status_t dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791
792 SortedVector< sp<Track> > mTracks;
793 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -0700794 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin799a70e2011-04-18 16:57:27 -0700795 AudioStreamOut* mOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 float mMasterVolume;
797 nsecs_t mLastWriteTime;
798 int mNumWrites;
799 int mNumDelayedWrites;
800 bool mInWrite;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 };
802
803 class MixerThread : public PlaybackThread {
804 public:
Eric Laurentde070132010-07-13 04:45:46 -0700805 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700806 AudioStreamOut* output,
Eric Laurentde070132010-07-13 04:45:46 -0700807 int id,
808 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 virtual ~MixerThread();
810
811 // Thread virtuals
812 virtual bool threadLoop();
813
814 void invalidateTracks(int streamType);
815 virtual bool checkForNewParameters_l();
816 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
817
818 protected:
Eric Laurentde070132010-07-13 04:45:46 -0700819 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
820 Vector< sp<Track> > *tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821 virtual int getTrackName_l();
822 virtual void deleteTrackName_l(int name);
823 virtual uint32_t activeSleepTimeUs();
824 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700825 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826
827 AudioMixer* mAudioMixer;
828 };
829
830 class DirectOutputThread : public PlaybackThread {
831 public:
832
Dima Zavin799a70e2011-04-18 16:57:27 -0700833 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 ~DirectOutputThread();
835
836 // Thread virtuals
837 virtual bool threadLoop();
838
839 virtual bool checkForNewParameters_l();
840
841 protected:
842 virtual int getTrackName_l();
843 virtual void deleteTrackName_l(int name);
844 virtual uint32_t activeSleepTimeUs();
845 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700846 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847
848 private:
849 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
850
851 float mLeftVolFloat;
852 float mRightVolFloat;
853 uint16_t mLeftVolShort;
854 uint16_t mRightVolShort;
855 };
856
857 class DuplicatingThread : public MixerThread {
858 public:
859 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
860 ~DuplicatingThread();
861
862 // Thread virtuals
863 virtual bool threadLoop();
864 void addOutputTrack(MixerThread* thread);
865 void removeOutputTrack(MixerThread* thread);
866 uint32_t waitTimeMs() { return mWaitTimeMs; }
867 protected:
868 virtual uint32_t activeSleepTimeUs();
869
870 private:
871 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
872 void updateWaitTime();
873
874 SortedVector < sp<OutputTrack> > mOutputTracks;
875 uint32_t mWaitTimeMs;
876 };
877
878 PlaybackThread *checkPlaybackThread_l(int output) const;
879 MixerThread *checkMixerThread_l(int output) const;
880 RecordThread *checkRecordThread_l(int input) const;
881 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
882 void audioConfigChanged_l(int event, int ioHandle, void *param2);
883
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700884 uint32_t nextUniqueId();
Eric Laurent59255e42011-07-27 19:49:51 -0700885 status_t moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -0700886 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -0700887 AudioFlinger::PlaybackThread *dstThread,
888 bool reRegister);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700889 PlaybackThread *primaryPlaybackThread_l();
890 uint32_t primaryOutputDevice_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891
892 friend class AudioBuffer;
893
894 class TrackHandle : public android::BnAudioTrack {
895 public:
896 TrackHandle(const sp<PlaybackThread::Track>& track);
897 virtual ~TrackHandle();
898 virtual status_t start();
899 virtual void stop();
900 virtual void flush();
901 virtual void mute(bool);
902 virtual void pause();
903 virtual void setVolume(float left, float right);
904 virtual sp<IMemory> getCblk() const;
905 virtual status_t attachAuxEffect(int effectId);
906 virtual status_t onTransact(
907 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
908 private:
909 sp<PlaybackThread::Track> mTrack;
910 };
911
912 friend class Client;
913 friend class PlaybackThread::Track;
914
915
916 void removeClient_l(pid_t pid);
917 void removeNotificationClient(pid_t pid);
918
919
920 // record thread
921 class RecordThread : public ThreadBase, public AudioBufferProvider
922 {
923 public:
924
925 // record track
926 class RecordTrack : public TrackBase {
927 public:
928 RecordTrack(const wp<ThreadBase>& thread,
929 const sp<Client>& client,
930 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700931 uint32_t format,
932 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 int frameCount,
934 uint32_t flags,
935 int sessionId);
936 ~RecordTrack();
937
938 virtual status_t start();
939 virtual void stop();
940
941 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
942 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
943
944 void dump(char* buffer, size_t size);
Eric Laurent59255e42011-07-27 19:49:51 -0700945
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 private:
947 friend class AudioFlinger;
948 friend class RecordThread;
949
950 RecordTrack(const RecordTrack&);
951 RecordTrack& operator = (const RecordTrack&);
952
953 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
954
955 bool mOverflow;
956 };
957
958
959 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700960 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 uint32_t sampleRate,
962 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700963 int id,
964 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 ~RecordThread();
966
967 virtual bool threadLoop();
968 virtual status_t readyToRun() { return NO_ERROR; }
969 virtual void onFirstRef();
970
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700971 virtual status_t initCheck() const { return (mInput == 0) ? NO_INIT : NO_ERROR; }
972 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
973 const sp<AudioFlinger::Client>& client,
974 uint32_t sampleRate,
975 int format,
976 int channelMask,
977 int frameCount,
978 uint32_t flags,
979 int sessionId,
980 status_t *status);
981
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982 status_t start(RecordTrack* recordTrack);
983 void stop(RecordTrack* recordTrack);
984 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin799a70e2011-04-18 16:57:27 -0700985 AudioStreamIn* getInput() { return mInput; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700986 virtual audio_stream_t* stream() { return &mInput->stream->common; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987
988 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
989 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
990 virtual bool checkForNewParameters_l();
991 virtual String8 getParameters(const String8& keys);
992 virtual void audioConfigChanged_l(int event, int param = 0);
993 void readInputParameters();
994 virtual unsigned int getInputFramesLost();
995
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700996 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
997 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
998 virtual uint32_t hasAudioSession(int sessionId);
999
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 private:
1001 RecordThread();
Dima Zavin799a70e2011-04-18 16:57:27 -07001002 AudioStreamIn *mInput;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001003 RecordTrack* mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004 sp<RecordTrack> mActiveTrack;
1005 Condition mStartStopCond;
1006 AudioResampler *mResampler;
1007 int32_t *mRsmpOutBuffer;
1008 int16_t *mRsmpInBuffer;
1009 size_t mRsmpInIndex;
1010 size_t mInputBytes;
1011 int mReqChannelCount;
1012 uint32_t mReqSampleRate;
1013 ssize_t mBytesRead;
1014 };
1015
1016 class RecordHandle : public android::BnAudioRecord {
1017 public:
1018 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
1019 virtual ~RecordHandle();
1020 virtual status_t start();
1021 virtual void stop();
1022 virtual sp<IMemory> getCblk() const;
1023 virtual status_t onTransact(
1024 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1025 private:
1026 sp<RecordThread::RecordTrack> mRecordTrack;
1027 };
1028
1029 //--- Audio Effect Management
1030
1031 // EffectModule and EffectChain classes both have their own mutex to protect
1032 // state changes or resource modifications. Always respect the following order
1033 // if multiple mutexes must be acquired to avoid cross deadlock:
1034 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1035
1036 // The EffectModule class is a wrapper object controlling the effect engine implementation
1037 // in the effect library. It prevents concurrent calls to process() and command() functions
1038 // from different client threads. It keeps a list of EffectHandle objects corresponding
1039 // to all client applications using this effect and notifies applications of effect state,
1040 // control or parameter changes. It manages the activation state machine to send appropriate
1041 // reset, enable, disable commands to effect engine and provide volume
1042 // ramping when effects are activated/deactivated.
1043 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1044 // the attached track(s) to accumulate their auxiliary channel.
1045 class EffectModule: public RefBase {
1046 public:
1047 EffectModule(const wp<ThreadBase>& wThread,
1048 const wp<AudioFlinger::EffectChain>& chain,
1049 effect_descriptor_t *desc,
1050 int id,
1051 int sessionId);
1052 ~EffectModule();
1053
1054 enum effect_state {
1055 IDLE,
1056 RESTART,
1057 STARTING,
1058 ACTIVE,
1059 STOPPING,
Eric Laurentec437d82011-07-26 20:54:46 -07001060 STOPPED,
1061 DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 };
1063
1064 int id() { return mId; }
1065 void process();
1066 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001067 status_t command(uint32_t cmdCode,
1068 uint32_t cmdSize,
1069 void *pCmdData,
1070 uint32_t *replySize,
1071 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001072
1073 void reset_l();
1074 status_t configure();
1075 status_t init();
1076 uint32_t state() {
1077 return mState;
1078 }
1079 uint32_t status() {
1080 return mStatus;
1081 }
Eric Laurentde070132010-07-13 04:45:46 -07001082 int sessionId() {
1083 return mSessionId;
1084 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 status_t setEnabled(bool enabled);
1086 bool isEnabled();
Eric Laurent8f45bd72010-08-31 13:50:07 -07001087 bool isProcessEnabled();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001088
1089 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1090 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1091 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1092 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001093 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1094 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent59255e42011-07-27 19:49:51 -07001095 wp<ThreadBase>& thread() { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096
1097 status_t addHandle(sp<EffectHandle>& handle);
1098 void disconnect(const wp<EffectHandle>& handle);
1099 size_t removeHandle (const wp<EffectHandle>& handle);
1100
1101 effect_descriptor_t& desc() { return mDescriptor; }
1102 wp<EffectChain>& chain() { return mChain; }
1103
1104 status_t setDevice(uint32_t device);
1105 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
1106 status_t setMode(uint32_t mode);
Eric Laurentec437d82011-07-26 20:54:46 -07001107 status_t stop();
Eric Laurent59255e42011-07-27 19:49:51 -07001108 void setSuspended(bool suspended);
1109 bool suspended();
1110
1111 sp<EffectHandle> controlHandle();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112
1113 status_t dump(int fd, const Vector<String16>& args);
1114
1115 protected:
1116
1117 // Maximum time allocated to effect engines to complete the turn off sequence
1118 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1119
1120 EffectModule(const EffectModule&);
1121 EffectModule& operator = (const EffectModule&);
1122
1123 status_t start_l();
1124 status_t stop_l();
1125
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126 Mutex mLock; // mutex for process, commands and handles list protection
1127 wp<ThreadBase> mThread; // parent thread
1128 wp<EffectChain> mChain; // parent effect chain
1129 int mId; // this instance unique ID
1130 int mSessionId; // audio session ID
1131 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1132 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001133 effect_handle_t mEffectInterface; // Effect module C API
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 status_t mStatus; // initialization status
1135 uint32_t mState; // current activation state (effect_state)
1136 Vector< wp<EffectHandle> > mHandles; // list of client handles
1137 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1138 // sending disable command.
1139 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent59255e42011-07-27 19:49:51 -07001140 bool mSuspended; // effect is suspended: temporarily disabled by framework
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 };
1142
1143 // The EffectHandle class implements the IEffect interface. It provides resources
1144 // to receive parameter updates, keeps track of effect control
1145 // ownership and state and has a pointer to the EffectModule object it is controlling.
1146 // There is one EffectHandle object for each application controlling (or using)
1147 // an effect module.
1148 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1149 class EffectHandle: public android::BnEffect {
1150 public:
1151
1152 EffectHandle(const sp<EffectModule>& effect,
1153 const sp<AudioFlinger::Client>& client,
1154 const sp<IEffectClient>& effectClient,
1155 int32_t priority);
1156 virtual ~EffectHandle();
1157
1158 // IEffect
1159 virtual status_t enable();
1160 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001161 virtual status_t command(uint32_t cmdCode,
1162 uint32_t cmdSize,
1163 void *pCmdData,
1164 uint32_t *replySize,
1165 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166 virtual void disconnect();
1167 virtual sp<IMemory> getCblk() const;
1168 virtual status_t onTransact(uint32_t code, const Parcel& data,
1169 Parcel* reply, uint32_t flags);
1170
1171
1172 // Give or take control of effect module
Eric Laurent59255e42011-07-27 19:49:51 -07001173 // - hasControl: true if control is given, false if removed
1174 // - signal: true client app should be signaled of change, false otherwise
1175 // - enabled: state of the effect when control is passed
1176 void setControl(bool hasControl, bool signal, bool enabled);
Eric Laurent25f43952010-07-28 05:40:18 -07001177 void commandExecuted(uint32_t cmdCode,
1178 uint32_t cmdSize,
1179 void *pCmdData,
1180 uint32_t replySize,
1181 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182 void setEnabled(bool enabled);
Eric Laurent59255e42011-07-27 19:49:51 -07001183 bool enabled() { return mEnabled; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184
1185 // Getters
1186 int id() { return mEffect->id(); }
1187 int priority() { return mPriority; }
1188 bool hasControl() { return mHasControl; }
1189 sp<EffectModule> effect() { return mEffect; }
1190
1191 void dump(char* buffer, size_t size);
1192
1193 protected:
1194
1195 EffectHandle(const EffectHandle&);
1196 EffectHandle& operator =(const EffectHandle&);
1197
1198 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1199 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1200 sp<Client> mClient; // client for shared memory allocation
1201 sp<IMemory> mCblkMemory; // shared memory for control block
1202 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1203 uint8_t* mBuffer; // pointer to parameter area in shared memory
1204 int mPriority; // client application priority to control the effect
1205 bool mHasControl; // true if this handle is controlling the effect
Eric Laurent59255e42011-07-27 19:49:51 -07001206 bool mEnabled; // cached enable state: needed when the effect is
1207 // restored after being suspended
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 };
1209
1210 // the EffectChain class represents a group of effects associated to one audio session.
1211 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1212 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1213 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1214 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1215 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1216 // input buffer used by the track as accumulation buffer.
1217 class EffectChain: public RefBase {
1218 public:
1219 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1220 ~EffectChain();
1221
Eric Laurent59255e42011-07-27 19:49:51 -07001222 // special key used for an entry in mSuspendedEffects keyed vector
1223 // corresponding to a suspend all request.
1224 static const int kKeyForSuspendAll = 0;
1225
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 void process_l();
1227
1228 void lock() {
1229 mLock.lock();
1230 }
1231 void unlock() {
1232 mLock.unlock();
1233 }
1234
Eric Laurentde070132010-07-13 04:45:46 -07001235 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001236 size_t removeEffect_l(const sp<EffectModule>& handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001238 int sessionId() { return mSessionId; }
1239 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001240
Eric Laurentcab11242010-07-15 12:50:15 -07001241 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1242 sp<EffectModule> getEffectFromId_l(int id);
Eric Laurent59255e42011-07-27 19:49:51 -07001243 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurentcab11242010-07-15 12:50:15 -07001244 bool setVolume_l(uint32_t *left, uint32_t *right);
1245 void setDevice_l(uint32_t device);
1246 void setMode_l(uint32_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1249 mInBuffer = buffer;
1250 mOwnInBuffer = ownsBuffer;
1251 }
1252 int16_t *inBuffer() {
1253 return mInBuffer;
1254 }
1255 void setOutBuffer(int16_t *buffer) {
1256 mOutBuffer = buffer;
1257 }
1258 int16_t *outBuffer() {
1259 return mOutBuffer;
1260 }
1261
Eric Laurentb469b942011-05-09 12:09:06 -07001262 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1263 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1264 int32_t trackCnt() { return mTrackCnt;}
1265
1266 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1267 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1268 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269
Eric Laurentde070132010-07-13 04:45:46 -07001270 uint32_t strategy() { return mStrategy; }
1271 void setStrategy(uint32_t strategy)
1272 { mStrategy = strategy; }
1273
Eric Laurent59255e42011-07-27 19:49:51 -07001274 // suspend effect of the given type
1275 void setEffectSuspended_l(const effect_uuid_t *type,
1276 bool suspend);
1277 // suspend all eligible effects
1278 void setEffectSuspendedAll_l(bool suspend);
1279 // check if effects should be suspend or restored when a given effect is enable or disabled
1280 virtual void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1281 bool enabled);
1282
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 status_t dump(int fd, const Vector<String16>& args);
1284
1285 protected:
1286
1287 EffectChain(const EffectChain&);
1288 EffectChain& operator =(const EffectChain&);
1289
Eric Laurent59255e42011-07-27 19:49:51 -07001290 class SuspendedEffectDesc : public RefBase {
1291 public:
1292 SuspendedEffectDesc() : mRefCount(0) {}
1293
1294 int mRefCount;
1295 effect_uuid_t mType;
1296 wp<EffectModule> mEffect;
1297 };
1298
1299 // get a list of effect modules to suspend when an effect of the type
1300 // passed is enabled.
1301 Vector< sp<EffectModule> > getSuspendEligibleEffects();
1302 // get an effect module if it is currently enable
1303 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
1304
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 wp<ThreadBase> mThread; // parent mixer thread
1306 Mutex mLock; // mutex protecting effect list
1307 Vector<sp<EffectModule> > mEffects; // list of effect modules
1308 int mSessionId; // audio session ID
1309 int16_t *mInBuffer; // chain input buffer
1310 int16_t *mOutBuffer; // chain output buffer
Eric Laurentb469b942011-05-09 12:09:06 -07001311 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1312 volatile int32_t mTrackCnt; // number of tracks connected
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07001314 int mVolumeCtrlIdx; // index of insert effect having control over volume
1315 uint32_t mLeftVolume; // previous volume on left channel
1316 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07001317 uint32_t mNewLeftVolume; // new volume on left channel
1318 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07001319 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent59255e42011-07-27 19:49:51 -07001320 // mSuspendedEffects lists all effect currently suspended in the chain
1321 // use effect type UUID timelow field as key. There is no real risk of identical
1322 // timeLow fields among effect type UUIDs.
1323 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324 };
1325
Dima Zavin799a70e2011-04-18 16:57:27 -07001326 struct AudioStreamOut {
1327 audio_hw_device_t *hwDev;
1328 audio_stream_out_t *stream;
1329
1330 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1331 hwDev(dev), stream(out) {}
1332 };
1333
1334 struct AudioStreamIn {
1335 audio_hw_device_t *hwDev;
1336 audio_stream_in_t *stream;
1337
1338 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1339 hwDev(dev), stream(in) {}
1340 };
1341
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342 friend class RecordThread;
1343 friend class PlaybackThread;
1344
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 mutable Mutex mLock;
1346
1347 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1348
1349 mutable Mutex mHardwareLock;
Dima Zavin799a70e2011-04-18 16:57:27 -07001350 audio_hw_device_t* mPrimaryHardwareDev;
1351 Vector<audio_hw_device_t*> mAudioHwDevs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352 mutable int mHardwareStatus;
1353
1354
1355 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavinfce7a472011-04-19 22:30:36 -07001356 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357 float mMasterVolume;
1358 bool mMasterMute;
1359
1360 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
1361
1362 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
1363 volatile int32_t mNextUniqueId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364 uint32_t mMode;
1365
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366};
1367
Dima Zavin799a70e2011-04-18 16:57:27 -07001368
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369// ----------------------------------------------------------------------------
1370
1371}; // namespace android
1372
1373#endif // ANDROID_AUDIO_FLINGER_H