blob: f35f38bc4eafa9e40c44b24afa9ed2b2623ee72c [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 Laurent4f0f17d2010-05-12 02:05:53 -070060class AudioFlinger : public BnAudioFlinger
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 Laurent47d0a922010-02-26 02:47:27 -0800111 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700112
Eric Laurentddb78e72009-07-28 08:44:33 -0700113 virtual int openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700114 uint32_t *pSamplingRate,
115 uint32_t *pFormat,
116 uint32_t *pChannels,
117 uint32_t *pLatencyMs,
118 uint32_t flags);
119
Eric Laurentddb78e72009-07-28 08:44:33 -0700120 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurenta553c252009-07-17 12:17:14 -0700121
Eric Laurentddb78e72009-07-28 08:44:33 -0700122 virtual status_t closeOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700123
Eric Laurentddb78e72009-07-28 08:44:33 -0700124 virtual status_t suspendOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700125
Eric Laurentddb78e72009-07-28 08:44:33 -0700126 virtual status_t restoreOutput(int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700127
Eric Laurentddb78e72009-07-28 08:44:33 -0700128 virtual int openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -0700129 uint32_t *pSamplingRate,
130 uint32_t *pFormat,
131 uint32_t *pChannels,
132 uint32_t acoustics);
133
Eric Laurentddb78e72009-07-28 08:44:33 -0700134 virtual status_t closeInput(int input);
Eric Laurenta553c252009-07-17 12:17:14 -0700135
Eric Laurentddb78e72009-07-28 08:44:33 -0700136 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurenta553c252009-07-17 12:17:14 -0700137
Eric Laurent415f3e22009-10-21 08:14:22 -0700138 virtual status_t setVoiceVolume(float volume);
139
Eric Laurent0986e792010-01-19 17:37:09 -0800140 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 enum hardware_call_state {
143 AUDIO_HW_IDLE = 0,
144 AUDIO_HW_INIT,
145 AUDIO_HW_OUTPUT_OPEN,
146 AUDIO_HW_OUTPUT_CLOSE,
147 AUDIO_HW_INPUT_OPEN,
148 AUDIO_HW_INPUT_CLOSE,
149 AUDIO_HW_STANDBY,
150 AUDIO_HW_SET_MASTER_VOLUME,
151 AUDIO_HW_GET_ROUTING,
152 AUDIO_HW_SET_ROUTING,
153 AUDIO_HW_GET_MODE,
154 AUDIO_HW_SET_MODE,
155 AUDIO_HW_GET_MIC_MUTE,
156 AUDIO_HW_SET_MIC_MUTE,
157 AUDIO_SET_VOICE_VOLUME,
158 AUDIO_SET_PARAMETER,
159 };
160
161 // record interface
162 virtual sp<IAudioRecord> openRecord(
163 pid_t pid,
Eric Laurentddb78e72009-07-28 08:44:33 -0700164 int input,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 uint32_t sampleRate,
166 int format,
167 int channelCount,
168 int frameCount,
169 uint32_t flags,
170 status_t *status);
171
172 virtual status_t onTransact(
173 uint32_t code,
174 const Parcel& data,
175 Parcel* reply,
176 uint32_t flags);
177
178private:
179 AudioFlinger();
180 virtual ~AudioFlinger();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
183 // Internal dump utilites.
184 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
185 status_t dumpClients(int fd, const Vector<String16>& args);
186 status_t dumpInternals(int fd, const Vector<String16>& args);
187
188 // --- Client ---
189 class Client : public RefBase {
190 public:
191 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
192 virtual ~Client();
193 const sp<MemoryDealer>& heap() const;
194 pid_t pid() const { return mPid; }
Eric Laurentb9481d82009-09-17 05:12:56 -0700195 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 private:
198 Client(const Client&);
199 Client& operator = (const Client&);
200 sp<AudioFlinger> mAudioFlinger;
201 sp<MemoryDealer> mMemoryDealer;
202 pid_t mPid;
203 };
204
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700205 // --- Notification Client ---
206 class NotificationClient : public IBinder::DeathRecipient {
207 public:
208 NotificationClient(const sp<AudioFlinger>& audioFlinger,
209 const sp<IAudioFlingerClient>& client,
210 pid_t pid);
211 virtual ~NotificationClient();
212
213 sp<IAudioFlingerClient> client() { return mClient; }
214
215 // IBinder::DeathRecipient
216 virtual void binderDied(const wp<IBinder>& who);
217
218 private:
219 NotificationClient(const NotificationClient&);
220 NotificationClient& operator = (const NotificationClient&);
221
222 sp<AudioFlinger> mAudioFlinger;
223 pid_t mPid;
224 sp<IAudioFlingerClient> mClient;
225 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
227 class TrackHandle;
228 class RecordHandle;
Eric Laurenta553c252009-07-17 12:17:14 -0700229 class RecordThread;
230 class PlaybackThread;
231 class MixerThread;
232 class DirectOutputThread;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800233 class DuplicatingThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700234 class Track;
235 class RecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Eric Laurenta553c252009-07-17 12:17:14 -0700237 class ThreadBase : public Thread {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800239 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700240 virtual ~ThreadBase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Eric Laurent3fdb1262009-11-07 00:01:32 -0800242 status_t dumpBase(int fd, const Vector<String16>& args);
243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 // base for record and playback
245 class TrackBase : public AudioBufferProvider, public RefBase {
246
247 public:
248 enum track_state {
249 IDLE,
250 TERMINATED,
251 STOPPED,
252 RESUMING,
253 ACTIVE,
254 PAUSING,
255 PAUSED
256 };
257
258 enum track_flags {
259 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
260 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
261 // The upper 16 bits are used for track-specific flags.
262 };
263
Eric Laurenta553c252009-07-17 12:17:14 -0700264 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 uint32_t sampleRate,
267 int format,
268 int channelCount,
269 int frameCount,
270 uint32_t flags,
271 const sp<IMemory>& sharedBuffer);
272 ~TrackBase();
273
274 virtual status_t start() = 0;
275 virtual void stop() = 0;
276 sp<IMemory> getCblk() const;
Eric Laurent6c30a712009-08-10 23:22:32 -0700277 audio_track_cblk_t* cblk() const { return mCblk; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
279 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700280 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 friend class RecordHandle;
Eric Laurent2c817f52009-07-23 13:17:39 -0700282 friend class PlaybackThread;
283 friend class RecordThread;
284 friend class MixerThread;
285 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
287 TrackBase(const TrackBase&);
288 TrackBase& operator = (const TrackBase&);
289
290 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
291 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 int format() const {
294 return mFormat;
295 }
296
297 int channelCount() const ;
298
299 int sampleRate() const;
300
301 void* getBuffer(uint32_t offset, uint32_t frames) const;
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 bool isStopped() const {
304 return mState == STOPPED;
305 }
306
307 bool isTerminated() const {
308 return mState == TERMINATED;
309 }
310
311 bool step();
312 void reset();
313
Eric Laurenta553c252009-07-17 12:17:14 -0700314 wp<ThreadBase> mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 sp<Client> mClient;
316 sp<IMemory> mCblkMemory;
317 audio_track_cblk_t* mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 void* mBuffer;
319 void* mBufferEnd;
320 uint32_t mFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 // we don't really need a lock for these
322 int mState;
323 int mClientTid;
324 uint8_t mFormat;
325 uint32_t mFlags;
326 };
327
Eric Laurenta553c252009-07-17 12:17:14 -0700328 class ConfigEvent {
329 public:
330 ConfigEvent() : mEvent(0), mParam(0) {}
331
332 int mEvent;
333 int mParam;
334 };
335
336 uint32_t sampleRate() const;
337 int channelCount() const;
338 int format() const;
339 size_t frameCount() const;
340 void wakeUp() { mWaitWorkCV.broadcast(); }
341 void exit();
342 virtual bool checkForNewParameters_l() = 0;
343 virtual status_t setParameters(const String8& keyValuePairs);
344 virtual String8 getParameters(const String8& keys) = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700345 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700346 void sendConfigEvent(int event, int param = 0);
Eric Laurent8fce46a2009-08-04 09:45:33 -0700347 void sendConfigEvent_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700348 void processConfigEvents();
Eric Laurent49f02be2009-11-19 09:00:56 -0800349 int id() const { return mId;}
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800350 bool standby() { return mStandby; }
Eric Laurenta553c252009-07-17 12:17:14 -0700351
Eric Laurent2c817f52009-07-23 13:17:39 -0700352 mutable Mutex mLock;
353
Eric Laurenta553c252009-07-17 12:17:14 -0700354 protected:
355
356 friend class Track;
357 friend class TrackBase;
358 friend class PlaybackThread;
359 friend class MixerThread;
360 friend class DirectOutputThread;
361 friend class DuplicatingThread;
362 friend class RecordThread;
363 friend class RecordTrack;
364
Eric Laurenta553c252009-07-17 12:17:14 -0700365 Condition mWaitWorkCV;
366 sp<AudioFlinger> mAudioFlinger;
367 uint32_t mSampleRate;
368 size_t mFrameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -0700369 uint32_t mChannels;
370 uint16_t mChannelCount;
371 uint16_t mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700372 int mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -0700373 Condition mParamCond;
Eric Laurent8fce46a2009-08-04 09:45:33 -0700374 Vector<String8> mNewParameters;
Eric Laurenta553c252009-07-17 12:17:14 -0700375 status_t mParamStatus;
376 Vector<ConfigEvent *> mConfigEvents;
377 bool mStandby;
Eric Laurent49f02be2009-11-19 09:00:56 -0800378 int mId;
379 bool mExiting;
Eric Laurenta553c252009-07-17 12:17:14 -0700380 };
381
382 // --- PlaybackThread ---
383 class PlaybackThread : public ThreadBase {
384 public:
385
386 enum type {
387 MIXER,
388 DIRECT,
389 DUPLICATING
390 };
391
Eric Laurent059b4be2009-11-09 23:32:22 -0800392 enum mixer_state {
393 MIXER_IDLE,
394 MIXER_TRACKS_ENABLED,
395 MIXER_TRACKS_READY
396 };
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 // playback track
399 class Track : public TrackBase {
400 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700401 Track( const wp<ThreadBase>& thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 const sp<Client>& client,
403 int streamType,
404 uint32_t sampleRate,
405 int format,
406 int channelCount,
407 int frameCount,
408 const sp<IMemory>& sharedBuffer);
409 ~Track();
410
411 void dump(char* buffer, size_t size);
412 virtual status_t start();
413 virtual void stop();
414 void pause();
415
416 void flush();
417 void destroy();
418 void mute(bool);
419 void setVolume(float left, float right);
Eric Laurenta553c252009-07-17 12:17:14 -0700420 int name() const {
421 return mName;
422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Eric Laurent4bc035a2009-05-22 09:18:15 -0700424 int type() const {
425 return mStreamType;
426 }
427
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700430 friend class ThreadBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700432 friend class TrackHandle;
433 friend class PlaybackThread;
434 friend class MixerThread;
435 friend class DirectOutputThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436
437 Track(const Track&);
438 Track& operator = (const Track&);
439
440 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurenta553c252009-07-17 12:17:14 -0700441 bool isMuted() { return mMute; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 bool isPausing() const {
443 return mState == PAUSING;
444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 bool isPaused() const {
446 return mState == PAUSED;
447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 bool isReady() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 void setPaused() { mState = PAUSED; }
450 void reset();
451
Eric Laurent49f02be2009-11-19 09:00:56 -0800452 bool isOutputTrack() const {
453 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 // we don't really need a lock for these
457 float mVolume[2];
458 volatile bool mMute;
459 // FILLED state is used for suppressing volume ramp at begin of playing
460 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
461 mutable uint8_t mFillingUpStatus;
462 int8_t mRetryCount;
463 sp<IMemory> mSharedBuffer;
464 bool mResetDone;
Eric Laurent4bc035a2009-05-22 09:18:15 -0700465 int mStreamType;
Eric Laurenta553c252009-07-17 12:17:14 -0700466 int mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }; // end of Track
468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
470 // playback track
471 class OutputTrack : public Track {
472 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 class Buffer: public AudioBufferProvider::Buffer {
475 public:
476 int16_t *mBuffer;
477 };
Eric Laurenta553c252009-07-17 12:17:14 -0700478
479 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800480 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 uint32_t sampleRate,
482 int format,
483 int channelCount,
484 int frameCount);
485 ~OutputTrack();
486
487 virtual status_t start();
488 virtual void stop();
Eric Laurenta553c252009-07-17 12:17:14 -0700489 bool write(int16_t* data, uint32_t frames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurenta553c252009-07-17 12:17:14 -0700491 bool isActive() { return mActive; }
492 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
494 private:
495
Eric Laurenta553c252009-07-17 12:17:14 -0700496 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 void clearBufferQueue();
Eric Laurenta553c252009-07-17 12:17:14 -0700498
499 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800500 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurenta553c252009-07-17 12:17:14 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 Vector < Buffer* > mBufferQueue;
503 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -0700504 bool mActive;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800505 DuplicatingThread* mSourceThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700506 }; // end of OutputTrack
507
Eric Laurent49f02be2009-11-19 09:00:56 -0800508 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700509 virtual ~PlaybackThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
511 virtual status_t dump(int fd, const Vector<String16>& args);
512
513 // Thread virtuals
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 virtual status_t readyToRun();
515 virtual void onFirstRef();
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 virtual uint32_t latency() const;
518
519 virtual status_t setMasterVolume(float value);
520 virtual status_t setMasterMute(bool muted);
521
522 virtual float masterVolume() const;
523 virtual bool masterMute() const;
524
525 virtual status_t setStreamVolume(int stream, float value);
526 virtual status_t setStreamMute(int stream, bool muted);
527
528 virtual float streamVolume(int stream) const;
529 virtual bool streamMute(int stream) const;
530
Eric Laurent23f25cd2010-01-25 08:49:09 -0800531 bool isStreamActive(int stream) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700532
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700533 sp<Track> createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 const sp<AudioFlinger::Client>& client,
535 int streamType,
536 uint32_t sampleRate,
537 int format,
538 int channelCount,
539 int frameCount,
540 const sp<IMemory>& sharedBuffer,
541 status_t *status);
Eric Laurenta553c252009-07-17 12:17:14 -0700542
543 AudioStreamOut* getOutput() { return mOutput; }
544
545 virtual int type() const { return mType; }
Eric Laurentd5603c12009-08-06 08:49:39 -0700546 void suspend() { mSuspended++; }
547 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800548 bool isSuspended() { return (mSuspended != 0); }
Eric Laurenta553c252009-07-17 12:17:14 -0700549 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700550 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurent0986e792010-01-19 17:37:09 -0800551 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurenta553c252009-07-17 12:17:14 -0700552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 struct stream_type_t {
554 stream_type_t()
555 : volume(1.0f),
556 mute(false)
557 {
558 }
559 float volume;
560 bool mute;
561 };
562
Eric Laurent2c817f52009-07-23 13:17:39 -0700563 protected:
564 int mType;
565 int16_t* mMixBuffer;
Eric Laurentd5603c12009-08-06 08:49:39 -0700566 int mSuspended;
Eric Laurent2c817f52009-07-23 13:17:39 -0700567 int mBytesWritten;
568 bool mMasterMute;
569 SortedVector< wp<Track> > mActiveTracks;
570
Eric Laurent62443f52009-10-05 20:29:18 -0700571 virtual int getTrackName_l() = 0;
572 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800573 virtual uint32_t activeSleepTimeUs() = 0;
574 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurent62443f52009-10-05 20:29:18 -0700575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 private:
577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 friend class AudioFlinger;
Eric Laurent6c30a712009-08-10 23:22:32 -0700579 friend class OutputTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 friend class Track;
581 friend class TrackBase;
Eric Laurenta553c252009-07-17 12:17:14 -0700582 friend class MixerThread;
583 friend class DirectOutputThread;
584 friend class DuplicatingThread;
585
586 PlaybackThread(const Client&);
587 PlaybackThread& operator = (const PlaybackThread&);
588
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700589 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700590 void destroyTrack_l(const sp<Track>& track);
Eric Laurent62443f52009-10-05 20:29:18 -0700591
Eric Laurenta553c252009-07-17 12:17:14 -0700592 void readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593
Eric Laurenta553c252009-07-17 12:17:14 -0700594 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurenta553c252009-07-17 12:17:14 -0700596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 SortedVector< sp<Track> > mTracks;
Eric Laurenta553c252009-07-17 12:17:14 -0700598 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
599 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 AudioStreamOut* mOutput;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 float mMasterVolume;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 nsecs_t mLastWriteTime;
603 int mNumWrites;
604 int mNumDelayedWrites;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 bool mInWrite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 };
607
Eric Laurenta553c252009-07-17 12:17:14 -0700608 class MixerThread : public PlaybackThread {
609 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800610 MixerThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700611 virtual ~MixerThread();
612
613 // Thread virtuals
614 virtual bool threadLoop();
615
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700616 void invalidateTracks(int streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700617 virtual bool checkForNewParameters_l();
618 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
619
620 protected:
Eric Laurent059b4be2009-11-09 23:32:22 -0800621 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove);
Eric Laurent62443f52009-10-05 20:29:18 -0700622 virtual int getTrackName_l();
623 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800624 virtual uint32_t activeSleepTimeUs();
625 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700626
627 AudioMixer* mAudioMixer;
628 };
629
630 class DirectOutputThread : public PlaybackThread {
631 public:
632
Eric Laurent49f02be2009-11-19 09:00:56 -0800633 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700634 ~DirectOutputThread();
635
636 // Thread virtuals
637 virtual bool threadLoop();
638
Eric Laurent62443f52009-10-05 20:29:18 -0700639 virtual bool checkForNewParameters_l();
640
641 protected:
Eric Laurenta553c252009-07-17 12:17:14 -0700642 virtual int getTrackName_l();
643 virtual void deleteTrackName_l(int name);
Eric Laurent059b4be2009-11-09 23:32:22 -0800644 virtual uint32_t activeSleepTimeUs();
645 virtual uint32_t idleSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700646
647 private:
648 float mLeftVolume;
649 float mRightVolume;
650 };
651
652 class DuplicatingThread : public MixerThread {
653 public:
Eric Laurent49f02be2009-11-19 09:00:56 -0800654 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700655 ~DuplicatingThread();
656
657 // Thread virtuals
658 virtual bool threadLoop();
659 void addOutputTrack(MixerThread* thread);
660 void removeOutputTrack(MixerThread* thread);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800661 uint32_t waitTimeMs() { return mWaitTimeMs; }
662 protected:
663 virtual uint32_t activeSleepTimeUs();
Eric Laurenta553c252009-07-17 12:17:14 -0700664
665 private:
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800666 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
667 void updateWaitTime();
668
Eric Laurenta553c252009-07-17 12:17:14 -0700669 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent8ac9f8d2009-12-18 05:47:48 -0800670 uint32_t mWaitTimeMs;
Eric Laurenta553c252009-07-17 12:17:14 -0700671 };
672
Eric Laurentddb78e72009-07-28 08:44:33 -0700673 PlaybackThread *checkPlaybackThread_l(int output) const;
674 MixerThread *checkMixerThread_l(int output) const;
675 RecordThread *checkRecordThread_l(int input) const;
Eric Laurenta553c252009-07-17 12:17:14 -0700676 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent49f02be2009-11-19 09:00:56 -0800677 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurenta553c252009-07-17 12:17:14 -0700678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 friend class AudioBuffer;
680
681 class TrackHandle : public android::BnAudioTrack {
682 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700683 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 virtual ~TrackHandle();
685 virtual status_t start();
686 virtual void stop();
687 virtual void flush();
688 virtual void mute(bool);
689 virtual void pause();
690 virtual void setVolume(float left, float right);
691 virtual sp<IMemory> getCblk() const;
692 virtual status_t onTransact(
693 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
694 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700695 sp<PlaybackThread::Track> mTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 };
697
698 friend class Client;
Eric Laurenta553c252009-07-17 12:17:14 -0700699 friend class PlaybackThread::Track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
701
Eric Laurentb9481d82009-09-17 05:12:56 -0700702 void removeClient_l(pid_t pid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700703 void removeNotificationClient(pid_t pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
705
Eric Laurenta553c252009-07-17 12:17:14 -0700706 // record thread
707 class RecordThread : public ThreadBase, public AudioBufferProvider
708 {
709 public:
710
711 // record track
712 class RecordTrack : public TrackBase {
713 public:
714 RecordTrack(const wp<ThreadBase>& thread,
715 const sp<Client>& client,
716 uint32_t sampleRate,
717 int format,
718 int channelCount,
719 int frameCount,
720 uint32_t flags);
721 ~RecordTrack();
722
723 virtual status_t start();
724 virtual void stop();
725
726 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
727 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
728
Eric Laurent3fdb1262009-11-07 00:01:32 -0800729 void dump(char* buffer, size_t size);
Eric Laurenta553c252009-07-17 12:17:14 -0700730 private:
731 friend class AudioFlinger;
Eric Laurent2c817f52009-07-23 13:17:39 -0700732 friend class RecordThread;
Eric Laurenta553c252009-07-17 12:17:14 -0700733
734 RecordTrack(const RecordTrack&);
735 RecordTrack& operator = (const RecordTrack&);
736
737 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
738
739 bool mOverflow;
740 };
741
742
743 RecordThread(const sp<AudioFlinger>& audioFlinger,
744 AudioStreamIn *input,
745 uint32_t sampleRate,
Eric Laurent49f02be2009-11-19 09:00:56 -0800746 uint32_t channels,
747 int id);
Eric Laurenta553c252009-07-17 12:17:14 -0700748 ~RecordThread();
749
750 virtual bool threadLoop();
751 virtual status_t readyToRun() { return NO_ERROR; }
752 virtual void onFirstRef();
753
754 status_t start(RecordTrack* recordTrack);
755 void stop(RecordTrack* recordTrack);
756 status_t dump(int fd, const Vector<String16>& args);
757 AudioStreamIn* getInput() { return mInput; }
758
759 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
760 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
761 virtual bool checkForNewParameters_l();
762 virtual String8 getParameters(const String8& keys);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700763 virtual void audioConfigChanged_l(int event, int param = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700764 void readInputParameters();
Eric Laurent47d0a922010-02-26 02:47:27 -0800765 virtual unsigned int getInputFramesLost();
Eric Laurenta553c252009-07-17 12:17:14 -0700766
767 private:
768 RecordThread();
769 AudioStreamIn *mInput;
770 sp<RecordTrack> mActiveTrack;
771 Condition mStartStopCond;
772 AudioResampler *mResampler;
773 int32_t *mRsmpOutBuffer;
774 int16_t *mRsmpInBuffer;
775 size_t mRsmpInIndex;
776 size_t mInputBytes;
777 int mReqChannelCount;
778 uint32_t mReqSampleRate;
Eric Laurent9cc489a22009-12-05 05:20:01 -0800779 ssize_t mBytesRead;
Eric Laurenta553c252009-07-17 12:17:14 -0700780 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781
782 class RecordHandle : public android::BnAudioRecord {
783 public:
Eric Laurenta553c252009-07-17 12:17:14 -0700784 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 virtual ~RecordHandle();
786 virtual status_t start();
787 virtual void stop();
788 virtual sp<IMemory> getCblk() const;
789 virtual status_t onTransact(
790 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
791 private:
Eric Laurenta553c252009-07-17 12:17:14 -0700792 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 };
794
Eric Laurenta553c252009-07-17 12:17:14 -0700795 friend class RecordThread;
796 friend class PlaybackThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700799 mutable Mutex mLock;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 DefaultKeyedVector< pid_t, wp<Client> > mClients;
802
Eric Laurenta553c252009-07-17 12:17:14 -0700803 mutable Mutex mHardwareLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 AudioHardwareInterface* mAudioHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 mutable int mHardwareStatus;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700806
Eric Laurenta553c252009-07-17 12:17:14 -0700807
Eric Laurentddb78e72009-07-28 08:44:33 -0700808 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurenta553c252009-07-17 12:17:14 -0700809 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
810 float mMasterVolume;
811 bool mMasterMute;
812
Eric Laurentddb78e72009-07-28 08:44:33 -0700813 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurenta553c252009-07-17 12:17:14 -0700814
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700815 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurentddb78e72009-07-28 08:44:33 -0700816 int mNextThreadId;
Eric Laurent4f0f17d2010-05-12 02:05:53 -0700817#ifdef LVMX
818 int mLifeVibesClientPid;
819#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820};
821
822// ----------------------------------------------------------------------------
823
824}; // namespace android
825
826#endif // ANDROID_AUDIO_FLINGER_H