blob: 0698dcb957f8900d3a241d1604041087bc2e76cc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/include/server/AudioFlinger/AudioFlinger.h
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef ANDROID_AUDIO_FLINGER_H
19#define ANDROID_AUDIO_FLINGER_H
20
21#include <stdint.h>
22#include <sys/types.h>
Eric Laurent8ac9f8d2009-12-18 05:47:48 -080023#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <media/IAudioFlinger.h>
26#include <media/IAudioFlingerClient.h>
27#include <media/IAudioTrack.h>
28#include <media/IAudioRecord.h>
29#include <media/AudioTrack.h>
30
31#include <utils/Atomic.h>
32#include <utils/Errors.h>
33#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <utils/SortedVector.h>
Dima Zavin31f188892011-04-18 16:57:27 -070035#include <utils/TypeHelpers.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/Vector.h>
37
Mathias Agopian24651682010-07-14 18:41:18 -070038#include <binder/BinderService.h>
39#include <binder/MemoryDealer.h>
40
Dima Zavin34bb4192011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin31f188892011-04-18 16:57:27 -070042#include <hardware/audio_hal.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include "AudioBufferProvider.h"
45
46namespace android {
47
48class audio_track_cblk_t;
Eric Laurent65b65452010-06-01 23:49:17 -070049class effect_param_cblk_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050class AudioMixer;
51class AudioBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -070052class AudioResampler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054// ----------------------------------------------------------------------------
55
56#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
57#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
58
59
60// ----------------------------------------------------------------------------
61
62static const nsecs_t kStandbyTimeInNsecs = seconds(3);
63
Mathias Agopian24651682010-07-14 18:41:18 -070064class AudioFlinger :
65 public BinderService<AudioFlinger>,
66 public BnAudioFlinger
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067{
Mathias Agopian24651682010-07-14 18:41:18 -070068 friend class BinderService<AudioFlinger>;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069public:
Mathias Agopian24651682010-07-14 18:41:18 -070070 static char const* getServiceName() { return "media.audio_flinger"; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72 virtual status_t dump(int fd, const Vector<String16>& args);
73
74 // IAudioFlinger interface
75 virtual sp<IAudioTrack> createTrack(
76 pid_t pid,
77 int streamType,
78 uint32_t sampleRate,
79 int format,
80 int channelCount,
81 int frameCount,
82 uint32_t flags,
83 const sp<IMemory>& sharedBuffer,
Eric Laurentddb78e72009-07-28 08:44:33 -070084 int output,
Eric Laurent65b65452010-06-01 23:49:17 -070085 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 status_t *status);
87
Eric Laurentddb78e72009-07-28 08:44:33 -070088 virtual uint32_t sampleRate(int output) const;
89 virtual int channelCount(int output) const;
90 virtual int format(int output) const;
91 virtual size_t frameCount(int output) const;
92 virtual uint32_t latency(int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 virtual status_t setMasterVolume(float value);
95 virtual status_t setMasterMute(bool muted);
96
97 virtual float masterVolume() const;
98 virtual bool masterMute() const;
99
Eric Laurentddb78e72009-07-28 08:44:33 -0700100 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 virtual status_t setStreamMute(int stream, bool muted);
102
Eric Laurentddb78e72009-07-28 08:44:33 -0700103 virtual float streamVolume(int stream, int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 virtual bool streamMute(int stream) const;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 virtual status_t setMode(int mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 virtual status_t setMicMute(bool state);
109 virtual bool getMicMute() const;
110
Eric Laurentddb78e72009-07-28 08:44:33 -0700111 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
112 virtual String8 getParameters(int ioHandle, const String8& keys);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurenta553c252009-07-17 12:17:14 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent47d0a922010-02-26 02:47:27 -0800117 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700118
Eric Laurentddb78e72009-07-28 08:44:33 -0700119 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700120 uint32_t *pSamplingRate,
121 uint32_t *pFormat,
122 uint32_t *pChannels,
123 uint32_t *pLatencyMs,
124 uint32_t flags);
125
Eric Laurentddb78e72009-07-28 08:44:33 -0700126 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700127
Eric Laurentddb78e72009-07-28 08:44:33 -0700128 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700129
Eric Laurentddb78e72009-07-28 08:44:33 -0700130 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700131
Eric Laurentddb78e72009-07-28 08:44:33 -0700132 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700133
Eric Laurentddb78e72009-07-28 08:44:33 -0700134 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700135 uint32_t *pSamplingRate,
136 uint32_t *pFormat,
137 uint32_t *pChannels,
138 uint32_t acoustics);
139
Eric Laurentddb78e72009-07-28 08:44:33 -0700140 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700141
Eric Laurentddb78e72009-07-28 08:44:33 -0700142 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700143
Eric Laurent415f3e22009-10-21 08:14:22 -0700144 virtual status_t setVoiceVolume(float volume);
145
Eric Laurent0986e792010-01-19 17:37:09 -0800146 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
147
Eric Laurent65b65452010-06-01 23:49:17 -0700148 virtual int newAudioSessionId();
149
Eric Laurent65b65452010-06-01 23:49:17 -0700150 virtual status_t queryNumberEffects(uint32_t *numEffects);
151
Eric Laurent53334cd2010-06-23 17:38:20 -0700152 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -0700153
154 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
155
156 virtual sp<IEffect> createEffect(pid_t pid,
157 effect_descriptor_t *pDesc,
158 const sp<IEffectClient>& effectClient,
159 int32_t priority,
160 int output,
161 int sessionId,
162 status_t *status,
163 int *id,
164 int *enabled);
165
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700166 virtual status_t moveEffects(int session, int srcOutput, int dstOutput);
Eric Laurent53334cd2010-06-23 17:38:20 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 enum hardware_call_state {
169 AUDIO_HW_IDLE = 0,
170 AUDIO_HW_INIT,
171 AUDIO_HW_OUTPUT_OPEN,
172 AUDIO_HW_OUTPUT_CLOSE,
173 AUDIO_HW_INPUT_OPEN,
174 AUDIO_HW_INPUT_CLOSE,
175 AUDIO_HW_STANDBY,
176 AUDIO_HW_SET_MASTER_VOLUME,
177 AUDIO_HW_GET_ROUTING,
178 AUDIO_HW_SET_ROUTING,
179 AUDIO_HW_GET_MODE,
180 AUDIO_HW_SET_MODE,
181 AUDIO_HW_GET_MIC_MUTE,
182 AUDIO_HW_SET_MIC_MUTE,
183 AUDIO_SET_VOICE_VOLUME,
184 AUDIO_SET_PARAMETER,
185 };
186
187 // record interface
188 virtual sp<IAudioRecord> openRecord(
189 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700190 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 uint32_t sampleRate,
192 int format,
193 int channelCount,
194 int frameCount,
195 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700196 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 status_t *status);
198
199 virtual status_t onTransact(
200 uint32_t code,
201 const Parcel& data,
202 Parcel* reply,
203 uint32_t flags);
204
Eric Laurent53334cd2010-06-23 17:38:20 -0700205 uint32_t getMode() { return mMode; }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207private:
208 AudioFlinger();
209 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
Dima Zavin2986f5b2011-04-19 19:04:32 -0700211 status_t initCheck() const;
212 virtual void onFirstRef();
Dima Zavin31f188892011-04-18 16:57:27 -0700213 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
215 // Internal dump utilites.
216 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
217 status_t dumpClients(int fd, const Vector<String16>& args);
218 status_t dumpInternals(int fd, const Vector<String16>& args);
219
220 // --- Client ---
221 class Client : public RefBase {
222 public:
223 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
224 virtual ~Client();
225 const sp<MemoryDealer>& heap() const;
226 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700227 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 private:
230 Client(const Client&);
231 Client& operator = (const Client&);
232 sp<AudioFlinger> mAudioFlinger;
233 sp<MemoryDealer> mMemoryDealer;
234 pid_t mPid;
235 };
236
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700237 // --- Notification Client ---
238 class NotificationClient : public IBinder::DeathRecipient {
239 public:
240 NotificationClient(const sp<AudioFlinger>& audioFlinger,
241 const sp<IAudioFlingerClient>& client,
242 pid_t pid);
243 virtual ~NotificationClient();
244
245 sp<IAudioFlingerClient> client() { return mClient; }
246
247 // IBinder::DeathRecipient
248 virtual void binderDied(const wp<IBinder>& who);
249
250 private:
251 NotificationClient(const NotificationClient&);
252 NotificationClient& operator = (const NotificationClient&);
253
254 sp<AudioFlinger> mAudioFlinger;
255 pid_t mPid;
256 sp<IAudioFlingerClient> mClient;
257 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
259 class TrackHandle;
260 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700261 class RecordThread;
262 class PlaybackThread;
263 class MixerThread;
264 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800265 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700266 class Track;
267 class RecordTrack;
Eric Laurent65b65452010-06-01 23:49:17 -0700268 class EffectModule;
269 class EffectHandle;
270 class EffectChain;
Dima Zavin31f188892011-04-18 16:57:27 -0700271 struct AudioStreamOut;
272 struct AudioStreamIn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
Eric Laurenta553c252009-07-17 12:17:14 -0700274 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800276 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700277 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
Eric Laurent3fdb1262009-11-07 00:01:32 -0800279 status_t dumpBase(int fd, const Vector<String16>& args);
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 // base for record and playback
282 class TrackBase : public AudioBufferProvider, public RefBase {
283
284 public:
285 enum track_state {
286 IDLE,
287 TERMINATED,
288 STOPPED,
289 RESUMING,
290 ACTIVE,
291 PAUSING,
292 PAUSED
293 };
294
295 enum track_flags {
296 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
297 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
298 // The upper 16 bits are used for track-specific flags.
299 };
300
Eric Laurenta553c252009-07-17 12:17:14 -0700301 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 uint32_t sampleRate,
304 int format,
305 int channelCount,
306 int frameCount,
307 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700308 const sp<IMemory>& sharedBuffer,
309 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 ~TrackBase();
311
312 virtual status_t start() = 0;
313 virtual void stop() = 0;
314 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700315 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700316 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317
318 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700319 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700321 friend class PlaybackThread;
322 friend class RecordThread;
323 friend class MixerThread;
324 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
326 TrackBase(const TrackBase&);
327 TrackBase& operator = (const TrackBase&);
328
329 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
330 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 int format() const {
333 return mFormat;
334 }
335
336 int channelCount() const ;
337
338 int sampleRate() const;
339
340 void* getBuffer(uint32_t offset, uint32_t frames) const;
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 bool isStopped() const {
343 return mState == STOPPED;
344 }
345
346 bool isTerminated() const {
347 return mState == TERMINATED;
348 }
349
350 bool step();
351 void reset();
352
Eric Laurenta553c252009-07-17 12:17:14 -0700353 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 sp<Client> mClient;
355 sp<IMemory> mCblkMemory;
356 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 void* mBuffer;
358 void* mBufferEnd;
359 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 // we don't really need a lock for these
361 int mState;
362 int mClientTid;
363 uint8_t mFormat;
364 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700365 int mSessionId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 };
367
Eric Laurenta553c252009-07-17 12:17:14 -0700368 class ConfigEvent {
369 public:
370 ConfigEvent() : mEvent(0), mParam(0) {}
371
372 int mEvent;
373 int mParam;
374 };
375
376 uint32_t sampleRate() const;
377 int channelCount() const;
378 int format() const;
379 size_t frameCount() const;
380 void wakeUp() { mWaitWorkCV.broadcast(); }
381 void exit();
382 virtual bool checkForNewParameters_l() = 0;
383 virtual status_t setParameters(const String8& keyValuePairs);
384 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700385 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700386 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700387 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700388 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800389 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800390 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700391
Eric Laurent2c817f52009-07-23 13:17:39 -0700392 mutable Mutex mLock;
393
Eric Laurenta553c252009-07-17 12:17:14 -0700394 protected:
395
396 friend class Track;
397 friend class TrackBase;
398 friend class PlaybackThread;
399 friend class MixerThread;
400 friend class DirectOutputThread;
401 friend class DuplicatingThread;
402 friend class RecordThread;
403 friend class RecordTrack;
404
Eric Laurenta553c252009-07-17 12:17:14 -0700405 Condition mWaitWorkCV;
406 sp<AudioFlinger> mAudioFlinger;
407 uint32_t mSampleRate;
408 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700409 uint32_t mChannels;
410 uint16_t mChannelCount;
411 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700412 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700413 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700414 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700415 status_t mParamStatus;
416 Vector<ConfigEvent *> mConfigEvents;
417 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800418 int mId;
419 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700420 };
421
422 // --- PlaybackThread ---
423 class PlaybackThread : public ThreadBase {
424 public:
425
426 enum type {
427 MIXER,
428 DIRECT,
429 DUPLICATING
430 };
431
Eric Laurent059b4be2009-11-09 23:32:22 -0800432 enum mixer_state {
433 MIXER_IDLE,
434 MIXER_TRACKS_ENABLED,
435 MIXER_TRACKS_READY
436 };
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 // playback track
439 class Track : public TrackBase {
440 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700441 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 const sp<Client>& client,
443 int streamType,
444 uint32_t sampleRate,
445 int format,
446 int channelCount,
447 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700448 const sp<IMemory>& sharedBuffer,
449 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 ~Track();
451
452 void dump(char* buffer, size_t size);
453 virtual status_t start();
454 virtual void stop();
455 void pause();
456
457 void flush();
458 void destroy();
459 void mute(bool);
460 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700461 int name() const {
462 return mName;
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464
Eric Laurent4bc035a2009-05-22 09:18:15 -0700465 int type() const {
466 return mStreamType;
467 }
Eric Laurent65b65452010-06-01 23:49:17 -0700468 status_t attachAuxEffect(int EffectId);
469 void setAuxBuffer(int EffectId, int32_t *buffer);
470 int32_t *auxBuffer() { return mAuxBuffer; }
471 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
472 int16_t *mainBuffer() { return mMainBuffer; }
473 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700474
475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700477 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700479 friend class TrackHandle;
480 friend class PlaybackThread;
481 friend class MixerThread;
482 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 Track(const Track&);
485 Track& operator = (const Track&);
486
487 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700488 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 bool isPausing() const {
490 return mState == PAUSING;
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 bool isPaused() const {
493 return mState == PAUSED;
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 void setPaused() { mState = PAUSED; }
497 void reset();
498
Eric Laurent49f02be2009-11-19 09:00:56 -0800499 bool isOutputTrack() const {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700500 return (mStreamType == AUDIO_STREAM_CNT);
Eric Laurent49f02be2009-11-19 09:00:56 -0800501 }
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 // we don't really need a lock for these
504 float mVolume[2];
505 volatile bool mMute;
506 // FILLED state is used for suppressing volume ramp at begin of playing
507 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
508 mutable uint8_t mFillingUpStatus;
509 int8_t mRetryCount;
510 sp<IMemory> mSharedBuffer;
511 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700512 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700513 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700514 int16_t *mMainBuffer;
515 int32_t *mAuxBuffer;
516 int mAuxEffectId;
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700517 bool mHasVolumeController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }; // end of Track
519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
521 // playback track
522 class OutputTrack : public Track {
523 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 class Buffer: public AudioBufferProvider::Buffer {
526 public:
527 int16_t *mBuffer;
528 };
Eric Laurenta553c252009-07-17 12:17:14 -0700529
530 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800531 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 uint32_t sampleRate,
533 int format,
534 int channelCount,
535 int frameCount);
536 ~OutputTrack();
537
538 virtual status_t start();
539 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700540 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700542 bool isActive() { return mActive; }
543 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
545 private:
546
Eric Laurenta553c252009-07-17 12:17:14 -0700547 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700549
550 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800551 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 Vector < Buffer* > mBufferQueue;
554 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700555 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800556 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700557 }; // end of OutputTrack
558
Dima Zavin31f188892011-04-18 16:57:27 -0700559 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700560 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
562 virtual status_t dump(int fd, const Vector<String16>& args);
563
564 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 virtual status_t readyToRun();
566 virtual void onFirstRef();
567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 virtual uint32_t latency() const;
569
570 virtual status_t setMasterVolume(float value);
571 virtual status_t setMasterMute(bool muted);
572
573 virtual float masterVolume() const;
574 virtual bool masterMute() const;
575
576 virtual status_t setStreamVolume(int stream, float value);
577 virtual status_t setStreamMute(int stream, bool muted);
578
579 virtual float streamVolume(int stream) const;
580 virtual bool streamMute(int stream) const;
581
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700582 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 const sp<AudioFlinger::Client>& client,
584 int streamType,
585 uint32_t sampleRate,
586 int format,
587 int channelCount,
588 int frameCount,
589 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700590 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700592
Dima Zavin31f188892011-04-18 16:57:27 -0700593 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700594
595 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700596 void suspend() { mSuspended++; }
597 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800598 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700599 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700600 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800601 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700602 int16_t *mixBuffer() { return mMixBuffer; };
603
604 sp<EffectHandle> createEffect_l(
605 const sp<AudioFlinger::Client>& client,
606 const sp<IEffectClient>& effectClient,
607 int32_t priority,
608 int sessionId,
609 effect_descriptor_t *desc,
610 int *enabled,
611 status_t *status);
Eric Laurent53334cd2010-06-23 17:38:20 -0700612 void disconnectEffect(const sp< EffectModule>& effect,
613 const wp<EffectHandle>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -0700614
Eric Laurent493941b2010-07-28 01:32:47 -0700615 // return values for hasAudioSession (bit field)
616 enum effect_state {
617 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
618 // effect
619 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
620 // track
621 };
622
623 uint32_t hasAudioSession(int sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700624 sp<EffectChain> getEffectChain(int sessionId);
625 sp<EffectChain> getEffectChain_l(int sessionId);
626 status_t addEffectChain_l(const sp<EffectChain>& chain);
627 size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700628 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
629 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
Eric Laurent65b65452010-06-01 23:49:17 -0700630
631 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
632 void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700633 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
634 int EffectId);
635 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
636 int EffectId);
Eric Laurent53334cd2010-06-23 17:38:20 -0700637 void setMode(uint32_t mode);
Eric Laurenta553c252009-07-17 12:17:14 -0700638
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700639 status_t addEffect_l(const sp< EffectModule>& effect);
640 void removeEffect_l(const sp< EffectModule>& effect);
641
642 uint32_t getStrategyForSession_l(int sessionId);
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 struct stream_type_t {
645 stream_type_t()
646 : volume(1.0f),
647 mute(false)
648 {
649 }
650 float volume;
651 bool mute;
652 };
653
Eric Laurent2c817f52009-07-23 13:17:39 -0700654 protected:
655 int mType;
656 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700657 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700658 int mBytesWritten;
659 bool mMasterMute;
660 SortedVector< wp<Track> > mActiveTracks;
661
Eric Laurent62443f52009-10-05 20:29:18 -0700662 virtual int getTrackName_l() = 0;
663 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800664 virtual uint32_t activeSleepTimeUs() = 0;
665 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent8448a792010-08-18 18:13:17 -0700666 virtual uint32_t suspendSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 private:
669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700671 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 friend class Track;
673 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700674 friend class MixerThread;
675 friend class DirectOutputThread;
676 friend class DuplicatingThread;
677
678 PlaybackThread(const Client&);
679 PlaybackThread& operator = (const PlaybackThread&);
680
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700681 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700682 void destroyTrack_l(const sp<Track>& track);
Eric Laurent90681d62011-05-09 12:09:06 -0700683 void removeTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700684
Eric Laurenta553c252009-07-17 12:17:14 -0700685 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686
Eric Laurent65b65452010-06-01 23:49:17 -0700687 uint32_t device() { return mDevice; }
688
Eric Laurenta553c252009-07-17 12:17:14 -0700689 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700691 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700694 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700695 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin31f188892011-04-18 16:57:27 -0700696 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 nsecs_t mLastWriteTime;
699 int mNumWrites;
700 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700702 Vector< sp<EffectChain> > mEffectChains;
703 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 };
705
Eric Laurenta553c252009-07-17 12:17:14 -0700706 class MixerThread : public PlaybackThread {
707 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700708 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700709 AudioStreamOut* output,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700710 int id,
711 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700712 virtual ~MixerThread();
713
714 // Thread virtuals
715 virtual bool threadLoop();
716
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700717 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700718 virtual bool checkForNewParameters_l();
719 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
720
721 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700722 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
723 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700724 virtual int getTrackName_l();
725 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800726 virtual uint32_t activeSleepTimeUs();
727 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700728 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700729
730 AudioMixer* mAudioMixer;
731 };
732
733 class DirectOutputThread : public PlaybackThread {
734 public:
735
Dima Zavin31f188892011-04-18 16:57:27 -0700736 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700737 ~DirectOutputThread();
738
739 // Thread virtuals
740 virtual bool threadLoop();
741
Eric Laurent62443f52009-10-05 20:29:18 -0700742 virtual bool checkForNewParameters_l();
743
744 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700745 virtual int getTrackName_l();
746 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800747 virtual uint32_t activeSleepTimeUs();
748 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700749 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700750
751 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700752 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
753
754 float mLeftVolFloat;
755 float mRightVolFloat;
756 uint16_t mLeftVolShort;
757 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700758 };
759
760 class DuplicatingThread : public MixerThread {
761 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800762 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700763 ~DuplicatingThread();
764
765 // Thread virtuals
766 virtual bool threadLoop();
767 void addOutputTrack(MixerThread* thread);
768 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800769 uint32_t waitTimeMs() { return mWaitTimeMs; }
770 protected:
771 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700772
773 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800774 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
775 void updateWaitTime();
776
Eric Laurenta553c252009-07-17 12:17:14 -0700777 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800778 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700779 };
780
Eric Laurentddb78e72009-07-28 08:44:33 -0700781 PlaybackThread *checkPlaybackThread_l(int output) const;
782 MixerThread *checkMixerThread_l(int output) const;
783 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700784 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800785 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700786
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800787 int nextUniqueId_l();
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700788 status_t moveEffectChain_l(int session,
789 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -0700790 AudioFlinger::PlaybackThread *dstThread,
791 bool reRegister);
Eric Laurent65b65452010-06-01 23:49:17 -0700792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 friend class AudioBuffer;
794
795 class TrackHandle : public android::BnAudioTrack {
796 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700797 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 virtual ~TrackHandle();
799 virtual status_t start();
800 virtual void stop();
801 virtual void flush();
802 virtual void mute(bool);
803 virtual void pause();
804 virtual void setVolume(float left, float right);
805 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700806 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 virtual status_t onTransact(
808 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
809 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700810 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 };
812
813 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700814 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815
816
Eric Laurentb9481d82009-09-17 05:12:56 -0700817 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700818 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820
Eric Laurenta553c252009-07-17 12:17:14 -0700821 // record thread
822 class RecordThread : public ThreadBase, public AudioBufferProvider
823 {
824 public:
825
826 // record track
827 class RecordTrack : public TrackBase {
828 public:
829 RecordTrack(const wp<ThreadBase>& thread,
830 const sp<Client>& client,
831 uint32_t sampleRate,
832 int format,
833 int channelCount,
834 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700835 uint32_t flags,
836 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700837 ~RecordTrack();
838
839 virtual status_t start();
840 virtual void stop();
841
842 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
843 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
844
Eric Laurent3fdb1262009-11-07 00:01:32 -0800845 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700846 private:
847 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700848 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700849
850 RecordTrack(const RecordTrack&);
851 RecordTrack& operator = (const RecordTrack&);
852
853 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
854
855 bool mOverflow;
856 };
857
858
859 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700860 AudioStreamIn *input,
Eric Laurenta553c252009-07-17 12:17:14 -0700861 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800862 uint32_t channels,
863 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700864 ~RecordThread();
865
866 virtual bool threadLoop();
867 virtual status_t readyToRun() { return NO_ERROR; }
868 virtual void onFirstRef();
869
870 status_t start(RecordTrack* recordTrack);
871 void stop(RecordTrack* recordTrack);
872 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin31f188892011-04-18 16:57:27 -0700873 AudioStreamIn* getInput() { return mInput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700874
875 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
876 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
877 virtual bool checkForNewParameters_l();
878 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700879 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700880 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800881 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700882
883 private:
884 RecordThread();
Dima Zavin31f188892011-04-18 16:57:27 -0700885 AudioStreamIn *mInput;
Eric Laurenta553c252009-07-17 12:17:14 -0700886 sp<RecordTrack> mActiveTrack;
887 Condition mStartStopCond;
888 AudioResampler *mResampler;
889 int32_t *mRsmpOutBuffer;
890 int16_t *mRsmpInBuffer;
891 size_t mRsmpInIndex;
892 size_t mInputBytes;
893 int mReqChannelCount;
894 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800895 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700896 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897
898 class RecordHandle : public android::BnAudioRecord {
899 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700900 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 virtual ~RecordHandle();
902 virtual status_t start();
903 virtual void stop();
904 virtual sp<IMemory> getCblk() const;
905 virtual status_t onTransact(
906 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
907 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700908 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 };
910
Eric Laurent65b65452010-06-01 23:49:17 -0700911 //--- Audio Effect Management
912
913 // EffectModule and EffectChain classes both have their own mutex to protect
914 // state changes or resource modifications. Always respect the following order
915 // if multiple mutexes must be acquired to avoid cross deadlock:
916 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
917
918 // The EffectModule class is a wrapper object controlling the effect engine implementation
919 // in the effect library. It prevents concurrent calls to process() and command() functions
920 // from different client threads. It keeps a list of EffectHandle objects corresponding
921 // to all client applications using this effect and notifies applications of effect state,
922 // control or parameter changes. It manages the activation state machine to send appropriate
923 // reset, enable, disable commands to effect engine and provide volume
924 // ramping when effects are activated/deactivated.
925 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
926 // the attached track(s) to accumulate their auxiliary channel.
927 class EffectModule: public RefBase {
928 public:
929 EffectModule(const wp<ThreadBase>& wThread,
930 const wp<AudioFlinger::EffectChain>& chain,
931 effect_descriptor_t *desc,
932 int id,
933 int sessionId);
934 ~EffectModule();
935
936 enum effect_state {
937 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700938 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700939 STARTING,
940 ACTIVE,
941 STOPPING,
942 STOPPED
943 };
944
945 int id() { return mId; }
946 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -0700947 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700948 status_t command(uint32_t cmdCode,
949 uint32_t cmdSize,
950 void *pCmdData,
951 uint32_t *replySize,
952 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -0700953
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700954 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700955 status_t configure();
956 status_t init();
957 uint32_t state() {
958 return mState;
959 }
960 uint32_t status() {
961 return mStatus;
962 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700963 int sessionId() {
964 return mSessionId;
965 }
Eric Laurent65b65452010-06-01 23:49:17 -0700966 status_t setEnabled(bool enabled);
967 bool isEnabled();
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700968 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -0700969
970 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
971 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
972 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
973 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700974 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
975 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -0700976
977 status_t addHandle(sp<EffectHandle>& handle);
978 void disconnect(const wp<EffectHandle>& handle);
979 size_t removeHandle (const wp<EffectHandle>& handle);
980
981 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700982 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700983
984 status_t setDevice(uint32_t device);
985 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700986 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700987
988 status_t dump(int fd, const Vector<String16>& args);
989
990 protected:
991
Eric Laurent7d850f22010-07-09 13:34:17 -0700992 // Maximum time allocated to effect engines to complete the turn off sequence
993 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
994
Eric Laurent65b65452010-06-01 23:49:17 -0700995 EffectModule(const EffectModule&);
996 EffectModule& operator = (const EffectModule&);
997
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700998 status_t start_l();
999 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001000
1001 Mutex mLock; // mutex for process, commands and handles list protection
1002 wp<ThreadBase> mThread; // parent thread
1003 wp<EffectChain> mChain; // parent effect chain
1004 int mId; // this instance unique ID
1005 int mSessionId; // audio session ID
1006 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1007 effect_config_t mConfig; // input and output audio configuration
Eric Laurent0fb66c22011-05-17 19:16:02 -07001008 effect_handle_t mEffectInterface; // Effect module C API
Eric Laurent65b65452010-06-01 23:49:17 -07001009 status_t mStatus; // initialization status
1010 uint32_t mState; // current activation state (effect_state)
1011 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001012 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1013 // sending disable command.
1014 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001015 };
1016
1017 // The EffectHandle class implements the IEffect interface. It provides resources
1018 // to receive parameter updates, keeps track of effect control
1019 // ownership and state and has a pointer to the EffectModule object it is controlling.
1020 // There is one EffectHandle object for each application controlling (or using)
1021 // an effect module.
1022 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1023 class EffectHandle: public android::BnEffect {
1024 public:
1025
1026 EffectHandle(const sp<EffectModule>& effect,
1027 const sp<AudioFlinger::Client>& client,
1028 const sp<IEffectClient>& effectClient,
1029 int32_t priority);
1030 virtual ~EffectHandle();
1031
1032 // IEffect
1033 virtual status_t enable();
1034 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001035 virtual status_t command(uint32_t cmdCode,
1036 uint32_t cmdSize,
1037 void *pCmdData,
1038 uint32_t *replySize,
1039 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001040 virtual void disconnect();
1041 virtual sp<IMemory> getCblk() const;
1042 virtual status_t onTransact(uint32_t code, const Parcel& data,
1043 Parcel* reply, uint32_t flags);
1044
1045
1046 // Give or take control of effect module
1047 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001048 void commandExecuted(uint32_t cmdCode,
1049 uint32_t cmdSize,
1050 void *pCmdData,
1051 uint32_t replySize,
1052 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001053 void setEnabled(bool enabled);
1054
1055 // Getters
1056 int id() { return mEffect->id(); }
1057 int priority() { return mPriority; }
1058 bool hasControl() { return mHasControl; }
1059 sp<EffectModule> effect() { return mEffect; }
1060
1061 void dump(char* buffer, size_t size);
1062
1063 protected:
1064
1065 EffectHandle(const EffectHandle&);
1066 EffectHandle& operator =(const EffectHandle&);
1067
1068 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1069 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1070 sp<Client> mClient; // client for shared memory allocation
1071 sp<IMemory> mCblkMemory; // shared memory for control block
1072 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1073 uint8_t* mBuffer; // pointer to parameter area in shared memory
1074 int mPriority; // client application priority to control the effect
1075 bool mHasControl; // true if this handle is controlling the effect
1076 };
1077
1078 // the EffectChain class represents a group of effects associated to one audio session.
1079 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1080 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1081 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1082 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1083 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1084 // input buffer used by the track as accumulation buffer.
1085 class EffectChain: public RefBase {
1086 public:
1087 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1088 ~EffectChain();
1089
1090 void process_l();
1091
1092 void lock() {
1093 mLock.lock();
1094 }
1095 void unlock() {
1096 mLock.unlock();
1097 }
1098
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001099 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001100 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001101
1102 int sessionId() {
1103 return mSessionId;
1104 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001105
Eric Laurent76c40f72010-07-15 12:50:15 -07001106 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1107 sp<EffectModule> getEffectFromId_l(int id);
1108 bool setVolume_l(uint32_t *left, uint32_t *right);
1109 void setDevice_l(uint32_t device);
1110 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001111
Eric Laurent65b65452010-06-01 23:49:17 -07001112 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1113 mInBuffer = buffer;
1114 mOwnInBuffer = ownsBuffer;
1115 }
1116 int16_t *inBuffer() {
1117 return mInBuffer;
1118 }
1119 void setOutBuffer(int16_t *buffer) {
1120 mOutBuffer = buffer;
1121 }
1122 int16_t *outBuffer() {
1123 return mOutBuffer;
1124 }
1125
Eric Laurent90681d62011-05-09 12:09:06 -07001126 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1127 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1128 int32_t trackCnt() { return mTrackCnt;}
1129
1130 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1131 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1132 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Eric Laurent65b65452010-06-01 23:49:17 -07001133
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001134 uint32_t strategy() { return mStrategy; }
1135 void setStrategy(uint32_t strategy)
1136 { mStrategy = strategy; }
1137
Eric Laurent65b65452010-06-01 23:49:17 -07001138 status_t dump(int fd, const Vector<String16>& args);
1139
1140 protected:
1141
1142 EffectChain(const EffectChain&);
1143 EffectChain& operator =(const EffectChain&);
1144
1145 wp<ThreadBase> mThread; // parent mixer thread
1146 Mutex mLock; // mutex protecting effect list
1147 Vector<sp<EffectModule> > mEffects; // list of effect modules
1148 int mSessionId; // audio session ID
1149 int16_t *mInBuffer; // chain input buffer
1150 int16_t *mOutBuffer; // chain output buffer
Eric Laurent90681d62011-05-09 12:09:06 -07001151 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1152 volatile int32_t mTrackCnt; // number of tracks connected
Eric Laurent65b65452010-06-01 23:49:17 -07001153 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001154 int mVolumeCtrlIdx; // index of insert effect having control over volume
1155 uint32_t mLeftVolume; // previous volume on left channel
1156 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001157 uint32_t mNewLeftVolume; // new volume on left channel
1158 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001159 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001160 };
1161
Dima Zavin31f188892011-04-18 16:57:27 -07001162 struct AudioStreamOut {
1163 audio_hw_device_t *hwDev;
1164 audio_stream_out_t *stream;
1165
1166 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1167 hwDev(dev), stream(out) {}
1168 };
1169
1170 struct AudioStreamIn {
1171 audio_hw_device_t *hwDev;
1172 audio_stream_in_t *stream;
1173
1174 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1175 hwDev(dev), stream(in) {}
1176 };
1177
Eric Laurenta553c252009-07-17 12:17:14 -07001178 friend class RecordThread;
1179 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001181 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1184
Eric Laurenta553c252009-07-17 12:17:14 -07001185 mutable Mutex mHardwareLock;
Dima Zavin31f188892011-04-18 16:57:27 -07001186 audio_hw_device_t* mPrimaryHardwareDev;
1187 Vector<audio_hw_device_t*> mAudioHwDevs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001189
Eric Laurenta553c252009-07-17 12:17:14 -07001190
Eric Laurentddb78e72009-07-28 08:44:33 -07001191 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001192 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurenta553c252009-07-17 12:17:14 -07001193 float mMasterVolume;
1194 bool mMasterMute;
1195
Eric Laurentddb78e72009-07-28 08:44:33 -07001196 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001197
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001198 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001199 volatile int32_t mNextUniqueId;
Eric Laurent53334cd2010-06-23 17:38:20 -07001200 uint32_t mMode;
1201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202};
1203
Dima Zavin31f188892011-04-18 16:57:27 -07001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205// ----------------------------------------------------------------------------
1206
1207}; // namespace android
1208
1209#endif // ANDROID_AUDIO_FLINGER_H