blob: ec3d202cd5c7a6544db5a82dc980c8768c24e2a2 [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 Laurentddb78e72009-07-28 08:44:33 -0700110 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
111 virtual String8 getParameters(int ioHandle, const String8& keys);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurenta553c252009-07-17 12:17:14 -0700114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent47d0a922010-02-26 02:47:27 -0800116 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700117
Eric Laurentddb78e72009-07-28 08:44:33 -0700118 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700119 uint32_t *pSamplingRate,
120 uint32_t *pFormat,
121 uint32_t *pChannels,
122 uint32_t *pLatencyMs,
123 uint32_t flags);
124
Eric Laurentddb78e72009-07-28 08:44:33 -0700125 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700126
Eric Laurentddb78e72009-07-28 08:44:33 -0700127 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700128
Eric Laurentddb78e72009-07-28 08:44:33 -0700129 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700130
Eric Laurentddb78e72009-07-28 08:44:33 -0700131 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700132
Eric Laurentddb78e72009-07-28 08:44:33 -0700133 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700134 uint32_t *pSamplingRate,
135 uint32_t *pFormat,
136 uint32_t *pChannels,
137 uint32_t acoustics);
138
Eric Laurentddb78e72009-07-28 08:44:33 -0700139 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700140
Eric Laurentddb78e72009-07-28 08:44:33 -0700141 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700142
Eric Laurent415f3e22009-10-21 08:14:22 -0700143 virtual status_t setVoiceVolume(float volume);
144
Eric Laurent0986e792010-01-19 17:37:09 -0800145 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
146
Eric Laurent65b65452010-06-01 23:49:17 -0700147 virtual int newAudioSessionId();
148
149 virtual status_t loadEffectLibrary(const char *libPath, int *handle);
150
151 virtual status_t unloadEffectLibrary(int handle);
152
153 virtual status_t queryNumberEffects(uint32_t *numEffects);
154
Eric Laurent53334cd2010-06-23 17:38:20 -0700155 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -0700156
157 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
158
159 virtual sp<IEffect> createEffect(pid_t pid,
160 effect_descriptor_t *pDesc,
161 const sp<IEffectClient>& effectClient,
162 int32_t priority,
163 int output,
164 int sessionId,
165 status_t *status,
166 int *id,
167 int *enabled);
168
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700169 virtual status_t moveEffects(int session, int srcOutput, int dstOutput);
Eric Laurent53334cd2010-06-23 17:38:20 -0700170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 enum hardware_call_state {
172 AUDIO_HW_IDLE = 0,
173 AUDIO_HW_INIT,
174 AUDIO_HW_OUTPUT_OPEN,
175 AUDIO_HW_OUTPUT_CLOSE,
176 AUDIO_HW_INPUT_OPEN,
177 AUDIO_HW_INPUT_CLOSE,
178 AUDIO_HW_STANDBY,
179 AUDIO_HW_SET_MASTER_VOLUME,
180 AUDIO_HW_GET_ROUTING,
181 AUDIO_HW_SET_ROUTING,
182 AUDIO_HW_GET_MODE,
183 AUDIO_HW_SET_MODE,
184 AUDIO_HW_GET_MIC_MUTE,
185 AUDIO_HW_SET_MIC_MUTE,
186 AUDIO_SET_VOICE_VOLUME,
187 AUDIO_SET_PARAMETER,
188 };
189
190 // record interface
191 virtual sp<IAudioRecord> openRecord(
192 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700193 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 uint32_t sampleRate,
195 int format,
196 int channelCount,
197 int frameCount,
198 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700199 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 status_t *status);
201
202 virtual status_t onTransact(
203 uint32_t code,
204 const Parcel& data,
205 Parcel* reply,
206 uint32_t flags);
207
Eric Laurent53334cd2010-06-23 17:38:20 -0700208 uint32_t getMode() { return mMode; }
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210private:
211 AudioFlinger();
212 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
215 // Internal dump utilites.
216 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
217 status_t dumpClients(int fd, const Vector<String16>& args);
218 status_t dumpInternals(int fd, const Vector<String16>& args);
219
220 // --- Client ---
221 class Client : public RefBase {
222 public:
223 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
224 virtual ~Client();
225 const sp<MemoryDealer>& heap() const;
226 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700227 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 private:
230 Client(const Client&);
231 Client& operator = (const Client&);
232 sp<AudioFlinger> mAudioFlinger;
233 sp<MemoryDealer> mMemoryDealer;
234 pid_t mPid;
235 };
236
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700237 // --- Notification Client ---
238 class NotificationClient : public IBinder::DeathRecipient {
239 public:
240 NotificationClient(const sp<AudioFlinger>& audioFlinger,
241 const sp<IAudioFlingerClient>& client,
242 pid_t pid);
243 virtual ~NotificationClient();
244
245 sp<IAudioFlingerClient> client() { return mClient; }
246
247 // IBinder::DeathRecipient
248 virtual void binderDied(const wp<IBinder>& who);
249
250 private:
251 NotificationClient(const NotificationClient&);
252 NotificationClient& operator = (const NotificationClient&);
253
254 sp<AudioFlinger> mAudioFlinger;
255 pid_t mPid;
256 sp<IAudioFlingerClient> mClient;
257 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
259 class TrackHandle;
260 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700261 class RecordThread;
262 class PlaybackThread;
263 class MixerThread;
264 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800265 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700266 class Track;
267 class RecordTrack;
Eric Laurent65b65452010-06-01 23:49:17 -0700268 class EffectModule;
269 class EffectHandle;
270 class EffectChain;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271
Eric Laurenta553c252009-07-17 12:17:14 -0700272 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800274 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700275 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276
Eric Laurent3fdb1262009-11-07 00:01:32 -0800277 status_t dumpBase(int fd, const Vector<String16>& args);
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 // base for record and playback
280 class TrackBase : public AudioBufferProvider, public RefBase {
281
282 public:
283 enum track_state {
284 IDLE,
285 TERMINATED,
286 STOPPED,
287 RESUMING,
288 ACTIVE,
289 PAUSING,
290 PAUSED
291 };
292
293 enum track_flags {
294 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
295 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
296 // The upper 16 bits are used for track-specific flags.
297 };
298
Eric Laurenta553c252009-07-17 12:17:14 -0700299 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 uint32_t sampleRate,
302 int format,
303 int channelCount,
304 int frameCount,
305 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700306 const sp<IMemory>& sharedBuffer,
307 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 ~TrackBase();
309
310 virtual status_t start() = 0;
311 virtual void stop() = 0;
312 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700313 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700314 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700317 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700319 friend class PlaybackThread;
320 friend class RecordThread;
321 friend class MixerThread;
322 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323
324 TrackBase(const TrackBase&);
325 TrackBase& operator = (const TrackBase&);
326
327 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
328 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 int format() const {
331 return mFormat;
332 }
333
334 int channelCount() const ;
335
336 int sampleRate() const;
337
338 void* getBuffer(uint32_t offset, uint32_t frames) const;
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 bool isStopped() const {
341 return mState == STOPPED;
342 }
343
344 bool isTerminated() const {
345 return mState == TERMINATED;
346 }
347
348 bool step();
349 void reset();
350
Eric Laurenta553c252009-07-17 12:17:14 -0700351 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 sp<Client> mClient;
353 sp<IMemory> mCblkMemory;
354 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 void* mBuffer;
356 void* mBufferEnd;
357 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 // we don't really need a lock for these
359 int mState;
360 int mClientTid;
361 uint8_t mFormat;
362 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700363 int mSessionId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 };
365
Eric Laurenta553c252009-07-17 12:17:14 -0700366 class ConfigEvent {
367 public:
368 ConfigEvent() : mEvent(0), mParam(0) {}
369
370 int mEvent;
371 int mParam;
372 };
373
374 uint32_t sampleRate() const;
375 int channelCount() const;
376 int format() const;
377 size_t frameCount() const;
378 void wakeUp() { mWaitWorkCV.broadcast(); }
379 void exit();
380 virtual bool checkForNewParameters_l() = 0;
381 virtual status_t setParameters(const String8& keyValuePairs);
382 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700383 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700384 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700385 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700386 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800387 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800388 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700389
Eric Laurent2c817f52009-07-23 13:17:39 -0700390 mutable Mutex mLock;
391
Eric Laurenta553c252009-07-17 12:17:14 -0700392 protected:
393
394 friend class Track;
395 friend class TrackBase;
396 friend class PlaybackThread;
397 friend class MixerThread;
398 friend class DirectOutputThread;
399 friend class DuplicatingThread;
400 friend class RecordThread;
401 friend class RecordTrack;
402
Eric Laurenta553c252009-07-17 12:17:14 -0700403 Condition mWaitWorkCV;
404 sp<AudioFlinger> mAudioFlinger;
405 uint32_t mSampleRate;
406 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700407 uint32_t mChannels;
408 uint16_t mChannelCount;
409 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700410 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700411 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700412 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700413 status_t mParamStatus;
414 Vector<ConfigEvent *> mConfigEvents;
415 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800416 int mId;
417 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700418 };
419
420 // --- PlaybackThread ---
421 class PlaybackThread : public ThreadBase {
422 public:
423
424 enum type {
425 MIXER,
426 DIRECT,
427 DUPLICATING
428 };
429
Eric Laurent059b4be2009-11-09 23:32:22 -0800430 enum mixer_state {
431 MIXER_IDLE,
432 MIXER_TRACKS_ENABLED,
433 MIXER_TRACKS_READY
434 };
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 // playback track
437 class Track : public TrackBase {
438 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700439 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 const sp<Client>& client,
441 int streamType,
442 uint32_t sampleRate,
443 int format,
444 int channelCount,
445 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700446 const sp<IMemory>& sharedBuffer,
447 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 ~Track();
449
450 void dump(char* buffer, size_t size);
451 virtual status_t start();
452 virtual void stop();
453 void pause();
454
455 void flush();
456 void destroy();
457 void mute(bool);
458 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700459 int name() const {
460 return mName;
461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462
Eric Laurent4bc035a2009-05-22 09:18:15 -0700463 int type() const {
464 return mStreamType;
465 }
Eric Laurent65b65452010-06-01 23:49:17 -0700466 status_t attachAuxEffect(int EffectId);
467 void setAuxBuffer(int EffectId, int32_t *buffer);
468 int32_t *auxBuffer() { return mAuxBuffer; }
469 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
470 int16_t *mainBuffer() { return mMainBuffer; }
471 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700472
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700475 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700477 friend class TrackHandle;
478 friend class PlaybackThread;
479 friend class MixerThread;
480 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481
482 Track(const Track&);
483 Track& operator = (const Track&);
484
485 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700486 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 bool isPausing() const {
488 return mState == PAUSING;
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 bool isPaused() const {
491 return mState == PAUSED;
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 void setPaused() { mState = PAUSED; }
495 void reset();
496
Eric Laurent49f02be2009-11-19 09:00:56 -0800497 bool isOutputTrack() const {
498 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
499 }
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 // we don't really need a lock for these
502 float mVolume[2];
503 volatile bool mMute;
504 // FILLED state is used for suppressing volume ramp at begin of playing
505 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
506 mutable uint8_t mFillingUpStatus;
507 int8_t mRetryCount;
508 sp<IMemory> mSharedBuffer;
509 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700510 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700511 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700512 int16_t *mMainBuffer;
513 int32_t *mAuxBuffer;
514 int mAuxEffectId;
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700515 bool mHasVolumeController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }; // end of Track
517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 // playback track
520 class OutputTrack : public Track {
521 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 class Buffer: public AudioBufferProvider::Buffer {
524 public:
525 int16_t *mBuffer;
526 };
Eric Laurenta553c252009-07-17 12:17:14 -0700527
528 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800529 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 uint32_t sampleRate,
531 int format,
532 int channelCount,
533 int frameCount);
534 ~OutputTrack();
535
536 virtual status_t start();
537 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700538 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700540 bool isActive() { return mActive; }
541 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
543 private:
544
Eric Laurenta553c252009-07-17 12:17:14 -0700545 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700547
548 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800549 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 Vector < Buffer* > mBufferQueue;
552 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700553 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800554 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700555 }; // end of OutputTrack
556
Eric Laurent65b65452010-06-01 23:49:17 -0700557 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700558 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
560 virtual status_t dump(int fd, const Vector<String16>& args);
561
562 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 virtual status_t readyToRun();
564 virtual void onFirstRef();
565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 virtual uint32_t latency() const;
567
568 virtual status_t setMasterVolume(float value);
569 virtual status_t setMasterMute(bool muted);
570
571 virtual float masterVolume() const;
572 virtual bool masterMute() const;
573
574 virtual status_t setStreamVolume(int stream, float value);
575 virtual status_t setStreamMute(int stream, bool muted);
576
577 virtual float streamVolume(int stream) const;
578 virtual bool streamMute(int stream) const;
579
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700580 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 const sp<AudioFlinger::Client>& client,
582 int streamType,
583 uint32_t sampleRate,
584 int format,
585 int channelCount,
586 int frameCount,
587 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700588 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700590
591 AudioStreamOut* getOutput() { return mOutput; }
592
593 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700594 void suspend() { mSuspended++; }
595 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800596 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700597 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700598 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800599 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700600 int16_t *mixBuffer() { return mMixBuffer; };
601
602 sp<EffectHandle> createEffect_l(
603 const sp<AudioFlinger::Client>& client,
604 const sp<IEffectClient>& effectClient,
605 int32_t priority,
606 int sessionId,
607 effect_descriptor_t *desc,
608 int *enabled,
609 status_t *status);
Eric Laurent53334cd2010-06-23 17:38:20 -0700610 void disconnectEffect(const sp< EffectModule>& effect,
611 const wp<EffectHandle>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -0700612
Eric Laurent493941b2010-07-28 01:32:47 -0700613 // return values for hasAudioSession (bit field)
614 enum effect_state {
615 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
616 // effect
617 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
618 // track
619 };
620
621 uint32_t hasAudioSession(int sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700622 sp<EffectChain> getEffectChain(int sessionId);
623 sp<EffectChain> getEffectChain_l(int sessionId);
624 status_t addEffectChain_l(const sp<EffectChain>& chain);
625 size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700626 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
627 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
Eric Laurent65b65452010-06-01 23:49:17 -0700628
629 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
630 void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700631 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
632 int EffectId);
633 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
634 int EffectId);
Eric Laurent53334cd2010-06-23 17:38:20 -0700635 void setMode(uint32_t mode);
Eric Laurenta553c252009-07-17 12:17:14 -0700636
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700637 status_t addEffect_l(const sp< EffectModule>& effect);
638 void removeEffect_l(const sp< EffectModule>& effect);
639
640 uint32_t getStrategyForSession_l(int sessionId);
641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 struct stream_type_t {
643 stream_type_t()
644 : volume(1.0f),
645 mute(false)
646 {
647 }
648 float volume;
649 bool mute;
650 };
651
Eric Laurent2c817f52009-07-23 13:17:39 -0700652 protected:
653 int mType;
654 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700655 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700656 int mBytesWritten;
657 bool mMasterMute;
658 SortedVector< wp<Track> > mActiveTracks;
659
Eric Laurent62443f52009-10-05 20:29:18 -0700660 virtual int getTrackName_l() = 0;
661 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800662 virtual uint32_t activeSleepTimeUs() = 0;
663 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent8448a792010-08-18 18:13:17 -0700664 virtual uint32_t suspendSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 private:
667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700669 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 friend class Track;
671 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700672 friend class MixerThread;
673 friend class DirectOutputThread;
674 friend class DuplicatingThread;
675
676 PlaybackThread(const Client&);
677 PlaybackThread& operator = (const PlaybackThread&);
678
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700679 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700680 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700681
Eric Laurenta553c252009-07-17 12:17:14 -0700682 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
Eric Laurent65b65452010-06-01 23:49:17 -0700684 uint32_t device() { return mDevice; }
685
Eric Laurenta553c252009-07-17 12:17:14 -0700686 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700688 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700691 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
692 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 nsecs_t mLastWriteTime;
696 int mNumWrites;
697 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700699 Vector< sp<EffectChain> > mEffectChains;
700 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 };
702
Eric Laurenta553c252009-07-17 12:17:14 -0700703 class MixerThread : public PlaybackThread {
704 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700705 MixerThread (const sp<AudioFlinger>& audioFlinger,
706 AudioStreamOut* output,
707 int id,
708 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700709 virtual ~MixerThread();
710
711 // Thread virtuals
712 virtual bool threadLoop();
713
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700714 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700715 virtual bool checkForNewParameters_l();
716 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
717
718 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700719 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
720 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700721 virtual int getTrackName_l();
722 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800723 virtual uint32_t activeSleepTimeUs();
724 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700725 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700726
727 AudioMixer* mAudioMixer;
728 };
729
730 class DirectOutputThread : public PlaybackThread {
731 public:
732
Eric Laurent65b65452010-06-01 23:49:17 -0700733 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700734 ~DirectOutputThread();
735
736 // Thread virtuals
737 virtual bool threadLoop();
738
Eric Laurent62443f52009-10-05 20:29:18 -0700739 virtual bool checkForNewParameters_l();
740
741 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700742 virtual int getTrackName_l();
743 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800744 virtual uint32_t activeSleepTimeUs();
745 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700746 virtual uint32_t suspendSleepTimeUs();
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 Laurentf3d6dd02010-11-18 08:40:16 -0800784 int nextUniqueId_l();
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 Laurent9cc489a2009-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();
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700965 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -0700966
967 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
968 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
969 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
970 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700971 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
972 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -0700973
974 status_t addHandle(sp<EffectHandle>& handle);
975 void disconnect(const wp<EffectHandle>& handle);
976 size_t removeHandle (const wp<EffectHandle>& handle);
977
978 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700979 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700980
981 status_t setDevice(uint32_t device);
982 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700983 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700984
985 status_t dump(int fd, const Vector<String16>& args);
986
987 protected:
988
Eric Laurent7d850f22010-07-09 13:34:17 -0700989 // Maximum time allocated to effect engines to complete the turn off sequence
990 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
991
Eric Laurent65b65452010-06-01 23:49:17 -0700992 EffectModule(const EffectModule&);
993 EffectModule& operator = (const EffectModule&);
994
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700995 status_t start_l();
996 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700997
Eric Laurent53334cd2010-06-23 17:38:20 -0700998 // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified
999 static const uint32_t sDeviceConvTable[];
1000 static uint32_t deviceAudioSystemToEffectApi(uint32_t device);
1001
1002 // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified
1003 static const uint32_t sModeConvTable[];
1004 static int modeAudioSystemToEffectApi(uint32_t mode);
1005
Eric Laurent65b65452010-06-01 23:49:17 -07001006 Mutex mLock; // mutex for process, commands and handles list protection
1007 wp<ThreadBase> mThread; // parent thread
1008 wp<EffectChain> mChain; // parent effect chain
1009 int mId; // this instance unique ID
1010 int mSessionId; // audio session ID
1011 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1012 effect_config_t mConfig; // input and output audio configuration
1013 effect_interface_t mEffectInterface; // Effect module C API
1014 status_t mStatus; // initialization status
1015 uint32_t mState; // current activation state (effect_state)
1016 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001017 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1018 // sending disable command.
1019 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001020 };
1021
1022 // The EffectHandle class implements the IEffect interface. It provides resources
1023 // to receive parameter updates, keeps track of effect control
1024 // ownership and state and has a pointer to the EffectModule object it is controlling.
1025 // There is one EffectHandle object for each application controlling (or using)
1026 // an effect module.
1027 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1028 class EffectHandle: public android::BnEffect {
1029 public:
1030
1031 EffectHandle(const sp<EffectModule>& effect,
1032 const sp<AudioFlinger::Client>& client,
1033 const sp<IEffectClient>& effectClient,
1034 int32_t priority);
1035 virtual ~EffectHandle();
1036
1037 // IEffect
1038 virtual status_t enable();
1039 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001040 virtual status_t command(uint32_t cmdCode,
1041 uint32_t cmdSize,
1042 void *pCmdData,
1043 uint32_t *replySize,
1044 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001045 virtual void disconnect();
1046 virtual sp<IMemory> getCblk() const;
1047 virtual status_t onTransact(uint32_t code, const Parcel& data,
1048 Parcel* reply, uint32_t flags);
1049
1050
1051 // Give or take control of effect module
1052 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001053 void commandExecuted(uint32_t cmdCode,
1054 uint32_t cmdSize,
1055 void *pCmdData,
1056 uint32_t replySize,
1057 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001058 void setEnabled(bool enabled);
1059
1060 // Getters
1061 int id() { return mEffect->id(); }
1062 int priority() { return mPriority; }
1063 bool hasControl() { return mHasControl; }
1064 sp<EffectModule> effect() { return mEffect; }
1065
1066 void dump(char* buffer, size_t size);
1067
1068 protected:
1069
1070 EffectHandle(const EffectHandle&);
1071 EffectHandle& operator =(const EffectHandle&);
1072
1073 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1074 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1075 sp<Client> mClient; // client for shared memory allocation
1076 sp<IMemory> mCblkMemory; // shared memory for control block
1077 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1078 uint8_t* mBuffer; // pointer to parameter area in shared memory
1079 int mPriority; // client application priority to control the effect
1080 bool mHasControl; // true if this handle is controlling the effect
1081 };
1082
1083 // the EffectChain class represents a group of effects associated to one audio session.
1084 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1085 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1086 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1087 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1088 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1089 // input buffer used by the track as accumulation buffer.
1090 class EffectChain: public RefBase {
1091 public:
1092 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1093 ~EffectChain();
1094
1095 void process_l();
1096
1097 void lock() {
1098 mLock.lock();
1099 }
1100 void unlock() {
1101 mLock.unlock();
1102 }
1103
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001104 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001105 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001106
1107 int sessionId() {
1108 return mSessionId;
1109 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001110
Eric Laurent76c40f72010-07-15 12:50:15 -07001111 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1112 sp<EffectModule> getEffectFromId_l(int id);
1113 bool setVolume_l(uint32_t *left, uint32_t *right);
1114 void setDevice_l(uint32_t device);
1115 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001116
Eric Laurent65b65452010-06-01 23:49:17 -07001117 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1118 mInBuffer = buffer;
1119 mOwnInBuffer = ownsBuffer;
1120 }
1121 int16_t *inBuffer() {
1122 return mInBuffer;
1123 }
1124 void setOutBuffer(int16_t *buffer) {
1125 mOutBuffer = buffer;
1126 }
1127 int16_t *outBuffer() {
1128 return mOutBuffer;
1129 }
1130
1131 void startTrack() {mActiveTrackCnt++;}
1132 void stopTrack() {mActiveTrackCnt--;}
1133 int activeTracks() { return mActiveTrackCnt;}
1134
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001135 uint32_t strategy() { return mStrategy; }
1136 void setStrategy(uint32_t strategy)
1137 { mStrategy = strategy; }
1138
Eric Laurent65b65452010-06-01 23:49:17 -07001139 status_t dump(int fd, const Vector<String16>& args);
1140
1141 protected:
1142
1143 EffectChain(const EffectChain&);
1144 EffectChain& operator =(const EffectChain&);
1145
1146 wp<ThreadBase> mThread; // parent mixer thread
1147 Mutex mLock; // mutex protecting effect list
1148 Vector<sp<EffectModule> > mEffects; // list of effect modules
1149 int mSessionId; // audio session ID
1150 int16_t *mInBuffer; // chain input buffer
1151 int16_t *mOutBuffer; // chain output buffer
Eric Laurent65b65452010-06-01 23:49:17 -07001152 int mActiveTrackCnt; // number of active tracks connected
1153 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001154 int mVolumeCtrlIdx; // index of insert effect having control over volume
1155 uint32_t mLeftVolume; // previous volume on left channel
1156 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001157 uint32_t mNewLeftVolume; // new volume on left channel
1158 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001159 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001160 };
1161
Eric Laurenta553c252009-07-17 12:17:14 -07001162 friend class RecordThread;
1163 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001166 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1169
Eric Laurenta553c252009-07-17 12:17:14 -07001170 mutable Mutex mHardwareLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 AudioHardwareInterface* mAudioHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001173
Eric Laurenta553c252009-07-17 12:17:14 -07001174
Eric Laurentddb78e72009-07-28 08:44:33 -07001175 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001176 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
1177 float mMasterVolume;
1178 bool mMasterMute;
1179
Eric Laurentddb78e72009-07-28 08:44:33 -07001180 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001181
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001182 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001183 volatile int32_t mNextUniqueId;
Eric Laurent53334cd2010-06-23 17:38:20 -07001184 uint32_t mMode;
1185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186};
1187
1188// ----------------------------------------------------------------------------
1189
1190}; // namespace android
1191
1192#endif // ANDROID_AUDIO_FLINGER_H