blob: 5520551992f4c8d6d9ac8cb1ac02ff5183ae7e5d [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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <utils/SortedVector.h>
35#include <utils/Vector.h>
36
Mathias Agopian24651682010-07-14 18:41:18 -070037#include <binder/BinderService.h>
38#include <binder/MemoryDealer.h>
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040#include <hardware_legacy/AudioHardwareInterface.h>
41
42#include "AudioBufferProvider.h"
43
44namespace android {
45
46class audio_track_cblk_t;
Eric Laurent65b65452010-06-01 23:49:17 -070047class effect_param_cblk_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048class AudioMixer;
49class AudioBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -070050class AudioResampler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52
53// ----------------------------------------------------------------------------
54
55#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
56#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
57
58
59// ----------------------------------------------------------------------------
60
61static const nsecs_t kStandbyTimeInNsecs = seconds(3);
62
Mathias Agopian24651682010-07-14 18:41:18 -070063class AudioFlinger :
64 public BinderService<AudioFlinger>,
65 public BnAudioFlinger
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066{
Mathias Agopian24651682010-07-14 18:41:18 -070067 friend class BinderService<AudioFlinger>;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068public:
Mathias Agopian24651682010-07-14 18:41:18 -070069 static char const* getServiceName() { return "media.audio_flinger"; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71 virtual status_t dump(int fd, const Vector<String16>& args);
72
73 // IAudioFlinger interface
74 virtual sp<IAudioTrack> createTrack(
75 pid_t pid,
76 int streamType,
77 uint32_t sampleRate,
78 int format,
79 int channelCount,
80 int frameCount,
81 uint32_t flags,
82 const sp<IMemory>& sharedBuffer,
Eric Laurentddb78e72009-07-28 08:44:33 -070083 int output,
Eric Laurent65b65452010-06-01 23:49:17 -070084 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 status_t *status);
86
Eric Laurentddb78e72009-07-28 08:44:33 -070087 virtual uint32_t sampleRate(int output) const;
88 virtual int channelCount(int output) const;
89 virtual int format(int output) const;
90 virtual size_t frameCount(int output) const;
91 virtual uint32_t latency(int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 virtual status_t setMasterVolume(float value);
94 virtual status_t setMasterMute(bool muted);
95
96 virtual float masterVolume() const;
97 virtual bool masterMute() const;
98
Eric Laurentddb78e72009-07-28 08:44:33 -070099 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 virtual status_t setStreamMute(int stream, bool muted);
101
Eric Laurentddb78e72009-07-28 08:44:33 -0700102 virtual float streamVolume(int stream, int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 virtual bool streamMute(int stream) const;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 virtual status_t setMode(int mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 virtual status_t setMicMute(bool state);
108 virtual bool getMicMute() const;
109
Eric Laurent23f25cd2010-01-25 08:49:09 -0800110 virtual bool isStreamActive(int stream) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Eric Laurentddb78e72009-07-28 08:44:33 -0700112 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
113 virtual String8 getParameters(int ioHandle, const String8& keys);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurenta553c252009-07-17 12:17:14 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent47d0a922010-02-26 02:47:27 -0800118 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700119
Eric Laurentddb78e72009-07-28 08:44:33 -0700120 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700121 uint32_t *pSamplingRate,
122 uint32_t *pFormat,
123 uint32_t *pChannels,
124 uint32_t *pLatencyMs,
125 uint32_t flags);
126
Eric Laurentddb78e72009-07-28 08:44:33 -0700127 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700128
Eric Laurentddb78e72009-07-28 08:44:33 -0700129 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700130
Eric Laurentddb78e72009-07-28 08:44:33 -0700131 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700132
Eric Laurentddb78e72009-07-28 08:44:33 -0700133 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700134
Eric Laurentddb78e72009-07-28 08:44:33 -0700135 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700136 uint32_t *pSamplingRate,
137 uint32_t *pFormat,
138 uint32_t *pChannels,
139 uint32_t acoustics);
140
Eric Laurentddb78e72009-07-28 08:44:33 -0700141 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700142
Eric Laurentddb78e72009-07-28 08:44:33 -0700143 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700144
Eric Laurent415f3e22009-10-21 08:14:22 -0700145 virtual status_t setVoiceVolume(float volume);
146
Eric Laurent0986e792010-01-19 17:37:09 -0800147 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
148
Eric Laurent65b65452010-06-01 23:49:17 -0700149 virtual int newAudioSessionId();
150
151 virtual status_t loadEffectLibrary(const char *libPath, int *handle);
152
153 virtual status_t unloadEffectLibrary(int handle);
154
155 virtual status_t queryNumberEffects(uint32_t *numEffects);
156
Eric Laurent53334cd2010-06-23 17:38:20 -0700157 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -0700158
159 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
160
161 virtual sp<IEffect> createEffect(pid_t pid,
162 effect_descriptor_t *pDesc,
163 const sp<IEffectClient>& effectClient,
164 int32_t priority,
165 int output,
166 int sessionId,
167 status_t *status,
168 int *id,
169 int *enabled);
170
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700171 virtual status_t moveEffects(int session, int srcOutput, int dstOutput);
Eric Laurent53334cd2010-06-23 17:38:20 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 enum hardware_call_state {
174 AUDIO_HW_IDLE = 0,
175 AUDIO_HW_INIT,
176 AUDIO_HW_OUTPUT_OPEN,
177 AUDIO_HW_OUTPUT_CLOSE,
178 AUDIO_HW_INPUT_OPEN,
179 AUDIO_HW_INPUT_CLOSE,
180 AUDIO_HW_STANDBY,
181 AUDIO_HW_SET_MASTER_VOLUME,
182 AUDIO_HW_GET_ROUTING,
183 AUDIO_HW_SET_ROUTING,
184 AUDIO_HW_GET_MODE,
185 AUDIO_HW_SET_MODE,
186 AUDIO_HW_GET_MIC_MUTE,
187 AUDIO_HW_SET_MIC_MUTE,
188 AUDIO_SET_VOICE_VOLUME,
189 AUDIO_SET_PARAMETER,
190 };
191
192 // record interface
193 virtual sp<IAudioRecord> openRecord(
194 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700195 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 uint32_t sampleRate,
197 int format,
198 int channelCount,
199 int frameCount,
200 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700201 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 status_t *status);
203
204 virtual status_t onTransact(
205 uint32_t code,
206 const Parcel& data,
207 Parcel* reply,
208 uint32_t flags);
209
Eric Laurent53334cd2010-06-23 17:38:20 -0700210 uint32_t getMode() { return mMode; }
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212private:
213 AudioFlinger();
214 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
217 // Internal dump utilites.
218 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
219 status_t dumpClients(int fd, const Vector<String16>& args);
220 status_t dumpInternals(int fd, const Vector<String16>& args);
221
222 // --- Client ---
223 class Client : public RefBase {
224 public:
225 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
226 virtual ~Client();
227 const sp<MemoryDealer>& heap() const;
228 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700229 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 private:
232 Client(const Client&);
233 Client& operator = (const Client&);
234 sp<AudioFlinger> mAudioFlinger;
235 sp<MemoryDealer> mMemoryDealer;
236 pid_t mPid;
237 };
238
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700239 // --- Notification Client ---
240 class NotificationClient : public IBinder::DeathRecipient {
241 public:
242 NotificationClient(const sp<AudioFlinger>& audioFlinger,
243 const sp<IAudioFlingerClient>& client,
244 pid_t pid);
245 virtual ~NotificationClient();
246
247 sp<IAudioFlingerClient> client() { return mClient; }
248
249 // IBinder::DeathRecipient
250 virtual void binderDied(const wp<IBinder>& who);
251
252 private:
253 NotificationClient(const NotificationClient&);
254 NotificationClient& operator = (const NotificationClient&);
255
256 sp<AudioFlinger> mAudioFlinger;
257 pid_t mPid;
258 sp<IAudioFlingerClient> mClient;
259 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 class TrackHandle;
262 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700263 class RecordThread;
264 class PlaybackThread;
265 class MixerThread;
266 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800267 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700268 class Track;
269 class RecordTrack;
Eric Laurent65b65452010-06-01 23:49:17 -0700270 class EffectModule;
271 class EffectHandle;
272 class EffectChain;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
Eric Laurenta553c252009-07-17 12:17:14 -0700274 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800276 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700277 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
Eric Laurent3fdb1262009-11-07 00:01:32 -0800279 status_t dumpBase(int fd, const Vector<String16>& args);
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 // base for record and playback
282 class TrackBase : public AudioBufferProvider, public RefBase {
283
284 public:
285 enum track_state {
286 IDLE,
287 TERMINATED,
288 STOPPED,
289 RESUMING,
290 ACTIVE,
291 PAUSING,
292 PAUSED
293 };
294
295 enum track_flags {
296 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
297 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
298 // The upper 16 bits are used for track-specific flags.
299 };
300
Eric Laurenta553c252009-07-17 12:17:14 -0700301 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 uint32_t sampleRate,
304 int format,
305 int channelCount,
306 int frameCount,
307 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700308 const sp<IMemory>& sharedBuffer,
309 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 ~TrackBase();
311
312 virtual status_t start() = 0;
313 virtual void stop() = 0;
314 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700315 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700316 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317
318 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700319 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700321 friend class PlaybackThread;
322 friend class RecordThread;
323 friend class MixerThread;
324 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
326 TrackBase(const TrackBase&);
327 TrackBase& operator = (const TrackBase&);
328
329 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
330 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 int format() const {
333 return mFormat;
334 }
335
336 int channelCount() const ;
337
338 int sampleRate() const;
339
340 void* getBuffer(uint32_t offset, uint32_t frames) const;
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 bool isStopped() const {
343 return mState == STOPPED;
344 }
345
346 bool isTerminated() const {
347 return mState == TERMINATED;
348 }
349
350 bool step();
351 void reset();
352
Eric Laurenta553c252009-07-17 12:17:14 -0700353 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 sp<Client> mClient;
355 sp<IMemory> mCblkMemory;
356 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 void* mBuffer;
358 void* mBufferEnd;
359 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 // we don't really need a lock for these
361 int mState;
362 int mClientTid;
363 uint8_t mFormat;
364 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700365 int mSessionId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 };
367
Eric Laurenta553c252009-07-17 12:17:14 -0700368 class ConfigEvent {
369 public:
370 ConfigEvent() : mEvent(0), mParam(0) {}
371
372 int mEvent;
373 int mParam;
374 };
375
376 uint32_t sampleRate() const;
377 int channelCount() const;
378 int format() const;
379 size_t frameCount() const;
380 void wakeUp() { mWaitWorkCV.broadcast(); }
381 void exit();
382 virtual bool checkForNewParameters_l() = 0;
383 virtual status_t setParameters(const String8& keyValuePairs);
384 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700385 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700386 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700387 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700388 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800389 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800390 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700391
Eric Laurent2c817f52009-07-23 13:17:39 -0700392 mutable Mutex mLock;
393
Eric Laurenta553c252009-07-17 12:17:14 -0700394 protected:
395
396 friend class Track;
397 friend class TrackBase;
398 friend class PlaybackThread;
399 friend class MixerThread;
400 friend class DirectOutputThread;
401 friend class DuplicatingThread;
402 friend class RecordThread;
403 friend class RecordTrack;
404
Eric Laurenta553c252009-07-17 12:17:14 -0700405 Condition mWaitWorkCV;
406 sp<AudioFlinger> mAudioFlinger;
407 uint32_t mSampleRate;
408 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700409 uint32_t mChannels;
410 uint16_t mChannelCount;
411 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700412 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700413 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700414 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700415 status_t mParamStatus;
416 Vector<ConfigEvent *> mConfigEvents;
417 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800418 int mId;
419 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700420 };
421
422 // --- PlaybackThread ---
423 class PlaybackThread : public ThreadBase {
424 public:
425
426 enum type {
427 MIXER,
428 DIRECT,
429 DUPLICATING
430 };
431
Eric Laurent059b4be2009-11-09 23:32:22 -0800432 enum mixer_state {
433 MIXER_IDLE,
434 MIXER_TRACKS_ENABLED,
435 MIXER_TRACKS_READY
436 };
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 // playback track
439 class Track : public TrackBase {
440 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700441 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 const sp<Client>& client,
443 int streamType,
444 uint32_t sampleRate,
445 int format,
446 int channelCount,
447 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700448 const sp<IMemory>& sharedBuffer,
449 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 ~Track();
451
452 void dump(char* buffer, size_t size);
453 virtual status_t start();
454 virtual void stop();
455 void pause();
456
457 void flush();
458 void destroy();
459 void mute(bool);
460 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700461 int name() const {
462 return mName;
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464
Eric Laurent4bc035a2009-05-22 09:18:15 -0700465 int type() const {
466 return mStreamType;
467 }
Eric Laurent65b65452010-06-01 23:49:17 -0700468 status_t attachAuxEffect(int EffectId);
469 void setAuxBuffer(int EffectId, int32_t *buffer);
470 int32_t *auxBuffer() { return mAuxBuffer; }
471 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
472 int16_t *mainBuffer() { return mMainBuffer; }
473 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700474
475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700477 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700479 friend class TrackHandle;
480 friend class PlaybackThread;
481 friend class MixerThread;
482 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 Track(const Track&);
485 Track& operator = (const Track&);
486
487 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700488 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 bool isPausing() const {
490 return mState == PAUSING;
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 bool isPaused() const {
493 return mState == PAUSED;
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 void setPaused() { mState = PAUSED; }
497 void reset();
498
Eric Laurent49f02be2009-11-19 09:00:56 -0800499 bool isOutputTrack() const {
500 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
501 }
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 // we don't really need a lock for these
504 float mVolume[2];
505 volatile bool mMute;
506 // FILLED state is used for suppressing volume ramp at begin of playing
507 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
508 mutable uint8_t mFillingUpStatus;
509 int8_t mRetryCount;
510 sp<IMemory> mSharedBuffer;
511 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700512 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700513 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700514 int16_t *mMainBuffer;
515 int32_t *mAuxBuffer;
516 int mAuxEffectId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }; // end of Track
518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
520 // playback track
521 class OutputTrack : public Track {
522 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 class Buffer: public AudioBufferProvider::Buffer {
525 public:
526 int16_t *mBuffer;
527 };
Eric Laurenta553c252009-07-17 12:17:14 -0700528
529 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800530 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 uint32_t sampleRate,
532 int format,
533 int channelCount,
534 int frameCount);
535 ~OutputTrack();
536
537 virtual status_t start();
538 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700539 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700541 bool isActive() { return mActive; }
542 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
544 private:
545
Eric Laurenta553c252009-07-17 12:17:14 -0700546 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700548
549 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800550 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 Vector < Buffer* > mBufferQueue;
553 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700554 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800555 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700556 }; // end of OutputTrack
557
Eric Laurent65b65452010-06-01 23:49:17 -0700558 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700559 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
561 virtual status_t dump(int fd, const Vector<String16>& args);
562
563 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 virtual status_t readyToRun();
565 virtual void onFirstRef();
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 virtual uint32_t latency() const;
568
569 virtual status_t setMasterVolume(float value);
570 virtual status_t setMasterMute(bool muted);
571
572 virtual float masterVolume() const;
573 virtual bool masterMute() const;
574
575 virtual status_t setStreamVolume(int stream, float value);
576 virtual status_t setStreamMute(int stream, bool muted);
577
578 virtual float streamVolume(int stream) const;
579 virtual bool streamMute(int stream) const;
580
Eric Laurent23f25cd2010-01-25 08:49:09 -0800581 bool isStreamActive(int stream) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700582
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700583 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 const sp<AudioFlinger::Client>& client,
585 int streamType,
586 uint32_t sampleRate,
587 int format,
588 int channelCount,
589 int frameCount,
590 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700591 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700593
594 AudioStreamOut* getOutput() { return mOutput; }
595
596 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700597 void suspend() { mSuspended++; }
598 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800599 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700600 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700601 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800602 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700603 int16_t *mixBuffer() { return mMixBuffer; };
604
605 sp<EffectHandle> createEffect_l(
606 const sp<AudioFlinger::Client>& client,
607 const sp<IEffectClient>& effectClient,
608 int32_t priority,
609 int sessionId,
610 effect_descriptor_t *desc,
611 int *enabled,
612 status_t *status);
Eric Laurent53334cd2010-06-23 17:38:20 -0700613 void disconnectEffect(const sp< EffectModule>& effect,
614 const wp<EffectHandle>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -0700615
Eric Laurent493941b2010-07-28 01:32:47 -0700616 // return values for hasAudioSession (bit field)
617 enum effect_state {
618 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
619 // effect
620 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
621 // track
622 };
623
624 uint32_t hasAudioSession(int sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700625 sp<EffectChain> getEffectChain(int sessionId);
626 sp<EffectChain> getEffectChain_l(int sessionId);
627 status_t addEffectChain_l(const sp<EffectChain>& chain);
628 size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700629 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
630 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
Eric Laurent65b65452010-06-01 23:49:17 -0700631
632 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
633 void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700634 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
635 int EffectId);
636 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
637 int EffectId);
Eric Laurent53334cd2010-06-23 17:38:20 -0700638 void setMode(uint32_t mode);
Eric Laurenta553c252009-07-17 12:17:14 -0700639
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700640 status_t addEffect_l(const sp< EffectModule>& effect);
641 void removeEffect_l(const sp< EffectModule>& effect);
642
643 uint32_t getStrategyForSession_l(int sessionId);
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 struct stream_type_t {
646 stream_type_t()
647 : volume(1.0f),
648 mute(false)
649 {
650 }
651 float volume;
652 bool mute;
653 };
654
Eric Laurent2c817f52009-07-23 13:17:39 -0700655 protected:
656 int mType;
657 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700658 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700659 int mBytesWritten;
660 bool mMasterMute;
661 SortedVector< wp<Track> > mActiveTracks;
662
Eric Laurent62443f52009-10-05 20:29:18 -0700663 virtual int getTrackName_l() = 0;
664 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800665 virtual uint32_t activeSleepTimeUs() = 0;
666 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 private:
669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700671 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 friend class Track;
673 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700674 friend class MixerThread;
675 friend class DirectOutputThread;
676 friend class DuplicatingThread;
677
678 PlaybackThread(const Client&);
679 PlaybackThread& operator = (const PlaybackThread&);
680
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700681 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700682 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700683
Eric Laurenta553c252009-07-17 12:17:14 -0700684 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685
Eric Laurent65b65452010-06-01 23:49:17 -0700686 uint32_t device() { return mDevice; }
687
Eric Laurenta553c252009-07-17 12:17:14 -0700688 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700690 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700693 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
694 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 nsecs_t mLastWriteTime;
698 int mNumWrites;
699 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700701 Vector< sp<EffectChain> > mEffectChains;
702 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 };
704
Eric Laurenta553c252009-07-17 12:17:14 -0700705 class MixerThread : public PlaybackThread {
706 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700707 MixerThread (const sp<AudioFlinger>& audioFlinger,
708 AudioStreamOut* output,
709 int id,
710 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700711 virtual ~MixerThread();
712
713 // Thread virtuals
714 virtual bool threadLoop();
715
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700716 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700717 virtual bool checkForNewParameters_l();
718 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
719
720 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700721 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
722 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700723 virtual int getTrackName_l();
724 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800725 virtual uint32_t activeSleepTimeUs();
726 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700727
728 AudioMixer* mAudioMixer;
729 };
730
731 class DirectOutputThread : public PlaybackThread {
732 public:
733
Eric Laurent65b65452010-06-01 23:49:17 -0700734 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700735 ~DirectOutputThread();
736
737 // Thread virtuals
738 virtual bool threadLoop();
739
Eric Laurent62443f52009-10-05 20:29:18 -0700740 virtual bool checkForNewParameters_l();
741
742 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700743 virtual int getTrackName_l();
744 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800745 virtual uint32_t activeSleepTimeUs();
746 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700747
748 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700749 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
750
751 float mLeftVolFloat;
752 float mRightVolFloat;
753 uint16_t mLeftVolShort;
754 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700755 };
756
757 class DuplicatingThread : public MixerThread {
758 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800759 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700760 ~DuplicatingThread();
761
762 // Thread virtuals
763 virtual bool threadLoop();
764 void addOutputTrack(MixerThread* thread);
765 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800766 uint32_t waitTimeMs() { return mWaitTimeMs; }
767 protected:
768 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700769
770 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800771 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
772 void updateWaitTime();
773
Eric Laurenta553c252009-07-17 12:17:14 -0700774 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800775 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700776 };
777
Eric Laurentddb78e72009-07-28 08:44:33 -0700778 PlaybackThread *checkPlaybackThread_l(int output) const;
779 MixerThread *checkMixerThread_l(int output) const;
780 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700781 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800782 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700783
Eric Laurent65b65452010-06-01 23:49:17 -0700784 int nextUniqueId();
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700785 status_t moveEffectChain_l(int session,
786 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -0700787 AudioFlinger::PlaybackThread *dstThread,
788 bool reRegister);
Eric Laurent65b65452010-06-01 23:49:17 -0700789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 friend class AudioBuffer;
791
792 class TrackHandle : public android::BnAudioTrack {
793 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700794 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 virtual ~TrackHandle();
796 virtual status_t start();
797 virtual void stop();
798 virtual void flush();
799 virtual void mute(bool);
800 virtual void pause();
801 virtual void setVolume(float left, float right);
802 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700803 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 virtual status_t onTransact(
805 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
806 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700807 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 };
809
810 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700811 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812
813
Eric Laurentb9481d82009-09-17 05:12:56 -0700814 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700815 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816
817
Eric Laurenta553c252009-07-17 12:17:14 -0700818 // record thread
819 class RecordThread : public ThreadBase, public AudioBufferProvider
820 {
821 public:
822
823 // record track
824 class RecordTrack : public TrackBase {
825 public:
826 RecordTrack(const wp<ThreadBase>& thread,
827 const sp<Client>& client,
828 uint32_t sampleRate,
829 int format,
830 int channelCount,
831 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700832 uint32_t flags,
833 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700834 ~RecordTrack();
835
836 virtual status_t start();
837 virtual void stop();
838
839 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
840 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
841
Eric Laurent3fdb1262009-11-07 00:01:32 -0800842 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700843 private:
844 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700845 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700846
847 RecordTrack(const RecordTrack&);
848 RecordTrack& operator = (const RecordTrack&);
849
850 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
851
852 bool mOverflow;
853 };
854
855
856 RecordThread(const sp<AudioFlinger>& audioFlinger,
857 AudioStreamIn *input,
858 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800859 uint32_t channels,
860 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700861 ~RecordThread();
862
863 virtual bool threadLoop();
864 virtual status_t readyToRun() { return NO_ERROR; }
865 virtual void onFirstRef();
866
867 status_t start(RecordTrack* recordTrack);
868 void stop(RecordTrack* recordTrack);
869 status_t dump(int fd, const Vector<String16>& args);
870 AudioStreamIn* getInput() { return mInput; }
871
872 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
873 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
874 virtual bool checkForNewParameters_l();
875 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700876 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700877 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800878 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700879
880 private:
881 RecordThread();
882 AudioStreamIn *mInput;
883 sp<RecordTrack> mActiveTrack;
884 Condition mStartStopCond;
885 AudioResampler *mResampler;
886 int32_t *mRsmpOutBuffer;
887 int16_t *mRsmpInBuffer;
888 size_t mRsmpInIndex;
889 size_t mInputBytes;
890 int mReqChannelCount;
891 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800892 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700893 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894
895 class RecordHandle : public android::BnAudioRecord {
896 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700897 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 virtual ~RecordHandle();
899 virtual status_t start();
900 virtual void stop();
901 virtual sp<IMemory> getCblk() const;
902 virtual status_t onTransact(
903 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
904 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700905 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 };
907
Eric Laurent65b65452010-06-01 23:49:17 -0700908 //--- Audio Effect Management
909
910 // EffectModule and EffectChain classes both have their own mutex to protect
911 // state changes or resource modifications. Always respect the following order
912 // if multiple mutexes must be acquired to avoid cross deadlock:
913 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
914
915 // The EffectModule class is a wrapper object controlling the effect engine implementation
916 // in the effect library. It prevents concurrent calls to process() and command() functions
917 // from different client threads. It keeps a list of EffectHandle objects corresponding
918 // to all client applications using this effect and notifies applications of effect state,
919 // control or parameter changes. It manages the activation state machine to send appropriate
920 // reset, enable, disable commands to effect engine and provide volume
921 // ramping when effects are activated/deactivated.
922 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
923 // the attached track(s) to accumulate their auxiliary channel.
924 class EffectModule: public RefBase {
925 public:
926 EffectModule(const wp<ThreadBase>& wThread,
927 const wp<AudioFlinger::EffectChain>& chain,
928 effect_descriptor_t *desc,
929 int id,
930 int sessionId);
931 ~EffectModule();
932
933 enum effect_state {
934 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700935 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700936 STARTING,
937 ACTIVE,
938 STOPPING,
939 STOPPED
940 };
941
942 int id() { return mId; }
943 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -0700944 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700945 status_t command(uint32_t cmdCode,
946 uint32_t cmdSize,
947 void *pCmdData,
948 uint32_t *replySize,
949 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -0700950
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700951 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700952 status_t configure();
953 status_t init();
954 uint32_t state() {
955 return mState;
956 }
957 uint32_t status() {
958 return mStatus;
959 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700960 int sessionId() {
961 return mSessionId;
962 }
Eric Laurent65b65452010-06-01 23:49:17 -0700963 status_t setEnabled(bool enabled);
964 bool isEnabled();
965
966 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
967 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
968 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
969 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700970 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
971 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -0700972
973 status_t addHandle(sp<EffectHandle>& handle);
974 void disconnect(const wp<EffectHandle>& handle);
975 size_t removeHandle (const wp<EffectHandle>& handle);
976
977 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700978 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700979
980 status_t setDevice(uint32_t device);
981 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700982 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700983
984 status_t dump(int fd, const Vector<String16>& args);
985
986 protected:
987
Eric Laurent7d850f22010-07-09 13:34:17 -0700988 // Maximum time allocated to effect engines to complete the turn off sequence
989 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
990
Eric Laurent65b65452010-06-01 23:49:17 -0700991 EffectModule(const EffectModule&);
992 EffectModule& operator = (const EffectModule&);
993
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700994 status_t start_l();
995 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700996
Eric Laurent53334cd2010-06-23 17:38:20 -0700997 // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified
998 static const uint32_t sDeviceConvTable[];
999 static uint32_t deviceAudioSystemToEffectApi(uint32_t device);
1000
1001 // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified
1002 static const uint32_t sModeConvTable[];
1003 static int modeAudioSystemToEffectApi(uint32_t mode);
1004
Eric Laurent65b65452010-06-01 23:49:17 -07001005 Mutex mLock; // mutex for process, commands and handles list protection
1006 wp<ThreadBase> mThread; // parent thread
1007 wp<EffectChain> mChain; // parent effect chain
1008 int mId; // this instance unique ID
1009 int mSessionId; // audio session ID
1010 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1011 effect_config_t mConfig; // input and output audio configuration
1012 effect_interface_t mEffectInterface; // Effect module C API
1013 status_t mStatus; // initialization status
1014 uint32_t mState; // current activation state (effect_state)
1015 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001016 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1017 // sending disable command.
1018 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001019 };
1020
1021 // The EffectHandle class implements the IEffect interface. It provides resources
1022 // to receive parameter updates, keeps track of effect control
1023 // ownership and state and has a pointer to the EffectModule object it is controlling.
1024 // There is one EffectHandle object for each application controlling (or using)
1025 // an effect module.
1026 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1027 class EffectHandle: public android::BnEffect {
1028 public:
1029
1030 EffectHandle(const sp<EffectModule>& effect,
1031 const sp<AudioFlinger::Client>& client,
1032 const sp<IEffectClient>& effectClient,
1033 int32_t priority);
1034 virtual ~EffectHandle();
1035
1036 // IEffect
1037 virtual status_t enable();
1038 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001039 virtual status_t command(uint32_t cmdCode,
1040 uint32_t cmdSize,
1041 void *pCmdData,
1042 uint32_t *replySize,
1043 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001044 virtual void disconnect();
1045 virtual sp<IMemory> getCblk() const;
1046 virtual status_t onTransact(uint32_t code, const Parcel& data,
1047 Parcel* reply, uint32_t flags);
1048
1049
1050 // Give or take control of effect module
1051 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001052 void commandExecuted(uint32_t cmdCode,
1053 uint32_t cmdSize,
1054 void *pCmdData,
1055 uint32_t replySize,
1056 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001057 void setEnabled(bool enabled);
1058
1059 // Getters
1060 int id() { return mEffect->id(); }
1061 int priority() { return mPriority; }
1062 bool hasControl() { return mHasControl; }
1063 sp<EffectModule> effect() { return mEffect; }
1064
1065 void dump(char* buffer, size_t size);
1066
1067 protected:
1068
1069 EffectHandle(const EffectHandle&);
1070 EffectHandle& operator =(const EffectHandle&);
1071
1072 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1073 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1074 sp<Client> mClient; // client for shared memory allocation
1075 sp<IMemory> mCblkMemory; // shared memory for control block
1076 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1077 uint8_t* mBuffer; // pointer to parameter area in shared memory
1078 int mPriority; // client application priority to control the effect
1079 bool mHasControl; // true if this handle is controlling the effect
1080 };
1081
1082 // the EffectChain class represents a group of effects associated to one audio session.
1083 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1084 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1085 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1086 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1087 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1088 // input buffer used by the track as accumulation buffer.
1089 class EffectChain: public RefBase {
1090 public:
1091 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1092 ~EffectChain();
1093
1094 void process_l();
1095
1096 void lock() {
1097 mLock.lock();
1098 }
1099 void unlock() {
1100 mLock.unlock();
1101 }
1102
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001103 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001104 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001105
1106 int sessionId() {
1107 return mSessionId;
1108 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001109
Eric Laurent76c40f72010-07-15 12:50:15 -07001110 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1111 sp<EffectModule> getEffectFromId_l(int id);
1112 bool setVolume_l(uint32_t *left, uint32_t *right);
1113 void setDevice_l(uint32_t device);
1114 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001115
Eric Laurent65b65452010-06-01 23:49:17 -07001116 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1117 mInBuffer = buffer;
1118 mOwnInBuffer = ownsBuffer;
1119 }
1120 int16_t *inBuffer() {
1121 return mInBuffer;
1122 }
1123 void setOutBuffer(int16_t *buffer) {
1124 mOutBuffer = buffer;
1125 }
1126 int16_t *outBuffer() {
1127 return mOutBuffer;
1128 }
1129
1130 void startTrack() {mActiveTrackCnt++;}
1131 void stopTrack() {mActiveTrackCnt--;}
1132 int activeTracks() { return mActiveTrackCnt;}
1133
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001134 uint32_t strategy() { return mStrategy; }
1135 void setStrategy(uint32_t strategy)
1136 { mStrategy = strategy; }
1137
Eric Laurent65b65452010-06-01 23:49:17 -07001138 status_t dump(int fd, const Vector<String16>& args);
1139
1140 protected:
1141
1142 EffectChain(const EffectChain&);
1143 EffectChain& operator =(const EffectChain&);
1144
1145 wp<ThreadBase> mThread; // parent mixer thread
1146 Mutex mLock; // mutex protecting effect list
1147 Vector<sp<EffectModule> > mEffects; // list of effect modules
1148 int mSessionId; // audio session ID
1149 int16_t *mInBuffer; // chain input buffer
1150 int16_t *mOutBuffer; // chain output buffer
Eric Laurent65b65452010-06-01 23:49:17 -07001151 int mActiveTrackCnt; // number of active tracks connected
1152 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001153 int mVolumeCtrlIdx; // index of insert effect having control over volume
1154 uint32_t mLeftVolume; // previous volume on left channel
1155 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001156 uint32_t mNewLeftVolume; // new volume on left channel
1157 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001158 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001159 };
1160
Eric Laurenta553c252009-07-17 12:17:14 -07001161 friend class RecordThread;
1162 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001165 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1168
Eric Laurenta553c252009-07-17 12:17:14 -07001169 mutable Mutex mHardwareLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 AudioHardwareInterface* mAudioHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001172
Eric Laurenta553c252009-07-17 12:17:14 -07001173
Eric Laurentddb78e72009-07-28 08:44:33 -07001174 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001175 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
1176 float mMasterVolume;
1177 bool mMasterMute;
1178
Eric Laurentddb78e72009-07-28 08:44:33 -07001179 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001180
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001181 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001182 volatile int32_t mNextUniqueId;
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001183#ifdef LVMX
1184 int mLifeVibesClientPid;
1185#endif
Eric Laurent53334cd2010-06-23 17:38:20 -07001186 uint32_t mMode;
1187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188};
1189
1190// ----------------------------------------------------------------------------
1191
1192}; // namespace android
1193
1194#endif // ANDROID_AUDIO_FLINGER_H