blob: edd3e2ab766c44970a0808797367956a81f181df [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 Laurentde070132010-07-13 04:45:46 -0700168 virtual status_t moveEffects(int session, 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
480 mutable Mutex mLock;
481
482 protected:
483
Eric Laurentfeb0db62011-07-22 09:04:31 -0700484 void acquireWakeLock();
485 void acquireWakeLock_l();
486 void releaseWakeLock();
487 void releaseWakeLock_l();
488
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 friend class Track;
490 friend class TrackBase;
491 friend class PlaybackThread;
492 friend class MixerThread;
493 friend class DirectOutputThread;
494 friend class DuplicatingThread;
495 friend class RecordThread;
496 friend class RecordTrack;
497
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700498 int mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499 Condition mWaitWorkCV;
500 sp<AudioFlinger> mAudioFlinger;
501 uint32_t mSampleRate;
502 size_t mFrameCount;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700503 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 uint16_t mChannelCount;
505 uint16_t mFrameSize;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700506 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 Condition mParamCond;
508 Vector<String8> mNewParameters;
509 status_t mParamStatus;
510 Vector<ConfigEvent *> mConfigEvents;
511 bool mStandby;
512 int mId;
513 bool mExiting;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700514 Vector< sp<EffectChain> > mEffectChains;
515 uint32_t mDevice; // output device for PlaybackThread
516 // input + output devices for RecordThread
Eric Laurentfeb0db62011-07-22 09:04:31 -0700517 static const int kNameLength = 32;
518 char mName[kNameLength];
519 sp<IPowerManager> mPowerManager;
520 sp<IBinder> mWakeLockToken;
521 sp<PMDeathRecipient> mDeathRecipient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522 };
523
524 // --- PlaybackThread ---
525 class PlaybackThread : public ThreadBase {
526 public:
527
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 enum mixer_state {
529 MIXER_IDLE,
530 MIXER_TRACKS_ENABLED,
531 MIXER_TRACKS_READY
532 };
533
534 // playback track
535 class Track : public TrackBase {
536 public:
537 Track( const wp<ThreadBase>& thread,
538 const sp<Client>& client,
539 int streamType,
540 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700541 uint32_t format,
542 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 int frameCount,
544 const sp<IMemory>& sharedBuffer,
545 int sessionId);
546 ~Track();
547
548 void dump(char* buffer, size_t size);
549 virtual status_t start();
550 virtual void stop();
551 void pause();
552
553 void flush();
554 void destroy();
555 void mute(bool);
556 void setVolume(float left, float right);
557 int name() const {
558 return mName;
559 }
560
561 int type() const {
562 return mStreamType;
563 }
564 status_t attachAuxEffect(int EffectId);
565 void setAuxBuffer(int EffectId, int32_t *buffer);
566 int32_t *auxBuffer() { return mAuxBuffer; }
567 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
568 int16_t *mainBuffer() { return mMainBuffer; }
569 int auxEffectId() { return mAuxEffectId; }
570
571
572 protected:
573 friend class ThreadBase;
574 friend class AudioFlinger;
575 friend class TrackHandle;
576 friend class PlaybackThread;
577 friend class MixerThread;
578 friend class DirectOutputThread;
579
580 Track(const Track&);
581 Track& operator = (const Track&);
582
583 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
584 bool isMuted() { return mMute; }
585 bool isPausing() const {
586 return mState == PAUSING;
587 }
588 bool isPaused() const {
589 return mState == PAUSED;
590 }
591 bool isReady() const;
592 void setPaused() { mState = PAUSED; }
593 void reset();
594
595 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700596 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 }
598
599 // we don't really need a lock for these
600 float mVolume[2];
601 volatile bool mMute;
602 // FILLED state is used for suppressing volume ramp at begin of playing
603 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
604 mutable uint8_t mFillingUpStatus;
605 int8_t mRetryCount;
606 sp<IMemory> mSharedBuffer;
607 bool mResetDone;
608 int mStreamType;
609 int mName;
610 int16_t *mMainBuffer;
611 int32_t *mAuxBuffer;
612 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700613 bool mHasVolumeController;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 }; // end of Track
615
616
617 // playback track
618 class OutputTrack : public Track {
619 public:
620
621 class Buffer: public AudioBufferProvider::Buffer {
622 public:
623 int16_t *mBuffer;
624 };
625
626 OutputTrack( const wp<ThreadBase>& thread,
627 DuplicatingThread *sourceThread,
628 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700629 uint32_t format,
630 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 int frameCount);
632 ~OutputTrack();
633
634 virtual status_t start();
635 virtual void stop();
636 bool write(int16_t* data, uint32_t frames);
637 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
638 bool isActive() { return mActive; }
639 wp<ThreadBase>& thread() { return mThread; }
640
641 private:
642
643 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
644 void clearBufferQueue();
645
646 // Maximum number of pending buffers allocated by OutputTrack::write()
647 static const uint8_t kMaxOverFlowBuffers = 10;
648
649 Vector < Buffer* > mBufferQueue;
650 AudioBufferProvider::Buffer mOutBuffer;
651 bool mActive;
652 DuplicatingThread* mSourceThread;
653 }; // end of OutputTrack
654
Dima Zavin799a70e2011-04-18 16:57:27 -0700655 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 virtual ~PlaybackThread();
657
658 virtual status_t dump(int fd, const Vector<String16>& args);
659
660 // Thread virtuals
661 virtual status_t readyToRun();
662 virtual void onFirstRef();
663
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700664 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
665
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 virtual uint32_t latency() const;
667
668 virtual status_t setMasterVolume(float value);
669 virtual status_t setMasterMute(bool muted);
670
671 virtual float masterVolume() const;
672 virtual bool masterMute() const;
673
674 virtual status_t setStreamVolume(int stream, float value);
675 virtual status_t setStreamMute(int stream, bool muted);
676
677 virtual float streamVolume(int stream) const;
678 virtual bool streamMute(int stream) const;
679
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 sp<Track> createTrack_l(
681 const sp<AudioFlinger::Client>& client,
682 int streamType,
683 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700684 uint32_t format,
685 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 int frameCount,
687 const sp<IMemory>& sharedBuffer,
688 int sessionId,
689 status_t *status);
690
Dima Zavin799a70e2011-04-18 16:57:27 -0700691 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700692 virtual audio_stream_t* stream() { return &mOutput->stream->common; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 void suspend() { mSuspended++; }
695 void restore() { if (mSuspended) mSuspended--; }
696 bool isSuspended() { return (mSuspended != 0); }
697 virtual String8 getParameters(const String8& keys);
698 virtual void audioConfigChanged_l(int event, int param = 0);
699 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
700 int16_t *mixBuffer() { return mMixBuffer; };
701
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700702 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -0700703 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
704 int EffectId);
705 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
706 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700708 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
709 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
710 virtual uint32_t hasAudioSession(int sessionId);
711 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -0700712
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 struct stream_type_t {
714 stream_type_t()
715 : volume(1.0f),
716 mute(false)
717 {
718 }
719 float volume;
720 bool mute;
721 };
722
723 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 int16_t* mMixBuffer;
725 int mSuspended;
726 int mBytesWritten;
727 bool mMasterMute;
728 SortedVector< wp<Track> > mActiveTracks;
729
730 virtual int getTrackName_l() = 0;
731 virtual void deleteTrackName_l(int name) = 0;
732 virtual uint32_t activeSleepTimeUs() = 0;
733 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700734 virtual uint32_t suspendSleepTimeUs() = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735
736 private:
737
738 friend class AudioFlinger;
739 friend class OutputTrack;
740 friend class Track;
741 friend class TrackBase;
742 friend class MixerThread;
743 friend class DirectOutputThread;
744 friend class DuplicatingThread;
745
746 PlaybackThread(const Client&);
747 PlaybackThread& operator = (const PlaybackThread&);
748
749 status_t addTrack_l(const sp<Track>& track);
750 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -0700751 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752
753 void readOutputParameters();
754
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
756 status_t dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757
758 SortedVector< sp<Track> > mTracks;
759 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -0700760 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin799a70e2011-04-18 16:57:27 -0700761 AudioStreamOut* mOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762 float mMasterVolume;
763 nsecs_t mLastWriteTime;
764 int mNumWrites;
765 int mNumDelayedWrites;
766 bool mInWrite;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 };
768
769 class MixerThread : public PlaybackThread {
770 public:
Eric Laurentde070132010-07-13 04:45:46 -0700771 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700772 AudioStreamOut* output,
Eric Laurentde070132010-07-13 04:45:46 -0700773 int id,
774 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 virtual ~MixerThread();
776
777 // Thread virtuals
778 virtual bool threadLoop();
779
780 void invalidateTracks(int streamType);
781 virtual bool checkForNewParameters_l();
782 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
783
784 protected:
Eric Laurentde070132010-07-13 04:45:46 -0700785 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
786 Vector< sp<Track> > *tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787 virtual int getTrackName_l();
788 virtual void deleteTrackName_l(int name);
789 virtual uint32_t activeSleepTimeUs();
790 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700791 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792
793 AudioMixer* mAudioMixer;
794 };
795
796 class DirectOutputThread : public PlaybackThread {
797 public:
798
Dima Zavin799a70e2011-04-18 16:57:27 -0700799 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 ~DirectOutputThread();
801
802 // Thread virtuals
803 virtual bool threadLoop();
804
805 virtual bool checkForNewParameters_l();
806
807 protected:
808 virtual int getTrackName_l();
809 virtual void deleteTrackName_l(int name);
810 virtual uint32_t activeSleepTimeUs();
811 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700812 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813
814 private:
815 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
816
817 float mLeftVolFloat;
818 float mRightVolFloat;
819 uint16_t mLeftVolShort;
820 uint16_t mRightVolShort;
821 };
822
823 class DuplicatingThread : public MixerThread {
824 public:
825 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
826 ~DuplicatingThread();
827
828 // Thread virtuals
829 virtual bool threadLoop();
830 void addOutputTrack(MixerThread* thread);
831 void removeOutputTrack(MixerThread* thread);
832 uint32_t waitTimeMs() { return mWaitTimeMs; }
833 protected:
834 virtual uint32_t activeSleepTimeUs();
835
836 private:
837 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
838 void updateWaitTime();
839
840 SortedVector < sp<OutputTrack> > mOutputTracks;
841 uint32_t mWaitTimeMs;
842 };
843
844 PlaybackThread *checkPlaybackThread_l(int output) const;
845 MixerThread *checkMixerThread_l(int output) const;
846 RecordThread *checkRecordThread_l(int input) const;
847 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
848 void audioConfigChanged_l(int event, int ioHandle, void *param2);
849
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700850 uint32_t nextUniqueId();
Eric Laurentde070132010-07-13 04:45:46 -0700851 status_t moveEffectChain_l(int session,
852 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -0700853 AudioFlinger::PlaybackThread *dstThread,
854 bool reRegister);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700855 PlaybackThread *primaryPlaybackThread_l();
856 uint32_t primaryOutputDevice_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857
858 friend class AudioBuffer;
859
860 class TrackHandle : public android::BnAudioTrack {
861 public:
862 TrackHandle(const sp<PlaybackThread::Track>& track);
863 virtual ~TrackHandle();
864 virtual status_t start();
865 virtual void stop();
866 virtual void flush();
867 virtual void mute(bool);
868 virtual void pause();
869 virtual void setVolume(float left, float right);
870 virtual sp<IMemory> getCblk() const;
871 virtual status_t attachAuxEffect(int effectId);
872 virtual status_t onTransact(
873 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
874 private:
875 sp<PlaybackThread::Track> mTrack;
876 };
877
878 friend class Client;
879 friend class PlaybackThread::Track;
880
881
882 void removeClient_l(pid_t pid);
883 void removeNotificationClient(pid_t pid);
884
885
886 // record thread
887 class RecordThread : public ThreadBase, public AudioBufferProvider
888 {
889 public:
890
891 // record track
892 class RecordTrack : public TrackBase {
893 public:
894 RecordTrack(const wp<ThreadBase>& thread,
895 const sp<Client>& client,
896 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700897 uint32_t format,
898 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 int frameCount,
900 uint32_t flags,
901 int sessionId);
902 ~RecordTrack();
903
904 virtual status_t start();
905 virtual void stop();
906
907 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
908 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
909
910 void dump(char* buffer, size_t size);
911 private:
912 friend class AudioFlinger;
913 friend class RecordThread;
914
915 RecordTrack(const RecordTrack&);
916 RecordTrack& operator = (const RecordTrack&);
917
918 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
919
920 bool mOverflow;
921 };
922
923
924 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700925 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 uint32_t sampleRate,
927 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700928 int id,
929 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 ~RecordThread();
931
932 virtual bool threadLoop();
933 virtual status_t readyToRun() { return NO_ERROR; }
934 virtual void onFirstRef();
935
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700936 virtual status_t initCheck() const { return (mInput == 0) ? NO_INIT : NO_ERROR; }
937 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
938 const sp<AudioFlinger::Client>& client,
939 uint32_t sampleRate,
940 int format,
941 int channelMask,
942 int frameCount,
943 uint32_t flags,
944 int sessionId,
945 status_t *status);
946
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 status_t start(RecordTrack* recordTrack);
948 void stop(RecordTrack* recordTrack);
949 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin799a70e2011-04-18 16:57:27 -0700950 AudioStreamIn* getInput() { return mInput; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700951 virtual audio_stream_t* stream() { return &mInput->stream->common; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700953
954 void setTrack(RecordTrack *recordTrack) { mTrack = recordTrack; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
956 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
957 virtual bool checkForNewParameters_l();
958 virtual String8 getParameters(const String8& keys);
959 virtual void audioConfigChanged_l(int event, int param = 0);
960 void readInputParameters();
961 virtual unsigned int getInputFramesLost();
962
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700963 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
964 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
965 virtual uint32_t hasAudioSession(int sessionId);
966
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 private:
968 RecordThread();
Dima Zavin799a70e2011-04-18 16:57:27 -0700969 AudioStreamIn *mInput;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700970 RecordTrack* mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 sp<RecordTrack> mActiveTrack;
972 Condition mStartStopCond;
973 AudioResampler *mResampler;
974 int32_t *mRsmpOutBuffer;
975 int16_t *mRsmpInBuffer;
976 size_t mRsmpInIndex;
977 size_t mInputBytes;
978 int mReqChannelCount;
979 uint32_t mReqSampleRate;
980 ssize_t mBytesRead;
981 };
982
983 class RecordHandle : public android::BnAudioRecord {
984 public:
985 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
986 virtual ~RecordHandle();
987 virtual status_t start();
988 virtual void stop();
989 virtual sp<IMemory> getCblk() const;
990 virtual status_t onTransact(
991 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
992 private:
993 sp<RecordThread::RecordTrack> mRecordTrack;
994 };
995
996 //--- Audio Effect Management
997
998 // EffectModule and EffectChain classes both have their own mutex to protect
999 // state changes or resource modifications. Always respect the following order
1000 // if multiple mutexes must be acquired to avoid cross deadlock:
1001 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1002
1003 // The EffectModule class is a wrapper object controlling the effect engine implementation
1004 // in the effect library. It prevents concurrent calls to process() and command() functions
1005 // from different client threads. It keeps a list of EffectHandle objects corresponding
1006 // to all client applications using this effect and notifies applications of effect state,
1007 // control or parameter changes. It manages the activation state machine to send appropriate
1008 // reset, enable, disable commands to effect engine and provide volume
1009 // ramping when effects are activated/deactivated.
1010 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1011 // the attached track(s) to accumulate their auxiliary channel.
1012 class EffectModule: public RefBase {
1013 public:
1014 EffectModule(const wp<ThreadBase>& wThread,
1015 const wp<AudioFlinger::EffectChain>& chain,
1016 effect_descriptor_t *desc,
1017 int id,
1018 int sessionId);
1019 ~EffectModule();
1020
1021 enum effect_state {
1022 IDLE,
1023 RESTART,
1024 STARTING,
1025 ACTIVE,
1026 STOPPING,
1027 STOPPED
1028 };
1029
1030 int id() { return mId; }
1031 void process();
1032 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001033 status_t command(uint32_t cmdCode,
1034 uint32_t cmdSize,
1035 void *pCmdData,
1036 uint32_t *replySize,
1037 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038
1039 void reset_l();
1040 status_t configure();
1041 status_t init();
1042 uint32_t state() {
1043 return mState;
1044 }
1045 uint32_t status() {
1046 return mStatus;
1047 }
Eric Laurentde070132010-07-13 04:45:46 -07001048 int sessionId() {
1049 return mSessionId;
1050 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 status_t setEnabled(bool enabled);
1052 bool isEnabled();
Eric Laurent8f45bd72010-08-31 13:50:07 -07001053 bool isProcessEnabled();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054
1055 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1056 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1057 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1058 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001059 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1060 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061
1062 status_t addHandle(sp<EffectHandle>& handle);
1063 void disconnect(const wp<EffectHandle>& handle);
1064 size_t removeHandle (const wp<EffectHandle>& handle);
1065
1066 effect_descriptor_t& desc() { return mDescriptor; }
1067 wp<EffectChain>& chain() { return mChain; }
1068
1069 status_t setDevice(uint32_t device);
1070 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
1071 status_t setMode(uint32_t mode);
1072
1073 status_t dump(int fd, const Vector<String16>& args);
1074
1075 protected:
1076
1077 // Maximum time allocated to effect engines to complete the turn off sequence
1078 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1079
1080 EffectModule(const EffectModule&);
1081 EffectModule& operator = (const EffectModule&);
1082
1083 status_t start_l();
1084 status_t stop_l();
1085
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086 Mutex mLock; // mutex for process, commands and handles list protection
1087 wp<ThreadBase> mThread; // parent thread
1088 wp<EffectChain> mChain; // parent effect chain
1089 int mId; // this instance unique ID
1090 int mSessionId; // audio session ID
1091 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1092 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001093 effect_handle_t mEffectInterface; // Effect module C API
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094 status_t mStatus; // initialization status
1095 uint32_t mState; // current activation state (effect_state)
1096 Vector< wp<EffectHandle> > mHandles; // list of client handles
1097 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1098 // sending disable command.
1099 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
1100 };
1101
1102 // The EffectHandle class implements the IEffect interface. It provides resources
1103 // to receive parameter updates, keeps track of effect control
1104 // ownership and state and has a pointer to the EffectModule object it is controlling.
1105 // There is one EffectHandle object for each application controlling (or using)
1106 // an effect module.
1107 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1108 class EffectHandle: public android::BnEffect {
1109 public:
1110
1111 EffectHandle(const sp<EffectModule>& effect,
1112 const sp<AudioFlinger::Client>& client,
1113 const sp<IEffectClient>& effectClient,
1114 int32_t priority);
1115 virtual ~EffectHandle();
1116
1117 // IEffect
1118 virtual status_t enable();
1119 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001120 virtual status_t command(uint32_t cmdCode,
1121 uint32_t cmdSize,
1122 void *pCmdData,
1123 uint32_t *replySize,
1124 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 virtual void disconnect();
1126 virtual sp<IMemory> getCblk() const;
1127 virtual status_t onTransact(uint32_t code, const Parcel& data,
1128 Parcel* reply, uint32_t flags);
1129
1130
1131 // Give or take control of effect module
1132 void setControl(bool hasControl, bool signal);
Eric Laurent25f43952010-07-28 05:40:18 -07001133 void commandExecuted(uint32_t cmdCode,
1134 uint32_t cmdSize,
1135 void *pCmdData,
1136 uint32_t replySize,
1137 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001138 void setEnabled(bool enabled);
1139
1140 // Getters
1141 int id() { return mEffect->id(); }
1142 int priority() { return mPriority; }
1143 bool hasControl() { return mHasControl; }
1144 sp<EffectModule> effect() { return mEffect; }
1145
1146 void dump(char* buffer, size_t size);
1147
1148 protected:
1149
1150 EffectHandle(const EffectHandle&);
1151 EffectHandle& operator =(const EffectHandle&);
1152
1153 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1154 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1155 sp<Client> mClient; // client for shared memory allocation
1156 sp<IMemory> mCblkMemory; // shared memory for control block
1157 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1158 uint8_t* mBuffer; // pointer to parameter area in shared memory
1159 int mPriority; // client application priority to control the effect
1160 bool mHasControl; // true if this handle is controlling the effect
1161 };
1162
1163 // the EffectChain class represents a group of effects associated to one audio session.
1164 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1165 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1166 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1167 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1168 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1169 // input buffer used by the track as accumulation buffer.
1170 class EffectChain: public RefBase {
1171 public:
1172 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1173 ~EffectChain();
1174
1175 void process_l();
1176
1177 void lock() {
1178 mLock.lock();
1179 }
1180 void unlock() {
1181 mLock.unlock();
1182 }
1183
Eric Laurentde070132010-07-13 04:45:46 -07001184 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001185 size_t removeEffect_l(const sp<EffectModule>& handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001187 int sessionId() { return mSessionId; }
1188 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001189
Eric Laurentcab11242010-07-15 12:50:15 -07001190 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1191 sp<EffectModule> getEffectFromId_l(int id);
1192 bool setVolume_l(uint32_t *left, uint32_t *right);
1193 void setDevice_l(uint32_t device);
1194 void setMode_l(uint32_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1197 mInBuffer = buffer;
1198 mOwnInBuffer = ownsBuffer;
1199 }
1200 int16_t *inBuffer() {
1201 return mInBuffer;
1202 }
1203 void setOutBuffer(int16_t *buffer) {
1204 mOutBuffer = buffer;
1205 }
1206 int16_t *outBuffer() {
1207 return mOutBuffer;
1208 }
1209
Eric Laurentb469b942011-05-09 12:09:06 -07001210 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1211 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1212 int32_t trackCnt() { return mTrackCnt;}
1213
1214 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1215 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1216 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217
Eric Laurentde070132010-07-13 04:45:46 -07001218 uint32_t strategy() { return mStrategy; }
1219 void setStrategy(uint32_t strategy)
1220 { mStrategy = strategy; }
1221
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 status_t dump(int fd, const Vector<String16>& args);
1223
1224 protected:
1225
1226 EffectChain(const EffectChain&);
1227 EffectChain& operator =(const EffectChain&);
1228
1229 wp<ThreadBase> mThread; // parent mixer thread
1230 Mutex mLock; // mutex protecting effect list
1231 Vector<sp<EffectModule> > mEffects; // list of effect modules
1232 int mSessionId; // audio session ID
1233 int16_t *mInBuffer; // chain input buffer
1234 int16_t *mOutBuffer; // chain output buffer
Eric Laurentb469b942011-05-09 12:09:06 -07001235 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1236 volatile int32_t mTrackCnt; // number of tracks connected
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07001238 int mVolumeCtrlIdx; // index of insert effect having control over volume
1239 uint32_t mLeftVolume; // previous volume on left channel
1240 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07001241 uint32_t mNewLeftVolume; // new volume on left channel
1242 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07001243 uint32_t mStrategy; // strategy for this effect chain
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 };
1245
Dima Zavin799a70e2011-04-18 16:57:27 -07001246 struct AudioStreamOut {
1247 audio_hw_device_t *hwDev;
1248 audio_stream_out_t *stream;
1249
1250 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1251 hwDev(dev), stream(out) {}
1252 };
1253
1254 struct AudioStreamIn {
1255 audio_hw_device_t *hwDev;
1256 audio_stream_in_t *stream;
1257
1258 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1259 hwDev(dev), stream(in) {}
1260 };
1261
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 friend class RecordThread;
1263 friend class PlaybackThread;
1264
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 mutable Mutex mLock;
1266
1267 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1268
1269 mutable Mutex mHardwareLock;
Dima Zavin799a70e2011-04-18 16:57:27 -07001270 audio_hw_device_t* mPrimaryHardwareDev;
1271 Vector<audio_hw_device_t*> mAudioHwDevs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 mutable int mHardwareStatus;
1273
1274
1275 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavinfce7a472011-04-19 22:30:36 -07001276 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 float mMasterVolume;
1278 bool mMasterMute;
1279
1280 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
1281
1282 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
1283 volatile int32_t mNextUniqueId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284 uint32_t mMode;
1285
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286};
1287
Dima Zavin799a70e2011-04-18 16:57:27 -07001288
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289// ----------------------------------------------------------------------------
1290
1291}; // namespace android
1292
1293#endif // ANDROID_AUDIO_FLINGER_H