blob: fff4f06ace47fda50414569b889c8bd3986a5651 [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);
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 // base for record and playback
290 class TrackBase : public AudioBufferProvider, public RefBase {
291
292 public:
293 enum track_state {
294 IDLE,
295 TERMINATED,
296 STOPPED,
297 RESUMING,
298 ACTIVE,
299 PAUSING,
300 PAUSED
301 };
302
303 enum track_flags {
304 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
305 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
306 // The upper 16 bits are used for track-specific flags.
307 };
308
Eric Laurenta553c252009-07-17 12:17:14 -0700309 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700312 uint32_t format,
313 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 int frameCount,
315 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700316 const sp<IMemory>& sharedBuffer,
317 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 ~TrackBase();
319
320 virtual status_t start() = 0;
321 virtual void stop() = 0;
322 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700323 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700324 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
326 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700327 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700329 friend class PlaybackThread;
330 friend class RecordThread;
331 friend class MixerThread;
332 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
334 TrackBase(const TrackBase&);
335 TrackBase& operator = (const TrackBase&);
336
337 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
338 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
339
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700340 uint32_t format() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 return mFormat;
342 }
343
344 int channelCount() const ;
345
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700346 uint32_t channelMask() const;
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 int sampleRate() const;
349
350 void* getBuffer(uint32_t offset, uint32_t frames) const;
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 bool isStopped() const {
353 return mState == STOPPED;
354 }
355
356 bool isTerminated() const {
357 return mState == TERMINATED;
358 }
359
360 bool step();
361 void reset();
362
Eric Laurenta553c252009-07-17 12:17:14 -0700363 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 sp<Client> mClient;
365 sp<IMemory> mCblkMemory;
366 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 void* mBuffer;
368 void* mBufferEnd;
369 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 // we don't really need a lock for these
371 int mState;
372 int mClientTid;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700373 uint32_t mFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700375 int mSessionId;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700376 uint8_t mChannelCount;
377 uint32_t mChannelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 };
379
Eric Laurenta553c252009-07-17 12:17:14 -0700380 class ConfigEvent {
381 public:
382 ConfigEvent() : mEvent(0), mParam(0) {}
383
384 int mEvent;
385 int mParam;
386 };
387
Eric Laurent464d5b32011-06-17 21:29:58 -0700388 virtual status_t initCheck() const = 0;
389 int type() const { return mType; }
Eric Laurenta553c252009-07-17 12:17:14 -0700390 uint32_t sampleRate() const;
391 int channelCount() const;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700392 uint32_t format() const;
Eric Laurenta553c252009-07-17 12:17:14 -0700393 size_t frameCount() const;
394 void wakeUp() { mWaitWorkCV.broadcast(); }
395 void exit();
396 virtual bool checkForNewParameters_l() = 0;
397 virtual status_t setParameters(const String8& keyValuePairs);
398 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700399 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700400 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700401 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700402 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800403 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800404 bool standby() { return mStandby; }
Eric Laurent464d5b32011-06-17 21:29:58 -0700405 uint32_t device() { return mDevice; }
406 virtual audio_stream_t* stream() = 0;
407
408 sp<EffectHandle> createEffect_l(
409 const sp<AudioFlinger::Client>& client,
410 const sp<IEffectClient>& effectClient,
411 int32_t priority,
412 int sessionId,
413 effect_descriptor_t *desc,
414 int *enabled,
415 status_t *status);
416 void disconnectEffect(const sp< EffectModule>& effect,
417 const wp<EffectHandle>& handle);
418
419 // return values for hasAudioSession (bit field)
420 enum effect_state {
421 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
422 // effect
423 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
424 // track
425 };
426
427 // get effect chain corresponding to session Id.
428 sp<EffectChain> getEffectChain(int sessionId);
429 // same as getEffectChain() but must be called with ThreadBase mutex locked
430 sp<EffectChain> getEffectChain_l(int sessionId);
431 // add an effect chain to the chain list (mEffectChains)
432 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
433 // remove an effect chain from the chain list (mEffectChains)
434 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
435 // lock mall effect chains Mutexes. Must be called before releasing the
436 // ThreadBase mutex before processing the mixer and effects. This guarantees the
437 // integrity of the chains during the process.
438 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
439 // unlock effect chains after process
440 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
441 // set audio mode to all effect chains
442 void setMode(uint32_t mode);
443 // get effect module with corresponding ID on specified audio session
444 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
445 // add and effect module. Also creates the effect chain is none exists for
446 // the effects audio session
447 status_t addEffect_l(const sp< EffectModule>& effect);
448 // remove and effect module. Also removes the effect chain is this was the last
449 // effect
450 void removeEffect_l(const sp< EffectModule>& effect);
451 // detach all tracks connected to an auxiliary effect
452 virtual void detachAuxEffect_l(int effectId) {}
453 // returns either EFFECT_SESSION if effects on this audio session exist in one
454 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
455 virtual uint32_t hasAudioSession(int sessionId) = 0;
456 // the value returned by default implementation is not important as the
457 // strategy is only meaningful for PlaybackThread which implements this method
458 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Eric Laurenta553c252009-07-17 12:17:14 -0700459
Eric Laurent2c817f52009-07-23 13:17:39 -0700460 mutable Mutex mLock;
461
Eric Laurenta553c252009-07-17 12:17:14 -0700462 protected:
463
464 friend class Track;
465 friend class TrackBase;
466 friend class PlaybackThread;
467 friend class MixerThread;
468 friend class DirectOutputThread;
469 friend class DuplicatingThread;
470 friend class RecordThread;
471 friend class RecordTrack;
472
Eric Laurent464d5b32011-06-17 21:29:58 -0700473 int mType;
Eric Laurenta553c252009-07-17 12:17:14 -0700474 Condition mWaitWorkCV;
475 sp<AudioFlinger> mAudioFlinger;
476 uint32_t mSampleRate;
477 size_t mFrameCount;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700478 uint32_t mChannelMask;
Eric Laurentb0a01472010-05-14 05:45:46 -0700479 uint16_t mChannelCount;
480 uint16_t mFrameSize;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700481 uint32_t mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700482 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700483 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700484 status_t mParamStatus;
485 Vector<ConfigEvent *> mConfigEvents;
486 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800487 int mId;
488 bool mExiting;
Eric Laurent464d5b32011-06-17 21:29:58 -0700489 Vector< sp<EffectChain> > mEffectChains;
490 uint32_t mDevice; // output device for PlaybackThread
491 // input + output devices for RecordThread
Eric Laurenta553c252009-07-17 12:17:14 -0700492 };
493
494 // --- PlaybackThread ---
495 class PlaybackThread : public ThreadBase {
496 public:
497
Eric Laurent059b4be2009-11-09 23:32:22 -0800498 enum mixer_state {
499 MIXER_IDLE,
500 MIXER_TRACKS_ENABLED,
501 MIXER_TRACKS_READY
502 };
503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 // playback track
505 class Track : public TrackBase {
506 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700507 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 const sp<Client>& client,
509 int streamType,
510 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700511 uint32_t format,
512 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700514 const sp<IMemory>& sharedBuffer,
515 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 ~Track();
517
518 void dump(char* buffer, size_t size);
519 virtual status_t start();
520 virtual void stop();
521 void pause();
522
523 void flush();
524 void destroy();
525 void mute(bool);
526 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700527 int name() const {
528 return mName;
529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530
Eric Laurent4bc035a2009-05-22 09:18:15 -0700531 int type() const {
532 return mStreamType;
533 }
Eric Laurent65b65452010-06-01 23:49:17 -0700534 status_t attachAuxEffect(int EffectId);
535 void setAuxBuffer(int EffectId, int32_t *buffer);
536 int32_t *auxBuffer() { return mAuxBuffer; }
537 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
538 int16_t *mainBuffer() { return mMainBuffer; }
539 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700540
541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700543 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700545 friend class TrackHandle;
546 friend class PlaybackThread;
547 friend class MixerThread;
548 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549
550 Track(const Track&);
551 Track& operator = (const Track&);
552
553 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700554 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 bool isPausing() const {
556 return mState == PAUSING;
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 bool isPaused() const {
559 return mState == PAUSED;
560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 void setPaused() { mState = PAUSED; }
563 void reset();
564
Eric Laurent49f02be2009-11-19 09:00:56 -0800565 bool isOutputTrack() const {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700566 return (mStreamType == AUDIO_STREAM_CNT);
Eric Laurent49f02be2009-11-19 09:00:56 -0800567 }
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 // we don't really need a lock for these
570 float mVolume[2];
571 volatile bool mMute;
572 // FILLED state is used for suppressing volume ramp at begin of playing
573 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
574 mutable uint8_t mFillingUpStatus;
575 int8_t mRetryCount;
576 sp<IMemory> mSharedBuffer;
577 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700578 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700579 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700580 int16_t *mMainBuffer;
581 int32_t *mAuxBuffer;
582 int mAuxEffectId;
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700583 bool mHasVolumeController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }; // end of Track
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
587 // playback track
588 class OutputTrack : public Track {
589 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 class Buffer: public AudioBufferProvider::Buffer {
592 public:
593 int16_t *mBuffer;
594 };
Eric Laurenta553c252009-07-17 12:17:14 -0700595
596 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800597 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700599 uint32_t format,
600 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 int frameCount);
602 ~OutputTrack();
603
604 virtual status_t start();
605 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700606 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700608 bool isActive() { return mActive; }
609 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610
611 private:
612
Eric Laurenta553c252009-07-17 12:17:14 -0700613 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700615
616 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800617 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 Vector < Buffer* > mBufferQueue;
620 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700621 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800622 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700623 }; // end of OutputTrack
624
Dima Zavin31f188892011-04-18 16:57:27 -0700625 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700626 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 virtual status_t dump(int fd, const Vector<String16>& args);
629
630 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 virtual status_t readyToRun();
632 virtual void onFirstRef();
633
Eric Laurent464d5b32011-06-17 21:29:58 -0700634 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 virtual uint32_t latency() const;
637
638 virtual status_t setMasterVolume(float value);
639 virtual status_t setMasterMute(bool muted);
640
641 virtual float masterVolume() const;
642 virtual bool masterMute() const;
643
644 virtual status_t setStreamVolume(int stream, float value);
645 virtual status_t setStreamMute(int stream, bool muted);
646
647 virtual float streamVolume(int stream) const;
648 virtual bool streamMute(int stream) const;
649
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700650 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 const sp<AudioFlinger::Client>& client,
652 int streamType,
653 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700654 uint32_t format,
655 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 int frameCount,
657 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700658 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700660
Dima Zavin31f188892011-04-18 16:57:27 -0700661 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurent464d5b32011-06-17 21:29:58 -0700662 virtual audio_stream_t* stream() { return &mOutput->stream->common; }
Eric Laurenta553c252009-07-17 12:17:14 -0700663
Eric Laurentd5603c12009-08-06 08:49:39 -0700664 void suspend() { mSuspended++; }
665 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800666 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700667 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700668 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800669 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700670 int16_t *mixBuffer() { return mMixBuffer; };
671
Eric Laurent464d5b32011-06-17 21:29:58 -0700672 virtual void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700673 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
674 int EffectId);
675 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
676 int EffectId);
Eric Laurenta553c252009-07-17 12:17:14 -0700677
Eric Laurent464d5b32011-06-17 21:29:58 -0700678 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
679 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
680 virtual uint32_t hasAudioSession(int sessionId);
681 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 struct stream_type_t {
684 stream_type_t()
685 : volume(1.0f),
686 mute(false)
687 {
688 }
689 float volume;
690 bool mute;
691 };
692
Eric Laurent2c817f52009-07-23 13:17:39 -0700693 protected:
Eric Laurent2c817f52009-07-23 13:17:39 -0700694 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700695 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700696 int mBytesWritten;
697 bool mMasterMute;
698 SortedVector< wp<Track> > mActiveTracks;
699
Eric Laurent62443f52009-10-05 20:29:18 -0700700 virtual int getTrackName_l() = 0;
701 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800702 virtual uint32_t activeSleepTimeUs() = 0;
703 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent8448a792010-08-18 18:13:17 -0700704 virtual uint32_t suspendSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 private:
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700709 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 friend class Track;
711 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700712 friend class MixerThread;
713 friend class DirectOutputThread;
714 friend class DuplicatingThread;
715
716 PlaybackThread(const Client&);
717 PlaybackThread& operator = (const PlaybackThread&);
718
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700719 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700720 void destroyTrack_l(const sp<Track>& track);
Eric Laurent90681d62011-05-09 12:09:06 -0700721 void removeTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700722
Eric Laurenta553c252009-07-17 12:17:14 -0700723 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
Eric Laurenta553c252009-07-17 12:17:14 -0700725 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700727 status_t dumpEffectChains(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,
998 STOPPED
999 };
1000
1001 int id() { return mId; }
1002 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -07001003 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001004 status_t command(uint32_t cmdCode,
1005 uint32_t cmdSize,
1006 void *pCmdData,
1007 uint32_t *replySize,
1008 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001009
Eric Laurentdf9b81c2010-07-02 08:12:41 -07001010 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001011 status_t configure();
1012 status_t init();
1013 uint32_t state() {
1014 return mState;
1015 }
1016 uint32_t status() {
1017 return mStatus;
1018 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001019 int sessionId() {
1020 return mSessionId;
1021 }
Eric Laurent65b65452010-06-01 23:49:17 -07001022 status_t setEnabled(bool enabled);
1023 bool isEnabled();
Eric Laurenta92ebfa2010-08-31 13:50:07 -07001024 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -07001025
1026 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1027 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1028 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1029 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001030 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1031 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -07001032
1033 status_t addHandle(sp<EffectHandle>& handle);
1034 void disconnect(const wp<EffectHandle>& handle);
1035 size_t removeHandle (const wp<EffectHandle>& handle);
1036
1037 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -07001038 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -07001039
1040 status_t setDevice(uint32_t device);
1041 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -07001042 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -07001043
1044 status_t dump(int fd, const Vector<String16>& args);
1045
1046 protected:
1047
Eric Laurent7d850f22010-07-09 13:34:17 -07001048 // Maximum time allocated to effect engines to complete the turn off sequence
1049 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1050
Eric Laurent65b65452010-06-01 23:49:17 -07001051 EffectModule(const EffectModule&);
1052 EffectModule& operator = (const EffectModule&);
1053
Eric Laurentdf9b81c2010-07-02 08:12:41 -07001054 status_t start_l();
1055 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001056
1057 Mutex mLock; // mutex for process, commands and handles list protection
1058 wp<ThreadBase> mThread; // parent thread
1059 wp<EffectChain> mChain; // parent effect chain
1060 int mId; // this instance unique ID
1061 int mSessionId; // audio session ID
1062 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1063 effect_config_t mConfig; // input and output audio configuration
Eric Laurent0fb66c22011-05-17 19:16:02 -07001064 effect_handle_t mEffectInterface; // Effect module C API
Eric Laurent65b65452010-06-01 23:49:17 -07001065 status_t mStatus; // initialization status
1066 uint32_t mState; // current activation state (effect_state)
1067 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001068 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1069 // sending disable command.
1070 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001071 };
1072
1073 // The EffectHandle class implements the IEffect interface. It provides resources
1074 // to receive parameter updates, keeps track of effect control
1075 // ownership and state and has a pointer to the EffectModule object it is controlling.
1076 // There is one EffectHandle object for each application controlling (or using)
1077 // an effect module.
1078 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1079 class EffectHandle: public android::BnEffect {
1080 public:
1081
1082 EffectHandle(const sp<EffectModule>& effect,
1083 const sp<AudioFlinger::Client>& client,
1084 const sp<IEffectClient>& effectClient,
1085 int32_t priority);
1086 virtual ~EffectHandle();
1087
1088 // IEffect
1089 virtual status_t enable();
1090 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001091 virtual status_t command(uint32_t cmdCode,
1092 uint32_t cmdSize,
1093 void *pCmdData,
1094 uint32_t *replySize,
1095 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001096 virtual void disconnect();
1097 virtual sp<IMemory> getCblk() const;
1098 virtual status_t onTransact(uint32_t code, const Parcel& data,
1099 Parcel* reply, uint32_t flags);
1100
1101
1102 // Give or take control of effect module
1103 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001104 void commandExecuted(uint32_t cmdCode,
1105 uint32_t cmdSize,
1106 void *pCmdData,
1107 uint32_t replySize,
1108 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001109 void setEnabled(bool enabled);
1110
1111 // Getters
1112 int id() { return mEffect->id(); }
1113 int priority() { return mPriority; }
1114 bool hasControl() { return mHasControl; }
1115 sp<EffectModule> effect() { return mEffect; }
1116
1117 void dump(char* buffer, size_t size);
1118
1119 protected:
1120
1121 EffectHandle(const EffectHandle&);
1122 EffectHandle& operator =(const EffectHandle&);
1123
1124 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1125 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1126 sp<Client> mClient; // client for shared memory allocation
1127 sp<IMemory> mCblkMemory; // shared memory for control block
1128 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1129 uint8_t* mBuffer; // pointer to parameter area in shared memory
1130 int mPriority; // client application priority to control the effect
1131 bool mHasControl; // true if this handle is controlling the effect
1132 };
1133
1134 // the EffectChain class represents a group of effects associated to one audio session.
1135 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1136 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1137 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1138 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1139 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1140 // input buffer used by the track as accumulation buffer.
1141 class EffectChain: public RefBase {
1142 public:
1143 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1144 ~EffectChain();
1145
1146 void process_l();
1147
1148 void lock() {
1149 mLock.lock();
1150 }
1151 void unlock() {
1152 mLock.unlock();
1153 }
1154
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001155 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001156 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001157
Eric Laurent464d5b32011-06-17 21:29:58 -07001158 int sessionId() { return mSessionId; }
1159 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001160
Eric Laurent76c40f72010-07-15 12:50:15 -07001161 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1162 sp<EffectModule> getEffectFromId_l(int id);
1163 bool setVolume_l(uint32_t *left, uint32_t *right);
1164 void setDevice_l(uint32_t device);
1165 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001166
Eric Laurent65b65452010-06-01 23:49:17 -07001167 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1168 mInBuffer = buffer;
1169 mOwnInBuffer = ownsBuffer;
1170 }
1171 int16_t *inBuffer() {
1172 return mInBuffer;
1173 }
1174 void setOutBuffer(int16_t *buffer) {
1175 mOutBuffer = buffer;
1176 }
1177 int16_t *outBuffer() {
1178 return mOutBuffer;
1179 }
1180
Eric Laurent90681d62011-05-09 12:09:06 -07001181 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1182 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1183 int32_t trackCnt() { return mTrackCnt;}
1184
1185 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1186 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1187 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Eric Laurent65b65452010-06-01 23:49:17 -07001188
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001189 uint32_t strategy() { return mStrategy; }
1190 void setStrategy(uint32_t strategy)
1191 { mStrategy = strategy; }
1192
Eric Laurent65b65452010-06-01 23:49:17 -07001193 status_t dump(int fd, const Vector<String16>& args);
1194
1195 protected:
1196
1197 EffectChain(const EffectChain&);
1198 EffectChain& operator =(const EffectChain&);
1199
1200 wp<ThreadBase> mThread; // parent mixer thread
1201 Mutex mLock; // mutex protecting effect list
1202 Vector<sp<EffectModule> > mEffects; // list of effect modules
1203 int mSessionId; // audio session ID
1204 int16_t *mInBuffer; // chain input buffer
1205 int16_t *mOutBuffer; // chain output buffer
Eric Laurent90681d62011-05-09 12:09:06 -07001206 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1207 volatile int32_t mTrackCnt; // number of tracks connected
Eric Laurent65b65452010-06-01 23:49:17 -07001208 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001209 int mVolumeCtrlIdx; // index of insert effect having control over volume
1210 uint32_t mLeftVolume; // previous volume on left channel
1211 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001212 uint32_t mNewLeftVolume; // new volume on left channel
1213 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001214 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001215 };
1216
Dima Zavin31f188892011-04-18 16:57:27 -07001217 struct AudioStreamOut {
1218 audio_hw_device_t *hwDev;
1219 audio_stream_out_t *stream;
1220
1221 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1222 hwDev(dev), stream(out) {}
1223 };
1224
1225 struct AudioStreamIn {
1226 audio_hw_device_t *hwDev;
1227 audio_stream_in_t *stream;
1228
1229 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1230 hwDev(dev), stream(in) {}
1231 };
1232
Eric Laurenta553c252009-07-17 12:17:14 -07001233 friend class RecordThread;
1234 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001236 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1239
Eric Laurenta553c252009-07-17 12:17:14 -07001240 mutable Mutex mHardwareLock;
Dima Zavin31f188892011-04-18 16:57:27 -07001241 audio_hw_device_t* mPrimaryHardwareDev;
1242 Vector<audio_hw_device_t*> mAudioHwDevs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001244
Eric Laurenta553c252009-07-17 12:17:14 -07001245
Eric Laurentddb78e72009-07-28 08:44:33 -07001246 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001247 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurenta553c252009-07-17 12:17:14 -07001248 float mMasterVolume;
1249 bool mMasterMute;
1250
Eric Laurentddb78e72009-07-28 08:44:33 -07001251 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001252
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001253 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001254 volatile int32_t mNextUniqueId;
Eric Laurent53334cd2010-06-23 17:38:20 -07001255 uint32_t mMode;
1256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257};
1258
Dima Zavin31f188892011-04-18 16:57:27 -07001259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260// ----------------------------------------------------------------------------
1261
1262}; // namespace android
1263
1264#endif // ANDROID_AUDIO_FLINGER_H