blob: 43be6fe37239c7b235da53189f2b94716da00d9c [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
150 virtual status_t loadEffectLibrary(const char *libPath, int *handle);
151
152 virtual status_t unloadEffectLibrary(int handle);
153
154 virtual status_t queryNumberEffects(uint32_t *numEffects);
155
Eric Laurent53334cd2010-06-23 17:38:20 -0700156 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -0700157
158 virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor);
159
160 virtual sp<IEffect> createEffect(pid_t pid,
161 effect_descriptor_t *pDesc,
162 const sp<IEffectClient>& effectClient,
163 int32_t priority,
164 int output,
165 int sessionId,
166 status_t *status,
167 int *id,
168 int *enabled);
169
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700170 virtual status_t moveEffects(int session, int srcOutput, int dstOutput);
Eric Laurent53334cd2010-06-23 17:38:20 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 enum hardware_call_state {
173 AUDIO_HW_IDLE = 0,
174 AUDIO_HW_INIT,
175 AUDIO_HW_OUTPUT_OPEN,
176 AUDIO_HW_OUTPUT_CLOSE,
177 AUDIO_HW_INPUT_OPEN,
178 AUDIO_HW_INPUT_CLOSE,
179 AUDIO_HW_STANDBY,
180 AUDIO_HW_SET_MASTER_VOLUME,
181 AUDIO_HW_GET_ROUTING,
182 AUDIO_HW_SET_ROUTING,
183 AUDIO_HW_GET_MODE,
184 AUDIO_HW_SET_MODE,
185 AUDIO_HW_GET_MIC_MUTE,
186 AUDIO_HW_SET_MIC_MUTE,
187 AUDIO_SET_VOICE_VOLUME,
188 AUDIO_SET_PARAMETER,
189 };
190
191 // record interface
192 virtual sp<IAudioRecord> openRecord(
193 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700194 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 uint32_t sampleRate,
196 int format,
197 int channelCount,
198 int frameCount,
199 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700200 int *sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 status_t *status);
202
203 virtual status_t onTransact(
204 uint32_t code,
205 const Parcel& data,
206 Parcel* reply,
207 uint32_t flags);
208
Eric Laurent53334cd2010-06-23 17:38:20 -0700209 uint32_t getMode() { return mMode; }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211private:
212 AudioFlinger();
213 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
Dima Zavin2986f5b2011-04-19 19:04:32 -0700215 status_t initCheck() const;
216 virtual void onFirstRef();
Dima Zavin31f188892011-04-18 16:57:27 -0700217 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
219 // Internal dump utilites.
220 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
221 status_t dumpClients(int fd, const Vector<String16>& args);
222 status_t dumpInternals(int fd, const Vector<String16>& args);
223
224 // --- Client ---
225 class Client : public RefBase {
226 public:
227 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
228 virtual ~Client();
229 const sp<MemoryDealer>& heap() const;
230 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700231 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 private:
234 Client(const Client&);
235 Client& operator = (const Client&);
236 sp<AudioFlinger> mAudioFlinger;
237 sp<MemoryDealer> mMemoryDealer;
238 pid_t mPid;
239 };
240
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700241 // --- Notification Client ---
242 class NotificationClient : public IBinder::DeathRecipient {
243 public:
244 NotificationClient(const sp<AudioFlinger>& audioFlinger,
245 const sp<IAudioFlingerClient>& client,
246 pid_t pid);
247 virtual ~NotificationClient();
248
249 sp<IAudioFlingerClient> client() { return mClient; }
250
251 // IBinder::DeathRecipient
252 virtual void binderDied(const wp<IBinder>& who);
253
254 private:
255 NotificationClient(const NotificationClient&);
256 NotificationClient& operator = (const NotificationClient&);
257
258 sp<AudioFlinger> mAudioFlinger;
259 pid_t mPid;
260 sp<IAudioFlingerClient> mClient;
261 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 class TrackHandle;
264 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700265 class RecordThread;
266 class PlaybackThread;
267 class MixerThread;
268 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800269 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700270 class Track;
271 class RecordTrack;
Eric Laurent65b65452010-06-01 23:49:17 -0700272 class EffectModule;
273 class EffectHandle;
274 class EffectChain;
Dima Zavin31f188892011-04-18 16:57:27 -0700275 struct AudioStreamOut;
276 struct AudioStreamIn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
Eric Laurenta553c252009-07-17 12:17:14 -0700278 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800280 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700281 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282
Eric Laurent3fdb1262009-11-07 00:01:32 -0800283 status_t dumpBase(int fd, const Vector<String16>& args);
284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 // base for record and playback
286 class TrackBase : public AudioBufferProvider, public RefBase {
287
288 public:
289 enum track_state {
290 IDLE,
291 TERMINATED,
292 STOPPED,
293 RESUMING,
294 ACTIVE,
295 PAUSING,
296 PAUSED
297 };
298
299 enum track_flags {
300 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
301 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
302 // The upper 16 bits are used for track-specific flags.
303 };
304
Eric Laurenta553c252009-07-17 12:17:14 -0700305 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 uint32_t sampleRate,
308 int format,
309 int channelCount,
310 int frameCount,
311 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700312 const sp<IMemory>& sharedBuffer,
313 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 ~TrackBase();
315
316 virtual status_t start() = 0;
317 virtual void stop() = 0;
318 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700319 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700320 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
322 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700323 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700325 friend class PlaybackThread;
326 friend class RecordThread;
327 friend class MixerThread;
328 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
330 TrackBase(const TrackBase&);
331 TrackBase& operator = (const TrackBase&);
332
333 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
334 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 int format() const {
337 return mFormat;
338 }
339
340 int channelCount() const ;
341
342 int sampleRate() const;
343
344 void* getBuffer(uint32_t offset, uint32_t frames) const;
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 bool isStopped() const {
347 return mState == STOPPED;
348 }
349
350 bool isTerminated() const {
351 return mState == TERMINATED;
352 }
353
354 bool step();
355 void reset();
356
Eric Laurenta553c252009-07-17 12:17:14 -0700357 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 sp<Client> mClient;
359 sp<IMemory> mCblkMemory;
360 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 void* mBuffer;
362 void* mBufferEnd;
363 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 // we don't really need a lock for these
365 int mState;
366 int mClientTid;
367 uint8_t mFormat;
368 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700369 int mSessionId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 };
371
Eric Laurenta553c252009-07-17 12:17:14 -0700372 class ConfigEvent {
373 public:
374 ConfigEvent() : mEvent(0), mParam(0) {}
375
376 int mEvent;
377 int mParam;
378 };
379
380 uint32_t sampleRate() const;
381 int channelCount() const;
382 int format() const;
383 size_t frameCount() const;
384 void wakeUp() { mWaitWorkCV.broadcast(); }
385 void exit();
386 virtual bool checkForNewParameters_l() = 0;
387 virtual status_t setParameters(const String8& keyValuePairs);
388 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700389 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700390 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700391 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700392 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800393 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800394 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700395
Eric Laurent2c817f52009-07-23 13:17:39 -0700396 mutable Mutex mLock;
397
Eric Laurenta553c252009-07-17 12:17:14 -0700398 protected:
399
400 friend class Track;
401 friend class TrackBase;
402 friend class PlaybackThread;
403 friend class MixerThread;
404 friend class DirectOutputThread;
405 friend class DuplicatingThread;
406 friend class RecordThread;
407 friend class RecordTrack;
408
Eric Laurenta553c252009-07-17 12:17:14 -0700409 Condition mWaitWorkCV;
410 sp<AudioFlinger> mAudioFlinger;
411 uint32_t mSampleRate;
412 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700413 uint32_t mChannels;
414 uint16_t mChannelCount;
415 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700416 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700417 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700418 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700419 status_t mParamStatus;
420 Vector<ConfigEvent *> mConfigEvents;
421 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800422 int mId;
423 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700424 };
425
426 // --- PlaybackThread ---
427 class PlaybackThread : public ThreadBase {
428 public:
429
430 enum type {
431 MIXER,
432 DIRECT,
433 DUPLICATING
434 };
435
Eric Laurent059b4be2009-11-09 23:32:22 -0800436 enum mixer_state {
437 MIXER_IDLE,
438 MIXER_TRACKS_ENABLED,
439 MIXER_TRACKS_READY
440 };
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 // playback track
443 class Track : public TrackBase {
444 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700445 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 const sp<Client>& client,
447 int streamType,
448 uint32_t sampleRate,
449 int format,
450 int channelCount,
451 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700452 const sp<IMemory>& sharedBuffer,
453 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 ~Track();
455
456 void dump(char* buffer, size_t size);
457 virtual status_t start();
458 virtual void stop();
459 void pause();
460
461 void flush();
462 void destroy();
463 void mute(bool);
464 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700465 int name() const {
466 return mName;
467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468
Eric Laurent4bc035a2009-05-22 09:18:15 -0700469 int type() const {
470 return mStreamType;
471 }
Eric Laurent65b65452010-06-01 23:49:17 -0700472 status_t attachAuxEffect(int EffectId);
473 void setAuxBuffer(int EffectId, int32_t *buffer);
474 int32_t *auxBuffer() { return mAuxBuffer; }
475 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
476 int16_t *mainBuffer() { return mMainBuffer; }
477 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700478
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700481 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700483 friend class TrackHandle;
484 friend class PlaybackThread;
485 friend class MixerThread;
486 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
488 Track(const Track&);
489 Track& operator = (const Track&);
490
491 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700492 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 bool isPausing() const {
494 return mState == PAUSING;
495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 bool isPaused() const {
497 return mState == PAUSED;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 void setPaused() { mState = PAUSED; }
501 void reset();
502
Eric Laurent49f02be2009-11-19 09:00:56 -0800503 bool isOutputTrack() const {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700504 return (mStreamType == AUDIO_STREAM_CNT);
Eric Laurent49f02be2009-11-19 09:00:56 -0800505 }
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 // we don't really need a lock for these
508 float mVolume[2];
509 volatile bool mMute;
510 // FILLED state is used for suppressing volume ramp at begin of playing
511 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
512 mutable uint8_t mFillingUpStatus;
513 int8_t mRetryCount;
514 sp<IMemory> mSharedBuffer;
515 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700516 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700517 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700518 int16_t *mMainBuffer;
519 int32_t *mAuxBuffer;
520 int mAuxEffectId;
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700521 bool mHasVolumeController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }; // end of Track
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524
525 // playback track
526 class OutputTrack : public Track {
527 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 class Buffer: public AudioBufferProvider::Buffer {
530 public:
531 int16_t *mBuffer;
532 };
Eric Laurenta553c252009-07-17 12:17:14 -0700533
534 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800535 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 uint32_t sampleRate,
537 int format,
538 int channelCount,
539 int frameCount);
540 ~OutputTrack();
541
542 virtual status_t start();
543 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700544 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700546 bool isActive() { return mActive; }
547 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548
549 private:
550
Eric Laurenta553c252009-07-17 12:17:14 -0700551 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700553
554 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800555 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 Vector < Buffer* > mBufferQueue;
558 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700559 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800560 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700561 }; // end of OutputTrack
562
Dima Zavin31f188892011-04-18 16:57:27 -0700563 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700564 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
566 virtual status_t dump(int fd, const Vector<String16>& args);
567
568 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 virtual status_t readyToRun();
570 virtual void onFirstRef();
571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 virtual uint32_t latency() const;
573
574 virtual status_t setMasterVolume(float value);
575 virtual status_t setMasterMute(bool muted);
576
577 virtual float masterVolume() const;
578 virtual bool masterMute() const;
579
580 virtual status_t setStreamVolume(int stream, float value);
581 virtual status_t setStreamMute(int stream, bool muted);
582
583 virtual float streamVolume(int stream) const;
584 virtual bool streamMute(int stream) const;
585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700586 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 const sp<AudioFlinger::Client>& client,
588 int streamType,
589 uint32_t sampleRate,
590 int format,
591 int channelCount,
592 int frameCount,
593 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700594 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700596
Dima Zavin31f188892011-04-18 16:57:27 -0700597 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700598
599 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700600 void suspend() { mSuspended++; }
601 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800602 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700603 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700604 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800605 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700606 int16_t *mixBuffer() { return mMixBuffer; };
607
608 sp<EffectHandle> createEffect_l(
609 const sp<AudioFlinger::Client>& client,
610 const sp<IEffectClient>& effectClient,
611 int32_t priority,
612 int sessionId,
613 effect_descriptor_t *desc,
614 int *enabled,
615 status_t *status);
Eric Laurent53334cd2010-06-23 17:38:20 -0700616 void disconnectEffect(const sp< EffectModule>& effect,
617 const wp<EffectHandle>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -0700618
Eric Laurent493941b2010-07-28 01:32:47 -0700619 // return values for hasAudioSession (bit field)
620 enum effect_state {
621 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
622 // effect
623 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
624 // track
625 };
626
627 uint32_t hasAudioSession(int sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700628 sp<EffectChain> getEffectChain(int sessionId);
629 sp<EffectChain> getEffectChain_l(int sessionId);
630 status_t addEffectChain_l(const sp<EffectChain>& chain);
631 size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700632 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
633 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
Eric Laurent65b65452010-06-01 23:49:17 -0700634
635 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
636 void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700637 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
638 int EffectId);
639 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
640 int EffectId);
Eric Laurent53334cd2010-06-23 17:38:20 -0700641 void setMode(uint32_t mode);
Eric Laurenta553c252009-07-17 12:17:14 -0700642
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700643 status_t addEffect_l(const sp< EffectModule>& effect);
644 void removeEffect_l(const sp< EffectModule>& effect);
645
646 uint32_t getStrategyForSession_l(int sessionId);
647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 struct stream_type_t {
649 stream_type_t()
650 : volume(1.0f),
651 mute(false)
652 {
653 }
654 float volume;
655 bool mute;
656 };
657
Eric Laurent2c817f52009-07-23 13:17:39 -0700658 protected:
659 int mType;
660 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700661 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700662 int mBytesWritten;
663 bool mMasterMute;
664 SortedVector< wp<Track> > mActiveTracks;
665
Eric Laurent62443f52009-10-05 20:29:18 -0700666 virtual int getTrackName_l() = 0;
667 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800668 virtual uint32_t activeSleepTimeUs() = 0;
669 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent8448a792010-08-18 18:13:17 -0700670 virtual uint32_t suspendSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 private:
673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700675 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 friend class Track;
677 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700678 friend class MixerThread;
679 friend class DirectOutputThread;
680 friend class DuplicatingThread;
681
682 PlaybackThread(const Client&);
683 PlaybackThread& operator = (const PlaybackThread&);
684
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700685 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700686 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700687
Eric Laurenta553c252009-07-17 12:17:14 -0700688 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689
Eric Laurent65b65452010-06-01 23:49:17 -0700690 uint32_t device() { return mDevice; }
691
Eric Laurenta553c252009-07-17 12:17:14 -0700692 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700694 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700697 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700698 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin31f188892011-04-18 16:57:27 -0700699 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 nsecs_t mLastWriteTime;
702 int mNumWrites;
703 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700705 Vector< sp<EffectChain> > mEffectChains;
706 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 };
708
Eric Laurenta553c252009-07-17 12:17:14 -0700709 class MixerThread : public PlaybackThread {
710 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700711 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700712 AudioStreamOut* output,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700713 int id,
714 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700715 virtual ~MixerThread();
716
717 // Thread virtuals
718 virtual bool threadLoop();
719
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700720 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700721 virtual bool checkForNewParameters_l();
722 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
723
724 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700725 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
726 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700727 virtual int getTrackName_l();
728 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800729 virtual uint32_t activeSleepTimeUs();
730 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700731 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700732
733 AudioMixer* mAudioMixer;
734 };
735
736 class DirectOutputThread : public PlaybackThread {
737 public:
738
Dima Zavin31f188892011-04-18 16:57:27 -0700739 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700740 ~DirectOutputThread();
741
742 // Thread virtuals
743 virtual bool threadLoop();
744
Eric Laurent62443f52009-10-05 20:29:18 -0700745 virtual bool checkForNewParameters_l();
746
747 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700748 virtual int getTrackName_l();
749 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800750 virtual uint32_t activeSleepTimeUs();
751 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700752 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700753
754 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700755 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
756
757 float mLeftVolFloat;
758 float mRightVolFloat;
759 uint16_t mLeftVolShort;
760 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700761 };
762
763 class DuplicatingThread : public MixerThread {
764 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800765 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700766 ~DuplicatingThread();
767
768 // Thread virtuals
769 virtual bool threadLoop();
770 void addOutputTrack(MixerThread* thread);
771 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800772 uint32_t waitTimeMs() { return mWaitTimeMs; }
773 protected:
774 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700775
776 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800777 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
778 void updateWaitTime();
779
Eric Laurenta553c252009-07-17 12:17:14 -0700780 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800781 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700782 };
783
Eric Laurentddb78e72009-07-28 08:44:33 -0700784 PlaybackThread *checkPlaybackThread_l(int output) const;
785 MixerThread *checkMixerThread_l(int output) const;
786 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700787 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800788 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700789
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800790 int nextUniqueId_l();
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700791 status_t moveEffectChain_l(int session,
792 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -0700793 AudioFlinger::PlaybackThread *dstThread,
794 bool reRegister);
Eric Laurent65b65452010-06-01 23:49:17 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 friend class AudioBuffer;
797
798 class TrackHandle : public android::BnAudioTrack {
799 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700800 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 virtual ~TrackHandle();
802 virtual status_t start();
803 virtual void stop();
804 virtual void flush();
805 virtual void mute(bool);
806 virtual void pause();
807 virtual void setVolume(float left, float right);
808 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700809 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 virtual status_t onTransact(
811 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
812 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700813 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 };
815
816 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700817 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818
819
Eric Laurentb9481d82009-09-17 05:12:56 -0700820 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700821 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822
823
Eric Laurenta553c252009-07-17 12:17:14 -0700824 // record thread
825 class RecordThread : public ThreadBase, public AudioBufferProvider
826 {
827 public:
828
829 // record track
830 class RecordTrack : public TrackBase {
831 public:
832 RecordTrack(const wp<ThreadBase>& thread,
833 const sp<Client>& client,
834 uint32_t sampleRate,
835 int format,
836 int channelCount,
837 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700838 uint32_t flags,
839 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700840 ~RecordTrack();
841
842 virtual status_t start();
843 virtual void stop();
844
845 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
846 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
847
Eric Laurent3fdb1262009-11-07 00:01:32 -0800848 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700849 private:
850 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700851 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700852
853 RecordTrack(const RecordTrack&);
854 RecordTrack& operator = (const RecordTrack&);
855
856 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
857
858 bool mOverflow;
859 };
860
861
862 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700863 AudioStreamIn *input,
Eric Laurenta553c252009-07-17 12:17:14 -0700864 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800865 uint32_t channels,
866 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700867 ~RecordThread();
868
869 virtual bool threadLoop();
870 virtual status_t readyToRun() { return NO_ERROR; }
871 virtual void onFirstRef();
872
873 status_t start(RecordTrack* recordTrack);
874 void stop(RecordTrack* recordTrack);
875 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin31f188892011-04-18 16:57:27 -0700876 AudioStreamIn* getInput() { return mInput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700877
878 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
879 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
880 virtual bool checkForNewParameters_l();
881 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700882 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700883 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800884 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700885
886 private:
887 RecordThread();
Dima Zavin31f188892011-04-18 16:57:27 -0700888 AudioStreamIn *mInput;
Eric Laurenta553c252009-07-17 12:17:14 -0700889 sp<RecordTrack> mActiveTrack;
890 Condition mStartStopCond;
891 AudioResampler *mResampler;
892 int32_t *mRsmpOutBuffer;
893 int16_t *mRsmpInBuffer;
894 size_t mRsmpInIndex;
895 size_t mInputBytes;
896 int mReqChannelCount;
897 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800898 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700899 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900
901 class RecordHandle : public android::BnAudioRecord {
902 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700903 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 virtual ~RecordHandle();
905 virtual status_t start();
906 virtual void stop();
907 virtual sp<IMemory> getCblk() const;
908 virtual status_t onTransact(
909 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
910 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700911 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 };
913
Eric Laurent65b65452010-06-01 23:49:17 -0700914 //--- Audio Effect Management
915
916 // EffectModule and EffectChain classes both have their own mutex to protect
917 // state changes or resource modifications. Always respect the following order
918 // if multiple mutexes must be acquired to avoid cross deadlock:
919 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
920
921 // The EffectModule class is a wrapper object controlling the effect engine implementation
922 // in the effect library. It prevents concurrent calls to process() and command() functions
923 // from different client threads. It keeps a list of EffectHandle objects corresponding
924 // to all client applications using this effect and notifies applications of effect state,
925 // control or parameter changes. It manages the activation state machine to send appropriate
926 // reset, enable, disable commands to effect engine and provide volume
927 // ramping when effects are activated/deactivated.
928 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
929 // the attached track(s) to accumulate their auxiliary channel.
930 class EffectModule: public RefBase {
931 public:
932 EffectModule(const wp<ThreadBase>& wThread,
933 const wp<AudioFlinger::EffectChain>& chain,
934 effect_descriptor_t *desc,
935 int id,
936 int sessionId);
937 ~EffectModule();
938
939 enum effect_state {
940 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700941 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700942 STARTING,
943 ACTIVE,
944 STOPPING,
945 STOPPED
946 };
947
948 int id() { return mId; }
949 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -0700950 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700951 status_t command(uint32_t cmdCode,
952 uint32_t cmdSize,
953 void *pCmdData,
954 uint32_t *replySize,
955 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -0700956
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700957 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700958 status_t configure();
959 status_t init();
960 uint32_t state() {
961 return mState;
962 }
963 uint32_t status() {
964 return mStatus;
965 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700966 int sessionId() {
967 return mSessionId;
968 }
Eric Laurent65b65452010-06-01 23:49:17 -0700969 status_t setEnabled(bool enabled);
970 bool isEnabled();
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700971 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -0700972
973 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
974 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
975 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
976 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700977 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
978 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -0700979
980 status_t addHandle(sp<EffectHandle>& handle);
981 void disconnect(const wp<EffectHandle>& handle);
982 size_t removeHandle (const wp<EffectHandle>& handle);
983
984 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700985 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700986
987 status_t setDevice(uint32_t device);
988 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700989 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700990
991 status_t dump(int fd, const Vector<String16>& args);
992
993 protected:
994
Eric Laurent7d850f22010-07-09 13:34:17 -0700995 // Maximum time allocated to effect engines to complete the turn off sequence
996 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
997
Eric Laurent65b65452010-06-01 23:49:17 -0700998 EffectModule(const EffectModule&);
999 EffectModule& operator = (const EffectModule&);
1000
Eric Laurentdf9b81c2010-07-02 08:12:41 -07001001 status_t start_l();
1002 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001003
Eric Laurent53334cd2010-06-23 17:38:20 -07001004 // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified
1005 static const uint32_t sDeviceConvTable[];
1006 static uint32_t deviceAudioSystemToEffectApi(uint32_t device);
1007
1008 // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified
1009 static const uint32_t sModeConvTable[];
1010 static int modeAudioSystemToEffectApi(uint32_t mode);
1011
Eric Laurent65b65452010-06-01 23:49:17 -07001012 Mutex mLock; // mutex for process, commands and handles list protection
1013 wp<ThreadBase> mThread; // parent thread
1014 wp<EffectChain> mChain; // parent effect chain
1015 int mId; // this instance unique ID
1016 int mSessionId; // audio session ID
1017 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1018 effect_config_t mConfig; // input and output audio configuration
1019 effect_interface_t mEffectInterface; // Effect module C API
1020 status_t mStatus; // initialization status
1021 uint32_t mState; // current activation state (effect_state)
1022 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001023 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1024 // sending disable command.
1025 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001026 };
1027
1028 // The EffectHandle class implements the IEffect interface. It provides resources
1029 // to receive parameter updates, keeps track of effect control
1030 // ownership and state and has a pointer to the EffectModule object it is controlling.
1031 // There is one EffectHandle object for each application controlling (or using)
1032 // an effect module.
1033 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1034 class EffectHandle: public android::BnEffect {
1035 public:
1036
1037 EffectHandle(const sp<EffectModule>& effect,
1038 const sp<AudioFlinger::Client>& client,
1039 const sp<IEffectClient>& effectClient,
1040 int32_t priority);
1041 virtual ~EffectHandle();
1042
1043 // IEffect
1044 virtual status_t enable();
1045 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001046 virtual status_t command(uint32_t cmdCode,
1047 uint32_t cmdSize,
1048 void *pCmdData,
1049 uint32_t *replySize,
1050 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001051 virtual void disconnect();
1052 virtual sp<IMemory> getCblk() const;
1053 virtual status_t onTransact(uint32_t code, const Parcel& data,
1054 Parcel* reply, uint32_t flags);
1055
1056
1057 // Give or take control of effect module
1058 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001059 void commandExecuted(uint32_t cmdCode,
1060 uint32_t cmdSize,
1061 void *pCmdData,
1062 uint32_t replySize,
1063 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001064 void setEnabled(bool enabled);
1065
1066 // Getters
1067 int id() { return mEffect->id(); }
1068 int priority() { return mPriority; }
1069 bool hasControl() { return mHasControl; }
1070 sp<EffectModule> effect() { return mEffect; }
1071
1072 void dump(char* buffer, size_t size);
1073
1074 protected:
1075
1076 EffectHandle(const EffectHandle&);
1077 EffectHandle& operator =(const EffectHandle&);
1078
1079 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1080 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1081 sp<Client> mClient; // client for shared memory allocation
1082 sp<IMemory> mCblkMemory; // shared memory for control block
1083 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1084 uint8_t* mBuffer; // pointer to parameter area in shared memory
1085 int mPriority; // client application priority to control the effect
1086 bool mHasControl; // true if this handle is controlling the effect
1087 };
1088
1089 // the EffectChain class represents a group of effects associated to one audio session.
1090 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1091 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1092 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1093 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1094 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1095 // input buffer used by the track as accumulation buffer.
1096 class EffectChain: public RefBase {
1097 public:
1098 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1099 ~EffectChain();
1100
1101 void process_l();
1102
1103 void lock() {
1104 mLock.lock();
1105 }
1106 void unlock() {
1107 mLock.unlock();
1108 }
1109
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001110 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001111 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001112
1113 int sessionId() {
1114 return mSessionId;
1115 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001116
Eric Laurent76c40f72010-07-15 12:50:15 -07001117 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1118 sp<EffectModule> getEffectFromId_l(int id);
1119 bool setVolume_l(uint32_t *left, uint32_t *right);
1120 void setDevice_l(uint32_t device);
1121 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001122
Eric Laurent65b65452010-06-01 23:49:17 -07001123 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1124 mInBuffer = buffer;
1125 mOwnInBuffer = ownsBuffer;
1126 }
1127 int16_t *inBuffer() {
1128 return mInBuffer;
1129 }
1130 void setOutBuffer(int16_t *buffer) {
1131 mOutBuffer = buffer;
1132 }
1133 int16_t *outBuffer() {
1134 return mOutBuffer;
1135 }
1136
1137 void startTrack() {mActiveTrackCnt++;}
1138 void stopTrack() {mActiveTrackCnt--;}
1139 int activeTracks() { return mActiveTrackCnt;}
1140
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001141 uint32_t strategy() { return mStrategy; }
1142 void setStrategy(uint32_t strategy)
1143 { mStrategy = strategy; }
1144
Eric Laurent65b65452010-06-01 23:49:17 -07001145 status_t dump(int fd, const Vector<String16>& args);
1146
1147 protected:
1148
1149 EffectChain(const EffectChain&);
1150 EffectChain& operator =(const EffectChain&);
1151
1152 wp<ThreadBase> mThread; // parent mixer thread
1153 Mutex mLock; // mutex protecting effect list
1154 Vector<sp<EffectModule> > mEffects; // list of effect modules
1155 int mSessionId; // audio session ID
1156 int16_t *mInBuffer; // chain input buffer
1157 int16_t *mOutBuffer; // chain output buffer
Eric Laurent65b65452010-06-01 23:49:17 -07001158 int mActiveTrackCnt; // number of active tracks connected
1159 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001160 int mVolumeCtrlIdx; // index of insert effect having control over volume
1161 uint32_t mLeftVolume; // previous volume on left channel
1162 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001163 uint32_t mNewLeftVolume; // new volume on left channel
1164 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001165 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001166 };
1167
Dima Zavin31f188892011-04-18 16:57:27 -07001168 struct AudioStreamOut {
1169 audio_hw_device_t *hwDev;
1170 audio_stream_out_t *stream;
1171
1172 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1173 hwDev(dev), stream(out) {}
1174 };
1175
1176 struct AudioStreamIn {
1177 audio_hw_device_t *hwDev;
1178 audio_stream_in_t *stream;
1179
1180 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1181 hwDev(dev), stream(in) {}
1182 };
1183
Eric Laurenta553c252009-07-17 12:17:14 -07001184 friend class RecordThread;
1185 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001187 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1190
Eric Laurenta553c252009-07-17 12:17:14 -07001191 mutable Mutex mHardwareLock;
Dima Zavin31f188892011-04-18 16:57:27 -07001192 audio_hw_device_t* mPrimaryHardwareDev;
1193 Vector<audio_hw_device_t*> mAudioHwDevs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001195
Eric Laurenta553c252009-07-17 12:17:14 -07001196
Eric Laurentddb78e72009-07-28 08:44:33 -07001197 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001198 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurenta553c252009-07-17 12:17:14 -07001199 float mMasterVolume;
1200 bool mMasterMute;
1201
Eric Laurentddb78e72009-07-28 08:44:33 -07001202 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001203
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001204 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001205 volatile int32_t mNextUniqueId;
Eric Laurent53334cd2010-06-23 17:38:20 -07001206 uint32_t mMode;
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208};
1209
Dima Zavin31f188892011-04-18 16:57:27 -07001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211// ----------------------------------------------------------------------------
1212
1213}; // namespace android
1214
1215#endif // ANDROID_AUDIO_FLINGER_H