blob: e543334cb714bc0746013d37924c302b28d3485c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Laurent7e2aad12009-12-18 05:47:48 -080023#include <limits.h>
The Android Open Source Projectedbf3b62009-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 Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/MemoryDealer.h>
The Android Open Source Projectedbf3b62009-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 Laurenteafff452010-06-01 23:49:17 -070045class effect_param_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046class AudioMixer;
47class AudioBuffer;
Eric Laurent9d91ad52009-07-17 12:17:14 -070048class AudioResampler;
The Android Open Source Projectedbf3b62009-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 Laurentd878cd82010-05-12 02:05:53 -070061class AudioFlinger : public BnAudioFlinger
The Android Open Source Projectedbf3b62009-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 Laurente0e9ecc2009-07-28 08:44:33 -070078 int output,
Eric Laurenteafff452010-06-01 23:49:17 -070079 int *sessionId,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 status_t *status);
81
Eric Laurente0e9ecc2009-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 Projectedbf3b62009-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 Laurente0e9ecc2009-07-28 08:44:33 -070094 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 virtual status_t setStreamMute(int stream, bool muted);
96
Eric Laurente0e9ecc2009-07-28 08:44:33 -070097 virtual float streamVolume(int stream, int output) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 virtual bool streamMute(int stream) const;
99
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 virtual status_t setMode(int mode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 virtual status_t setMicMute(bool state);
103 virtual bool getMicMute() const;
104
Eric Laurent43c0b0a2010-01-25 08:49:09 -0800105 virtual bool isStreamActive(int stream) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
Eric Laurente0e9ecc2009-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 Projectedbf3b62009-03-03 19:31:44 -0800109
110 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent134ccbd2010-02-26 02:47:27 -0800113 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700114
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700115 virtual int openOutput(uint32_t *pDevices,
Eric Laurent9d91ad52009-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 Laurente0e9ecc2009-07-28 08:44:33 -0700122 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700123
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700124 virtual status_t closeOutput(int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700125
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700126 virtual status_t suspendOutput(int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700127
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700128 virtual status_t restoreOutput(int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700129
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700130 virtual int openInput(uint32_t *pDevices,
Eric Laurent9d91ad52009-07-17 12:17:14 -0700131 uint32_t *pSamplingRate,
132 uint32_t *pFormat,
133 uint32_t *pChannels,
134 uint32_t acoustics);
135
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700136 virtual status_t closeInput(int input);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700137
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700138 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700139
Eric Laurent63da2b62009-10-21 08:14:22 -0700140 virtual status_t setVoiceVolume(float volume);
141
Eric Laurente9ed2722010-01-19 17:37:09 -0800142 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
143
Eric Laurenteafff452010-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
152 virtual status_t queryNextEffect(effect_descriptor_t *descriptor);
153
154 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
155
156 virtual sp<IEffect> createEffect(pid_t pid,
157 effect_descriptor_t *pDesc,
158 const sp<IEffectClient>& effectClient,
159 int32_t priority,
160 int output,
161 int sessionId,
162 status_t *status,
163 int *id,
164 int *enabled);
165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 enum hardware_call_state {
167 AUDIO_HW_IDLE = 0,
168 AUDIO_HW_INIT,
169 AUDIO_HW_OUTPUT_OPEN,
170 AUDIO_HW_OUTPUT_CLOSE,
171 AUDIO_HW_INPUT_OPEN,
172 AUDIO_HW_INPUT_CLOSE,
173 AUDIO_HW_STANDBY,
174 AUDIO_HW_SET_MASTER_VOLUME,
175 AUDIO_HW_GET_ROUTING,
176 AUDIO_HW_SET_ROUTING,
177 AUDIO_HW_GET_MODE,
178 AUDIO_HW_SET_MODE,
179 AUDIO_HW_GET_MIC_MUTE,
180 AUDIO_HW_SET_MIC_MUTE,
181 AUDIO_SET_VOICE_VOLUME,
182 AUDIO_SET_PARAMETER,
183 };
184
185 // record interface
186 virtual sp<IAudioRecord> openRecord(
187 pid_t pid,
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700188 int input,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 uint32_t sampleRate,
190 int format,
191 int channelCount,
192 int frameCount,
193 uint32_t flags,
Eric Laurenteafff452010-06-01 23:49:17 -0700194 int *sessionId,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 status_t *status);
196
197 virtual status_t onTransact(
198 uint32_t code,
199 const Parcel& data,
200 Parcel* reply,
201 uint32_t flags);
202
203private:
204 AudioFlinger();
205 virtual ~AudioFlinger();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
208 // Internal dump utilites.
209 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
210 status_t dumpClients(int fd, const Vector<String16>& args);
211 status_t dumpInternals(int fd, const Vector<String16>& args);
212
213 // --- Client ---
214 class Client : public RefBase {
215 public:
216 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
217 virtual ~Client();
218 const sp<MemoryDealer>& heap() const;
219 pid_t pid() const { return mPid; }
Eric Laurent0f8ab672009-09-17 05:12:56 -0700220 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 private:
223 Client(const Client&);
224 Client& operator = (const Client&);
225 sp<AudioFlinger> mAudioFlinger;
226 sp<MemoryDealer> mMemoryDealer;
227 pid_t mPid;
228 };
229
Eric Laurentd878cd82010-05-12 02:05:53 -0700230 // --- Notification Client ---
231 class NotificationClient : public IBinder::DeathRecipient {
232 public:
233 NotificationClient(const sp<AudioFlinger>& audioFlinger,
234 const sp<IAudioFlingerClient>& client,
235 pid_t pid);
236 virtual ~NotificationClient();
237
238 sp<IAudioFlingerClient> client() { return mClient; }
239
240 // IBinder::DeathRecipient
241 virtual void binderDied(const wp<IBinder>& who);
242
243 private:
244 NotificationClient(const NotificationClient&);
245 NotificationClient& operator = (const NotificationClient&);
246
247 sp<AudioFlinger> mAudioFlinger;
248 pid_t mPid;
249 sp<IAudioFlingerClient> mClient;
250 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
252 class TrackHandle;
253 class RecordHandle;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700254 class RecordThread;
255 class PlaybackThread;
256 class MixerThread;
257 class DirectOutputThread;
Eric Laurent7e2aad12009-12-18 05:47:48 -0800258 class DuplicatingThread;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700259 class Track;
260 class RecordTrack;
Eric Laurenteafff452010-06-01 23:49:17 -0700261 class EffectModule;
262 class EffectHandle;
263 class EffectChain;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264
Eric Laurent9d91ad52009-07-17 12:17:14 -0700265 class ThreadBase : public Thread {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 public:
Eric Laurent09b4ba82009-11-19 09:00:56 -0800267 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700268 virtual ~ThreadBase();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
Eric Laurentee47d432009-11-07 00:01:32 -0800270 status_t dumpBase(int fd, const Vector<String16>& args);
271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 // base for record and playback
273 class TrackBase : public AudioBufferProvider, public RefBase {
274
275 public:
276 enum track_state {
277 IDLE,
278 TERMINATED,
279 STOPPED,
280 RESUMING,
281 ACTIVE,
282 PAUSING,
283 PAUSED
284 };
285
286 enum track_flags {
287 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
288 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
289 // The upper 16 bits are used for track-specific flags.
290 };
291
Eric Laurent9d91ad52009-07-17 12:17:14 -0700292 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294 uint32_t sampleRate,
295 int format,
296 int channelCount,
297 int frameCount,
298 uint32_t flags,
Eric Laurenteafff452010-06-01 23:49:17 -0700299 const sp<IMemory>& sharedBuffer,
300 int sessionId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 ~TrackBase();
302
303 virtual status_t start() = 0;
304 virtual void stop() = 0;
305 sp<IMemory> getCblk() const;
Eric Laurentf5aba822009-08-10 23:22:32 -0700306 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurenteafff452010-06-01 23:49:17 -0700307 int sessionId() { return mSessionId; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
309 protected:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700310 friend class ThreadBase;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 friend class RecordHandle;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700312 friend class PlaybackThread;
313 friend class RecordThread;
314 friend class MixerThread;
315 friend class DirectOutputThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316
317 TrackBase(const TrackBase&);
318 TrackBase& operator = (const TrackBase&);
319
320 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
321 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 int format() const {
324 return mFormat;
325 }
326
327 int channelCount() const ;
328
329 int sampleRate() const;
330
331 void* getBuffer(uint32_t offset, uint32_t frames) const;
332
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 bool isStopped() const {
334 return mState == STOPPED;
335 }
336
337 bool isTerminated() const {
338 return mState == TERMINATED;
339 }
340
341 bool step();
342 void reset();
343
Eric Laurent9d91ad52009-07-17 12:17:14 -0700344 wp<ThreadBase> mThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 sp<Client> mClient;
346 sp<IMemory> mCblkMemory;
347 audio_track_cblk_t* mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 void* mBuffer;
349 void* mBufferEnd;
350 uint32_t mFrameCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 // we don't really need a lock for these
352 int mState;
353 int mClientTid;
354 uint8_t mFormat;
355 uint32_t mFlags;
Eric Laurenteafff452010-06-01 23:49:17 -0700356 int mSessionId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 };
358
Eric Laurent9d91ad52009-07-17 12:17:14 -0700359 class ConfigEvent {
360 public:
361 ConfigEvent() : mEvent(0), mParam(0) {}
362
363 int mEvent;
364 int mParam;
365 };
366
367 uint32_t sampleRate() const;
368 int channelCount() const;
369 int format() const;
370 size_t frameCount() const;
371 void wakeUp() { mWaitWorkCV.broadcast(); }
372 void exit();
373 virtual bool checkForNewParameters_l() = 0;
374 virtual status_t setParameters(const String8& keyValuePairs);
375 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurent4edfe752010-05-14 03:26:45 -0700376 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700377 void sendConfigEvent(int event, int param = 0);
Eric Laurent3464c012009-08-04 09:45:33 -0700378 void sendConfigEvent_l(int event, int param = 0);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700379 void processConfigEvents();
Eric Laurent09b4ba82009-11-19 09:00:56 -0800380 int id() const { return mId;}
Eric Laurent7e2aad12009-12-18 05:47:48 -0800381 bool standby() { return mStandby; }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700382
Eric Laurent9395d9b2009-07-23 13:17:39 -0700383 mutable Mutex mLock;
384
Eric Laurent9d91ad52009-07-17 12:17:14 -0700385 protected:
386
387 friend class Track;
388 friend class TrackBase;
389 friend class PlaybackThread;
390 friend class MixerThread;
391 friend class DirectOutputThread;
392 friend class DuplicatingThread;
393 friend class RecordThread;
394 friend class RecordTrack;
395
Eric Laurent9d91ad52009-07-17 12:17:14 -0700396 Condition mWaitWorkCV;
397 sp<AudioFlinger> mAudioFlinger;
398 uint32_t mSampleRate;
399 size_t mFrameCount;
Eric Laurente1512162010-05-14 05:45:46 -0700400 uint32_t mChannels;
401 uint16_t mChannelCount;
402 uint16_t mFrameSize;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700403 int mFormat;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700404 Condition mParamCond;
Eric Laurent3464c012009-08-04 09:45:33 -0700405 Vector<String8> mNewParameters;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700406 status_t mParamStatus;
407 Vector<ConfigEvent *> mConfigEvents;
408 bool mStandby;
Eric Laurent09b4ba82009-11-19 09:00:56 -0800409 int mId;
410 bool mExiting;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700411 };
412
413 // --- PlaybackThread ---
414 class PlaybackThread : public ThreadBase {
415 public:
416
417 enum type {
418 MIXER,
419 DIRECT,
420 DUPLICATING
421 };
422
Eric Laurent0e49d352009-11-09 23:32:22 -0800423 enum mixer_state {
424 MIXER_IDLE,
425 MIXER_TRACKS_ENABLED,
426 MIXER_TRACKS_READY
427 };
428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 // playback track
430 class Track : public TrackBase {
431 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700432 Track( const wp<ThreadBase>& thread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 const sp<Client>& client,
434 int streamType,
435 uint32_t sampleRate,
436 int format,
437 int channelCount,
438 int frameCount,
Eric Laurenteafff452010-06-01 23:49:17 -0700439 const sp<IMemory>& sharedBuffer,
440 int sessionId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 ~Track();
442
443 void dump(char* buffer, size_t size);
444 virtual status_t start();
445 virtual void stop();
446 void pause();
447
448 void flush();
449 void destroy();
450 void mute(bool);
451 void setVolume(float left, float right);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700452 int name() const {
453 return mName;
454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455
Eric Laurent570dd0b2009-05-22 09:18:15 -0700456 int type() const {
457 return mStreamType;
458 }
Eric Laurenteafff452010-06-01 23:49:17 -0700459 status_t attachAuxEffect(int EffectId);
460 void setAuxBuffer(int EffectId, int32_t *buffer);
461 int32_t *auxBuffer() { return mAuxBuffer; }
462 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
463 int16_t *mainBuffer() { return mMainBuffer; }
464 int auxEffectId() { return mAuxEffectId; }
Eric Laurent570dd0b2009-05-22 09:18:15 -0700465
466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 protected:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700468 friend class ThreadBase;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 friend class AudioFlinger;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700470 friend class TrackHandle;
471 friend class PlaybackThread;
472 friend class MixerThread;
473 friend class DirectOutputThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474
475 Track(const Track&);
476 Track& operator = (const Track&);
477
478 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700479 bool isMuted() { return mMute; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 bool isPausing() const {
481 return mState == PAUSING;
482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 bool isPaused() const {
484 return mState == PAUSED;
485 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 bool isReady() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 void setPaused() { mState = PAUSED; }
488 void reset();
489
Eric Laurent09b4ba82009-11-19 09:00:56 -0800490 bool isOutputTrack() const {
491 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
492 }
493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 // we don't really need a lock for these
495 float mVolume[2];
496 volatile bool mMute;
497 // FILLED state is used for suppressing volume ramp at begin of playing
498 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
499 mutable uint8_t mFillingUpStatus;
500 int8_t mRetryCount;
501 sp<IMemory> mSharedBuffer;
502 bool mResetDone;
Eric Laurent570dd0b2009-05-22 09:18:15 -0700503 int mStreamType;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700504 int mName;
Eric Laurenteafff452010-06-01 23:49:17 -0700505 int16_t *mMainBuffer;
506 int32_t *mAuxBuffer;
507 int mAuxEffectId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 }; // end of Track
509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510
511 // playback track
512 class OutputTrack : public Track {
513 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700514
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 class Buffer: public AudioBufferProvider::Buffer {
516 public:
517 int16_t *mBuffer;
518 };
Eric Laurent9d91ad52009-07-17 12:17:14 -0700519
520 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent7e2aad12009-12-18 05:47:48 -0800521 DuplicatingThread *sourceThread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 uint32_t sampleRate,
523 int format,
524 int channelCount,
525 int frameCount);
526 ~OutputTrack();
527
528 virtual status_t start();
529 virtual void stop();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700530 bool write(int16_t* data, uint32_t frames);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700532 bool isActive() { return mActive; }
533 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534
535 private:
536
Eric Laurent9d91ad52009-07-17 12:17:14 -0700537 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 void clearBufferQueue();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700539
540 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent7e2aad12009-12-18 05:47:48 -0800541 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 Vector < Buffer* > mBufferQueue;
544 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700545 bool mActive;
Eric Laurent7e2aad12009-12-18 05:47:48 -0800546 DuplicatingThread* mSourceThread;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700547 }; // end of OutputTrack
548
Eric Laurenteafff452010-06-01 23:49:17 -0700549 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700550 virtual ~PlaybackThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551
552 virtual status_t dump(int fd, const Vector<String16>& args);
553
554 // Thread virtuals
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 virtual status_t readyToRun();
556 virtual void onFirstRef();
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 virtual uint32_t latency() const;
559
560 virtual status_t setMasterVolume(float value);
561 virtual status_t setMasterMute(bool muted);
562
563 virtual float masterVolume() const;
564 virtual bool masterMute() const;
565
566 virtual status_t setStreamVolume(int stream, float value);
567 virtual status_t setStreamMute(int stream, bool muted);
568
569 virtual float streamVolume(int stream) const;
570 virtual bool streamMute(int stream) const;
571
Eric Laurent43c0b0a2010-01-25 08:49:09 -0800572 bool isStreamActive(int stream) const;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700573
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700574 sp<Track> createTrack_l(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 const sp<AudioFlinger::Client>& client,
576 int streamType,
577 uint32_t sampleRate,
578 int format,
579 int channelCount,
580 int frameCount,
581 const sp<IMemory>& sharedBuffer,
Eric Laurenteafff452010-06-01 23:49:17 -0700582 int sessionId,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 status_t *status);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700584
585 AudioStreamOut* getOutput() { return mOutput; }
586
587 virtual int type() const { return mType; }
Eric Laurentf9df2492009-08-06 08:49:39 -0700588 void suspend() { mSuspended++; }
589 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent7e2aad12009-12-18 05:47:48 -0800590 bool isSuspended() { return (mSuspended != 0); }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700591 virtual String8 getParameters(const String8& keys);
Eric Laurent4edfe752010-05-14 03:26:45 -0700592 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurente9ed2722010-01-19 17:37:09 -0800593 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurenteafff452010-06-01 23:49:17 -0700594 int16_t *mixBuffer() { return mMixBuffer; };
595
596 sp<EffectHandle> createEffect_l(
597 const sp<AudioFlinger::Client>& client,
598 const sp<IEffectClient>& effectClient,
599 int32_t priority,
600 int sessionId,
601 effect_descriptor_t *desc,
602 int *enabled,
603 status_t *status);
604
605 bool hasAudioSession(int sessionId);
606 sp<EffectChain> getEffectChain(int sessionId);
607 sp<EffectChain> getEffectChain_l(int sessionId);
608 status_t addEffectChain_l(const sp<EffectChain>& chain);
609 size_t removeEffectChain_l(const sp<EffectChain>& chain);
610 void lockEffectChains_l();
611 void unlockEffectChains();
612
613 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
614 void detachAuxEffect_l(int effectId);
615 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId);
616 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700617
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 struct stream_type_t {
619 stream_type_t()
620 : volume(1.0f),
621 mute(false)
622 {
623 }
624 float volume;
625 bool mute;
626 };
627
Eric Laurent9395d9b2009-07-23 13:17:39 -0700628 protected:
629 int mType;
630 int16_t* mMixBuffer;
Eric Laurentf9df2492009-08-06 08:49:39 -0700631 int mSuspended;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700632 int mBytesWritten;
633 bool mMasterMute;
634 SortedVector< wp<Track> > mActiveTracks;
635
Eric Laurentf5e868b2009-10-05 20:29:18 -0700636 virtual int getTrackName_l() = 0;
637 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent0e49d352009-11-09 23:32:22 -0800638 virtual uint32_t activeSleepTimeUs() = 0;
639 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurentf5e868b2009-10-05 20:29:18 -0700640
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 private:
642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 friend class AudioFlinger;
Eric Laurentf5aba822009-08-10 23:22:32 -0700644 friend class OutputTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 friend class Track;
646 friend class TrackBase;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700647 friend class MixerThread;
648 friend class DirectOutputThread;
649 friend class DuplicatingThread;
650
651 PlaybackThread(const Client&);
652 PlaybackThread& operator = (const PlaybackThread&);
653
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700654 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700655 void destroyTrack_l(const sp<Track>& track);
Eric Laurentf5e868b2009-10-05 20:29:18 -0700656
Eric Laurent9d91ad52009-07-17 12:17:14 -0700657 void readOutputParameters();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658
Eric Laurenteafff452010-06-01 23:49:17 -0700659 uint32_t device() { return mDevice; }
660
Eric Laurent9d91ad52009-07-17 12:17:14 -0700661 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurenteafff452010-06-01 23:49:17 -0700663 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665 SortedVector< sp<Track> > mTracks;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700666 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
667 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 AudioStreamOut* mOutput;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 float mMasterVolume;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670 nsecs_t mLastWriteTime;
671 int mNumWrites;
672 int mNumDelayedWrites;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 bool mInWrite;
Eric Laurenteafff452010-06-01 23:49:17 -0700674 Vector< sp<EffectChain> > mEffectChains;
675 uint32_t mDevice;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 };
677
Eric Laurent9d91ad52009-07-17 12:17:14 -0700678 class MixerThread : public PlaybackThread {
679 public:
Eric Laurenteafff452010-06-01 23:49:17 -0700680 MixerThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700681 virtual ~MixerThread();
682
683 // Thread virtuals
684 virtual bool threadLoop();
685
Eric Laurent4edfe752010-05-14 03:26:45 -0700686 void invalidateTracks(int streamType);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700687 virtual bool checkForNewParameters_l();
688 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
689
690 protected:
Eric Laurent0e49d352009-11-09 23:32:22 -0800691 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove);
Eric Laurentf5e868b2009-10-05 20:29:18 -0700692 virtual int getTrackName_l();
693 virtual void deleteTrackName_l(int name);
Eric Laurent0e49d352009-11-09 23:32:22 -0800694 virtual uint32_t activeSleepTimeUs();
695 virtual uint32_t idleSleepTimeUs();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700696
697 AudioMixer* mAudioMixer;
698 };
699
700 class DirectOutputThread : public PlaybackThread {
701 public:
702
Eric Laurenteafff452010-06-01 23:49:17 -0700703 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700704 ~DirectOutputThread();
705
706 // Thread virtuals
707 virtual bool threadLoop();
708
Eric Laurentf5e868b2009-10-05 20:29:18 -0700709 virtual bool checkForNewParameters_l();
710
711 protected:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700712 virtual int getTrackName_l();
713 virtual void deleteTrackName_l(int name);
Eric Laurent0e49d352009-11-09 23:32:22 -0800714 virtual uint32_t activeSleepTimeUs();
715 virtual uint32_t idleSleepTimeUs();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700716
717 private:
Eric Laurenteafff452010-06-01 23:49:17 -0700718 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
719
720 float mLeftVolFloat;
721 float mRightVolFloat;
722 uint16_t mLeftVolShort;
723 uint16_t mRightVolShort;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700724 };
725
726 class DuplicatingThread : public MixerThread {
727 public:
Eric Laurent09b4ba82009-11-19 09:00:56 -0800728 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700729 ~DuplicatingThread();
730
731 // Thread virtuals
732 virtual bool threadLoop();
733 void addOutputTrack(MixerThread* thread);
734 void removeOutputTrack(MixerThread* thread);
Eric Laurent7e2aad12009-12-18 05:47:48 -0800735 uint32_t waitTimeMs() { return mWaitTimeMs; }
736 protected:
737 virtual uint32_t activeSleepTimeUs();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700738
739 private:
Eric Laurent7e2aad12009-12-18 05:47:48 -0800740 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
741 void updateWaitTime();
742
Eric Laurent9d91ad52009-07-17 12:17:14 -0700743 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent7e2aad12009-12-18 05:47:48 -0800744 uint32_t mWaitTimeMs;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700745 };
746
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700747 PlaybackThread *checkPlaybackThread_l(int output) const;
748 MixerThread *checkMixerThread_l(int output) const;
749 RecordThread *checkRecordThread_l(int input) const;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700750 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent09b4ba82009-11-19 09:00:56 -0800751 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700752
Eric Laurenteafff452010-06-01 23:49:17 -0700753 int nextUniqueId();
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 friend class AudioBuffer;
756
757 class TrackHandle : public android::BnAudioTrack {
758 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700759 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 virtual ~TrackHandle();
761 virtual status_t start();
762 virtual void stop();
763 virtual void flush();
764 virtual void mute(bool);
765 virtual void pause();
766 virtual void setVolume(float left, float right);
767 virtual sp<IMemory> getCblk() const;
Eric Laurenteafff452010-06-01 23:49:17 -0700768 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769 virtual status_t onTransact(
770 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
771 private:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700772 sp<PlaybackThread::Track> mTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 };
774
775 friend class Client;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700776 friend class PlaybackThread::Track;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777
778
Eric Laurent0f8ab672009-09-17 05:12:56 -0700779 void removeClient_l(pid_t pid);
Eric Laurentd878cd82010-05-12 02:05:53 -0700780 void removeNotificationClient(pid_t pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781
782
Eric Laurent9d91ad52009-07-17 12:17:14 -0700783 // record thread
784 class RecordThread : public ThreadBase, public AudioBufferProvider
785 {
786 public:
787
788 // record track
789 class RecordTrack : public TrackBase {
790 public:
791 RecordTrack(const wp<ThreadBase>& thread,
792 const sp<Client>& client,
793 uint32_t sampleRate,
794 int format,
795 int channelCount,
796 int frameCount,
Eric Laurenteafff452010-06-01 23:49:17 -0700797 uint32_t flags,
798 int sessionId);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700799 ~RecordTrack();
800
801 virtual status_t start();
802 virtual void stop();
803
804 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
805 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
806
Eric Laurentee47d432009-11-07 00:01:32 -0800807 void dump(char* buffer, size_t size);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700808 private:
809 friend class AudioFlinger;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700810 friend class RecordThread;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700811
812 RecordTrack(const RecordTrack&);
813 RecordTrack& operator = (const RecordTrack&);
814
815 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
816
817 bool mOverflow;
818 };
819
820
821 RecordThread(const sp<AudioFlinger>& audioFlinger,
822 AudioStreamIn *input,
823 uint32_t sampleRate,
Eric Laurent09b4ba82009-11-19 09:00:56 -0800824 uint32_t channels,
825 int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700826 ~RecordThread();
827
828 virtual bool threadLoop();
829 virtual status_t readyToRun() { return NO_ERROR; }
830 virtual void onFirstRef();
831
832 status_t start(RecordTrack* recordTrack);
833 void stop(RecordTrack* recordTrack);
834 status_t dump(int fd, const Vector<String16>& args);
835 AudioStreamIn* getInput() { return mInput; }
836
837 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
838 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
839 virtual bool checkForNewParameters_l();
840 virtual String8 getParameters(const String8& keys);
Eric Laurent4edfe752010-05-14 03:26:45 -0700841 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700842 void readInputParameters();
Eric Laurent134ccbd2010-02-26 02:47:27 -0800843 virtual unsigned int getInputFramesLost();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700844
845 private:
846 RecordThread();
847 AudioStreamIn *mInput;
848 sp<RecordTrack> mActiveTrack;
849 Condition mStartStopCond;
850 AudioResampler *mResampler;
851 int32_t *mRsmpOutBuffer;
852 int16_t *mRsmpInBuffer;
853 size_t mRsmpInIndex;
854 size_t mInputBytes;
855 int mReqChannelCount;
856 uint32_t mReqSampleRate;
Eric Laurent52910952009-12-05 05:20:01 -0800857 ssize_t mBytesRead;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700858 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859
860 class RecordHandle : public android::BnAudioRecord {
861 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700862 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 virtual ~RecordHandle();
864 virtual status_t start();
865 virtual void stop();
866 virtual sp<IMemory> getCblk() const;
867 virtual status_t onTransact(
868 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
869 private:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700870 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 };
872
Eric Laurenteafff452010-06-01 23:49:17 -0700873 //--- Audio Effect Management
874
875 // EffectModule and EffectChain classes both have their own mutex to protect
876 // state changes or resource modifications. Always respect the following order
877 // if multiple mutexes must be acquired to avoid cross deadlock:
878 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
879
880 // The EffectModule class is a wrapper object controlling the effect engine implementation
881 // in the effect library. It prevents concurrent calls to process() and command() functions
882 // from different client threads. It keeps a list of EffectHandle objects corresponding
883 // to all client applications using this effect and notifies applications of effect state,
884 // control or parameter changes. It manages the activation state machine to send appropriate
885 // reset, enable, disable commands to effect engine and provide volume
886 // ramping when effects are activated/deactivated.
887 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
888 // the attached track(s) to accumulate their auxiliary channel.
889 class EffectModule: public RefBase {
890 public:
891 EffectModule(const wp<ThreadBase>& wThread,
892 const wp<AudioFlinger::EffectChain>& chain,
893 effect_descriptor_t *desc,
894 int id,
895 int sessionId);
896 ~EffectModule();
897
898 enum effect_state {
899 IDLE,
900 RESET,
901 STARTING,
902 ACTIVE,
903 STOPPING,
904 STOPPED
905 };
906
907 int id() { return mId; }
908 void process();
909 status_t command(int cmdCode, int cmdSize, void *pCmdData, int *replySize, void *pReplyData);
910
911 void reset();
912 status_t configure();
913 status_t init();
914 uint32_t state() {
915 return mState;
916 }
917 uint32_t status() {
918 return mStatus;
919 }
920 status_t setEnabled(bool enabled);
921 bool isEnabled();
922
923 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
924 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
925 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
926 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
927
928 status_t addHandle(sp<EffectHandle>& handle);
929 void disconnect(const wp<EffectHandle>& handle);
930 size_t removeHandle (const wp<EffectHandle>& handle);
931
932 effect_descriptor_t& desc() { return mDescriptor; }
933
934 status_t setDevice(uint32_t device);
935 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
936
937 status_t dump(int fd, const Vector<String16>& args);
938
939 protected:
940
941 EffectModule(const EffectModule&);
942 EffectModule& operator = (const EffectModule&);
943
944 status_t start();
945 status_t stop();
946
947 Mutex mLock; // mutex for process, commands and handles list protection
948 wp<ThreadBase> mThread; // parent thread
949 wp<EffectChain> mChain; // parent effect chain
950 int mId; // this instance unique ID
951 int mSessionId; // audio session ID
952 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
953 effect_config_t mConfig; // input and output audio configuration
954 effect_interface_t mEffectInterface; // Effect module C API
955 status_t mStatus; // initialization status
956 uint32_t mState; // current activation state (effect_state)
957 Vector< wp<EffectHandle> > mHandles; // list of client handles
958 };
959
960 // The EffectHandle class implements the IEffect interface. It provides resources
961 // to receive parameter updates, keeps track of effect control
962 // ownership and state and has a pointer to the EffectModule object it is controlling.
963 // There is one EffectHandle object for each application controlling (or using)
964 // an effect module.
965 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
966 class EffectHandle: public android::BnEffect {
967 public:
968
969 EffectHandle(const sp<EffectModule>& effect,
970 const sp<AudioFlinger::Client>& client,
971 const sp<IEffectClient>& effectClient,
972 int32_t priority);
973 virtual ~EffectHandle();
974
975 // IEffect
976 virtual status_t enable();
977 virtual status_t disable();
978 virtual status_t command(int cmdCode, int cmdSize, void *pCmdData, int *replySize, void *pReplyData);
979 virtual void disconnect();
980 virtual sp<IMemory> getCblk() const;
981 virtual status_t onTransact(uint32_t code, const Parcel& data,
982 Parcel* reply, uint32_t flags);
983
984
985 // Give or take control of effect module
986 void setControl(bool hasControl, bool signal);
987 void commandExecuted(int cmdCode, int cmdSize, void *pCmdData, int replySize, void *pReplyData);
988 void setEnabled(bool enabled);
989
990 // Getters
991 int id() { return mEffect->id(); }
992 int priority() { return mPriority; }
993 bool hasControl() { return mHasControl; }
994 sp<EffectModule> effect() { return mEffect; }
995
996 void dump(char* buffer, size_t size);
997
998 protected:
999
1000 EffectHandle(const EffectHandle&);
1001 EffectHandle& operator =(const EffectHandle&);
1002
1003 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1004 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1005 sp<Client> mClient; // client for shared memory allocation
1006 sp<IMemory> mCblkMemory; // shared memory for control block
1007 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1008 uint8_t* mBuffer; // pointer to parameter area in shared memory
1009 int mPriority; // client application priority to control the effect
1010 bool mHasControl; // true if this handle is controlling the effect
1011 };
1012
1013 // the EffectChain class represents a group of effects associated to one audio session.
1014 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1015 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1016 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1017 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1018 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1019 // input buffer used by the track as accumulation buffer.
1020 class EffectChain: public RefBase {
1021 public:
1022 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1023 ~EffectChain();
1024
1025 void process_l();
1026
1027 void lock() {
1028 mLock.lock();
1029 }
1030 void unlock() {
1031 mLock.unlock();
1032 }
1033
1034 status_t addEffect(sp<EffectModule>& handle);
1035 size_t removeEffect(const sp<EffectModule>& handle);
1036
1037 int sessionId() {
1038 return mSessionId;
1039 }
1040 sp<EffectModule> getEffectFromDesc(effect_descriptor_t *descriptor);
1041 sp<EffectModule> getEffectFromId(int id);
1042 sp<EffectModule> getVolumeController();
1043 bool setVolume(uint32_t *left, uint32_t *right);
1044 void setDevice(uint32_t device);
1045
1046 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1047 mInBuffer = buffer;
1048 mOwnInBuffer = ownsBuffer;
1049 }
1050 int16_t *inBuffer() {
1051 return mInBuffer;
1052 }
1053 void setOutBuffer(int16_t *buffer) {
1054 mOutBuffer = buffer;
1055 }
1056 int16_t *outBuffer() {
1057 return mOutBuffer;
1058 }
1059
1060 void startTrack() {mActiveTrackCnt++;}
1061 void stopTrack() {mActiveTrackCnt--;}
1062 int activeTracks() { return mActiveTrackCnt;}
1063
1064 status_t dump(int fd, const Vector<String16>& args);
1065
1066 protected:
1067
1068 EffectChain(const EffectChain&);
1069 EffectChain& operator =(const EffectChain&);
1070
1071 wp<ThreadBase> mThread; // parent mixer thread
1072 Mutex mLock; // mutex protecting effect list
1073 Vector<sp<EffectModule> > mEffects; // list of effect modules
1074 int mSessionId; // audio session ID
1075 int16_t *mInBuffer; // chain input buffer
1076 int16_t *mOutBuffer; // chain output buffer
1077 int mVolumeCtrlIdx; // index of insert effect having control over volume
1078 int mActiveTrackCnt; // number of active tracks connected
1079 bool mOwnInBuffer; // true if the chain owns its input buffer
1080 };
1081
Eric Laurent9d91ad52009-07-17 12:17:14 -07001082 friend class RecordThread;
1083 friend class PlaybackThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085
The Android Open Source Project22f8def2009-03-09 11:52:12 -07001086 mutable Mutex mLock;
The Android Open Source Project22f8def2009-03-09 11:52:12 -07001087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1089
Eric Laurent9d91ad52009-07-17 12:17:14 -07001090 mutable Mutex mHardwareLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 AudioHardwareInterface* mAudioHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 mutable int mHardwareStatus;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -07001093
Eric Laurent9d91ad52009-07-17 12:17:14 -07001094
Eric Laurente0e9ecc2009-07-28 08:44:33 -07001095 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001096 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
1097 float mMasterVolume;
1098 bool mMasterMute;
1099
Eric Laurente0e9ecc2009-07-28 08:44:33 -07001100 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001101
Eric Laurentd878cd82010-05-12 02:05:53 -07001102 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurenteafff452010-06-01 23:49:17 -07001103 volatile int32_t mNextUniqueId;
Eric Laurentd878cd82010-05-12 02:05:53 -07001104#ifdef LVMX
1105 int mLifeVibesClientPid;
1106#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107};
1108
1109// ----------------------------------------------------------------------------
1110
1111}; // namespace android
1112
1113#endif // ANDROID_AUDIO_FLINGER_H