blob: 17975e3cfc99d6abf182261a7dc16b82185777ce [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/* //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>
23#include <limits.h>
24
25#include <media/IAudioFlinger.h>
26#include <media/IAudioFlingerClient.h>
27#include <media/IAudioTrack.h>
28#include <media/IAudioRecord.h>
29#include <media/AudioTrack.h>
30
31#include <utils/Atomic.h>
32#include <utils/Errors.h>
33#include <utils/threads.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/SortedVector.h>
Dima Zavin799a70e2011-04-18 16:57:27 -070035#include <utils/TypeHelpers.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <utils/Vector.h>
37
Mathias Agopian5462fc92010-07-14 18:41:18 -070038#include <binder/BinderService.h>
39#include <binder/MemoryDealer.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioBufferProvider.h"
45
Eric Laurentfeb0db62011-07-22 09:04:31 -070046#include <powermanager/IPowerManager.h>
47
Mathias Agopian65ab4712010-07-14 17:59:35 -070048namespace android {
49
50class audio_track_cblk_t;
51class effect_param_cblk_t;
52class AudioMixer;
53class AudioBuffer;
54class AudioResampler;
55
Mathias Agopian65ab4712010-07-14 17:59:35 -070056// ----------------------------------------------------------------------------
57
58#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
59#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
60
61
62// ----------------------------------------------------------------------------
63
64static const nsecs_t kStandbyTimeInNsecs = seconds(3);
65
Mathias Agopian5462fc92010-07-14 18:41:18 -070066class AudioFlinger :
67 public BinderService<AudioFlinger>,
68 public BnAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -070069{
Mathias Agopian5462fc92010-07-14 18:41:18 -070070 friend class BinderService<AudioFlinger>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070071public:
Mathias Agopian5462fc92010-07-14 18:41:18 -070072 static char const* getServiceName() { return "media.audio_flinger"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
74 virtual status_t dump(int fd, const Vector<String16>& args);
75
76 // IAudioFlinger interface
77 virtual sp<IAudioTrack> createTrack(
78 pid_t pid,
79 int streamType,
80 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070081 uint32_t format,
82 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -070083 int frameCount,
84 uint32_t flags,
85 const sp<IMemory>& sharedBuffer,
86 int output,
Mike J. Chen535235c2011-08-15 13:28:26 -070087 bool isTimed,
Mathias Agopian65ab4712010-07-14 17:59:35 -070088 int *sessionId,
89 status_t *status);
90
91 virtual uint32_t sampleRate(int output) const;
92 virtual int channelCount(int output) const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070093 virtual uint32_t format(int output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 virtual size_t frameCount(int output) const;
95 virtual uint32_t latency(int output) const;
96
97 virtual status_t setMasterVolume(float value);
98 virtual status_t setMasterMute(bool muted);
99
100 virtual float masterVolume() const;
Mike J. Chen535235c2011-08-15 13:28:26 -0700101 virtual float masterVolumeSW() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102 virtual bool masterMute() const;
103
104 virtual status_t setStreamVolume(int stream, float value, int output);
105 virtual status_t setStreamMute(int stream, bool muted);
106
107 virtual float streamVolume(int stream, int output) const;
108 virtual bool streamMute(int stream) const;
109
110 virtual status_t setMode(int mode);
111
112 virtual status_t setMicMute(bool state);
113 virtual bool getMicMute() const;
114
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
116 virtual String8 getParameters(int ioHandle, const String8& keys);
117
118 virtual void registerClient(const sp<IAudioFlingerClient>& client);
119
120 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
121 virtual unsigned int getInputFramesLost(int ioHandle);
122
123 virtual int openOutput(uint32_t *pDevices,
124 uint32_t *pSamplingRate,
125 uint32_t *pFormat,
126 uint32_t *pChannels,
127 uint32_t *pLatencyMs,
128 uint32_t flags);
129
130 virtual int openDuplicateOutput(int output1, int output2);
131
132 virtual status_t closeOutput(int output);
133
134 virtual status_t suspendOutput(int output);
135
136 virtual status_t restoreOutput(int output);
137
138 virtual int openInput(uint32_t *pDevices,
139 uint32_t *pSamplingRate,
140 uint32_t *pFormat,
141 uint32_t *pChannels,
142 uint32_t acoustics);
143
144 virtual status_t closeInput(int input);
145
146 virtual status_t setStreamOutput(uint32_t stream, int output);
147
148 virtual status_t setVoiceVolume(float volume);
149
150 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
151
152 virtual int newAudioSessionId();
153
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700154 virtual void acquireAudioSessionId(int audioSession);
155
156 virtual void releaseAudioSessionId(int audioSession);
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 virtual status_t queryNumberEffects(uint32_t *numEffects);
159
160 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
161
162 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
163
164 virtual sp<IEffect> createEffect(pid_t pid,
165 effect_descriptor_t *pDesc,
166 const sp<IEffectClient>& effectClient,
167 int32_t priority,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700168 int io,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 int sessionId,
170 status_t *status,
171 int *id,
172 int *enabled);
173
Eric Laurent59255e42011-07-27 19:49:51 -0700174 virtual status_t moveEffects(int sessionId, int srcOutput, int dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175
176 enum hardware_call_state {
177 AUDIO_HW_IDLE = 0,
178 AUDIO_HW_INIT,
179 AUDIO_HW_OUTPUT_OPEN,
180 AUDIO_HW_OUTPUT_CLOSE,
181 AUDIO_HW_INPUT_OPEN,
182 AUDIO_HW_INPUT_CLOSE,
183 AUDIO_HW_STANDBY,
184 AUDIO_HW_SET_MASTER_VOLUME,
185 AUDIO_HW_GET_ROUTING,
186 AUDIO_HW_SET_ROUTING,
187 AUDIO_HW_GET_MODE,
188 AUDIO_HW_SET_MODE,
189 AUDIO_HW_GET_MIC_MUTE,
190 AUDIO_HW_SET_MIC_MUTE,
191 AUDIO_SET_VOICE_VOLUME,
192 AUDIO_SET_PARAMETER,
Mike J. Chen535235c2011-08-15 13:28:26 -0700193 AUDIO_HW_GET_MASTER_VOLUME,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 };
195
196 // record interface
197 virtual sp<IAudioRecord> openRecord(
198 pid_t pid,
199 int input,
200 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700201 uint32_t format,
202 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 int frameCount,
204 uint32_t flags,
205 int *sessionId,
206 status_t *status);
207
208 virtual status_t onTransact(
209 uint32_t code,
210 const Parcel& data,
211 Parcel* reply,
212 uint32_t flags);
213
214 uint32_t getMode() { return mMode; }
215
Eric Laurentbee53372011-08-29 12:42:48 -0700216 bool btNrecIsOff() { return mBtNrecIsOff; }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700217
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218private:
219 AudioFlinger();
220 virtual ~AudioFlinger();
221
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700222 status_t initCheck() const;
223 virtual void onFirstRef();
Dima Zavin799a70e2011-04-18 16:57:27 -0700224 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700225 void purgeStaleEffects_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226
227 // Internal dump utilites.
228 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
229 status_t dumpClients(int fd, const Vector<String16>& args);
230 status_t dumpInternals(int fd, const Vector<String16>& args);
231
232 // --- Client ---
233 class Client : public RefBase {
234 public:
235 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
236 virtual ~Client();
237 const sp<MemoryDealer>& heap() const;
238 pid_t pid() const { return mPid; }
239 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
240
Mike J. Chen535235c2011-08-15 13:28:26 -0700241 bool reserveTimedTrack();
242 void releaseTimedTrack();
243
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 private:
245 Client(const Client&);
246 Client& operator = (const Client&);
247 sp<AudioFlinger> mAudioFlinger;
248 sp<MemoryDealer> mMemoryDealer;
249 pid_t mPid;
Mike J. Chen535235c2011-08-15 13:28:26 -0700250
251 Mutex mTimedTrackLock;
252 int mTimedTrackCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 };
254
255 // --- Notification Client ---
256 class NotificationClient : public IBinder::DeathRecipient {
257 public:
258 NotificationClient(const sp<AudioFlinger>& audioFlinger,
259 const sp<IAudioFlingerClient>& client,
260 pid_t pid);
261 virtual ~NotificationClient();
262
263 sp<IAudioFlingerClient> client() { return mClient; }
264
265 // IBinder::DeathRecipient
266 virtual void binderDied(const wp<IBinder>& who);
267
268 private:
269 NotificationClient(const NotificationClient&);
270 NotificationClient& operator = (const NotificationClient&);
271
272 sp<AudioFlinger> mAudioFlinger;
273 pid_t mPid;
274 sp<IAudioFlingerClient> mClient;
275 };
276
277 class TrackHandle;
278 class RecordHandle;
279 class RecordThread;
280 class PlaybackThread;
281 class MixerThread;
282 class DirectOutputThread;
283 class DuplicatingThread;
284 class Track;
285 class RecordTrack;
286 class EffectModule;
287 class EffectHandle;
288 class EffectChain;
Dima Zavin799a70e2011-04-18 16:57:27 -0700289 struct AudioStreamOut;
290 struct AudioStreamIn;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291
292 class ThreadBase : public Thread {
293 public:
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700294 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 virtual ~ThreadBase();
296
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700297
298 enum type {
299 MIXER, // Thread class is MixerThread
300 DIRECT, // Thread class is DirectOutputThread
301 DUPLICATING, // Thread class is DuplicatingThread
302 RECORD // Thread class is RecordThread
303 };
304
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 status_t dumpBase(int fd, const Vector<String16>& args);
Eric Laurent1d2bff02011-07-24 17:49:51 -0700306 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307
Eric Laurentfeb0db62011-07-22 09:04:31 -0700308 void clearPowerManager();
309
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 // base for record and playback
311 class TrackBase : public AudioBufferProvider, public RefBase {
312
313 public:
314 enum track_state {
315 IDLE,
316 TERMINATED,
317 STOPPED,
318 RESUMING,
319 ACTIVE,
320 PAUSING,
321 PAUSED
322 };
323
324 enum track_flags {
325 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
326 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
327 // The upper 16 bits are used for track-specific flags.
328 };
329
330 TrackBase(const wp<ThreadBase>& thread,
331 const sp<Client>& client,
332 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700333 uint32_t format,
334 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 int frameCount,
336 uint32_t flags,
337 const sp<IMemory>& sharedBuffer,
338 int sessionId);
339 ~TrackBase();
340
341 virtual status_t start() = 0;
342 virtual void stop() = 0;
343 sp<IMemory> getCblk() const;
344 audio_track_cblk_t* cblk() const { return mCblk; }
345 int sessionId() { return mSessionId; }
346
347 protected:
348 friend class ThreadBase;
349 friend class RecordHandle;
350 friend class PlaybackThread;
351 friend class RecordThread;
352 friend class MixerThread;
353 friend class DirectOutputThread;
354
355 TrackBase(const TrackBase&);
356 TrackBase& operator = (const TrackBase&);
357
Mike J. Chen535235c2011-08-15 13:28:26 -0700358 virtual status_t getNextBuffer(
359 AudioBufferProvider::Buffer* buffer,
360 int64_t pts) = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
362
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700363 uint32_t format() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 return mFormat;
365 }
366
367 int channelCount() const ;
368
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700369 uint32_t channelMask() const;
370
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 int sampleRate() const;
372
373 void* getBuffer(uint32_t offset, uint32_t frames) const;
374
375 bool isStopped() const {
376 return mState == STOPPED;
377 }
378
379 bool isTerminated() const {
380 return mState == TERMINATED;
381 }
382
383 bool step();
384 void reset();
385
386 wp<ThreadBase> mThread;
387 sp<Client> mClient;
388 sp<IMemory> mCblkMemory;
389 audio_track_cblk_t* mCblk;
390 void* mBuffer;
391 void* mBufferEnd;
392 uint32_t mFrameCount;
393 // we don't really need a lock for these
394 int mState;
395 int mClientTid;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700396 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 uint32_t mFlags;
398 int mSessionId;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700399 uint8_t mChannelCount;
400 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 };
402
403 class ConfigEvent {
404 public:
405 ConfigEvent() : mEvent(0), mParam(0) {}
406
407 int mEvent;
408 int mParam;
409 };
410
Eric Laurentfeb0db62011-07-22 09:04:31 -0700411 class PMDeathRecipient : public IBinder::DeathRecipient {
412 public:
413 PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
414 virtual ~PMDeathRecipient() {}
415
416 // IBinder::DeathRecipient
417 virtual void binderDied(const wp<IBinder>& who);
418
419 private:
420 PMDeathRecipient(const PMDeathRecipient&);
421 PMDeathRecipient& operator = (const PMDeathRecipient&);
422
423 wp<ThreadBase> mThread;
424 };
425
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700426 virtual status_t initCheck() const = 0;
427 int type() const { return mType; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 uint32_t sampleRate() const;
429 int channelCount() const;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700430 uint32_t format() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 size_t frameCount() const;
432 void wakeUp() { mWaitWorkCV.broadcast(); }
433 void exit();
434 virtual bool checkForNewParameters_l() = 0;
435 virtual status_t setParameters(const String8& keyValuePairs);
436 virtual String8 getParameters(const String8& keys) = 0;
437 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
438 void sendConfigEvent(int event, int param = 0);
439 void sendConfigEvent_l(int event, int param = 0);
440 void processConfigEvents();
441 int id() const { return mId;}
442 bool standby() { return mStandby; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700443 uint32_t device() { return mDevice; }
444 virtual audio_stream_t* stream() = 0;
445
446 sp<EffectHandle> createEffect_l(
447 const sp<AudioFlinger::Client>& client,
448 const sp<IEffectClient>& effectClient,
449 int32_t priority,
450 int sessionId,
451 effect_descriptor_t *desc,
452 int *enabled,
453 status_t *status);
454 void disconnectEffect(const sp< EffectModule>& effect,
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700455 const wp<EffectHandle>& handle,
456 bool unpiniflast);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700457
458 // return values for hasAudioSession (bit field)
459 enum effect_state {
460 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
461 // effect
462 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
463 // track
464 };
465
466 // get effect chain corresponding to session Id.
467 sp<EffectChain> getEffectChain(int sessionId);
468 // same as getEffectChain() but must be called with ThreadBase mutex locked
469 sp<EffectChain> getEffectChain_l(int sessionId);
470 // add an effect chain to the chain list (mEffectChains)
471 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
472 // remove an effect chain from the chain list (mEffectChains)
473 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
474 // lock mall effect chains Mutexes. Must be called before releasing the
475 // ThreadBase mutex before processing the mixer and effects. This guarantees the
476 // integrity of the chains during the process.
477 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
478 // unlock effect chains after process
479 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
480 // set audio mode to all effect chains
481 void setMode(uint32_t mode);
482 // get effect module with corresponding ID on specified audio session
483 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
484 // add and effect module. Also creates the effect chain is none exists for
485 // the effects audio session
486 status_t addEffect_l(const sp< EffectModule>& effect);
487 // remove and effect module. Also removes the effect chain is this was the last
488 // effect
489 void removeEffect_l(const sp< EffectModule>& effect);
490 // detach all tracks connected to an auxiliary effect
491 virtual void detachAuxEffect_l(int effectId) {}
492 // returns either EFFECT_SESSION if effects on this audio session exist in one
493 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
494 virtual uint32_t hasAudioSession(int sessionId) = 0;
495 // the value returned by default implementation is not important as the
496 // strategy is only meaningful for PlaybackThread which implements this method
497 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498
Eric Laurent59255e42011-07-27 19:49:51 -0700499 // suspend or restore effect according to the type of effect passed. a NULL
500 // type pointer means suspend all effects in the session
501 void setEffectSuspended(const effect_uuid_t *type,
502 bool suspend,
503 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
504 // check if some effects must be suspended/restored when an effect is enabled
505 // or disabled
506 virtual void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
507 bool enabled,
508 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
509
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510 mutable Mutex mLock;
511
512 protected:
513
Eric Laurent59255e42011-07-27 19:49:51 -0700514 // entry describing an effect being suspended in mSuspendedSessions keyed vector
515 class SuspendedSessionDesc : public RefBase {
516 public:
517 SuspendedSessionDesc() : mRefCount(0) {}
518
519 int mRefCount; // number of active suspend requests
520 effect_uuid_t mType; // effect type UUID
521 };
522
Eric Laurentfeb0db62011-07-22 09:04:31 -0700523 void acquireWakeLock();
524 void acquireWakeLock_l();
525 void releaseWakeLock();
526 void releaseWakeLock_l();
Eric Laurent59255e42011-07-27 19:49:51 -0700527 void setEffectSuspended_l(const effect_uuid_t *type,
528 bool suspend,
529 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
530 // updated mSuspendedSessions when an effect suspended or restored
531 void updateSuspendedSessions_l(const effect_uuid_t *type,
532 bool suspend,
533 int sessionId);
534 // check if some effects must be suspended when an effect chain is added
535 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
Eric Laurentfeb0db62011-07-22 09:04:31 -0700536
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700537 friend class AudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 friend class Track;
539 friend class TrackBase;
540 friend class PlaybackThread;
541 friend class MixerThread;
542 friend class DirectOutputThread;
543 friend class DuplicatingThread;
544 friend class RecordThread;
545 friend class RecordTrack;
546
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700547 int mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 Condition mWaitWorkCV;
549 sp<AudioFlinger> mAudioFlinger;
550 uint32_t mSampleRate;
551 size_t mFrameCount;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700552 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 uint16_t mChannelCount;
554 uint16_t mFrameSize;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700555 uint32_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 Condition mParamCond;
557 Vector<String8> mNewParameters;
558 status_t mParamStatus;
559 Vector<ConfigEvent *> mConfigEvents;
560 bool mStandby;
561 int mId;
562 bool mExiting;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700563 Vector< sp<EffectChain> > mEffectChains;
564 uint32_t mDevice; // output device for PlaybackThread
565 // input + output devices for RecordThread
Eric Laurentfeb0db62011-07-22 09:04:31 -0700566 static const int kNameLength = 32;
567 char mName[kNameLength];
568 sp<IPowerManager> mPowerManager;
569 sp<IBinder> mWakeLockToken;
570 sp<PMDeathRecipient> mDeathRecipient;
Eric Laurent59255e42011-07-27 19:49:51 -0700571 // list of suspended effects per session and per type. The first vector is
572 // keyed by session ID, the second by type UUID timeLow field
573 KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > > mSuspendedSessions;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 };
575
576 // --- PlaybackThread ---
577 class PlaybackThread : public ThreadBase {
578 public:
579
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 enum mixer_state {
581 MIXER_IDLE,
582 MIXER_TRACKS_ENABLED,
583 MIXER_TRACKS_READY
584 };
585
586 // playback track
587 class Track : public TrackBase {
588 public:
589 Track( const wp<ThreadBase>& thread,
590 const sp<Client>& client,
591 int streamType,
592 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700593 uint32_t format,
594 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 int frameCount,
596 const sp<IMemory>& sharedBuffer,
597 int sessionId);
598 ~Track();
599
600 void dump(char* buffer, size_t size);
601 virtual status_t start();
602 virtual void stop();
603 void pause();
604
605 void flush();
606 void destroy();
607 void mute(bool);
608 void setVolume(float left, float right);
609 int name() const {
610 return mName;
611 }
612
613 int type() const {
614 return mStreamType;
615 }
616 status_t attachAuxEffect(int EffectId);
617 void setAuxBuffer(int EffectId, int32_t *buffer);
618 int32_t *auxBuffer() { return mAuxBuffer; }
619 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
620 int16_t *mainBuffer() { return mMainBuffer; }
621 int auxEffectId() { return mAuxEffectId; }
622
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 protected:
624 friend class ThreadBase;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 friend class TrackHandle;
626 friend class PlaybackThread;
627 friend class MixerThread;
628 friend class DirectOutputThread;
629
630 Track(const Track&);
631 Track& operator = (const Track&);
632
Mike J. Chen535235c2011-08-15 13:28:26 -0700633 virtual status_t getNextBuffer(
634 AudioBufferProvider::Buffer* buffer,
635 int64_t pts);
636 virtual uint32_t framesReady() const;
637
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 bool isMuted() { return mMute; }
639 bool isPausing() const {
640 return mState == PAUSING;
641 }
642 bool isPaused() const {
643 return mState == PAUSED;
644 }
645 bool isReady() const;
646 void setPaused() { mState = PAUSED; }
647 void reset();
648
649 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700650 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 }
652
Mike J. Chen535235c2011-08-15 13:28:26 -0700653 virtual bool isTimedTrack() const { return false; }
654
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 // we don't really need a lock for these
656 float mVolume[2];
657 volatile bool mMute;
658 // FILLED state is used for suppressing volume ramp at begin of playing
659 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
660 mutable uint8_t mFillingUpStatus;
661 int8_t mRetryCount;
662 sp<IMemory> mSharedBuffer;
663 bool mResetDone;
664 int mStreamType;
665 int mName;
666 int16_t *mMainBuffer;
667 int32_t *mAuxBuffer;
668 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700669 bool mHasVolumeController;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 }; // end of Track
671
Mike J. Chen535235c2011-08-15 13:28:26 -0700672 class TimedTrack : public Track {
673 public:
674 static sp<TimedTrack> create(const wp<ThreadBase>& thread,
675 const sp<Client>& client,
676 int streamType,
677 uint32_t sampleRate,
678 uint32_t format,
679 uint32_t channelMask,
680 int frameCount,
681 const sp<IMemory>& sharedBuffer,
682 int sessionId);
683 ~TimedTrack();
684
685 class TimedBuffer {
686 public:
687 TimedBuffer();
688 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
689 const sp<IMemory>& buffer() const { return mBuffer; }
690 int64_t pts() const { return mPTS; }
691 int position() const { return mPosition; }
692 void setPosition(int pos) { mPosition = pos; }
693 private:
694 sp<IMemory> mBuffer;
695 int64_t mPTS;
696 int mPosition;
697 };
698
699 virtual bool isTimedTrack() const { return true; }
700
701 virtual uint32_t framesReady() const;
702
703 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
704 int64_t pts);
705 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
706 void timedYieldSamples(AudioBufferProvider::Buffer* buffer);
707 void timedYieldSilence(uint32_t numFrames,
708 AudioBufferProvider::Buffer* buffer);
709
710 status_t allocateTimedBuffer(size_t size,
711 sp<IMemory>* buffer);
712 status_t queueTimedBuffer(const sp<IMemory>& buffer,
713 int64_t pts);
714 status_t setMediaTimeTransform(const LinearTransform& xform,
715 TimedAudioTrack::TargetTimeline target);
716 void trimTimedBufferQueue_l();
717
718 private:
719 TimedTrack(const wp<ThreadBase>& thread,
720 const sp<Client>& client,
721 int streamType,
722 uint32_t sampleRate,
723 uint32_t format,
724 uint32_t channelMask,
725 int frameCount,
726 const sp<IMemory>& sharedBuffer,
727 int sessionId);
728
729 uint64_t mLocalTimeFreq;
730 LinearTransform mLocalTimeToSampleTransform;
731 sp<MemoryDealer> mTimedMemoryDealer;
732 Vector<TimedBuffer> mTimedBufferQueue;
733 uint8_t* mTimedSilenceBuffer;
734 uint32_t mTimedSilenceBufferSize;
735 mutable Mutex mTimedBufferQueueLock;
736 bool mTimedAudioOutputOnTime;
737
738 Mutex mMediaTimeTransformLock;
739 LinearTransform mMediaTimeTransform;
740 bool mMediaTimeTransformValid;
741 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
742 };
743
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744
745 // playback track
746 class OutputTrack : public Track {
747 public:
748
749 class Buffer: public AudioBufferProvider::Buffer {
750 public:
751 int16_t *mBuffer;
752 };
753
754 OutputTrack( const wp<ThreadBase>& thread,
755 DuplicatingThread *sourceThread,
756 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700757 uint32_t format,
758 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759 int frameCount);
760 ~OutputTrack();
761
762 virtual status_t start();
763 virtual void stop();
764 bool write(int16_t* data, uint32_t frames);
765 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
766 bool isActive() { return mActive; }
767 wp<ThreadBase>& thread() { return mThread; }
768
769 private:
770
771 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
772 void clearBufferQueue();
773
774 // Maximum number of pending buffers allocated by OutputTrack::write()
775 static const uint8_t kMaxOverFlowBuffers = 10;
776
777 Vector < Buffer* > mBufferQueue;
778 AudioBufferProvider::Buffer mOutBuffer;
779 bool mActive;
780 DuplicatingThread* mSourceThread;
781 }; // end of OutputTrack
782
Dima Zavin799a70e2011-04-18 16:57:27 -0700783 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 virtual ~PlaybackThread();
785
786 virtual status_t dump(int fd, const Vector<String16>& args);
787
788 // Thread virtuals
789 virtual status_t readyToRun();
790 virtual void onFirstRef();
791
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700792 virtual status_t initCheck() const { return (mOutput == 0) ? NO_INIT : NO_ERROR; }
793
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 virtual uint32_t latency() const;
795
796 virtual status_t setMasterVolume(float value);
797 virtual status_t setMasterMute(bool muted);
798
799 virtual float masterVolume() const;
800 virtual bool masterMute() const;
801
802 virtual status_t setStreamVolume(int stream, float value);
803 virtual status_t setStreamMute(int stream, bool muted);
804
805 virtual float streamVolume(int stream) const;
806 virtual bool streamMute(int stream) const;
807
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 sp<Track> createTrack_l(
809 const sp<AudioFlinger::Client>& client,
810 int streamType,
811 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700812 uint32_t format,
813 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 int frameCount,
815 const sp<IMemory>& sharedBuffer,
816 int sessionId,
Mike J. Chen535235c2011-08-15 13:28:26 -0700817 bool isTimed,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 status_t *status);
819
Eric Laurentb8ba0a92011-08-07 16:32:26 -0700820 AudioStreamOut* getOutput();
821 AudioStreamOut* clearOutput();
822 virtual audio_stream_t* stream();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 void suspend() { mSuspended++; }
825 void restore() { if (mSuspended) mSuspended--; }
826 bool isSuspended() { return (mSuspended != 0); }
827 virtual String8 getParameters(const String8& keys);
828 virtual void audioConfigChanged_l(int event, int param = 0);
829 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
830 int16_t *mixBuffer() { return mMixBuffer; };
831
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700832 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -0700833 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
834 int EffectId);
835 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
836 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700838 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
839 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
840 virtual uint32_t hasAudioSession(int sessionId);
841 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -0700842
Eric Laurent9f6530f2011-08-30 10:18:54 -0700843 void setStreamValid(int streamType, bool valid);
844
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 struct stream_type_t {
846 stream_type_t()
847 : volume(1.0f),
Eric Laurent9f6530f2011-08-30 10:18:54 -0700848 mute(false),
849 valid(true)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 {
851 }
852 float volume;
853 bool mute;
Eric Laurent9f6530f2011-08-30 10:18:54 -0700854 bool valid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 };
856
857 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 int16_t* mMixBuffer;
859 int mSuspended;
860 int mBytesWritten;
861 bool mMasterMute;
862 SortedVector< wp<Track> > mActiveTracks;
863
864 virtual int getTrackName_l() = 0;
865 virtual void deleteTrackName_l(int name) = 0;
866 virtual uint32_t activeSleepTimeUs() = 0;
867 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700868 virtual uint32_t suspendSleepTimeUs() = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869
870 private:
871
872 friend class AudioFlinger;
873 friend class OutputTrack;
874 friend class Track;
875 friend class TrackBase;
876 friend class MixerThread;
877 friend class DirectOutputThread;
878 friend class DuplicatingThread;
879
880 PlaybackThread(const Client&);
881 PlaybackThread& operator = (const PlaybackThread&);
882
883 status_t addTrack_l(const sp<Track>& track);
884 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -0700885 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886
887 void readOutputParameters();
888
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
890 status_t dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891
892 SortedVector< sp<Track> > mTracks;
893 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -0700894 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin799a70e2011-04-18 16:57:27 -0700895 AudioStreamOut* mOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896 float mMasterVolume;
897 nsecs_t mLastWriteTime;
898 int mNumWrites;
899 int mNumDelayedWrites;
900 bool mInWrite;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 };
902
903 class MixerThread : public PlaybackThread {
904 public:
Eric Laurentde070132010-07-13 04:45:46 -0700905 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700906 AudioStreamOut* output,
Eric Laurentde070132010-07-13 04:45:46 -0700907 int id,
908 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 virtual ~MixerThread();
910
911 // Thread virtuals
912 virtual bool threadLoop();
913
914 void invalidateTracks(int streamType);
915 virtual bool checkForNewParameters_l();
916 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
917
918 protected:
Eric Laurentde070132010-07-13 04:45:46 -0700919 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
920 Vector< sp<Track> > *tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 virtual int getTrackName_l();
922 virtual void deleteTrackName_l(int name);
923 virtual uint32_t activeSleepTimeUs();
924 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700925 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926
927 AudioMixer* mAudioMixer;
928 };
929
930 class DirectOutputThread : public PlaybackThread {
931 public:
932
Dima Zavin799a70e2011-04-18 16:57:27 -0700933 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 ~DirectOutputThread();
935
936 // Thread virtuals
937 virtual bool threadLoop();
938
939 virtual bool checkForNewParameters_l();
940
941 protected:
942 virtual int getTrackName_l();
943 virtual void deleteTrackName_l(int name);
944 virtual uint32_t activeSleepTimeUs();
945 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700946 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947
948 private:
949 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
950
951 float mLeftVolFloat;
952 float mRightVolFloat;
953 uint16_t mLeftVolShort;
954 uint16_t mRightVolShort;
955 };
956
957 class DuplicatingThread : public MixerThread {
958 public:
959 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
960 ~DuplicatingThread();
961
962 // Thread virtuals
963 virtual bool threadLoop();
964 void addOutputTrack(MixerThread* thread);
965 void removeOutputTrack(MixerThread* thread);
966 uint32_t waitTimeMs() { return mWaitTimeMs; }
967 protected:
968 virtual uint32_t activeSleepTimeUs();
969
970 private:
971 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
972 void updateWaitTime();
973
974 SortedVector < sp<OutputTrack> > mOutputTracks;
975 uint32_t mWaitTimeMs;
976 };
977
978 PlaybackThread *checkPlaybackThread_l(int output) const;
979 MixerThread *checkMixerThread_l(int output) const;
980 RecordThread *checkRecordThread_l(int input) const;
981 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
982 void audioConfigChanged_l(int event, int ioHandle, void *param2);
983
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700984 uint32_t nextUniqueId();
Eric Laurent59255e42011-07-27 19:49:51 -0700985 status_t moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -0700986 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -0700987 AudioFlinger::PlaybackThread *dstThread,
988 bool reRegister);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700989 PlaybackThread *primaryPlaybackThread_l();
990 uint32_t primaryOutputDevice_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991
992 friend class AudioBuffer;
993
994 class TrackHandle : public android::BnAudioTrack {
995 public:
996 TrackHandle(const sp<PlaybackThread::Track>& track);
997 virtual ~TrackHandle();
998 virtual status_t start();
999 virtual void stop();
1000 virtual void flush();
1001 virtual void mute(bool);
1002 virtual void pause();
1003 virtual void setVolume(float left, float right);
1004 virtual sp<IMemory> getCblk() const;
1005 virtual status_t attachAuxEffect(int effectId);
Mike J. Chen535235c2011-08-15 13:28:26 -07001006 virtual status_t allocateTimedBuffer(size_t size,
1007 sp<IMemory>* buffer);
1008 virtual status_t queueTimedBuffer(const sp<IMemory>& buffer,
1009 int64_t pts);
1010 virtual status_t setMediaTimeTransform(const LinearTransform& xform,
1011 int target);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 virtual status_t onTransact(
1013 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1014 private:
1015 sp<PlaybackThread::Track> mTrack;
1016 };
1017
1018 friend class Client;
1019 friend class PlaybackThread::Track;
1020
1021
1022 void removeClient_l(pid_t pid);
1023 void removeNotificationClient(pid_t pid);
1024
1025
1026 // record thread
1027 class RecordThread : public ThreadBase, public AudioBufferProvider
1028 {
1029 public:
1030
1031 // record track
1032 class RecordTrack : public TrackBase {
1033 public:
1034 RecordTrack(const wp<ThreadBase>& thread,
1035 const sp<Client>& client,
1036 uint32_t sampleRate,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001037 uint32_t format,
1038 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 int frameCount,
1040 uint32_t flags,
1041 int sessionId);
1042 ~RecordTrack();
1043
1044 virtual status_t start();
1045 virtual void stop();
1046
1047 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
1048 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
1049
1050 void dump(char* buffer, size_t size);
Eric Laurent59255e42011-07-27 19:49:51 -07001051
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 private:
1053 friend class AudioFlinger;
1054 friend class RecordThread;
1055
1056 RecordTrack(const RecordTrack&);
1057 RecordTrack& operator = (const RecordTrack&);
1058
Mike J. Chen535235c2011-08-15 13:28:26 -07001059 virtual status_t getNextBuffer(
1060 AudioBufferProvider::Buffer* buffer,
1061 int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062
1063 bool mOverflow;
1064 };
1065
1066
1067 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -07001068 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 uint32_t sampleRate,
1070 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001071 int id,
1072 uint32_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 ~RecordThread();
1074
1075 virtual bool threadLoop();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001076 virtual status_t readyToRun();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 virtual void onFirstRef();
1078
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001079 virtual status_t initCheck() const { return (mInput == 0) ? NO_INIT : NO_ERROR; }
1080 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
1081 const sp<AudioFlinger::Client>& client,
1082 uint32_t sampleRate,
1083 int format,
1084 int channelMask,
1085 int frameCount,
1086 uint32_t flags,
1087 int sessionId,
1088 status_t *status);
1089
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 status_t start(RecordTrack* recordTrack);
1091 void stop(RecordTrack* recordTrack);
1092 status_t dump(int fd, const Vector<String16>& args);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001093 AudioStreamIn* getInput();
1094 AudioStreamIn* clearInput();
1095 virtual audio_stream_t* stream();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096
Mike J. Chen535235c2011-08-15 13:28:26 -07001097 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
1098 int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
1100 virtual bool checkForNewParameters_l();
1101 virtual String8 getParameters(const String8& keys);
1102 virtual void audioConfigChanged_l(int event, int param = 0);
1103 void readInputParameters();
1104 virtual unsigned int getInputFramesLost();
1105
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001106 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1107 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
1108 virtual uint32_t hasAudioSession(int sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001109 RecordTrack* track();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001110
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111 private:
1112 RecordThread();
Dima Zavin799a70e2011-04-18 16:57:27 -07001113 AudioStreamIn *mInput;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001114 RecordTrack* mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 sp<RecordTrack> mActiveTrack;
1116 Condition mStartStopCond;
1117 AudioResampler *mResampler;
1118 int32_t *mRsmpOutBuffer;
1119 int16_t *mRsmpInBuffer;
1120 size_t mRsmpInIndex;
1121 size_t mInputBytes;
1122 int mReqChannelCount;
1123 uint32_t mReqSampleRate;
1124 ssize_t mBytesRead;
1125 };
1126
1127 class RecordHandle : public android::BnAudioRecord {
1128 public:
1129 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
1130 virtual ~RecordHandle();
1131 virtual status_t start();
1132 virtual void stop();
1133 virtual sp<IMemory> getCblk() const;
1134 virtual status_t onTransact(
1135 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1136 private:
1137 sp<RecordThread::RecordTrack> mRecordTrack;
1138 };
1139
1140 //--- Audio Effect Management
1141
1142 // EffectModule and EffectChain classes both have their own mutex to protect
1143 // state changes or resource modifications. Always respect the following order
1144 // if multiple mutexes must be acquired to avoid cross deadlock:
1145 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1146
1147 // The EffectModule class is a wrapper object controlling the effect engine implementation
1148 // in the effect library. It prevents concurrent calls to process() and command() functions
1149 // from different client threads. It keeps a list of EffectHandle objects corresponding
1150 // to all client applications using this effect and notifies applications of effect state,
1151 // control or parameter changes. It manages the activation state machine to send appropriate
1152 // reset, enable, disable commands to effect engine and provide volume
1153 // ramping when effects are activated/deactivated.
1154 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1155 // the attached track(s) to accumulate their auxiliary channel.
1156 class EffectModule: public RefBase {
1157 public:
1158 EffectModule(const wp<ThreadBase>& wThread,
1159 const wp<AudioFlinger::EffectChain>& chain,
1160 effect_descriptor_t *desc,
1161 int id,
1162 int sessionId);
1163 ~EffectModule();
1164
1165 enum effect_state {
1166 IDLE,
1167 RESTART,
1168 STARTING,
1169 ACTIVE,
1170 STOPPING,
Eric Laurentec437d82011-07-26 20:54:46 -07001171 STOPPED,
1172 DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 };
1174
1175 int id() { return mId; }
1176 void process();
1177 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001178 status_t command(uint32_t cmdCode,
1179 uint32_t cmdSize,
1180 void *pCmdData,
1181 uint32_t *replySize,
1182 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183
1184 void reset_l();
1185 status_t configure();
1186 status_t init();
1187 uint32_t state() {
1188 return mState;
1189 }
1190 uint32_t status() {
1191 return mStatus;
1192 }
Eric Laurentde070132010-07-13 04:45:46 -07001193 int sessionId() {
1194 return mSessionId;
1195 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196 status_t setEnabled(bool enabled);
1197 bool isEnabled();
Eric Laurent8f45bd72010-08-31 13:50:07 -07001198 bool isProcessEnabled();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199
1200 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1201 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1202 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1203 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001204 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1205 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent59255e42011-07-27 19:49:51 -07001206 wp<ThreadBase>& thread() { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207
1208 status_t addHandle(sp<EffectHandle>& handle);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001209 void disconnect(const wp<EffectHandle>& handle, bool unpiniflast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 size_t removeHandle (const wp<EffectHandle>& handle);
1211
1212 effect_descriptor_t& desc() { return mDescriptor; }
1213 wp<EffectChain>& chain() { return mChain; }
1214
1215 status_t setDevice(uint32_t device);
1216 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
1217 status_t setMode(uint32_t mode);
Eric Laurentec437d82011-07-26 20:54:46 -07001218 status_t stop();
Eric Laurent59255e42011-07-27 19:49:51 -07001219 void setSuspended(bool suspended);
1220 bool suspended();
1221
1222 sp<EffectHandle> controlHandle();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001224 bool isPinned() { return mPinned; }
1225 void unPin() { mPinned = false; }
1226
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 status_t dump(int fd, const Vector<String16>& args);
1228
1229 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001230 friend class EffectHandle;
1231 friend class AudioFlinger;
1232 bool mPinned;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233
1234 // Maximum time allocated to effect engines to complete the turn off sequence
1235 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1236
1237 EffectModule(const EffectModule&);
1238 EffectModule& operator = (const EffectModule&);
1239
1240 status_t start_l();
1241 status_t stop_l();
1242
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 Mutex mLock; // mutex for process, commands and handles list protection
1244 wp<ThreadBase> mThread; // parent thread
1245 wp<EffectChain> mChain; // parent effect chain
1246 int mId; // this instance unique ID
1247 int mSessionId; // audio session ID
1248 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1249 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001250 effect_handle_t mEffectInterface; // Effect module C API
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 status_t mStatus; // initialization status
1252 uint32_t mState; // current activation state (effect_state)
1253 Vector< wp<EffectHandle> > mHandles; // list of client handles
1254 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1255 // sending disable command.
1256 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent59255e42011-07-27 19:49:51 -07001257 bool mSuspended; // effect is suspended: temporarily disabled by framework
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258 };
1259
1260 // The EffectHandle class implements the IEffect interface. It provides resources
1261 // to receive parameter updates, keeps track of effect control
1262 // ownership and state and has a pointer to the EffectModule object it is controlling.
1263 // There is one EffectHandle object for each application controlling (or using)
1264 // an effect module.
1265 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1266 class EffectHandle: public android::BnEffect {
1267 public:
1268
1269 EffectHandle(const sp<EffectModule>& effect,
1270 const sp<AudioFlinger::Client>& client,
1271 const sp<IEffectClient>& effectClient,
1272 int32_t priority);
1273 virtual ~EffectHandle();
1274
1275 // IEffect
1276 virtual status_t enable();
1277 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001278 virtual status_t command(uint32_t cmdCode,
1279 uint32_t cmdSize,
1280 void *pCmdData,
1281 uint32_t *replySize,
1282 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 virtual void disconnect();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001284 virtual void disconnect(bool unpiniflast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 virtual sp<IMemory> getCblk() const;
1286 virtual status_t onTransact(uint32_t code, const Parcel& data,
1287 Parcel* reply, uint32_t flags);
1288
1289
1290 // Give or take control of effect module
Eric Laurent59255e42011-07-27 19:49:51 -07001291 // - hasControl: true if control is given, false if removed
1292 // - signal: true client app should be signaled of change, false otherwise
1293 // - enabled: state of the effect when control is passed
1294 void setControl(bool hasControl, bool signal, bool enabled);
Eric Laurent25f43952010-07-28 05:40:18 -07001295 void commandExecuted(uint32_t cmdCode,
1296 uint32_t cmdSize,
1297 void *pCmdData,
1298 uint32_t replySize,
1299 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300 void setEnabled(bool enabled);
Eric Laurent59255e42011-07-27 19:49:51 -07001301 bool enabled() { return mEnabled; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302
1303 // Getters
1304 int id() { return mEffect->id(); }
1305 int priority() { return mPriority; }
1306 bool hasControl() { return mHasControl; }
1307 sp<EffectModule> effect() { return mEffect; }
1308
1309 void dump(char* buffer, size_t size);
1310
1311 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001312 friend class AudioFlinger;
1313 friend class EffectModule;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 EffectHandle(const EffectHandle&);
1315 EffectHandle& operator =(const EffectHandle&);
1316
1317 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1318 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1319 sp<Client> mClient; // client for shared memory allocation
1320 sp<IMemory> mCblkMemory; // shared memory for control block
1321 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1322 uint8_t* mBuffer; // pointer to parameter area in shared memory
1323 int mPriority; // client application priority to control the effect
1324 bool mHasControl; // true if this handle is controlling the effect
Eric Laurent59255e42011-07-27 19:49:51 -07001325 bool mEnabled; // cached enable state: needed when the effect is
1326 // restored after being suspended
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 };
1328
1329 // the EffectChain class represents a group of effects associated to one audio session.
1330 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1331 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1332 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1333 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1334 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1335 // input buffer used by the track as accumulation buffer.
1336 class EffectChain: public RefBase {
1337 public:
1338 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1339 ~EffectChain();
1340
Eric Laurent59255e42011-07-27 19:49:51 -07001341 // special key used for an entry in mSuspendedEffects keyed vector
1342 // corresponding to a suspend all request.
1343 static const int kKeyForSuspendAll = 0;
1344
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 void process_l();
1346
1347 void lock() {
1348 mLock.lock();
1349 }
1350 void unlock() {
1351 mLock.unlock();
1352 }
1353
Eric Laurentde070132010-07-13 04:45:46 -07001354 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001355 size_t removeEffect_l(const sp<EffectModule>& handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001357 int sessionId() { return mSessionId; }
1358 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001359
Eric Laurentcab11242010-07-15 12:50:15 -07001360 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1361 sp<EffectModule> getEffectFromId_l(int id);
Eric Laurent59255e42011-07-27 19:49:51 -07001362 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurentcab11242010-07-15 12:50:15 -07001363 bool setVolume_l(uint32_t *left, uint32_t *right);
1364 void setDevice_l(uint32_t device);
1365 void setMode_l(uint32_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366
Mathias Agopian65ab4712010-07-14 17:59:35 -07001367 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1368 mInBuffer = buffer;
1369 mOwnInBuffer = ownsBuffer;
1370 }
1371 int16_t *inBuffer() {
1372 return mInBuffer;
1373 }
1374 void setOutBuffer(int16_t *buffer) {
1375 mOutBuffer = buffer;
1376 }
1377 int16_t *outBuffer() {
1378 return mOutBuffer;
1379 }
1380
Eric Laurentb469b942011-05-09 12:09:06 -07001381 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1382 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1383 int32_t trackCnt() { return mTrackCnt;}
1384
1385 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1386 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1387 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388
Eric Laurentde070132010-07-13 04:45:46 -07001389 uint32_t strategy() { return mStrategy; }
1390 void setStrategy(uint32_t strategy)
1391 { mStrategy = strategy; }
1392
Eric Laurent59255e42011-07-27 19:49:51 -07001393 // suspend effect of the given type
1394 void setEffectSuspended_l(const effect_uuid_t *type,
1395 bool suspend);
1396 // suspend all eligible effects
1397 void setEffectSuspendedAll_l(bool suspend);
1398 // check if effects should be suspend or restored when a given effect is enable or disabled
1399 virtual void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1400 bool enabled);
1401
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 status_t dump(int fd, const Vector<String16>& args);
1403
1404 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001405 friend class AudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 EffectChain(const EffectChain&);
1407 EffectChain& operator =(const EffectChain&);
1408
Eric Laurent59255e42011-07-27 19:49:51 -07001409 class SuspendedEffectDesc : public RefBase {
1410 public:
1411 SuspendedEffectDesc() : mRefCount(0) {}
1412
1413 int mRefCount;
1414 effect_uuid_t mType;
1415 wp<EffectModule> mEffect;
1416 };
1417
1418 // get a list of effect modules to suspend when an effect of the type
1419 // passed is enabled.
1420 Vector< sp<EffectModule> > getSuspendEligibleEffects();
1421 // get an effect module if it is currently enable
1422 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurentdb7c0792011-08-10 10:37:50 -07001423 // true if the effect whose descriptor is passed can be suspended
1424 // OEMs can modify the rules implemented in this method to exclude specific effect
1425 // types or implementations from the suspend/restore mechanism.
1426 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
Eric Laurent59255e42011-07-27 19:49:51 -07001427
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 wp<ThreadBase> mThread; // parent mixer thread
1429 Mutex mLock; // mutex protecting effect list
1430 Vector<sp<EffectModule> > mEffects; // list of effect modules
1431 int mSessionId; // audio session ID
1432 int16_t *mInBuffer; // chain input buffer
1433 int16_t *mOutBuffer; // chain output buffer
Eric Laurentb469b942011-05-09 12:09:06 -07001434 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1435 volatile int32_t mTrackCnt; // number of tracks connected
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07001437 int mVolumeCtrlIdx; // index of insert effect having control over volume
1438 uint32_t mLeftVolume; // previous volume on left channel
1439 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07001440 uint32_t mNewLeftVolume; // new volume on left channel
1441 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07001442 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent59255e42011-07-27 19:49:51 -07001443 // mSuspendedEffects lists all effect currently suspended in the chain
1444 // use effect type UUID timelow field as key. There is no real risk of identical
1445 // timeLow fields among effect type UUIDs.
1446 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 };
1448
Dima Zavin799a70e2011-04-18 16:57:27 -07001449 struct AudioStreamOut {
1450 audio_hw_device_t *hwDev;
1451 audio_stream_out_t *stream;
1452
1453 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1454 hwDev(dev), stream(out) {}
1455 };
1456
1457 struct AudioStreamIn {
1458 audio_hw_device_t *hwDev;
1459 audio_stream_in_t *stream;
1460
1461 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1462 hwDev(dev), stream(in) {}
1463 };
1464
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001465 struct AudioSessionRef {
1466 int sessionid;
1467 pid_t pid;
1468 int cnt;
1469 };
1470
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 friend class RecordThread;
1472 friend class PlaybackThread;
1473
Mike J. Chen535235c2011-08-15 13:28:26 -07001474 enum master_volume_support {
1475 MVS_NONE,
1476 MVS_SETONLY,
1477 MVS_FULL,
1478 };
1479
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 mutable Mutex mLock;
1481
1482 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1483
1484 mutable Mutex mHardwareLock;
Dima Zavin799a70e2011-04-18 16:57:27 -07001485 audio_hw_device_t* mPrimaryHardwareDev;
1486 Vector<audio_hw_device_t*> mAudioHwDevs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 mutable int mHardwareStatus;
1488
1489
1490 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavinfce7a472011-04-19 22:30:36 -07001491 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 float mMasterVolume;
Mike J. Chen535235c2011-08-15 13:28:26 -07001493 float mMasterVolumeSW;
1494 master_volume_support mMasterVolumeSupportLvl;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495 bool mMasterMute;
1496
1497 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
1498
1499 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
1500 volatile int32_t mNextUniqueId;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001501 uint32_t mMode;
Eric Laurentbee53372011-08-29 12:42:48 -07001502 bool mBtNrecIsOff;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001504 Vector<AudioSessionRef*> mAudioSessionRefs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505};
1506
Dima Zavin799a70e2011-04-18 16:57:27 -07001507
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508// ----------------------------------------------------------------------------
1509
1510}; // namespace android
1511
1512#endif // ANDROID_AUDIO_FLINGER_H