blob: e2cf946d0aceb6e0d5e7af4e1d4d9aaef1ae48aa [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
46namespace android {
47
48class audio_track_cblk_t;
49class effect_param_cblk_t;
50class AudioMixer;
51class AudioBuffer;
52class AudioResampler;
53
Mathias Agopian65ab4712010-07-14 17:59:35 -070054// ----------------------------------------------------------------------------
55
56#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
57#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
58
59
60// ----------------------------------------------------------------------------
61
62static const nsecs_t kStandbyTimeInNsecs = seconds(3);
63
Mathias Agopian5462fc92010-07-14 18:41:18 -070064class AudioFlinger :
65 public BinderService<AudioFlinger>,
66 public BnAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -070067{
Mathias Agopian5462fc92010-07-14 18:41:18 -070068 friend class BinderService<AudioFlinger>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070069public:
Mathias Agopian5462fc92010-07-14 18:41:18 -070070 static char const* getServiceName() { return "media.audio_flinger"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070071
72 virtual status_t dump(int fd, const Vector<String16>& args);
73
74 // IAudioFlinger interface
75 virtual sp<IAudioTrack> createTrack(
76 pid_t pid,
77 int streamType,
78 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070079 uint32_t format,
80 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -070081 int frameCount,
82 uint32_t flags,
83 const sp<IMemory>& sharedBuffer,
84 int output,
85 int *sessionId,
86 status_t *status);
87
88 virtual uint32_t sampleRate(int output) const;
89 virtual int channelCount(int output) const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070090 virtual uint32_t format(int output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -070091 virtual size_t frameCount(int output) const;
92 virtual uint32_t latency(int output) const;
93
94 virtual status_t setMasterVolume(float value);
95 virtual status_t setMasterMute(bool muted);
96
97 virtual float masterVolume() const;
98 virtual bool masterMute() const;
99
100 virtual status_t setStreamVolume(int stream, float value, int output);
101 virtual status_t setStreamMute(int stream, bool muted);
102
103 virtual float streamVolume(int stream, int output) const;
104 virtual bool streamMute(int stream) const;
105
106 virtual status_t setMode(int mode);
107
108 virtual status_t setMicMute(bool state);
109 virtual bool getMicMute() const;
110
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
112 virtual String8 getParameters(int ioHandle, const String8& keys);
113
114 virtual void registerClient(const sp<IAudioFlingerClient>& client);
115
116 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
117 virtual unsigned int getInputFramesLost(int ioHandle);
118
119 virtual int openOutput(uint32_t *pDevices,
120 uint32_t *pSamplingRate,
121 uint32_t *pFormat,
122 uint32_t *pChannels,
123 uint32_t *pLatencyMs,
124 uint32_t flags);
125
126 virtual int openDuplicateOutput(int output1, int output2);
127
128 virtual status_t closeOutput(int output);
129
130 virtual status_t suspendOutput(int output);
131
132 virtual status_t restoreOutput(int output);
133
134 virtual int openInput(uint32_t *pDevices,
135 uint32_t *pSamplingRate,
136 uint32_t *pFormat,
137 uint32_t *pChannels,
138 uint32_t acoustics);
139
140 virtual status_t closeInput(int input);
141
142 virtual status_t setStreamOutput(uint32_t stream, int output);
143
144 virtual status_t setVoiceVolume(float volume);
145
146 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
147
148 virtual int newAudioSessionId();
149
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150 virtual status_t queryNumberEffects(uint32_t *numEffects);
151
152 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
153
154 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
155
156 virtual sp<IEffect> createEffect(pid_t pid,
157 effect_descriptor_t *pDesc,
158 const sp<IEffectClient>& effectClient,
159 int32_t priority,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700160 int io,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161 int sessionId,
162 status_t *status,
163 int *id,
164 int *enabled);
165
Eric Laurentde070132010-07-13 04:45:46 -0700166 virtual status_t moveEffects(int session, int srcOutput, int dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167
168 enum hardware_call_state {
169 AUDIO_HW_IDLE = 0,
170 AUDIO_HW_INIT,
171 AUDIO_HW_OUTPUT_OPEN,
172 AUDIO_HW_OUTPUT_CLOSE,
173 AUDIO_HW_INPUT_OPEN,
174 AUDIO_HW_INPUT_CLOSE,
175 AUDIO_HW_STANDBY,
176 AUDIO_HW_SET_MASTER_VOLUME,
177 AUDIO_HW_GET_ROUTING,
178 AUDIO_HW_SET_ROUTING,
179 AUDIO_HW_GET_MODE,
180 AUDIO_HW_SET_MODE,
181 AUDIO_HW_GET_MIC_MUTE,
182 AUDIO_HW_SET_MIC_MUTE,
183 AUDIO_SET_VOICE_VOLUME,
184 AUDIO_SET_PARAMETER,
185 };
186
187 // record interface
188 virtual sp<IAudioRecord> openRecord(
189 pid_t pid,
190 int input,
191 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700192 uint32_t format,
193 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 int frameCount,
195 uint32_t flags,
196 int *sessionId,
197 status_t *status);
198
199 virtual status_t onTransact(
200 uint32_t code,
201 const Parcel& data,
202 Parcel* reply,
203 uint32_t flags);
204
205 uint32_t getMode() { return mMode; }
206
207private:
208 AudioFlinger();
209 virtual ~AudioFlinger();
210
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700211 status_t initCheck() const;
212 virtual void onFirstRef();
Dima Zavin799a70e2011-04-18 16:57:27 -0700213 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214
215 // Internal dump utilites.
216 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
217 status_t dumpClients(int fd, const Vector<String16>& args);
218 status_t dumpInternals(int fd, const Vector<String16>& args);
219
220 // --- Client ---
221 class Client : public RefBase {
222 public:
223 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
224 virtual ~Client();
225 const sp<MemoryDealer>& heap() const;
226 pid_t pid() const { return mPid; }
227 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
228
229 private:
230 Client(const Client&);
231 Client& operator = (const Client&);
232 sp<AudioFlinger> mAudioFlinger;
233 sp<MemoryDealer> mMemoryDealer;
234 pid_t mPid;
235 };
236
237 // --- Notification Client ---
238 class NotificationClient : public IBinder::DeathRecipient {
239 public:
240 NotificationClient(const sp<AudioFlinger>& audioFlinger,
241 const sp<IAudioFlingerClient>& client,
242 pid_t pid);
243 virtual ~NotificationClient();
244
245 sp<IAudioFlingerClient> client() { return mClient; }
246
247 // IBinder::DeathRecipient
248 virtual void binderDied(const wp<IBinder>& who);
249
250 private:
251 NotificationClient(const NotificationClient&);
252 NotificationClient& operator = (const NotificationClient&);
253
254 sp<AudioFlinger> mAudioFlinger;
255 pid_t mPid;
256 sp<IAudioFlingerClient> mClient;
257 };
258
259 class TrackHandle;
260 class RecordHandle;
261 class RecordThread;
262 class PlaybackThread;
263 class MixerThread;
264 class DirectOutputThread;
265 class DuplicatingThread;
266 class Track;
267 class RecordTrack;
268 class EffectModule;
269 class EffectHandle;
270 class EffectChain;
Dima Zavin799a70e2011-04-18 16:57:27 -0700271 struct AudioStreamOut;
272 struct AudioStreamIn;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273
274 class ThreadBase : public Thread {
275 public:
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700276 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 virtual ~ThreadBase();
278
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700279
280 enum type {
281 MIXER, // Thread class is MixerThread
282 DIRECT, // Thread class is DirectOutputThread
283 DUPLICATING, // Thread class is DuplicatingThread
284 RECORD // Thread class is RecordThread
285 };
286
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 status_t dumpBase(int fd, const Vector<String16>& args);
Eric Laurent1d2bff02011-07-24 17:49:51 -0700288 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289
290 // base for record and playback
291 class TrackBase : public AudioBufferProvider, public RefBase {
292
293 public:
294 enum track_state {
295 IDLE,
296 TERMINATED,
297 STOPPED,
298 RESUMING,
299 ACTIVE,
300 PAUSING,
301 PAUSED
302 };
303
304 enum track_flags {
305 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
306 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
307 // The upper 16 bits are used for track-specific flags.
308 };
309
310 TrackBase(const wp<ThreadBase>& thread,
311 const sp<Client>& client,
312 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700313 uint32_t format,
314 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 int frameCount,
316 uint32_t flags,
317 const sp<IMemory>& sharedBuffer,
318 int sessionId);
319 ~TrackBase();
320
321 virtual status_t start() = 0;
322 virtual void stop() = 0;
323 sp<IMemory> getCblk() const;
324 audio_track_cblk_t* cblk() const { return mCblk; }
325 int sessionId() { return mSessionId; }
326
327 protected:
328 friend class ThreadBase;
329 friend class RecordHandle;
330 friend class PlaybackThread;
331 friend class RecordThread;
332 friend class MixerThread;
333 friend class DirectOutputThread;
334
335 TrackBase(const TrackBase&);
336 TrackBase& operator = (const TrackBase&);
337
338 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
339 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
340
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700341 uint32_t format() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342 return mFormat;
343 }
344
345 int channelCount() const ;
346
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700347 uint32_t channelMask() const;
348
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 int sampleRate() const;
350
351 void* getBuffer(uint32_t offset, uint32_t frames) const;
352
353 bool isStopped() const {
354 return mState == STOPPED;
355 }
356
357 bool isTerminated() const {
358 return mState == TERMINATED;
359 }
360
361 bool step();
362 void reset();
363
364 wp<ThreadBase> mThread;
365 sp<Client> mClient;
366 sp<IMemory> mCblkMemory;
367 audio_track_cblk_t* mCblk;
368 void* mBuffer;
369 void* mBufferEnd;
370 uint32_t mFrameCount;
371 // we don't really need a lock for these
372 int mState;
373 int mClientTid;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700374 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 uint32_t mFlags;
376 int mSessionId;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700377 uint8_t mChannelCount;
378 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 };
380
381 class ConfigEvent {
382 public:
383 ConfigEvent() : mEvent(0), mParam(0) {}
384
385 int mEvent;
386 int mParam;
387 };
388
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700389 virtual status_t initCheck() const = 0;
390 int type() const { return mType; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 uint32_t sampleRate() const;
392 int channelCount() const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700393 uint32_t format() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 size_t frameCount() const;
395 void wakeUp() { mWaitWorkCV.broadcast(); }
396 void exit();
397 virtual bool checkForNewParameters_l() = 0;
398 virtual status_t setParameters(const String8& keyValuePairs);
399 virtual String8 getParameters(const String8& keys) = 0;
400 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
401 void sendConfigEvent(int event, int param = 0);
402 void sendConfigEvent_l(int event, int param = 0);
403 void processConfigEvents();
404 int id() const { return mId;}
405 bool standby() { return mStandby; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700406 uint32_t device() { return mDevice; }
407 virtual audio_stream_t* stream() = 0;
408
409 sp<EffectHandle> createEffect_l(
410 const sp<AudioFlinger::Client>& client,
411 const sp<IEffectClient>& effectClient,
412 int32_t priority,
413 int sessionId,
414 effect_descriptor_t *desc,
415 int *enabled,
416 status_t *status);
417 void disconnectEffect(const sp< EffectModule>& effect,
418 const wp<EffectHandle>& handle);
419
420 // return values for hasAudioSession (bit field)
421 enum effect_state {
422 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
423 // effect
424 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
425 // track
426 };
427
428 // get effect chain corresponding to session Id.
429 sp<EffectChain> getEffectChain(int sessionId);
430 // same as getEffectChain() but must be called with ThreadBase mutex locked
431 sp<EffectChain> getEffectChain_l(int sessionId);
432 // add an effect chain to the chain list (mEffectChains)
433 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
434 // remove an effect chain from the chain list (mEffectChains)
435 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
436 // lock mall effect chains Mutexes. Must be called before releasing the
437 // ThreadBase mutex before processing the mixer and effects. This guarantees the
438 // integrity of the chains during the process.
439 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
440 // unlock effect chains after process
441 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
442 // set audio mode to all effect chains
443 void setMode(uint32_t mode);
444 // get effect module with corresponding ID on specified audio session
445 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
446 // add and effect module. Also creates the effect chain is none exists for
447 // the effects audio session
448 status_t addEffect_l(const sp< EffectModule>& effect);
449 // remove and effect module. Also removes the effect chain is this was the last
450 // effect
451 void removeEffect_l(const sp< EffectModule>& effect);
452 // detach all tracks connected to an auxiliary effect
453 virtual void detachAuxEffect_l(int effectId) {}
454 // returns either EFFECT_SESSION if effects on this audio session exist in one
455 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
456 virtual uint32_t hasAudioSession(int sessionId) = 0;
457 // the value returned by default implementation is not important as the
458 // strategy is only meaningful for PlaybackThread which implements this method
459 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460
461 mutable Mutex mLock;
462
463 protected:
464
465 friend class Track;
466 friend class TrackBase;
467 friend class PlaybackThread;
468 friend class MixerThread;
469 friend class DirectOutputThread;
470 friend class DuplicatingThread;
471 friend class RecordThread;
472 friend class RecordTrack;
473
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700474 int mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 Condition mWaitWorkCV;
476 sp<AudioFlinger> mAudioFlinger;
477 uint32_t mSampleRate;
478 size_t mFrameCount;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700479 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 uint16_t mChannelCount;
481 uint16_t mFrameSize;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700482 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 Condition mParamCond;
484 Vector<String8> mNewParameters;
485 status_t mParamStatus;
486 Vector<ConfigEvent *> mConfigEvents;
487 bool mStandby;
488 int mId;
489 bool mExiting;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700490 Vector< sp<EffectChain> > mEffectChains;
491 uint32_t mDevice; // output device for PlaybackThread
492 // input + output devices for RecordThread
Mathias Agopian65ab4712010-07-14 17:59:35 -0700493 };
494
495 // --- PlaybackThread ---
496 class PlaybackThread : public ThreadBase {
497 public:
498
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499 enum mixer_state {
500 MIXER_IDLE,
501 MIXER_TRACKS_ENABLED,
502 MIXER_TRACKS_READY
503 };
504
505 // playback track
506 class Track : public TrackBase {
507 public:
508 Track( const wp<ThreadBase>& thread,
509 const sp<Client>& client,
510 int streamType,
511 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700512 uint32_t format,
513 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514 int frameCount,
515 const sp<IMemory>& sharedBuffer,
516 int sessionId);
517 ~Track();
518
519 void dump(char* buffer, size_t size);
520 virtual status_t start();
521 virtual void stop();
522 void pause();
523
524 void flush();
525 void destroy();
526 void mute(bool);
527 void setVolume(float left, float right);
528 int name() const {
529 return mName;
530 }
531
532 int type() const {
533 return mStreamType;
534 }
535 status_t attachAuxEffect(int EffectId);
536 void setAuxBuffer(int EffectId, int32_t *buffer);
537 int32_t *auxBuffer() { return mAuxBuffer; }
538 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
539 int16_t *mainBuffer() { return mMainBuffer; }
540 int auxEffectId() { return mAuxEffectId; }
541
542
543 protected:
544 friend class ThreadBase;
545 friend class AudioFlinger;
546 friend class TrackHandle;
547 friend class PlaybackThread;
548 friend class MixerThread;
549 friend class DirectOutputThread;
550
551 Track(const Track&);
552 Track& operator = (const Track&);
553
554 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
555 bool isMuted() { return mMute; }
556 bool isPausing() const {
557 return mState == PAUSING;
558 }
559 bool isPaused() const {
560 return mState == PAUSED;
561 }
562 bool isReady() const;
563 void setPaused() { mState = PAUSED; }
564 void reset();
565
566 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700567 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 }
569
570 // we don't really need a lock for these
571 float mVolume[2];
572 volatile bool mMute;
573 // FILLED state is used for suppressing volume ramp at begin of playing
574 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
575 mutable uint8_t mFillingUpStatus;
576 int8_t mRetryCount;
577 sp<IMemory> mSharedBuffer;
578 bool mResetDone;
579 int mStreamType;
580 int mName;
581 int16_t *mMainBuffer;
582 int32_t *mAuxBuffer;
583 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700584 bool mHasVolumeController;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 }; // end of Track
586
587
588 // playback track
589 class OutputTrack : public Track {
590 public:
591
592 class Buffer: public AudioBufferProvider::Buffer {
593 public:
594 int16_t *mBuffer;
595 };
596
597 OutputTrack( const wp<ThreadBase>& thread,
598 DuplicatingThread *sourceThread,
599 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700600 uint32_t format,
601 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 int frameCount);
603 ~OutputTrack();
604
605 virtual status_t start();
606 virtual void stop();
607 bool write(int16_t* data, uint32_t frames);
608 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
609 bool isActive() { return mActive; }
610 wp<ThreadBase>& thread() { return mThread; }
611
612 private:
613
614 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
615 void clearBufferQueue();
616
617 // Maximum number of pending buffers allocated by OutputTrack::write()
618 static const uint8_t kMaxOverFlowBuffers = 10;
619
620 Vector < Buffer* > mBufferQueue;
621 AudioBufferProvider::Buffer mOutBuffer;
622 bool mActive;
623 DuplicatingThread* mSourceThread;
624 }; // end of OutputTrack
625
Dima Zavin799a70e2011-04-18 16:57:27 -0700626 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627 virtual ~PlaybackThread();
628
629 virtual status_t dump(int fd, const Vector<String16>& args);
630
631 // Thread virtuals
632 virtual status_t readyToRun();
633 virtual void onFirstRef();
634
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700635 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 virtual uint32_t latency() const;
638
639 virtual status_t setMasterVolume(float value);
640 virtual status_t setMasterMute(bool muted);
641
642 virtual float masterVolume() const;
643 virtual bool masterMute() const;
644
645 virtual status_t setStreamVolume(int stream, float value);
646 virtual status_t setStreamMute(int stream, bool muted);
647
648 virtual float streamVolume(int stream) const;
649 virtual bool streamMute(int stream) const;
650
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 sp<Track> createTrack_l(
652 const sp<AudioFlinger::Client>& client,
653 int streamType,
654 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700655 uint32_t format,
656 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 int frameCount,
658 const sp<IMemory>& sharedBuffer,
659 int sessionId,
660 status_t *status);
661
Dima Zavin799a70e2011-04-18 16:57:27 -0700662 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700663 virtual audio_stream_t* stream() { return &mOutput->stream->common; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 void suspend() { mSuspended++; }
666 void restore() { if (mSuspended) mSuspended--; }
667 bool isSuspended() { return (mSuspended != 0); }
668 virtual String8 getParameters(const String8& keys);
669 virtual void audioConfigChanged_l(int event, int param = 0);
670 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
671 int16_t *mixBuffer() { return mMixBuffer; };
672
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700673 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -0700674 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
675 int EffectId);
676 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
677 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700679 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
680 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
681 virtual uint32_t hasAudioSession(int sessionId);
682 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -0700683
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 struct stream_type_t {
685 stream_type_t()
686 : volume(1.0f),
687 mute(false)
688 {
689 }
690 float volume;
691 bool mute;
692 };
693
694 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 int16_t* mMixBuffer;
696 int mSuspended;
697 int mBytesWritten;
698 bool mMasterMute;
699 SortedVector< wp<Track> > mActiveTracks;
700
701 virtual int getTrackName_l() = 0;
702 virtual void deleteTrackName_l(int name) = 0;
703 virtual uint32_t activeSleepTimeUs() = 0;
704 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700705 virtual uint32_t suspendSleepTimeUs() = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706
707 private:
708
709 friend class AudioFlinger;
710 friend class OutputTrack;
711 friend class Track;
712 friend class TrackBase;
713 friend class MixerThread;
714 friend class DirectOutputThread;
715 friend class DuplicatingThread;
716
717 PlaybackThread(const Client&);
718 PlaybackThread& operator = (const PlaybackThread&);
719
720 status_t addTrack_l(const sp<Track>& track);
721 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -0700722 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723
724 void readOutputParameters();
725
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
727 status_t dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728
729 SortedVector< sp<Track> > mTracks;
730 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -0700731 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin799a70e2011-04-18 16:57:27 -0700732 AudioStreamOut* mOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 float mMasterVolume;
734 nsecs_t mLastWriteTime;
735 int mNumWrites;
736 int mNumDelayedWrites;
737 bool mInWrite;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 };
739
740 class MixerThread : public PlaybackThread {
741 public:
Eric Laurentde070132010-07-13 04:45:46 -0700742 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700743 AudioStreamOut* output,
Eric Laurentde070132010-07-13 04:45:46 -0700744 int id,
745 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746 virtual ~MixerThread();
747
748 // Thread virtuals
749 virtual bool threadLoop();
750
751 void invalidateTracks(int streamType);
752 virtual bool checkForNewParameters_l();
753 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
754
755 protected:
Eric Laurentde070132010-07-13 04:45:46 -0700756 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
757 Vector< sp<Track> > *tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 virtual int getTrackName_l();
759 virtual void deleteTrackName_l(int name);
760 virtual uint32_t activeSleepTimeUs();
761 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700762 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763
764 AudioMixer* mAudioMixer;
765 };
766
767 class DirectOutputThread : public PlaybackThread {
768 public:
769
Dima Zavin799a70e2011-04-18 16:57:27 -0700770 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 ~DirectOutputThread();
772
773 // Thread virtuals
774 virtual bool threadLoop();
775
776 virtual bool checkForNewParameters_l();
777
778 protected:
779 virtual int getTrackName_l();
780 virtual void deleteTrackName_l(int name);
781 virtual uint32_t activeSleepTimeUs();
782 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700783 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784
785 private:
786 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
787
788 float mLeftVolFloat;
789 float mRightVolFloat;
790 uint16_t mLeftVolShort;
791 uint16_t mRightVolShort;
792 };
793
794 class DuplicatingThread : public MixerThread {
795 public:
796 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
797 ~DuplicatingThread();
798
799 // Thread virtuals
800 virtual bool threadLoop();
801 void addOutputTrack(MixerThread* thread);
802 void removeOutputTrack(MixerThread* thread);
803 uint32_t waitTimeMs() { return mWaitTimeMs; }
804 protected:
805 virtual uint32_t activeSleepTimeUs();
806
807 private:
808 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
809 void updateWaitTime();
810
811 SortedVector < sp<OutputTrack> > mOutputTracks;
812 uint32_t mWaitTimeMs;
813 };
814
815 PlaybackThread *checkPlaybackThread_l(int output) const;
816 MixerThread *checkMixerThread_l(int output) const;
817 RecordThread *checkRecordThread_l(int input) const;
818 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
819 void audioConfigChanged_l(int event, int ioHandle, void *param2);
820
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700821 uint32_t nextUniqueId();
Eric Laurentde070132010-07-13 04:45:46 -0700822 status_t moveEffectChain_l(int session,
823 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -0700824 AudioFlinger::PlaybackThread *dstThread,
825 bool reRegister);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700826 PlaybackThread *primaryPlaybackThread_l();
827 uint32_t primaryOutputDevice_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828
829 friend class AudioBuffer;
830
831 class TrackHandle : public android::BnAudioTrack {
832 public:
833 TrackHandle(const sp<PlaybackThread::Track>& track);
834 virtual ~TrackHandle();
835 virtual status_t start();
836 virtual void stop();
837 virtual void flush();
838 virtual void mute(bool);
839 virtual void pause();
840 virtual void setVolume(float left, float right);
841 virtual sp<IMemory> getCblk() const;
842 virtual status_t attachAuxEffect(int effectId);
843 virtual status_t onTransact(
844 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
845 private:
846 sp<PlaybackThread::Track> mTrack;
847 };
848
849 friend class Client;
850 friend class PlaybackThread::Track;
851
852
853 void removeClient_l(pid_t pid);
854 void removeNotificationClient(pid_t pid);
855
856
857 // record thread
858 class RecordThread : public ThreadBase, public AudioBufferProvider
859 {
860 public:
861
862 // record track
863 class RecordTrack : public TrackBase {
864 public:
865 RecordTrack(const wp<ThreadBase>& thread,
866 const sp<Client>& client,
867 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700868 uint32_t format,
869 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 int frameCount,
871 uint32_t flags,
872 int sessionId);
873 ~RecordTrack();
874
875 virtual status_t start();
876 virtual void stop();
877
878 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
879 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
880
881 void dump(char* buffer, size_t size);
882 private:
883 friend class AudioFlinger;
884 friend class RecordThread;
885
886 RecordTrack(const RecordTrack&);
887 RecordTrack& operator = (const RecordTrack&);
888
889 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
890
891 bool mOverflow;
892 };
893
894
895 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700896 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 uint32_t sampleRate,
898 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700899 int id,
900 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 ~RecordThread();
902
903 virtual bool threadLoop();
904 virtual status_t readyToRun() { return NO_ERROR; }
905 virtual void onFirstRef();
906
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700907 virtual status_t initCheck() const { return (mInput == 0) ? NO_INIT : NO_ERROR; }
908 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
909 const sp<AudioFlinger::Client>& client,
910 uint32_t sampleRate,
911 int format,
912 int channelMask,
913 int frameCount,
914 uint32_t flags,
915 int sessionId,
916 status_t *status);
917
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 status_t start(RecordTrack* recordTrack);
919 void stop(RecordTrack* recordTrack);
920 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin799a70e2011-04-18 16:57:27 -0700921 AudioStreamIn* getInput() { return mInput; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700922 virtual audio_stream_t* stream() { return &mInput->stream->common; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700924
925 void setTrack(RecordTrack *recordTrack) { mTrack = recordTrack; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
927 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
928 virtual bool checkForNewParameters_l();
929 virtual String8 getParameters(const String8& keys);
930 virtual void audioConfigChanged_l(int event, int param = 0);
931 void readInputParameters();
932 virtual unsigned int getInputFramesLost();
933
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700934 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
935 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
936 virtual uint32_t hasAudioSession(int sessionId);
937
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 private:
939 RecordThread();
Dima Zavin799a70e2011-04-18 16:57:27 -0700940 AudioStreamIn *mInput;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700941 RecordTrack* mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 sp<RecordTrack> mActiveTrack;
943 Condition mStartStopCond;
944 AudioResampler *mResampler;
945 int32_t *mRsmpOutBuffer;
946 int16_t *mRsmpInBuffer;
947 size_t mRsmpInIndex;
948 size_t mInputBytes;
949 int mReqChannelCount;
950 uint32_t mReqSampleRate;
951 ssize_t mBytesRead;
952 };
953
954 class RecordHandle : public android::BnAudioRecord {
955 public:
956 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
957 virtual ~RecordHandle();
958 virtual status_t start();
959 virtual void stop();
960 virtual sp<IMemory> getCblk() const;
961 virtual status_t onTransact(
962 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
963 private:
964 sp<RecordThread::RecordTrack> mRecordTrack;
965 };
966
967 //--- Audio Effect Management
968
969 // EffectModule and EffectChain classes both have their own mutex to protect
970 // state changes or resource modifications. Always respect the following order
971 // if multiple mutexes must be acquired to avoid cross deadlock:
972 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
973
974 // The EffectModule class is a wrapper object controlling the effect engine implementation
975 // in the effect library. It prevents concurrent calls to process() and command() functions
976 // from different client threads. It keeps a list of EffectHandle objects corresponding
977 // to all client applications using this effect and notifies applications of effect state,
978 // control or parameter changes. It manages the activation state machine to send appropriate
979 // reset, enable, disable commands to effect engine and provide volume
980 // ramping when effects are activated/deactivated.
981 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
982 // the attached track(s) to accumulate their auxiliary channel.
983 class EffectModule: public RefBase {
984 public:
985 EffectModule(const wp<ThreadBase>& wThread,
986 const wp<AudioFlinger::EffectChain>& chain,
987 effect_descriptor_t *desc,
988 int id,
989 int sessionId);
990 ~EffectModule();
991
992 enum effect_state {
993 IDLE,
994 RESTART,
995 STARTING,
996 ACTIVE,
997 STOPPING,
998 STOPPED
999 };
1000
1001 int id() { return mId; }
1002 void process();
1003 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001004 status_t command(uint32_t cmdCode,
1005 uint32_t cmdSize,
1006 void *pCmdData,
1007 uint32_t *replySize,
1008 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009
1010 void reset_l();
1011 status_t configure();
1012 status_t init();
1013 uint32_t state() {
1014 return mState;
1015 }
1016 uint32_t status() {
1017 return mStatus;
1018 }
Eric Laurentde070132010-07-13 04:45:46 -07001019 int sessionId() {
1020 return mSessionId;
1021 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 status_t setEnabled(bool enabled);
1023 bool isEnabled();
Eric Laurent8f45bd72010-08-31 13:50:07 -07001024 bool isProcessEnabled();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025
1026 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1027 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1028 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1029 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001030 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1031 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032
1033 status_t addHandle(sp<EffectHandle>& handle);
1034 void disconnect(const wp<EffectHandle>& handle);
1035 size_t removeHandle (const wp<EffectHandle>& handle);
1036
1037 effect_descriptor_t& desc() { return mDescriptor; }
1038 wp<EffectChain>& chain() { return mChain; }
1039
1040 status_t setDevice(uint32_t device);
1041 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
1042 status_t setMode(uint32_t mode);
1043
1044 status_t dump(int fd, const Vector<String16>& args);
1045
1046 protected:
1047
1048 // Maximum time allocated to effect engines to complete the turn off sequence
1049 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1050
1051 EffectModule(const EffectModule&);
1052 EffectModule& operator = (const EffectModule&);
1053
1054 status_t start_l();
1055 status_t stop_l();
1056
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 Mutex mLock; // mutex for process, commands and handles list protection
1058 wp<ThreadBase> mThread; // parent thread
1059 wp<EffectChain> mChain; // parent effect chain
1060 int mId; // this instance unique ID
1061 int mSessionId; // audio session ID
1062 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1063 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001064 effect_handle_t mEffectInterface; // Effect module C API
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 status_t mStatus; // initialization status
1066 uint32_t mState; // current activation state (effect_state)
1067 Vector< wp<EffectHandle> > mHandles; // list of client handles
1068 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1069 // sending disable command.
1070 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
1071 };
1072
1073 // The EffectHandle class implements the IEffect interface. It provides resources
1074 // to receive parameter updates, keeps track of effect control
1075 // ownership and state and has a pointer to the EffectModule object it is controlling.
1076 // There is one EffectHandle object for each application controlling (or using)
1077 // an effect module.
1078 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1079 class EffectHandle: public android::BnEffect {
1080 public:
1081
1082 EffectHandle(const sp<EffectModule>& effect,
1083 const sp<AudioFlinger::Client>& client,
1084 const sp<IEffectClient>& effectClient,
1085 int32_t priority);
1086 virtual ~EffectHandle();
1087
1088 // IEffect
1089 virtual status_t enable();
1090 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001091 virtual status_t command(uint32_t cmdCode,
1092 uint32_t cmdSize,
1093 void *pCmdData,
1094 uint32_t *replySize,
1095 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 virtual void disconnect();
1097 virtual sp<IMemory> getCblk() const;
1098 virtual status_t onTransact(uint32_t code, const Parcel& data,
1099 Parcel* reply, uint32_t flags);
1100
1101
1102 // Give or take control of effect module
1103 void setControl(bool hasControl, bool signal);
Eric Laurent25f43952010-07-28 05:40:18 -07001104 void commandExecuted(uint32_t cmdCode,
1105 uint32_t cmdSize,
1106 void *pCmdData,
1107 uint32_t replySize,
1108 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001109 void setEnabled(bool enabled);
1110
1111 // Getters
1112 int id() { return mEffect->id(); }
1113 int priority() { return mPriority; }
1114 bool hasControl() { return mHasControl; }
1115 sp<EffectModule> effect() { return mEffect; }
1116
1117 void dump(char* buffer, size_t size);
1118
1119 protected:
1120
1121 EffectHandle(const EffectHandle&);
1122 EffectHandle& operator =(const EffectHandle&);
1123
1124 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1125 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1126 sp<Client> mClient; // client for shared memory allocation
1127 sp<IMemory> mCblkMemory; // shared memory for control block
1128 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1129 uint8_t* mBuffer; // pointer to parameter area in shared memory
1130 int mPriority; // client application priority to control the effect
1131 bool mHasControl; // true if this handle is controlling the effect
1132 };
1133
1134 // the EffectChain class represents a group of effects associated to one audio session.
1135 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1136 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1137 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1138 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1139 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1140 // input buffer used by the track as accumulation buffer.
1141 class EffectChain: public RefBase {
1142 public:
1143 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1144 ~EffectChain();
1145
1146 void process_l();
1147
1148 void lock() {
1149 mLock.lock();
1150 }
1151 void unlock() {
1152 mLock.unlock();
1153 }
1154
Eric Laurentde070132010-07-13 04:45:46 -07001155 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001156 size_t removeEffect_l(const sp<EffectModule>& handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001158 int sessionId() { return mSessionId; }
1159 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001160
Eric Laurentcab11242010-07-15 12:50:15 -07001161 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1162 sp<EffectModule> getEffectFromId_l(int id);
1163 bool setVolume_l(uint32_t *left, uint32_t *right);
1164 void setDevice_l(uint32_t device);
1165 void setMode_l(uint32_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1168 mInBuffer = buffer;
1169 mOwnInBuffer = ownsBuffer;
1170 }
1171 int16_t *inBuffer() {
1172 return mInBuffer;
1173 }
1174 void setOutBuffer(int16_t *buffer) {
1175 mOutBuffer = buffer;
1176 }
1177 int16_t *outBuffer() {
1178 return mOutBuffer;
1179 }
1180
Eric Laurentb469b942011-05-09 12:09:06 -07001181 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1182 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1183 int32_t trackCnt() { return mTrackCnt;}
1184
1185 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1186 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1187 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001188
Eric Laurentde070132010-07-13 04:45:46 -07001189 uint32_t strategy() { return mStrategy; }
1190 void setStrategy(uint32_t strategy)
1191 { mStrategy = strategy; }
1192
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193 status_t dump(int fd, const Vector<String16>& args);
1194
1195 protected:
1196
1197 EffectChain(const EffectChain&);
1198 EffectChain& operator =(const EffectChain&);
1199
1200 wp<ThreadBase> mThread; // parent mixer thread
1201 Mutex mLock; // mutex protecting effect list
1202 Vector<sp<EffectModule> > mEffects; // list of effect modules
1203 int mSessionId; // audio session ID
1204 int16_t *mInBuffer; // chain input buffer
1205 int16_t *mOutBuffer; // chain output buffer
Eric Laurentb469b942011-05-09 12:09:06 -07001206 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1207 volatile int32_t mTrackCnt; // number of tracks connected
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07001209 int mVolumeCtrlIdx; // index of insert effect having control over volume
1210 uint32_t mLeftVolume; // previous volume on left channel
1211 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07001212 uint32_t mNewLeftVolume; // new volume on left channel
1213 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07001214 uint32_t mStrategy; // strategy for this effect chain
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215 };
1216
Dima Zavin799a70e2011-04-18 16:57:27 -07001217 struct AudioStreamOut {
1218 audio_hw_device_t *hwDev;
1219 audio_stream_out_t *stream;
1220
1221 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1222 hwDev(dev), stream(out) {}
1223 };
1224
1225 struct AudioStreamIn {
1226 audio_hw_device_t *hwDev;
1227 audio_stream_in_t *stream;
1228
1229 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1230 hwDev(dev), stream(in) {}
1231 };
1232
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 friend class RecordThread;
1234 friend class PlaybackThread;
1235
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 mutable Mutex mLock;
1237
1238 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1239
1240 mutable Mutex mHardwareLock;
Dima Zavin799a70e2011-04-18 16:57:27 -07001241 audio_hw_device_t* mPrimaryHardwareDev;
1242 Vector<audio_hw_device_t*> mAudioHwDevs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 mutable int mHardwareStatus;
1244
1245
1246 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavinfce7a472011-04-19 22:30:36 -07001247 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 float mMasterVolume;
1249 bool mMasterMute;
1250
1251 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
1252
1253 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
1254 volatile int32_t mNextUniqueId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 uint32_t mMode;
1256
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257};
1258
Dima Zavin799a70e2011-04-18 16:57:27 -07001259
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260// ----------------------------------------------------------------------------
1261
1262}; // namespace android
1263
1264#endif // ANDROID_AUDIO_FLINGER_H