blob: 507c9ac841234c99359b011b914a47a6886e0545 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //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>
Eric Laurent8ac9f8d2009-12-18 05:47:48 -080023#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
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 Agopian07952722009-05-19 19:08:10 -070034#include <binder/MemoryDealer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/SortedVector.h>
36#include <utils/Vector.h>
37
38#include <hardware_legacy/AudioHardwareInterface.h>
39
40#include "AudioBufferProvider.h"
41
42namespace android {
43
44class audio_track_cblk_t;
Eric Laurent65b65452010-06-01 23:49:17 -070045class effect_param_cblk_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046class AudioMixer;
47class AudioBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -070048class AudioResampler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50
51// ----------------------------------------------------------------------------
52
53#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
54#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
55
56
57// ----------------------------------------------------------------------------
58
59static const nsecs_t kStandbyTimeInNsecs = seconds(3);
60
Eric Laurent4f0f17d2010-05-12 02:05:53 -070061class AudioFlinger : public BnAudioFlinger
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062{
63public:
64 static void instantiate();
65
66 virtual status_t dump(int fd, const Vector<String16>& args);
67
68 // IAudioFlinger interface
69 virtual sp<IAudioTrack> createTrack(
70 pid_t pid,
71 int streamType,
72 uint32_t sampleRate,
73 int format,
74 int channelCount,
75 int frameCount,
76 uint32_t flags,
77 const sp<IMemory>& sharedBuffer,
Eric Laurentddb78e72009-07-28 08:44:33 -070078 int output,
Eric Laurent65b65452010-06-01 23:49:17 -070079 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 status_t *status);
81
Eric Laurentddb78e72009-07-28 08:44:33 -070082 virtual uint32_t sampleRate(int output) const;
83 virtual int channelCount(int output) const;
84 virtual int format(int output) const;
85 virtual size_t frameCount(int output) const;
86 virtual uint32_t latency(int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88 virtual status_t setMasterVolume(float value);
89 virtual status_t setMasterMute(bool muted);
90
91 virtual float masterVolume() const;
92 virtual bool masterMute() const;
93
Eric Laurentddb78e72009-07-28 08:44:33 -070094 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 virtual status_t setStreamMute(int stream, bool muted);
96
Eric Laurentddb78e72009-07-28 08:44:33 -070097 virtual float streamVolume(int stream, int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 virtual bool streamMute(int stream) const;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 virtual status_t setMode(int mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 virtual status_t setMicMute(bool state);
103 virtual bool getMicMute() const;
104
Eric Laurent23f25cd2010-01-25 08:49:09 -0800105 virtual bool isStreamActive(int stream) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Eric Laurentddb78e72009-07-28 08:44:33 -0700107 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
108 virtual String8 getParameters(int ioHandle, const String8& keys);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurenta553c252009-07-17 12:17:14 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent47d0a922010-02-26 02:47:27 -0800113 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700114
Eric Laurentddb78e72009-07-28 08:44:33 -0700115 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700116 uint32_t *pSamplingRate,
117 uint32_t *pFormat,
118 uint32_t *pChannels,
119 uint32_t *pLatencyMs,
120 uint32_t flags);
121
Eric Laurentddb78e72009-07-28 08:44:33 -0700122 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700123
Eric Laurentddb78e72009-07-28 08:44:33 -0700124 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700125
Eric Laurentddb78e72009-07-28 08:44:33 -0700126 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700127
Eric Laurentddb78e72009-07-28 08:44:33 -0700128 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700129
Eric Laurentddb78e72009-07-28 08:44:33 -0700130 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700131 uint32_t *pSamplingRate,
132 uint32_t *pFormat,
133 uint32_t *pChannels,
134 uint32_t acoustics);
135
Eric Laurentddb78e72009-07-28 08:44:33 -0700136 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700137
Eric Laurentddb78e72009-07-28 08:44:33 -0700138 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700139
Eric Laurent415f3e22009-10-21 08:14:22 -0700140 virtual status_t setVoiceVolume(float volume);
141
Eric Laurent0986e792010-01-19 17:37:09 -0800142 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
143
Eric Laurent65b65452010-06-01 23:49:17 -0700144 virtual int newAudioSessionId();
145
146 virtual status_t loadEffectLibrary(const char *libPath, int *handle);
147
148 virtual status_t unloadEffectLibrary(int handle);
149
150 virtual status_t queryNumberEffects(uint32_t *numEffects);
151
Eric Laurent53334cd2010-06-23 17:38:20 -0700152 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -0700153
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,
160 int output,
161 int sessionId,
162 status_t *status,
163 int *id,
164 int *enabled);
165
Eric Laurent53334cd2010-06-23 17:38:20 -0700166 status_t registerEffectResource_l(effect_descriptor_t *desc);
167 void unregisterEffectResource_l(effect_descriptor_t *desc);
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 enum hardware_call_state {
170 AUDIO_HW_IDLE = 0,
171 AUDIO_HW_INIT,
172 AUDIO_HW_OUTPUT_OPEN,
173 AUDIO_HW_OUTPUT_CLOSE,
174 AUDIO_HW_INPUT_OPEN,
175 AUDIO_HW_INPUT_CLOSE,
176 AUDIO_HW_STANDBY,
177 AUDIO_HW_SET_MASTER_VOLUME,
178 AUDIO_HW_GET_ROUTING,
179 AUDIO_HW_SET_ROUTING,
180 AUDIO_HW_GET_MODE,
181 AUDIO_HW_SET_MODE,
182 AUDIO_HW_GET_MIC_MUTE,
183 AUDIO_HW_SET_MIC_MUTE,
184 AUDIO_SET_VOICE_VOLUME,
185 AUDIO_SET_PARAMETER,
186 };
187
188 // record interface
189 virtual sp<IAudioRecord> openRecord(
190 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700191 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 uint32_t sampleRate,
193 int format,
194 int channelCount,
195 int frameCount,
196 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700197 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 status_t *status);
199
200 virtual status_t onTransact(
201 uint32_t code,
202 const Parcel& data,
203 Parcel* reply,
204 uint32_t flags);
205
Eric Laurent53334cd2010-06-23 17:38:20 -0700206 uint32_t getMode() { return mMode; }
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208private:
209 AudioFlinger();
210 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
213 // Internal dump utilites.
214 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
215 status_t dumpClients(int fd, const Vector<String16>& args);
216 status_t dumpInternals(int fd, const Vector<String16>& args);
217
218 // --- Client ---
219 class Client : public RefBase {
220 public:
221 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
222 virtual ~Client();
223 const sp<MemoryDealer>& heap() const;
224 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700225 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 private:
228 Client(const Client&);
229 Client& operator = (const Client&);
230 sp<AudioFlinger> mAudioFlinger;
231 sp<MemoryDealer> mMemoryDealer;
232 pid_t mPid;
233 };
234
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700235 // --- Notification Client ---
236 class NotificationClient : public IBinder::DeathRecipient {
237 public:
238 NotificationClient(const sp<AudioFlinger>& audioFlinger,
239 const sp<IAudioFlingerClient>& client,
240 pid_t pid);
241 virtual ~NotificationClient();
242
243 sp<IAudioFlingerClient> client() { return mClient; }
244
245 // IBinder::DeathRecipient
246 virtual void binderDied(const wp<IBinder>& who);
247
248 private:
249 NotificationClient(const NotificationClient&);
250 NotificationClient& operator = (const NotificationClient&);
251
252 sp<AudioFlinger> mAudioFlinger;
253 pid_t mPid;
254 sp<IAudioFlingerClient> mClient;
255 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 class TrackHandle;
258 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700259 class RecordThread;
260 class PlaybackThread;
261 class MixerThread;
262 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800263 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700264 class Track;
265 class RecordTrack;
Eric Laurent65b65452010-06-01 23:49:17 -0700266 class EffectModule;
267 class EffectHandle;
268 class EffectChain;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
Eric Laurenta553c252009-07-17 12:17:14 -0700270 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800272 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700273 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
Eric Laurent3fdb1262009-11-07 00:01:32 -0800275 status_t dumpBase(int fd, const Vector<String16>& args);
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 // base for record and playback
278 class TrackBase : public AudioBufferProvider, public RefBase {
279
280 public:
281 enum track_state {
282 IDLE,
283 TERMINATED,
284 STOPPED,
285 RESUMING,
286 ACTIVE,
287 PAUSING,
288 PAUSED
289 };
290
291 enum track_flags {
292 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
293 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
294 // The upper 16 bits are used for track-specific flags.
295 };
296
Eric Laurenta553c252009-07-17 12:17:14 -0700297 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 uint32_t sampleRate,
300 int format,
301 int channelCount,
302 int frameCount,
303 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700304 const sp<IMemory>& sharedBuffer,
305 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 ~TrackBase();
307
308 virtual status_t start() = 0;
309 virtual void stop() = 0;
310 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700311 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700312 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700315 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700317 friend class PlaybackThread;
318 friend class RecordThread;
319 friend class MixerThread;
320 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
322 TrackBase(const TrackBase&);
323 TrackBase& operator = (const TrackBase&);
324
325 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
326 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 int format() const {
329 return mFormat;
330 }
331
332 int channelCount() const ;
333
334 int sampleRate() const;
335
336 void* getBuffer(uint32_t offset, uint32_t frames) const;
337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 bool isStopped() const {
339 return mState == STOPPED;
340 }
341
342 bool isTerminated() const {
343 return mState == TERMINATED;
344 }
345
346 bool step();
347 void reset();
348
Eric Laurenta553c252009-07-17 12:17:14 -0700349 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 sp<Client> mClient;
351 sp<IMemory> mCblkMemory;
352 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 void* mBuffer;
354 void* mBufferEnd;
355 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 // we don't really need a lock for these
357 int mState;
358 int mClientTid;
359 uint8_t mFormat;
360 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700361 int mSessionId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 };
363
Eric Laurenta553c252009-07-17 12:17:14 -0700364 class ConfigEvent {
365 public:
366 ConfigEvent() : mEvent(0), mParam(0) {}
367
368 int mEvent;
369 int mParam;
370 };
371
372 uint32_t sampleRate() const;
373 int channelCount() const;
374 int format() const;
375 size_t frameCount() const;
376 void wakeUp() { mWaitWorkCV.broadcast(); }
377 void exit();
378 virtual bool checkForNewParameters_l() = 0;
379 virtual status_t setParameters(const String8& keyValuePairs);
380 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700381 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700382 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700383 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700384 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800385 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800386 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700387
Eric Laurent2c817f52009-07-23 13:17:39 -0700388 mutable Mutex mLock;
389
Eric Laurenta553c252009-07-17 12:17:14 -0700390 protected:
391
392 friend class Track;
393 friend class TrackBase;
394 friend class PlaybackThread;
395 friend class MixerThread;
396 friend class DirectOutputThread;
397 friend class DuplicatingThread;
398 friend class RecordThread;
399 friend class RecordTrack;
400
Eric Laurenta553c252009-07-17 12:17:14 -0700401 Condition mWaitWorkCV;
402 sp<AudioFlinger> mAudioFlinger;
403 uint32_t mSampleRate;
404 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700405 uint32_t mChannels;
406 uint16_t mChannelCount;
407 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700408 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700409 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700410 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700411 status_t mParamStatus;
412 Vector<ConfigEvent *> mConfigEvents;
413 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800414 int mId;
415 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700416 };
417
418 // --- PlaybackThread ---
419 class PlaybackThread : public ThreadBase {
420 public:
421
422 enum type {
423 MIXER,
424 DIRECT,
425 DUPLICATING
426 };
427
Eric Laurent059b4be2009-11-09 23:32:22 -0800428 enum mixer_state {
429 MIXER_IDLE,
430 MIXER_TRACKS_ENABLED,
431 MIXER_TRACKS_READY
432 };
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 // playback track
435 class Track : public TrackBase {
436 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700437 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 const sp<Client>& client,
439 int streamType,
440 uint32_t sampleRate,
441 int format,
442 int channelCount,
443 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700444 const sp<IMemory>& sharedBuffer,
445 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 ~Track();
447
448 void dump(char* buffer, size_t size);
449 virtual status_t start();
450 virtual void stop();
451 void pause();
452
453 void flush();
454 void destroy();
455 void mute(bool);
456 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700457 int name() const {
458 return mName;
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
Eric Laurent4bc035a2009-05-22 09:18:15 -0700461 int type() const {
462 return mStreamType;
463 }
Eric Laurent65b65452010-06-01 23:49:17 -0700464 status_t attachAuxEffect(int EffectId);
465 void setAuxBuffer(int EffectId, int32_t *buffer);
466 int32_t *auxBuffer() { return mAuxBuffer; }
467 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
468 int16_t *mainBuffer() { return mMainBuffer; }
469 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700470
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700473 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700475 friend class TrackHandle;
476 friend class PlaybackThread;
477 friend class MixerThread;
478 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479
480 Track(const Track&);
481 Track& operator = (const Track&);
482
483 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700484 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 bool isPausing() const {
486 return mState == PAUSING;
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 bool isPaused() const {
489 return mState == PAUSED;
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 void setPaused() { mState = PAUSED; }
493 void reset();
494
Eric Laurent49f02be2009-11-19 09:00:56 -0800495 bool isOutputTrack() const {
496 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
497 }
498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 // we don't really need a lock for these
500 float mVolume[2];
501 volatile bool mMute;
502 // FILLED state is used for suppressing volume ramp at begin of playing
503 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
504 mutable uint8_t mFillingUpStatus;
505 int8_t mRetryCount;
506 sp<IMemory> mSharedBuffer;
507 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700508 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700509 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700510 int16_t *mMainBuffer;
511 int32_t *mAuxBuffer;
512 int mAuxEffectId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }; // end of Track
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515
516 // playback track
517 class OutputTrack : public Track {
518 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 class Buffer: public AudioBufferProvider::Buffer {
521 public:
522 int16_t *mBuffer;
523 };
Eric Laurenta553c252009-07-17 12:17:14 -0700524
525 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800526 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 uint32_t sampleRate,
528 int format,
529 int channelCount,
530 int frameCount);
531 ~OutputTrack();
532
533 virtual status_t start();
534 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700535 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700537 bool isActive() { return mActive; }
538 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539
540 private:
541
Eric Laurenta553c252009-07-17 12:17:14 -0700542 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700544
545 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800546 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 Vector < Buffer* > mBufferQueue;
549 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700550 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800551 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700552 }; // end of OutputTrack
553
Eric Laurent65b65452010-06-01 23:49:17 -0700554 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700555 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
557 virtual status_t dump(int fd, const Vector<String16>& args);
558
559 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 virtual status_t readyToRun();
561 virtual void onFirstRef();
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 virtual uint32_t latency() const;
564
565 virtual status_t setMasterVolume(float value);
566 virtual status_t setMasterMute(bool muted);
567
568 virtual float masterVolume() const;
569 virtual bool masterMute() const;
570
571 virtual status_t setStreamVolume(int stream, float value);
572 virtual status_t setStreamMute(int stream, bool muted);
573
574 virtual float streamVolume(int stream) const;
575 virtual bool streamMute(int stream) const;
576
Eric Laurent23f25cd2010-01-25 08:49:09 -0800577 bool isStreamActive(int stream) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700578
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700579 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 const sp<AudioFlinger::Client>& client,
581 int streamType,
582 uint32_t sampleRate,
583 int format,
584 int channelCount,
585 int frameCount,
586 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700587 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700589
590 AudioStreamOut* getOutput() { return mOutput; }
591
592 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700593 void suspend() { mSuspended++; }
594 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800595 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700596 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700597 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800598 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700599 int16_t *mixBuffer() { return mMixBuffer; };
600
601 sp<EffectHandle> createEffect_l(
602 const sp<AudioFlinger::Client>& client,
603 const sp<IEffectClient>& effectClient,
604 int32_t priority,
605 int sessionId,
606 effect_descriptor_t *desc,
607 int *enabled,
608 status_t *status);
Eric Laurent53334cd2010-06-23 17:38:20 -0700609 void disconnectEffect(const sp< EffectModule>& effect,
610 const wp<EffectHandle>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -0700611
612 bool hasAudioSession(int sessionId);
613 sp<EffectChain> getEffectChain(int sessionId);
614 sp<EffectChain> getEffectChain_l(int sessionId);
615 status_t addEffectChain_l(const sp<EffectChain>& chain);
616 size_t removeEffectChain_l(const sp<EffectChain>& chain);
617 void lockEffectChains_l();
618 void unlockEffectChains();
619
620 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
621 void detachAuxEffect_l(int effectId);
622 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId);
623 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId);
Eric Laurent53334cd2010-06-23 17:38:20 -0700624 void setMode(uint32_t mode);
Eric Laurenta553c252009-07-17 12:17:14 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 struct stream_type_t {
627 stream_type_t()
628 : volume(1.0f),
629 mute(false)
630 {
631 }
632 float volume;
633 bool mute;
634 };
635
Eric Laurent2c817f52009-07-23 13:17:39 -0700636 protected:
637 int mType;
638 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700639 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700640 int mBytesWritten;
641 bool mMasterMute;
642 SortedVector< wp<Track> > mActiveTracks;
643
Eric Laurent62443f52009-10-05 20:29:18 -0700644 virtual int getTrackName_l() = 0;
645 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800646 virtual uint32_t activeSleepTimeUs() = 0;
647 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 private:
650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700652 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 friend class Track;
654 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700655 friend class MixerThread;
656 friend class DirectOutputThread;
657 friend class DuplicatingThread;
658
659 PlaybackThread(const Client&);
660 PlaybackThread& operator = (const PlaybackThread&);
661
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700662 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700663 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700664
Eric Laurenta553c252009-07-17 12:17:14 -0700665 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666
Eric Laurent65b65452010-06-01 23:49:17 -0700667 uint32_t device() { return mDevice; }
668
Eric Laurenta553c252009-07-17 12:17:14 -0700669 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700671 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700674 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
675 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 nsecs_t mLastWriteTime;
679 int mNumWrites;
680 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700682 Vector< sp<EffectChain> > mEffectChains;
683 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 };
685
Eric Laurenta553c252009-07-17 12:17:14 -0700686 class MixerThread : public PlaybackThread {
687 public:
Eric Laurent65b65452010-06-01 23:49:17 -0700688 MixerThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700689 virtual ~MixerThread();
690
691 // Thread virtuals
692 virtual bool threadLoop();
693
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700694 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700695 virtual bool checkForNewParameters_l();
696 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
697
698 protected:
Eric Laurent059b4be2009-11-09 23:32:22 -0800699 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700700 virtual int getTrackName_l();
701 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800702 virtual uint32_t activeSleepTimeUs();
703 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700704
705 AudioMixer* mAudioMixer;
706 };
707
708 class DirectOutputThread : public PlaybackThread {
709 public:
710
Eric Laurent65b65452010-06-01 23:49:17 -0700711 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700712 ~DirectOutputThread();
713
714 // Thread virtuals
715 virtual bool threadLoop();
716
Eric Laurent62443f52009-10-05 20:29:18 -0700717 virtual bool checkForNewParameters_l();
718
719 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700720 virtual int getTrackName_l();
721 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800722 virtual uint32_t activeSleepTimeUs();
723 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700724
725 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700726 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
727
728 float mLeftVolFloat;
729 float mRightVolFloat;
730 uint16_t mLeftVolShort;
731 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700732 };
733
734 class DuplicatingThread : public MixerThread {
735 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800736 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700737 ~DuplicatingThread();
738
739 // Thread virtuals
740 virtual bool threadLoop();
741 void addOutputTrack(MixerThread* thread);
742 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800743 uint32_t waitTimeMs() { return mWaitTimeMs; }
744 protected:
745 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700746
747 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800748 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
749 void updateWaitTime();
750
Eric Laurenta553c252009-07-17 12:17:14 -0700751 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800752 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700753 };
754
Eric Laurentddb78e72009-07-28 08:44:33 -0700755 PlaybackThread *checkPlaybackThread_l(int output) const;
756 MixerThread *checkMixerThread_l(int output) const;
757 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700758 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800759 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700760
Eric Laurent65b65452010-06-01 23:49:17 -0700761 int nextUniqueId();
762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 friend class AudioBuffer;
764
765 class TrackHandle : public android::BnAudioTrack {
766 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700767 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 virtual ~TrackHandle();
769 virtual status_t start();
770 virtual void stop();
771 virtual void flush();
772 virtual void mute(bool);
773 virtual void pause();
774 virtual void setVolume(float left, float right);
775 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700776 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 virtual status_t onTransact(
778 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
779 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700780 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 };
782
783 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700784 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785
786
Eric Laurentb9481d82009-09-17 05:12:56 -0700787 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700788 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789
790
Eric Laurenta553c252009-07-17 12:17:14 -0700791 // record thread
792 class RecordThread : public ThreadBase, public AudioBufferProvider
793 {
794 public:
795
796 // record track
797 class RecordTrack : public TrackBase {
798 public:
799 RecordTrack(const wp<ThreadBase>& thread,
800 const sp<Client>& client,
801 uint32_t sampleRate,
802 int format,
803 int channelCount,
804 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700805 uint32_t flags,
806 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700807 ~RecordTrack();
808
809 virtual status_t start();
810 virtual void stop();
811
812 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
813 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
814
Eric Laurent3fdb1262009-11-07 00:01:32 -0800815 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700816 private:
817 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700818 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700819
820 RecordTrack(const RecordTrack&);
821 RecordTrack& operator = (const RecordTrack&);
822
823 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
824
825 bool mOverflow;
826 };
827
828
829 RecordThread(const sp<AudioFlinger>& audioFlinger,
830 AudioStreamIn *input,
831 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800832 uint32_t channels,
833 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700834 ~RecordThread();
835
836 virtual bool threadLoop();
837 virtual status_t readyToRun() { return NO_ERROR; }
838 virtual void onFirstRef();
839
840 status_t start(RecordTrack* recordTrack);
841 void stop(RecordTrack* recordTrack);
842 status_t dump(int fd, const Vector<String16>& args);
843 AudioStreamIn* getInput() { return mInput; }
844
845 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
846 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
847 virtual bool checkForNewParameters_l();
848 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700849 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700850 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800851 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700852
853 private:
854 RecordThread();
855 AudioStreamIn *mInput;
856 sp<RecordTrack> mActiveTrack;
857 Condition mStartStopCond;
858 AudioResampler *mResampler;
859 int32_t *mRsmpOutBuffer;
860 int16_t *mRsmpInBuffer;
861 size_t mRsmpInIndex;
862 size_t mInputBytes;
863 int mReqChannelCount;
864 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800865 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700866 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867
868 class RecordHandle : public android::BnAudioRecord {
869 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700870 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 virtual ~RecordHandle();
872 virtual status_t start();
873 virtual void stop();
874 virtual sp<IMemory> getCblk() const;
875 virtual status_t onTransact(
876 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
877 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700878 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 };
880
Eric Laurent65b65452010-06-01 23:49:17 -0700881 //--- Audio Effect Management
882
883 // EffectModule and EffectChain classes both have their own mutex to protect
884 // state changes or resource modifications. Always respect the following order
885 // if multiple mutexes must be acquired to avoid cross deadlock:
886 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
887
888 // The EffectModule class is a wrapper object controlling the effect engine implementation
889 // in the effect library. It prevents concurrent calls to process() and command() functions
890 // from different client threads. It keeps a list of EffectHandle objects corresponding
891 // to all client applications using this effect and notifies applications of effect state,
892 // control or parameter changes. It manages the activation state machine to send appropriate
893 // reset, enable, disable commands to effect engine and provide volume
894 // ramping when effects are activated/deactivated.
895 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
896 // the attached track(s) to accumulate their auxiliary channel.
897 class EffectModule: public RefBase {
898 public:
899 EffectModule(const wp<ThreadBase>& wThread,
900 const wp<AudioFlinger::EffectChain>& chain,
901 effect_descriptor_t *desc,
902 int id,
903 int sessionId);
904 ~EffectModule();
905
906 enum effect_state {
907 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700908 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700909 STARTING,
910 ACTIVE,
911 STOPPING,
912 STOPPED
913 };
914
915 int id() { return mId; }
916 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -0700917 void updateState();
Eric Laurent65b65452010-06-01 23:49:17 -0700918 status_t command(int cmdCode, int cmdSize, void *pCmdData, int *replySize, void *pReplyData);
919
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700920 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700921 status_t configure();
922 status_t init();
923 uint32_t state() {
924 return mState;
925 }
926 uint32_t status() {
927 return mStatus;
928 }
929 status_t setEnabled(bool enabled);
930 bool isEnabled();
931
932 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
933 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
934 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
935 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
936
937 status_t addHandle(sp<EffectHandle>& handle);
938 void disconnect(const wp<EffectHandle>& handle);
939 size_t removeHandle (const wp<EffectHandle>& handle);
940
941 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700942 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700943
944 status_t setDevice(uint32_t device);
945 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700946 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700947
948 status_t dump(int fd, const Vector<String16>& args);
949
950 protected:
951
Eric Laurent7d850f22010-07-09 13:34:17 -0700952 // Maximum time allocated to effect engines to complete the turn off sequence
953 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
954
Eric Laurent65b65452010-06-01 23:49:17 -0700955 EffectModule(const EffectModule&);
956 EffectModule& operator = (const EffectModule&);
957
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700958 status_t start_l();
959 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700960
Eric Laurent53334cd2010-06-23 17:38:20 -0700961 // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified
962 static const uint32_t sDeviceConvTable[];
963 static uint32_t deviceAudioSystemToEffectApi(uint32_t device);
964
965 // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified
966 static const uint32_t sModeConvTable[];
967 static int modeAudioSystemToEffectApi(uint32_t mode);
968
Eric Laurent65b65452010-06-01 23:49:17 -0700969 Mutex mLock; // mutex for process, commands and handles list protection
970 wp<ThreadBase> mThread; // parent thread
971 wp<EffectChain> mChain; // parent effect chain
972 int mId; // this instance unique ID
973 int mSessionId; // audio session ID
974 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
975 effect_config_t mConfig; // input and output audio configuration
976 effect_interface_t mEffectInterface; // Effect module C API
977 status_t mStatus; // initialization status
978 uint32_t mState; // current activation state (effect_state)
979 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -0700980 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
981 // sending disable command.
982 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -0700983 };
984
985 // The EffectHandle class implements the IEffect interface. It provides resources
986 // to receive parameter updates, keeps track of effect control
987 // ownership and state and has a pointer to the EffectModule object it is controlling.
988 // There is one EffectHandle object for each application controlling (or using)
989 // an effect module.
990 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
991 class EffectHandle: public android::BnEffect {
992 public:
993
994 EffectHandle(const sp<EffectModule>& effect,
995 const sp<AudioFlinger::Client>& client,
996 const sp<IEffectClient>& effectClient,
997 int32_t priority);
998 virtual ~EffectHandle();
999
1000 // IEffect
1001 virtual status_t enable();
1002 virtual status_t disable();
1003 virtual status_t command(int cmdCode, int cmdSize, void *pCmdData, int *replySize, void *pReplyData);
1004 virtual void disconnect();
1005 virtual sp<IMemory> getCblk() const;
1006 virtual status_t onTransact(uint32_t code, const Parcel& data,
1007 Parcel* reply, uint32_t flags);
1008
1009
1010 // Give or take control of effect module
1011 void setControl(bool hasControl, bool signal);
1012 void commandExecuted(int cmdCode, int cmdSize, void *pCmdData, int replySize, void *pReplyData);
1013 void setEnabled(bool enabled);
1014
1015 // Getters
1016 int id() { return mEffect->id(); }
1017 int priority() { return mPriority; }
1018 bool hasControl() { return mHasControl; }
1019 sp<EffectModule> effect() { return mEffect; }
1020
1021 void dump(char* buffer, size_t size);
1022
1023 protected:
1024
1025 EffectHandle(const EffectHandle&);
1026 EffectHandle& operator =(const EffectHandle&);
1027
1028 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1029 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1030 sp<Client> mClient; // client for shared memory allocation
1031 sp<IMemory> mCblkMemory; // shared memory for control block
1032 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1033 uint8_t* mBuffer; // pointer to parameter area in shared memory
1034 int mPriority; // client application priority to control the effect
1035 bool mHasControl; // true if this handle is controlling the effect
1036 };
1037
1038 // the EffectChain class represents a group of effects associated to one audio session.
1039 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1040 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1041 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1042 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1043 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1044 // input buffer used by the track as accumulation buffer.
1045 class EffectChain: public RefBase {
1046 public:
1047 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1048 ~EffectChain();
1049
1050 void process_l();
1051
1052 void lock() {
1053 mLock.lock();
1054 }
1055 void unlock() {
1056 mLock.unlock();
1057 }
1058
1059 status_t addEffect(sp<EffectModule>& handle);
1060 size_t removeEffect(const sp<EffectModule>& handle);
1061
1062 int sessionId() {
1063 return mSessionId;
1064 }
1065 sp<EffectModule> getEffectFromDesc(effect_descriptor_t *descriptor);
1066 sp<EffectModule> getEffectFromId(int id);
1067 sp<EffectModule> getVolumeController();
1068 bool setVolume(uint32_t *left, uint32_t *right);
1069 void setDevice(uint32_t device);
Eric Laurent53334cd2010-06-23 17:38:20 -07001070 void setMode(uint32_t mode);
1071
Eric Laurent65b65452010-06-01 23:49:17 -07001072
1073 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1074 mInBuffer = buffer;
1075 mOwnInBuffer = ownsBuffer;
1076 }
1077 int16_t *inBuffer() {
1078 return mInBuffer;
1079 }
1080 void setOutBuffer(int16_t *buffer) {
1081 mOutBuffer = buffer;
1082 }
1083 int16_t *outBuffer() {
1084 return mOutBuffer;
1085 }
1086
1087 void startTrack() {mActiveTrackCnt++;}
1088 void stopTrack() {mActiveTrackCnt--;}
1089 int activeTracks() { return mActiveTrackCnt;}
1090
1091 status_t dump(int fd, const Vector<String16>& args);
1092
1093 protected:
1094
1095 EffectChain(const EffectChain&);
1096 EffectChain& operator =(const EffectChain&);
1097
1098 wp<ThreadBase> mThread; // parent mixer thread
1099 Mutex mLock; // mutex protecting effect list
1100 Vector<sp<EffectModule> > mEffects; // list of effect modules
1101 int mSessionId; // audio session ID
1102 int16_t *mInBuffer; // chain input buffer
1103 int16_t *mOutBuffer; // chain output buffer
1104 int mVolumeCtrlIdx; // index of insert effect having control over volume
1105 int mActiveTrackCnt; // number of active tracks connected
1106 bool mOwnInBuffer; // true if the chain owns its input buffer
1107 };
1108
Eric Laurenta553c252009-07-17 12:17:14 -07001109 friend class RecordThread;
1110 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001113 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1116
Eric Laurenta553c252009-07-17 12:17:14 -07001117 mutable Mutex mHardwareLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 AudioHardwareInterface* mAudioHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001120
Eric Laurenta553c252009-07-17 12:17:14 -07001121
Eric Laurentddb78e72009-07-28 08:44:33 -07001122 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001123 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
1124 float mMasterVolume;
1125 bool mMasterMute;
1126
Eric Laurentddb78e72009-07-28 08:44:33 -07001127 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001128
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001129 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001130 volatile int32_t mNextUniqueId;
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001131#ifdef LVMX
1132 int mLifeVibesClientPid;
1133#endif
Eric Laurent53334cd2010-06-23 17:38:20 -07001134 uint32_t mMode;
1135
1136 // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
1137 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
1138 // Maximum memory allocated to audio effects in KB
1139 static const uint32_t MAX_EFFECTS_MEMORY = 512;
1140 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
1141 uint32_t mTotalEffectsMemory; // current memory used by effects
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142};
1143
1144// ----------------------------------------------------------------------------
1145
1146}; // namespace android
1147
1148#endif // ANDROID_AUDIO_FLINGER_H