blob: 739ec3331165bef3cc34cff1ca259e7a170e7fc6 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Laurent7e2aad12009-12-18 05:47:48 -080023#include <limits.h>
The Android Open Source Projectedbf3b62009-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 Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/MemoryDealer.h>
The Android Open Source Projectedbf3b62009-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 Laurent9d91ad52009-07-17 12:17:14 -070047class AudioResampler;
The Android Open Source Projectedbf3b62009-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 Laurent9d91ad52009-07-17 12:17:14 -070060class AudioFlinger : public BnAudioFlinger, public IBinder::DeathRecipient
The Android Open Source Projectedbf3b62009-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 Laurente0e9ecc2009-07-28 08:44:33 -070077 int output,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 status_t *status);
79
Eric Laurente0e9ecc2009-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 Projectedbf3b62009-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 Laurente0e9ecc2009-07-28 08:44:33 -070092 virtual status_t setStreamVolume(int stream, float value, int output);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 virtual status_t setStreamMute(int stream, bool muted);
94
Eric Laurente0e9ecc2009-07-28 08:44:33 -070095 virtual float streamVolume(int stream, int output) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 virtual bool streamMute(int stream) const;
97
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 virtual status_t setMode(int mode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099
100 virtual status_t setMicMute(bool state);
101 virtual bool getMicMute() const;
102
Eric Laurent43c0b0a2010-01-25 08:49:09 -0800103 virtual bool isStreamActive(int stream) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104
Eric Laurente0e9ecc2009-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 Projectedbf3b62009-03-03 19:31:44 -0800107
108 virtual void registerClient(const sp<IAudioFlingerClient>& client);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
Eric Laurent134ccbd2010-02-26 02:47:27 -0800111 virtual unsigned int getInputFramesLost(int ioHandle);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700112
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700113 virtual int openOutput(uint32_t *pDevices,
Eric Laurent9d91ad52009-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 Laurente0e9ecc2009-07-28 08:44:33 -0700120 virtual int openDuplicateOutput(int output1, int output2);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700121
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700122 virtual status_t closeOutput(int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700123
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700124 virtual status_t suspendOutput(int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700125
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700126 virtual status_t restoreOutput(int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700127
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700128 virtual int openInput(uint32_t *pDevices,
Eric Laurent9d91ad52009-07-17 12:17:14 -0700129 uint32_t *pSamplingRate,
130 uint32_t *pFormat,
131 uint32_t *pChannels,
132 uint32_t acoustics);
133
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700134 virtual status_t closeInput(int input);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700135
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700136 virtual status_t setStreamOutput(uint32_t stream, int output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700137
Eric Laurent63da2b62009-10-21 08:14:22 -0700138 virtual status_t setVoiceVolume(float volume);
139
Eric Laurente9ed2722010-01-19 17:37:09 -0800140 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output);
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 // IBinder::DeathRecipient
143 virtual void binderDied(const wp<IBinder>& who);
144
145 enum hardware_call_state {
146 AUDIO_HW_IDLE = 0,
147 AUDIO_HW_INIT,
148 AUDIO_HW_OUTPUT_OPEN,
149 AUDIO_HW_OUTPUT_CLOSE,
150 AUDIO_HW_INPUT_OPEN,
151 AUDIO_HW_INPUT_CLOSE,
152 AUDIO_HW_STANDBY,
153 AUDIO_HW_SET_MASTER_VOLUME,
154 AUDIO_HW_GET_ROUTING,
155 AUDIO_HW_SET_ROUTING,
156 AUDIO_HW_GET_MODE,
157 AUDIO_HW_SET_MODE,
158 AUDIO_HW_GET_MIC_MUTE,
159 AUDIO_HW_SET_MIC_MUTE,
160 AUDIO_SET_VOICE_VOLUME,
161 AUDIO_SET_PARAMETER,
162 };
163
164 // record interface
165 virtual sp<IAudioRecord> openRecord(
166 pid_t pid,
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700167 int input,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 uint32_t sampleRate,
169 int format,
170 int channelCount,
171 int frameCount,
172 uint32_t flags,
173 status_t *status);
174
175 virtual status_t onTransact(
176 uint32_t code,
177 const Parcel& data,
178 Parcel* reply,
179 uint32_t flags);
180
181private:
182 AudioFlinger();
183 virtual ~AudioFlinger();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
186 // Internal dump utilites.
187 status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
188 status_t dumpClients(int fd, const Vector<String16>& args);
189 status_t dumpInternals(int fd, const Vector<String16>& args);
190
191 // --- Client ---
192 class Client : public RefBase {
193 public:
194 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
195 virtual ~Client();
196 const sp<MemoryDealer>& heap() const;
197 pid_t pid() const { return mPid; }
Eric Laurent0f8ab672009-09-17 05:12:56 -0700198 sp<AudioFlinger> audioFlinger() { return mAudioFlinger; }
199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 private:
201 Client(const Client&);
202 Client& operator = (const Client&);
203 sp<AudioFlinger> mAudioFlinger;
204 sp<MemoryDealer> mMemoryDealer;
205 pid_t mPid;
206 };
207
208
209 class TrackHandle;
210 class RecordHandle;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700211 class RecordThread;
212 class PlaybackThread;
213 class MixerThread;
214 class DirectOutputThread;
Eric Laurent7e2aad12009-12-18 05:47:48 -0800215 class DuplicatingThread;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700216 class Track;
217 class RecordTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218
Eric Laurent9d91ad52009-07-17 12:17:14 -0700219 class ThreadBase : public Thread {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 public:
Eric Laurent09b4ba82009-11-19 09:00:56 -0800221 ThreadBase (const sp<AudioFlinger>& audioFlinger, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700222 virtual ~ThreadBase();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223
Eric Laurentee47d432009-11-07 00:01:32 -0800224 status_t dumpBase(int fd, const Vector<String16>& args);
225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 // base for record and playback
227 class TrackBase : public AudioBufferProvider, public RefBase {
228
229 public:
230 enum track_state {
231 IDLE,
232 TERMINATED,
233 STOPPED,
234 RESUMING,
235 ACTIVE,
236 PAUSING,
237 PAUSED
238 };
239
240 enum track_flags {
241 STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex
242 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
243 // The upper 16 bits are used for track-specific flags.
244 };
245
Eric Laurent9d91ad52009-07-17 12:17:14 -0700246 TrackBase(const wp<ThreadBase>& thread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 uint32_t sampleRate,
249 int format,
250 int channelCount,
251 int frameCount,
252 uint32_t flags,
253 const sp<IMemory>& sharedBuffer);
254 ~TrackBase();
255
256 virtual status_t start() = 0;
257 virtual void stop() = 0;
258 sp<IMemory> getCblk() const;
Eric Laurentf5aba822009-08-10 23:22:32 -0700259 audio_track_cblk_t* cblk() const { return mCblk; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260
261 protected:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700262 friend class ThreadBase;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 friend class RecordHandle;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700264 friend class PlaybackThread;
265 friend class RecordThread;
266 friend class MixerThread;
267 friend class DirectOutputThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268
269 TrackBase(const TrackBase&);
270 TrackBase& operator = (const TrackBase&);
271
272 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
273 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 int format() const {
276 return mFormat;
277 }
278
279 int channelCount() const ;
280
281 int sampleRate() const;
282
283 void* getBuffer(uint32_t offset, uint32_t frames) const;
284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285 bool isStopped() const {
286 return mState == STOPPED;
287 }
288
289 bool isTerminated() const {
290 return mState == TERMINATED;
291 }
292
293 bool step();
294 void reset();
295
Eric Laurent9d91ad52009-07-17 12:17:14 -0700296 wp<ThreadBase> mThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 sp<Client> mClient;
298 sp<IMemory> mCblkMemory;
299 audio_track_cblk_t* mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 void* mBuffer;
301 void* mBufferEnd;
302 uint32_t mFrameCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 // we don't really need a lock for these
304 int mState;
305 int mClientTid;
306 uint8_t mFormat;
307 uint32_t mFlags;
308 };
309
Eric Laurent9d91ad52009-07-17 12:17:14 -0700310 class ConfigEvent {
311 public:
312 ConfigEvent() : mEvent(0), mParam(0) {}
313
314 int mEvent;
315 int mParam;
316 };
317
318 uint32_t sampleRate() const;
319 int channelCount() const;
320 int format() const;
321 size_t frameCount() const;
322 void wakeUp() { mWaitWorkCV.broadcast(); }
323 void exit();
324 virtual bool checkForNewParameters_l() = 0;
325 virtual status_t setParameters(const String8& keyValuePairs);
326 virtual String8 getParameters(const String8& keys) = 0;
327 virtual void audioConfigChanged(int event, int param = 0) = 0;
328 void sendConfigEvent(int event, int param = 0);
Eric Laurent3464c012009-08-04 09:45:33 -0700329 void sendConfigEvent_l(int event, int param = 0);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700330 void processConfigEvents();
Eric Laurent09b4ba82009-11-19 09:00:56 -0800331 int id() const { return mId;}
Eric Laurent7e2aad12009-12-18 05:47:48 -0800332 bool standby() { return mStandby; }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700333
Eric Laurent9395d9b2009-07-23 13:17:39 -0700334 mutable Mutex mLock;
335
Eric Laurent9d91ad52009-07-17 12:17:14 -0700336 protected:
337
338 friend class Track;
339 friend class TrackBase;
340 friend class PlaybackThread;
341 friend class MixerThread;
342 friend class DirectOutputThread;
343 friend class DuplicatingThread;
344 friend class RecordThread;
345 friend class RecordTrack;
346
Eric Laurent9d91ad52009-07-17 12:17:14 -0700347 Condition mWaitWorkCV;
348 sp<AudioFlinger> mAudioFlinger;
349 uint32_t mSampleRate;
350 size_t mFrameCount;
351 int mChannelCount;
352 int mFormat;
353 uint32_t mFrameSize;
354 Condition mParamCond;
Eric Laurent3464c012009-08-04 09:45:33 -0700355 Vector<String8> mNewParameters;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700356 status_t mParamStatus;
357 Vector<ConfigEvent *> mConfigEvents;
358 bool mStandby;
Eric Laurent09b4ba82009-11-19 09:00:56 -0800359 int mId;
360 bool mExiting;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700361 };
362
363 // --- PlaybackThread ---
364 class PlaybackThread : public ThreadBase {
365 public:
366
367 enum type {
368 MIXER,
369 DIRECT,
370 DUPLICATING
371 };
372
Eric Laurent0e49d352009-11-09 23:32:22 -0800373 enum mixer_state {
374 MIXER_IDLE,
375 MIXER_TRACKS_ENABLED,
376 MIXER_TRACKS_READY
377 };
378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 // playback track
380 class Track : public TrackBase {
381 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700382 Track( const wp<ThreadBase>& thread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 const sp<Client>& client,
384 int streamType,
385 uint32_t sampleRate,
386 int format,
387 int channelCount,
388 int frameCount,
389 const sp<IMemory>& sharedBuffer);
390 ~Track();
391
392 void dump(char* buffer, size_t size);
393 virtual status_t start();
394 virtual void stop();
395 void pause();
396
397 void flush();
398 void destroy();
399 void mute(bool);
400 void setVolume(float left, float right);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700401 int name() const {
402 return mName;
403 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404
Eric Laurent570dd0b2009-05-22 09:18:15 -0700405 int type() const {
406 return mStreamType;
407 }
408
409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 protected:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700411 friend class ThreadBase;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 friend class AudioFlinger;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700413 friend class TrackHandle;
414 friend class PlaybackThread;
415 friend class MixerThread;
416 friend class DirectOutputThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417
418 Track(const Track&);
419 Track& operator = (const Track&);
420
421 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700422 bool isMuted() { return mMute; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 bool isPausing() const {
424 return mState == PAUSING;
425 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 bool isPaused() const {
427 return mState == PAUSED;
428 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 bool isReady() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 void setPaused() { mState = PAUSED; }
431 void reset();
432
Eric Laurent09b4ba82009-11-19 09:00:56 -0800433 bool isOutputTrack() const {
434 return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
435 }
436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 // we don't really need a lock for these
438 float mVolume[2];
439 volatile bool mMute;
440 // FILLED state is used for suppressing volume ramp at begin of playing
441 enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
442 mutable uint8_t mFillingUpStatus;
443 int8_t mRetryCount;
444 sp<IMemory> mSharedBuffer;
445 bool mResetDone;
Eric Laurent570dd0b2009-05-22 09:18:15 -0700446 int mStreamType;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700447 int mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448 }; // end of Track
449
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450
451 // playback track
452 class OutputTrack : public Track {
453 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 class Buffer: public AudioBufferProvider::Buffer {
456 public:
457 int16_t *mBuffer;
458 };
Eric Laurent9d91ad52009-07-17 12:17:14 -0700459
460 OutputTrack( const wp<ThreadBase>& thread,
Eric Laurent7e2aad12009-12-18 05:47:48 -0800461 DuplicatingThread *sourceThread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 uint32_t sampleRate,
463 int format,
464 int channelCount,
465 int frameCount);
466 ~OutputTrack();
467
468 virtual status_t start();
469 virtual void stop();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700470 bool write(int16_t* data, uint32_t frames);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700472 bool isActive() { return mActive; }
473 wp<ThreadBase>& thread() { return mThread; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474
475 private:
476
Eric Laurent9d91ad52009-07-17 12:17:14 -0700477 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 void clearBufferQueue();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700479
480 // Maximum number of pending buffers allocated by OutputTrack::write()
Eric Laurent7e2aad12009-12-18 05:47:48 -0800481 static const uint8_t kMaxOverFlowBuffers = 10;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 Vector < Buffer* > mBufferQueue;
484 AudioBufferProvider::Buffer mOutBuffer;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700485 bool mActive;
Eric Laurent7e2aad12009-12-18 05:47:48 -0800486 DuplicatingThread* mSourceThread;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700487 }; // end of OutputTrack
488
Eric Laurent09b4ba82009-11-19 09:00:56 -0800489 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700490 virtual ~PlaybackThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491
492 virtual status_t dump(int fd, const Vector<String16>& args);
493
494 // Thread virtuals
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 virtual status_t readyToRun();
496 virtual void onFirstRef();
497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 virtual uint32_t latency() const;
499
500 virtual status_t setMasterVolume(float value);
501 virtual status_t setMasterMute(bool muted);
502
503 virtual float masterVolume() const;
504 virtual bool masterMute() const;
505
506 virtual status_t setStreamVolume(int stream, float value);
507 virtual status_t setStreamMute(int stream, bool muted);
508
509 virtual float streamVolume(int stream) const;
510 virtual bool streamMute(int stream) const;
511
Eric Laurent43c0b0a2010-01-25 08:49:09 -0800512 bool isStreamActive(int stream) const;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700513
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700514 sp<Track> createTrack_l(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 const sp<AudioFlinger::Client>& client,
516 int streamType,
517 uint32_t sampleRate,
518 int format,
519 int channelCount,
520 int frameCount,
521 const sp<IMemory>& sharedBuffer,
522 status_t *status);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700523
524 AudioStreamOut* getOutput() { return mOutput; }
525
526 virtual int type() const { return mType; }
Eric Laurentf9df2492009-08-06 08:49:39 -0700527 void suspend() { mSuspended++; }
528 void restore() { if (mSuspended) mSuspended--; }
Eric Laurent7e2aad12009-12-18 05:47:48 -0800529 bool isSuspended() { return (mSuspended != 0); }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700530 virtual String8 getParameters(const String8& keys);
531 virtual void audioConfigChanged(int event, int param = 0);
Eric Laurente9ed2722010-01-19 17:37:09 -0800532 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700533
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534 struct stream_type_t {
535 stream_type_t()
536 : volume(1.0f),
537 mute(false)
538 {
539 }
540 float volume;
541 bool mute;
542 };
543
Eric Laurent9395d9b2009-07-23 13:17:39 -0700544 protected:
545 int mType;
546 int16_t* mMixBuffer;
Eric Laurentf9df2492009-08-06 08:49:39 -0700547 int mSuspended;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700548 int mBytesWritten;
549 bool mMasterMute;
550 SortedVector< wp<Track> > mActiveTracks;
551
Eric Laurentf5e868b2009-10-05 20:29:18 -0700552 virtual int getTrackName_l() = 0;
553 virtual void deleteTrackName_l(int name) = 0;
Eric Laurent0e49d352009-11-09 23:32:22 -0800554 virtual uint32_t activeSleepTimeUs() = 0;
555 virtual uint32_t idleSleepTimeUs() = 0;
Eric Laurentf5e868b2009-10-05 20:29:18 -0700556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 private:
558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 friend class AudioFlinger;
Eric Laurentf5aba822009-08-10 23:22:32 -0700560 friend class OutputTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 friend class Track;
562 friend class TrackBase;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700563 friend class MixerThread;
564 friend class DirectOutputThread;
565 friend class DuplicatingThread;
566
567 PlaybackThread(const Client&);
568 PlaybackThread& operator = (const PlaybackThread&);
569
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700570 status_t addTrack_l(const sp<Track>& track);
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700571 void destroyTrack_l(const sp<Track>& track);
Eric Laurentf5e868b2009-10-05 20:29:18 -0700572
Eric Laurent9d91ad52009-07-17 12:17:14 -0700573 void readOutputParameters();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574
Eric Laurent9d91ad52009-07-17 12:17:14 -0700575 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 status_t dumpTracks(int fd, const Vector<String16>& args);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 SortedVector< sp<Track> > mTracks;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700579 // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
580 stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 AudioStreamOut* mOutput;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 float mMasterVolume;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 nsecs_t mLastWriteTime;
584 int mNumWrites;
585 int mNumDelayedWrites;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 bool mInWrite;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 };
588
Eric Laurent9d91ad52009-07-17 12:17:14 -0700589 class MixerThread : public PlaybackThread {
590 public:
Eric Laurent09b4ba82009-11-19 09:00:56 -0800591 MixerThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700592 virtual ~MixerThread();
593
594 // Thread virtuals
595 virtual bool threadLoop();
596
597 void getTracks(SortedVector < sp<Track> >& tracks,
598 SortedVector < wp<Track> >& activeTracks,
599 int streamType);
600 void putTracks(SortedVector < sp<Track> >& tracks,
601 SortedVector < wp<Track> >& activeTracks);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700602 virtual bool checkForNewParameters_l();
603 virtual status_t dumpInternals(int fd, const Vector<String16>& args);
604
605 protected:
Eric Laurent0e49d352009-11-09 23:32:22 -0800606 uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove);
Eric Laurentf5e868b2009-10-05 20:29:18 -0700607 virtual int getTrackName_l();
608 virtual void deleteTrackName_l(int name);
Eric Laurent0e49d352009-11-09 23:32:22 -0800609 virtual uint32_t activeSleepTimeUs();
610 virtual uint32_t idleSleepTimeUs();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700611
612 AudioMixer* mAudioMixer;
613 };
614
615 class DirectOutputThread : public PlaybackThread {
616 public:
617
Eric Laurent09b4ba82009-11-19 09:00:56 -0800618 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700619 ~DirectOutputThread();
620
621 // Thread virtuals
622 virtual bool threadLoop();
623
Eric Laurentf5e868b2009-10-05 20:29:18 -0700624 virtual bool checkForNewParameters_l();
625
626 protected:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700627 virtual int getTrackName_l();
628 virtual void deleteTrackName_l(int name);
Eric Laurent0e49d352009-11-09 23:32:22 -0800629 virtual uint32_t activeSleepTimeUs();
630 virtual uint32_t idleSleepTimeUs();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700631
632 private:
633 float mLeftVolume;
634 float mRightVolume;
635 };
636
637 class DuplicatingThread : public MixerThread {
638 public:
Eric Laurent09b4ba82009-11-19 09:00:56 -0800639 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700640 ~DuplicatingThread();
641
642 // Thread virtuals
643 virtual bool threadLoop();
644 void addOutputTrack(MixerThread* thread);
645 void removeOutputTrack(MixerThread* thread);
Eric Laurent7e2aad12009-12-18 05:47:48 -0800646 uint32_t waitTimeMs() { return mWaitTimeMs; }
647 protected:
648 virtual uint32_t activeSleepTimeUs();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700649
650 private:
Eric Laurent7e2aad12009-12-18 05:47:48 -0800651 bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks);
652 void updateWaitTime();
653
Eric Laurent9d91ad52009-07-17 12:17:14 -0700654 SortedVector < sp<OutputTrack> > mOutputTracks;
Eric Laurent7e2aad12009-12-18 05:47:48 -0800655 uint32_t mWaitTimeMs;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700656 };
657
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700658 PlaybackThread *checkPlaybackThread_l(int output) const;
659 MixerThread *checkMixerThread_l(int output) const;
660 RecordThread *checkRecordThread_l(int input) const;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700661 float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
Eric Laurent09b4ba82009-11-19 09:00:56 -0800662 void audioConfigChanged_l(int event, int ioHandle, void *param2);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 friend class AudioBuffer;
665
666 class TrackHandle : public android::BnAudioTrack {
667 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700668 TrackHandle(const sp<PlaybackThread::Track>& track);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 virtual ~TrackHandle();
670 virtual status_t start();
671 virtual void stop();
672 virtual void flush();
673 virtual void mute(bool);
674 virtual void pause();
675 virtual void setVolume(float left, float right);
676 virtual sp<IMemory> getCblk() const;
677 virtual status_t onTransact(
678 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
679 private:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700680 sp<PlaybackThread::Track> mTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 };
682
683 friend class Client;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700684 friend class PlaybackThread::Track;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685
686
Eric Laurent0f8ab672009-09-17 05:12:56 -0700687 void removeClient_l(pid_t pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688
689
Eric Laurent9d91ad52009-07-17 12:17:14 -0700690 // record thread
691 class RecordThread : public ThreadBase, public AudioBufferProvider
692 {
693 public:
694
695 // record track
696 class RecordTrack : public TrackBase {
697 public:
698 RecordTrack(const wp<ThreadBase>& thread,
699 const sp<Client>& client,
700 uint32_t sampleRate,
701 int format,
702 int channelCount,
703 int frameCount,
704 uint32_t flags);
705 ~RecordTrack();
706
707 virtual status_t start();
708 virtual void stop();
709
710 bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
711 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
712
Eric Laurentee47d432009-11-07 00:01:32 -0800713 void dump(char* buffer, size_t size);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700714 private:
715 friend class AudioFlinger;
Eric Laurent9395d9b2009-07-23 13:17:39 -0700716 friend class RecordThread;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700717
718 RecordTrack(const RecordTrack&);
719 RecordTrack& operator = (const RecordTrack&);
720
721 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
722
723 bool mOverflow;
724 };
725
726
727 RecordThread(const sp<AudioFlinger>& audioFlinger,
728 AudioStreamIn *input,
729 uint32_t sampleRate,
Eric Laurent09b4ba82009-11-19 09:00:56 -0800730 uint32_t channels,
731 int id);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700732 ~RecordThread();
733
734 virtual bool threadLoop();
735 virtual status_t readyToRun() { return NO_ERROR; }
736 virtual void onFirstRef();
737
738 status_t start(RecordTrack* recordTrack);
739 void stop(RecordTrack* recordTrack);
740 status_t dump(int fd, const Vector<String16>& args);
741 AudioStreamIn* getInput() { return mInput; }
742
743 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
744 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
745 virtual bool checkForNewParameters_l();
746 virtual String8 getParameters(const String8& keys);
747 virtual void audioConfigChanged(int event, int param = 0);
748 void readInputParameters();
Eric Laurent134ccbd2010-02-26 02:47:27 -0800749 virtual unsigned int getInputFramesLost();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700750
751 private:
752 RecordThread();
753 AudioStreamIn *mInput;
754 sp<RecordTrack> mActiveTrack;
755 Condition mStartStopCond;
756 AudioResampler *mResampler;
757 int32_t *mRsmpOutBuffer;
758 int16_t *mRsmpInBuffer;
759 size_t mRsmpInIndex;
760 size_t mInputBytes;
761 int mReqChannelCount;
762 uint32_t mReqSampleRate;
Eric Laurent52910952009-12-05 05:20:01 -0800763 ssize_t mBytesRead;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700764 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765
766 class RecordHandle : public android::BnAudioRecord {
767 public:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700768 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769 virtual ~RecordHandle();
770 virtual status_t start();
771 virtual void stop();
772 virtual sp<IMemory> getCblk() const;
773 virtual status_t onTransact(
774 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
775 private:
Eric Laurent9d91ad52009-07-17 12:17:14 -0700776 sp<RecordThread::RecordTrack> mRecordTrack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 };
778
Eric Laurent9d91ad52009-07-17 12:17:14 -0700779 friend class RecordThread;
780 friend class PlaybackThread;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700783 mutable Mutex mLock;
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 DefaultKeyedVector< pid_t, wp<Client> > mClients;
786
Eric Laurent9d91ad52009-07-17 12:17:14 -0700787 mutable Mutex mHardwareLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 AudioHardwareInterface* mAudioHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 mutable int mHardwareStatus;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700790
Eric Laurent9d91ad52009-07-17 12:17:14 -0700791
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700792 DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700793 PlaybackThread::stream_type_t mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
794 float mMasterVolume;
795 bool mMasterMute;
796
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700797 DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700798
799 SortedVector< sp<IBinder> > mNotificationClients;
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700800 int mNextThreadId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801};
802
803// ----------------------------------------------------------------------------
804
805}; // namespace android
806
807#endif // ANDROID_AUDIO_FLINGER_H