blob: d2daae79c72be16aed33783b82d055bb005d0b9d [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
vivek mehta2192bc92012-12-04 11:46:08 -08004** Copyright (c) 2012, The Linux Foundation. All rights reserved.
5** Not a Contribution, Apache license notifications and license are retained
6** for attribution purposes only.
Mathias Agopian65ab4712010-07-14 17:59:35 -07007**
8** Licensed under the Apache License, Version 2.0 (the "License");
9** you may not use this file except in compliance with the License.
10** You may obtain a copy of the License at
11**
12** http://www.apache.org/licenses/LICENSE-2.0
13**
14** Unless required by applicable law or agreed to in writing, software
15** distributed under the License is distributed on an "AS IS" BASIS,
16** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17** See the License for the specific language governing permissions and
18** limitations under the License.
19*/
20
21#ifndef ANDROID_AUDIO_FLINGER_H
22#define ANDROID_AUDIO_FLINGER_H
23
24#include <stdint.h>
25#include <sys/types.h>
26#include <limits.h>
27
John Grossman4ff14ba2012-02-08 16:37:41 -080028#include <common_time/cc_helper.h>
29
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <media/IAudioFlinger.h>
31#include <media/IAudioFlingerClient.h>
vivek mehta2192bc92012-12-04 11:46:08 -080032#ifdef QCOM_HARDWARE
33#include <media/IDirectTrack.h>
34#include <media/IDirectTrackClient.h>
35#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <media/IAudioTrack.h>
37#include <media/IAudioRecord.h>
Glenn Kasten335787f2012-01-20 17:00:00 -080038#include <media/AudioSystem.h>
John Grossman4ff14ba2012-02-08 16:37:41 -080039#include <media/AudioTrack.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
41#include <utils/Atomic.h>
42#include <utils/Errors.h>
43#include <utils/threads.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044#include <utils/SortedVector.h>
Dima Zavin799a70e2011-04-18 16:57:27 -070045#include <utils/TypeHelpers.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046#include <utils/Vector.h>
47
Mathias Agopian5462fc92010-07-14 18:41:18 -070048#include <binder/BinderService.h>
49#include <binder/MemoryDealer.h>
50
Dima Zavin64760242011-05-11 14:15:23 -070051#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070052#include <hardware/audio.h>
Eric Laurenta4c5a552012-03-29 10:12:40 -070053#include <hardware/audio_policy.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070055#include <media/AudioBufferProvider.h>
56#include <media/ExtendedAudioBufferProvider.h>
Glenn Kasten58912562012-04-03 10:45:00 -070057#include "FastMixer.h"
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070058#include <media/nbaio/NBAIO.h>
Glenn Kastenc15d6652012-05-30 14:52:57 -070059#include "AudioWatchdog.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
Eric Laurentfeb0db62011-07-22 09:04:31 -070061#include <powermanager/IPowerManager.h>
vivek mehta2192bc92012-12-04 11:46:08 -080062#include <utils/List.h>
Eric Laurentfeb0db62011-07-22 09:04:31 -070063
Mathias Agopian65ab4712010-07-14 17:59:35 -070064namespace android {
65
66class audio_track_cblk_t;
67class effect_param_cblk_t;
68class AudioMixer;
69class AudioBuffer;
70class AudioResampler;
Glenn Kasten58912562012-04-03 10:45:00 -070071class FastMixer;
Mathias Agopian65ab4712010-07-14 17:59:35 -070072
Mathias Agopian65ab4712010-07-14 17:59:35 -070073// ----------------------------------------------------------------------------
74
Glenn Kasten53d76db2012-03-08 12:32:47 -080075// AudioFlinger has a hard-coded upper limit of 2 channels for capture and playback.
76// There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect.
77// Adding full support for > 2 channel capture or playback would require more than simply changing
78// this #define. There is an independent hard-coded upper limit in AudioMixer;
79// removing that AudioMixer limit would be necessary but insufficient to support > 2 channels.
80// The macro FCC_2 highlights some (but not all) places where there is are 2-channel assumptions.
81// Search also for "2", "left", "right", "[0]", "[1]", ">> 16", "<< 16", etc.
82#define FCC_2 2 // FCC_2 = Fixed Channel Count 2
83
John Grossman4ff14ba2012-02-08 16:37:41 -080084static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3);
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Mathias Agopian5462fc92010-07-14 18:41:18 -070086class AudioFlinger :
87 public BinderService<AudioFlinger>,
88 public BnAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -070089{
Glenn Kasten19986612012-03-09 12:07:30 -080090 friend class BinderService<AudioFlinger>; // for AudioFlinger()
Mathias Agopian65ab4712010-07-14 17:59:35 -070091public:
Glenn Kasten54c3b662012-01-06 07:46:30 -080092 static const char* getServiceName() { return "media.audio_flinger"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
94 virtual status_t dump(int fd, const Vector<String16>& args);
95
Glenn Kasten2f732eb2012-01-26 09:48:03 -080096 // IAudioFlinger interface, in binder opcode order
Mathias Agopian65ab4712010-07-14 17:59:35 -070097 virtual sp<IAudioTrack> createTrack(
98 pid_t pid,
Glenn Kastenfff6d712012-01-12 16:38:12 -080099 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800101 audio_format_t format,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700102 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103 int frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800104 IAudioFlinger::track_flags_t flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800106 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800107 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108 int *sessionId,
109 status_t *status);
vivek mehta2192bc92012-12-04 11:46:08 -0800110#ifdef QCOM_HARDWARE
111 virtual sp<IDirectTrack> createDirectTrack(
112 pid_t pid,
113 uint32_t sampleRate,
114 audio_channel_mask_t channelMask,
115 audio_io_handle_t output,
116 int *sessionId,
117 IDirectTrackClient* client,
118 audio_stream_type_t streamType,
119 status_t *status);
120
121 virtual void deleteEffectSession();
122#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123
Glenn Kasten2f732eb2012-01-26 09:48:03 -0800124 virtual sp<IAudioRecord> openRecord(
125 pid_t pid,
126 audio_io_handle_t input,
127 uint32_t sampleRate,
128 audio_format_t format,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700129 audio_channel_mask_t channelMask,
Glenn Kasten2f732eb2012-01-26 09:48:03 -0800130 int frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800131 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -0700132 pid_t tid,
Glenn Kasten2f732eb2012-01-26 09:48:03 -0800133 int *sessionId,
134 status_t *status);
135
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800136 virtual uint32_t sampleRate(audio_io_handle_t output) const;
137 virtual int channelCount(audio_io_handle_t output) const;
138 virtual audio_format_t format(audio_io_handle_t output) const;
139 virtual size_t frameCount(audio_io_handle_t output) const;
140 virtual uint32_t latency(audio_io_handle_t output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141
142 virtual status_t setMasterVolume(float value);
143 virtual status_t setMasterMute(bool muted);
144
145 virtual float masterVolume() const;
146 virtual bool masterMute() const;
147
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800148 virtual status_t setStreamVolume(audio_stream_type_t stream, float value,
149 audio_io_handle_t output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800150 virtual status_t setStreamMute(audio_stream_type_t stream, bool muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800152 virtual float streamVolume(audio_stream_type_t stream,
153 audio_io_handle_t output) const;
Glenn Kastenfff6d712012-01-12 16:38:12 -0800154 virtual bool streamMute(audio_stream_type_t stream) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155
Glenn Kastenf78aee72012-01-04 11:00:47 -0800156 virtual status_t setMode(audio_mode_t mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157
158 virtual status_t setMicMute(bool state);
159 virtual bool getMicMute() const;
160
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800161 virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
162 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163
164 virtual void registerClient(const sp<IAudioFlingerClient>& client);
vivek mehta2192bc92012-12-04 11:46:08 -0800165#ifdef QCOM_HARDWARE
166 virtual status_t deregisterClient(const sp<IAudioFlingerClient>& client);
167#endif
Glenn Kastendd8104c2012-07-02 12:42:44 -0700168 virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
169 audio_channel_mask_t channelMask) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170
Eric Laurenta4c5a552012-03-29 10:12:40 -0700171 virtual audio_io_handle_t openOutput(audio_module_handle_t module,
172 audio_devices_t *pDevices,
173 uint32_t *pSamplingRate,
174 audio_format_t *pFormat,
175 audio_channel_mask_t *pChannelMask,
176 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700177 audio_output_flags_t flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800179 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
180 audio_io_handle_t output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800182 virtual status_t closeOutput(audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800184 virtual status_t suspendOutput(audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800186 virtual status_t restoreOutput(audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187
Eric Laurenta4c5a552012-03-29 10:12:40 -0700188 virtual audio_io_handle_t openInput(audio_module_handle_t module,
189 audio_devices_t *pDevices,
190 uint32_t *pSamplingRate,
191 audio_format_t *pFormat,
192 audio_channel_mask_t *pChannelMask);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800194 virtual status_t closeInput(audio_io_handle_t input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800196 virtual status_t setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197
198 virtual status_t setVoiceVolume(float volume);
199
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800200 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
201 audio_io_handle_t output) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202
Glenn Kasten2f732eb2012-01-26 09:48:03 -0800203 virtual unsigned int getInputFramesLost(audio_io_handle_t ioHandle) const;
204
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 virtual int newAudioSessionId();
206
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700207 virtual void acquireAudioSessionId(int audioSession);
208
209 virtual void releaseAudioSessionId(int audioSession);
210
Glenn Kastenf587ba52012-01-26 16:25:10 -0800211 virtual status_t queryNumberEffects(uint32_t *numEffects) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212
Glenn Kastenf587ba52012-01-26 16:25:10 -0800213 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214
Glenn Kasten5e92a782012-01-30 07:40:52 -0800215 virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -0800216 effect_descriptor_t *descriptor) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217
218 virtual sp<IEffect> createEffect(pid_t pid,
219 effect_descriptor_t *pDesc,
220 const sp<IEffectClient>& effectClient,
221 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800222 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 int sessionId,
224 status_t *status,
225 int *id,
226 int *enabled);
227
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800228 virtual status_t moveEffects(int sessionId, audio_io_handle_t srcOutput,
229 audio_io_handle_t dstOutput);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230
Eric Laurenta4c5a552012-03-29 10:12:40 -0700231 virtual audio_module_handle_t loadHwModule(const char *name);
232
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700233 virtual int32_t getPrimaryOutputSamplingRate();
234 virtual int32_t getPrimaryOutputFrameCount();
235
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 virtual status_t onTransact(
237 uint32_t code,
238 const Parcel& data,
239 Parcel* reply,
240 uint32_t flags);
241
vivek mehta2192bc92012-12-04 11:46:08 -0800242#ifdef QCOM_HARDWARE
243 void applyEffectsOn(void *token,
244 int16_t *buffer1,
245 int16_t *buffer2,
246 int size);
247#endif
248
Glenn Kasten2f732eb2012-01-26 09:48:03 -0800249 // end of IAudioFlinger interface
250
Eric Laurenta011e352012-03-29 15:51:43 -0700251 class SyncEvent;
252
253 typedef void (*sync_event_callback_t)(const wp<SyncEvent>& event) ;
254
255 class SyncEvent : public RefBase {
256 public:
257 SyncEvent(AudioSystem::sync_event_t type,
258 int triggerSession,
259 int listenerSession,
260 sync_event_callback_t callBack,
261 void *cookie)
262 : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
263 mCallback(callBack), mCookie(cookie)
264 {}
265
266 virtual ~SyncEvent() {}
267
268 void trigger() { Mutex::Autolock _l(mLock); if (mCallback) mCallback(this); }
Glenn Kasten106e8a42012-08-02 13:37:12 -0700269 bool isCancelled() const { Mutex::Autolock _l(mLock); return (mCallback == NULL); }
270 void cancel() { Mutex::Autolock _l(mLock); mCallback = NULL; }
Eric Laurenta011e352012-03-29 15:51:43 -0700271 AudioSystem::sync_event_t type() const { return mType; }
272 int triggerSession() const { return mTriggerSession; }
273 int listenerSession() const { return mListenerSession; }
274 void *cookie() const { return mCookie; }
275
276 private:
277 const AudioSystem::sync_event_t mType;
278 const int mTriggerSession;
279 const int mListenerSession;
280 sync_event_callback_t mCallback;
281 void * const mCookie;
Glenn Kasten106e8a42012-08-02 13:37:12 -0700282 mutable Mutex mLock;
Eric Laurenta011e352012-03-29 15:51:43 -0700283 };
284
285 sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
286 int triggerSession,
287 int listenerSession,
288 sync_event_callback_t callBack,
289 void *cookie);
Eric Laurent717e1282012-06-29 16:36:52 -0700290
Glenn Kasten2f732eb2012-01-26 09:48:03 -0800291private:
John Grossmanee578c02012-07-23 17:05:46 -0700292 class AudioHwDevice; // fwd declaration for findSuitableHwDev_l
293
Glenn Kastenf78aee72012-01-04 11:00:47 -0800294 audio_mode_t getMode() const { return mMode; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295
Glenn Kastenc59c0042012-02-02 14:06:11 -0800296 bool btNrecIsOff() const { return mBtNrecIsOff; }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700297
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298 AudioFlinger();
299 virtual ~AudioFlinger();
300
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800301 // call in any IAudioFlinger method that accesses mPrimaryHardwareDev
302 status_t initCheck() const { return mPrimaryHardwareDev == NULL ? NO_INIT : NO_ERROR; }
303
Glenn Kasten000f0e32012-03-01 17:10:56 -0800304 // RefBase
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700305 virtual void onFirstRef();
Glenn Kasten000f0e32012-03-01 17:10:56 -0800306
John Grossmanee578c02012-07-23 17:05:46 -0700307 AudioHwDevice* findSuitableHwDev_l(audio_module_handle_t module, audio_devices_t devices);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700308 void purgeStaleEffects_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309
Glenn Kasten66fcab92012-02-24 14:59:21 -0800310 // standby delay for MIXER and DUPLICATING playback threads is read from property
311 // ro.audio.flinger_standbytime_ms or defaults to kDefaultStandbyTimeInNsecs
John Grossman4ff14ba2012-02-08 16:37:41 -0800312 static nsecs_t mStandbyTimeInNsecs;
313
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700314 // Internal dump utilities.
315 void dumpPermissionDenial(int fd, const Vector<String16>& args);
316 void dumpClients(int fd, const Vector<String16>& args);
317 void dumpInternals(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318
319 // --- Client ---
320 class Client : public RefBase {
321 public:
322 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
323 virtual ~Client();
Glenn Kasten435dbe62012-01-30 10:15:48 -0800324 sp<MemoryDealer> heap() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 pid_t pid() const { return mPid; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800326 sp<AudioFlinger> audioFlinger() const { return mAudioFlinger; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327
John Grossman4ff14ba2012-02-08 16:37:41 -0800328 bool reserveTimedTrack();
329 void releaseTimedTrack();
330
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 private:
332 Client(const Client&);
333 Client& operator = (const Client&);
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800334 const sp<AudioFlinger> mAudioFlinger;
335 const sp<MemoryDealer> mMemoryDealer;
336 const pid_t mPid;
John Grossman4ff14ba2012-02-08 16:37:41 -0800337
338 Mutex mTimedTrackLock;
339 int mTimedTrackCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 };
341
342 // --- Notification Client ---
343 class NotificationClient : public IBinder::DeathRecipient {
344 public:
345 NotificationClient(const sp<AudioFlinger>& audioFlinger,
346 const sp<IAudioFlingerClient>& client,
vivek mehta2192bc92012-12-04 11:46:08 -0800347 sp<IBinder> binder);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 virtual ~NotificationClient();
349
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800350 sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351
352 // IBinder::DeathRecipient
353 virtual void binderDied(const wp<IBinder>& who);
354
355 private:
356 NotificationClient(const NotificationClient&);
357 NotificationClient& operator = (const NotificationClient&);
358
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800359 const sp<AudioFlinger> mAudioFlinger;
vivek mehta2192bc92012-12-04 11:46:08 -0800360 sp<IBinder> mBinder;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800361 const sp<IAudioFlingerClient> mAudioFlingerClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 };
363
364 class TrackHandle;
365 class RecordHandle;
366 class RecordThread;
367 class PlaybackThread;
368 class MixerThread;
369 class DirectOutputThread;
370 class DuplicatingThread;
371 class Track;
372 class RecordTrack;
373 class EffectModule;
374 class EffectHandle;
375 class EffectChain;
vivek mehta2192bc92012-12-04 11:46:08 -0800376#ifdef QCOM_HARDWARE
377 struct AudioSessionDescriptor;
378#endif
Dima Zavin799a70e2011-04-18 16:57:27 -0700379 struct AudioStreamOut;
380 struct AudioStreamIn;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381
382 class ThreadBase : public Thread {
383 public:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800385 enum type_t {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700386 MIXER, // Thread class is MixerThread
387 DIRECT, // Thread class is DirectOutputThread
388 DUPLICATING, // Thread class is DuplicatingThread
389 RECORD // Thread class is RecordThread
390 };
391
Eric Laurentf1c04f92012-08-28 14:26:53 -0700392 ThreadBase (const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
393 audio_devices_t outDevice, audio_devices_t inDevice, type_t type);
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800394 virtual ~ThreadBase();
395
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700396 void dumpBase(int fd, const Vector<String16>& args);
397 void dumpEffectChains(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398
Eric Laurentfeb0db62011-07-22 09:04:31 -0700399 void clearPowerManager();
400
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 // base for record and playback
Glenn Kasten288ed212012-04-25 17:52:27 -0700402 class TrackBase : public ExtendedAudioBufferProvider, public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403
404 public:
405 enum track_state {
406 IDLE,
407 TERMINATED,
Eric Laurent29864602012-05-08 18:57:51 -0700408 FLUSHED,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 STOPPED,
Glenn Kastend08f48c2012-05-01 18:14:02 -0700410 // next 2 states are currently used for fast tracks only
411 STOPPING_1, // waiting for first underrun
412 STOPPING_2, // waiting for presentation complete
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 RESUMING,
414 ACTIVE,
415 PAUSING,
416 PAUSED
417 };
418
Glenn Kasten9eaa5572012-01-20 13:32:16 -0800419 TrackBase(ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 const sp<Client>& client,
421 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800422 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700423 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 int frameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 const sp<IMemory>& sharedBuffer,
426 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800427 virtual ~TrackBase();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428
Glenn Kasten0ec23ce2012-07-10 12:56:08 -0700429 virtual status_t start(AudioSystem::sync_event_t event,
430 int triggerSession) = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 virtual void stop() = 0;
Glenn Kastenc59c0042012-02-02 14:06:11 -0800432 sp<IMemory> getCblk() const { return mCblkMemory; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 audio_track_cblk_t* cblk() const { return mCblk; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800434 int sessionId() const { return mSessionId; }
Eric Laurenta011e352012-03-29 15:51:43 -0700435 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436
437 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 TrackBase(const TrackBase&);
439 TrackBase& operator = (const TrackBase&);
440
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800441 // AudioBufferProvider interface
442 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
444
Glenn Kasten288ed212012-04-25 17:52:27 -0700445 // ExtendedAudioBufferProvider interface is only needed for Track,
446 // but putting it in TrackBase avoids the complexity of virtual inheritance
447 virtual size_t framesReady() const { return SIZE_MAX; }
448
Glenn Kasten58f30212012-01-12 12:27:51 -0800449 audio_format_t format() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 return mFormat;
451 }
452
Glenn Kastenc59c0042012-02-02 14:06:11 -0800453 int channelCount() const { return mChannelCount; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454
Glenn Kasten254af182012-07-03 14:59:05 -0700455 audio_channel_mask_t channelMask() const { return mChannelMask; }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700456
Glenn Kastenc59c0042012-02-02 14:06:11 -0800457 int sampleRate() const; // FIXME inline after cblk sr moved
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458
Glenn Kasten66488212012-06-21 12:57:20 -0700459 // Return a pointer to the start of a contiguous slice of the track buffer.
460 // Parameter 'offset' is the requested start position, expressed in
461 // monotonically increasing frame units relative to the track epoch.
462 // Parameter 'frames' is the requested length, also in frame units.
463 // Always returns non-NULL. It is the caller's responsibility to
464 // verify that this will be successful; the result of calling this
465 // function with invalid 'offset' or 'frames' is undefined.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 void* getBuffer(uint32_t offset, uint32_t frames) const;
467
468 bool isStopped() const {
Eric Laurent29864602012-05-08 18:57:51 -0700469 return (mState == STOPPED || mState == FLUSHED);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 }
471
Glenn Kastend08f48c2012-05-01 18:14:02 -0700472 // for fast tracks only
473 bool isStopping() const {
474 return mState == STOPPING_1 || mState == STOPPING_2;
475 }
476 bool isStopping_1() const {
477 return mState == STOPPING_1;
478 }
479 bool isStopping_2() const {
480 return mState == STOPPING_2;
481 }
482
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 bool isTerminated() const {
484 return mState == TERMINATED;
485 }
486
487 bool step();
488 void reset();
489
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800490 const wp<ThreadBase> mThread;
491 /*const*/ sp<Client> mClient; // see explanation at ~TrackBase() why not const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492 sp<IMemory> mCblkMemory;
493 audio_track_cblk_t* mCblk;
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700494 void* mBuffer; // start of track buffer, typically in shared memory
495 void* mBufferEnd; // &mBuffer[mFrameCount * frameSize], where frameSize
496 // is based on mChannelCount and 16-bit samples
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497 uint32_t mFrameCount;
498 // we don't really need a lock for these
Glenn Kastenb853e982012-01-26 13:39:18 -0800499 track_state mState;
Glenn Kasten58912562012-04-03 10:45:00 -0700500 const uint32_t mSampleRate; // initial sample rate only; for tracks which
501 // support dynamic rates, the current value is in control block
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800502 const audio_format_t mFormat;
Glenn Kasten5cf034d2012-02-21 10:35:56 -0800503 bool mStepServerFailed;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800504 const int mSessionId;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700505 uint8_t mChannelCount;
Glenn Kasten254af182012-07-03 14:59:05 -0700506 audio_channel_mask_t mChannelMask;
Eric Laurenta011e352012-03-29 15:51:43 -0700507 Vector < sp<SyncEvent> >mSyncEvents;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508 };
509
Eric Laurent896adcd2012-09-13 11:18:23 -0700510 enum {
511 CFG_EVENT_IO,
512 CFG_EVENT_PRIO
513 };
514
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 class ConfigEvent {
516 public:
Eric Laurent896adcd2012-09-13 11:18:23 -0700517 ConfigEvent(int type) : mType(type) {}
518 virtual ~ConfigEvent() {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519
Eric Laurent896adcd2012-09-13 11:18:23 -0700520 int type() const { return mType; }
521
522 virtual void dump(char *buffer, size_t size) = 0;
523
524 private:
525 const int mType;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 };
527
Eric Laurent896adcd2012-09-13 11:18:23 -0700528 class IoConfigEvent : public ConfigEvent {
529 public:
530 IoConfigEvent(int event, int param) :
531 ConfigEvent(CFG_EVENT_IO), mEvent(event), mParam(event) {}
532 virtual ~IoConfigEvent() {}
533
534 int event() const { return mEvent; }
535 int param() const { return mParam; }
536
537 virtual void dump(char *buffer, size_t size) {
538 snprintf(buffer, size, "IO event: event %d, param %d\n", mEvent, mParam);
539 }
540
541 private:
542 const int mEvent;
543 const int mParam;
544 };
545
546 class PrioConfigEvent : public ConfigEvent {
547 public:
548 PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
549 ConfigEvent(CFG_EVENT_PRIO), mPid(pid), mTid(tid), mPrio(prio) {}
550 virtual ~PrioConfigEvent() {}
551
552 pid_t pid() const { return mPid; }
553 pid_t tid() const { return mTid; }
554 int32_t prio() const { return mPrio; }
555
556 virtual void dump(char *buffer, size_t size) {
557 snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
558 }
559
560 private:
561 const pid_t mPid;
562 const pid_t mTid;
563 const int32_t mPrio;
564 };
565
566
Eric Laurentfeb0db62011-07-22 09:04:31 -0700567 class PMDeathRecipient : public IBinder::DeathRecipient {
568 public:
569 PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
570 virtual ~PMDeathRecipient() {}
571
572 // IBinder::DeathRecipient
573 virtual void binderDied(const wp<IBinder>& who);
574
575 private:
576 PMDeathRecipient(const PMDeathRecipient&);
577 PMDeathRecipient& operator = (const PMDeathRecipient&);
578
579 wp<ThreadBase> mThread;
580 };
581
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700582 virtual status_t initCheck() const = 0;
Glenn Kasten01542f22012-07-02 12:46:15 -0700583
584 // static externally-visible
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800585 type_t type() const { return mType; }
Glenn Kasten01542f22012-07-02 12:46:15 -0700586 audio_io_handle_t id() const { return mId;}
587
588 // dynamic externally-visible
Glenn Kastenc59c0042012-02-02 14:06:11 -0800589 uint32_t sampleRate() const { return mSampleRate; }
590 int channelCount() const { return mChannelCount; }
Glenn Kasten254af182012-07-03 14:59:05 -0700591 audio_channel_mask_t channelMask() const { return mChannelMask; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800592 audio_format_t format() const { return mFormat; }
Glenn Kasten58912562012-04-03 10:45:00 -0700593 // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700594 // and returns the normal mix buffer's frame count.
Glenn Kasten58912562012-04-03 10:45:00 -0700595 size_t frameCount() const { return mNormalFrameCount; }
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700596 // Return's the HAL's frame count i.e. fast mixer buffer size.
597 size_t frameCountHAL() const { return mFrameCount; }
Glenn Kasten01542f22012-07-02 12:46:15 -0700598
Glenn Kastenb28686f2012-01-06 08:39:38 -0800599 // Should be "virtual status_t requestExitAndWait()" and override same
600 // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 void exit();
602 virtual bool checkForNewParameters_l() = 0;
603 virtual status_t setParameters(const String8& keyValuePairs);
604 virtual String8 getParameters(const String8& keys) = 0;
605 virtual void audioConfigChanged_l(int event, int param = 0) = 0;
vivek mehta2192bc92012-12-04 11:46:08 -0800606#ifdef QCOM_HARDWARE
607 void effectConfigChanged();
608#endif
Eric Laurent896adcd2012-09-13 11:18:23 -0700609 void sendIoConfigEvent(int event, int param = 0);
610 void sendIoConfigEvent_l(int event, int param = 0);
611 void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 void processConfigEvents();
Glenn Kasten01542f22012-07-02 12:46:15 -0700613
Eric Laurentf1c04f92012-08-28 14:26:53 -0700614 // see note at declaration of mStandby, mOutDevice and mInDevice
Glenn Kasten02fe1bf2012-02-24 15:42:17 -0800615 bool standby() const { return mStandby; }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700616 audio_devices_t outDevice() const { return mOutDevice; }
617 audio_devices_t inDevice() const { return mInDevice; }
Glenn Kasten01542f22012-07-02 12:46:15 -0700618
Glenn Kasten0bf65bd2012-02-28 18:32:53 -0800619 virtual audio_stream_t* stream() const = 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700620
621 sp<EffectHandle> createEffect_l(
622 const sp<AudioFlinger::Client>& client,
623 const sp<IEffectClient>& effectClient,
624 int32_t priority,
625 int sessionId,
626 effect_descriptor_t *desc,
627 int *enabled,
628 status_t *status);
629 void disconnectEffect(const sp< EffectModule>& effect,
Eric Laurenta5f44eb2012-06-25 11:38:29 -0700630 EffectHandle *handle,
Glenn Kasten58123c32012-02-03 10:32:24 -0800631 bool unpinIfLast);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700632
633 // return values for hasAudioSession (bit field)
634 enum effect_state {
635 EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
636 // effect
637 TRACK_SESSION = 0x2 // the audio session corresponds to at least one
638 // track
639 };
640
641 // get effect chain corresponding to session Id.
642 sp<EffectChain> getEffectChain(int sessionId);
643 // same as getEffectChain() but must be called with ThreadBase mutex locked
Glenn Kasten106e8a42012-08-02 13:37:12 -0700644 sp<EffectChain> getEffectChain_l(int sessionId) const;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700645 // add an effect chain to the chain list (mEffectChains)
646 virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
647 // remove an effect chain from the chain list (mEffectChains)
648 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
Glenn Kastend805b712012-02-24 15:42:48 -0800649 // lock all effect chains Mutexes. Must be called before releasing the
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700650 // ThreadBase mutex before processing the mixer and effects. This guarantees the
651 // integrity of the chains during the process.
Glenn Kastend805b712012-02-24 15:42:48 -0800652 // Also sets the parameter 'effectChains' to current value of mEffectChains.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700653 void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700654 // unlock effect chains after process
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700655 void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700656 // set audio mode to all effect chains
Glenn Kastenf78aee72012-01-04 11:00:47 -0800657 void setMode(audio_mode_t mode);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700658 // get effect module with corresponding ID on specified audio session
Eric Laurent717e1282012-06-29 16:36:52 -0700659 sp<AudioFlinger::EffectModule> getEffect(int sessionId, int effectId);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700660 sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
661 // add and effect module. Also creates the effect chain is none exists for
662 // the effects audio session
663 status_t addEffect_l(const sp< EffectModule>& effect);
664 // remove and effect module. Also removes the effect chain is this was the last
665 // effect
666 void removeEffect_l(const sp< EffectModule>& effect);
667 // detach all tracks connected to an auxiliary effect
668 virtual void detachAuxEffect_l(int effectId) {}
669 // returns either EFFECT_SESSION if effects on this audio session exist in one
670 // chain, or TRACK_SESSION if tracks on this audio session exist, or both
Glenn Kasten106e8a42012-08-02 13:37:12 -0700671 virtual uint32_t hasAudioSession(int sessionId) const = 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700672 // the value returned by default implementation is not important as the
673 // strategy is only meaningful for PlaybackThread which implements this method
674 virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675
Eric Laurent59255e42011-07-27 19:49:51 -0700676 // suspend or restore effect according to the type of effect passed. a NULL
677 // type pointer means suspend all effects in the session
678 void setEffectSuspended(const effect_uuid_t *type,
679 bool suspend,
680 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
681 // check if some effects must be suspended/restored when an effect is enabled
682 // or disabled
Eric Laurenta85a74a2011-10-19 11:44:54 -0700683 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
Eric Laurent59255e42011-07-27 19:49:51 -0700684 bool enabled,
685 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
Eric Laurenta85a74a2011-10-19 11:44:54 -0700686 void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
687 bool enabled,
688 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
Eric Laurenta011e352012-03-29 15:51:43 -0700689
690 virtual status_t setSyncEvent(const sp<SyncEvent>& event) = 0;
Glenn Kasten106e8a42012-08-02 13:37:12 -0700691 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
Eric Laurenta011e352012-03-29 15:51:43 -0700692
693
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 mutable Mutex mLock;
695
696 protected:
697
Eric Laurent59255e42011-07-27 19:49:51 -0700698 // entry describing an effect being suspended in mSuspendedSessions keyed vector
699 class SuspendedSessionDesc : public RefBase {
700 public:
701 SuspendedSessionDesc() : mRefCount(0) {}
702
703 int mRefCount; // number of active suspend requests
704 effect_uuid_t mType; // effect type UUID
705 };
706
Eric Laurentfeb0db62011-07-22 09:04:31 -0700707 void acquireWakeLock();
708 void acquireWakeLock_l();
709 void releaseWakeLock();
710 void releaseWakeLock_l();
Eric Laurent59255e42011-07-27 19:49:51 -0700711 void setEffectSuspended_l(const effect_uuid_t *type,
712 bool suspend,
Glenn Kasten0a7af182012-07-09 16:09:19 -0700713 int sessionId);
Eric Laurent59255e42011-07-27 19:49:51 -0700714 // updated mSuspendedSessions when an effect suspended or restored
715 void updateSuspendedSessions_l(const effect_uuid_t *type,
716 bool suspend,
717 int sessionId);
718 // check if some effects must be suspended when an effect chain is added
719 void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
Eric Laurentfeb0db62011-07-22 09:04:31 -0700720
Jean-Michel Trivi2bfc6b42012-09-28 14:49:39 -0700721 virtual void preExit() { }
722
Glenn Kasten19986612012-03-09 12:07:30 -0800723 friend class AudioFlinger; // for mEffectChains
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724
Glenn Kasten23bb8be2012-01-26 10:38:26 -0800725 const type_t mType;
Glenn Kastencbc52bf2012-03-01 09:21:37 -0800726
727 // Used by parameters, config events, addTrack_l, exit
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 Condition mWaitWorkCV;
Glenn Kastencbc52bf2012-03-01 09:21:37 -0800729
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800730 const sp<AudioFlinger> mAudioFlinger;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731 uint32_t mSampleRate;
Glenn Kasten58912562012-04-03 10:45:00 -0700732 size_t mFrameCount; // output HAL, direct output, record
733 size_t mNormalFrameCount; // normal mixer and effects
Glenn Kasten254af182012-07-03 14:59:05 -0700734 audio_channel_mask_t mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 uint16_t mChannelCount;
Glenn Kastenb9980652012-01-11 09:48:27 -0800736 size_t mFrameSize;
Glenn Kasten58f30212012-01-12 12:27:51 -0800737 audio_format_t mFormat;
Glenn Kastencbc52bf2012-03-01 09:21:37 -0800738
739 // Parameter sequence by client: binder thread calling setParameters():
740 // 1. Lock mLock
741 // 2. Append to mNewParameters
742 // 3. mWaitWorkCV.signal
743 // 4. mParamCond.waitRelative with timeout
744 // 5. read mParamStatus
745 // 6. mWaitWorkCV.signal
746 // 7. Unlock
747 //
748 // Parameter sequence by server: threadLoop calling checkForNewParameters_l():
749 // 1. Lock mLock
750 // 2. If there is an entry in mNewParameters proceed ...
751 // 2. Read first entry in mNewParameters
752 // 3. Process
753 // 4. Remove first entry from mNewParameters
754 // 5. Set mParamStatus
755 // 6. mParamCond.signal
756 // 7. mWaitWorkCV.wait with timeout (this is to avoid overwriting mParamStatus)
757 // 8. Unlock
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 Condition mParamCond;
759 Vector<String8> mNewParameters;
760 status_t mParamStatus;
Glenn Kastencbc52bf2012-03-01 09:21:37 -0800761
Eric Laurent896adcd2012-09-13 11:18:23 -0700762 Vector<ConfigEvent *> mConfigEvents;
Glenn Kasten01542f22012-07-02 12:46:15 -0700763
764 // These fields are written and read by thread itself without lock or barrier,
Eric Laurentf1c04f92012-08-28 14:26:53 -0700765 // and read by other threads without lock or barrier via standby() , outDevice()
766 // and inDevice().
Glenn Kasten01542f22012-07-02 12:46:15 -0700767 // Because of the absence of a lock or barrier, any other thread that reads
768 // these fields must use the information in isolation, or be prepared to deal
769 // with possibility that it might be inconsistent with other information.
770 bool mStandby; // Whether thread is currently in standby.
Eric Laurentf1c04f92012-08-28 14:26:53 -0700771 audio_devices_t mOutDevice; // output device
772 audio_devices_t mInDevice; // input device
Eric Laurent57b2dd12012-08-31 17:44:06 -0700773 audio_source_t mAudioSource; // (see audio.h, audio_source_t)
Glenn Kasten01542f22012-07-02 12:46:15 -0700774
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800775 const audio_io_handle_t mId;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700776 Vector< sp<EffectChain> > mEffectChains;
Glenn Kasten01542f22012-07-02 12:46:15 -0700777
Glenn Kasten480b4682012-02-28 12:30:08 -0800778 static const int kNameLength = 16; // prctl(PR_SET_NAME) limit
Eric Laurentfeb0db62011-07-22 09:04:31 -0700779 char mName[kNameLength];
780 sp<IPowerManager> mPowerManager;
781 sp<IBinder> mWakeLockToken;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800782 const sp<PMDeathRecipient> mDeathRecipient;
Eric Laurent59255e42011-07-27 19:49:51 -0700783 // list of suspended effects per session and per type. The first vector is
784 // keyed by session ID, the second by type UUID timeLow field
785 KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > > mSuspendedSessions;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786 };
787
Glenn Kastenb7bf7962012-02-08 12:36:25 -0800788 struct stream_type_t {
789 stream_type_t()
790 : volume(1.0f),
Eric Laurent1a9ed112012-03-20 18:36:01 -0700791 mute(false)
Glenn Kastenb7bf7962012-02-08 12:36:25 -0800792 {
793 }
794 float volume;
795 bool mute;
Glenn Kastenb7bf7962012-02-08 12:36:25 -0800796 };
797
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 // --- PlaybackThread ---
799 class PlaybackThread : public ThreadBase {
800 public:
801
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 enum mixer_state {
Glenn Kastend805b712012-02-24 15:42:48 -0800803 MIXER_IDLE, // no active tracks
804 MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready
805 MIXER_TRACKS_READY // at least one active track, and at least one track has data
806 // standby mode does not have an enum value
807 // suspend by audio policy manager is orthogonal to mixer state
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 };
809
810 // playback track
Glenn Kasten58912562012-04-03 10:45:00 -0700811 class Track : public TrackBase, public VolumeProvider {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 public:
Glenn Kasten9eaa5572012-01-20 13:32:16 -0800813 Track( PlaybackThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 const sp<Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800815 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800817 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700818 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 int frameCount,
820 const sp<IMemory>& sharedBuffer,
Glenn Kasten73d22752012-03-19 13:38:30 -0700821 int sessionId,
822 IAudioFlinger::track_flags_t flags);
Glenn Kastenc19e2242012-01-30 14:54:39 -0800823 virtual ~Track();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824
Glenn Kasten288ed212012-04-25 17:52:27 -0700825 static void appendDumpHeader(String8& result);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 void dump(char* buffer, size_t size);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800827 virtual status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
Eric Laurenta011e352012-03-29 15:51:43 -0700828 int triggerSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 virtual void stop();
830 void pause();
831
832 void flush();
833 void destroy();
834 void mute(bool);
Glenn Kasten9f34a362012-03-20 16:46:41 -0700835 int name() const { return mName; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836
Glenn Kasten02bbd202012-02-08 12:35:35 -0800837 audio_stream_type_t streamType() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 return mStreamType;
839 }
840 status_t attachAuxEffect(int EffectId);
841 void setAuxBuffer(int EffectId, int32_t *buffer);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800842 int32_t *auxBuffer() const { return mAuxBuffer; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
Glenn Kastenc59c0042012-02-02 14:06:11 -0800844 int16_t *mainBuffer() const { return mMainBuffer; }
845 int auxEffectId() const { return mAuxEffectId; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846
Glenn Kasten58912562012-04-03 10:45:00 -0700847 // implement FastMixerState::VolumeProvider interface
848 virtual uint32_t getVolumeLR();
Eric Laurent29864602012-05-08 18:57:51 -0700849 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
Glenn Kasten73d22752012-03-19 13:38:30 -0700850
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 protected:
Glenn Kasten19986612012-03-09 12:07:30 -0800852 // for numerous
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 friend class PlaybackThread;
854 friend class MixerThread;
855 friend class DirectOutputThread;
856
857 Track(const Track&);
858 Track& operator = (const Track&);
859
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800860 // AudioBufferProvider interface
861 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts = kInvalidPTS);
862 // releaseBuffer() not overridden
863
Glenn Kasten288ed212012-04-25 17:52:27 -0700864 virtual size_t framesReady() const;
John Grossman4ff14ba2012-02-08 16:37:41 -0800865
Glenn Kastenc59c0042012-02-02 14:06:11 -0800866 bool isMuted() const { return mMute; }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 bool isPausing() const {
868 return mState == PAUSING;
869 }
870 bool isPaused() const {
871 return mState == PAUSED;
872 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700873 bool isResuming() const {
874 return mState == RESUMING;
875 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 bool isReady() const;
877 void setPaused() { mState = PAUSED; }
878 void reset();
879
880 bool isOutputTrack() const {
Dima Zavinfce7a472011-04-19 22:30:36 -0700881 return (mStreamType == AUDIO_STREAM_CNT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 }
883
Eric Laurent83faee02012-04-27 18:24:29 -0700884 sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
885
Eric Laurenta011e352012-03-29 15:51:43 -0700886 bool presentationComplete(size_t framesWritten, size_t audioHalFrames);
Eric Laurenta011e352012-03-29 15:51:43 -0700887
Glenn Kasten19986612012-03-09 12:07:30 -0800888 public:
Eric Laurent29864602012-05-08 18:57:51 -0700889 void triggerEvents(AudioSystem::sync_event_t type);
John Grossman4ff14ba2012-02-08 16:37:41 -0800890 virtual bool isTimedTrack() const { return false; }
Glenn Kasten58912562012-04-03 10:45:00 -0700891 bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
Glenn Kastena34f8ec2012-06-21 12:57:49 -0700892
Glenn Kasten19986612012-03-09 12:07:30 -0800893 protected:
John Grossman4ff14ba2012-02-08 16:37:41 -0800894
Glenn Kastena34f8ec2012-06-21 12:57:49 -0700895 // written by Track::mute() called by binder thread(s), without a mutex or barrier.
896 // read by Track::isMuted() called by playback thread, also without a mutex or barrier.
897 // The lack of mutex or barrier is safe because the mute status is only used by itself.
898 bool mMute;
899
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 // FILLED state is used for suppressing volume ramp at begin of playing
Glenn Kasten58912562012-04-03 10:45:00 -0700901 enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE};
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 mutable uint8_t mFillingUpStatus;
903 int8_t mRetryCount;
Glenn Kastenf9959012012-03-19 11:14:37 -0700904 const sp<IMemory> mSharedBuffer;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 bool mResetDone;
Glenn Kastenf9959012012-03-19 11:14:37 -0700906 const audio_stream_type_t mStreamType;
Glenn Kasten288ed212012-04-25 17:52:27 -0700907 int mName; // track name on the normal mixer,
908 // allocated statically at track creation time,
909 // and is even allocated (though unused) for fast tracks
Glenn Kasten893a0542012-05-30 10:32:06 -0700910 // FIXME don't allocate track name for fast tracks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 int16_t *mMainBuffer;
912 int32_t *mAuxBuffer;
913 int mAuxEffectId;
Eric Laurent8f45bd72010-08-31 13:50:07 -0700914 bool mHasVolumeController;
Eric Laurenta011e352012-03-29 15:51:43 -0700915 size_t mPresentationCompleteFrames; // number of frames written to the audio HAL
916 // when this track will be fully rendered
Glenn Kasten73d22752012-03-19 13:38:30 -0700917 private:
918 IAudioFlinger::track_flags_t mFlags;
Glenn Kasten288ed212012-04-25 17:52:27 -0700919
920 // The following fields are only for fast tracks, and should be in a subclass
921 int mFastIndex; // index within FastMixerState::mFastTracks[];
Glenn Kasten893a0542012-05-30 10:32:06 -0700922 // either mFastIndex == -1 if not isFastTrack()
Glenn Kasten288ed212012-04-25 17:52:27 -0700923 // or 0 < mFastIndex < FastMixerState::kMaxFast because
924 // index 0 is reserved for normal mixer's submix;
925 // index is allocated statically at track creation time
926 // but the slot is only used if track is active
Glenn Kasten09474df2012-05-10 14:48:07 -0700927 FastTrackUnderruns mObservedUnderruns; // Most recently observed value of
Glenn Kasten288ed212012-04-25 17:52:27 -0700928 // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns
929 uint32_t mUnderrunCount; // Counter of total number of underruns, never reset
Glenn Kasten58912562012-04-03 10:45:00 -0700930 volatile float mCachedVolume; // combined master volume and stream type volume;
931 // 'volatile' means accessed without lock or
932 // barrier, but is read/written atomically
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 }; // end of Track
934
John Grossman4ff14ba2012-02-08 16:37:41 -0800935 class TimedTrack : public Track {
936 public:
Glenn Kasten9eaa5572012-01-20 13:32:16 -0800937 static sp<TimedTrack> create(PlaybackThread *thread,
John Grossman4ff14ba2012-02-08 16:37:41 -0800938 const sp<Client>& client,
939 audio_stream_type_t streamType,
940 uint32_t sampleRate,
941 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700942 audio_channel_mask_t channelMask,
John Grossman4ff14ba2012-02-08 16:37:41 -0800943 int frameCount,
944 const sp<IMemory>& sharedBuffer,
945 int sessionId);
Glenn Kastend96c5722012-04-25 13:44:49 -0700946 virtual ~TimedTrack();
John Grossman4ff14ba2012-02-08 16:37:41 -0800947
948 class TimedBuffer {
949 public:
950 TimedBuffer();
951 TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
952 const sp<IMemory>& buffer() const { return mBuffer; }
953 int64_t pts() const { return mPTS; }
John Grossman1c345192012-03-27 14:00:17 -0700954 uint32_t position() const { return mPosition; }
955 void setPosition(uint32_t pos) { mPosition = pos; }
John Grossman4ff14ba2012-02-08 16:37:41 -0800956 private:
957 sp<IMemory> mBuffer;
John Grossman1c345192012-03-27 14:00:17 -0700958 int64_t mPTS;
959 uint32_t mPosition;
John Grossman4ff14ba2012-02-08 16:37:41 -0800960 };
961
John Grossman1c345192012-03-27 14:00:17 -0700962 // Mixer facing methods.
John Grossman4ff14ba2012-02-08 16:37:41 -0800963 virtual bool isTimedTrack() const { return true; }
Glenn Kasten288ed212012-04-25 17:52:27 -0700964 virtual size_t framesReady() const;
John Grossman4ff14ba2012-02-08 16:37:41 -0800965
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800966 // AudioBufferProvider interface
John Grossman1c345192012-03-27 14:00:17 -0700967 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
968 int64_t pts);
John Grossman4ff14ba2012-02-08 16:37:41 -0800969 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
John Grossman4ff14ba2012-02-08 16:37:41 -0800970
John Grossman1c345192012-03-27 14:00:17 -0700971 // Client/App facing methods.
John Grossman4ff14ba2012-02-08 16:37:41 -0800972 status_t allocateTimedBuffer(size_t size,
973 sp<IMemory>* buffer);
974 status_t queueTimedBuffer(const sp<IMemory>& buffer,
975 int64_t pts);
976 status_t setMediaTimeTransform(const LinearTransform& xform,
977 TimedAudioTrack::TargetTimeline target);
John Grossman4ff14ba2012-02-08 16:37:41 -0800978
979 private:
Glenn Kasten9eaa5572012-01-20 13:32:16 -0800980 TimedTrack(PlaybackThread *thread,
John Grossman4ff14ba2012-02-08 16:37:41 -0800981 const sp<Client>& client,
982 audio_stream_type_t streamType,
983 uint32_t sampleRate,
984 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700985 audio_channel_mask_t channelMask,
John Grossman4ff14ba2012-02-08 16:37:41 -0800986 int frameCount,
987 const sp<IMemory>& sharedBuffer,
988 int sessionId);
989
John Grossman1c345192012-03-27 14:00:17 -0700990 void timedYieldSamples_l(AudioBufferProvider::Buffer* buffer);
991 void timedYieldSilence_l(uint32_t numFrames,
992 AudioBufferProvider::Buffer* buffer);
993 void trimTimedBufferQueue_l();
994 void trimTimedBufferQueueHead_l(const char* logTag);
995 void updateFramesPendingAfterTrim_l(const TimedBuffer& buf,
996 const char* logTag);
997
John Grossman4ff14ba2012-02-08 16:37:41 -0800998 uint64_t mLocalTimeFreq;
999 LinearTransform mLocalTimeToSampleTransform;
John Grossman9fbdee12012-03-26 17:51:46 -07001000 LinearTransform mMediaTimeToSampleTransform;
John Grossman4ff14ba2012-02-08 16:37:41 -08001001 sp<MemoryDealer> mTimedMemoryDealer;
John Grossman9fbdee12012-03-26 17:51:46 -07001002
John Grossman4ff14ba2012-02-08 16:37:41 -08001003 Vector<TimedBuffer> mTimedBufferQueue;
John Grossman9fbdee12012-03-26 17:51:46 -07001004 bool mQueueHeadInFlight;
1005 bool mTrimQueueHeadOnRelease;
John Grossman1c345192012-03-27 14:00:17 -07001006 uint32_t mFramesPendingInQueue;
John Grossman9fbdee12012-03-26 17:51:46 -07001007
John Grossman4ff14ba2012-02-08 16:37:41 -08001008 uint8_t* mTimedSilenceBuffer;
1009 uint32_t mTimedSilenceBufferSize;
1010 mutable Mutex mTimedBufferQueueLock;
1011 bool mTimedAudioOutputOnTime;
1012 CCHelper mCCHelper;
1013
1014 Mutex mMediaTimeTransformLock;
1015 LinearTransform mMediaTimeTransform;
1016 bool mMediaTimeTransformValid;
1017 TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
1018 };
1019
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020
1021 // playback track
1022 class OutputTrack : public Track {
1023 public:
1024
1025 class Buffer: public AudioBufferProvider::Buffer {
1026 public:
1027 int16_t *mBuffer;
1028 };
1029
Glenn Kasten9eaa5572012-01-20 13:32:16 -08001030 OutputTrack(PlaybackThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 DuplicatingThread *sourceThread,
1032 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001033 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001034 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035 int frameCount);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001036 virtual ~OutputTrack();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037
Glenn Kasten3acbd052012-02-28 10:39:56 -08001038 virtual status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
Eric Laurenta011e352012-03-29 15:51:43 -07001039 int triggerSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 virtual void stop();
1041 bool write(int16_t* data, uint32_t frames);
Glenn Kastena1117922012-01-26 10:53:32 -08001042 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001043 bool isActive() const { return mActive; }
1044 const wp<ThreadBase>& thread() const { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045
1046 private:
1047
Glenn Kasten335787f2012-01-20 17:00:00 -08001048 enum {
1049 NO_MORE_BUFFERS = 0x80000001, // same in AudioTrack.h, ok to be different value
1050 };
1051
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
1053 void clearBufferQueue();
1054
1055 // Maximum number of pending buffers allocated by OutputTrack::write()
1056 static const uint8_t kMaxOverFlowBuffers = 10;
1057
1058 Vector < Buffer* > mBufferQueue;
1059 AudioBufferProvider::Buffer mOutBuffer;
1060 bool mActive;
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001061 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }; // end of OutputTrack
1063
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001064 PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001065 audio_io_handle_t id, audio_devices_t device, type_t type);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 virtual ~PlaybackThread();
1067
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001068 void dump(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069
1070 // Thread virtuals
1071 virtual status_t readyToRun();
Glenn Kasten000f0e32012-03-01 17:10:56 -08001072 virtual bool threadLoop();
1073
1074 // RefBase
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 virtual void onFirstRef();
1076
Glenn Kasten000f0e32012-03-01 17:10:56 -08001077protected:
1078 // Code snippets that were lifted up out of threadLoop()
1079 virtual void threadLoop_mix() = 0;
1080 virtual void threadLoop_sleepTime() = 0;
1081 virtual void threadLoop_write();
1082 virtual void threadLoop_standby();
Eric Laurent44a957f2012-05-15 15:26:05 -07001083 virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
Glenn Kasten000f0e32012-03-01 17:10:56 -08001084
Glenn Kasten288ed212012-04-25 17:52:27 -07001085 // prepareTracks_l reads and writes mActiveTracks, and returns
1086 // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller
1087 // is responsible for clearing or destroying this Vector later on, when it
Glenn Kasten1465f0c2012-03-06 11:23:32 -08001088 // is safe to do so. That will drop the final ref count and destroy the tracks.
1089 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
Glenn Kasten000f0e32012-03-01 17:10:56 -08001090
Jean-Michel Trivi2bfc6b42012-09-28 14:49:39 -07001091 // ThreadBase virtuals
1092 virtual void preExit();
1093
Glenn Kasten000f0e32012-03-01 17:10:56 -08001094public:
1095
Glenn Kastene0feee32011-12-13 11:53:26 -08001096 virtual status_t initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001097
Glenn Kasten688aac72012-03-09 10:30:26 -08001098 // return estimated latency in milliseconds, as reported by HAL
1099 uint32_t latency() const;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -07001100 // same, but lock must already be held
1101 uint32_t latency_l() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001102
Glenn Kasten6637baa2012-01-09 09:40:36 -08001103 void setMasterVolume(float value);
1104 void setMasterMute(bool muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001105
Glenn Kasten6637baa2012-01-09 09:40:36 -08001106 void setStreamVolume(audio_stream_type_t stream, float value);
1107 void setStreamMute(audio_stream_type_t stream, bool muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108
Glenn Kasten6637baa2012-01-09 09:40:36 -08001109 float streamVolume(audio_stream_type_t stream) const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111 sp<Track> createTrack_l(
1112 const sp<AudioFlinger::Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -08001113 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001115 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001116 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 int frameCount,
1118 const sp<IMemory>& sharedBuffer,
1119 int sessionId,
Glenn Kasten73d22752012-03-19 13:38:30 -07001120 IAudioFlinger::track_flags_t flags,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001121 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122 status_t *status);
1123
Glenn Kastenaed850d2012-01-26 09:46:34 -08001124 AudioStreamOut* getOutput() const;
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001125 AudioStreamOut* clearOutput();
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001126 virtual audio_stream_t* stream() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127
Glenn Kasten1ea6d232012-07-09 14:31:33 -07001128 // a very large number of suspend() will eventually wraparound, but unlikely
1129 void suspend() { (void) android_atomic_inc(&mSuspended); }
1130 void restore()
1131 {
1132 // if restore() is done without suspend(), get back into
1133 // range so that the next suspend() will operate correctly
1134 if (android_atomic_dec(&mSuspended) <= 0) {
1135 android_atomic_release_store(0, &mSuspended);
1136 }
1137 }
1138 bool isSuspended() const
1139 { return android_atomic_acquire_load(&mSuspended) > 0; }
1140
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 virtual String8 getParameters(const String8& keys);
1142 virtual void audioConfigChanged_l(int event, int param = 0);
Glenn Kasten688aac72012-03-09 10:30:26 -08001143 status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
Glenn Kastenc59c0042012-02-02 14:06:11 -08001144 int16_t *mixBuffer() const { return mMixBuffer; };
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001146 virtual void detachAuxEffect_l(int effectId);
Eric Laurentde070132010-07-13 04:45:46 -07001147 status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
1148 int EffectId);
1149 status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
1150 int EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001152 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1153 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
Glenn Kasten106e8a42012-08-02 13:37:12 -07001154 virtual uint32_t hasAudioSession(int sessionId) const;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001155 virtual uint32_t getStrategyForSession_l(int sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07001156
Eric Laurent9f6530f2011-08-30 10:18:54 -07001157
Eric Laurenta011e352012-03-29 15:51:43 -07001158 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
Glenn Kasten106e8a42012-08-02 13:37:12 -07001159 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
Eric Laurent22167852012-06-20 12:26:32 -07001160 void invalidateTracks(audio_stream_type_t streamType);
1161
Eric Laurenta011e352012-03-29 15:51:43 -07001162
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 protected:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164 int16_t* mMixBuffer;
Glenn Kasten1ea6d232012-07-09 14:31:33 -07001165
1166 // suspend count, > 0 means suspended. While suspended, the thread continues to pull from
1167 // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle
1168 // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
1169 // workaround that restriction.
1170 // 'volatile' means accessed via atomic operations and no lock.
1171 volatile int32_t mSuspended;
1172
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 int mBytesWritten;
Glenn Kasten98067102011-12-13 11:47:54 -08001174 private:
Glenn Kasten99e53b82012-01-19 08:59:58 -08001175 // mMasterMute is in both PlaybackThread and in AudioFlinger. When a
1176 // PlaybackThread needs to find out if master-muted, it checks it's local
1177 // copy rather than the one in AudioFlinger. This optimization saves a lock.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 bool mMasterMute;
Glenn Kasten6637baa2012-01-09 09:40:36 -08001179 void setMasterMute_l(bool muted) { mMasterMute = muted; }
Glenn Kasten98067102011-12-13 11:47:54 -08001180 protected:
Glenn Kasten288ed212012-04-25 17:52:27 -07001181 SortedVector< wp<Track> > mActiveTracks; // FIXME check if this could be sp<>
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001183 // Allocate a track name for a given channel mask.
1184 // Returns name >= 0 if successful, -1 on failure.
Jean-Michel Trivife3156e2012-09-10 18:58:27 -07001185 virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId) = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 virtual void deleteTrackName_l(int name) = 0;
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001187
1188 // Time to sleep between cycles when:
1189 virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED
1190 virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE
1191 virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us
1192 // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
1193 // No sleep in standby mode; waits on a condition
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194
Glenn Kasten37d825e2012-02-24 07:21:48 -08001195 // Code snippets that are temporarily lifted up out of threadLoop() until the merge
1196 void checkSilentMode_l();
1197
Glenn Kastenfa26a852012-03-06 11:28:04 -08001198 // Non-trivial for DUPLICATING only
1199 virtual void saveOutputTracks() { }
1200 virtual void clearOutputTracks() { }
1201
Glenn Kasten66fcab92012-02-24 14:59:21 -08001202 // Cache various calculated values, at threadLoop() entry and after a parameter change
1203 virtual void cacheParameters_l();
1204
Eric Laurente737cda2012-05-22 18:55:44 -07001205 virtual uint32_t correctLatency(uint32_t latency) const;
1206
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 private:
1208
Glenn Kasten19986612012-03-09 12:07:30 -08001209 friend class AudioFlinger; // for numerous
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210
1211 PlaybackThread(const Client&);
1212 PlaybackThread& operator = (const PlaybackThread&);
1213
1214 status_t addTrack_l(const sp<Track>& track);
1215 void destroyTrack_l(const sp<Track>& track);
Eric Laurentb469b942011-05-09 12:09:06 -07001216 void removeTrack_l(const sp<Track>& track);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217
1218 void readOutputParameters();
1219
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001220 virtual void dumpInternals(int fd, const Vector<String16>& args);
1221 void dumpTracks(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222
1223 SortedVector< sp<Track> > mTracks;
Glenn Kasten263709e2012-01-06 08:40:01 -08001224 // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by DuplicatingThread
Dima Zavinfce7a472011-04-19 22:30:36 -07001225 stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1];
Glenn Kastenaed850d2012-01-26 09:46:34 -08001226 AudioStreamOut *mOutput;
Glenn Kasten9f34a362012-03-20 16:46:41 -07001227
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 float mMasterVolume;
1229 nsecs_t mLastWriteTime;
1230 int mNumWrites;
1231 int mNumDelayedWrites;
1232 bool mInWrite;
Glenn Kasten000f0e32012-03-01 17:10:56 -08001233
1234 // FIXME rename these former local variables of threadLoop to standard "m" names
1235 nsecs_t standbyTime;
1236 size_t mixBufferSize;
Glenn Kasten66fcab92012-02-24 14:59:21 -08001237
1238 // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
Glenn Kasten000f0e32012-03-01 17:10:56 -08001239 uint32_t activeSleepTime;
1240 uint32_t idleSleepTime;
Glenn Kasten66fcab92012-02-24 14:59:21 -08001241
Glenn Kasten000f0e32012-03-01 17:10:56 -08001242 uint32_t sleepTime;
Glenn Kastenfec279f2012-03-08 07:47:15 -08001243
1244 // mixer status returned by prepareTracks_l()
Eric Laurentda747442012-04-25 18:53:13 -07001245 mixer_state mMixerStatus; // current cycle
1246 // previous cycle when in prepareTracks_l()
Glenn Kasten81028042012-04-30 18:15:12 -07001247 mixer_state mMixerStatusIgnoringFastTracks;
1248 // FIXME or a separate ready state per track
Glenn Kasten000f0e32012-03-01 17:10:56 -08001249
1250 // FIXME move these declarations into the specific sub-class that needs them
1251 // MIXER only
Glenn Kasten000f0e32012-03-01 17:10:56 -08001252 uint32_t sleepTimeShift;
Glenn Kasten66fcab92012-02-24 14:59:21 -08001253
1254 // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
Glenn Kasten000f0e32012-03-01 17:10:56 -08001255 nsecs_t standbyDelay;
Glenn Kasten66fcab92012-02-24 14:59:21 -08001256
1257 // MIXER only
1258 nsecs_t maxPeriod;
1259
Glenn Kasten000f0e32012-03-01 17:10:56 -08001260 // DUPLICATING only
Glenn Kasten000f0e32012-03-01 17:10:56 -08001261 uint32_t writeFrames;
Glenn Kasten58912562012-04-03 10:45:00 -07001262
1263 private:
1264 // The HAL output sink is treated as non-blocking, but current implementation is blocking
1265 sp<NBAIO_Sink> mOutputSink;
1266 // If a fast mixer is present, the blocking pipe sink, otherwise clear
1267 sp<NBAIO_Sink> mPipeSink;
1268 // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
1269 sp<NBAIO_Sink> mNormalSink;
Glenn Kastenfbae5da2012-05-21 09:17:20 -07001270 // For dumpsys
1271 sp<NBAIO_Sink> mTeeSink;
1272 sp<NBAIO_Source> mTeeSource;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001273 uint32_t mScreenState; // cached copy of gScreenState
Glenn Kasten58912562012-04-03 10:45:00 -07001274 public:
1275 virtual bool hasFastMixer() const = 0;
Glenn Kasten09474df2012-05-10 14:48:07 -07001276 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const
1277 { FastTrackUnderruns dummy; return dummy; }
Glenn Kasten58912562012-04-03 10:45:00 -07001278
1279 protected:
1280 // accessed by both binder threads and within threadLoop(), lock on mutex needed
1281 unsigned mFastTrackAvailMask; // bit i set if fast track [i] is available
Glenn Kasten58912562012-04-03 10:45:00 -07001282
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 };
1284
1285 class MixerThread : public PlaybackThread {
1286 public:
Eric Laurentde070132010-07-13 04:45:46 -07001287 MixerThread (const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -07001288 AudioStreamOut* output,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001289 audio_io_handle_t id,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001290 audio_devices_t device,
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001291 type_t type = MIXER);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 virtual ~MixerThread();
1293
1294 // Thread virtuals
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 virtual bool checkForNewParameters_l();
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001297 virtual void dumpInternals(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298
1299 protected:
Glenn Kasten000f0e32012-03-01 17:10:56 -08001300 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
Jean-Michel Trivife3156e2012-09-10 18:58:27 -07001301 virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302 virtual void deleteTrackName_l(int name);
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001303 virtual uint32_t idleSleepTimeUs() const;
1304 virtual uint32_t suspendSleepTimeUs() const;
Glenn Kasten66fcab92012-02-24 14:59:21 -08001305 virtual void cacheParameters_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306
Glenn Kasten000f0e32012-03-01 17:10:56 -08001307 // threadLoop snippets
Glenn Kasten58912562012-04-03 10:45:00 -07001308 virtual void threadLoop_write();
1309 virtual void threadLoop_standby();
Glenn Kasten000f0e32012-03-01 17:10:56 -08001310 virtual void threadLoop_mix();
1311 virtual void threadLoop_sleepTime();
Glenn Kasten58912562012-04-03 10:45:00 -07001312 virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
Eric Laurente737cda2012-05-22 18:55:44 -07001313 virtual uint32_t correctLatency(uint32_t latency) const;
Glenn Kasten000f0e32012-03-01 17:10:56 -08001314
Glenn Kasten58912562012-04-03 10:45:00 -07001315 AudioMixer* mAudioMixer; // normal mixer
1316 private:
Glenn Kasten58912562012-04-03 10:45:00 -07001317 // one-time initialization, no locks required
1318 FastMixer* mFastMixer; // non-NULL if there is also a fast mixer
Glenn Kastenc15d6652012-05-30 14:52:57 -07001319 sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
Glenn Kasten58912562012-04-03 10:45:00 -07001320
1321 // contents are not guaranteed to be consistent, no locks required
1322 FastMixerDumpState mFastMixerDumpState;
Glenn Kasten39993082012-05-31 13:40:27 -07001323#ifdef STATE_QUEUE_DUMP
1324 StateQueueObserverDump mStateQueueObserverDump;
1325 StateQueueMutatorDump mStateQueueMutatorDump;
1326#endif
Glenn Kastenc15d6652012-05-30 14:52:57 -07001327 AudioWatchdogDump mAudioWatchdogDump;
Glenn Kasten58912562012-04-03 10:45:00 -07001328
1329 // accessible only within the threadLoop(), no locks required
1330 // mFastMixer->sq() // for mutating and pushing state
1331 int32_t mFastMixerFutex; // for cold idle
1332
1333 public:
1334 virtual bool hasFastMixer() const { return mFastMixer != NULL; }
Glenn Kasten09474df2012-05-10 14:48:07 -07001335 virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
Glenn Kasten88cbea82012-05-15 07:39:27 -07001336 ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks);
Glenn Kasten09474df2012-05-10 14:48:07 -07001337 return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
Glenn Kasten288ed212012-04-25 17:52:27 -07001338 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339 };
1340
1341 class DirectOutputThread : public PlaybackThread {
1342 public:
1343
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001344 DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001345 audio_io_handle_t id, audio_devices_t device);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001346 virtual ~DirectOutputThread();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347
1348 // Thread virtuals
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349
1350 virtual bool checkForNewParameters_l();
1351
1352 protected:
Jean-Michel Trivife3156e2012-09-10 18:58:27 -07001353 virtual int getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354 virtual void deleteTrackName_l(int name);
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001355 virtual uint32_t activeSleepTimeUs() const;
1356 virtual uint32_t idleSleepTimeUs() const;
1357 virtual uint32_t suspendSleepTimeUs() const;
Glenn Kasten66fcab92012-02-24 14:59:21 -08001358 virtual void cacheParameters_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359
Glenn Kasten000f0e32012-03-01 17:10:56 -08001360 // threadLoop snippets
Glenn Kasten1465f0c2012-03-06 11:23:32 -08001361 virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
Glenn Kasten000f0e32012-03-01 17:10:56 -08001362 virtual void threadLoop_mix();
1363 virtual void threadLoop_sleepTime();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364
Glenn Kasten99e53b82012-01-19 08:59:58 -08001365 // volumes last sent to audio HAL with stream->set_volume()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366 float mLeftVolFloat;
1367 float mRightVolFloat;
Glenn Kastend4513b02012-03-06 15:52:35 -08001368
1369private:
Glenn Kastenb071e9b2012-03-07 17:05:59 -08001370 // prepareTracks_l() tells threadLoop_mix() the name of the single active track
1371 sp<Track> mActiveTrack;
Glenn Kasten58912562012-04-03 10:45:00 -07001372 public:
1373 virtual bool hasFastMixer() const { return false; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374 };
1375
1376 class DuplicatingThread : public MixerThread {
1377 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001378 DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
1379 audio_io_handle_t id);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001380 virtual ~DuplicatingThread();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381
1382 // Thread virtuals
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383 void addOutputTrack(MixerThread* thread);
1384 void removeOutputTrack(MixerThread* thread);
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001385 uint32_t waitTimeMs() const { return mWaitTimeMs; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 protected:
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001387 virtual uint32_t activeSleepTimeUs() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388
1389 private:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001390 bool outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
Glenn Kasten000f0e32012-03-01 17:10:56 -08001391 protected:
1392 // threadLoop snippets
1393 virtual void threadLoop_mix();
1394 virtual void threadLoop_sleepTime();
1395 virtual void threadLoop_write();
1396 virtual void threadLoop_standby();
Glenn Kasten66fcab92012-02-24 14:59:21 -08001397 virtual void cacheParameters_l();
Glenn Kasten438b0362012-03-06 11:24:48 -08001398
Glenn Kasten66fcab92012-02-24 14:59:21 -08001399 private:
Glenn Kasten438b0362012-03-06 11:24:48 -08001400 // called from threadLoop, addOutputTrack, removeOutputTrack
1401 virtual void updateWaitTime_l();
Glenn Kasten66fcab92012-02-24 14:59:21 -08001402 protected:
Glenn Kastenfa26a852012-03-06 11:28:04 -08001403 virtual void saveOutputTracks();
1404 virtual void clearOutputTracks();
Glenn Kasten000f0e32012-03-01 17:10:56 -08001405 private:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 uint32_t mWaitTimeMs;
Glenn Kastenfa26a852012-03-06 11:28:04 -08001408 SortedVector < sp<OutputTrack> > outputTracks;
1409 SortedVector < sp<OutputTrack> > mOutputTracks;
Glenn Kasten58912562012-04-03 10:45:00 -07001410 public:
1411 virtual bool hasFastMixer() const { return false; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 };
1413
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001414 PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const;
1415 MixerThread *checkMixerThread_l(audio_io_handle_t output) const;
1416 RecordThread *checkRecordThread_l(audio_io_handle_t input) const;
Glenn Kasten6637baa2012-01-09 09:40:36 -08001417 // no range check, AudioFlinger::mLock held
1418 bool streamMute_l(audio_stream_type_t stream) const
1419 { return mStreamTypes[stream].mute; }
1420 // no range check, doesn't check per-thread stream volume, AudioFlinger::mLock held
1421 float streamVolume_l(audio_stream_type_t stream) const
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001422 { return mStreamTypes[stream].volume; }
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001423 void audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001425 // allocate an audio_io_handle_t, session ID, or effect ID
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001426 uint32_t nextUniqueId();
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001427
Eric Laurent59255e42011-07-27 19:49:51 -07001428 status_t moveEffectChain_l(int sessionId,
Glenn Kastena1117922012-01-26 10:53:32 -08001429 PlaybackThread *srcThread,
1430 PlaybackThread *dstThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07001431 bool reRegister);
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001432 // return thread associated with primary hardware device, or NULL
1433 PlaybackThread *primaryPlaybackThread_l() const;
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001434 audio_devices_t primaryOutputDevice_l() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435
Eric Laurent717e1282012-06-29 16:36:52 -07001436 sp<PlaybackThread> getEffectThread_l(int sessionId, int EffectId);
1437
Glenn Kasten99e53b82012-01-19 08:59:58 -08001438 // server side of the client's IAudioTrack
vivek mehta2192bc92012-12-04 11:46:08 -08001439#ifdef QCOM_HARDWARE
1440 class DirectAudioTrack : public android::BnDirectTrack,
1441 public AudioEventObserver
1442 {
1443 public:
1444 DirectAudioTrack(const sp<AudioFlinger>& audioFlinger,
1445 int output, AudioSessionDescriptor *outputDesc,
1446 IDirectTrackClient* client, audio_output_flags_t outflag);
1447 virtual ~DirectAudioTrack();
1448 virtual status_t start();
1449 virtual void stop();
1450 virtual void flush();
1451 virtual void mute(bool);
1452 virtual void pause();
1453 virtual ssize_t write(const void *buffer, size_t bytes);
1454 virtual void setVolume(float left, float right);
1455 virtual int64_t getTimeStamp();
1456 virtual void postEOS(int64_t delayUs);
1457
1458 virtual status_t onTransact(
1459 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1460 private:
1461
1462 IDirectTrackClient* mClient;
1463 AudioSessionDescriptor *mOutputDesc;
1464 int mOutput;
1465 bool mIsPaused;
1466 audio_output_flags_t mFlag;
1467
1468 class BufferInfo {
1469 public:
1470 BufferInfo(void *buf1, void *buf2, int32_t nSize) :
1471 localBuf(buf1), dspBuf(buf2), memBufsize(nSize)
1472 {}
1473
1474 void *localBuf;
1475 void *dspBuf;
1476 uint32_t memBufsize;
1477 uint32_t bytesToWrite;
1478 };
1479 List<BufferInfo> mBufPool;
1480 List<BufferInfo> mEffectsPool;
1481
1482 void allocateBufPool();
1483 void deallocateBufPool();
1484
1485 //******Effects*************
1486 static void *EffectsThreadWrapper(void *me);
1487 void EffectsThreadEntry();
1488 // make sure the Effects thread also exited
1489 void requestAndWaitForEffectsThreadExit();
1490 void createEffectThread();
1491 Condition mEffectCv;
1492 Mutex mEffectLock;
1493 pthread_t mEffectsThread;
1494 bool mKillEffectsThread;
1495 bool mEffectsThreadAlive;
1496 bool mEffectConfigChanged;
1497
1498 //Structure to recieve the Effect notification from the flinger.
1499 class AudioFlingerDirectTrackClient: public IBinder::DeathRecipient, public BnAudioFlingerClient {
1500 public:
1501 AudioFlingerDirectTrackClient(void *obj);
1502
1503 DirectAudioTrack *pBaseClass;
1504 // DeathRecipient
1505 virtual void binderDied(const wp<IBinder>& who);
1506
1507 // IAudioFlingerClient
1508
1509 // indicate a change in the configuration of an output or input: keeps the cached
1510 // values for output/input parameters upto date in client process
1511 virtual void ioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2);
1512
1513 friend class DirectAudioTrack;
1514 };
1515 // helper function to obtain AudioFlinger service handle
1516 sp<AudioFlinger> mAudioFlinger;
1517 sp<AudioFlingerDirectTrackClient> mAudioFlingerClient;
1518
1519 void clearPowerManager();
1520 class PMDeathRecipient : public IBinder::DeathRecipient {
1521 public:
1522 PMDeathRecipient(void *obj){parentClass = (DirectAudioTrack *)obj;}
1523 virtual ~PMDeathRecipient() {}
1524
1525 // IBinder::DeathRecipient
1526 virtual void binderDied(const wp<IBinder>& who);
1527
1528 private:
1529 DirectAudioTrack *parentClass;
1530 PMDeathRecipient(const PMDeathRecipient&);
1531 PMDeathRecipient& operator = (const PMDeathRecipient&);
1532
1533 friend class DirectAudioTrack;
1534 };
1535
1536 friend class PMDeathRecipient;
1537
1538 Mutex pmLock;
1539 void acquireWakeLock();
1540 void releaseWakeLock();
1541
1542 sp<IPowerManager> mPowerManager;
1543 sp<IBinder> mWakeLockToken;
1544 sp<PMDeathRecipient> mDeathRecipient;
1545 };
1546#endif
1547
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548 class TrackHandle : public android::BnAudioTrack {
1549 public:
1550 TrackHandle(const sp<PlaybackThread::Track>& track);
1551 virtual ~TrackHandle();
Glenn Kasten90716c52012-01-26 13:40:12 -08001552 virtual sp<IMemory> getCblk() const;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001553 virtual status_t start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554 virtual void stop();
1555 virtual void flush();
1556 virtual void mute(bool);
1557 virtual void pause();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 virtual status_t attachAuxEffect(int effectId);
John Grossman4ff14ba2012-02-08 16:37:41 -08001559 virtual status_t allocateTimedBuffer(size_t size,
1560 sp<IMemory>* buffer);
1561 virtual status_t queueTimedBuffer(const sp<IMemory>& buffer,
1562 int64_t pts);
1563 virtual status_t setMediaTimeTransform(const LinearTransform& xform,
1564 int target);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565 virtual status_t onTransact(
1566 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1567 private:
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001568 const sp<PlaybackThread::Track> mTrack;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 };
1570
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571 void removeClient_l(pid_t pid);
vivek mehta2192bc92012-12-04 11:46:08 -08001572 void removeNotificationClient(sp<IBinder> binder);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573
1574
1575 // record thread
1576 class RecordThread : public ThreadBase, public AudioBufferProvider
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001577 // derives from AudioBufferProvider interface for use by resampler
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 {
1579 public:
1580
1581 // record track
1582 class RecordTrack : public TrackBase {
1583 public:
Glenn Kasten9eaa5572012-01-20 13:32:16 -08001584 RecordTrack(RecordThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585 const sp<Client>& client,
1586 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001587 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001588 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589 int frameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001590 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001591 virtual ~RecordTrack();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592
Glenn Kasten0ec23ce2012-07-10 12:56:08 -07001593 virtual status_t start(AudioSystem::sync_event_t event, int triggerSession);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 virtual void stop();
1595
Glenn Kasten510a3d62012-07-16 14:24:34 -07001596 void destroy();
1597
Glenn Kasten04270da2012-07-10 12:55:49 -07001598 // clear the buffer overflow flag
1599 void clearOverflow() { mOverflow = false; }
1600 // set the buffer overflow flag and return previous value
Mathias Agopian65ab4712010-07-14 17:59:35 -07001601 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
1602
Glenn Kasten510a3d62012-07-16 14:24:34 -07001603 static void appendDumpHeader(String8& result);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604 void dump(char* buffer, size_t size);
Eric Laurent59255e42011-07-27 19:49:51 -07001605
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606 private:
Glenn Kasten19986612012-03-09 12:07:30 -08001607 friend class AudioFlinger; // for mState
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608
1609 RecordTrack(const RecordTrack&);
1610 RecordTrack& operator = (const RecordTrack&);
1611
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08001612 // AudioBufferProvider interface
1613 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts = kInvalidPTS);
1614 // releaseBuffer() not overridden
Mathias Agopian65ab4712010-07-14 17:59:35 -07001615
Glenn Kasten04270da2012-07-10 12:55:49 -07001616 bool mOverflow; // overflow on most recent attempt to fill client buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617 };
1618
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 RecordThread(const sp<AudioFlinger>& audioFlinger,
Dima Zavin799a70e2011-04-18 16:57:27 -07001620 AudioStreamIn *input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001621 uint32_t sampleRate,
Glenn Kasten254af182012-07-03 14:59:05 -07001622 audio_channel_mask_t channelMask,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001623 audio_io_handle_t id,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001624 audio_devices_t device);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001625 virtual ~RecordThread();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626
Glenn Kasten510a3d62012-07-16 14:24:34 -07001627 // no addTrack_l ?
1628 void destroyTrack_l(const sp<RecordTrack>& track);
1629 void removeTrack_l(const sp<RecordTrack>& track);
1630
1631 void dumpInternals(int fd, const Vector<String16>& args);
1632 void dumpTracks(int fd, const Vector<String16>& args);
1633
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001634 // Thread virtuals
Mathias Agopian65ab4712010-07-14 17:59:35 -07001635 virtual bool threadLoop();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001636 virtual status_t readyToRun();
Glenn Kasten000f0e32012-03-01 17:10:56 -08001637
1638 // RefBase
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 virtual void onFirstRef();
1640
Glenn Kastene0feee32011-12-13 11:53:26 -08001641 virtual status_t initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001642 sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l(
1643 const sp<AudioFlinger::Client>& client,
1644 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001645 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001646 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001647 int frameCount,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001648 int sessionId,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001649 IAudioFlinger::track_flags_t flags,
1650 pid_t tid,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001651 status_t *status);
1652
Glenn Kasten3acbd052012-02-28 10:39:56 -08001653 status_t start(RecordTrack* recordTrack,
Eric Laurenta011e352012-03-29 15:51:43 -07001654 AudioSystem::sync_event_t event,
1655 int triggerSession);
Glenn Kasten1d491ff2012-07-16 14:28:13 -07001656
1657 // ask the thread to stop the specified track, and
1658 // return true if the caller should then do it's part of the stopping process
1659 bool stop_l(RecordTrack* recordTrack);
1660
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001661 void dump(int fd, const Vector<String16>& args);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001662 AudioStreamIn* clearInput();
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08001663 virtual audio_stream_t* stream() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08001665 // AudioBufferProvider interface
1666 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08001668
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669 virtual bool checkForNewParameters_l();
1670 virtual String8 getParameters(const String8& keys);
1671 virtual void audioConfigChanged_l(int event, int param = 0);
1672 void readInputParameters();
1673 virtual unsigned int getInputFramesLost();
1674
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001675 virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1676 virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
Glenn Kasten106e8a42012-08-02 13:37:12 -07001677 virtual uint32_t hasAudioSession(int sessionId) const;
Glenn Kasten510a3d62012-07-16 14:24:34 -07001678
1679 // Return the set of unique session IDs across all tracks.
1680 // The keys are the session IDs, and the associated values are meaningless.
1681 // FIXME replace by Set [and implement Bag/Multiset for other uses].
Glenn Kasten106e8a42012-08-02 13:37:12 -07001682 KeyedVector<int, bool> sessionIds() const;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001683
Eric Laurenta011e352012-03-29 15:51:43 -07001684 virtual status_t setSyncEvent(const sp<SyncEvent>& event);
Glenn Kasten106e8a42012-08-02 13:37:12 -07001685 virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const;
Eric Laurenta011e352012-03-29 15:51:43 -07001686
1687 static void syncStartEventCallback(const wp<SyncEvent>& event);
1688 void handleSyncStartEvent(const sp<SyncEvent>& event);
1689
Mathias Agopian65ab4712010-07-14 17:59:35 -07001690 private:
Eric Laurenta011e352012-03-29 15:51:43 -07001691 void clearSyncStartEvent();
1692
Glenn Kastene4e2a372012-07-23 12:55:09 -07001693 // Enter standby if not already in standby, and set mStandby flag
1694 void standby();
1695
1696 // Call the HAL standby method unconditionally, and don't change mStandby flag
1697 void inputStandBy();
1698
Dima Zavin799a70e2011-04-18 16:57:27 -07001699 AudioStreamIn *mInput;
Glenn Kasten510a3d62012-07-16 14:24:34 -07001700 SortedVector < sp<RecordTrack> > mTracks;
1701 // mActiveTrack has dual roles: it indicates the current active track, and
1702 // is used together with mStartStopCond to indicate start()/stop() progress
Mathias Agopian65ab4712010-07-14 17:59:35 -07001703 sp<RecordTrack> mActiveTrack;
1704 Condition mStartStopCond;
1705 AudioResampler *mResampler;
1706 int32_t *mRsmpOutBuffer;
1707 int16_t *mRsmpInBuffer;
1708 size_t mRsmpInIndex;
1709 size_t mInputBytes;
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001710 const int mReqChannelCount;
1711 const uint32_t mReqSampleRate;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001712 ssize_t mBytesRead;
Eric Laurenta011e352012-03-29 15:51:43 -07001713 // sync event triggering actual audio capture. Frames read before this event will
1714 // be dropped and therefore not read by the application.
1715 sp<SyncEvent> mSyncStartEvent;
1716 // number of captured frames to drop after the start sync event has been received.
Eric Laurent29864602012-05-08 18:57:51 -07001717 // when < 0, maximum frames to drop before starting capture even if sync event is
1718 // not received
Eric Laurenta011e352012-03-29 15:51:43 -07001719 ssize_t mFramestoDrop;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720 };
1721
Glenn Kasten99e53b82012-01-19 08:59:58 -08001722 // server side of the client's IAudioRecord
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723 class RecordHandle : public android::BnAudioRecord {
1724 public:
1725 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
1726 virtual ~RecordHandle();
Glenn Kasten90716c52012-01-26 13:40:12 -08001727 virtual sp<IMemory> getCblk() const;
Glenn Kasten0ec23ce2012-07-10 12:56:08 -07001728 virtual status_t start(int /*AudioSystem::sync_event_t*/ event, int triggerSession);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001729 virtual void stop();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730 virtual status_t onTransact(
1731 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1732 private:
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001733 const sp<RecordThread::RecordTrack> mRecordTrack;
Glenn Kastend96c5722012-04-25 13:44:49 -07001734
1735 // for use from destructor
1736 void stop_nonvirtual();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001737 };
1738
1739 //--- Audio Effect Management
1740
1741 // EffectModule and EffectChain classes both have their own mutex to protect
1742 // state changes or resource modifications. Always respect the following order
1743 // if multiple mutexes must be acquired to avoid cross deadlock:
1744 // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1745
1746 // The EffectModule class is a wrapper object controlling the effect engine implementation
1747 // in the effect library. It prevents concurrent calls to process() and command() functions
1748 // from different client threads. It keeps a list of EffectHandle objects corresponding
1749 // to all client applications using this effect and notifies applications of effect state,
1750 // control or parameter changes. It manages the activation state machine to send appropriate
1751 // reset, enable, disable commands to effect engine and provide volume
1752 // ramping when effects are activated/deactivated.
1753 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1754 // the attached track(s) to accumulate their auxiliary channel.
1755 class EffectModule: public RefBase {
1756 public:
Glenn Kasten9eaa5572012-01-20 13:32:16 -08001757 EffectModule(ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001758 const wp<AudioFlinger::EffectChain>& chain,
1759 effect_descriptor_t *desc,
1760 int id,
1761 int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001762 virtual ~EffectModule();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763
1764 enum effect_state {
1765 IDLE,
1766 RESTART,
1767 STARTING,
1768 ACTIVE,
1769 STOPPING,
Eric Laurentec437d82011-07-26 20:54:46 -07001770 STOPPED,
1771 DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772 };
1773
Glenn Kastenc59c0042012-02-02 14:06:11 -08001774 int id() const { return mId; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775 void process();
1776 void updateState();
Eric Laurent25f43952010-07-28 05:40:18 -07001777 status_t command(uint32_t cmdCode,
1778 uint32_t cmdSize,
1779 void *pCmdData,
1780 uint32_t *replySize,
1781 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782
1783 void reset_l();
vivek mehta2192bc92012-12-04 11:46:08 -08001784#ifdef QCOM_HARDWARE
1785 status_t configure(bool isForLPA = false,
1786 int sampleRate = 0,
1787 int channelCount = 0,
1788 int frameCount = 0);
1789#else
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790 status_t configure();
vivek mehta2192bc92012-12-04 11:46:08 -08001791#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792 status_t init();
Glenn Kasten28243dd2012-01-26 13:43:46 -08001793 effect_state state() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001794 return mState;
1795 }
1796 uint32_t status() {
1797 return mStatus;
1798 }
Glenn Kastenc59c0042012-02-02 14:06:11 -08001799 int sessionId() const {
Eric Laurentde070132010-07-13 04:45:46 -07001800 return mSessionId;
1801 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001802 status_t setEnabled(bool enabled);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001803 status_t setEnabled_l(bool enabled);
Glenn Kastenc59c0042012-02-02 14:06:11 -08001804 bool isEnabled() const;
1805 bool isProcessEnabled() const;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806
1807 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1808 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1809 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1810 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
Eric Laurentde070132010-07-13 04:45:46 -07001811 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
1812 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001813 const wp<ThreadBase>& thread() { return mThread; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001814
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001815 status_t addHandle(EffectHandle *handle);
1816 size_t disconnect(EffectHandle *handle, bool unpinIfLast);
1817 size_t removeHandle(EffectHandle *handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001818
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001819 const effect_descriptor_t& desc() const { return mDescriptor; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820 wp<EffectChain>& chain() { return mChain; }
1821
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001822 status_t setDevice(audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001823 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
Glenn Kastenf78aee72012-01-04 11:00:47 -08001824 status_t setMode(audio_mode_t mode);
Eric Laurent57b2dd12012-08-31 17:44:06 -07001825 status_t setAudioSource(audio_source_t source);
Eric Laurentec35a142011-10-05 17:42:25 -07001826 status_t start();
Eric Laurentec437d82011-07-26 20:54:46 -07001827 status_t stop();
Eric Laurent59255e42011-07-27 19:49:51 -07001828 void setSuspended(bool suspended);
Glenn Kastena3a85482012-01-04 11:01:11 -08001829 bool suspended() const;
Eric Laurent59255e42011-07-27 19:49:51 -07001830
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001831 EffectHandle* controlHandle_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001832
Glenn Kastenc59c0042012-02-02 14:06:11 -08001833 bool isPinned() const { return mPinned; }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001834 void unPin() { mPinned = false; }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001835 bool purgeHandles();
1836 void lock() { mLock.lock(); }
1837 void unlock() { mLock.unlock(); }
vivek mehta2192bc92012-12-04 11:46:08 -08001838#ifdef QCOM_HARDWARE
1839 bool isOnLPA() { return mIsForLPA;}
1840 void setLPAFlag(bool isForLPA) {mIsForLPA = isForLPA; }
1841#endif
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001842 void dump(int fd, const Vector<String16>& args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843
1844 protected:
Glenn Kasten19986612012-03-09 12:07:30 -08001845 friend class AudioFlinger; // for mHandles
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001846 bool mPinned;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847
1848 // Maximum time allocated to effect engines to complete the turn off sequence
1849 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1850
1851 EffectModule(const EffectModule&);
1852 EffectModule& operator = (const EffectModule&);
1853
1854 status_t start_l();
1855 status_t stop_l();
1856
Glenn Kastena3a85482012-01-04 11:01:11 -08001857mutable Mutex mLock; // mutex for process, commands and handles list protection
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858 wp<ThreadBase> mThread; // parent thread
1859 wp<EffectChain> mChain; // parent effect chain
Glenn Kasten415fa752012-07-02 16:11:18 -07001860 const int mId; // this instance unique ID
1861 const int mSessionId; // audio session ID
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001862 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863 effect_config_t mConfig; // input and output audio configuration
Eric Laurente1315cf2011-05-17 19:16:02 -07001864 effect_handle_t mEffectInterface; // Effect module C API
Glenn Kasten28243dd2012-01-26 13:43:46 -08001865 status_t mStatus; // initialization status
1866 effect_state mState; // current activation state
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001867 Vector<EffectHandle *> mHandles; // list of client handles
Glenn Kasten99e53b82012-01-19 08:59:58 -08001868 // First handle in mHandles has highest priority and controls the effect module
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
1870 // sending disable command.
1871 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent59255e42011-07-27 19:49:51 -07001872 bool mSuspended; // effect is suspended: temporarily disabled by framework
vivek mehta2192bc92012-12-04 11:46:08 -08001873#ifdef QCOM_HARDWARE
1874 bool mIsForLPA;
1875#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001876 };
1877
1878 // The EffectHandle class implements the IEffect interface. It provides resources
1879 // to receive parameter updates, keeps track of effect control
1880 // ownership and state and has a pointer to the EffectModule object it is controlling.
1881 // There is one EffectHandle object for each application controlling (or using)
1882 // an effect module.
1883 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1884 class EffectHandle: public android::BnEffect {
1885 public:
1886
1887 EffectHandle(const sp<EffectModule>& effect,
1888 const sp<AudioFlinger::Client>& client,
1889 const sp<IEffectClient>& effectClient,
1890 int32_t priority);
1891 virtual ~EffectHandle();
1892
1893 // IEffect
1894 virtual status_t enable();
1895 virtual status_t disable();
Eric Laurent25f43952010-07-28 05:40:18 -07001896 virtual status_t command(uint32_t cmdCode,
1897 uint32_t cmdSize,
1898 void *pCmdData,
1899 uint32_t *replySize,
1900 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 virtual void disconnect();
Glenn Kasten58123c32012-02-03 10:32:24 -08001902 private:
1903 void disconnect(bool unpinIfLast);
1904 public:
Glenn Kastenc59c0042012-02-02 14:06:11 -08001905 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001906 virtual status_t onTransact(uint32_t code, const Parcel& data,
1907 Parcel* reply, uint32_t flags);
1908
1909
1910 // Give or take control of effect module
Eric Laurent59255e42011-07-27 19:49:51 -07001911 // - hasControl: true if control is given, false if removed
1912 // - signal: true client app should be signaled of change, false otherwise
1913 // - enabled: state of the effect when control is passed
1914 void setControl(bool hasControl, bool signal, bool enabled);
Eric Laurent25f43952010-07-28 05:40:18 -07001915 void commandExecuted(uint32_t cmdCode,
1916 uint32_t cmdSize,
1917 void *pCmdData,
1918 uint32_t replySize,
1919 void *pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920 void setEnabled(bool enabled);
Glenn Kastenc59c0042012-02-02 14:06:11 -08001921 bool enabled() const { return mEnabled; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922
1923 // Getters
Glenn Kastenc59c0042012-02-02 14:06:11 -08001924 int id() const { return mEffect->id(); }
1925 int priority() const { return mPriority; }
1926 bool hasControl() const { return mHasControl; }
1927 sp<EffectModule> effect() const { return mEffect; }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001928 // destroyed_l() must be called with the associated EffectModule mLock held
1929 bool destroyed_l() const { return mDestroyed; }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930
1931 void dump(char* buffer, size_t size);
1932
1933 protected:
Glenn Kasten19986612012-03-09 12:07:30 -08001934 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 EffectHandle(const EffectHandle&);
1936 EffectHandle& operator =(const EffectHandle&);
1937
1938 sp<EffectModule> mEffect; // pointer to controlled EffectModule
1939 sp<IEffectClient> mEffectClient; // callback interface for client notifications
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001940 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 sp<IMemory> mCblkMemory; // shared memory for control block
1942 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory
1943 uint8_t* mBuffer; // pointer to parameter area in shared memory
1944 int mPriority; // client application priority to control the effect
1945 bool mHasControl; // true if this handle is controlling the effect
Eric Laurent59255e42011-07-27 19:49:51 -07001946 bool mEnabled; // cached enable state: needed when the effect is
1947 // restored after being suspended
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001948 bool mDestroyed; // Set to true by destructor. Access with EffectModule
1949 // mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 };
1951
1952 // the EffectChain class represents a group of effects associated to one audio session.
1953 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1954 // The EffecChain with session ID 0 contains global effects applied to the output mix.
1955 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1956 // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1957 // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1958 // input buffer used by the track as accumulation buffer.
1959 class EffectChain: public RefBase {
1960 public:
1961 EffectChain(const wp<ThreadBase>& wThread, int sessionId);
Glenn Kasten9eaa5572012-01-20 13:32:16 -08001962 EffectChain(ThreadBase *thread, int sessionId);
Glenn Kastenc19e2242012-01-30 14:54:39 -08001963 virtual ~EffectChain();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001964
Eric Laurent59255e42011-07-27 19:49:51 -07001965 // special key used for an entry in mSuspendedEffects keyed vector
1966 // corresponding to a suspend all request.
1967 static const int kKeyForSuspendAll = 0;
1968
Eric Laurent544fe9b2011-11-11 15:42:52 -08001969 // minimum duration during which we force calling effect process when last track on
1970 // a session is stopped or removed to allow effect tail to be rendered
1971 static const int kProcessTailDurationMs = 1000;
1972
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973 void process_l();
1974
1975 void lock() {
1976 mLock.lock();
1977 }
1978 void unlock() {
1979 mLock.unlock();
1980 }
1981
Eric Laurentde070132010-07-13 04:45:46 -07001982 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentcab11242010-07-15 12:50:15 -07001983 size_t removeEffect_l(const sp<EffectModule>& handle);
vivek mehta2192bc92012-12-04 11:46:08 -08001984#ifdef QCOM_HARDWARE
1985 size_t getNumEffects() { return mEffects.size(); }
1986#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987
Glenn Kastenc59c0042012-02-02 14:06:11 -08001988 int sessionId() const { return mSessionId; }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001989 void setSessionId(int sessionId) { mSessionId = sessionId; }
Eric Laurentde070132010-07-13 04:45:46 -07001990
Eric Laurentcab11242010-07-15 12:50:15 -07001991 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1992 sp<EffectModule> getEffectFromId_l(int id);
vivek mehta2192bc92012-12-04 11:46:08 -08001993#ifdef QCOM_HARDWARE
1994 sp<EffectModule> getEffectFromIndex_l(int idx);
1995#endif
Eric Laurent59255e42011-07-27 19:49:51 -07001996 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurentcab11242010-07-15 12:50:15 -07001997 bool setVolume_l(uint32_t *left, uint32_t *right);
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001998 void setDevice_l(audio_devices_t device);
Glenn Kastenf78aee72012-01-04 11:00:47 -08001999 void setMode_l(audio_mode_t mode);
Eric Laurent57b2dd12012-08-31 17:44:06 -07002000 void setAudioSource_l(audio_source_t source);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001
Mathias Agopian65ab4712010-07-14 17:59:35 -07002002 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
2003 mInBuffer = buffer;
2004 mOwnInBuffer = ownsBuffer;
2005 }
Glenn Kastenc59c0042012-02-02 14:06:11 -08002006 int16_t *inBuffer() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002007 return mInBuffer;
2008 }
2009 void setOutBuffer(int16_t *buffer) {
2010 mOutBuffer = buffer;
2011 }
Glenn Kastenc59c0042012-02-02 14:06:11 -08002012 int16_t *outBuffer() const {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002013 return mOutBuffer;
2014 }
2015
Eric Laurentb469b942011-05-09 12:09:06 -07002016 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
2017 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
Glenn Kasten2df8f552012-07-09 16:46:23 -07002018 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
Eric Laurentb469b942011-05-09 12:09:06 -07002019
Eric Laurent544fe9b2011-11-11 15:42:52 -08002020 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
2021 mTailBufferCount = mMaxTailBuffers; }
Eric Laurentb469b942011-05-09 12:09:06 -07002022 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
Glenn Kasten2df8f552012-07-09 16:46:23 -07002023 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024
Glenn Kastenc59c0042012-02-02 14:06:11 -08002025 uint32_t strategy() const { return mStrategy; }
Eric Laurentde070132010-07-13 04:45:46 -07002026 void setStrategy(uint32_t strategy)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002027 { mStrategy = strategy; }
Eric Laurentde070132010-07-13 04:45:46 -07002028
Eric Laurent59255e42011-07-27 19:49:51 -07002029 // suspend effect of the given type
2030 void setEffectSuspended_l(const effect_uuid_t *type,
2031 bool suspend);
2032 // suspend all eligible effects
2033 void setEffectSuspendedAll_l(bool suspend);
2034 // check if effects should be suspend or restored when a given effect is enable or disabled
Eric Laurenta85a74a2011-10-19 11:44:54 -07002035 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
Eric Laurent59255e42011-07-27 19:49:51 -07002036 bool enabled);
2037
Eric Laurent91b14c42012-05-30 12:30:29 -07002038 void clearInputBuffer();
2039
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07002040 void dump(int fd, const Vector<String16>& args);
vivek mehta2192bc92012-12-04 11:46:08 -08002041#ifdef QCOM_HARDWARE
2042 bool isForLPATrack() {return mIsForLPATrack; }
2043 void setLPAFlag(bool flag) {mIsForLPATrack = flag;}
2044#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07002045
2046 protected:
Glenn Kasten19986612012-03-09 12:07:30 -08002047 friend class AudioFlinger; // for mThread, mEffects
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048 EffectChain(const EffectChain&);
2049 EffectChain& operator =(const EffectChain&);
2050
Eric Laurent59255e42011-07-27 19:49:51 -07002051 class SuspendedEffectDesc : public RefBase {
2052 public:
2053 SuspendedEffectDesc() : mRefCount(0) {}
2054
2055 int mRefCount;
2056 effect_uuid_t mType;
2057 wp<EffectModule> mEffect;
2058 };
2059
2060 // get a list of effect modules to suspend when an effect of the type
2061 // passed is enabled.
Glenn Kastend0539712012-01-30 12:56:03 -08002062 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
2063
Eric Laurent59255e42011-07-27 19:49:51 -07002064 // get an effect module if it is currently enable
2065 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurentdb7c0792011-08-10 10:37:50 -07002066 // true if the effect whose descriptor is passed can be suspended
2067 // OEMs can modify the rules implemented in this method to exclude specific effect
2068 // types or implementations from the suspend/restore mechanism.
2069 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
Eric Laurent59255e42011-07-27 19:49:51 -07002070
Eric Laurent91b14c42012-05-30 12:30:29 -07002071 void clearInputBuffer_l(sp<ThreadBase> thread);
2072
Mathias Agopian65ab4712010-07-14 17:59:35 -07002073 wp<ThreadBase> mThread; // parent mixer thread
2074 Mutex mLock; // mutex protecting effect list
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002075 Vector< sp<EffectModule> > mEffects; // list of effect modules
Mathias Agopian65ab4712010-07-14 17:59:35 -07002076 int mSessionId; // audio session ID
2077 int16_t *mInBuffer; // chain input buffer
2078 int16_t *mOutBuffer; // chain output buffer
Glenn Kasten2df8f552012-07-09 16:46:23 -07002079
2080 // 'volatile' here means these are accessed with atomic operations instead of mutex
2081 volatile int32_t mActiveTrackCnt; // number of active tracks connected
2082 volatile int32_t mTrackCnt; // number of tracks connected
2083
Eric Laurent544fe9b2011-11-11 15:42:52 -08002084 int32_t mTailBufferCount; // current effect tail buffer count
2085 int32_t mMaxTailBuffers; // maximum effect tail buffers
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086 bool mOwnInBuffer; // true if the chain owns its input buffer
Eric Laurentcab11242010-07-15 12:50:15 -07002087 int mVolumeCtrlIdx; // index of insert effect having control over volume
2088 uint32_t mLeftVolume; // previous volume on left channel
2089 uint32_t mRightVolume; // previous volume on right channel
Eric Laurentf997cab2010-07-19 06:24:46 -07002090 uint32_t mNewLeftVolume; // new volume on left channel
2091 uint32_t mNewRightVolume; // new volume on right channel
Eric Laurentde070132010-07-13 04:45:46 -07002092 uint32_t mStrategy; // strategy for this effect chain
vivek mehta2192bc92012-12-04 11:46:08 -08002093#ifdef QCOM_HARDWARE
2094 bool mIsForLPATrack;
2095#endif
Glenn Kasten17a736c2012-02-14 08:52:15 -08002096 // mSuspendedEffects lists all effects currently suspended in the chain.
2097 // Use effect type UUID timelow field as key. There is no real risk of identical
Eric Laurent59255e42011-07-27 19:49:51 -07002098 // timeLow fields among effect type UUIDs.
Glenn Kasten17a736c2012-02-14 08:52:15 -08002099 // Updated by updateSuspendedSessions_l() only.
Eric Laurent59255e42011-07-27 19:49:51 -07002100 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 };
2102
John Grossmanee578c02012-07-23 17:05:46 -07002103 class AudioHwDevice {
2104 public:
2105 enum Flags {
2106 AHWD_CAN_SET_MASTER_VOLUME = 0x1,
2107 AHWD_CAN_SET_MASTER_MUTE = 0x2,
2108 };
2109
2110 AudioHwDevice(const char *moduleName,
2111 audio_hw_device_t *hwDevice,
2112 Flags flags)
2113 : mModuleName(strdup(moduleName))
2114 , mHwDevice(hwDevice)
2115 , mFlags(flags) { }
2116 /*virtual*/ ~AudioHwDevice() { free((void *)mModuleName); }
2117
2118 bool canSetMasterVolume() const {
2119 return (0 != (mFlags & AHWD_CAN_SET_MASTER_VOLUME));
2120 }
2121
2122 bool canSetMasterMute() const {
2123 return (0 != (mFlags & AHWD_CAN_SET_MASTER_MUTE));
2124 }
2125
2126 const char *moduleName() const { return mModuleName; }
2127 audio_hw_device_t *hwDevice() const { return mHwDevice; }
2128 private:
2129 const char * const mModuleName;
2130 audio_hw_device_t * const mHwDevice;
2131 Flags mFlags;
2132 };
2133
Glenn Kastenaed850d2012-01-26 09:46:34 -08002134 // AudioStreamOut and AudioStreamIn are immutable, so their fields are const.
2135 // For emphasis, we could also make all pointers to them be "const *",
2136 // but that would clutter the code unnecessarily.
2137
Dima Zavin799a70e2011-04-18 16:57:27 -07002138 struct AudioStreamOut {
John Grossmanee578c02012-07-23 17:05:46 -07002139 AudioHwDevice* const audioHwDev;
Glenn Kastenaed850d2012-01-26 09:46:34 -08002140 audio_stream_out_t* const stream;
Dima Zavin799a70e2011-04-18 16:57:27 -07002141
John Grossmanee578c02012-07-23 17:05:46 -07002142 audio_hw_device_t* hwDev() const { return audioHwDev->hwDevice(); }
2143
2144 AudioStreamOut(AudioHwDevice *dev, audio_stream_out_t *out) :
2145 audioHwDev(dev), stream(out) {}
Dima Zavin799a70e2011-04-18 16:57:27 -07002146 };
2147
2148 struct AudioStreamIn {
John Grossmanee578c02012-07-23 17:05:46 -07002149 AudioHwDevice* const audioHwDev;
Glenn Kastenaed850d2012-01-26 09:46:34 -08002150 audio_stream_in_t* const stream;
Dima Zavin799a70e2011-04-18 16:57:27 -07002151
John Grossmanee578c02012-07-23 17:05:46 -07002152 audio_hw_device_t* hwDev() const { return audioHwDev->hwDevice(); }
2153
2154 AudioStreamIn(AudioHwDevice *dev, audio_stream_in_t *in) :
2155 audioHwDev(dev), stream(in) {}
Dima Zavin799a70e2011-04-18 16:57:27 -07002156 };
vivek mehta2192bc92012-12-04 11:46:08 -08002157#ifdef QCOM_HARDWARE
2158 struct AudioSessionDescriptor {
2159 bool mActive;
2160 int mStreamType;
2161 float mVolumeLeft;
2162 float mVolumeRight;
2163 audio_hw_device_t *hwDev;
2164 audio_stream_out_t *stream;
2165 audio_output_flags_t flag;
2166 void *trackRefPtr;
2167 audio_devices_t device;
2168 AudioSessionDescriptor(audio_hw_device_t *dev, audio_stream_out_t *out, audio_output_flags_t outflag) :
2169 hwDev(dev), stream(out), flag(outflag) {}
2170 };
2171#endif
Glenn Kasten99e53b82012-01-19 08:59:58 -08002172 // for mAudioSessionRefs only
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002173 struct AudioSessionRef {
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002174 AudioSessionRef(int sessionid, pid_t pid) :
2175 mSessionid(sessionid), mPid(pid), mCnt(1) {}
2176 const int mSessionid;
2177 const pid_t mPid;
2178 int mCnt;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002179 };
2180
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181 mutable Mutex mLock;
2182
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002183 DefaultKeyedVector< pid_t, wp<Client> > mClients; // see ~Client()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184
2185 mutable Mutex mHardwareLock;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002186 // NOTE: If both mLock and mHardwareLock mutexes must be held,
2187 // always take mLock before mHardwareLock
Glenn Kasten2b213bc2012-02-02 14:05:20 -08002188
2189 // These two fields are immutable after onFirstRef(), so no lock needed to access
John Grossmanee578c02012-07-23 17:05:46 -07002190 AudioHwDevice* mPrimaryHardwareDev; // mAudioHwDevs[0] or NULL
Eric Laurenta4c5a552012-03-29 10:12:40 -07002191 DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*> mAudioHwDevs;
Glenn Kasten2f732eb2012-01-26 09:48:03 -08002192
Glenn Kasten8abf44d2012-02-02 14:16:03 -08002193 // for dump, indicates which hardware operation is currently in progress (but not stream ops)
Glenn Kasten2f732eb2012-01-26 09:48:03 -08002194 enum hardware_call_state {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08002195 AUDIO_HW_IDLE = 0, // no operation in progress
2196 AUDIO_HW_INIT, // init_check
2197 AUDIO_HW_OUTPUT_OPEN, // open_output_stream
2198 AUDIO_HW_OUTPUT_CLOSE, // unused
2199 AUDIO_HW_INPUT_OPEN, // unused
2200 AUDIO_HW_INPUT_CLOSE, // unused
2201 AUDIO_HW_STANDBY, // unused
2202 AUDIO_HW_SET_MASTER_VOLUME, // set_master_volume
2203 AUDIO_HW_GET_ROUTING, // unused
2204 AUDIO_HW_SET_ROUTING, // unused
2205 AUDIO_HW_GET_MODE, // unused
2206 AUDIO_HW_SET_MODE, // set_mode
2207 AUDIO_HW_GET_MIC_MUTE, // get_mic_mute
2208 AUDIO_HW_SET_MIC_MUTE, // set_mic_mute
2209 AUDIO_HW_SET_VOICE_VOLUME, // set_voice_volume
2210 AUDIO_HW_SET_PARAMETER, // set_parameters
2211 AUDIO_HW_GET_INPUT_BUFFER_SIZE, // get_input_buffer_size
2212 AUDIO_HW_GET_MASTER_VOLUME, // get_master_volume
2213 AUDIO_HW_GET_PARAMETER, // get_parameters
John Grossmand8f178d2012-07-20 14:51:35 -07002214 AUDIO_HW_SET_MASTER_MUTE, // set_master_mute
2215 AUDIO_HW_GET_MASTER_MUTE, // get_master_mute
Glenn Kasten2f732eb2012-01-26 09:48:03 -08002216 };
2217
Glenn Kastena4454b42012-01-04 11:02:33 -08002218 mutable hardware_call_state mHardwareStatus; // for dump only
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219
2220
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002221 DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> > mPlaybackThreads;
Glenn Kastenb7bf7962012-02-08 12:36:25 -08002222 stream_type_t mStreamTypes[AUDIO_STREAM_CNT];
Glenn Kasten98067102011-12-13 11:47:54 -08002223
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07002224 // member variables below are protected by mLock
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 float mMasterVolume;
2226 bool mMasterMute;
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07002227 // end of variables protected by mLock
Mathias Agopian65ab4712010-07-14 17:59:35 -07002228
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002229 DefaultKeyedVector< audio_io_handle_t, sp<RecordThread> > mRecordThreads;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002230
vivek mehta2192bc92012-12-04 11:46:08 -08002231 DefaultKeyedVector< sp<IBinder>, sp<NotificationClient> > mNotificationClients;
Glenn Kasten99e53b82012-01-19 08:59:58 -08002232 volatile int32_t mNextUniqueId; // updated by android_atomic_inc
Glenn Kastenf78aee72012-01-04 11:00:47 -08002233 audio_mode_t mMode;
Eric Laurentbee53372011-08-29 12:42:48 -07002234 bool mBtNrecIsOff;
vivek mehta2192bc92012-12-04 11:46:08 -08002235#ifdef QCOM_HARDWARE
2236 DefaultKeyedVector<audio_io_handle_t, AudioSessionDescriptor *> mDirectAudioTracks;
2237#endif
Glenn Kasten99e53b82012-01-19 08:59:58 -08002238 // protected by mLock
vivek mehta2192bc92012-12-04 11:46:08 -08002239#ifdef QCOM_HARDWARE
2240 volatile bool mIsEffectConfigChanged;
2241#endif
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002242 Vector<AudioSessionRef*> mAudioSessionRefs;
vivek mehta2192bc92012-12-04 11:46:08 -08002243#ifdef QCOM_HARDWARE
2244 sp<EffectChain> mLPAEffectChain;
2245 int mLPASessionId;
2246 int mLPASampleRate;
2247 int mLPANumChannels;
2248 volatile bool mAllChainsLocked;
2249#endif
John Grossman4ff14ba2012-02-08 16:37:41 -08002250 float masterVolume_l() const;
John Grossmand8f178d2012-07-20 14:51:35 -07002251 bool masterMute_l() const;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002252 audio_module_handle_t loadHwModule_l(const char *name);
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002253
Eric Laurenta011e352012-03-29 15:51:43 -07002254 Vector < sp<SyncEvent> > mPendingSyncEvents; // sync events awaiting for a session
2255 // to be created
2256
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002257private:
2258 sp<Client> registerPid_l(pid_t pid); // always returns non-0
2259
Glenn Kastend96c5722012-04-25 13:44:49 -07002260 // for use from destructor
2261 status_t closeOutput_nonvirtual(audio_io_handle_t output);
2262 status_t closeInput_nonvirtual(audio_io_handle_t input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263};
2264
Dima Zavin799a70e2011-04-18 16:57:27 -07002265
Mathias Agopian65ab4712010-07-14 17:59:35 -07002266// ----------------------------------------------------------------------------
2267
2268}; // namespace android
2269
2270#endif // ANDROID_AUDIO_FLINGER_H