blob: 44da9ed45646d20cebbddda1529ff40c4511d914 [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>
Mathias Agopian07952722009-05-19 19:08:10 -070034#include <binder/MemoryDealer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/SortedVector.h>
36#include <utils/Vector.h>
37
38#include <hardware_legacy/AudioHardwareInterface.h>
39
40#include "AudioBufferProvider.h"
41
42namespace android {
43
44class audio_track_cblk_t;
45class AudioMixer;
46class AudioBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -070047class AudioResampler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49
50// ----------------------------------------------------------------------------
51
52#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
53#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
54
55
56// ----------------------------------------------------------------------------
57
58static const nsecs_t kStandbyTimeInNsecs = seconds(3);
59
Eric Laurenta553c252009-07-17 12:17:14 -070060class AudioFlinger : public BnAudioFlinger, public IBinder::DeathRecipient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061{
62public:
63 static void instantiate();
64
65 virtual status_t dump(int fd, const Vector<String16>& args);
66
67 // IAudioFlinger interface
68 virtual sp<IAudioTrack> createTrack(
69 pid_t pid,
70 int streamType,
71 uint32_t sampleRate,
72 int format,
73 int channelCount,
74 int frameCount,
75 uint32_t flags,
76 const sp<IMemory>& sharedBuffer,
Eric Laurentddb78e72009-07-28 08:44:33 -070077 int output,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 status_t *status);
79
Eric Laurentddb78e72009-07-28 08:44:33 -070080 virtual uint32_t sampleRate(int output) const;
81 virtual int channelCount(int output) const;
82 virtual int format(int output) const;
83 virtual size_t frameCount(int output) const;
84 virtual uint32_t latency(int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86 virtual status_t setMasterVolume(float value);
87 virtual status_t setMasterMute(bool muted);
88
89 virtual float masterVolume() const;
90 virtual bool masterMute() const;
91
Eric Laurentddb78e72009-07-28 08:44:33 -070092 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 virtual status_t setStreamMute(int stream, bool muted);
94
Eric Laurentddb78e72009-07-28 08:44:33 -070095 virtual float streamVolume(int stream, int output) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 virtual bool streamMute(int stream) const;
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 virtual status_t setMode(int mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
100 virtual status_t setMicMute(bool state);
101 virtual bool getMicMute() const;
102
Eric Laurent23f25cd2010-01-25 08:49:09 -0800103 virtual bool isStreamActive(int stream) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Eric Laurentddb78e72009-07-28 08:44:33 -0700105 virtual status_t setParameters(int ioHandle, const String8& keyValuePairs);
106 virtual String8 getParameters(int ioHandle, const String8& keys);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurenta553c252009-07-17 12:17:14 -0700109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700111
Eric Laurentddb78e72009-07-28 08:44:33 -0700112 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700113 uint32_t *pSamplingRate,
114 uint32_t *pFormat,
115 uint32_t *pChannels,
116 uint32_t *pLatencyMs,
117 uint32_t flags);
118
Eric Laurentddb78e72009-07-28 08:44:33 -0700119 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700120
Eric Laurentddb78e72009-07-28 08:44:33 -0700121 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700122
Eric Laurentddb78e72009-07-28 08:44:33 -0700123 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700124
Eric Laurentddb78e72009-07-28 08:44:33 -0700125 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700126
Eric Laurentddb78e72009-07-28 08:44:33 -0700127 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700128 uint32_t *pSamplingRate,
129 uint32_t *pFormat,
130 uint32_t *pChannels,
131 uint32_t acoustics);
132
Eric Laurentddb78e72009-07-28 08:44:33 -0700133 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700134
Eric Laurentddb78e72009-07-28 08:44:33 -0700135 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700136
Eric Laurent415f3e22009-10-21 08:14:22 -0700137 virtual status_t setVoiceVolume(float volume);
138
Eric Laurent0986e792010-01-19 17:37:09 -0800139 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 // IBinder::DeathRecipient
142 virtual void binderDied(const wp<IBinder>& who);
143
144 enum hardware_call_state {
145 AUDIO_HW_IDLE = 0,
146 AUDIO_HW_INIT,
147 AUDIO_HW_OUTPUT_OPEN,
148 AUDIO_HW_OUTPUT_CLOSE,
149 AUDIO_HW_INPUT_OPEN,
150 AUDIO_HW_INPUT_CLOSE,
151 AUDIO_HW_STANDBY,
152 AUDIO_HW_SET_MASTER_VOLUME,
153 AUDIO_HW_GET_ROUTING,
154 AUDIO_HW_SET_ROUTING,
155 AUDIO_HW_GET_MODE,
156 AUDIO_HW_SET_MODE,
157 AUDIO_HW_GET_MIC_MUTE,
158 AUDIO_HW_SET_MIC_MUTE,
159 AUDIO_SET_VOICE_VOLUME,
160 AUDIO_SET_PARAMETER,
161 };
162
163 // record interface
164 virtual sp<IAudioRecord> openRecord(
165 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700166 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 uint32_t sampleRate,
168 int format,
169 int channelCount,
170 int frameCount,
171 uint32_t flags,
172 status_t *status);
173
174 virtual status_t onTransact(
175 uint32_t code,
176 const Parcel& data,
177 Parcel* reply,
178 uint32_t flags);
179
180private:
181 AudioFlinger();
182 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
185 // Internal dump utilites.
186 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
187 status_t dumpClients(int fd, const Vector<String16>& args);
188 status_t dumpInternals(int fd, const Vector<String16>& args);
189
190 // --- Client ---
191 class Client : public RefBase {
192 public:
193 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
194 virtual ~Client();
195 const sp<MemoryDealer>& heap() const;
196 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700197 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 private:
200 Client(const Client&);
201 Client& operator = (const Client&);
202 sp<AudioFlinger> mAudioFlinger;
203 sp<MemoryDealer> mMemoryDealer;
204 pid_t mPid;
205 };
206
207
208 class TrackHandle;
209 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700210 class RecordThread;
211 class PlaybackThread;
212 class MixerThread;
213 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800214 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700215 class Track;
216 class RecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
Eric Laurenta553c252009-07-17 12:17:14 -0700218 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800220 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700221 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Eric Laurent3fdb1262009-11-07 00:01:32 -0800223 status_t dumpBase(int fd, const Vector<String16>& args);
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 // base for record and playback
226 class TrackBase : public AudioBufferProvider, public RefBase {
227
228 public:
229 enum track_state {
230 IDLE,
231 TERMINATED,
232 STOPPED,
233 RESUMING,
234 ACTIVE,
235 PAUSING,
236 PAUSED
237 };
238
239 enum track_flags {
240 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
241 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
242 // The upper 16 bits are used for track-specific flags.
243 };
244
Eric Laurenta553c252009-07-17 12:17:14 -0700245 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 uint32_t sampleRate,
248 int format,
249 int channelCount,
250 int frameCount,
251 uint32_t flags,
252 const sp<IMemory>& sharedBuffer);
253 ~TrackBase();
254
255 virtual status_t start() = 0;
256 virtual void stop() = 0;
257 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700258 audio_track_cblk_t* cblk() const { return mCblk; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
260 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700261 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700263 friend class PlaybackThread;
264 friend class RecordThread;
265 friend class MixerThread;
266 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
268 TrackBase(const TrackBase&);
269 TrackBase& operator = (const TrackBase&);
270
271 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
272 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 int format() const {
275 return mFormat;
276 }
277
278 int channelCount() const ;
279
280 int sampleRate() const;
281
282 void* getBuffer(uint32_t offset, uint32_t frames) const;
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 bool isStopped() const {
285 return mState == STOPPED;
286 }
287
288 bool isTerminated() const {
289 return mState == TERMINATED;
290 }
291
292 bool step();
293 void reset();
294
Eric Laurenta553c252009-07-17 12:17:14 -0700295 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 sp<Client> mClient;
297 sp<IMemory> mCblkMemory;
298 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 void* mBuffer;
300 void* mBufferEnd;
301 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 // we don't really need a lock for these
303 int mState;
304 int mClientTid;
305 uint8_t mFormat;
306 uint32_t mFlags;
307 };
308
Eric Laurenta553c252009-07-17 12:17:14 -0700309 class ConfigEvent {
310 public:
311 ConfigEvent() : mEvent(0), mParam(0) {}
312
313 int mEvent;
314 int mParam;
315 };
316
317 uint32_t sampleRate() const;
318 int channelCount() const;
319 int format() const;
320 size_t frameCount() const;
321 void wakeUp() { mWaitWorkCV.broadcast(); }
322 void exit();
323 virtual bool checkForNewParameters_l() = 0;
324 virtual status_t setParameters(const String8& keyValuePairs);
325 virtual String8 getParameters(const String8& keys) = 0;
326 virtual void audioConfigChanged(int event, int param = 0) = 0;
327 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700328 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700329 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800330 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800331 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700332
Eric Laurent2c817f52009-07-23 13:17:39 -0700333 mutable Mutex mLock;
334
Eric Laurenta553c252009-07-17 12:17:14 -0700335 protected:
336
337 friend class Track;
338 friend class TrackBase;
339 friend class PlaybackThread;
340 friend class MixerThread;
341 friend class DirectOutputThread;
342 friend class DuplicatingThread;
343 friend class RecordThread;
344 friend class RecordTrack;
345
Eric Laurenta553c252009-07-17 12:17:14 -0700346 Condition mWaitWorkCV;
347 sp<AudioFlinger> mAudioFlinger;
348 uint32_t mSampleRate;
349 size_t mFrameCount;
350 int mChannelCount;
351 int mFormat;
352 uint32_t mFrameSize;
353 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700354 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700355 status_t mParamStatus;
356 Vector<ConfigEvent *> mConfigEvents;
357 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800358 int mId;
359 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700360 };
361
362 // --- PlaybackThread ---
363 class PlaybackThread : public ThreadBase {
364 public:
365
366 enum type {
367 MIXER,
368 DIRECT,
369 DUPLICATING
370 };
371
Eric Laurent059b4be2009-11-09 23:32:22 -0800372 enum mixer_state {
373 MIXER_IDLE,
374 MIXER_TRACKS_ENABLED,
375 MIXER_TRACKS_READY
376 };
377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 // playback track
379 class Track : public TrackBase {
380 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700381 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 const sp<Client>& client,
383 int streamType,
384 uint32_t sampleRate,
385 int format,
386 int channelCount,
387 int frameCount,
388 const sp<IMemory>& sharedBuffer);
389 ~Track();
390
391 void dump(char* buffer, size_t size);
392 virtual status_t start();
393 virtual void stop();
394 void pause();
395
396 void flush();
397 void destroy();
398 void mute(bool);
399 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700400 int name() const {
401 return mName;
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Eric Laurent4bc035a2009-05-22 09:18:15 -0700404 int type() const {
405 return mStreamType;
406 }
407
408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700410 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700412 friend class TrackHandle;
413 friend class PlaybackThread;
414 friend class MixerThread;
415 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416
417 Track(const Track&);
418 Track& operator = (const Track&);
419
420 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700421 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 bool isPausing() const {
423 return mState == PAUSING;
424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 bool isPaused() const {
426 return mState == PAUSED;
427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 void setPaused() { mState = PAUSED; }
430 void reset();
431
Eric Laurent49f02be2009-11-19 09:00:56 -0800432 bool isOutputTrack() const {
433 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
434 }
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 // we don't really need a lock for these
437 float mVolume[2];
438 volatile bool mMute;
439 // FILLED state is used for suppressing volume ramp at begin of playing
440 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
441 mutable uint8_t mFillingUpStatus;
442 int8_t mRetryCount;
443 sp<IMemory> mSharedBuffer;
444 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700445 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700446 int mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }; // end of Track
448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449
450 // playback track
451 class OutputTrack : public Track {
452 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 class Buffer: public AudioBufferProvider::Buffer {
455 public:
456 int16_t *mBuffer;
457 };
Eric Laurenta553c252009-07-17 12:17:14 -0700458
459 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800460 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 uint32_t sampleRate,
462 int format,
463 int channelCount,
464 int frameCount);
465 ~OutputTrack();
466
467 virtual status_t start();
468 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700469 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700471 bool isActive() { return mActive; }
472 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
474 private:
475
Eric Laurenta553c252009-07-17 12:17:14 -0700476 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700478
479 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800480 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 Vector < Buffer* > mBufferQueue;
483 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700484 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800485 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700486 }; // end of OutputTrack
487
Eric Laurent49f02be2009-11-19 09:00:56 -0800488 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700489 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
491 virtual status_t dump(int fd, const Vector<String16>& args);
492
493 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 virtual status_t readyToRun();
495 virtual void onFirstRef();
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 virtual uint32_t latency() const;
498
499 virtual status_t setMasterVolume(float value);
500 virtual status_t setMasterMute(bool muted);
501
502 virtual float masterVolume() const;
503 virtual bool masterMute() const;
504
505 virtual status_t setStreamVolume(int stream, float value);
506 virtual status_t setStreamMute(int stream, bool muted);
507
508 virtual float streamVolume(int stream) const;
509 virtual bool streamMute(int stream) const;
510
Eric Laurent23f25cd2010-01-25 08:49:09 -0800511 bool isStreamActive(int stream) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700512
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700513 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 const sp<AudioFlinger::Client>& client,
515 int streamType,
516 uint32_t sampleRate,
517 int format,
518 int channelCount,
519 int frameCount,
520 const sp<IMemory>& sharedBuffer,
521 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700522
523 AudioStreamOut* getOutput() { return mOutput; }
524
525 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700526 void suspend() { mSuspended++; }
527 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800528 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700529 virtual String8 getParameters(const String8& keys);
530 virtual void audioConfigChanged(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800531 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurenta553c252009-07-17 12:17:14 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 struct stream_type_t {
534 stream_type_t()
535 : volume(1.0f),
536 mute(false)
537 {
538 }
539 float volume;
540 bool mute;
541 };
542
Eric Laurent2c817f52009-07-23 13:17:39 -0700543 protected:
544 int mType;
545 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700546 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700547 int mBytesWritten;
548 bool mMasterMute;
549 SortedVector< wp<Track> > mActiveTracks;
550
Eric Laurent62443f52009-10-05 20:29:18 -0700551 virtual int getTrackName_l() = 0;
552 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800553 virtual uint32_t activeSleepTimeUs() = 0;
554 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 private:
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700559 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 friend class Track;
561 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700562 friend class MixerThread;
563 friend class DirectOutputThread;
564 friend class DuplicatingThread;
565
566 PlaybackThread(const Client&);
567 PlaybackThread& operator = (const PlaybackThread&);
568
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700569 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700570 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700571
Eric Laurenta553c252009-07-17 12:17:14 -0700572 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573
Eric Laurenta553c252009-07-17 12:17:14 -0700574 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700578 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
579 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 nsecs_t mLastWriteTime;
583 int mNumWrites;
584 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 bool mInWrite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 };
587
Eric Laurenta553c252009-07-17 12:17:14 -0700588 class MixerThread : public PlaybackThread {
589 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800590 MixerThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700591 virtual ~MixerThread();
592
593 // Thread virtuals
594 virtual bool threadLoop();
595
596 void getTracks(SortedVector < sp<Track> >& tracks,
597 SortedVector < wp<Track> >& activeTracks,
598 int streamType);
599 void putTracks(SortedVector < sp<Track> >& tracks,
600 SortedVector < wp<Track> >& activeTracks);
Eric Laurenta553c252009-07-17 12:17:14 -0700601 virtual bool checkForNewParameters_l();
602 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
603
604 protected:
Eric Laurent059b4be2009-11-09 23:32:22 -0800605 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700606 virtual int getTrackName_l();
607 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800608 virtual uint32_t activeSleepTimeUs();
609 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700610
611 AudioMixer* mAudioMixer;
612 };
613
614 class DirectOutputThread : public PlaybackThread {
615 public:
616
Eric Laurent49f02be2009-11-19 09:00:56 -0800617 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700618 ~DirectOutputThread();
619
620 // Thread virtuals
621 virtual bool threadLoop();
622
Eric Laurent62443f52009-10-05 20:29:18 -0700623 virtual bool checkForNewParameters_l();
624
625 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700626 virtual int getTrackName_l();
627 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800628 virtual uint32_t activeSleepTimeUs();
629 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700630
631 private:
632 float mLeftVolume;
633 float mRightVolume;
634 };
635
636 class DuplicatingThread : public MixerThread {
637 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800638 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700639 ~DuplicatingThread();
640
641 // Thread virtuals
642 virtual bool threadLoop();
643 void addOutputTrack(MixerThread* thread);
644 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800645 uint32_t waitTimeMs() { return mWaitTimeMs; }
646 protected:
647 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700648
649 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800650 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
651 void updateWaitTime();
652
Eric Laurenta553c252009-07-17 12:17:14 -0700653 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800654 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700655 };
656
Eric Laurentddb78e72009-07-28 08:44:33 -0700657 PlaybackThread *checkPlaybackThread_l(int output) const;
658 MixerThread *checkMixerThread_l(int output) const;
659 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700660 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800661 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 friend class AudioBuffer;
664
665 class TrackHandle : public android::BnAudioTrack {
666 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700667 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 virtual ~TrackHandle();
669 virtual status_t start();
670 virtual void stop();
671 virtual void flush();
672 virtual void mute(bool);
673 virtual void pause();
674 virtual void setVolume(float left, float right);
675 virtual sp<IMemory> getCblk() const;
676 virtual status_t onTransact(
677 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
678 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700679 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 };
681
682 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700683 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
685
Eric Laurentb9481d82009-09-17 05:12:56 -0700686 void removeClient_l(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687
688
Eric Laurenta553c252009-07-17 12:17:14 -0700689 // record thread
690 class RecordThread : public ThreadBase, public AudioBufferProvider
691 {
692 public:
693
694 // record track
695 class RecordTrack : public TrackBase {
696 public:
697 RecordTrack(const wp<ThreadBase>& thread,
698 const sp<Client>& client,
699 uint32_t sampleRate,
700 int format,
701 int channelCount,
702 int frameCount,
703 uint32_t flags);
704 ~RecordTrack();
705
706 virtual status_t start();
707 virtual void stop();
708
709 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
710 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
711
Eric Laurent3fdb1262009-11-07 00:01:32 -0800712 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700713 private:
714 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700715 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700716
717 RecordTrack(const RecordTrack&);
718 RecordTrack& operator = (const RecordTrack&);
719
720 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
721
722 bool mOverflow;
723 };
724
725
726 RecordThread(const sp<AudioFlinger>& audioFlinger,
727 AudioStreamIn *input,
728 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800729 uint32_t channels,
730 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700731 ~RecordThread();
732
733 virtual bool threadLoop();
734 virtual status_t readyToRun() { return NO_ERROR; }
735 virtual void onFirstRef();
736
737 status_t start(RecordTrack* recordTrack);
738 void stop(RecordTrack* recordTrack);
739 status_t dump(int fd, const Vector<String16>& args);
740 AudioStreamIn* getInput() { return mInput; }
741
742 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
743 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
744 virtual bool checkForNewParameters_l();
745 virtual String8 getParameters(const String8& keys);
746 virtual void audioConfigChanged(int event, int param = 0);
747 void readInputParameters();
748
749 private:
750 RecordThread();
751 AudioStreamIn *mInput;
752 sp<RecordTrack> mActiveTrack;
753 Condition mStartStopCond;
754 AudioResampler *mResampler;
755 int32_t *mRsmpOutBuffer;
756 int16_t *mRsmpInBuffer;
757 size_t mRsmpInIndex;
758 size_t mInputBytes;
759 int mReqChannelCount;
760 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800761 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700762 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763
764 class RecordHandle : public android::BnAudioRecord {
765 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700766 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 virtual ~RecordHandle();
768 virtual status_t start();
769 virtual void stop();
770 virtual sp<IMemory> getCblk() const;
771 virtual status_t onTransact(
772 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
773 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700774 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 };
776
Eric Laurenta553c252009-07-17 12:17:14 -0700777 friend class RecordThread;
778 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700781 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 DefaultKeyedVector< pid_t, wp<Client> > mClients;
784
Eric Laurenta553c252009-07-17 12:17:14 -0700785 mutable Mutex mHardwareLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 AudioHardwareInterface* mAudioHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700788
Eric Laurenta553c252009-07-17 12:17:14 -0700789
Eric Laurentddb78e72009-07-28 08:44:33 -0700790 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurenta553c252009-07-17 12:17:14 -0700791 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
792 float mMasterVolume;
793 bool mMasterMute;
794
Eric Laurentddb78e72009-07-28 08:44:33 -0700795 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -0700796
797 SortedVector< sp<IBinder> > mNotificationClients;
Eric Laurentddb78e72009-07-28 08:44:33 -0700798 int mNextThreadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799};
800
801// ----------------------------------------------------------------------------
802
803}; // namespace android
804
805#endif // ANDROID_AUDIO_FLINGER_H