blob: f3e198414b05a85158e6d67b3929324a0791a7d3 [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 Laurent90681d62011-05-09 12:09:06 -0700687 void removeTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700688
Eric Laurenta553c252009-07-17 12:17:14 -0700689 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690
Eric Laurent65b65452010-06-01 23:49:17 -0700691 uint32_t device() { return mDevice; }
692
Eric Laurenta553c252009-07-17 12:17:14 -0700693 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700695 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700698 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700699 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin31f188892011-04-18 16:57:27 -0700700 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 nsecs_t mLastWriteTime;
703 int mNumWrites;
704 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700706 Vector< sp<EffectChain> > mEffectChains;
707 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 };
709
Eric Laurenta553c252009-07-17 12:17:14 -0700710 class MixerThread : public PlaybackThread {
711 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700712 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700713 AudioStreamOut* output,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700714 int id,
715 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700716 virtual ~MixerThread();
717
718 // Thread virtuals
719 virtual bool threadLoop();
720
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700721 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700722 virtual bool checkForNewParameters_l();
723 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
724
725 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700726 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
727 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700728 virtual int getTrackName_l();
729 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800730 virtual uint32_t activeSleepTimeUs();
731 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700732 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700733
734 AudioMixer* mAudioMixer;
735 };
736
737 class DirectOutputThread : public PlaybackThread {
738 public:
739
Dima Zavin31f188892011-04-18 16:57:27 -0700740 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700741 ~DirectOutputThread();
742
743 // Thread virtuals
744 virtual bool threadLoop();
745
Eric Laurent62443f52009-10-05 20:29:18 -0700746 virtual bool checkForNewParameters_l();
747
748 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700749 virtual int getTrackName_l();
750 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800751 virtual uint32_t activeSleepTimeUs();
752 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700753 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700754
755 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700756 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
757
758 float mLeftVolFloat;
759 float mRightVolFloat;
760 uint16_t mLeftVolShort;
761 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700762 };
763
764 class DuplicatingThread : public MixerThread {
765 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800766 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700767 ~DuplicatingThread();
768
769 // Thread virtuals
770 virtual bool threadLoop();
771 void addOutputTrack(MixerThread* thread);
772 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800773 uint32_t waitTimeMs() { return mWaitTimeMs; }
774 protected:
775 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700776
777 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800778 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
779 void updateWaitTime();
780
Eric Laurenta553c252009-07-17 12:17:14 -0700781 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800782 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700783 };
784
Eric Laurentddb78e72009-07-28 08:44:33 -0700785 PlaybackThread *checkPlaybackThread_l(int output) const;
786 MixerThread *checkMixerThread_l(int output) const;
787 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700788 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800789 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700790
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800791 int nextUniqueId_l();
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700792 status_t moveEffectChain_l(int session,
793 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -0700794 AudioFlinger::PlaybackThread *dstThread,
795 bool reRegister);
Eric Laurent65b65452010-06-01 23:49:17 -0700796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 friend class AudioBuffer;
798
799 class TrackHandle : public android::BnAudioTrack {
800 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700801 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 virtual ~TrackHandle();
803 virtual status_t start();
804 virtual void stop();
805 virtual void flush();
806 virtual void mute(bool);
807 virtual void pause();
808 virtual void setVolume(float left, float right);
809 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700810 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 virtual status_t onTransact(
812 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
813 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700814 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 };
816
817 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700818 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820
Eric Laurentb9481d82009-09-17 05:12:56 -0700821 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700822 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
824
Eric Laurenta553c252009-07-17 12:17:14 -0700825 // record thread
826 class RecordThread : public ThreadBase, public AudioBufferProvider
827 {
828 public:
829
830 // record track
831 class RecordTrack : public TrackBase {
832 public:
833 RecordTrack(const wp<ThreadBase>& thread,
834 const sp<Client>& client,
835 uint32_t sampleRate,
836 int format,
837 int channelCount,
838 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700839 uint32_t flags,
840 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700841 ~RecordTrack();
842
843 virtual status_t start();
844 virtual void stop();
845
846 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
847 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
848
Eric Laurent3fdb1262009-11-07 00:01:32 -0800849 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700850 private:
851 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700852 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700853
854 RecordTrack(const RecordTrack&);
855 RecordTrack& operator = (const RecordTrack&);
856
857 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
858
859 bool mOverflow;
860 };
861
862
863 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700864 AudioStreamIn *input,
Eric Laurenta553c252009-07-17 12:17:14 -0700865 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800866 uint32_t channels,
867 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700868 ~RecordThread();
869
870 virtual bool threadLoop();
871 virtual status_t readyToRun() { return NO_ERROR; }
872 virtual void onFirstRef();
873
874 status_t start(RecordTrack* recordTrack);
875 void stop(RecordTrack* recordTrack);
876 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin31f188892011-04-18 16:57:27 -0700877 AudioStreamIn* getInput() { return mInput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700878
879 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
880 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
881 virtual bool checkForNewParameters_l();
882 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700883 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700884 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800885 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700886
887 private:
888 RecordThread();
Dima Zavin31f188892011-04-18 16:57:27 -0700889 AudioStreamIn *mInput;
Eric Laurenta553c252009-07-17 12:17:14 -0700890 sp<RecordTrack> mActiveTrack;
891 Condition mStartStopCond;
892 AudioResampler *mResampler;
893 int32_t *mRsmpOutBuffer;
894 int16_t *mRsmpInBuffer;
895 size_t mRsmpInIndex;
896 size_t mInputBytes;
897 int mReqChannelCount;
898 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800899 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700900 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901
902 class RecordHandle : public android::BnAudioRecord {
903 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700904 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 virtual ~RecordHandle();
906 virtual status_t start();
907 virtual void stop();
908 virtual sp<IMemory> getCblk() const;
909 virtual status_t onTransact(
910 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
911 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700912 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 };
914
Eric Laurent65b65452010-06-01 23:49:17 -0700915 //--- Audio Effect Management
916
917 // EffectModule and EffectChain classes both have their own mutex to protect
918 // state changes or resource modifications. Always respect the following order
919 // if multiple mutexes must be acquired to avoid cross deadlock:
920 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
921
922 // The EffectModule class is a wrapper object controlling the effect engine implementation
923 // in the effect library. It prevents concurrent calls to process() and command() functions
924 // from different client threads. It keeps a list of EffectHandle objects corresponding
925 // to all client applications using this effect and notifies applications of effect state,
926 // control or parameter changes. It manages the activation state machine to send appropriate
927 // reset, enable, disable commands to effect engine and provide volume
928 // ramping when effects are activated/deactivated.
929 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
930 // the attached track(s) to accumulate their auxiliary channel.
931 class EffectModule: public RefBase {
932 public:
933 EffectModule(const wp<ThreadBase>& wThread,
934 const wp<AudioFlinger::EffectChain>& chain,
935 effect_descriptor_t *desc,
936 int id,
937 int sessionId);
938 ~EffectModule();
939
940 enum effect_state {
941 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700942 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700943 STARTING,
944 ACTIVE,
945 STOPPING,
946 STOPPED
947 };
948
949 int id() { return mId; }
950 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -0700951 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700952 status_t command(uint32_t cmdCode,
953 uint32_t cmdSize,
954 void *pCmdData,
955 uint32_t *replySize,
956 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -0700957
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700958 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700959 status_t configure();
960 status_t init();
961 uint32_t state() {
962 return mState;
963 }
964 uint32_t status() {
965 return mStatus;
966 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700967 int sessionId() {
968 return mSessionId;
969 }
Eric Laurent65b65452010-06-01 23:49:17 -0700970 status_t setEnabled(bool enabled);
971 bool isEnabled();
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700972 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -0700973
974 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
975 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
976 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
977 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700978 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
979 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -0700980
981 status_t addHandle(sp<EffectHandle>& handle);
982 void disconnect(const wp<EffectHandle>& handle);
983 size_t removeHandle (const wp<EffectHandle>& handle);
984
985 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700986 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700987
988 status_t setDevice(uint32_t device);
989 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700990 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700991
992 status_t dump(int fd, const Vector<String16>& args);
993
994 protected:
995
Eric Laurent7d850f22010-07-09 13:34:17 -0700996 // Maximum time allocated to effect engines to complete the turn off sequence
997 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
998
Eric Laurent65b65452010-06-01 23:49:17 -0700999 EffectModule(const EffectModule&);
1000 EffectModule& operator = (const EffectModule&);
1001
Eric Laurentdf9b81c2010-07-02 08:12:41 -07001002 status_t start_l();
1003 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001004
1005 Mutex mLock; // mutex for process, commands and handles list protection
1006 wp<ThreadBase> mThread; // parent thread
1007 wp<EffectChain> mChain; // parent effect chain
1008 int mId; // this instance unique ID
1009 int mSessionId; // audio session ID
1010 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1011 effect_config_t mConfig; // input and output audio configuration
Eric Laurent0fb66c22011-05-17 19:16:02 -07001012 effect_handle_t mEffectInterface; // Effect module C API
Eric Laurent65b65452010-06-01 23:49:17 -07001013 status_t mStatus; // initialization status
1014 uint32_t mState; // current activation state (effect_state)
1015 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001016 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1017 // sending disable command.
1018 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001019 };
1020
1021 // The EffectHandle class implements the IEffect interface. It provides resources
1022 // to receive parameter updates, keeps track of effect control
1023 // ownership and state and has a pointer to the EffectModule object it is controlling.
1024 // There is one EffectHandle object for each application controlling (or using)
1025 // an effect module.
1026 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1027 class EffectHandle: public android::BnEffect {
1028 public:
1029
1030 EffectHandle(const sp<EffectModule>& effect,
1031 const sp<AudioFlinger::Client>& client,
1032 const sp<IEffectClient>& effectClient,
1033 int32_t priority);
1034 virtual ~EffectHandle();
1035
1036 // IEffect
1037 virtual status_t enable();
1038 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001039 virtual status_t command(uint32_t cmdCode,
1040 uint32_t cmdSize,
1041 void *pCmdData,
1042 uint32_t *replySize,
1043 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001044 virtual void disconnect();
1045 virtual sp<IMemory> getCblk() const;
1046 virtual status_t onTransact(uint32_t code, const Parcel& data,
1047 Parcel* reply, uint32_t flags);
1048
1049
1050 // Give or take control of effect module
1051 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001052 void commandExecuted(uint32_t cmdCode,
1053 uint32_t cmdSize,
1054 void *pCmdData,
1055 uint32_t replySize,
1056 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001057 void setEnabled(bool enabled);
1058
1059 // Getters
1060 int id() { return mEffect->id(); }
1061 int priority() { return mPriority; }
1062 bool hasControl() { return mHasControl; }
1063 sp<EffectModule> effect() { return mEffect; }
1064
1065 void dump(char* buffer, size_t size);
1066
1067 protected:
1068
1069 EffectHandle(const EffectHandle&);
1070 EffectHandle& operator =(const EffectHandle&);
1071
1072 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1073 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1074 sp<Client> mClient; // client for shared memory allocation
1075 sp<IMemory> mCblkMemory; // shared memory for control block
1076 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1077 uint8_t* mBuffer; // pointer to parameter area in shared memory
1078 int mPriority; // client application priority to control the effect
1079 bool mHasControl; // true if this handle is controlling the effect
1080 };
1081
1082 // the EffectChain class represents a group of effects associated to one audio session.
1083 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1084 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1085 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1086 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1087 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1088 // input buffer used by the track as accumulation buffer.
1089 class EffectChain: public RefBase {
1090 public:
1091 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1092 ~EffectChain();
1093
1094 void process_l();
1095
1096 void lock() {
1097 mLock.lock();
1098 }
1099 void unlock() {
1100 mLock.unlock();
1101 }
1102
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001103 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001104 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001105
1106 int sessionId() {
1107 return mSessionId;
1108 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001109
Eric Laurent76c40f72010-07-15 12:50:15 -07001110 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1111 sp<EffectModule> getEffectFromId_l(int id);
1112 bool setVolume_l(uint32_t *left, uint32_t *right);
1113 void setDevice_l(uint32_t device);
1114 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001115
Eric Laurent65b65452010-06-01 23:49:17 -07001116 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1117 mInBuffer = buffer;
1118 mOwnInBuffer = ownsBuffer;
1119 }
1120 int16_t *inBuffer() {
1121 return mInBuffer;
1122 }
1123 void setOutBuffer(int16_t *buffer) {
1124 mOutBuffer = buffer;
1125 }
1126 int16_t *outBuffer() {
1127 return mOutBuffer;
1128 }
1129
Eric Laurent90681d62011-05-09 12:09:06 -07001130 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1131 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1132 int32_t trackCnt() { return mTrackCnt;}
1133
1134 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1135 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1136 int32_t activeTrackCnt() { return mActiveTrackCnt;}
Eric Laurent65b65452010-06-01 23:49:17 -07001137
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001138 uint32_t strategy() { return mStrategy; }
1139 void setStrategy(uint32_t strategy)
1140 { mStrategy = strategy; }
1141
Eric Laurent65b65452010-06-01 23:49:17 -07001142 status_t dump(int fd, const Vector<String16>& args);
1143
1144 protected:
1145
1146 EffectChain(const EffectChain&);
1147 EffectChain& operator =(const EffectChain&);
1148
1149 wp<ThreadBase> mThread; // parent mixer thread
1150 Mutex mLock; // mutex protecting effect list
1151 Vector<sp<EffectModule> > mEffects; // list of effect modules
1152 int mSessionId; // audio session ID
1153 int16_t *mInBuffer; // chain input buffer
1154 int16_t *mOutBuffer; // chain output buffer
Eric Laurent90681d62011-05-09 12:09:06 -07001155 volatile int32_t mActiveTrackCnt; // number of active tracks connected
1156 volatile int32_t mTrackCnt; // number of tracks connected
Eric Laurent65b65452010-06-01 23:49:17 -07001157 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurent76c40f72010-07-15 12:50:15 -07001158 int mVolumeCtrlIdx; // index of insert effect having control over volume
1159 uint32_t mLeftVolume; // previous volume on left channel
1160 uint32_t mRightVolume; // previous volume on right channel
Eric Laurent0d7e0482010-07-19 06:24:46 -07001161 uint32_t mNewLeftVolume; // new volume on left channel
1162 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001163 uint32_t mStrategy; // strategy for this effect chain
Eric Laurent65b65452010-06-01 23:49:17 -07001164 };
1165
Dima Zavin31f188892011-04-18 16:57:27 -07001166 struct AudioStreamOut {
1167 audio_hw_device_t *hwDev;
1168 audio_stream_out_t *stream;
1169
1170 AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1171 hwDev(dev), stream(out) {}
1172 };
1173
1174 struct AudioStreamIn {
1175 audio_hw_device_t *hwDev;
1176 audio_stream_in_t *stream;
1177
1178 AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1179 hwDev(dev), stream(in) {}
1180 };
1181
Eric Laurenta553c252009-07-17 12:17:14 -07001182 friend class RecordThread;
1183 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001185 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 DefaultKeyedVector< pid_t, wp<Client> > mClients;
1188
Eric Laurenta553c252009-07-17 12:17:14 -07001189 mutable Mutex mHardwareLock;
Dima Zavin31f188892011-04-18 16:57:27 -07001190 audio_hw_device_t* mPrimaryHardwareDev;
1191 Vector<audio_hw_device_t*> mAudioHwDevs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001193
Eric Laurenta553c252009-07-17 12:17:14 -07001194
Eric Laurentddb78e72009-07-28 08:44:33 -07001195 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001196 PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Eric Laurenta553c252009-07-17 12:17:14 -07001197 float mMasterVolume;
1198 bool mMasterMute;
1199
Eric Laurentddb78e72009-07-28 08:44:33 -07001200 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -07001201
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001202 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent65b65452010-06-01 23:49:17 -07001203 volatile int32_t mNextUniqueId;
Eric Laurent53334cd2010-06-23 17:38:20 -07001204 uint32_t mMode;
1205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206};
1207
Dima Zavin31f188892011-04-18 16:57:27 -07001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209// ----------------------------------------------------------------------------
1210
1211}; // namespace android
1212
1213#endif // ANDROID_AUDIO_FLINGER_H