blob: 674b0475d763bf9e6a3947ca4e6fd2296617dc21 [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>
Glenn Kasten335787f2012-01-20 17:00:00 -080029#include <media/AudioSystem.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030
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
Mathias Agopian65ab4712010-07-14 17:59:35 -070058static const nsecs_t kStandbyTimeInNsecs = seconds(3);
59
Mathias Agopian5462fc92010-07-14 18:41:18 -070060class AudioFlinger :
61 public BinderService<AudioFlinger>,
62 public BnAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
Mathias Agopian5462fc92010-07-14 18:41:18 -070064 friend class BinderService<AudioFlinger>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070065public:
Glenn Kasten54c3b662012-01-06 07:46:30 -080066 static const char* getServiceName() { return "media.audio_flinger"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
68 virtual status_t dump(int fd, const Vector<String16>& args);
69
70 // IAudioFlinger interface
71 virtual sp<IAudioTrack> createTrack(
72 pid_t pid,
Glenn Kastenfff6d712012-01-12 16:38:12 -080073 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -080075 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070076 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -070077 int frameCount,
78 uint32_t flags,
79 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -080080 audio_io_handle_t output,
Mathias Agopian65ab4712010-07-14 17:59:35 -070081 int *sessionId,
82 status_t *status);
83
Glenn Kasten72ef00d2012-01-17 11:09:42 -080084 virtual uint32_t sampleRate(audio_io_handle_t output) const;
85 virtual int channelCount(audio_io_handle_t output) const;
86 virtual audio_format_t format(audio_io_handle_t output) const;
87 virtual size_t frameCount(audio_io_handle_t output) const;
88 virtual uint32_t latency(audio_io_handle_t output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -070089
90 virtual status_t setMasterVolume(float value);
91 virtual status_t setMasterMute(bool muted);
92
93 virtual float masterVolume() const;
94 virtual bool masterMute() const;
95
Glenn Kasten72ef00d2012-01-17 11:09:42 -080096 virtual status_t setStreamVolume(audio_stream_type_t stream, float value,
97 audio_io_handle_t output);
Glenn Kastenfff6d712012-01-12 16:38:12 -080098 virtual status_t setStreamMute(audio_stream_type_t stream, bool muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -070099
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800100 virtual float streamVolume(audio_stream_type_t stream,
101 audio_io_handle_t output) const;
Glenn Kastenfff6d712012-01-12 16:38:12 -0800102 virtual bool streamMute(audio_stream_type_t stream) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103
Glenn Kastenf78aee72012-01-04 11:00:47 -0800104 virtual status_t setMode(audio_mode_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105
106 virtual status_t setMicMute(bool state);
107 virtual bool getMicMute() const;
108
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800109 virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
110 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111
112 virtual void registerClient(const sp<IAudioFlingerClient>& client);
113
Glenn Kastenf587ba52012-01-26 16:25:10 -0800114 virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800115 virtual unsigned int getInputFramesLost(audio_io_handle_t ioHandle) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700116
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800117 virtual audio_io_handle_t openOutput(uint32_t *pDevices,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118 uint32_t *pSamplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800119 audio_format_t *pFormat,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120 uint32_t *pChannels,
121 uint32_t *pLatencyMs,
122 uint32_t flags);
123
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800124 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
125 audio_io_handle_t output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800127 virtual status_t closeOutput(audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800129 virtual status_t suspendOutput(audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800131 virtual status_t restoreOutput(audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800133 virtual audio_io_handle_t openInput(uint32_t *pDevices,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 uint32_t *pSamplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800135 audio_format_t *pFormat,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136 uint32_t *pChannels,
Glenn Kastende9719b2012-01-27 12:32:34 -0800137 audio_in_acoustics_t acoustics);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800139 virtual status_t closeInput(audio_io_handle_t input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800141 virtual status_t setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142
143 virtual status_t setVoiceVolume(float volume);
144
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800145 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
146 audio_io_handle_t output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147
148 virtual int newAudioSessionId();
149
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700150 virtual void acquireAudioSessionId(int audioSession);
151
152 virtual void releaseAudioSessionId(int audioSession);
153
Glenn Kastenf587ba52012-01-26 16:25:10 -0800154 virtual status_t queryNumberEffects(uint32_t *numEffects) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155
Glenn Kastenf587ba52012-01-26 16:25:10 -0800156 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157
Glenn Kasten5e92a782012-01-30 07:40:52 -0800158 virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -0800159 effect_descriptor_t *descriptor) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160
161 virtual sp<IEffect> createEffect(pid_t pid,
162 effect_descriptor_t *pDesc,
163 const sp<IEffectClient>& effectClient,
164 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800165 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166 int sessionId,
167 status_t *status,
168 int *id,
169 int *enabled);
170
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800171 virtual status_t moveEffects(int sessionId, audio_io_handle_t srcOutput,
172 audio_io_handle_t dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173
174 enum hardware_call_state {
175 AUDIO_HW_IDLE = 0,
176 AUDIO_HW_INIT,
177 AUDIO_HW_OUTPUT_OPEN,
178 AUDIO_HW_OUTPUT_CLOSE,
179 AUDIO_HW_INPUT_OPEN,
180 AUDIO_HW_INPUT_CLOSE,
181 AUDIO_HW_STANDBY,
182 AUDIO_HW_SET_MASTER_VOLUME,
183 AUDIO_HW_GET_ROUTING,
184 AUDIO_HW_SET_ROUTING,
185 AUDIO_HW_GET_MODE,
186 AUDIO_HW_SET_MODE,
187 AUDIO_HW_GET_MIC_MUTE,
188 AUDIO_HW_SET_MIC_MUTE,
189 AUDIO_SET_VOICE_VOLUME,
190 AUDIO_SET_PARAMETER,
191 };
192
193 // record interface
194 virtual sp<IAudioRecord> openRecord(
195 pid_t pid,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800196 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800198 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700199 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200 int frameCount,
201 uint32_t flags,
202 int *sessionId,
203 status_t *status);
204
205 virtual status_t onTransact(
206 uint32_t code,
207 const Parcel& data,
208 Parcel* reply,
209 uint32_t flags);
210
Glenn Kastenf78aee72012-01-04 11:00:47 -0800211 audio_mode_t getMode() const { return mMode; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212
Glenn Kastenc59c0042012-02-02 14:06:11 -0800213 bool btNrecIsOff() const { return mBtNrecIsOff; }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700214
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215private:
Glenn Kasten335787f2012-01-20 17:00:00 -0800216
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 AudioFlinger();
218 virtual ~AudioFlinger();
219
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700220 status_t initCheck() const;
221 virtual void onFirstRef();
Dima Zavin799a70e2011-04-18 16:57:27 -0700222 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700223 void purgeStaleEffects_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224
225 // Internal dump utilites.
226 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
227 status_t dumpClients(int fd, const Vector<String16>& args);
228 status_t dumpInternals(int fd, const Vector<String16>& args);
229
230 // --- Client ---
231 class Client : public RefBase {
232 public:
233 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
234 virtual ~Client();
Glenn Kasten435dbe62012-01-30 10:15:48 -0800235 sp<MemoryDealer> heap() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 pid_t pid() const { return mPid; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800237 sp<AudioFlinger> audioFlinger() const { return mAudioFlinger; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238
239 private:
240 Client(const Client&);
241 Client& operator = (const Client&);
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800242 const sp<AudioFlinger> mAudioFlinger;
243 const sp<MemoryDealer> mMemoryDealer;
244 const pid_t mPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245 };
246
247 // --- Notification Client ---
248 class NotificationClient : public IBinder::DeathRecipient {
249 public:
250 NotificationClient(const sp<AudioFlinger>& audioFlinger,
251 const sp<IAudioFlingerClient>& client,
252 pid_t pid);
253 virtual ~NotificationClient();
254
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800255 sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256
257 // IBinder::DeathRecipient
258 virtual void binderDied(const wp<IBinder>& who);
259
260 private:
261 NotificationClient(const NotificationClient&);
262 NotificationClient& operator = (const NotificationClient&);
263
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800264 const sp<AudioFlinger> mAudioFlinger;
265 const pid_t mPid;
266 const sp<IAudioFlingerClient> mAudioFlingerClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 };
268
269 class TrackHandle;
270 class RecordHandle;
271 class RecordThread;
272 class PlaybackThread;
273 class MixerThread;
274 class DirectOutputThread;
275 class DuplicatingThread;
276 class Track;
277 class RecordTrack;
278 class EffectModule;
279 class EffectHandle;
280 class EffectChain;
Dima Zavin799a70e2011-04-18 16:57:27 -0700281 struct AudioStreamOut;
282 struct AudioStreamIn;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283
284 class ThreadBase : public Thread {
285 public:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800287 enum type_t {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700288 MIXER, // Thread class is MixerThread
289 DIRECT, // Thread class is DirectOutputThread
290 DUPLICATING, // Thread class is DuplicatingThread
291 RECORD // Thread class is RecordThread
292 };
293
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800294 ThreadBase (const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, uint32_t device, type_t type);
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800295 virtual ~ThreadBase();
296
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297 status_t dumpBase(int fd, const Vector<String16>& args);
Eric Laurent1d2bff02011-07-24 17:49:51 -0700298 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299
Eric Laurentfeb0db62011-07-22 09:04:31 -0700300 void clearPowerManager();
301
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 // base for record and playback
303 class TrackBase : public AudioBufferProvider, public RefBase {
304
305 public:
306 enum track_state {
307 IDLE,
308 TERMINATED,
309 STOPPED,
310 RESUMING,
311 ACTIVE,
312 PAUSING,
313 PAUSED
314 };
315
316 enum track_flags {
317 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
318 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
319 // The upper 16 bits are used for track-specific flags.
320 };
321
322 TrackBase(const wp<ThreadBase>& thread,
323 const sp<Client>& client,
324 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800325 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700326 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 int frameCount,
328 uint32_t flags,
329 const sp<IMemory>& sharedBuffer,
330 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800331 virtual ~TrackBase();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332
333 virtual status_t start() = 0;
334 virtual void stop() = 0;
Glenn Kastenc59c0042012-02-02 14:06:11 -0800335 sp<IMemory> getCblk() const { return mCblkMemory; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 audio_track_cblk_t* cblk() const { return mCblk; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800337 int sessionId() const { return mSessionId; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338
339 protected:
340 friend class ThreadBase;
341 friend class RecordHandle;
342 friend class PlaybackThread;
343 friend class RecordThread;
344 friend class MixerThread;
345 friend class DirectOutputThread;
346
347 TrackBase(const TrackBase&);
348 TrackBase& operator = (const TrackBase&);
349
350 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
351 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
352
Glenn Kasten58f30212012-01-12 12:27:51 -0800353 audio_format_t format() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 return mFormat;
355 }
356
Glenn Kastenc59c0042012-02-02 14:06:11 -0800357 int channelCount() const { return mChannelCount; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358
Glenn Kastenc59c0042012-02-02 14:06:11 -0800359 uint32_t channelMask() const { return mChannelMask; }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700360
Glenn Kastenc59c0042012-02-02 14:06:11 -0800361 int sampleRate() const; // FIXME inline after cblk sr moved
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362
363 void* getBuffer(uint32_t offset, uint32_t frames) const;
364
365 bool isStopped() const {
366 return mState == STOPPED;
367 }
368
369 bool isTerminated() const {
370 return mState == TERMINATED;
371 }
372
373 bool step();
374 void reset();
375
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800376 const wp<ThreadBase> mThread;
377 /*const*/ sp<Client> mClient; // see explanation at ~TrackBase() why not const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 sp<IMemory> mCblkMemory;
379 audio_track_cblk_t* mCblk;
380 void* mBuffer;
381 void* mBufferEnd;
382 uint32_t mFrameCount;
383 // we don't really need a lock for these
Glenn Kastenb853e982012-01-26 13:39:18 -0800384 track_state mState;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800385 const audio_format_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 uint32_t mFlags;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800387 const int mSessionId;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700388 uint8_t mChannelCount;
389 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 };
391
392 class ConfigEvent {
393 public:
394 ConfigEvent() : mEvent(0), mParam(0) {}
395
396 int mEvent;
397 int mParam;
398 };
399
Eric Laurentfeb0db62011-07-22 09:04:31 -0700400 class PMDeathRecipient : public IBinder::DeathRecipient {
401 public:
402 PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
403 virtual ~PMDeathRecipient() {}
404
405 // IBinder::DeathRecipient
406 virtual void binderDied(const wp<IBinder>& who);
407
408 private:
409 PMDeathRecipient(const PMDeathRecipient&);
410 PMDeathRecipient& operator = (const PMDeathRecipient&);
411
412 wp<ThreadBase> mThread;
413 };
414
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700415 virtual status_t initCheck() const = 0;
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800416 type_t type() const { return mType; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800417 uint32_t sampleRate() const { return mSampleRate; }
418 int channelCount() const { return mChannelCount; }
419 audio_format_t format() const { return mFormat; }
420 size_t frameCount() const { return mFrameCount; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 void wakeUp() { mWaitWorkCV.broadcast(); }
422 void exit();
423 virtual bool checkForNewParameters_l() = 0;
424 virtual status_t setParameters(const String8& keyValuePairs);
425 virtual String8 getParameters(const String8& keys) = 0;
426 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
427 void sendConfigEvent(int event, int param = 0);
428 void sendConfigEvent_l(int event, int param = 0);
429 void processConfigEvents();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800430 audio_io_handle_t id() const { return mId;}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 bool standby() { return mStandby; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700432 uint32_t device() { return mDevice; }
433 virtual audio_stream_t* stream() = 0;
434
435 sp<EffectHandle> createEffect_l(
436 const sp<AudioFlinger::Client>& client,
437 const sp<IEffectClient>& effectClient,
438 int32_t priority,
439 int sessionId,
440 effect_descriptor_t *desc,
441 int *enabled,
442 status_t *status);
443 void disconnectEffect(const sp< EffectModule>& effect,
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700444 const wp<EffectHandle>& handle,
445 bool unpiniflast);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700446
447 // return values for hasAudioSession (bit field)
448 enum effect_state {
449 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
450 // effect
451 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
452 // track
453 };
454
455 // get effect chain corresponding to session Id.
456 sp<EffectChain> getEffectChain(int sessionId);
457 // same as getEffectChain() but must be called with ThreadBase mutex locked
458 sp<EffectChain> getEffectChain_l(int sessionId);
459 // add an effect chain to the chain list (mEffectChains)
460 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
461 // remove an effect chain from the chain list (mEffectChains)
462 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
463 // lock mall effect chains Mutexes. Must be called before releasing the
464 // ThreadBase mutex before processing the mixer and effects. This guarantees the
465 // integrity of the chains during the process.
466 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
467 // unlock effect chains after process
468 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
469 // set audio mode to all effect chains
Glenn Kastenf78aee72012-01-04 11:00:47 -0800470 void setMode(audio_mode_t mode);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700471 // get effect module with corresponding ID on specified audio session
472 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
473 // add and effect module. Also creates the effect chain is none exists for
474 // the effects audio session
475 status_t addEffect_l(const sp< EffectModule>& effect);
476 // remove and effect module. Also removes the effect chain is this was the last
477 // effect
478 void removeEffect_l(const sp< EffectModule>& effect);
479 // detach all tracks connected to an auxiliary effect
480 virtual void detachAuxEffect_l(int effectId) {}
481 // returns either EFFECT_SESSION if effects on this audio session exist in one
482 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
483 virtual uint32_t hasAudioSession(int sessionId) = 0;
484 // the value returned by default implementation is not important as the
485 // strategy is only meaningful for PlaybackThread which implements this method
486 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487
Eric Laurent59255e42011-07-27 19:49:51 -0700488 // suspend or restore effect according to the type of effect passed. a NULL
489 // type pointer means suspend all effects in the session
490 void setEffectSuspended(const effect_uuid_t *type,
491 bool suspend,
492 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
493 // check if some effects must be suspended/restored when an effect is enabled
494 // or disabled
Eric Laurenta85a74a2011-10-19 11:44:54 -0700495 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
Eric Laurent59255e42011-07-27 19:49:51 -0700496 bool enabled,
497 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
Eric Laurenta85a74a2011-10-19 11:44:54 -0700498 void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
499 bool enabled,
500 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 mutable Mutex mLock;
502
503 protected:
504
Eric Laurent59255e42011-07-27 19:49:51 -0700505 // entry describing an effect being suspended in mSuspendedSessions keyed vector
506 class SuspendedSessionDesc : public RefBase {
507 public:
508 SuspendedSessionDesc() : mRefCount(0) {}
509
510 int mRefCount; // number of active suspend requests
511 effect_uuid_t mType; // effect type UUID
512 };
513
Eric Laurentfeb0db62011-07-22 09:04:31 -0700514 void acquireWakeLock();
515 void acquireWakeLock_l();
516 void releaseWakeLock();
517 void releaseWakeLock_l();
Eric Laurent59255e42011-07-27 19:49:51 -0700518 void setEffectSuspended_l(const effect_uuid_t *type,
519 bool suspend,
520 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
521 // updated mSuspendedSessions when an effect suspended or restored
522 void updateSuspendedSessions_l(const effect_uuid_t *type,
523 bool suspend,
524 int sessionId);
525 // check if some effects must be suspended when an effect chain is added
526 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
Eric Laurentfeb0db62011-07-22 09:04:31 -0700527
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700528 friend class AudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 friend class Track;
530 friend class TrackBase;
531 friend class PlaybackThread;
532 friend class MixerThread;
533 friend class DirectOutputThread;
534 friend class DuplicatingThread;
535 friend class RecordThread;
536 friend class RecordTrack;
537
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800538 const type_t mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 Condition mWaitWorkCV;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800540 const sp<AudioFlinger> mAudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 uint32_t mSampleRate;
542 size_t mFrameCount;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700543 uint32_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544 uint16_t mChannelCount;
Glenn Kastenb9980652012-01-11 09:48:27 -0800545 size_t mFrameSize;
Glenn Kasten58f30212012-01-12 12:27:51 -0800546 audio_format_t mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 Condition mParamCond;
548 Vector<String8> mNewParameters;
549 status_t mParamStatus;
Glenn Kastenf3990f22011-12-13 11:50:00 -0800550 Vector<ConfigEvent> mConfigEvents;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 bool mStandby;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800552 const audio_io_handle_t mId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 bool mExiting;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700554 Vector< sp<EffectChain> > mEffectChains;
555 uint32_t mDevice; // output device for PlaybackThread
556 // input + output devices for RecordThread
Eric Laurentfeb0db62011-07-22 09:04:31 -0700557 static const int kNameLength = 32;
558 char mName[kNameLength];
559 sp<IPowerManager> mPowerManager;
560 sp<IBinder> mWakeLockToken;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800561 const sp<PMDeathRecipient> mDeathRecipient;
Eric Laurent59255e42011-07-27 19:49:51 -0700562 // list of suspended effects per session and per type. The first vector is
563 // keyed by session ID, the second by type UUID timeLow field
564 KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > > mSuspendedSessions;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565 };
566
567 // --- PlaybackThread ---
568 class PlaybackThread : public ThreadBase {
569 public:
570
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 enum mixer_state {
572 MIXER_IDLE,
573 MIXER_TRACKS_ENABLED,
574 MIXER_TRACKS_READY
575 };
576
577 // playback track
578 class Track : public TrackBase {
579 public:
580 Track( const wp<ThreadBase>& thread,
581 const sp<Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800582 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800584 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700585 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 int frameCount,
587 const sp<IMemory>& sharedBuffer,
588 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800589 virtual ~Track();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590
591 void dump(char* buffer, size_t size);
592 virtual status_t start();
593 virtual void stop();
594 void pause();
595
596 void flush();
597 void destroy();
598 void mute(bool);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 int name() const {
600 return mName;
601 }
602
Glenn Kastenfff6d712012-01-12 16:38:12 -0800603 audio_stream_type_t type() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 return mStreamType;
605 }
606 status_t attachAuxEffect(int EffectId);
607 void setAuxBuffer(int EffectId, int32_t *buffer);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800608 int32_t *auxBuffer() const { return mAuxBuffer; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800610 int16_t *mainBuffer() const { return mMainBuffer; }
611 int auxEffectId() const { return mAuxEffectId; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612
613
614 protected:
615 friend class ThreadBase;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616 friend class TrackHandle;
617 friend class PlaybackThread;
618 friend class MixerThread;
619 friend class DirectOutputThread;
620
621 Track(const Track&);
622 Track& operator = (const Track&);
623
624 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800625 bool isMuted() const { return mMute; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 bool isPausing() const {
627 return mState == PAUSING;
628 }
629 bool isPaused() const {
630 return mState == PAUSED;
631 }
632 bool isReady() const;
633 void setPaused() { mState = PAUSED; }
634 void reset();
635
636 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700637 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 }
639
640 // we don't really need a lock for these
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 volatile bool mMute;
642 // FILLED state is used for suppressing volume ramp at begin of playing
643 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
644 mutable uint8_t mFillingUpStatus;
645 int8_t mRetryCount;
646 sp<IMemory> mSharedBuffer;
647 bool mResetDone;
Glenn Kastenfff6d712012-01-12 16:38:12 -0800648 audio_stream_type_t mStreamType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 int mName;
650 int16_t *mMainBuffer;
651 int32_t *mAuxBuffer;
652 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700653 bool mHasVolumeController;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 }; // end of Track
655
656
657 // playback track
658 class OutputTrack : public Track {
659 public:
660
661 class Buffer: public AudioBufferProvider::Buffer {
662 public:
663 int16_t *mBuffer;
664 };
665
666 OutputTrack( const wp<ThreadBase>& thread,
667 DuplicatingThread *sourceThread,
668 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800669 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700670 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 int frameCount);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800672 virtual ~OutputTrack();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673
674 virtual status_t start();
675 virtual void stop();
676 bool write(int16_t* data, uint32_t frames);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800677 bool bufferQueueEmpty() const { return (mBufferQueue.size() == 0) ? true : false; }
678 bool isActive() const { return mActive; }
679 const wp<ThreadBase>& thread() const { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680
681 private:
682
Glenn Kasten335787f2012-01-20 17:00:00 -0800683 enum {
684 NO_MORE_BUFFERS = 0x80000001, // same in AudioTrack.h, ok to be different value
685 };
686
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
688 void clearBufferQueue();
689
690 // Maximum number of pending buffers allocated by OutputTrack::write()
691 static const uint8_t kMaxOverFlowBuffers = 10;
692
693 Vector < Buffer* > mBufferQueue;
694 AudioBufferProvider::Buffer mOutBuffer;
695 bool mActive;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800696 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 }; // end of OutputTrack
698
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800699 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
700 audio_io_handle_t id, uint32_t device, type_t type);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 virtual ~PlaybackThread();
702
703 virtual status_t dump(int fd, const Vector<String16>& args);
704
705 // Thread virtuals
706 virtual status_t readyToRun();
707 virtual void onFirstRef();
708
Glenn Kastene0feee32011-12-13 11:53:26 -0800709 virtual status_t initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700710
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 virtual uint32_t latency() const;
712
Glenn Kasten6637baa2012-01-09 09:40:36 -0800713 void setMasterVolume(float value);
714 void setMasterMute(bool muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715
Glenn Kasten6637baa2012-01-09 09:40:36 -0800716 void setStreamVolume(audio_stream_type_t stream, float value);
717 void setStreamMute(audio_stream_type_t stream, bool muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718
Glenn Kasten6637baa2012-01-09 09:40:36 -0800719 float streamVolume(audio_stream_type_t stream) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 sp<Track> createTrack_l(
722 const sp<AudioFlinger::Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800723 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800725 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700726 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 int frameCount,
728 const sp<IMemory>& sharedBuffer,
729 int sessionId,
730 status_t *status);
731
Glenn Kastenaed850d2012-01-26 09:46:34 -0800732 AudioStreamOut* getOutput() const;
Eric Laurentb8ba0a92011-08-07 16:32:26 -0700733 AudioStreamOut* clearOutput();
734 virtual audio_stream_t* stream();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 void suspend() { mSuspended++; }
737 void restore() { if (mSuspended) mSuspended--; }
Glenn Kastena3a85482012-01-04 11:01:11 -0800738 bool isSuspended() const { return (mSuspended != 0); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739 virtual String8 getParameters(const String8& keys);
740 virtual void audioConfigChanged_l(int event, int param = 0);
741 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800742 int16_t *mixBuffer() const { return mMixBuffer; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700744 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -0700745 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
746 int EffectId);
747 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
748 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700750 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
751 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
752 virtual uint32_t hasAudioSession(int sessionId);
753 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -0700754
Glenn Kastenfff6d712012-01-12 16:38:12 -0800755 void setStreamValid(audio_stream_type_t streamType, bool valid);
Eric Laurent9f6530f2011-08-30 10:18:54 -0700756
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757 struct stream_type_t {
758 stream_type_t()
759 : volume(1.0f),
Eric Laurent9f6530f2011-08-30 10:18:54 -0700760 mute(false),
761 valid(true)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762 {
763 }
764 float volume;
765 bool mute;
Eric Laurent9f6530f2011-08-30 10:18:54 -0700766 bool valid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 };
768
769 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770 int16_t* mMixBuffer;
771 int mSuspended;
772 int mBytesWritten;
Glenn Kasten98067102011-12-13 11:47:54 -0800773 private:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 bool mMasterMute;
Glenn Kasten6637baa2012-01-09 09:40:36 -0800775 void setMasterMute_l(bool muted) { mMasterMute = muted; }
Glenn Kasten98067102011-12-13 11:47:54 -0800776 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 SortedVector< wp<Track> > mActiveTracks;
778
779 virtual int getTrackName_l() = 0;
780 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent162b40b2011-12-05 09:47:19 -0800781 virtual uint32_t activeSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700783 virtual uint32_t suspendSleepTimeUs() = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784
785 private:
786
787 friend class AudioFlinger;
788 friend class OutputTrack;
789 friend class Track;
790 friend class TrackBase;
791 friend class MixerThread;
792 friend class DirectOutputThread;
793 friend class DuplicatingThread;
794
795 PlaybackThread(const Client&);
796 PlaybackThread& operator = (const PlaybackThread&);
797
798 status_t addTrack_l(const sp<Track>& track);
799 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -0700800 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801
802 void readOutputParameters();
803
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
805 status_t dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806
807 SortedVector< sp<Track> > mTracks;
Glenn Kasten263709e2012-01-06 08:40:01 -0800808 // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -0700809 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Glenn Kastenaed850d2012-01-26 09:46:34 -0800810 AudioStreamOut *mOutput;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 float mMasterVolume;
812 nsecs_t mLastWriteTime;
813 int mNumWrites;
814 int mNumDelayedWrites;
815 bool mInWrite;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 };
817
818 class MixerThread : public PlaybackThread {
819 public:
Eric Laurentde070132010-07-13 04:45:46 -0700820 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700821 AudioStreamOut* output,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800822 audio_io_handle_t id,
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800823 uint32_t device,
824 type_t type = MIXER);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 virtual ~MixerThread();
826
827 // Thread virtuals
828 virtual bool threadLoop();
829
Glenn Kastenfff6d712012-01-12 16:38:12 -0800830 void invalidateTracks(audio_stream_type_t streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 virtual bool checkForNewParameters_l();
832 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
833
834 protected:
Glenn Kasten29c23c32012-01-26 13:37:52 -0800835 mixer_state prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
Eric Laurentde070132010-07-13 04:45:46 -0700836 Vector< sp<Track> > *tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 virtual int getTrackName_l();
838 virtual void deleteTrackName_l(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700840 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841
Eric Laurent27741442012-01-17 19:20:12 -0800842 AudioMixer* mAudioMixer;
Glenn Kasten29c23c32012-01-26 13:37:52 -0800843 mixer_state mPrevMixerStatus; // previous status returned by prepareTracks_l()
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 };
845
846 class DirectOutputThread : public PlaybackThread {
847 public:
848
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800849 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
850 audio_io_handle_t id, uint32_t device);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800851 virtual ~DirectOutputThread();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852
853 // Thread virtuals
854 virtual bool threadLoop();
855
856 virtual bool checkForNewParameters_l();
857
858 protected:
859 virtual int getTrackName_l();
860 virtual void deleteTrackName_l(int name);
861 virtual uint32_t activeSleepTimeUs();
862 virtual uint32_t idleSleepTimeUs();
Eric Laurent25cbe0e2010-08-18 18:13:17 -0700863 virtual uint32_t suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864
865 private:
866 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
867
868 float mLeftVolFloat;
869 float mRightVolFloat;
870 uint16_t mLeftVolShort;
871 uint16_t mRightVolShort;
872 };
873
874 class DuplicatingThread : public MixerThread {
875 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800876 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
877 audio_io_handle_t id);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800878 virtual ~DuplicatingThread();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879
880 // Thread virtuals
881 virtual bool threadLoop();
882 void addOutputTrack(MixerThread* thread);
883 void removeOutputTrack(MixerThread* thread);
884 uint32_t waitTimeMs() { return mWaitTimeMs; }
885 protected:
886 virtual uint32_t activeSleepTimeUs();
887
888 private:
889 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
890 void updateWaitTime();
891
892 SortedVector < sp<OutputTrack> > mOutputTracks;
893 uint32_t mWaitTimeMs;
894 };
895
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800896 PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const;
897 MixerThread *checkMixerThread_l(audio_io_handle_t output) const;
898 RecordThread *checkRecordThread_l(audio_io_handle_t input) const;
Glenn Kasten6637baa2012-01-09 09:40:36 -0800899 // no range check, AudioFlinger::mLock held
900 bool streamMute_l(audio_stream_type_t stream) const
901 { return mStreamTypes[stream].mute; }
902 // no range check, doesn't check per-thread stream volume, AudioFlinger::mLock held
903 float streamVolume_l(audio_stream_type_t stream) const
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800904 { return mStreamTypes[stream].volume; }
905 void audioConfigChanged_l(int event, audio_io_handle_t ioHandle, void *param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800907 // allocate an audio_io_handle_t, session ID, or effect ID
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700908 uint32_t nextUniqueId();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800909
Eric Laurent59255e42011-07-27 19:49:51 -0700910 status_t moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -0700911 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -0700912 AudioFlinger::PlaybackThread *dstThread,
913 bool reRegister);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700914 PlaybackThread *primaryPlaybackThread_l();
915 uint32_t primaryOutputDevice_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916
917 friend class AudioBuffer;
918
919 class TrackHandle : public android::BnAudioTrack {
920 public:
921 TrackHandle(const sp<PlaybackThread::Track>& track);
922 virtual ~TrackHandle();
Glenn Kasten90716c52012-01-26 13:40:12 -0800923 virtual sp<IMemory> getCblk() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 virtual status_t start();
925 virtual void stop();
926 virtual void flush();
927 virtual void mute(bool);
928 virtual void pause();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 virtual status_t attachAuxEffect(int effectId);
930 virtual status_t onTransact(
931 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
932 private:
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800933 const sp<PlaybackThread::Track> mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 };
935
936 friend class Client;
937 friend class PlaybackThread::Track;
938
939
940 void removeClient_l(pid_t pid);
941 void removeNotificationClient(pid_t pid);
942
943
944 // record thread
945 class RecordThread : public ThreadBase, public AudioBufferProvider
946 {
947 public:
948
949 // record track
950 class RecordTrack : public TrackBase {
951 public:
952 RecordTrack(const wp<ThreadBase>& thread,
953 const sp<Client>& client,
954 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800955 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700956 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 int frameCount,
958 uint32_t flags,
959 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800960 virtual ~RecordTrack();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961
962 virtual status_t start();
963 virtual void stop();
964
965 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
966 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
967
968 void dump(char* buffer, size_t size);
Eric Laurent59255e42011-07-27 19:49:51 -0700969
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 private:
971 friend class AudioFlinger;
972 friend class RecordThread;
973
974 RecordTrack(const RecordTrack&);
975 RecordTrack& operator = (const RecordTrack&);
976
977 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
978
979 bool mOverflow;
980 };
981
982
983 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -0700984 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 uint32_t sampleRate,
986 uint32_t channels,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800987 audio_io_handle_t id,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700988 uint32_t device);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800989 virtual ~RecordThread();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990
991 virtual bool threadLoop();
Eric Laurentb8ba0a92011-08-07 16:32:26 -0700992 virtual status_t readyToRun();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 virtual void onFirstRef();
994
Glenn Kastene0feee32011-12-13 11:53:26 -0800995 virtual status_t initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700996 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
997 const sp<AudioFlinger::Client>& client,
998 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800999 audio_format_t format,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001000 int channelMask,
1001 int frameCount,
1002 uint32_t flags,
1003 int sessionId,
1004 status_t *status);
1005
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 status_t start(RecordTrack* recordTrack);
1007 void stop(RecordTrack* recordTrack);
1008 status_t dump(int fd, const Vector<String16>& args);
Glenn Kastenaed850d2012-01-26 09:46:34 -08001009 AudioStreamIn* getInput() const;
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001010 AudioStreamIn* clearInput();
1011 virtual audio_stream_t* stream();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012
1013 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
1014 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
1015 virtual bool checkForNewParameters_l();
1016 virtual String8 getParameters(const String8& keys);
1017 virtual void audioConfigChanged_l(int event, int param = 0);
1018 void readInputParameters();
1019 virtual unsigned int getInputFramesLost();
1020
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001021 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1022 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
1023 virtual uint32_t hasAudioSession(int sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001024 RecordTrack* track();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001025
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 private:
1027 RecordThread();
Dima Zavin799a70e2011-04-18 16:57:27 -07001028 AudioStreamIn *mInput;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001029 RecordTrack* mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 sp<RecordTrack> mActiveTrack;
1031 Condition mStartStopCond;
1032 AudioResampler *mResampler;
1033 int32_t *mRsmpOutBuffer;
1034 int16_t *mRsmpInBuffer;
1035 size_t mRsmpInIndex;
1036 size_t mInputBytes;
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001037 const int mReqChannelCount;
1038 const uint32_t mReqSampleRate;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 ssize_t mBytesRead;
1040 };
1041
1042 class RecordHandle : public android::BnAudioRecord {
1043 public:
1044 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
1045 virtual ~RecordHandle();
Glenn Kasten90716c52012-01-26 13:40:12 -08001046 virtual sp<IMemory> getCblk() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 virtual status_t start();
1048 virtual void stop();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 virtual status_t onTransact(
1050 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1051 private:
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001052 const sp<RecordThread::RecordTrack> mRecordTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053 };
1054
1055 //--- Audio Effect Management
1056
1057 // EffectModule and EffectChain classes both have their own mutex to protect
1058 // state changes or resource modifications. Always respect the following order
1059 // if multiple mutexes must be acquired to avoid cross deadlock:
1060 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1061
1062 // The EffectModule class is a wrapper object controlling the effect engine implementation
1063 // in the effect library. It prevents concurrent calls to process() and command() functions
1064 // from different client threads. It keeps a list of EffectHandle objects corresponding
1065 // to all client applications using this effect and notifies applications of effect state,
1066 // control or parameter changes. It manages the activation state machine to send appropriate
1067 // reset, enable, disable commands to effect engine and provide volume
1068 // ramping when effects are activated/deactivated.
1069 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1070 // the attached track(s) to accumulate their auxiliary channel.
1071 class EffectModule: public RefBase {
1072 public:
1073 EffectModule(const wp<ThreadBase>& wThread,
1074 const wp<AudioFlinger::EffectChain>& chain,
1075 effect_descriptor_t *desc,
1076 int id,
1077 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001078 virtual ~EffectModule();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079
1080 enum effect_state {
1081 IDLE,
1082 RESTART,
1083 STARTING,
1084 ACTIVE,
1085 STOPPING,
Eric Laurentec437d82011-07-26 20:54:46 -07001086 STOPPED,
1087 DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07001088 };
1089
Glenn Kastenc59c0042012-02-02 14:06:11 -08001090 int id() const { return mId; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 void process();
1092 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001093 status_t command(uint32_t cmdCode,
1094 uint32_t cmdSize,
1095 void *pCmdData,
1096 uint32_t *replySize,
1097 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098
1099 void reset_l();
1100 status_t configure();
1101 status_t init();
Glenn Kasten28243dd2012-01-26 13:43:46 -08001102 effect_state state() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 return mState;
1104 }
1105 uint32_t status() {
1106 return mStatus;
1107 }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001108 int sessionId() const {
Eric Laurentde070132010-07-13 04:45:46 -07001109 return mSessionId;
1110 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111 status_t setEnabled(bool enabled);
Glenn Kastenc59c0042012-02-02 14:06:11 -08001112 bool isEnabled() const;
1113 bool isProcessEnabled() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114
1115 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1116 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1117 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1118 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001119 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1120 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001121 const wp<ThreadBase>& thread() { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122
Glenn Kasten435dbe62012-01-30 10:15:48 -08001123 status_t addHandle(const sp<EffectHandle>& handle);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001124 void disconnect(const wp<EffectHandle>& handle, bool unpiniflast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 size_t removeHandle (const wp<EffectHandle>& handle);
1126
1127 effect_descriptor_t& desc() { return mDescriptor; }
1128 wp<EffectChain>& chain() { return mChain; }
1129
1130 status_t setDevice(uint32_t device);
1131 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Glenn Kastenf78aee72012-01-04 11:00:47 -08001132 status_t setMode(audio_mode_t mode);
Eric Laurentec35a142011-10-05 17:42:25 -07001133 status_t start();
Eric Laurentec437d82011-07-26 20:54:46 -07001134 status_t stop();
Eric Laurent59255e42011-07-27 19:49:51 -07001135 void setSuspended(bool suspended);
Glenn Kastena3a85482012-01-04 11:01:11 -08001136 bool suspended() const;
Eric Laurent59255e42011-07-27 19:49:51 -07001137
1138 sp<EffectHandle> controlHandle();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139
Glenn Kastenc59c0042012-02-02 14:06:11 -08001140 bool isPinned() const { return mPinned; }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001141 void unPin() { mPinned = false; }
1142
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 status_t dump(int fd, const Vector<String16>& args);
1144
1145 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001146 friend class EffectHandle;
1147 friend class AudioFlinger;
1148 bool mPinned;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149
1150 // Maximum time allocated to effect engines to complete the turn off sequence
1151 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1152
1153 EffectModule(const EffectModule&);
1154 EffectModule& operator = (const EffectModule&);
1155
1156 status_t start_l();
1157 status_t stop_l();
1158
Glenn Kastena3a85482012-01-04 11:01:11 -08001159mutable Mutex mLock; // mutex for process, commands and handles list protection
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160 wp<ThreadBase> mThread; // parent thread
1161 wp<EffectChain> mChain; // parent effect chain
1162 int mId; // this instance unique ID
1163 int mSessionId; // audio session ID
1164 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1165 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001166 effect_handle_t mEffectInterface; // Effect module C API
Glenn Kasten28243dd2012-01-26 13:43:46 -08001167 status_t mStatus; // initialization status
1168 effect_state mState; // current activation state
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169 Vector< wp<EffectHandle> > mHandles; // list of client handles
1170 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1171 // sending disable command.
1172 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent59255e42011-07-27 19:49:51 -07001173 bool mSuspended; // effect is suspended: temporarily disabled by framework
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174 };
1175
1176 // The EffectHandle class implements the IEffect interface. It provides resources
1177 // to receive parameter updates, keeps track of effect control
1178 // ownership and state and has a pointer to the EffectModule object it is controlling.
1179 // There is one EffectHandle object for each application controlling (or using)
1180 // an effect module.
1181 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1182 class EffectHandle: public android::BnEffect {
1183 public:
1184
1185 EffectHandle(const sp<EffectModule>& effect,
1186 const sp<AudioFlinger::Client>& client,
1187 const sp<IEffectClient>& effectClient,
1188 int32_t priority);
1189 virtual ~EffectHandle();
1190
1191 // IEffect
1192 virtual status_t enable();
1193 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001194 virtual status_t command(uint32_t cmdCode,
1195 uint32_t cmdSize,
1196 void *pCmdData,
1197 uint32_t *replySize,
1198 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 virtual void disconnect();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001200 virtual void disconnect(bool unpiniflast);
Glenn Kastenc59c0042012-02-02 14:06:11 -08001201 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202 virtual status_t onTransact(uint32_t code, const Parcel& data,
1203 Parcel* reply, uint32_t flags);
1204
1205
1206 // Give or take control of effect module
Eric Laurent59255e42011-07-27 19:49:51 -07001207 // - hasControl: true if control is given, false if removed
1208 // - signal: true client app should be signaled of change, false otherwise
1209 // - enabled: state of the effect when control is passed
1210 void setControl(bool hasControl, bool signal, bool enabled);
Eric Laurent25f43952010-07-28 05:40:18 -07001211 void commandExecuted(uint32_t cmdCode,
1212 uint32_t cmdSize,
1213 void *pCmdData,
1214 uint32_t replySize,
1215 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 void setEnabled(bool enabled);
Glenn Kastenc59c0042012-02-02 14:06:11 -08001217 bool enabled() const { return mEnabled; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218
1219 // Getters
Glenn Kastenc59c0042012-02-02 14:06:11 -08001220 int id() const { return mEffect->id(); }
1221 int priority() const { return mPriority; }
1222 bool hasControl() const { return mHasControl; }
1223 sp<EffectModule> effect() const { return mEffect; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224
1225 void dump(char* buffer, size_t size);
1226
1227 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001228 friend class AudioFlinger;
1229 friend class EffectModule;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 EffectHandle(const EffectHandle&);
1231 EffectHandle& operator =(const EffectHandle&);
1232
1233 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1234 sp<IEffectClient> mEffectClient; // callback interface for client notifications
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001235 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 sp<IMemory> mCblkMemory; // shared memory for control block
1237 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1238 uint8_t* mBuffer; // pointer to parameter area in shared memory
1239 int mPriority; // client application priority to control the effect
1240 bool mHasControl; // true if this handle is controlling the effect
Eric Laurent59255e42011-07-27 19:49:51 -07001241 bool mEnabled; // cached enable state: needed when the effect is
1242 // restored after being suspended
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 };
1244
1245 // the EffectChain class represents a group of effects associated to one audio session.
1246 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1247 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1248 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1249 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1250 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1251 // input buffer used by the track as accumulation buffer.
1252 class EffectChain: public RefBase {
1253 public:
1254 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001255 virtual ~EffectChain();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256
Eric Laurent59255e42011-07-27 19:49:51 -07001257 // special key used for an entry in mSuspendedEffects keyed vector
1258 // corresponding to a suspend all request.
1259 static const int kKeyForSuspendAll = 0;
1260
Eric Laurent544fe9b2011-11-11 15:42:52 -08001261 // minimum duration during which we force calling effect process when last track on
1262 // a session is stopped or removed to allow effect tail to be rendered
1263 static const int kProcessTailDurationMs = 1000;
1264
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 void process_l();
1266
1267 void lock() {
1268 mLock.lock();
1269 }
1270 void unlock() {
1271 mLock.unlock();
1272 }
1273
Eric Laurentde070132010-07-13 04:45:46 -07001274 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001275 size_t removeEffect_l(const sp<EffectModule>& handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276
Glenn Kastenc59c0042012-02-02 14:06:11 -08001277 int sessionId() const { return mSessionId; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001278 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001279
Eric Laurentcab11242010-07-15 12:50:15 -07001280 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1281 sp<EffectModule> getEffectFromId_l(int id);
Eric Laurent59255e42011-07-27 19:49:51 -07001282 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurentcab11242010-07-15 12:50:15 -07001283 bool setVolume_l(uint32_t *left, uint32_t *right);
1284 void setDevice_l(uint32_t device);
Glenn Kastenf78aee72012-01-04 11:00:47 -08001285 void setMode_l(audio_mode_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1288 mInBuffer = buffer;
1289 mOwnInBuffer = ownsBuffer;
1290 }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001291 int16_t *inBuffer() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 return mInBuffer;
1293 }
1294 void setOutBuffer(int16_t *buffer) {
1295 mOutBuffer = buffer;
1296 }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001297 int16_t *outBuffer() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298 return mOutBuffer;
1299 }
1300
Eric Laurentb469b942011-05-09 12:09:06 -07001301 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1302 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001303 int32_t trackCnt() const { return mTrackCnt;}
Eric Laurentb469b942011-05-09 12:09:06 -07001304
Eric Laurent544fe9b2011-11-11 15:42:52 -08001305 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
1306 mTailBufferCount = mMaxTailBuffers; }
Eric Laurentb469b942011-05-09 12:09:06 -07001307 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001308 int32_t activeTrackCnt() const { return mActiveTrackCnt;}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309
Glenn Kastenc59c0042012-02-02 14:06:11 -08001310 uint32_t strategy() const { return mStrategy; }
Eric Laurentde070132010-07-13 04:45:46 -07001311 void setStrategy(uint32_t strategy)
1312 { mStrategy = strategy; }
1313
Eric Laurent59255e42011-07-27 19:49:51 -07001314 // suspend effect of the given type
1315 void setEffectSuspended_l(const effect_uuid_t *type,
1316 bool suspend);
1317 // suspend all eligible effects
1318 void setEffectSuspendedAll_l(bool suspend);
1319 // check if effects should be suspend or restored when a given effect is enable or disabled
Eric Laurenta85a74a2011-10-19 11:44:54 -07001320 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
Eric Laurent59255e42011-07-27 19:49:51 -07001321 bool enabled);
1322
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323 status_t dump(int fd, const Vector<String16>& args);
1324
1325 protected:
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001326 friend class AudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 EffectChain(const EffectChain&);
1328 EffectChain& operator =(const EffectChain&);
1329
Eric Laurent59255e42011-07-27 19:49:51 -07001330 class SuspendedEffectDesc : public RefBase {
1331 public:
1332 SuspendedEffectDesc() : mRefCount(0) {}
1333
1334 int mRefCount;
1335 effect_uuid_t mType;
1336 wp<EffectModule> mEffect;
1337 };
1338
1339 // get a list of effect modules to suspend when an effect of the type
1340 // passed is enabled.
Glenn Kastend0539712012-01-30 12:56:03 -08001341 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
1342
Eric Laurent59255e42011-07-27 19:49:51 -07001343 // get an effect module if it is currently enable
1344 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurentdb7c0792011-08-10 10:37:50 -07001345 // true if the effect whose descriptor is passed can be suspended
1346 // OEMs can modify the rules implemented in this method to exclude specific effect
1347 // types or implementations from the suspend/restore mechanism.
1348 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
Eric Laurent59255e42011-07-27 19:49:51 -07001349
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 wp<ThreadBase> mThread; // parent mixer thread
1351 Mutex mLock; // mutex protecting effect list
1352 Vector<sp<EffectModule> > mEffects; // list of effect modules
1353 int mSessionId; // audio session ID
1354 int16_t *mInBuffer; // chain input buffer
1355 int16_t *mOutBuffer; // chain output buffer
Eric Laurentb469b942011-05-09 12:09:06 -07001356 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1357 volatile int32_t mTrackCnt; // number of tracks connected
Eric Laurent544fe9b2011-11-11 15:42:52 -08001358 int32_t mTailBufferCount; // current effect tail buffer count
1359 int32_t mMaxTailBuffers; // maximum effect tail buffers
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07001361 int mVolumeCtrlIdx; // index of insert effect having control over volume
1362 uint32_t mLeftVolume; // previous volume on left channel
1363 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07001364 uint32_t mNewLeftVolume; // new volume on left channel
1365 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07001366 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent59255e42011-07-27 19:49:51 -07001367 // mSuspendedEffects lists all effect currently suspended in the chain
1368 // use effect type UUID timelow field as key. There is no real risk of identical
1369 // timeLow fields among effect type UUIDs.
1370 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371 };
1372
Glenn Kastenaed850d2012-01-26 09:46:34 -08001373 // AudioStreamOut and AudioStreamIn are immutable, so their fields are const.
1374 // For emphasis, we could also make all pointers to them be "const *",
1375 // but that would clutter the code unnecessarily.
1376
Dima Zavin799a70e2011-04-18 16:57:27 -07001377 struct AudioStreamOut {
Glenn Kastenaed850d2012-01-26 09:46:34 -08001378 audio_hw_device_t* const hwDev;
1379 audio_stream_out_t* const stream;
Dima Zavin799a70e2011-04-18 16:57:27 -07001380
1381 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1382 hwDev(dev), stream(out) {}
1383 };
1384
1385 struct AudioStreamIn {
Glenn Kastenaed850d2012-01-26 09:46:34 -08001386 audio_hw_device_t* const hwDev;
1387 audio_stream_in_t* const stream;
Dima Zavin799a70e2011-04-18 16:57:27 -07001388
1389 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1390 hwDev(dev), stream(in) {}
1391 };
1392
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001393 struct AudioSessionRef {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001394 // FIXME rename parameter names when fields get "m" prefix
1395 AudioSessionRef(int sessionid_, pid_t pid_) :
1396 sessionid(sessionid_), pid(pid_), cnt(1) {}
1397 const int sessionid;
1398 const pid_t pid;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001399 int cnt;
1400 };
1401
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 friend class RecordThread;
1403 friend class PlaybackThread;
1404
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 mutable Mutex mLock;
1406
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001407 DefaultKeyedVector< pid_t, wp<Client> > mClients; // see ~Client()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408
1409 mutable Mutex mHardwareLock;
Dima Zavin799a70e2011-04-18 16:57:27 -07001410 audio_hw_device_t* mPrimaryHardwareDev;
1411 Vector<audio_hw_device_t*> mAudioHwDevs;
Glenn Kastena4454b42012-01-04 11:02:33 -08001412 mutable hardware_call_state mHardwareStatus; // for dump only
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413
1414
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001415 DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavinfce7a472011-04-19 22:30:36 -07001416 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Glenn Kasten98067102011-12-13 11:47:54 -08001417
1418 // both are protected by mLock
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419 float mMasterVolume;
1420 bool mMasterMute;
1421
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001422 DefaultKeyedVector< audio_io_handle_t, sp<RecordThread> > mRecordThreads;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423
1424 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
1425 volatile int32_t mNextUniqueId;
Glenn Kastenf78aee72012-01-04 11:00:47 -08001426 audio_mode_t mMode;
Eric Laurentbee53372011-08-29 12:42:48 -07001427 bool mBtNrecIsOff;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001429 Vector<AudioSessionRef*> mAudioSessionRefs;
Glenn Kasten98067102011-12-13 11:47:54 -08001430
1431 float masterVolume_l() const { return mMasterVolume; }
1432 bool masterMute_l() const { return mMasterMute; }
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001433
1434private:
1435 sp<Client> registerPid_l(pid_t pid); // always returns non-0
1436
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437};
1438
Dima Zavin799a70e2011-04-18 16:57:27 -07001439
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440// ----------------------------------------------------------------------------
1441
1442}; // namespace android
1443
1444#endif // ANDROID_AUDIO_FLINGER_H