blob: 9817c9042aeeef0d041a748809fd7243c6890fbf [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>
Dima Zavin31f188892011-04-18 16:57:27 -070035#include <utils/TypeHelpers.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/Vector.h>
37
Mathias Agopian24651682010-07-14 18:41:18 -070038#include <binder/BinderService.h>
39#include <binder/MemoryDealer.h>
40
Dima Zavin34bb4192011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070042#include <hardware/audio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include "AudioBufferProvider.h"
45
46namespace android {
47
48class audio_track_cblk_t;
Eric Laurent65b65452010-06-01 23:49:17 -070049class effect_param_cblk_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050class AudioMixer;
51class AudioBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -070052class AudioResampler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054// ----------------------------------------------------------------------------
55
56#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
57#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
58
59
60// ----------------------------------------------------------------------------
61
62static const nsecs_t kStandbyTimeInNsecs = seconds(3);
63
Mathias Agopian24651682010-07-14 18:41:18 -070064class AudioFlinger :
65 public BinderService<AudioFlinger>,
66 public BnAudioFlinger
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067{
Mathias Agopian24651682010-07-14 18:41:18 -070068 friend class BinderService<AudioFlinger>;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069public:
Mathias Agopian24651682010-07-14 18:41:18 -070070 static char const* getServiceName() { return "media.audio_flinger"; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72 virtual status_t dump(int fd, const Vector<String16>& args);
73
74 // IAudioFlinger interface
75 virtual sp<IAudioTrack> createTrack(
76 pid_t pid,
77 int streamType,
78 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -070079 uint32_t format,
80 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 int frameCount,
82 uint32_t flags,
83 const sp<IMemory>& sharedBuffer,
Eric Laurentddb78e72009-07-28 08:44:33 -070084 int output,
Eric Laurent65b65452010-06-01 23:49:17 -070085 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 status_t *status);
87
Eric Laurentddb78e72009-07-28 08:44:33 -070088 virtual uint32_t sampleRate(int output) const;
89 virtual int channelCount(int output) const;
Jean-Michel Trivi54392232011-05-24 15:53:33 -070090 virtual uint32_t format(int output) const;
Eric Laurentddb78e72009-07-28 08:44:33 -070091 virtual size_t frameCount(int output) const;
92 virtual uint32_t latency(int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 virtual status_t setMasterVolume(float value);
95 virtual status_t setMasterMute(bool muted);
96
97 virtual float masterVolume() const;
98 virtual bool masterMute() const;
99
Eric Laurentddb78e72009-07-28 08:44:33 -0700100 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 virtual status_t setStreamMute(int stream, bool muted);
102
Eric Laurentddb78e72009-07-28 08:44:33 -0700103 virtual float streamVolume(int stream, int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 virtual bool streamMute(int stream) const;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 virtual status_t setMode(int mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 virtual status_t setMicMute(bool state);
109 virtual bool getMicMute() const;
110
Eric Laurentddb78e72009-07-28 08:44:33 -0700111 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
112 virtual String8 getParameters(int ioHandle, const String8& keys);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurenta553c252009-07-17 12:17:14 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent47d0a922010-02-26 02:47:27 -0800117 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700118
Eric Laurentddb78e72009-07-28 08:44:33 -0700119 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700120 uint32_t *pSamplingRate,
121 uint32_t *pFormat,
122 uint32_t *pChannels,
123 uint32_t *pLatencyMs,
124 uint32_t flags);
125
Eric Laurentddb78e72009-07-28 08:44:33 -0700126 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700127
Eric Laurentddb78e72009-07-28 08:44:33 -0700128 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700129
Eric Laurentddb78e72009-07-28 08:44:33 -0700130 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700131
Eric Laurentddb78e72009-07-28 08:44:33 -0700132 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700133
Eric Laurentddb78e72009-07-28 08:44:33 -0700134 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700135 uint32_t *pSamplingRate,
136 uint32_t *pFormat,
137 uint32_t *pChannels,
138 uint32_t acoustics);
139
Eric Laurentddb78e72009-07-28 08:44:33 -0700140 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700141
Eric Laurentddb78e72009-07-28 08:44:33 -0700142 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700143
Eric Laurent415f3e22009-10-21 08:14:22 -0700144 virtual status_t setVoiceVolume(float volume);
145
Eric Laurent0986e792010-01-19 17:37:09 -0800146 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
147
Eric Laurent65b65452010-06-01 23:49:17 -0700148 virtual int newAudioSessionId();
149
Eric Laurent65b65452010-06-01 23:49:17 -0700150 virtual status_t queryNumberEffects(uint32_t *numEffects);
151
Eric Laurent53334cd2010-06-23 17:38:20 -0700152 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -0700153
154 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
155
156 virtual sp<IEffect> createEffect(pid_t pid,
157 effect_descriptor_t *pDesc,
158 const sp<IEffectClient>& effectClient,
159 int32_t priority,
Eric Laurent464d5b32011-06-17 21:29:58 -0700160 int io,
Eric Laurent65b65452010-06-01 23:49:17 -0700161 int sessionId,
162 status_t *status,
163 int *id,
164 int *enabled);
165
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700166 virtual status_t moveEffects(int session, int srcOutput, int dstOutput);
Eric Laurent53334cd2010-06-23 17:38:20 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 enum hardware_call_state {
169 AUDIO_HW_IDLE = 0,
170 AUDIO_HW_INIT,
171 AUDIO_HW_OUTPUT_OPEN,
172 AUDIO_HW_OUTPUT_CLOSE,
173 AUDIO_HW_INPUT_OPEN,
174 AUDIO_HW_INPUT_CLOSE,
175 AUDIO_HW_STANDBY,
176 AUDIO_HW_SET_MASTER_VOLUME,
177 AUDIO_HW_GET_ROUTING,
178 AUDIO_HW_SET_ROUTING,
179 AUDIO_HW_GET_MODE,
180 AUDIO_HW_SET_MODE,
181 AUDIO_HW_GET_MIC_MUTE,
182 AUDIO_HW_SET_MIC_MUTE,
183 AUDIO_SET_VOICE_VOLUME,
184 AUDIO_SET_PARAMETER,
185 };
186
187 // record interface
188 virtual sp<IAudioRecord> openRecord(
189 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700190 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700192 uint32_t format,
193 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 int frameCount,
195 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700196 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 status_t *status);
198
199 virtual status_t onTransact(
200 uint32_t code,
201 const Parcel& data,
202 Parcel* reply,
203 uint32_t flags);
204
Eric Laurent53334cd2010-06-23 17:38:20 -0700205 uint32_t getMode() { return mMode; }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207private:
208 AudioFlinger();
209 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
Dima Zavin2986f5b2011-04-19 19:04:32 -0700211 status_t initCheck() const;
212 virtual void onFirstRef();
Dima Zavin31f188892011-04-18 16:57:27 -0700213 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
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;
Dima Zavin31f188892011-04-18 16:57:27 -0700271 struct AudioStreamOut;
272 struct AudioStreamIn;
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 Laurent464d5b32011-06-17 21:29:58 -0700276 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700277 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
Eric Laurent464d5b32011-06-17 21:29:58 -0700279
280 enum type {
281 MIXER, // Thread class is MixerThread
282 DIRECT, // Thread class is DirectOutputThread
283 DUPLICATING, // Thread class is DuplicatingThread
284 RECORD // Thread class is RecordThread
285 };
286
Eric Laurent3fdb1262009-11-07 00:01:32 -0800287 status_t dumpBase(int fd, const Vector<String16>& args);
Eric Laurent1345d332011-07-24 17:49:51 -0700288 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurent3fdb1262009-11-07 00:01:32 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 // base for record and playback
291 class TrackBase : public AudioBufferProvider, public RefBase {
292
293 public:
294 enum track_state {
295 IDLE,
296 TERMINATED,
297 STOPPED,
298 RESUMING,
299 ACTIVE,
300 PAUSING,
301 PAUSED
302 };
303
304 enum track_flags {
305 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
306 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
307 // The upper 16 bits are used for track-specific flags.
308 };
309
Eric Laurenta553c252009-07-17 12:17:14 -0700310 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700313 uint32_t format,
314 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 int frameCount,
316 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700317 const sp<IMemory>& sharedBuffer,
318 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 ~TrackBase();
320
321 virtual status_t start() = 0;
322 virtual void stop() = 0;
323 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700324 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700325 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
327 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700328 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700330 friend class PlaybackThread;
331 friend class RecordThread;
332 friend class MixerThread;
333 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
335 TrackBase(const TrackBase&);
336 TrackBase& operator = (const TrackBase&);
337
338 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
339 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
340
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700341 uint32_t format() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 return mFormat;
343 }
344
345 int channelCount() const ;
346
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700347 uint32_t channelMask() const;
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 int sampleRate() const;
350
351 void* getBuffer(uint32_t offset, uint32_t frames) const;
352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 bool isStopped() const {
354 return mState == STOPPED;
355 }
356
357 bool isTerminated() const {
358 return mState == TERMINATED;
359 }
360
361 bool step();
362 void reset();
363
Eric Laurenta553c252009-07-17 12:17:14 -0700364 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 sp<Client> mClient;
366 sp<IMemory> mCblkMemory;
367 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 void* mBuffer;
369 void* mBufferEnd;
370 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 // we don't really need a lock for these
372 int mState;
373 int mClientTid;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700374 uint32_t mFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700376 int mSessionId;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700377 uint8_t mChannelCount;
378 uint32_t mChannelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 };
380
Eric Laurenta553c252009-07-17 12:17:14 -0700381 class ConfigEvent {
382 public:
383 ConfigEvent() : mEvent(0), mParam(0) {}
384
385 int mEvent;
386 int mParam;
387 };
388
Eric Laurent464d5b32011-06-17 21:29:58 -0700389 virtual status_t initCheck() const = 0;
390 int type() const { return mType; }
Eric Laurenta553c252009-07-17 12:17:14 -0700391 uint32_t sampleRate() const;
392 int channelCount() const;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700393 uint32_t format() const;
Eric Laurenta553c252009-07-17 12:17:14 -0700394 size_t frameCount() const;
395 void wakeUp() { mWaitWorkCV.broadcast(); }
396 void exit();
397 virtual bool checkForNewParameters_l() = 0;
398 virtual status_t setParameters(const String8& keyValuePairs);
399 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700400 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700401 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700402 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700403 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800404 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800405 bool standby() { return mStandby; }
Eric Laurent464d5b32011-06-17 21:29:58 -0700406 uint32_t device() { return mDevice; }
407 virtual audio_stream_t* stream() = 0;
408
409 sp<EffectHandle> createEffect_l(
410 const sp<AudioFlinger::Client>& client,
411 const sp<IEffectClient>& effectClient,
412 int32_t priority,
413 int sessionId,
414 effect_descriptor_t *desc,
415 int *enabled,
416 status_t *status);
417 void disconnectEffect(const sp< EffectModule>& effect,
418 const wp<EffectHandle>& handle);
419
420 // return values for hasAudioSession (bit field)
421 enum effect_state {
422 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
423 // effect
424 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
425 // track
426 };
427
428 // get effect chain corresponding to session Id.
429 sp<EffectChain> getEffectChain(int sessionId);
430 // same as getEffectChain() but must be called with ThreadBase mutex locked
431 sp<EffectChain> getEffectChain_l(int sessionId);
432 // add an effect chain to the chain list (mEffectChains)
433 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
434 // remove an effect chain from the chain list (mEffectChains)
435 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
436 // lock mall effect chains Mutexes. Must be called before releasing the
437 // ThreadBase mutex before processing the mixer and effects. This guarantees the
438 // integrity of the chains during the process.
439 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
440 // unlock effect chains after process
441 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
442 // set audio mode to all effect chains
443 void setMode(uint32_t mode);
444 // get effect module with corresponding ID on specified audio session
445 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
446 // add and effect module. Also creates the effect chain is none exists for
447 // the effects audio session
448 status_t addEffect_l(const sp< EffectModule>& effect);
449 // remove and effect module. Also removes the effect chain is this was the last
450 // effect
451 void removeEffect_l(const sp< EffectModule>& effect);
452 // detach all tracks connected to an auxiliary effect
453 virtual void detachAuxEffect_l(int effectId) {}
454 // returns either EFFECT_SESSION if effects on this audio session exist in one
455 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
456 virtual uint32_t hasAudioSession(int sessionId) = 0;
457 // the value returned by default implementation is not important as the
458 // strategy is only meaningful for PlaybackThread which implements this method
459 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Eric Laurenta553c252009-07-17 12:17:14 -0700460
Eric Laurent2c817f52009-07-23 13:17:39 -0700461 mutable Mutex mLock;
462
Eric Laurenta553c252009-07-17 12:17:14 -0700463 protected:
464
465 friend class Track;
466 friend class TrackBase;
467 friend class PlaybackThread;
468 friend class MixerThread;
469 friend class DirectOutputThread;
470 friend class DuplicatingThread;
471 friend class RecordThread;
472 friend class RecordTrack;
473
Eric Laurent464d5b32011-06-17 21:29:58 -0700474 int mType;
Eric Laurenta553c252009-07-17 12:17:14 -0700475 Condition mWaitWorkCV;
476 sp<AudioFlinger> mAudioFlinger;
477 uint32_t mSampleRate;
478 size_t mFrameCount;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700479 uint32_t mChannelMask;
Eric Laurentb0a01472010-05-14 05:45:46 -0700480 uint16_t mChannelCount;
481 uint16_t mFrameSize;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700482 uint32_t mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700483 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700484 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700485 status_t mParamStatus;
486 Vector<ConfigEvent *> mConfigEvents;
487 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800488 int mId;
489 bool mExiting;
Eric Laurent464d5b32011-06-17 21:29:58 -0700490 Vector< sp<EffectChain> > mEffectChains;
491 uint32_t mDevice; // output device for PlaybackThread
492 // input + output devices for RecordThread
Eric Laurenta553c252009-07-17 12:17:14 -0700493 };
494
495 // --- PlaybackThread ---
496 class PlaybackThread : public ThreadBase {
497 public:
498
Eric Laurent059b4be2009-11-09 23:32:22 -0800499 enum mixer_state {
500 MIXER_IDLE,
501 MIXER_TRACKS_ENABLED,
502 MIXER_TRACKS_READY
503 };
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 // playback track
506 class Track : public TrackBase {
507 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700508 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 const sp<Client>& client,
510 int streamType,
511 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700512 uint32_t format,
513 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700515 const sp<IMemory>& sharedBuffer,
516 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 ~Track();
518
519 void dump(char* buffer, size_t size);
520 virtual status_t start();
521 virtual void stop();
522 void pause();
523
524 void flush();
525 void destroy();
526 void mute(bool);
527 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700528 int name() const {
529 return mName;
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
Eric Laurent4bc035a2009-05-22 09:18:15 -0700532 int type() const {
533 return mStreamType;
534 }
Eric Laurent65b65452010-06-01 23:49:17 -0700535 status_t attachAuxEffect(int EffectId);
536 void setAuxBuffer(int EffectId, int32_t *buffer);
537 int32_t *auxBuffer() { return mAuxBuffer; }
538 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
539 int16_t *mainBuffer() { return mMainBuffer; }
540 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700541
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700544 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700546 friend class TrackHandle;
547 friend class PlaybackThread;
548 friend class MixerThread;
549 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550
551 Track(const Track&);
552 Track& operator = (const Track&);
553
554 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700555 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 bool isPausing() const {
557 return mState == PAUSING;
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 bool isPaused() const {
560 return mState == PAUSED;
561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 void setPaused() { mState = PAUSED; }
564 void reset();
565
Eric Laurent49f02be2009-11-19 09:00:56 -0800566 bool isOutputTrack() const {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700567 return (mStreamType == AUDIO_STREAM_CNT);
Eric Laurent49f02be2009-11-19 09:00:56 -0800568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 // we don't really need a lock for these
571 float mVolume[2];
572 volatile bool mMute;
573 // FILLED state is used for suppressing volume ramp at begin of playing
574 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
575 mutable uint8_t mFillingUpStatus;
576 int8_t mRetryCount;
577 sp<IMemory> mSharedBuffer;
578 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700579 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700580 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700581 int16_t *mMainBuffer;
582 int32_t *mAuxBuffer;
583 int mAuxEffectId;
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700584 bool mHasVolumeController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }; // end of Track
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587
588 // playback track
589 class OutputTrack : public Track {
590 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 class Buffer: public AudioBufferProvider::Buffer {
593 public:
594 int16_t *mBuffer;
595 };
Eric Laurenta553c252009-07-17 12:17:14 -0700596
597 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800598 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700600 uint32_t format,
601 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 int frameCount);
603 ~OutputTrack();
604
605 virtual status_t start();
606 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700607 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700609 bool isActive() { return mActive; }
610 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611
612 private:
613
Eric Laurenta553c252009-07-17 12:17:14 -0700614 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700616
617 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800618 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 Vector < Buffer* > mBufferQueue;
621 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700622 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800623 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700624 }; // end of OutputTrack
625
Dima Zavin31f188892011-04-18 16:57:27 -0700626 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700627 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 virtual status_t dump(int fd, const Vector<String16>& args);
630
631 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 virtual status_t readyToRun();
633 virtual void onFirstRef();
634
Eric Laurent464d5b32011-06-17 21:29:58 -0700635 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 virtual uint32_t latency() const;
638
639 virtual status_t setMasterVolume(float value);
640 virtual status_t setMasterMute(bool muted);
641
642 virtual float masterVolume() const;
643 virtual bool masterMute() const;
644
645 virtual status_t setStreamVolume(int stream, float value);
646 virtual status_t setStreamMute(int stream, bool muted);
647
648 virtual float streamVolume(int stream) const;
649 virtual bool streamMute(int stream) const;
650
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700651 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 const sp<AudioFlinger::Client>& client,
653 int streamType,
654 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700655 uint32_t format,
656 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 int frameCount,
658 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700659 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700661
Dima Zavin31f188892011-04-18 16:57:27 -0700662 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurent464d5b32011-06-17 21:29:58 -0700663 virtual audio_stream_t* stream() { return &mOutput->stream->common; }
Eric Laurenta553c252009-07-17 12:17:14 -0700664
Eric Laurentd5603c12009-08-06 08:49:39 -0700665 void suspend() { mSuspended++; }
666 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800667 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700668 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700669 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800670 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700671 int16_t *mixBuffer() { return mMixBuffer; };
672
Eric Laurent464d5b32011-06-17 21:29:58 -0700673 virtual void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700674 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
675 int EffectId);
676 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
677 int EffectId);
Eric Laurenta553c252009-07-17 12:17:14 -0700678
Eric Laurent464d5b32011-06-17 21:29:58 -0700679 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
680 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
681 virtual uint32_t hasAudioSession(int sessionId);
682 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 struct stream_type_t {
685 stream_type_t()
686 : volume(1.0f),
687 mute(false)
688 {
689 }
690 float volume;
691 bool mute;
692 };
693
Eric Laurent2c817f52009-07-23 13:17:39 -0700694 protected:
Eric Laurent2c817f52009-07-23 13:17:39 -0700695 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700696 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700697 int mBytesWritten;
698 bool mMasterMute;
699 SortedVector< wp<Track> > mActiveTracks;
700
Eric Laurent62443f52009-10-05 20:29:18 -0700701 virtual int getTrackName_l() = 0;
702 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800703 virtual uint32_t activeSleepTimeUs() = 0;
704 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent8448a792010-08-18 18:13:17 -0700705 virtual uint32_t suspendSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 private:
708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700710 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 friend class Track;
712 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700713 friend class MixerThread;
714 friend class DirectOutputThread;
715 friend class DuplicatingThread;
716
717 PlaybackThread(const Client&);
718 PlaybackThread& operator = (const PlaybackThread&);
719
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700720 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700721 void destroyTrack_l(const sp<Track>& track);
Eric Laurent90681d62011-05-09 12:09:06 -0700722 void removeTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700723
Eric Laurenta553c252009-07-17 12:17:14 -0700724 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725
Eric Laurenta553c252009-07-17 12:17:14 -0700726 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700730 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700731 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin31f188892011-04-18 16:57:27 -0700732 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 nsecs_t mLastWriteTime;
735 int mNumWrites;
736 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 bool mInWrite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 };
739
Eric Laurenta553c252009-07-17 12:17:14 -0700740 class MixerThread : public PlaybackThread {
741 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700742 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700743 AudioStreamOut* output,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700744 int id,
745 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700746 virtual ~MixerThread();
747
748 // Thread virtuals
749 virtual bool threadLoop();
750
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700751 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700752 virtual bool checkForNewParameters_l();
753 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
754
755 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700756 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
757 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700758 virtual int getTrackName_l();
759 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800760 virtual uint32_t activeSleepTimeUs();
761 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700762 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700763
764 AudioMixer* mAudioMixer;
765 };
766
767 class DirectOutputThread : public PlaybackThread {
768 public:
769
Dima Zavin31f188892011-04-18 16:57:27 -0700770 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700771 ~DirectOutputThread();
772
773 // Thread virtuals
774 virtual bool threadLoop();
775
Eric Laurent62443f52009-10-05 20:29:18 -0700776 virtual bool checkForNewParameters_l();
777
778 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700779 virtual int getTrackName_l();
780 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800781 virtual uint32_t activeSleepTimeUs();
782 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700783 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700784
785 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700786 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
787
788 float mLeftVolFloat;
789 float mRightVolFloat;
790 uint16_t mLeftVolShort;
791 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700792 };
793
794 class DuplicatingThread : public MixerThread {
795 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800796 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700797 ~DuplicatingThread();
798
799 // Thread virtuals
800 virtual bool threadLoop();
801 void addOutputTrack(MixerThread* thread);
802 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800803 uint32_t waitTimeMs() { return mWaitTimeMs; }
804 protected:
805 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700806
807 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800808 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
809 void updateWaitTime();
810
Eric Laurenta553c252009-07-17 12:17:14 -0700811 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800812 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700813 };
814
Eric Laurentddb78e72009-07-28 08:44:33 -0700815 PlaybackThread *checkPlaybackThread_l(int output) const;
816 MixerThread *checkMixerThread_l(int output) const;
817 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700818 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800819 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700820
Eric Laurent464d5b32011-06-17 21:29:58 -0700821 uint32_t nextUniqueId();
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700822 status_t moveEffectChain_l(int session,
823 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -0700824 AudioFlinger::PlaybackThread *dstThread,
825 bool reRegister);
Eric Laurent464d5b32011-06-17 21:29:58 -0700826 PlaybackThread *primaryPlaybackThread_l();
827 uint32_t primaryOutputDevice_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 friend class AudioBuffer;
830
831 class TrackHandle : public android::BnAudioTrack {
832 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700833 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 virtual ~TrackHandle();
835 virtual status_t start();
836 virtual void stop();
837 virtual void flush();
838 virtual void mute(bool);
839 virtual void pause();
840 virtual void setVolume(float left, float right);
841 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700842 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 virtual status_t onTransact(
844 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
845 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700846 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 };
848
849 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700850 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851
852
Eric Laurentb9481d82009-09-17 05:12:56 -0700853 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700854 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
856
Eric Laurenta553c252009-07-17 12:17:14 -0700857 // record thread
858 class RecordThread : public ThreadBase, public AudioBufferProvider
859 {
860 public:
861
862 // record track
863 class RecordTrack : public TrackBase {
864 public:
865 RecordTrack(const wp<ThreadBase>& thread,
866 const sp<Client>& client,
867 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700868 uint32_t format,
869 uint32_t channelMask,
Eric Laurenta553c252009-07-17 12:17:14 -0700870 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700871 uint32_t flags,
872 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700873 ~RecordTrack();
874
875 virtual status_t start();
876 virtual void stop();
877
878 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
879 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
880
Eric Laurent3fdb1262009-11-07 00:01:32 -0800881 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700882 private:
883 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700884 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700885
886 RecordTrack(const RecordTrack&);
887 RecordTrack& operator = (const RecordTrack&);
888
889 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
890
891 bool mOverflow;
892 };
893
894
895 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700896 AudioStreamIn *input,
Eric Laurenta553c252009-07-17 12:17:14 -0700897 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800898 uint32_t channels,
Eric Laurent464d5b32011-06-17 21:29:58 -0700899 int id,
900 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700901 ~RecordThread();
902
903 virtual bool threadLoop();
904 virtual status_t readyToRun() { return NO_ERROR; }
905 virtual void onFirstRef();
906
Eric Laurent464d5b32011-06-17 21:29:58 -0700907 virtual status_t initCheck() const { return (mInput == 0) ? NO_INIT : NO_ERROR; }
908 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
909 const sp<AudioFlinger::Client>& client,
910 uint32_t sampleRate,
911 int format,
912 int channelMask,
913 int frameCount,
914 uint32_t flags,
915 int sessionId,
916 status_t *status);
917
Eric Laurenta553c252009-07-17 12:17:14 -0700918 status_t start(RecordTrack* recordTrack);
919 void stop(RecordTrack* recordTrack);
920 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin31f188892011-04-18 16:57:27 -0700921 AudioStreamIn* getInput() { return mInput; }
Eric Laurent464d5b32011-06-17 21:29:58 -0700922 virtual audio_stream_t* stream() { return &mInput->stream->common; }
Eric Laurenta553c252009-07-17 12:17:14 -0700923
Eric Laurent464d5b32011-06-17 21:29:58 -0700924
925 void setTrack(RecordTrack *recordTrack) { mTrack = recordTrack; }
Eric Laurenta553c252009-07-17 12:17:14 -0700926 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
927 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
928 virtual bool checkForNewParameters_l();
929 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700930 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700931 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800932 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700933
Eric Laurent464d5b32011-06-17 21:29:58 -0700934 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
935 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
936 virtual uint32_t hasAudioSession(int sessionId);
937
Eric Laurenta553c252009-07-17 12:17:14 -0700938 private:
939 RecordThread();
Dima Zavin31f188892011-04-18 16:57:27 -0700940 AudioStreamIn *mInput;
Eric Laurent464d5b32011-06-17 21:29:58 -0700941 RecordTrack* mTrack;
Eric Laurenta553c252009-07-17 12:17:14 -0700942 sp<RecordTrack> mActiveTrack;
943 Condition mStartStopCond;
944 AudioResampler *mResampler;
945 int32_t *mRsmpOutBuffer;
946 int16_t *mRsmpInBuffer;
947 size_t mRsmpInIndex;
948 size_t mInputBytes;
949 int mReqChannelCount;
950 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800951 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700952 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953
954 class RecordHandle : public android::BnAudioRecord {
955 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700956 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 virtual ~RecordHandle();
958 virtual status_t start();
959 virtual void stop();
960 virtual sp<IMemory> getCblk() const;
961 virtual status_t onTransact(
962 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
963 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700964 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 };
966
Eric Laurent65b65452010-06-01 23:49:17 -0700967 //--- Audio Effect Management
968
969 // EffectModule and EffectChain classes both have their own mutex to protect
970 // state changes or resource modifications. Always respect the following order
971 // if multiple mutexes must be acquired to avoid cross deadlock:
972 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
973
974 // The EffectModule class is a wrapper object controlling the effect engine implementation
975 // in the effect library. It prevents concurrent calls to process() and command() functions
976 // from different client threads. It keeps a list of EffectHandle objects corresponding
977 // to all client applications using this effect and notifies applications of effect state,
978 // control or parameter changes. It manages the activation state machine to send appropriate
979 // reset, enable, disable commands to effect engine and provide volume
980 // ramping when effects are activated/deactivated.
981 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
982 // the attached track(s) to accumulate their auxiliary channel.
983 class EffectModule: public RefBase {
984 public:
985 EffectModule(const wp<ThreadBase>& wThread,
986 const wp<AudioFlinger::EffectChain>& chain,
987 effect_descriptor_t *desc,
988 int id,
989 int sessionId);
990 ~EffectModule();
991
992 enum effect_state {
993 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700994 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700995 STARTING,
996 ACTIVE,
997 STOPPING,
Eric Laurent21b5c472011-07-26 20:54:46 -0700998 STOPPED,
999 DESTROYED
Eric Laurent65b65452010-06-01 23:49:17 -07001000 };
1001
1002 int id() { return mId; }
1003 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -07001004 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001005 status_t command(uint32_t cmdCode,
1006 uint32_t cmdSize,
1007 void *pCmdData,
1008 uint32_t *replySize,
1009 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001010
Eric Laurentdf9b81c2010-07-02 08:12:41 -07001011 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001012 status_t configure();
1013 status_t init();
1014 uint32_t state() {
1015 return mState;
1016 }
1017 uint32_t status() {
1018 return mStatus;
1019 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001020 int sessionId() {
1021 return mSessionId;
1022 }
Eric Laurent65b65452010-06-01 23:49:17 -07001023 status_t setEnabled(bool enabled);
1024 bool isEnabled();
Eric Laurenta92ebfa2010-08-31 13:50:07 -07001025 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -07001026
1027 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1028 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1029 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1030 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001031 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1032 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -07001033
1034 status_t addHandle(sp<EffectHandle>& handle);
1035 void disconnect(const wp<EffectHandle>& handle);
1036 size_t removeHandle (const wp<EffectHandle>& handle);
1037
1038 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -07001039 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -07001040
1041 status_t setDevice(uint32_t device);
1042 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -07001043 status_t setMode(uint32_t mode);
Eric Laurent21b5c472011-07-26 20:54:46 -07001044 status_t stop();
Eric Laurent65b65452010-06-01 23:49:17 -07001045
1046 status_t dump(int fd, const Vector<String16>& args);
1047
1048 protected:
1049
Eric Laurent7d850f22010-07-09 13:34:17 -07001050 // Maximum time allocated to effect engines to complete the turn off sequence
1051 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1052
Eric Laurent65b65452010-06-01 23:49:17 -07001053 EffectModule(const EffectModule&);
1054 EffectModule& operator = (const EffectModule&);
1055
Eric Laurentdf9b81c2010-07-02 08:12:41 -07001056 status_t start_l();
1057 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001058
1059 Mutex mLock; // mutex for process, commands and handles list protection
1060 wp<ThreadBase> mThread; // parent thread
1061 wp<EffectChain> mChain; // parent effect chain
1062 int mId; // this instance unique ID
1063 int mSessionId; // audio session ID
1064 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1065 effect_config_t mConfig; // input and output audio configuration
Eric Laurent0fb66c22011-05-17 19:16:02 -07001066 effect_handle_t mEffectInterface; // Effect module C API
Eric Laurent65b65452010-06-01 23:49:17 -07001067 status_t mStatus; // initialization status
1068 uint32_t mState; // current activation state (effect_state)
1069 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001070 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1071 // sending disable command.
1072 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001073 };
1074
1075 // The EffectHandle class implements the IEffect interface. It provides resources
1076 // to receive parameter updates, keeps track of effect control
1077 // ownership and state and has a pointer to the EffectModule object it is controlling.
1078 // There is one EffectHandle object for each application controlling (or using)
1079 // an effect module.
1080 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1081 class EffectHandle: public android::BnEffect {
1082 public:
1083
1084 EffectHandle(const sp<EffectModule>& effect,
1085 const sp<AudioFlinger::Client>& client,
1086 const sp<IEffectClient>& effectClient,
1087 int32_t priority);
1088 virtual ~EffectHandle();
1089
1090 // IEffect
1091 virtual status_t enable();
1092 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001093 virtual status_t command(uint32_t cmdCode,
1094 uint32_t cmdSize,
1095 void *pCmdData,
1096 uint32_t *replySize,
1097 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001098 virtual void disconnect();
1099 virtual sp<IMemory> getCblk() const;
1100 virtual status_t onTransact(uint32_t code, const Parcel& data,
1101 Parcel* reply, uint32_t flags);
1102
1103
1104 // Give or take control of effect module
1105 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001106 void commandExecuted(uint32_t cmdCode,
1107 uint32_t cmdSize,
1108 void *pCmdData,
1109 uint32_t replySize,
1110 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001111 void setEnabled(bool enabled);
1112
1113 // Getters
1114 int id() { return mEffect->id(); }
1115 int priority() { return mPriority; }
1116 bool hasControl() { return mHasControl; }
1117 sp<EffectModule> effect() { return mEffect; }
1118
1119 void dump(char* buffer, size_t size);
1120
1121 protected:
1122
1123 EffectHandle(const EffectHandle&);
1124 EffectHandle& operator =(const EffectHandle&);
1125
1126 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1127 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1128 sp<Client> mClient; // client for shared memory allocation
1129 sp<IMemory> mCblkMemory; // shared memory for control block
1130 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1131 uint8_t* mBuffer; // pointer to parameter area in shared memory
1132 int mPriority; // client application priority to control the effect
1133 bool mHasControl; // true if this handle is controlling the effect
1134 };
1135
1136 // the EffectChain class represents a group of effects associated to one audio session.
1137 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1138 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1139 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1140 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1141 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1142 // input buffer used by the track as accumulation buffer.
1143 class EffectChain: public RefBase {
1144 public:
1145 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1146 ~EffectChain();
1147
1148 void process_l();
1149
1150 void lock() {
1151 mLock.lock();
1152 }
1153 void unlock() {
1154 mLock.unlock();
1155 }
1156
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001157 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001158 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001159
Eric Laurent464d5b32011-06-17 21:29:58 -07001160 int sessionId() { return mSessionId; }
1161 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001162
Eric Laurent76c40f72010-07-15 12:50:15 -07001163 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1164 sp<EffectModule> getEffectFromId_l(int id);
1165 bool setVolume_l(uint32_t *left, uint32_t *right);
1166 void setDevice_l(uint32_t device);
1167 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001168
Eric Laurent65b65452010-06-01 23:49:17 -07001169 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1170 mInBuffer = buffer;
1171 mOwnInBuffer = ownsBuffer;
1172 }
1173 int16_t *inBuffer() {
1174 return mInBuffer;
1175 }
1176 void setOutBuffer(int16_t *buffer) {
1177 mOutBuffer = buffer;
1178 }
1179 int16_t *outBuffer() {
1180 return mOutBuffer;
1181 }
1182
Eric Laurent90681d62011-05-09 12:09:06 -07001183 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1184 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1185 int32_t trackCnt() { return mTrackCnt;}
1186
1187 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1188 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1189 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Eric Laurent65b65452010-06-01 23:49:17 -07001190
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001191 uint32_t strategy() { return mStrategy; }
1192 void setStrategy(uint32_t strategy)
1193 { mStrategy = strategy; }
1194
Eric Laurent65b65452010-06-01 23:49:17 -07001195 status_t dump(int fd, const Vector<String16>& args);
1196
1197 protected:
1198
1199 EffectChain(const EffectChain&);
1200 EffectChain& operator =(const EffectChain&);
1201
1202 wp<ThreadBase> mThread; // parent mixer thread
1203 Mutex mLock; // mutex protecting effect list
1204 Vector<sp<EffectModule> > mEffects; // list of effect modules
1205 int mSessionId; // audio session ID
1206 int16_t *mInBuffer; // chain input buffer
1207 int16_t *mOutBuffer; // chain output buffer
Eric Laurent90681d62011-05-09 12:09:06 -07001208 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1209 volatile int32_t mTrackCnt; // number of tracks connected
Eric Laurent65b65452010-06-01 23:49:17 -07001210 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001211 int mVolumeCtrlIdx; // index of insert effect having control over volume
1212 uint32_t mLeftVolume; // previous volume on left channel
1213 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001214 uint32_t mNewLeftVolume; // new volume on left channel
1215 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001216 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001217 };
1218
Dima Zavin31f188892011-04-18 16:57:27 -07001219 struct AudioStreamOut {
1220 audio_hw_device_t *hwDev;
1221 audio_stream_out_t *stream;
1222
1223 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1224 hwDev(dev), stream(out) {}
1225 };
1226
1227 struct AudioStreamIn {
1228 audio_hw_device_t *hwDev;
1229 audio_stream_in_t *stream;
1230
1231 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1232 hwDev(dev), stream(in) {}
1233 };
1234
Eric Laurenta553c252009-07-17 12:17:14 -07001235 friend class RecordThread;
1236 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001238 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1241
Eric Laurenta553c252009-07-17 12:17:14 -07001242 mutable Mutex mHardwareLock;
Dima Zavin31f188892011-04-18 16:57:27 -07001243 audio_hw_device_t* mPrimaryHardwareDev;
1244 Vector<audio_hw_device_t*> mAudioHwDevs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001246
Eric Laurenta553c252009-07-17 12:17:14 -07001247
Eric Laurentddb78e72009-07-28 08:44:33 -07001248 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001249 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurenta553c252009-07-17 12:17:14 -07001250 float mMasterVolume;
1251 bool mMasterMute;
1252
Eric Laurentddb78e72009-07-28 08:44:33 -07001253 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001254
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001255 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001256 volatile int32_t mNextUniqueId;
Eric Laurent53334cd2010-06-23 17:38:20 -07001257 uint32_t mMode;
1258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259};
1260
Dima Zavin31f188892011-04-18 16:57:27 -07001261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262// ----------------------------------------------------------------------------
1263
1264}; // namespace android
1265
1266#endif // ANDROID_AUDIO_FLINGER_H