blob: 94f786112cbbe046914978b0b6ea0e3d760b104f [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 Zavin24fc2fb2011-04-19 22:30:36 -070041#include <hardware/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 Zavin31f188892011-04-18 16:57:27 -0700215 audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
217 // Internal dump utilites.
218 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
219 status_t dumpClients(int fd, const Vector<String16>& args);
220 status_t dumpInternals(int fd, const Vector<String16>& args);
221
222 // --- Client ---
223 class Client : public RefBase {
224 public:
225 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
226 virtual ~Client();
227 const sp<MemoryDealer>& heap() const;
228 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700229 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 private:
232 Client(const Client&);
233 Client& operator = (const Client&);
234 sp<AudioFlinger> mAudioFlinger;
235 sp<MemoryDealer> mMemoryDealer;
236 pid_t mPid;
237 };
238
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700239 // --- Notification Client ---
240 class NotificationClient : public IBinder::DeathRecipient {
241 public:
242 NotificationClient(const sp<AudioFlinger>& audioFlinger,
243 const sp<IAudioFlingerClient>& client,
244 pid_t pid);
245 virtual ~NotificationClient();
246
247 sp<IAudioFlingerClient> client() { return mClient; }
248
249 // IBinder::DeathRecipient
250 virtual void binderDied(const wp<IBinder>& who);
251
252 private:
253 NotificationClient(const NotificationClient&);
254 NotificationClient& operator = (const NotificationClient&);
255
256 sp<AudioFlinger> mAudioFlinger;
257 pid_t mPid;
258 sp<IAudioFlingerClient> mClient;
259 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 class TrackHandle;
262 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700263 class RecordThread;
264 class PlaybackThread;
265 class MixerThread;
266 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800267 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700268 class Track;
269 class RecordTrack;
Eric Laurent65b65452010-06-01 23:49:17 -0700270 class EffectModule;
271 class EffectHandle;
272 class EffectChain;
Dima Zavin31f188892011-04-18 16:57:27 -0700273 struct AudioStreamOut;
274 struct AudioStreamIn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
Eric Laurenta553c252009-07-17 12:17:14 -0700276 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800278 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700279 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
Eric Laurent3fdb1262009-11-07 00:01:32 -0800281 status_t dumpBase(int fd, const Vector<String16>& args);
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 // base for record and playback
284 class TrackBase : public AudioBufferProvider, public RefBase {
285
286 public:
287 enum track_state {
288 IDLE,
289 TERMINATED,
290 STOPPED,
291 RESUMING,
292 ACTIVE,
293 PAUSING,
294 PAUSED
295 };
296
297 enum track_flags {
298 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
299 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
300 // The upper 16 bits are used for track-specific flags.
301 };
302
Eric Laurenta553c252009-07-17 12:17:14 -0700303 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 uint32_t sampleRate,
306 int format,
307 int channelCount,
308 int frameCount,
309 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -0700310 const sp<IMemory>& sharedBuffer,
311 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 ~TrackBase();
313
314 virtual status_t start() = 0;
315 virtual void stop() = 0;
316 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700317 audio_track_cblk_t* cblk() const { return mCblk; }
Eric Laurent65b65452010-06-01 23:49:17 -0700318 int sessionId() { return mSessionId; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
320 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700321 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700323 friend class PlaybackThread;
324 friend class RecordThread;
325 friend class MixerThread;
326 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
328 TrackBase(const TrackBase&);
329 TrackBase& operator = (const TrackBase&);
330
331 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
332 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 int format() const {
335 return mFormat;
336 }
337
338 int channelCount() const ;
339
340 int sampleRate() const;
341
342 void* getBuffer(uint32_t offset, uint32_t frames) const;
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 bool isStopped() const {
345 return mState == STOPPED;
346 }
347
348 bool isTerminated() const {
349 return mState == TERMINATED;
350 }
351
352 bool step();
353 void reset();
354
Eric Laurenta553c252009-07-17 12:17:14 -0700355 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 sp<Client> mClient;
357 sp<IMemory> mCblkMemory;
358 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 void* mBuffer;
360 void* mBufferEnd;
361 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 // we don't really need a lock for these
363 int mState;
364 int mClientTid;
365 uint8_t mFormat;
366 uint32_t mFlags;
Eric Laurent65b65452010-06-01 23:49:17 -0700367 int mSessionId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 };
369
Eric Laurenta553c252009-07-17 12:17:14 -0700370 class ConfigEvent {
371 public:
372 ConfigEvent() : mEvent(0), mParam(0) {}
373
374 int mEvent;
375 int mParam;
376 };
377
378 uint32_t sampleRate() const;
379 int channelCount() const;
380 int format() const;
381 size_t frameCount() const;
382 void wakeUp() { mWaitWorkCV.broadcast(); }
383 void exit();
384 virtual bool checkForNewParameters_l() = 0;
385 virtual status_t setParameters(const String8& keyValuePairs);
386 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700387 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700388 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700389 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700390 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800391 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800392 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700393
Eric Laurent2c817f52009-07-23 13:17:39 -0700394 mutable Mutex mLock;
395
Eric Laurenta553c252009-07-17 12:17:14 -0700396 protected:
397
398 friend class Track;
399 friend class TrackBase;
400 friend class PlaybackThread;
401 friend class MixerThread;
402 friend class DirectOutputThread;
403 friend class DuplicatingThread;
404 friend class RecordThread;
405 friend class RecordTrack;
406
Eric Laurenta553c252009-07-17 12:17:14 -0700407 Condition mWaitWorkCV;
408 sp<AudioFlinger> mAudioFlinger;
409 uint32_t mSampleRate;
410 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700411 uint32_t mChannels;
412 uint16_t mChannelCount;
413 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700414 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700415 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700416 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700417 status_t mParamStatus;
418 Vector<ConfigEvent *> mConfigEvents;
419 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800420 int mId;
421 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700422 };
423
424 // --- PlaybackThread ---
425 class PlaybackThread : public ThreadBase {
426 public:
427
428 enum type {
429 MIXER,
430 DIRECT,
431 DUPLICATING
432 };
433
Eric Laurent059b4be2009-11-09 23:32:22 -0800434 enum mixer_state {
435 MIXER_IDLE,
436 MIXER_TRACKS_ENABLED,
437 MIXER_TRACKS_READY
438 };
439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 // playback track
441 class Track : public TrackBase {
442 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700443 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 const sp<Client>& client,
445 int streamType,
446 uint32_t sampleRate,
447 int format,
448 int channelCount,
449 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700450 const sp<IMemory>& sharedBuffer,
451 int sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 ~Track();
453
454 void dump(char* buffer, size_t size);
455 virtual status_t start();
456 virtual void stop();
457 void pause();
458
459 void flush();
460 void destroy();
461 void mute(bool);
462 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700463 int name() const {
464 return mName;
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466
Eric Laurent4bc035a2009-05-22 09:18:15 -0700467 int type() const {
468 return mStreamType;
469 }
Eric Laurent65b65452010-06-01 23:49:17 -0700470 status_t attachAuxEffect(int EffectId);
471 void setAuxBuffer(int EffectId, int32_t *buffer);
472 int32_t *auxBuffer() { return mAuxBuffer; }
473 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
474 int16_t *mainBuffer() { return mMainBuffer; }
475 int auxEffectId() { return mAuxEffectId; }
Eric Laurent4bc035a2009-05-22 09:18:15 -0700476
477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700479 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700481 friend class TrackHandle;
482 friend class PlaybackThread;
483 friend class MixerThread;
484 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
486 Track(const Track&);
487 Track& operator = (const Track&);
488
489 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700490 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 bool isPausing() const {
492 return mState == PAUSING;
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 bool isPaused() const {
495 return mState == PAUSED;
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 void setPaused() { mState = PAUSED; }
499 void reset();
500
Eric Laurent49f02be2009-11-19 09:00:56 -0800501 bool isOutputTrack() const {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700502 return (mStreamType == AUDIO_STREAM_CNT);
Eric Laurent49f02be2009-11-19 09:00:56 -0800503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 // we don't really need a lock for these
506 float mVolume[2];
507 volatile bool mMute;
508 // FILLED state is used for suppressing volume ramp at begin of playing
509 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
510 mutable uint8_t mFillingUpStatus;
511 int8_t mRetryCount;
512 sp<IMemory> mSharedBuffer;
513 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700514 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700515 int mName;
Eric Laurent65b65452010-06-01 23:49:17 -0700516 int16_t *mMainBuffer;
517 int32_t *mAuxBuffer;
518 int mAuxEffectId;
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700519 bool mHasVolumeController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }; // end of Track
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
523 // playback track
524 class OutputTrack : public Track {
525 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 class Buffer: public AudioBufferProvider::Buffer {
528 public:
529 int16_t *mBuffer;
530 };
Eric Laurenta553c252009-07-17 12:17:14 -0700531
532 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800533 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 uint32_t sampleRate,
535 int format,
536 int channelCount,
537 int frameCount);
538 ~OutputTrack();
539
540 virtual status_t start();
541 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700542 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700544 bool isActive() { return mActive; }
545 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546
547 private:
548
Eric Laurenta553c252009-07-17 12:17:14 -0700549 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700551
552 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800553 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 Vector < Buffer* > mBufferQueue;
556 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700557 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800558 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700559 }; // end of OutputTrack
560
Dima Zavin31f188892011-04-18 16:57:27 -0700561 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700562 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
564 virtual status_t dump(int fd, const Vector<String16>& args);
565
566 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 virtual status_t readyToRun();
568 virtual void onFirstRef();
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 virtual uint32_t latency() const;
571
572 virtual status_t setMasterVolume(float value);
573 virtual status_t setMasterMute(bool muted);
574
575 virtual float masterVolume() const;
576 virtual bool masterMute() const;
577
578 virtual status_t setStreamVolume(int stream, float value);
579 virtual status_t setStreamMute(int stream, bool muted);
580
581 virtual float streamVolume(int stream) const;
582 virtual bool streamMute(int stream) const;
583
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700584 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 const sp<AudioFlinger::Client>& client,
586 int streamType,
587 uint32_t sampleRate,
588 int format,
589 int channelCount,
590 int frameCount,
591 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700592 int sessionId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700594
Dima Zavin31f188892011-04-18 16:57:27 -0700595 AudioStreamOut* getOutput() { return mOutput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700596
597 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700598 void suspend() { mSuspended++; }
599 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800600 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700601 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700602 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800603 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent65b65452010-06-01 23:49:17 -0700604 int16_t *mixBuffer() { return mMixBuffer; };
605
606 sp<EffectHandle> createEffect_l(
607 const sp<AudioFlinger::Client>& client,
608 const sp<IEffectClient>& effectClient,
609 int32_t priority,
610 int sessionId,
611 effect_descriptor_t *desc,
612 int *enabled,
613 status_t *status);
Eric Laurent53334cd2010-06-23 17:38:20 -0700614 void disconnectEffect(const sp< EffectModule>& effect,
615 const wp<EffectHandle>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -0700616
Eric Laurent493941b2010-07-28 01:32:47 -0700617 // return values for hasAudioSession (bit field)
618 enum effect_state {
619 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
620 // effect
621 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
622 // track
623 };
624
625 uint32_t hasAudioSession(int sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700626 sp<EffectChain> getEffectChain(int sessionId);
627 sp<EffectChain> getEffectChain_l(int sessionId);
628 status_t addEffectChain_l(const sp<EffectChain>& chain);
629 size_t removeEffectChain_l(const sp<EffectChain>& chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700630 void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains);
631 void unlockEffectChains(Vector<sp <EffectChain> >& effectChains);
Eric Laurent65b65452010-06-01 23:49:17 -0700632
633 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
634 void detachAuxEffect_l(int effectId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700635 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
636 int EffectId);
637 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
638 int EffectId);
Eric Laurent53334cd2010-06-23 17:38:20 -0700639 void setMode(uint32_t mode);
Eric Laurenta553c252009-07-17 12:17:14 -0700640
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700641 status_t addEffect_l(const sp< EffectModule>& effect);
642 void removeEffect_l(const sp< EffectModule>& effect);
643
644 uint32_t getStrategyForSession_l(int sessionId);
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 struct stream_type_t {
647 stream_type_t()
648 : volume(1.0f),
649 mute(false)
650 {
651 }
652 float volume;
653 bool mute;
654 };
655
Eric Laurent2c817f52009-07-23 13:17:39 -0700656 protected:
657 int mType;
658 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700659 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700660 int mBytesWritten;
661 bool mMasterMute;
662 SortedVector< wp<Track> > mActiveTracks;
663
Eric Laurent62443f52009-10-05 20:29:18 -0700664 virtual int getTrackName_l() = 0;
665 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800666 virtual uint32_t activeSleepTimeUs() = 0;
667 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent8448a792010-08-18 18:13:17 -0700668 virtual uint32_t suspendSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 private:
671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700673 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 friend class Track;
675 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700676 friend class MixerThread;
677 friend class DirectOutputThread;
678 friend class DuplicatingThread;
679
680 PlaybackThread(const Client&);
681 PlaybackThread& operator = (const PlaybackThread&);
682
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700683 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700684 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700685
Eric Laurenta553c252009-07-17 12:17:14 -0700686 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687
Eric Laurent65b65452010-06-01 23:49:17 -0700688 uint32_t device() { return mDevice; }
689
Eric Laurenta553c252009-07-17 12:17:14 -0700690 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent65b65452010-06-01 23:49:17 -0700692 status_t dumpEffectChains(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700695 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700696 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Dima Zavin31f188892011-04-18 16:57:27 -0700697 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 nsecs_t mLastWriteTime;
700 int mNumWrites;
701 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 bool mInWrite;
Eric Laurent65b65452010-06-01 23:49:17 -0700703 Vector< sp<EffectChain> > mEffectChains;
704 uint32_t mDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 };
706
Eric Laurenta553c252009-07-17 12:17:14 -0700707 class MixerThread : public PlaybackThread {
708 public:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700709 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700710 AudioStreamOut* output,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700711 int id,
712 uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700713 virtual ~MixerThread();
714
715 // Thread virtuals
716 virtual bool threadLoop();
717
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700718 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700719 virtual bool checkForNewParameters_l();
720 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
721
722 protected:
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700723 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks,
724 Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700725 virtual int getTrackName_l();
726 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800727 virtual uint32_t activeSleepTimeUs();
728 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700729 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700730
731 AudioMixer* mAudioMixer;
732 };
733
734 class DirectOutputThread : public PlaybackThread {
735 public:
736
Dima Zavin31f188892011-04-18 16:57:27 -0700737 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
Eric Laurenta553c252009-07-17 12:17:14 -0700738 ~DirectOutputThread();
739
740 // Thread virtuals
741 virtual bool threadLoop();
742
Eric Laurent62443f52009-10-05 20:29:18 -0700743 virtual bool checkForNewParameters_l();
744
745 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700746 virtual int getTrackName_l();
747 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800748 virtual uint32_t activeSleepTimeUs();
749 virtual uint32_t idleSleepTimeUs();
Eric Laurent8448a792010-08-18 18:13:17 -0700750 virtual uint32_t suspendSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700751
752 private:
Eric Laurent65b65452010-06-01 23:49:17 -0700753 void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp);
754
755 float mLeftVolFloat;
756 float mRightVolFloat;
757 uint16_t mLeftVolShort;
758 uint16_t mRightVolShort;
Eric Laurenta553c252009-07-17 12:17:14 -0700759 };
760
761 class DuplicatingThread : public MixerThread {
762 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800763 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700764 ~DuplicatingThread();
765
766 // Thread virtuals
767 virtual bool threadLoop();
768 void addOutputTrack(MixerThread* thread);
769 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800770 uint32_t waitTimeMs() { return mWaitTimeMs; }
771 protected:
772 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700773
774 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800775 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
776 void updateWaitTime();
777
Eric Laurenta553c252009-07-17 12:17:14 -0700778 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800779 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700780 };
781
Eric Laurentddb78e72009-07-28 08:44:33 -0700782 PlaybackThread *checkPlaybackThread_l(int output) const;
783 MixerThread *checkMixerThread_l(int output) const;
784 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700785 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800786 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700787
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800788 int nextUniqueId_l();
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700789 status_t moveEffectChain_l(int session,
790 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -0700791 AudioFlinger::PlaybackThread *dstThread,
792 bool reRegister);
Eric Laurent65b65452010-06-01 23:49:17 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 friend class AudioBuffer;
795
796 class TrackHandle : public android::BnAudioTrack {
797 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700798 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 virtual ~TrackHandle();
800 virtual status_t start();
801 virtual void stop();
802 virtual void flush();
803 virtual void mute(bool);
804 virtual void pause();
805 virtual void setVolume(float left, float right);
806 virtual sp<IMemory> getCblk() const;
Eric Laurent65b65452010-06-01 23:49:17 -0700807 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 virtual status_t onTransact(
809 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
810 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700811 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 };
813
814 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700815 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816
817
Eric Laurentb9481d82009-09-17 05:12:56 -0700818 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700819 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820
821
Eric Laurenta553c252009-07-17 12:17:14 -0700822 // record thread
823 class RecordThread : public ThreadBase, public AudioBufferProvider
824 {
825 public:
826
827 // record track
828 class RecordTrack : public TrackBase {
829 public:
830 RecordTrack(const wp<ThreadBase>& thread,
831 const sp<Client>& client,
832 uint32_t sampleRate,
833 int format,
834 int channelCount,
835 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -0700836 uint32_t flags,
837 int sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700838 ~RecordTrack();
839
840 virtual status_t start();
841 virtual void stop();
842
843 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
844 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
845
Eric Laurent3fdb1262009-11-07 00:01:32 -0800846 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700847 private:
848 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700849 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700850
851 RecordTrack(const RecordTrack&);
852 RecordTrack& operator = (const RecordTrack&);
853
854 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
855
856 bool mOverflow;
857 };
858
859
860 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin31f188892011-04-18 16:57:27 -0700861 AudioStreamIn *input,
Eric Laurenta553c252009-07-17 12:17:14 -0700862 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800863 uint32_t channels,
864 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700865 ~RecordThread();
866
867 virtual bool threadLoop();
868 virtual status_t readyToRun() { return NO_ERROR; }
869 virtual void onFirstRef();
870
871 status_t start(RecordTrack* recordTrack);
872 void stop(RecordTrack* recordTrack);
873 status_t dump(int fd, const Vector<String16>& args);
Dima Zavin31f188892011-04-18 16:57:27 -0700874 AudioStreamIn* getInput() { return mInput; }
Eric Laurenta553c252009-07-17 12:17:14 -0700875
876 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
877 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
878 virtual bool checkForNewParameters_l();
879 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700880 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700881 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800882 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700883
884 private:
885 RecordThread();
Dima Zavin31f188892011-04-18 16:57:27 -0700886 AudioStreamIn *mInput;
Eric Laurenta553c252009-07-17 12:17:14 -0700887 sp<RecordTrack> mActiveTrack;
888 Condition mStartStopCond;
889 AudioResampler *mResampler;
890 int32_t *mRsmpOutBuffer;
891 int16_t *mRsmpInBuffer;
892 size_t mRsmpInIndex;
893 size_t mInputBytes;
894 int mReqChannelCount;
895 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800896 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700897 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898
899 class RecordHandle : public android::BnAudioRecord {
900 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700901 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 virtual ~RecordHandle();
903 virtual status_t start();
904 virtual void stop();
905 virtual sp<IMemory> getCblk() const;
906 virtual status_t onTransact(
907 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
908 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700909 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 };
911
Eric Laurent65b65452010-06-01 23:49:17 -0700912 //--- Audio Effect Management
913
914 // EffectModule and EffectChain classes both have their own mutex to protect
915 // state changes or resource modifications. Always respect the following order
916 // if multiple mutexes must be acquired to avoid cross deadlock:
917 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
918
919 // The EffectModule class is a wrapper object controlling the effect engine implementation
920 // in the effect library. It prevents concurrent calls to process() and command() functions
921 // from different client threads. It keeps a list of EffectHandle objects corresponding
922 // to all client applications using this effect and notifies applications of effect state,
923 // control or parameter changes. It manages the activation state machine to send appropriate
924 // reset, enable, disable commands to effect engine and provide volume
925 // ramping when effects are activated/deactivated.
926 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
927 // the attached track(s) to accumulate their auxiliary channel.
928 class EffectModule: public RefBase {
929 public:
930 EffectModule(const wp<ThreadBase>& wThread,
931 const wp<AudioFlinger::EffectChain>& chain,
932 effect_descriptor_t *desc,
933 int id,
934 int sessionId);
935 ~EffectModule();
936
937 enum effect_state {
938 IDLE,
Eric Laurent7d850f22010-07-09 13:34:17 -0700939 RESTART,
Eric Laurent65b65452010-06-01 23:49:17 -0700940 STARTING,
941 ACTIVE,
942 STOPPING,
943 STOPPED
944 };
945
946 int id() { return mId; }
947 void process();
Eric Laurent7d850f22010-07-09 13:34:17 -0700948 void updateState();
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700949 status_t command(uint32_t cmdCode,
950 uint32_t cmdSize,
951 void *pCmdData,
952 uint32_t *replySize,
953 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -0700954
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700955 void reset_l();
Eric Laurent65b65452010-06-01 23:49:17 -0700956 status_t configure();
957 status_t init();
958 uint32_t state() {
959 return mState;
960 }
961 uint32_t status() {
962 return mStatus;
963 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700964 int sessionId() {
965 return mSessionId;
966 }
Eric Laurent65b65452010-06-01 23:49:17 -0700967 status_t setEnabled(bool enabled);
968 bool isEnabled();
Eric Laurenta92ebfa2010-08-31 13:50:07 -0700969 bool isProcessEnabled();
Eric Laurent65b65452010-06-01 23:49:17 -0700970
971 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
972 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
973 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
974 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700975 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
976 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Eric Laurent65b65452010-06-01 23:49:17 -0700977
978 status_t addHandle(sp<EffectHandle>& handle);
979 void disconnect(const wp<EffectHandle>& handle);
980 size_t removeHandle (const wp<EffectHandle>& handle);
981
982 effect_descriptor_t& desc() { return mDescriptor; }
Eric Laurent53334cd2010-06-23 17:38:20 -0700983 wp<EffectChain>& chain() { return mChain; }
Eric Laurent65b65452010-06-01 23:49:17 -0700984
985 status_t setDevice(uint32_t device);
986 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Eric Laurent53334cd2010-06-23 17:38:20 -0700987 status_t setMode(uint32_t mode);
Eric Laurent65b65452010-06-01 23:49:17 -0700988
989 status_t dump(int fd, const Vector<String16>& args);
990
991 protected:
992
Eric Laurent7d850f22010-07-09 13:34:17 -0700993 // Maximum time allocated to effect engines to complete the turn off sequence
994 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
995
Eric Laurent65b65452010-06-01 23:49:17 -0700996 EffectModule(const EffectModule&);
997 EffectModule& operator = (const EffectModule&);
998
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700999 status_t start_l();
1000 status_t stop_l();
Eric Laurent65b65452010-06-01 23:49:17 -07001001
Eric Laurent53334cd2010-06-23 17:38:20 -07001002 // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified
1003 static const uint32_t sDeviceConvTable[];
1004 static uint32_t deviceAudioSystemToEffectApi(uint32_t device);
1005
1006 // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified
1007 static const uint32_t sModeConvTable[];
1008 static int modeAudioSystemToEffectApi(uint32_t mode);
1009
Eric Laurent65b65452010-06-01 23:49:17 -07001010 Mutex mLock; // mutex for process, commands and handles list protection
1011 wp<ThreadBase> mThread; // parent thread
1012 wp<EffectChain> mChain; // parent effect chain
1013 int mId; // this instance unique ID
1014 int mSessionId; // audio session ID
1015 effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1016 effect_config_t mConfig; // input and output audio configuration
1017 effect_interface_t mEffectInterface; // Effect module C API
1018 status_t mStatus; // initialization status
1019 uint32_t mState; // current activation state (effect_state)
1020 Vector< wp<EffectHandle> > mHandles; // list of client handles
Eric Laurent7d850f22010-07-09 13:34:17 -07001021 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1022 // sending disable command.
1023 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent65b65452010-06-01 23:49:17 -07001024 };
1025
1026 // The EffectHandle class implements the IEffect interface. It provides resources
1027 // to receive parameter updates, keeps track of effect control
1028 // ownership and state and has a pointer to the EffectModule object it is controlling.
1029 // There is one EffectHandle object for each application controlling (or using)
1030 // an effect module.
1031 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1032 class EffectHandle: public android::BnEffect {
1033 public:
1034
1035 EffectHandle(const sp<EffectModule>& effect,
1036 const sp<AudioFlinger::Client>& client,
1037 const sp<IEffectClient>& effectClient,
1038 int32_t priority);
1039 virtual ~EffectHandle();
1040
1041 // IEffect
1042 virtual status_t enable();
1043 virtual status_t disable();
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001044 virtual status_t command(uint32_t cmdCode,
1045 uint32_t cmdSize,
1046 void *pCmdData,
1047 uint32_t *replySize,
1048 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001049 virtual void disconnect();
1050 virtual sp<IMemory> getCblk() const;
1051 virtual status_t onTransact(uint32_t code, const Parcel& data,
1052 Parcel* reply, uint32_t flags);
1053
1054
1055 // Give or take control of effect module
1056 void setControl(bool hasControl, bool signal);
Eric Laurenta4c72ac2010-07-28 05:40:18 -07001057 void commandExecuted(uint32_t cmdCode,
1058 uint32_t cmdSize,
1059 void *pCmdData,
1060 uint32_t replySize,
1061 void *pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07001062 void setEnabled(bool enabled);
1063
1064 // Getters
1065 int id() { return mEffect->id(); }
1066 int priority() { return mPriority; }
1067 bool hasControl() { return mHasControl; }
1068 sp<EffectModule> effect() { return mEffect; }
1069
1070 void dump(char* buffer, size_t size);
1071
1072 protected:
1073
1074 EffectHandle(const EffectHandle&);
1075 EffectHandle& operator =(const EffectHandle&);
1076
1077 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1078 sp<IEffectClient> mEffectClient; // callback interface for client notifications
1079 sp<Client> mClient; // client for shared memory allocation
1080 sp<IMemory> mCblkMemory; // shared memory for control block
1081 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1082 uint8_t* mBuffer; // pointer to parameter area in shared memory
1083 int mPriority; // client application priority to control the effect
1084 bool mHasControl; // true if this handle is controlling the effect
1085 };
1086
1087 // the EffectChain class represents a group of effects associated to one audio session.
1088 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1089 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1090 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1091 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1092 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1093 // input buffer used by the track as accumulation buffer.
1094 class EffectChain: public RefBase {
1095 public:
1096 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1097 ~EffectChain();
1098
1099 void process_l();
1100
1101 void lock() {
1102 mLock.lock();
1103 }
1104 void unlock() {
1105 mLock.unlock();
1106 }
1107
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001108 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent76c40f72010-07-15 12:50:15 -07001109 size_t removeEffect_l(const sp<EffectModule>& handle);
Eric Laurent65b65452010-06-01 23:49:17 -07001110
1111 int sessionId() {
1112 return mSessionId;
1113 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001114
Eric Laurent76c40f72010-07-15 12:50:15 -07001115 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1116 sp<EffectModule> getEffectFromId_l(int id);
1117 bool setVolume_l(uint32_t *left, uint32_t *right);
1118 void setDevice_l(uint32_t device);
1119 void setMode_l(uint32_t mode);
Eric Laurent53334cd2010-06-23 17:38:20 -07001120
Eric Laurent65b65452010-06-01 23:49:17 -07001121 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1122 mInBuffer = buffer;
1123 mOwnInBuffer = ownsBuffer;
1124 }
1125 int16_t *inBuffer() {
1126 return mInBuffer;
1127 }
1128 void setOutBuffer(int16_t *buffer) {
1129 mOutBuffer = buffer;
1130 }
1131 int16_t *outBuffer() {
1132 return mOutBuffer;
1133 }
1134
1135 void startTrack() {mActiveTrackCnt++;}
1136 void stopTrack() {mActiveTrackCnt--;}
1137 int activeTracks() { return mActiveTrackCnt;}
1138
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001139 uint32_t strategy() { return mStrategy; }
1140 void setStrategy(uint32_t strategy)
1141 { mStrategy = strategy; }
1142
Eric Laurent65b65452010-06-01 23:49:17 -07001143 status_t dump(int fd, const Vector<String16>& args);
1144
1145 protected:
1146
1147 EffectChain(const EffectChain&);
1148 EffectChain& operator =(const EffectChain&);
1149
1150 wp<ThreadBase> mThread; // parent mixer thread
1151 Mutex mLock; // mutex protecting effect list
1152 Vector<sp<EffectModule> > mEffects; // list of effect modules
1153 int mSessionId; // audio session ID
1154 int16_t *mInBuffer; // chain input buffer
1155 int16_t *mOutBuffer; // chain output buffer
Eric Laurent65b65452010-06-01 23:49:17 -07001156 int mActiveTrackCnt; // number of active tracks connected
1157 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