Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* //device/include/server/AudioFlinger/AudioFlinger.h |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_AUDIO_FLINGER_H |
| 19 | #define ANDROID_AUDIO_FLINGER_H |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <limits.h> |
| 24 | |
| 25 | #include <media/IAudioFlinger.h> |
| 26 | #include <media/IAudioFlingerClient.h> |
| 27 | #include <media/IAudioTrack.h> |
| 28 | #include <media/IAudioRecord.h> |
| 29 | #include <media/AudioTrack.h> |
| 30 | |
| 31 | #include <utils/Atomic.h> |
| 32 | #include <utils/Errors.h> |
| 33 | #include <utils/threads.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | #include <utils/SortedVector.h> |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 35 | #include <utils/TypeHelpers.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | #include <utils/Vector.h> |
| 37 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 38 | #include <binder/BinderService.h> |
| 39 | #include <binder/MemoryDealer.h> |
| 40 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame^] | 41 | #include <system/audio.h> |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 42 | #include <hardware/audio_hal.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | |
| 44 | #include "AudioBufferProvider.h" |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | class audio_track_cblk_t; |
| 49 | class effect_param_cblk_t; |
| 50 | class AudioMixer; |
| 51 | class AudioBuffer; |
| 52 | class AudioResampler; |
| 53 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | // ---------------------------------------------------------------------------- |
| 55 | |
| 56 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 57 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 58 | |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | static const nsecs_t kStandbyTimeInNsecs = seconds(3); |
| 63 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 64 | class AudioFlinger : |
| 65 | public BinderService<AudioFlinger>, |
| 66 | public BnAudioFlinger |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | { |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 68 | friend class BinderService<AudioFlinger>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | public: |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 70 | static char const* getServiceName() { return "media.audio_flinger"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 71 | |
| 72 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 73 | |
| 74 | // IAudioFlinger interface |
| 75 | virtual sp<IAudioTrack> createTrack( |
| 76 | pid_t pid, |
| 77 | int streamType, |
| 78 | uint32_t sampleRate, |
| 79 | int format, |
| 80 | int channelCount, |
| 81 | int frameCount, |
| 82 | uint32_t flags, |
| 83 | const sp<IMemory>& sharedBuffer, |
| 84 | int output, |
| 85 | int *sessionId, |
| 86 | status_t *status); |
| 87 | |
| 88 | virtual uint32_t sampleRate(int output) const; |
| 89 | virtual int channelCount(int output) const; |
| 90 | virtual int format(int output) const; |
| 91 | virtual size_t frameCount(int output) const; |
| 92 | virtual uint32_t latency(int output) const; |
| 93 | |
| 94 | virtual status_t setMasterVolume(float value); |
| 95 | virtual status_t setMasterMute(bool muted); |
| 96 | |
| 97 | virtual float masterVolume() const; |
| 98 | virtual bool masterMute() const; |
| 99 | |
| 100 | virtual status_t setStreamVolume(int stream, float value, int output); |
| 101 | virtual status_t setStreamMute(int stream, bool muted); |
| 102 | |
| 103 | virtual float streamVolume(int stream, int output) const; |
| 104 | virtual bool streamMute(int stream) const; |
| 105 | |
| 106 | virtual status_t setMode(int mode); |
| 107 | |
| 108 | virtual status_t setMicMute(bool state); |
| 109 | virtual bool getMicMute() const; |
| 110 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 111 | virtual status_t setParameters(int ioHandle, const String8& keyValuePairs); |
| 112 | virtual String8 getParameters(int ioHandle, const String8& keys); |
| 113 | |
| 114 | virtual void registerClient(const sp<IAudioFlingerClient>& client); |
| 115 | |
| 116 | virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount); |
| 117 | virtual unsigned int getInputFramesLost(int ioHandle); |
| 118 | |
| 119 | virtual int openOutput(uint32_t *pDevices, |
| 120 | uint32_t *pSamplingRate, |
| 121 | uint32_t *pFormat, |
| 122 | uint32_t *pChannels, |
| 123 | uint32_t *pLatencyMs, |
| 124 | uint32_t flags); |
| 125 | |
| 126 | virtual int openDuplicateOutput(int output1, int output2); |
| 127 | |
| 128 | virtual status_t closeOutput(int output); |
| 129 | |
| 130 | virtual status_t suspendOutput(int output); |
| 131 | |
| 132 | virtual status_t restoreOutput(int output); |
| 133 | |
| 134 | virtual int openInput(uint32_t *pDevices, |
| 135 | uint32_t *pSamplingRate, |
| 136 | uint32_t *pFormat, |
| 137 | uint32_t *pChannels, |
| 138 | uint32_t acoustics); |
| 139 | |
| 140 | virtual status_t closeInput(int input); |
| 141 | |
| 142 | virtual status_t setStreamOutput(uint32_t stream, int output); |
| 143 | |
| 144 | virtual status_t setVoiceVolume(float volume); |
| 145 | |
| 146 | virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output); |
| 147 | |
| 148 | virtual int newAudioSessionId(); |
| 149 | |
| 150 | virtual status_t loadEffectLibrary(const char *libPath, int *handle); |
| 151 | |
| 152 | virtual status_t unloadEffectLibrary(int handle); |
| 153 | |
| 154 | virtual status_t queryNumberEffects(uint32_t *numEffects); |
| 155 | |
| 156 | virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor); |
| 157 | |
| 158 | virtual status_t getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor); |
| 159 | |
| 160 | virtual sp<IEffect> createEffect(pid_t pid, |
| 161 | effect_descriptor_t *pDesc, |
| 162 | const sp<IEffectClient>& effectClient, |
| 163 | int32_t priority, |
| 164 | int output, |
| 165 | int sessionId, |
| 166 | status_t *status, |
| 167 | int *id, |
| 168 | int *enabled); |
| 169 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 170 | virtual status_t moveEffects(int session, int srcOutput, int dstOutput); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 171 | |
| 172 | enum hardware_call_state { |
| 173 | AUDIO_HW_IDLE = 0, |
| 174 | AUDIO_HW_INIT, |
| 175 | AUDIO_HW_OUTPUT_OPEN, |
| 176 | AUDIO_HW_OUTPUT_CLOSE, |
| 177 | AUDIO_HW_INPUT_OPEN, |
| 178 | AUDIO_HW_INPUT_CLOSE, |
| 179 | AUDIO_HW_STANDBY, |
| 180 | AUDIO_HW_SET_MASTER_VOLUME, |
| 181 | AUDIO_HW_GET_ROUTING, |
| 182 | AUDIO_HW_SET_ROUTING, |
| 183 | AUDIO_HW_GET_MODE, |
| 184 | AUDIO_HW_SET_MODE, |
| 185 | AUDIO_HW_GET_MIC_MUTE, |
| 186 | AUDIO_HW_SET_MIC_MUTE, |
| 187 | AUDIO_SET_VOICE_VOLUME, |
| 188 | AUDIO_SET_PARAMETER, |
| 189 | }; |
| 190 | |
| 191 | // record interface |
| 192 | virtual sp<IAudioRecord> openRecord( |
| 193 | pid_t pid, |
| 194 | int input, |
| 195 | uint32_t sampleRate, |
| 196 | int format, |
| 197 | int channelCount, |
| 198 | int frameCount, |
| 199 | uint32_t flags, |
| 200 | int *sessionId, |
| 201 | status_t *status); |
| 202 | |
| 203 | virtual status_t onTransact( |
| 204 | uint32_t code, |
| 205 | const Parcel& data, |
| 206 | Parcel* reply, |
| 207 | uint32_t flags); |
| 208 | |
| 209 | uint32_t getMode() { return mMode; } |
| 210 | |
| 211 | private: |
| 212 | AudioFlinger(); |
| 213 | virtual ~AudioFlinger(); |
| 214 | |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 215 | status_t initCheck() const; |
| 216 | virtual void onFirstRef(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 217 | audio_hw_device_t* findSuitableHwDev_l(uint32_t devices); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 218 | |
| 219 | // Internal dump utilites. |
| 220 | status_t dumpPermissionDenial(int fd, const Vector<String16>& args); |
| 221 | status_t dumpClients(int fd, const Vector<String16>& args); |
| 222 | status_t dumpInternals(int fd, const Vector<String16>& args); |
| 223 | |
| 224 | // --- Client --- |
| 225 | class Client : public RefBase { |
| 226 | public: |
| 227 | Client(const sp<AudioFlinger>& audioFlinger, pid_t pid); |
| 228 | virtual ~Client(); |
| 229 | const sp<MemoryDealer>& heap() const; |
| 230 | pid_t pid() const { return mPid; } |
| 231 | sp<AudioFlinger> audioFlinger() { return mAudioFlinger; } |
| 232 | |
| 233 | private: |
| 234 | Client(const Client&); |
| 235 | Client& operator = (const Client&); |
| 236 | sp<AudioFlinger> mAudioFlinger; |
| 237 | sp<MemoryDealer> mMemoryDealer; |
| 238 | pid_t mPid; |
| 239 | }; |
| 240 | |
| 241 | // --- Notification Client --- |
| 242 | class NotificationClient : public IBinder::DeathRecipient { |
| 243 | public: |
| 244 | NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 245 | const sp<IAudioFlingerClient>& client, |
| 246 | pid_t pid); |
| 247 | virtual ~NotificationClient(); |
| 248 | |
| 249 | sp<IAudioFlingerClient> client() { return mClient; } |
| 250 | |
| 251 | // IBinder::DeathRecipient |
| 252 | virtual void binderDied(const wp<IBinder>& who); |
| 253 | |
| 254 | private: |
| 255 | NotificationClient(const NotificationClient&); |
| 256 | NotificationClient& operator = (const NotificationClient&); |
| 257 | |
| 258 | sp<AudioFlinger> mAudioFlinger; |
| 259 | pid_t mPid; |
| 260 | sp<IAudioFlingerClient> mClient; |
| 261 | }; |
| 262 | |
| 263 | class TrackHandle; |
| 264 | class RecordHandle; |
| 265 | class RecordThread; |
| 266 | class PlaybackThread; |
| 267 | class MixerThread; |
| 268 | class DirectOutputThread; |
| 269 | class DuplicatingThread; |
| 270 | class Track; |
| 271 | class RecordTrack; |
| 272 | class EffectModule; |
| 273 | class EffectHandle; |
| 274 | class EffectChain; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 275 | struct AudioStreamOut; |
| 276 | struct AudioStreamIn; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 277 | |
| 278 | class ThreadBase : public Thread { |
| 279 | public: |
| 280 | ThreadBase (const sp<AudioFlinger>& audioFlinger, int id); |
| 281 | virtual ~ThreadBase(); |
| 282 | |
| 283 | status_t dumpBase(int fd, const Vector<String16>& args); |
| 284 | |
| 285 | // base for record and playback |
| 286 | class TrackBase : public AudioBufferProvider, public RefBase { |
| 287 | |
| 288 | public: |
| 289 | enum track_state { |
| 290 | IDLE, |
| 291 | TERMINATED, |
| 292 | STOPPED, |
| 293 | RESUMING, |
| 294 | ACTIVE, |
| 295 | PAUSING, |
| 296 | PAUSED |
| 297 | }; |
| 298 | |
| 299 | enum track_flags { |
| 300 | STEPSERVER_FAILED = 0x01, // StepServer could not acquire cblk->lock mutex |
| 301 | SYSTEM_FLAGS_MASK = 0x0000ffffUL, |
| 302 | // The upper 16 bits are used for track-specific flags. |
| 303 | }; |
| 304 | |
| 305 | TrackBase(const wp<ThreadBase>& thread, |
| 306 | const sp<Client>& client, |
| 307 | uint32_t sampleRate, |
| 308 | int format, |
| 309 | int channelCount, |
| 310 | int frameCount, |
| 311 | uint32_t flags, |
| 312 | const sp<IMemory>& sharedBuffer, |
| 313 | int sessionId); |
| 314 | ~TrackBase(); |
| 315 | |
| 316 | virtual status_t start() = 0; |
| 317 | virtual void stop() = 0; |
| 318 | sp<IMemory> getCblk() const; |
| 319 | audio_track_cblk_t* cblk() const { return mCblk; } |
| 320 | int sessionId() { return mSessionId; } |
| 321 | |
| 322 | protected: |
| 323 | friend class ThreadBase; |
| 324 | friend class RecordHandle; |
| 325 | friend class PlaybackThread; |
| 326 | friend class RecordThread; |
| 327 | friend class MixerThread; |
| 328 | friend class DirectOutputThread; |
| 329 | |
| 330 | TrackBase(const TrackBase&); |
| 331 | TrackBase& operator = (const TrackBase&); |
| 332 | |
| 333 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0; |
| 334 | virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); |
| 335 | |
| 336 | int format() const { |
| 337 | return mFormat; |
| 338 | } |
| 339 | |
| 340 | int channelCount() const ; |
| 341 | |
| 342 | int sampleRate() const; |
| 343 | |
| 344 | void* getBuffer(uint32_t offset, uint32_t frames) const; |
| 345 | |
| 346 | bool isStopped() const { |
| 347 | return mState == STOPPED; |
| 348 | } |
| 349 | |
| 350 | bool isTerminated() const { |
| 351 | return mState == TERMINATED; |
| 352 | } |
| 353 | |
| 354 | bool step(); |
| 355 | void reset(); |
| 356 | |
| 357 | wp<ThreadBase> mThread; |
| 358 | sp<Client> mClient; |
| 359 | sp<IMemory> mCblkMemory; |
| 360 | audio_track_cblk_t* mCblk; |
| 361 | void* mBuffer; |
| 362 | void* mBufferEnd; |
| 363 | uint32_t mFrameCount; |
| 364 | // we don't really need a lock for these |
| 365 | int mState; |
| 366 | int mClientTid; |
| 367 | uint8_t mFormat; |
| 368 | uint32_t mFlags; |
| 369 | int mSessionId; |
| 370 | }; |
| 371 | |
| 372 | class ConfigEvent { |
| 373 | public: |
| 374 | ConfigEvent() : mEvent(0), mParam(0) {} |
| 375 | |
| 376 | int mEvent; |
| 377 | int mParam; |
| 378 | }; |
| 379 | |
| 380 | uint32_t sampleRate() const; |
| 381 | int channelCount() const; |
| 382 | int format() const; |
| 383 | size_t frameCount() const; |
| 384 | void wakeUp() { mWaitWorkCV.broadcast(); } |
| 385 | void exit(); |
| 386 | virtual bool checkForNewParameters_l() = 0; |
| 387 | virtual status_t setParameters(const String8& keyValuePairs); |
| 388 | virtual String8 getParameters(const String8& keys) = 0; |
| 389 | virtual void audioConfigChanged_l(int event, int param = 0) = 0; |
| 390 | void sendConfigEvent(int event, int param = 0); |
| 391 | void sendConfigEvent_l(int event, int param = 0); |
| 392 | void processConfigEvents(); |
| 393 | int id() const { return mId;} |
| 394 | bool standby() { return mStandby; } |
| 395 | |
| 396 | mutable Mutex mLock; |
| 397 | |
| 398 | protected: |
| 399 | |
| 400 | friend class Track; |
| 401 | friend class TrackBase; |
| 402 | friend class PlaybackThread; |
| 403 | friend class MixerThread; |
| 404 | friend class DirectOutputThread; |
| 405 | friend class DuplicatingThread; |
| 406 | friend class RecordThread; |
| 407 | friend class RecordTrack; |
| 408 | |
| 409 | Condition mWaitWorkCV; |
| 410 | sp<AudioFlinger> mAudioFlinger; |
| 411 | uint32_t mSampleRate; |
| 412 | size_t mFrameCount; |
| 413 | uint32_t mChannels; |
| 414 | uint16_t mChannelCount; |
| 415 | uint16_t mFrameSize; |
| 416 | int mFormat; |
| 417 | Condition mParamCond; |
| 418 | Vector<String8> mNewParameters; |
| 419 | status_t mParamStatus; |
| 420 | Vector<ConfigEvent *> mConfigEvents; |
| 421 | bool mStandby; |
| 422 | int mId; |
| 423 | bool mExiting; |
| 424 | }; |
| 425 | |
| 426 | // --- PlaybackThread --- |
| 427 | class PlaybackThread : public ThreadBase { |
| 428 | public: |
| 429 | |
| 430 | enum type { |
| 431 | MIXER, |
| 432 | DIRECT, |
| 433 | DUPLICATING |
| 434 | }; |
| 435 | |
| 436 | enum mixer_state { |
| 437 | MIXER_IDLE, |
| 438 | MIXER_TRACKS_ENABLED, |
| 439 | MIXER_TRACKS_READY |
| 440 | }; |
| 441 | |
| 442 | // playback track |
| 443 | class Track : public TrackBase { |
| 444 | public: |
| 445 | Track( const wp<ThreadBase>& thread, |
| 446 | const sp<Client>& client, |
| 447 | int streamType, |
| 448 | uint32_t sampleRate, |
| 449 | int format, |
| 450 | int channelCount, |
| 451 | int frameCount, |
| 452 | const sp<IMemory>& sharedBuffer, |
| 453 | int sessionId); |
| 454 | ~Track(); |
| 455 | |
| 456 | void dump(char* buffer, size_t size); |
| 457 | virtual status_t start(); |
| 458 | virtual void stop(); |
| 459 | void pause(); |
| 460 | |
| 461 | void flush(); |
| 462 | void destroy(); |
| 463 | void mute(bool); |
| 464 | void setVolume(float left, float right); |
| 465 | int name() const { |
| 466 | return mName; |
| 467 | } |
| 468 | |
| 469 | int type() const { |
| 470 | return mStreamType; |
| 471 | } |
| 472 | status_t attachAuxEffect(int EffectId); |
| 473 | void setAuxBuffer(int EffectId, int32_t *buffer); |
| 474 | int32_t *auxBuffer() { return mAuxBuffer; } |
| 475 | void setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; } |
| 476 | int16_t *mainBuffer() { return mMainBuffer; } |
| 477 | int auxEffectId() { return mAuxEffectId; } |
| 478 | |
| 479 | |
| 480 | protected: |
| 481 | friend class ThreadBase; |
| 482 | friend class AudioFlinger; |
| 483 | friend class TrackHandle; |
| 484 | friend class PlaybackThread; |
| 485 | friend class MixerThread; |
| 486 | friend class DirectOutputThread; |
| 487 | |
| 488 | Track(const Track&); |
| 489 | Track& operator = (const Track&); |
| 490 | |
| 491 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); |
| 492 | bool isMuted() { return mMute; } |
| 493 | bool isPausing() const { |
| 494 | return mState == PAUSING; |
| 495 | } |
| 496 | bool isPaused() const { |
| 497 | return mState == PAUSED; |
| 498 | } |
| 499 | bool isReady() const; |
| 500 | void setPaused() { mState = PAUSED; } |
| 501 | void reset(); |
| 502 | |
| 503 | bool isOutputTrack() const { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 504 | return (mStreamType == AUDIO_STREAM_CNT); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // we don't really need a lock for these |
| 508 | float mVolume[2]; |
| 509 | volatile bool mMute; |
| 510 | // FILLED state is used for suppressing volume ramp at begin of playing |
| 511 | enum {FS_FILLING, FS_FILLED, FS_ACTIVE}; |
| 512 | mutable uint8_t mFillingUpStatus; |
| 513 | int8_t mRetryCount; |
| 514 | sp<IMemory> mSharedBuffer; |
| 515 | bool mResetDone; |
| 516 | int mStreamType; |
| 517 | int mName; |
| 518 | int16_t *mMainBuffer; |
| 519 | int32_t *mAuxBuffer; |
| 520 | int mAuxEffectId; |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 521 | bool mHasVolumeController; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 522 | }; // end of Track |
| 523 | |
| 524 | |
| 525 | // playback track |
| 526 | class OutputTrack : public Track { |
| 527 | public: |
| 528 | |
| 529 | class Buffer: public AudioBufferProvider::Buffer { |
| 530 | public: |
| 531 | int16_t *mBuffer; |
| 532 | }; |
| 533 | |
| 534 | OutputTrack( const wp<ThreadBase>& thread, |
| 535 | DuplicatingThread *sourceThread, |
| 536 | uint32_t sampleRate, |
| 537 | int format, |
| 538 | int channelCount, |
| 539 | int frameCount); |
| 540 | ~OutputTrack(); |
| 541 | |
| 542 | virtual status_t start(); |
| 543 | virtual void stop(); |
| 544 | bool write(int16_t* data, uint32_t frames); |
| 545 | bool bufferQueueEmpty() { return (mBufferQueue.size() == 0) ? true : false; } |
| 546 | bool isActive() { return mActive; } |
| 547 | wp<ThreadBase>& thread() { return mThread; } |
| 548 | |
| 549 | private: |
| 550 | |
| 551 | status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs); |
| 552 | void clearBufferQueue(); |
| 553 | |
| 554 | // Maximum number of pending buffers allocated by OutputTrack::write() |
| 555 | static const uint8_t kMaxOverFlowBuffers = 10; |
| 556 | |
| 557 | Vector < Buffer* > mBufferQueue; |
| 558 | AudioBufferProvider::Buffer mOutBuffer; |
| 559 | bool mActive; |
| 560 | DuplicatingThread* mSourceThread; |
| 561 | }; // end of OutputTrack |
| 562 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 563 | PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 564 | virtual ~PlaybackThread(); |
| 565 | |
| 566 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 567 | |
| 568 | // Thread virtuals |
| 569 | virtual status_t readyToRun(); |
| 570 | virtual void onFirstRef(); |
| 571 | |
| 572 | virtual uint32_t latency() const; |
| 573 | |
| 574 | virtual status_t setMasterVolume(float value); |
| 575 | virtual status_t setMasterMute(bool muted); |
| 576 | |
| 577 | virtual float masterVolume() const; |
| 578 | virtual bool masterMute() const; |
| 579 | |
| 580 | virtual status_t setStreamVolume(int stream, float value); |
| 581 | virtual status_t setStreamMute(int stream, bool muted); |
| 582 | |
| 583 | virtual float streamVolume(int stream) const; |
| 584 | virtual bool streamMute(int stream) const; |
| 585 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | sp<Track> createTrack_l( |
| 587 | const sp<AudioFlinger::Client>& client, |
| 588 | int streamType, |
| 589 | uint32_t sampleRate, |
| 590 | int format, |
| 591 | int channelCount, |
| 592 | int frameCount, |
| 593 | const sp<IMemory>& sharedBuffer, |
| 594 | int sessionId, |
| 595 | status_t *status); |
| 596 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 597 | AudioStreamOut* getOutput() { return mOutput; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 598 | |
| 599 | virtual int type() const { return mType; } |
| 600 | void suspend() { mSuspended++; } |
| 601 | void restore() { if (mSuspended) mSuspended--; } |
| 602 | bool isSuspended() { return (mSuspended != 0); } |
| 603 | virtual String8 getParameters(const String8& keys); |
| 604 | virtual void audioConfigChanged_l(int event, int param = 0); |
| 605 | virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames); |
| 606 | int16_t *mixBuffer() { return mMixBuffer; }; |
| 607 | |
| 608 | sp<EffectHandle> createEffect_l( |
| 609 | const sp<AudioFlinger::Client>& client, |
| 610 | const sp<IEffectClient>& effectClient, |
| 611 | int32_t priority, |
| 612 | int sessionId, |
| 613 | effect_descriptor_t *desc, |
| 614 | int *enabled, |
| 615 | status_t *status); |
| 616 | void disconnectEffect(const sp< EffectModule>& effect, |
| 617 | const wp<EffectHandle>& handle); |
| 618 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 619 | // return values for hasAudioSession (bit field) |
| 620 | enum effect_state { |
| 621 | EFFECT_SESSION = 0x1, // the audio session corresponds to at least one |
| 622 | // effect |
| 623 | TRACK_SESSION = 0x2 // the audio session corresponds to at least one |
| 624 | // track |
| 625 | }; |
| 626 | |
| 627 | uint32_t hasAudioSession(int sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 628 | sp<EffectChain> getEffectChain(int sessionId); |
| 629 | sp<EffectChain> getEffectChain_l(int sessionId); |
| 630 | status_t addEffectChain_l(const sp<EffectChain>& chain); |
| 631 | size_t removeEffectChain_l(const sp<EffectChain>& chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 632 | void lockEffectChains_l(Vector<sp <EffectChain> >& effectChains); |
| 633 | void unlockEffectChains(Vector<sp <EffectChain> >& effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 634 | |
| 635 | sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId); |
| 636 | void detachAuxEffect_l(int effectId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 637 | status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track, |
| 638 | int EffectId); |
| 639 | status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track, |
| 640 | int EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 641 | void setMode(uint32_t mode); |
| 642 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 643 | status_t addEffect_l(const sp< EffectModule>& effect); |
| 644 | void removeEffect_l(const sp< EffectModule>& effect); |
| 645 | |
| 646 | uint32_t getStrategyForSession_l(int sessionId); |
| 647 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 648 | struct stream_type_t { |
| 649 | stream_type_t() |
| 650 | : volume(1.0f), |
| 651 | mute(false) |
| 652 | { |
| 653 | } |
| 654 | float volume; |
| 655 | bool mute; |
| 656 | }; |
| 657 | |
| 658 | protected: |
| 659 | int mType; |
| 660 | int16_t* mMixBuffer; |
| 661 | int mSuspended; |
| 662 | int mBytesWritten; |
| 663 | bool mMasterMute; |
| 664 | SortedVector< wp<Track> > mActiveTracks; |
| 665 | |
| 666 | virtual int getTrackName_l() = 0; |
| 667 | virtual void deleteTrackName_l(int name) = 0; |
| 668 | virtual uint32_t activeSleepTimeUs() = 0; |
| 669 | virtual uint32_t idleSleepTimeUs() = 0; |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 670 | virtual uint32_t suspendSleepTimeUs() = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 671 | |
| 672 | private: |
| 673 | |
| 674 | friend class AudioFlinger; |
| 675 | friend class OutputTrack; |
| 676 | friend class Track; |
| 677 | friend class TrackBase; |
| 678 | friend class MixerThread; |
| 679 | friend class DirectOutputThread; |
| 680 | friend class DuplicatingThread; |
| 681 | |
| 682 | PlaybackThread(const Client&); |
| 683 | PlaybackThread& operator = (const PlaybackThread&); |
| 684 | |
| 685 | status_t addTrack_l(const sp<Track>& track); |
| 686 | void destroyTrack_l(const sp<Track>& track); |
| 687 | |
| 688 | void readOutputParameters(); |
| 689 | |
| 690 | uint32_t device() { return mDevice; } |
| 691 | |
| 692 | virtual status_t dumpInternals(int fd, const Vector<String16>& args); |
| 693 | status_t dumpTracks(int fd, const Vector<String16>& args); |
| 694 | status_t dumpEffectChains(int fd, const Vector<String16>& args); |
| 695 | |
| 696 | SortedVector< sp<Track> > mTracks; |
| 697 | // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 698 | stream_type_t mStreamTypes[AUDIO_STREAM_CNT + 1]; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 699 | AudioStreamOut* mOutput; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 700 | float mMasterVolume; |
| 701 | nsecs_t mLastWriteTime; |
| 702 | int mNumWrites; |
| 703 | int mNumDelayedWrites; |
| 704 | bool mInWrite; |
| 705 | Vector< sp<EffectChain> > mEffectChains; |
| 706 | uint32_t mDevice; |
| 707 | }; |
| 708 | |
| 709 | class MixerThread : public PlaybackThread { |
| 710 | public: |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 711 | MixerThread (const sp<AudioFlinger>& audioFlinger, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 712 | AudioStreamOut* output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 713 | int id, |
| 714 | uint32_t device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 715 | virtual ~MixerThread(); |
| 716 | |
| 717 | // Thread virtuals |
| 718 | virtual bool threadLoop(); |
| 719 | |
| 720 | void invalidateTracks(int streamType); |
| 721 | virtual bool checkForNewParameters_l(); |
| 722 | virtual status_t dumpInternals(int fd, const Vector<String16>& args); |
| 723 | |
| 724 | protected: |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 725 | uint32_t prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, |
| 726 | Vector< sp<Track> > *tracksToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 727 | virtual int getTrackName_l(); |
| 728 | virtual void deleteTrackName_l(int name); |
| 729 | virtual uint32_t activeSleepTimeUs(); |
| 730 | virtual uint32_t idleSleepTimeUs(); |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 731 | virtual uint32_t suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 732 | |
| 733 | AudioMixer* mAudioMixer; |
| 734 | }; |
| 735 | |
| 736 | class DirectOutputThread : public PlaybackThread { |
| 737 | public: |
| 738 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 739 | DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 740 | ~DirectOutputThread(); |
| 741 | |
| 742 | // Thread virtuals |
| 743 | virtual bool threadLoop(); |
| 744 | |
| 745 | virtual bool checkForNewParameters_l(); |
| 746 | |
| 747 | protected: |
| 748 | virtual int getTrackName_l(); |
| 749 | virtual void deleteTrackName_l(int name); |
| 750 | virtual uint32_t activeSleepTimeUs(); |
| 751 | virtual uint32_t idleSleepTimeUs(); |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 752 | virtual uint32_t suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 753 | |
| 754 | private: |
| 755 | void applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp); |
| 756 | |
| 757 | float mLeftVolFloat; |
| 758 | float mRightVolFloat; |
| 759 | uint16_t mLeftVolShort; |
| 760 | uint16_t mRightVolShort; |
| 761 | }; |
| 762 | |
| 763 | class DuplicatingThread : public MixerThread { |
| 764 | public: |
| 765 | DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, int id); |
| 766 | ~DuplicatingThread(); |
| 767 | |
| 768 | // Thread virtuals |
| 769 | virtual bool threadLoop(); |
| 770 | void addOutputTrack(MixerThread* thread); |
| 771 | void removeOutputTrack(MixerThread* thread); |
| 772 | uint32_t waitTimeMs() { return mWaitTimeMs; } |
| 773 | protected: |
| 774 | virtual uint32_t activeSleepTimeUs(); |
| 775 | |
| 776 | private: |
| 777 | bool outputsReady(SortedVector< sp<OutputTrack> > &outputTracks); |
| 778 | void updateWaitTime(); |
| 779 | |
| 780 | SortedVector < sp<OutputTrack> > mOutputTracks; |
| 781 | uint32_t mWaitTimeMs; |
| 782 | }; |
| 783 | |
| 784 | PlaybackThread *checkPlaybackThread_l(int output) const; |
| 785 | MixerThread *checkMixerThread_l(int output) const; |
| 786 | RecordThread *checkRecordThread_l(int input) const; |
| 787 | float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; } |
| 788 | void audioConfigChanged_l(int event, int ioHandle, void *param2); |
| 789 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 790 | int nextUniqueId_l(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 791 | status_t moveEffectChain_l(int session, |
| 792 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 793 | AudioFlinger::PlaybackThread *dstThread, |
| 794 | bool reRegister); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 795 | |
| 796 | friend class AudioBuffer; |
| 797 | |
| 798 | class TrackHandle : public android::BnAudioTrack { |
| 799 | public: |
| 800 | TrackHandle(const sp<PlaybackThread::Track>& track); |
| 801 | virtual ~TrackHandle(); |
| 802 | virtual status_t start(); |
| 803 | virtual void stop(); |
| 804 | virtual void flush(); |
| 805 | virtual void mute(bool); |
| 806 | virtual void pause(); |
| 807 | virtual void setVolume(float left, float right); |
| 808 | virtual sp<IMemory> getCblk() const; |
| 809 | virtual status_t attachAuxEffect(int effectId); |
| 810 | virtual status_t onTransact( |
| 811 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 812 | private: |
| 813 | sp<PlaybackThread::Track> mTrack; |
| 814 | }; |
| 815 | |
| 816 | friend class Client; |
| 817 | friend class PlaybackThread::Track; |
| 818 | |
| 819 | |
| 820 | void removeClient_l(pid_t pid); |
| 821 | void removeNotificationClient(pid_t pid); |
| 822 | |
| 823 | |
| 824 | // record thread |
| 825 | class RecordThread : public ThreadBase, public AudioBufferProvider |
| 826 | { |
| 827 | public: |
| 828 | |
| 829 | // record track |
| 830 | class RecordTrack : public TrackBase { |
| 831 | public: |
| 832 | RecordTrack(const wp<ThreadBase>& thread, |
| 833 | const sp<Client>& client, |
| 834 | uint32_t sampleRate, |
| 835 | int format, |
| 836 | int channelCount, |
| 837 | int frameCount, |
| 838 | uint32_t flags, |
| 839 | int sessionId); |
| 840 | ~RecordTrack(); |
| 841 | |
| 842 | virtual status_t start(); |
| 843 | virtual void stop(); |
| 844 | |
| 845 | bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; } |
| 846 | bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; } |
| 847 | |
| 848 | void dump(char* buffer, size_t size); |
| 849 | private: |
| 850 | friend class AudioFlinger; |
| 851 | friend class RecordThread; |
| 852 | |
| 853 | RecordTrack(const RecordTrack&); |
| 854 | RecordTrack& operator = (const RecordTrack&); |
| 855 | |
| 856 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); |
| 857 | |
| 858 | bool mOverflow; |
| 859 | }; |
| 860 | |
| 861 | |
| 862 | RecordThread(const sp<AudioFlinger>& audioFlinger, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 863 | AudioStreamIn *input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 864 | uint32_t sampleRate, |
| 865 | uint32_t channels, |
| 866 | int id); |
| 867 | ~RecordThread(); |
| 868 | |
| 869 | virtual bool threadLoop(); |
| 870 | virtual status_t readyToRun() { return NO_ERROR; } |
| 871 | virtual void onFirstRef(); |
| 872 | |
| 873 | status_t start(RecordTrack* recordTrack); |
| 874 | void stop(RecordTrack* recordTrack); |
| 875 | status_t dump(int fd, const Vector<String16>& args); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 876 | AudioStreamIn* getInput() { return mInput; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 877 | |
| 878 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); |
| 879 | virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); |
| 880 | virtual bool checkForNewParameters_l(); |
| 881 | virtual String8 getParameters(const String8& keys); |
| 882 | virtual void audioConfigChanged_l(int event, int param = 0); |
| 883 | void readInputParameters(); |
| 884 | virtual unsigned int getInputFramesLost(); |
| 885 | |
| 886 | private: |
| 887 | RecordThread(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 888 | AudioStreamIn *mInput; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 889 | sp<RecordTrack> mActiveTrack; |
| 890 | Condition mStartStopCond; |
| 891 | AudioResampler *mResampler; |
| 892 | int32_t *mRsmpOutBuffer; |
| 893 | int16_t *mRsmpInBuffer; |
| 894 | size_t mRsmpInIndex; |
| 895 | size_t mInputBytes; |
| 896 | int mReqChannelCount; |
| 897 | uint32_t mReqSampleRate; |
| 898 | ssize_t mBytesRead; |
| 899 | }; |
| 900 | |
| 901 | class RecordHandle : public android::BnAudioRecord { |
| 902 | public: |
| 903 | RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack); |
| 904 | virtual ~RecordHandle(); |
| 905 | virtual status_t start(); |
| 906 | virtual void stop(); |
| 907 | virtual sp<IMemory> getCblk() const; |
| 908 | virtual status_t onTransact( |
| 909 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 910 | private: |
| 911 | sp<RecordThread::RecordTrack> mRecordTrack; |
| 912 | }; |
| 913 | |
| 914 | //--- Audio Effect Management |
| 915 | |
| 916 | // EffectModule and EffectChain classes both have their own mutex to protect |
| 917 | // state changes or resource modifications. Always respect the following order |
| 918 | // if multiple mutexes must be acquired to avoid cross deadlock: |
| 919 | // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule |
| 920 | |
| 921 | // The EffectModule class is a wrapper object controlling the effect engine implementation |
| 922 | // in the effect library. It prevents concurrent calls to process() and command() functions |
| 923 | // from different client threads. It keeps a list of EffectHandle objects corresponding |
| 924 | // to all client applications using this effect and notifies applications of effect state, |
| 925 | // control or parameter changes. It manages the activation state machine to send appropriate |
| 926 | // reset, enable, disable commands to effect engine and provide volume |
| 927 | // ramping when effects are activated/deactivated. |
| 928 | // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by |
| 929 | // the attached track(s) to accumulate their auxiliary channel. |
| 930 | class EffectModule: public RefBase { |
| 931 | public: |
| 932 | EffectModule(const wp<ThreadBase>& wThread, |
| 933 | const wp<AudioFlinger::EffectChain>& chain, |
| 934 | effect_descriptor_t *desc, |
| 935 | int id, |
| 936 | int sessionId); |
| 937 | ~EffectModule(); |
| 938 | |
| 939 | enum effect_state { |
| 940 | IDLE, |
| 941 | RESTART, |
| 942 | STARTING, |
| 943 | ACTIVE, |
| 944 | STOPPING, |
| 945 | STOPPED |
| 946 | }; |
| 947 | |
| 948 | int id() { return mId; } |
| 949 | void process(); |
| 950 | void updateState(); |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 951 | status_t command(uint32_t cmdCode, |
| 952 | uint32_t cmdSize, |
| 953 | void *pCmdData, |
| 954 | uint32_t *replySize, |
| 955 | void *pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 956 | |
| 957 | void reset_l(); |
| 958 | status_t configure(); |
| 959 | status_t init(); |
| 960 | uint32_t state() { |
| 961 | return mState; |
| 962 | } |
| 963 | uint32_t status() { |
| 964 | return mStatus; |
| 965 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 966 | int sessionId() { |
| 967 | return mSessionId; |
| 968 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 969 | status_t setEnabled(bool enabled); |
| 970 | bool isEnabled(); |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 971 | bool isProcessEnabled(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 972 | |
| 973 | void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; } |
| 974 | int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; } |
| 975 | void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; } |
| 976 | int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 977 | void setChain(const wp<EffectChain>& chain) { mChain = chain; } |
| 978 | void setThread(const wp<ThreadBase>& thread) { mThread = thread; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 979 | |
| 980 | status_t addHandle(sp<EffectHandle>& handle); |
| 981 | void disconnect(const wp<EffectHandle>& handle); |
| 982 | size_t removeHandle (const wp<EffectHandle>& handle); |
| 983 | |
| 984 | effect_descriptor_t& desc() { return mDescriptor; } |
| 985 | wp<EffectChain>& chain() { return mChain; } |
| 986 | |
| 987 | status_t setDevice(uint32_t device); |
| 988 | status_t setVolume(uint32_t *left, uint32_t *right, bool controller); |
| 989 | status_t setMode(uint32_t mode); |
| 990 | |
| 991 | status_t dump(int fd, const Vector<String16>& args); |
| 992 | |
| 993 | protected: |
| 994 | |
| 995 | // Maximum time allocated to effect engines to complete the turn off sequence |
| 996 | static const uint32_t MAX_DISABLE_TIME_MS = 10000; |
| 997 | |
| 998 | EffectModule(const EffectModule&); |
| 999 | EffectModule& operator = (const EffectModule&); |
| 1000 | |
| 1001 | status_t start_l(); |
| 1002 | status_t stop_l(); |
| 1003 | |
| 1004 | // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified |
| 1005 | static const uint32_t sDeviceConvTable[]; |
| 1006 | static uint32_t deviceAudioSystemToEffectApi(uint32_t device); |
| 1007 | |
| 1008 | // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified |
| 1009 | static const uint32_t sModeConvTable[]; |
| 1010 | static int modeAudioSystemToEffectApi(uint32_t mode); |
| 1011 | |
| 1012 | Mutex mLock; // mutex for process, commands and handles list protection |
| 1013 | wp<ThreadBase> mThread; // parent thread |
| 1014 | wp<EffectChain> mChain; // parent effect chain |
| 1015 | int mId; // this instance unique ID |
| 1016 | int mSessionId; // audio session ID |
| 1017 | effect_descriptor_t mDescriptor;// effect descriptor received from effect engine |
| 1018 | effect_config_t mConfig; // input and output audio configuration |
| 1019 | effect_interface_t mEffectInterface; // Effect module C API |
| 1020 | status_t mStatus; // initialization status |
| 1021 | uint32_t mState; // current activation state (effect_state) |
| 1022 | Vector< wp<EffectHandle> > mHandles; // list of client handles |
| 1023 | uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after |
| 1024 | // sending disable command. |
| 1025 | uint32_t mDisableWaitCnt; // current process() calls count during disable period. |
| 1026 | }; |
| 1027 | |
| 1028 | // The EffectHandle class implements the IEffect interface. It provides resources |
| 1029 | // to receive parameter updates, keeps track of effect control |
| 1030 | // ownership and state and has a pointer to the EffectModule object it is controlling. |
| 1031 | // There is one EffectHandle object for each application controlling (or using) |
| 1032 | // an effect module. |
| 1033 | // The EffectHandle is obtained by calling AudioFlinger::createEffect(). |
| 1034 | class EffectHandle: public android::BnEffect { |
| 1035 | public: |
| 1036 | |
| 1037 | EffectHandle(const sp<EffectModule>& effect, |
| 1038 | const sp<AudioFlinger::Client>& client, |
| 1039 | const sp<IEffectClient>& effectClient, |
| 1040 | int32_t priority); |
| 1041 | virtual ~EffectHandle(); |
| 1042 | |
| 1043 | // IEffect |
| 1044 | virtual status_t enable(); |
| 1045 | virtual status_t disable(); |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 1046 | virtual status_t command(uint32_t cmdCode, |
| 1047 | uint32_t cmdSize, |
| 1048 | void *pCmdData, |
| 1049 | uint32_t *replySize, |
| 1050 | void *pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1051 | virtual void disconnect(); |
| 1052 | virtual sp<IMemory> getCblk() const; |
| 1053 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 1054 | Parcel* reply, uint32_t flags); |
| 1055 | |
| 1056 | |
| 1057 | // Give or take control of effect module |
| 1058 | void setControl(bool hasControl, bool signal); |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 1059 | void commandExecuted(uint32_t cmdCode, |
| 1060 | uint32_t cmdSize, |
| 1061 | void *pCmdData, |
| 1062 | uint32_t replySize, |
| 1063 | void *pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1064 | void setEnabled(bool enabled); |
| 1065 | |
| 1066 | // Getters |
| 1067 | int id() { return mEffect->id(); } |
| 1068 | int priority() { return mPriority; } |
| 1069 | bool hasControl() { return mHasControl; } |
| 1070 | sp<EffectModule> effect() { return mEffect; } |
| 1071 | |
| 1072 | void dump(char* buffer, size_t size); |
| 1073 | |
| 1074 | protected: |
| 1075 | |
| 1076 | EffectHandle(const EffectHandle&); |
| 1077 | EffectHandle& operator =(const EffectHandle&); |
| 1078 | |
| 1079 | sp<EffectModule> mEffect; // pointer to controlled EffectModule |
| 1080 | sp<IEffectClient> mEffectClient; // callback interface for client notifications |
| 1081 | sp<Client> mClient; // client for shared memory allocation |
| 1082 | sp<IMemory> mCblkMemory; // shared memory for control block |
| 1083 | effect_param_cblk_t* mCblk; // control block for deferred parameter setting via shared memory |
| 1084 | uint8_t* mBuffer; // pointer to parameter area in shared memory |
| 1085 | int mPriority; // client application priority to control the effect |
| 1086 | bool mHasControl; // true if this handle is controlling the effect |
| 1087 | }; |
| 1088 | |
| 1089 | // the EffectChain class represents a group of effects associated to one audio session. |
| 1090 | // There can be any number of EffectChain objects per output mixer thread (PlaybackThread). |
| 1091 | // The EffecChain with session ID 0 contains global effects applied to the output mix. |
| 1092 | // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks) |
| 1093 | // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding |
| 1094 | // in the effect process order. When attached to a track (session ID != 0), it also provide it's own |
| 1095 | // input buffer used by the track as accumulation buffer. |
| 1096 | class EffectChain: public RefBase { |
| 1097 | public: |
| 1098 | EffectChain(const wp<ThreadBase>& wThread, int sessionId); |
| 1099 | ~EffectChain(); |
| 1100 | |
| 1101 | void process_l(); |
| 1102 | |
| 1103 | void lock() { |
| 1104 | mLock.lock(); |
| 1105 | } |
| 1106 | void unlock() { |
| 1107 | mLock.unlock(); |
| 1108 | } |
| 1109 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1110 | status_t addEffect_l(const sp<EffectModule>& handle); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1111 | size_t removeEffect_l(const sp<EffectModule>& handle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1112 | |
| 1113 | int sessionId() { |
| 1114 | return mSessionId; |
| 1115 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1116 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1117 | sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor); |
| 1118 | sp<EffectModule> getEffectFromId_l(int id); |
| 1119 | bool setVolume_l(uint32_t *left, uint32_t *right); |
| 1120 | void setDevice_l(uint32_t device); |
| 1121 | void setMode_l(uint32_t mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1122 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1123 | void setInBuffer(int16_t *buffer, bool ownsBuffer = false) { |
| 1124 | mInBuffer = buffer; |
| 1125 | mOwnInBuffer = ownsBuffer; |
| 1126 | } |
| 1127 | int16_t *inBuffer() { |
| 1128 | return mInBuffer; |
| 1129 | } |
| 1130 | void setOutBuffer(int16_t *buffer) { |
| 1131 | mOutBuffer = buffer; |
| 1132 | } |
| 1133 | int16_t *outBuffer() { |
| 1134 | return mOutBuffer; |
| 1135 | } |
| 1136 | |
| 1137 | void startTrack() {mActiveTrackCnt++;} |
| 1138 | void stopTrack() {mActiveTrackCnt--;} |
| 1139 | int activeTracks() { return mActiveTrackCnt;} |
| 1140 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1141 | uint32_t strategy() { return mStrategy; } |
| 1142 | void setStrategy(uint32_t strategy) |
| 1143 | { mStrategy = strategy; } |
| 1144 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1145 | status_t dump(int fd, const Vector<String16>& args); |
| 1146 | |
| 1147 | protected: |
| 1148 | |
| 1149 | EffectChain(const EffectChain&); |
| 1150 | EffectChain& operator =(const EffectChain&); |
| 1151 | |
| 1152 | wp<ThreadBase> mThread; // parent mixer thread |
| 1153 | Mutex mLock; // mutex protecting effect list |
| 1154 | Vector<sp<EffectModule> > mEffects; // list of effect modules |
| 1155 | int mSessionId; // audio session ID |
| 1156 | int16_t *mInBuffer; // chain input buffer |
| 1157 | int16_t *mOutBuffer; // chain output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1158 | int mActiveTrackCnt; // number of active tracks connected |
| 1159 | bool mOwnInBuffer; // true if the chain owns its input buffer |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1160 | int mVolumeCtrlIdx; // index of insert effect having control over volume |
| 1161 | uint32_t mLeftVolume; // previous volume on left channel |
| 1162 | uint32_t mRightVolume; // previous volume on right channel |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 1163 | uint32_t mNewLeftVolume; // new volume on left channel |
| 1164 | uint32_t mNewRightVolume; // new volume on right channel |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1165 | uint32_t mStrategy; // strategy for this effect chain |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1166 | }; |
| 1167 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1168 | struct AudioStreamOut { |
| 1169 | audio_hw_device_t *hwDev; |
| 1170 | audio_stream_out_t *stream; |
| 1171 | |
| 1172 | AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) : |
| 1173 | hwDev(dev), stream(out) {} |
| 1174 | }; |
| 1175 | |
| 1176 | struct AudioStreamIn { |
| 1177 | audio_hw_device_t *hwDev; |
| 1178 | audio_stream_in_t *stream; |
| 1179 | |
| 1180 | AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) : |
| 1181 | hwDev(dev), stream(in) {} |
| 1182 | }; |
| 1183 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1184 | friend class RecordThread; |
| 1185 | friend class PlaybackThread; |
| 1186 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1187 | mutable Mutex mLock; |
| 1188 | |
| 1189 | DefaultKeyedVector< pid_t, wp<Client> > mClients; |
| 1190 | |
| 1191 | mutable Mutex mHardwareLock; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1192 | audio_hw_device_t* mPrimaryHardwareDev; |
| 1193 | Vector<audio_hw_device_t*> mAudioHwDevs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1194 | mutable int mHardwareStatus; |
| 1195 | |
| 1196 | |
| 1197 | DefaultKeyedVector< int, sp<PlaybackThread> > mPlaybackThreads; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1198 | PlaybackThread::stream_type_t mStreamTypes[AUDIO_STREAM_CNT]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1199 | float mMasterVolume; |
| 1200 | bool mMasterMute; |
| 1201 | |
| 1202 | DefaultKeyedVector< int, sp<RecordThread> > mRecordThreads; |
| 1203 | |
| 1204 | DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients; |
| 1205 | volatile int32_t mNextUniqueId; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1206 | uint32_t mMode; |
| 1207 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1208 | }; |
| 1209 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1210 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1211 | // ---------------------------------------------------------------------------- |
| 1212 | |
| 1213 | }; // namespace android |
| 1214 | |
| 1215 | #endif // ANDROID_AUDIO_FLINGER_H |