AudioFlinger.h revision a4c5a550e2a3bc237179b8684e51718e05894492
1/*
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 <common_time/cc_helper.h>
26
27#include <media/IAudioFlinger.h>
28#include <media/IAudioFlingerClient.h>
29#include <media/IAudioTrack.h>
30#include <media/IAudioRecord.h>
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Atomic.h>
35#include <utils/Errors.h>
36#include <utils/threads.h>
37#include <utils/SortedVector.h>
38#include <utils/TypeHelpers.h>
39#include <utils/Vector.h>
40
41#include <binder/BinderService.h>
42#include <binder/MemoryDealer.h>
43
44#include <system/audio.h>
45#include <hardware/audio.h>
46#include <hardware/audio_policy.h>
47
48#include "AudioBufferProvider.h"
49
50#include <powermanager/IPowerManager.h>
51
52namespace android {
53
54class audio_track_cblk_t;
55class effect_param_cblk_t;
56class AudioMixer;
57class AudioBuffer;
58class AudioResampler;
59
60// ----------------------------------------------------------------------------
61
62// AudioFlinger has a hard-coded upper limit of 2 channels for capture and playback.
63// There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect.
64// Adding full support for > 2 channel capture or playback would require more than simply changing
65// this #define.  There is an independent hard-coded upper limit in AudioMixer;
66// removing that AudioMixer limit would be necessary but insufficient to support > 2 channels.
67// The macro FCC_2 highlights some (but not all) places where there is are 2-channel assumptions.
68// Search also for "2", "left", "right", "[0]", "[1]", ">> 16", "<< 16", etc.
69#define FCC_2 2     // FCC_2 = Fixed Channel Count 2
70
71static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3);
72
73class AudioFlinger :
74    public BinderService<AudioFlinger>,
75    public BnAudioFlinger
76{
77    friend class BinderService<AudioFlinger>;   // for AudioFlinger()
78public:
79    static const char* getServiceName() { return "media.audio_flinger"; }
80
81    virtual     status_t    dump(int fd, const Vector<String16>& args);
82
83    // IAudioFlinger interface, in binder opcode order
84    virtual sp<IAudioTrack> createTrack(
85                                pid_t pid,
86                                audio_stream_type_t streamType,
87                                uint32_t sampleRate,
88                                audio_format_t format,
89                                uint32_t channelMask,
90                                int frameCount,
91                                IAudioFlinger::track_flags_t flags,
92                                const sp<IMemory>& sharedBuffer,
93                                audio_io_handle_t output,
94                                int *sessionId,
95                                status_t *status);
96
97    virtual sp<IAudioRecord> openRecord(
98                                pid_t pid,
99                                audio_io_handle_t input,
100                                uint32_t sampleRate,
101                                audio_format_t format,
102                                uint32_t channelMask,
103                                int frameCount,
104                                IAudioFlinger::track_flags_t flags,
105                                int *sessionId,
106                                status_t *status);
107
108    virtual     uint32_t    sampleRate(audio_io_handle_t output) const;
109    virtual     int         channelCount(audio_io_handle_t output) const;
110    virtual     audio_format_t format(audio_io_handle_t output) const;
111    virtual     size_t      frameCount(audio_io_handle_t output) const;
112    virtual     uint32_t    latency(audio_io_handle_t output) const;
113
114    virtual     status_t    setMasterVolume(float value);
115    virtual     status_t    setMasterMute(bool muted);
116
117    virtual     float       masterVolume() const;
118    virtual     float       masterVolumeSW() const;
119    virtual     bool        masterMute() const;
120
121    virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
122                                            audio_io_handle_t output);
123    virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted);
124
125    virtual     float       streamVolume(audio_stream_type_t stream,
126                                         audio_io_handle_t output) const;
127    virtual     bool        streamMute(audio_stream_type_t stream) const;
128
129    virtual     status_t    setMode(audio_mode_t mode);
130
131    virtual     status_t    setMicMute(bool state);
132    virtual     bool        getMicMute() const;
133
134    virtual     status_t    setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
135    virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys) const;
136
137    virtual     void        registerClient(const sp<IAudioFlingerClient>& client);
138
139    virtual     size_t      getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const;
140
141    virtual audio_io_handle_t openOutput(audio_module_handle_t module,
142                                         audio_devices_t *pDevices,
143                                         uint32_t *pSamplingRate,
144                                         audio_format_t *pFormat,
145                                         audio_channel_mask_t *pChannelMask,
146                                         uint32_t *pLatencyMs,
147                                         audio_policy_output_flags_t flags);
148
149    virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
150                                                  audio_io_handle_t output2);
151
152    virtual status_t closeOutput(audio_io_handle_t output);
153
154    virtual status_t suspendOutput(audio_io_handle_t output);
155
156    virtual status_t restoreOutput(audio_io_handle_t output);
157
158    virtual audio_io_handle_t openInput(audio_module_handle_t module,
159                                        audio_devices_t *pDevices,
160                                        uint32_t *pSamplingRate,
161                                        audio_format_t *pFormat,
162                                        audio_channel_mask_t *pChannelMask);
163
164    virtual status_t closeInput(audio_io_handle_t input);
165
166    virtual status_t setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output);
167
168    virtual status_t setVoiceVolume(float volume);
169
170    virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
171                                       audio_io_handle_t output) const;
172
173    virtual     unsigned int  getInputFramesLost(audio_io_handle_t ioHandle) const;
174
175    virtual int newAudioSessionId();
176
177    virtual void acquireAudioSessionId(int audioSession);
178
179    virtual void releaseAudioSessionId(int audioSession);
180
181    virtual status_t queryNumberEffects(uint32_t *numEffects) const;
182
183    virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const;
184
185    virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
186                                         effect_descriptor_t *descriptor) const;
187
188    virtual sp<IEffect> createEffect(pid_t pid,
189                        effect_descriptor_t *pDesc,
190                        const sp<IEffectClient>& effectClient,
191                        int32_t priority,
192                        audio_io_handle_t io,
193                        int sessionId,
194                        status_t *status,
195                        int *id,
196                        int *enabled);
197
198    virtual status_t moveEffects(int sessionId, audio_io_handle_t srcOutput,
199                        audio_io_handle_t dstOutput);
200
201    virtual audio_module_handle_t loadHwModule(const char *name);
202
203    virtual     status_t    onTransact(
204                                uint32_t code,
205                                const Parcel& data,
206                                Parcel* reply,
207                                uint32_t flags);
208
209    // end of IAudioFlinger interface
210
211    class SyncEvent;
212
213    typedef void (*sync_event_callback_t)(const wp<SyncEvent>& event) ;
214
215    class SyncEvent : public RefBase {
216    public:
217        SyncEvent(AudioSystem::sync_event_t type,
218                  int triggerSession,
219                  int listenerSession,
220                  sync_event_callback_t callBack,
221                  void *cookie)
222        : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
223          mCallback(callBack), mCookie(cookie)
224        {}
225
226        virtual ~SyncEvent() {}
227
228        void trigger() { Mutex::Autolock _l(mLock); if (mCallback) mCallback(this); }
229        void cancel() {Mutex::Autolock _l(mLock); mCallback = NULL; }
230        AudioSystem::sync_event_t type() const { return mType; }
231        int triggerSession() const { return mTriggerSession; }
232        int listenerSession() const { return mListenerSession; }
233        void *cookie() const { return mCookie; }
234
235    private:
236          const AudioSystem::sync_event_t mType;
237          const int mTriggerSession;
238          const int mListenerSession;
239          sync_event_callback_t mCallback;
240          void * const mCookie;
241          Mutex mLock;
242    };
243
244    sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
245                                        int triggerSession,
246                                        int listenerSession,
247                                        sync_event_callback_t callBack,
248                                        void *cookie);
249private:
250               audio_mode_t getMode() const { return mMode; }
251
252                bool        btNrecIsOff() const { return mBtNrecIsOff; }
253
254                            AudioFlinger();
255    virtual                 ~AudioFlinger();
256
257    // call in any IAudioFlinger method that accesses mPrimaryHardwareDev
258    status_t                initCheck() const { return mPrimaryHardwareDev == NULL ? NO_INIT : NO_ERROR; }
259
260    // RefBase
261    virtual     void        onFirstRef();
262
263    audio_hw_device_t*      findSuitableHwDev_l(audio_module_handle_t module, uint32_t devices);
264    void                    purgeStaleEffects_l();
265
266    // standby delay for MIXER and DUPLICATING playback threads is read from property
267    // ro.audio.flinger_standbytime_ms or defaults to kDefaultStandbyTimeInNsecs
268    static nsecs_t          mStandbyTimeInNsecs;
269
270    // Internal dump utilites.
271    status_t dumpPermissionDenial(int fd, const Vector<String16>& args);
272    status_t dumpClients(int fd, const Vector<String16>& args);
273    status_t dumpInternals(int fd, const Vector<String16>& args);
274
275    // --- Client ---
276    class Client : public RefBase {
277    public:
278                            Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
279        virtual             ~Client();
280        sp<MemoryDealer>    heap() const;
281        pid_t               pid() const { return mPid; }
282        sp<AudioFlinger>    audioFlinger() const { return mAudioFlinger; }
283
284        bool reserveTimedTrack();
285        void releaseTimedTrack();
286
287    private:
288                            Client(const Client&);
289                            Client& operator = (const Client&);
290        const sp<AudioFlinger> mAudioFlinger;
291        const sp<MemoryDealer> mMemoryDealer;
292        const pid_t         mPid;
293
294        Mutex               mTimedTrackLock;
295        int                 mTimedTrackCount;
296    };
297
298    // --- Notification Client ---
299    class NotificationClient : public IBinder::DeathRecipient {
300    public:
301                            NotificationClient(const sp<AudioFlinger>& audioFlinger,
302                                                const sp<IAudioFlingerClient>& client,
303                                                pid_t pid);
304        virtual             ~NotificationClient();
305
306                sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; }
307
308                // IBinder::DeathRecipient
309                virtual     void        binderDied(const wp<IBinder>& who);
310
311    private:
312                            NotificationClient(const NotificationClient&);
313                            NotificationClient& operator = (const NotificationClient&);
314
315        const sp<AudioFlinger>  mAudioFlinger;
316        const pid_t             mPid;
317        const sp<IAudioFlingerClient> mAudioFlingerClient;
318    };
319
320    class TrackHandle;
321    class RecordHandle;
322    class RecordThread;
323    class PlaybackThread;
324    class MixerThread;
325    class DirectOutputThread;
326    class DuplicatingThread;
327    class Track;
328    class RecordTrack;
329    class EffectModule;
330    class EffectHandle;
331    class EffectChain;
332    struct AudioStreamOut;
333    struct AudioStreamIn;
334
335    class ThreadBase : public Thread {
336    public:
337
338        enum type_t {
339            MIXER,              // Thread class is MixerThread
340            DIRECT,             // Thread class is DirectOutputThread
341            DUPLICATING,        // Thread class is DuplicatingThread
342            RECORD              // Thread class is RecordThread
343        };
344
345        ThreadBase (const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, uint32_t device, type_t type);
346        virtual             ~ThreadBase();
347
348        status_t dumpBase(int fd, const Vector<String16>& args);
349        status_t dumpEffectChains(int fd, const Vector<String16>& args);
350
351        void clearPowerManager();
352
353        // base for record and playback
354        class TrackBase : public AudioBufferProvider, public RefBase {
355
356        public:
357            enum track_state {
358                IDLE,
359                TERMINATED,
360                // These are order-sensitive; do not change order without reviewing the impact.
361                // In particular there are assumptions about > STOPPED.
362                STOPPED,
363                RESUMING,
364                ACTIVE,
365                PAUSING,
366                PAUSED
367            };
368
369                                TrackBase(ThreadBase *thread,
370                                        const sp<Client>& client,
371                                        uint32_t sampleRate,
372                                        audio_format_t format,
373                                        uint32_t channelMask,
374                                        int frameCount,
375                                        const sp<IMemory>& sharedBuffer,
376                                        int sessionId);
377            virtual             ~TrackBase();
378
379            virtual status_t    start(pid_t tid,
380                                     AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
381                                     int triggerSession = 0) = 0;
382            virtual void        stop() = 0;
383                    sp<IMemory> getCblk() const { return mCblkMemory; }
384                    audio_track_cblk_t* cblk() const { return mCblk; }
385                    int         sessionId() const { return mSessionId; }
386            virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
387
388        protected:
389                                TrackBase(const TrackBase&);
390                                TrackBase& operator = (const TrackBase&);
391
392            // AudioBufferProvider interface
393            virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) = 0;
394            virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
395
396            audio_format_t format() const {
397                return mFormat;
398            }
399
400            int channelCount() const { return mChannelCount; }
401
402            uint32_t channelMask() const { return mChannelMask; }
403
404            int sampleRate() const; // FIXME inline after cblk sr moved
405
406            void* getBuffer(uint32_t offset, uint32_t frames) const;
407
408            bool isStopped() const {
409                return mState == STOPPED;
410            }
411
412            bool isTerminated() const {
413                return mState == TERMINATED;
414            }
415
416            bool step();
417            void reset();
418
419            const wp<ThreadBase> mThread;
420            /*const*/ sp<Client> mClient;   // see explanation at ~TrackBase() why not const
421            sp<IMemory>         mCblkMemory;
422            audio_track_cblk_t* mCblk;
423            void*               mBuffer;
424            void*               mBufferEnd;
425            uint32_t            mFrameCount;
426            // we don't really need a lock for these
427            track_state         mState;
428            const audio_format_t mFormat;
429            bool                mStepServerFailed;
430            const int           mSessionId;
431            uint8_t             mChannelCount;
432            uint32_t            mChannelMask;
433            Vector < sp<SyncEvent> >mSyncEvents;
434        };
435
436        class ConfigEvent {
437        public:
438            ConfigEvent() : mEvent(0), mParam(0) {}
439
440            int mEvent;
441            int mParam;
442        };
443
444        class PMDeathRecipient : public IBinder::DeathRecipient {
445        public:
446                        PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
447            virtual     ~PMDeathRecipient() {}
448
449            // IBinder::DeathRecipient
450            virtual     void        binderDied(const wp<IBinder>& who);
451
452        private:
453                        PMDeathRecipient(const PMDeathRecipient&);
454                        PMDeathRecipient& operator = (const PMDeathRecipient&);
455
456            wp<ThreadBase> mThread;
457        };
458
459        virtual     status_t    initCheck() const = 0;
460                    type_t      type() const { return mType; }
461                    uint32_t    sampleRate() const { return mSampleRate; }
462                    int         channelCount() const { return mChannelCount; }
463                    audio_format_t format() const { return mFormat; }
464                    size_t      frameCount() const { return mFrameCount; }
465                    void        wakeUp()    { mWaitWorkCV.broadcast(); }
466        // Should be "virtual status_t requestExitAndWait()" and override same
467        // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
468                    void        exit();
469        virtual     bool        checkForNewParameters_l() = 0;
470        virtual     status_t    setParameters(const String8& keyValuePairs);
471        virtual     String8     getParameters(const String8& keys) = 0;
472        virtual     void        audioConfigChanged_l(int event, int param = 0) = 0;
473                    void        sendConfigEvent(int event, int param = 0);
474                    void        sendConfigEvent_l(int event, int param = 0);
475                    void        processConfigEvents();
476                    audio_io_handle_t id() const { return mId;}
477                    bool        standby() const { return mStandby; }
478                    uint32_t    device() const { return mDevice; }
479        virtual     audio_stream_t* stream() const = 0;
480
481                    sp<EffectHandle> createEffect_l(
482                                        const sp<AudioFlinger::Client>& client,
483                                        const sp<IEffectClient>& effectClient,
484                                        int32_t priority,
485                                        int sessionId,
486                                        effect_descriptor_t *desc,
487                                        int *enabled,
488                                        status_t *status);
489                    void disconnectEffect(const sp< EffectModule>& effect,
490                                          const wp<EffectHandle>& handle,
491                                          bool unpinIfLast);
492
493                    // return values for hasAudioSession (bit field)
494                    enum effect_state {
495                        EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
496                                                // effect
497                        TRACK_SESSION = 0x2     // the audio session corresponds to at least one
498                                                // track
499                    };
500
501                    // get effect chain corresponding to session Id.
502                    sp<EffectChain> getEffectChain(int sessionId);
503                    // same as getEffectChain() but must be called with ThreadBase mutex locked
504                    sp<EffectChain> getEffectChain_l(int sessionId);
505                    // add an effect chain to the chain list (mEffectChains)
506        virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
507                    // remove an effect chain from the chain list (mEffectChains)
508        virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
509                    // lock all effect chains Mutexes. Must be called before releasing the
510                    // ThreadBase mutex before processing the mixer and effects. This guarantees the
511                    // integrity of the chains during the process.
512                    // Also sets the parameter 'effectChains' to current value of mEffectChains.
513                    void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
514                    // unlock effect chains after process
515                    void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
516                    // set audio mode to all effect chains
517                    void setMode(audio_mode_t mode);
518                    // get effect module with corresponding ID on specified audio session
519                    sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
520                    // add and effect module. Also creates the effect chain is none exists for
521                    // the effects audio session
522                    status_t addEffect_l(const sp< EffectModule>& effect);
523                    // remove and effect module. Also removes the effect chain is this was the last
524                    // effect
525                    void removeEffect_l(const sp< EffectModule>& effect);
526                    // detach all tracks connected to an auxiliary effect
527        virtual     void detachAuxEffect_l(int effectId) {}
528                    // returns either EFFECT_SESSION if effects on this audio session exist in one
529                    // chain, or TRACK_SESSION if tracks on this audio session exist, or both
530                    virtual uint32_t hasAudioSession(int sessionId) = 0;
531                    // the value returned by default implementation is not important as the
532                    // strategy is only meaningful for PlaybackThread which implements this method
533                    virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
534
535                    // suspend or restore effect according to the type of effect passed. a NULL
536                    // type pointer means suspend all effects in the session
537                    void setEffectSuspended(const effect_uuid_t *type,
538                                            bool suspend,
539                                            int sessionId = AUDIO_SESSION_OUTPUT_MIX);
540                    // check if some effects must be suspended/restored when an effect is enabled
541                    // or disabled
542                    void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
543                                                     bool enabled,
544                                                     int sessionId = AUDIO_SESSION_OUTPUT_MIX);
545                    void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
546                                                       bool enabled,
547                                                       int sessionId = AUDIO_SESSION_OUTPUT_MIX);
548
549                    virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
550                    virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) = 0;
551
552
553        mutable     Mutex                   mLock;
554
555    protected:
556
557                    // entry describing an effect being suspended in mSuspendedSessions keyed vector
558                    class SuspendedSessionDesc : public RefBase {
559                    public:
560                        SuspendedSessionDesc() : mRefCount(0) {}
561
562                        int mRefCount;          // number of active suspend requests
563                        effect_uuid_t mType;    // effect type UUID
564                    };
565
566                    void        acquireWakeLock();
567                    void        acquireWakeLock_l();
568                    void        releaseWakeLock();
569                    void        releaseWakeLock_l();
570                    void setEffectSuspended_l(const effect_uuid_t *type,
571                                              bool suspend,
572                                              int sessionId = AUDIO_SESSION_OUTPUT_MIX);
573                    // updated mSuspendedSessions when an effect suspended or restored
574                    void        updateSuspendedSessions_l(const effect_uuid_t *type,
575                                                          bool suspend,
576                                                          int sessionId);
577                    // check if some effects must be suspended when an effect chain is added
578                    void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
579
580        friend class AudioFlinger;      // for mEffectChains
581
582                    const type_t            mType;
583
584                    // Used by parameters, config events, addTrack_l, exit
585                    Condition               mWaitWorkCV;
586
587                    const sp<AudioFlinger>  mAudioFlinger;
588                    uint32_t                mSampleRate;
589                    size_t                  mFrameCount;
590                    uint32_t                mChannelMask;
591                    uint16_t                mChannelCount;
592                    size_t                  mFrameSize;
593                    audio_format_t          mFormat;
594
595                    // Parameter sequence by client: binder thread calling setParameters():
596                    //  1. Lock mLock
597                    //  2. Append to mNewParameters
598                    //  3. mWaitWorkCV.signal
599                    //  4. mParamCond.waitRelative with timeout
600                    //  5. read mParamStatus
601                    //  6. mWaitWorkCV.signal
602                    //  7. Unlock
603                    //
604                    // Parameter sequence by server: threadLoop calling checkForNewParameters_l():
605                    // 1. Lock mLock
606                    // 2. If there is an entry in mNewParameters proceed ...
607                    // 2. Read first entry in mNewParameters
608                    // 3. Process
609                    // 4. Remove first entry from mNewParameters
610                    // 5. Set mParamStatus
611                    // 6. mParamCond.signal
612                    // 7. mWaitWorkCV.wait with timeout (this is to avoid overwriting mParamStatus)
613                    // 8. Unlock
614                    Condition               mParamCond;
615                    Vector<String8>         mNewParameters;
616                    status_t                mParamStatus;
617
618                    Vector<ConfigEvent>     mConfigEvents;
619                    bool                    mStandby;
620                    const audio_io_handle_t mId;
621                    Vector< sp<EffectChain> > mEffectChains;
622                    uint32_t                mDevice;    // output device for PlaybackThread
623                                                        // input + output devices for RecordThread
624                    static const int        kNameLength = 16;   // prctl(PR_SET_NAME) limit
625                    char                    mName[kNameLength];
626                    sp<IPowerManager>       mPowerManager;
627                    sp<IBinder>             mWakeLockToken;
628                    const sp<PMDeathRecipient> mDeathRecipient;
629                    // list of suspended effects per session and per type. The first vector is
630                    // keyed by session ID, the second by type UUID timeLow field
631                    KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > >  mSuspendedSessions;
632    };
633
634    struct  stream_type_t {
635        stream_type_t()
636            :   volume(1.0f),
637                mute(false)
638        {
639        }
640        float       volume;
641        bool        mute;
642    };
643
644    // --- PlaybackThread ---
645    class PlaybackThread : public ThreadBase {
646    public:
647
648        enum mixer_state {
649            MIXER_IDLE,             // no active tracks
650            MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
651            MIXER_TRACKS_READY      // at least one active track, and at least one track has data
652            // standby mode does not have an enum value
653            // suspend by audio policy manager is orthogonal to mixer state
654        };
655
656        // playback track
657        class Track : public TrackBase {
658        public:
659                                Track(  PlaybackThread *thread,
660                                        const sp<Client>& client,
661                                        audio_stream_type_t streamType,
662                                        uint32_t sampleRate,
663                                        audio_format_t format,
664                                        uint32_t channelMask,
665                                        int frameCount,
666                                        const sp<IMemory>& sharedBuffer,
667                                        int sessionId,
668                                        IAudioFlinger::track_flags_t flags);
669            virtual             ~Track();
670
671                    void        dump(char* buffer, size_t size);
672            virtual status_t    start(pid_t tid,
673                                     AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
674                                     int triggerSession = 0);
675            virtual void        stop();
676                    void        pause();
677
678                    void        flush();
679                    void        destroy();
680                    void        mute(bool);
681                    int name() const {
682                        return mName;
683                    }
684
685                    audio_stream_type_t streamType() const {
686                        return mStreamType;
687                    }
688                    status_t    attachAuxEffect(int EffectId);
689                    void        setAuxBuffer(int EffectId, int32_t *buffer);
690                    int32_t     *auxBuffer() const { return mAuxBuffer; }
691                    void        setMainBuffer(int16_t *buffer) { mMainBuffer = buffer; }
692                    int16_t     *mainBuffer() const { return mMainBuffer; }
693                    int         auxEffectId() const { return mAuxEffectId; }
694
695                    bool        isFastTrack() const
696                            { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
697
698        protected:
699            // for numerous
700            friend class PlaybackThread;
701            friend class MixerThread;
702            friend class DirectOutputThread;
703
704                                Track(const Track&);
705                                Track& operator = (const Track&);
706
707            // AudioBufferProvider interface
708            virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts = kInvalidPTS);
709            // releaseBuffer() not overridden
710
711            virtual uint32_t framesReady() const;
712
713            bool isMuted() const { return mMute; }
714            bool isPausing() const {
715                return mState == PAUSING;
716            }
717            bool isPaused() const {
718                return mState == PAUSED;
719            }
720            bool isReady() const;
721            void setPaused() { mState = PAUSED; }
722            void reset();
723
724            bool isOutputTrack() const {
725                return (mStreamType == AUDIO_STREAM_CNT);
726            }
727
728            bool presentationComplete(size_t framesWritten, size_t audioHalFrames);
729            void triggerEvents(AudioSystem::sync_event_t type);
730
731        public:
732            virtual bool isTimedTrack() const { return false; }
733        protected:
734
735            // we don't really need a lock for these
736            volatile bool       mMute;
737            // FILLED state is used for suppressing volume ramp at begin of playing
738            enum {FS_FILLING, FS_FILLED, FS_ACTIVE};
739            mutable uint8_t     mFillingUpStatus;
740            int8_t              mRetryCount;
741            const sp<IMemory>   mSharedBuffer;
742            bool                mResetDone;
743            const audio_stream_type_t mStreamType;
744            int                 mName;
745            int16_t             *mMainBuffer;
746            int32_t             *mAuxBuffer;
747            int                 mAuxEffectId;
748            bool                mHasVolumeController;
749            size_t              mPresentationCompleteFrames; // number of frames written to the audio HAL
750                                                       // when this track will be fully rendered
751        private:
752            IAudioFlinger::track_flags_t mFlags;
753        };  // end of Track
754
755        class TimedTrack : public Track {
756          public:
757            static sp<TimedTrack> create(PlaybackThread *thread,
758                                         const sp<Client>& client,
759                                         audio_stream_type_t streamType,
760                                         uint32_t sampleRate,
761                                         audio_format_t format,
762                                         uint32_t channelMask,
763                                         int frameCount,
764                                         const sp<IMemory>& sharedBuffer,
765                                         int sessionId);
766            ~TimedTrack();
767
768            class TimedBuffer {
769              public:
770                TimedBuffer();
771                TimedBuffer(const sp<IMemory>& buffer, int64_t pts);
772                const sp<IMemory>& buffer() const { return mBuffer; }
773                int64_t pts() const { return mPTS; }
774                int position() const { return mPosition; }
775                void setPosition(int pos) { mPosition = pos; }
776              private:
777                sp<IMemory> mBuffer;
778                int64_t mPTS;
779                int mPosition;
780            };
781
782            virtual bool isTimedTrack() const { return true; }
783
784            virtual uint32_t framesReady() const;
785
786            // AudioBufferProvider interface
787            virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
788            virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
789
790            void timedYieldSamples(AudioBufferProvider::Buffer* buffer);
791            void timedYieldSilence(uint32_t numFrames,
792                                   AudioBufferProvider::Buffer* buffer);
793
794            status_t    allocateTimedBuffer(size_t size,
795                                            sp<IMemory>* buffer);
796            status_t    queueTimedBuffer(const sp<IMemory>& buffer,
797                                         int64_t pts);
798            status_t    setMediaTimeTransform(const LinearTransform& xform,
799                                              TimedAudioTrack::TargetTimeline target);
800            void        trimTimedBufferQueue_l();
801
802          private:
803            TimedTrack(PlaybackThread *thread,
804                       const sp<Client>& client,
805                       audio_stream_type_t streamType,
806                       uint32_t sampleRate,
807                       audio_format_t format,
808                       uint32_t channelMask,
809                       int frameCount,
810                       const sp<IMemory>& sharedBuffer,
811                       int sessionId);
812
813            uint64_t            mLocalTimeFreq;
814            LinearTransform     mLocalTimeToSampleTransform;
815            sp<MemoryDealer>    mTimedMemoryDealer;
816            Vector<TimedBuffer> mTimedBufferQueue;
817            uint8_t*            mTimedSilenceBuffer;
818            uint32_t            mTimedSilenceBufferSize;
819            mutable Mutex       mTimedBufferQueueLock;
820            bool                mTimedAudioOutputOnTime;
821            CCHelper            mCCHelper;
822
823            Mutex               mMediaTimeTransformLock;
824            LinearTransform     mMediaTimeTransform;
825            bool                mMediaTimeTransformValid;
826            TimedAudioTrack::TargetTimeline mMediaTimeTransformTarget;
827        };
828
829
830        // playback track
831        class OutputTrack : public Track {
832        public:
833
834            class Buffer: public AudioBufferProvider::Buffer {
835            public:
836                int16_t *mBuffer;
837            };
838
839                                OutputTrack(PlaybackThread *thread,
840                                        DuplicatingThread *sourceThread,
841                                        uint32_t sampleRate,
842                                        audio_format_t format,
843                                        uint32_t channelMask,
844                                        int frameCount);
845            virtual             ~OutputTrack();
846
847            virtual status_t    start(pid_t tid,
848                                     AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
849                                     int triggerSession = 0);
850            virtual void        stop();
851                    bool        write(int16_t* data, uint32_t frames);
852                    bool        bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
853                    bool        isActive() const { return mActive; }
854            const wp<ThreadBase>& thread() const { return mThread; }
855
856        private:
857
858            enum {
859                NO_MORE_BUFFERS = 0x80000001,   // same in AudioTrack.h, ok to be different value
860            };
861
862            status_t            obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs);
863            void                clearBufferQueue();
864
865            // Maximum number of pending buffers allocated by OutputTrack::write()
866            static const uint8_t kMaxOverFlowBuffers = 10;
867
868            Vector < Buffer* >          mBufferQueue;
869            AudioBufferProvider::Buffer mOutBuffer;
870            bool                        mActive;
871            DuplicatingThread* const mSourceThread; // for waitTimeMs() in write()
872        };  // end of OutputTrack
873
874        PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
875                        audio_io_handle_t id, uint32_t device, type_t type);
876        virtual             ~PlaybackThread();
877
878                    status_t    dump(int fd, const Vector<String16>& args);
879
880        // Thread virtuals
881        virtual     status_t    readyToRun();
882        virtual     bool        threadLoop();
883
884        // RefBase
885        virtual     void        onFirstRef();
886
887protected:
888        // Code snippets that were lifted up out of threadLoop()
889        virtual     void        threadLoop_mix() = 0;
890        virtual     void        threadLoop_sleepTime() = 0;
891        virtual     void        threadLoop_write();
892        virtual     void        threadLoop_standby();
893
894                    // prepareTracks_l reads and writes mActiveTracks, and also returns the
895                    // pending set of tracks to remove via Vector 'tracksToRemove'.  The caller is
896                    // responsible for clearing or destroying this Vector later on, when it
897                    // is safe to do so. That will drop the final ref count and destroy the tracks.
898        virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
899
900public:
901
902        virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
903
904                    // return estimated latency in milliseconds, as reported by HAL
905                    uint32_t    latency() const;
906
907                    void        setMasterVolume(float value);
908                    void        setMasterMute(bool muted);
909
910                    void        setStreamVolume(audio_stream_type_t stream, float value);
911                    void        setStreamMute(audio_stream_type_t stream, bool muted);
912
913                    float       streamVolume(audio_stream_type_t stream) const;
914
915                    sp<Track>   createTrack_l(
916                                    const sp<AudioFlinger::Client>& client,
917                                    audio_stream_type_t streamType,
918                                    uint32_t sampleRate,
919                                    audio_format_t format,
920                                    uint32_t channelMask,
921                                    int frameCount,
922                                    const sp<IMemory>& sharedBuffer,
923                                    int sessionId,
924                                    IAudioFlinger::track_flags_t flags,
925                                    status_t *status);
926
927                    AudioStreamOut* getOutput() const;
928                    AudioStreamOut* clearOutput();
929                    virtual audio_stream_t* stream() const;
930
931                    void        suspend() { mSuspended++; }
932                    void        restore() { if (mSuspended > 0) mSuspended--; }
933                    bool        isSuspended() const { return (mSuspended > 0); }
934        virtual     String8     getParameters(const String8& keys);
935        virtual     void        audioConfigChanged_l(int event, int param = 0);
936                    status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
937                    int16_t     *mixBuffer() const { return mMixBuffer; };
938
939        virtual     void detachAuxEffect_l(int effectId);
940                    status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
941                            int EffectId);
942                    status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
943                            int EffectId);
944
945                    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
946                    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
947                    virtual uint32_t hasAudioSession(int sessionId);
948                    virtual uint32_t getStrategyForSession_l(int sessionId);
949
950
951                    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
952                    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event);
953
954    protected:
955        int16_t*                        mMixBuffer;
956        uint32_t                        mSuspended;     // suspend count, > 0 means suspended
957        int                             mBytesWritten;
958    private:
959        // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
960        // PlaybackThread needs to find out if master-muted, it checks it's local
961        // copy rather than the one in AudioFlinger.  This optimization saves a lock.
962        bool                            mMasterMute;
963                    void        setMasterMute_l(bool muted) { mMasterMute = muted; }
964    protected:
965        SortedVector< wp<Track> >       mActiveTracks;
966
967        // Allocate a track name.  Returns name >= 0 if successful, -1 on failure.
968        virtual int             getTrackName_l() = 0;
969        virtual void            deleteTrackName_l(int name) = 0;
970
971        // Time to sleep between cycles when:
972        virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
973        virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
974        virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
975        // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
976        // No sleep in standby mode; waits on a condition
977
978        // Code snippets that are temporarily lifted up out of threadLoop() until the merge
979                    void        checkSilentMode_l();
980
981        // Non-trivial for DUPLICATING only
982        virtual     void        saveOutputTracks() { }
983        virtual     void        clearOutputTracks() { }
984
985        // Cache various calculated values, at threadLoop() entry and after a parameter change
986        virtual     void        cacheParameters_l();
987
988    private:
989
990        friend class AudioFlinger;      // for numerous
991
992        PlaybackThread(const Client&);
993        PlaybackThread& operator = (const PlaybackThread&);
994
995        status_t    addTrack_l(const sp<Track>& track);
996        void        destroyTrack_l(const sp<Track>& track);
997        void        removeTrack_l(const sp<Track>& track);
998
999        void        readOutputParameters();
1000
1001        virtual status_t    dumpInternals(int fd, const Vector<String16>& args);
1002        status_t    dumpTracks(int fd, const Vector<String16>& args);
1003
1004        SortedVector< sp<Track> >       mTracks;
1005        // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by DuplicatingThread
1006        stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT + 1];
1007        AudioStreamOut                  *mOutput;
1008        float                           mMasterVolume;
1009        nsecs_t                         mLastWriteTime;
1010        int                             mNumWrites;
1011        int                             mNumDelayedWrites;
1012        bool                            mInWrite;
1013
1014        // FIXME rename these former local variables of threadLoop to standard "m" names
1015        nsecs_t                         standbyTime;
1016        size_t                          mixBufferSize;
1017
1018        // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
1019        uint32_t                        activeSleepTime;
1020        uint32_t                        idleSleepTime;
1021
1022        uint32_t                        sleepTime;
1023
1024        // mixer status returned by prepareTracks_l()
1025        mixer_state                     mMixerStatus;       // current cycle
1026        mixer_state                     mPrevMixerStatus;   // previous cycle
1027
1028        // FIXME move these declarations into the specific sub-class that needs them
1029        // MIXER only
1030        bool                            longStandbyExit;
1031        uint32_t                        sleepTimeShift;
1032
1033        // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
1034        nsecs_t                         standbyDelay;
1035
1036        // MIXER only
1037        nsecs_t                         maxPeriod;
1038
1039        // DUPLICATING only
1040        uint32_t                        writeFrames;
1041    };
1042
1043    class MixerThread : public PlaybackThread {
1044    public:
1045        MixerThread (const sp<AudioFlinger>& audioFlinger,
1046                     AudioStreamOut* output,
1047                     audio_io_handle_t id,
1048                     uint32_t device,
1049                     type_t type = MIXER);
1050        virtual             ~MixerThread();
1051
1052        // Thread virtuals
1053
1054                    void        invalidateTracks(audio_stream_type_t streamType);
1055        virtual     bool        checkForNewParameters_l();
1056        virtual     status_t    dumpInternals(int fd, const Vector<String16>& args);
1057
1058    protected:
1059        virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1060        virtual     int         getTrackName_l();
1061        virtual     void        deleteTrackName_l(int name);
1062        virtual     uint32_t    idleSleepTimeUs() const;
1063        virtual     uint32_t    suspendSleepTimeUs() const;
1064        virtual     void        cacheParameters_l();
1065
1066        // threadLoop snippets
1067        virtual     void        threadLoop_mix();
1068        virtual     void        threadLoop_sleepTime();
1069
1070                    AudioMixer* mAudioMixer;
1071    };
1072
1073    class DirectOutputThread : public PlaybackThread {
1074    public:
1075
1076        DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1077                            audio_io_handle_t id, uint32_t device);
1078        virtual                 ~DirectOutputThread();
1079
1080        // Thread virtuals
1081
1082        virtual     bool        checkForNewParameters_l();
1083
1084    protected:
1085        virtual     int         getTrackName_l();
1086        virtual     void        deleteTrackName_l(int name);
1087        virtual     uint32_t    activeSleepTimeUs() const;
1088        virtual     uint32_t    idleSleepTimeUs() const;
1089        virtual     uint32_t    suspendSleepTimeUs() const;
1090        virtual     void        cacheParameters_l();
1091
1092        // threadLoop snippets
1093        virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1094        virtual     void        threadLoop_mix();
1095        virtual     void        threadLoop_sleepTime();
1096
1097        // volumes last sent to audio HAL with stream->set_volume()
1098        // FIXME use standard representation and names
1099        float mLeftVolFloat;
1100        float mRightVolFloat;
1101        uint16_t mLeftVolShort;
1102        uint16_t mRightVolShort;
1103
1104        // FIXME rename these former local variables of threadLoop to standard names
1105        // next 3 were local to the while !exitingPending loop
1106        bool rampVolume;
1107        uint16_t leftVol;
1108        uint16_t rightVol;
1109
1110private:
1111        // prepareTracks_l() tells threadLoop_mix() the name of the single active track
1112        sp<Track>               mActiveTrack;
1113    };
1114
1115    class DuplicatingThread : public MixerThread {
1116    public:
1117        DuplicatingThread (const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
1118                           audio_io_handle_t id);
1119        virtual                 ~DuplicatingThread();
1120
1121        // Thread virtuals
1122                    void        addOutputTrack(MixerThread* thread);
1123                    void        removeOutputTrack(MixerThread* thread);
1124                    uint32_t    waitTimeMs() const { return mWaitTimeMs; }
1125    protected:
1126        virtual     uint32_t    activeSleepTimeUs() const;
1127
1128    private:
1129                    bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
1130    protected:
1131        // threadLoop snippets
1132        virtual     void        threadLoop_mix();
1133        virtual     void        threadLoop_sleepTime();
1134        virtual     void        threadLoop_write();
1135        virtual     void        threadLoop_standby();
1136        virtual     void        cacheParameters_l();
1137
1138    private:
1139        // called from threadLoop, addOutputTrack, removeOutputTrack
1140        virtual     void        updateWaitTime_l();
1141    protected:
1142        virtual     void        saveOutputTracks();
1143        virtual     void        clearOutputTracks();
1144    private:
1145
1146                    uint32_t    mWaitTimeMs;
1147        SortedVector < sp<OutputTrack> >  outputTracks;
1148        SortedVector < sp<OutputTrack> >  mOutputTracks;
1149    };
1150
1151              PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const;
1152              MixerThread *checkMixerThread_l(audio_io_handle_t output) const;
1153              RecordThread *checkRecordThread_l(audio_io_handle_t input) const;
1154              // no range check, AudioFlinger::mLock held
1155              bool streamMute_l(audio_stream_type_t stream) const
1156                                { return mStreamTypes[stream].mute; }
1157              // no range check, doesn't check per-thread stream volume, AudioFlinger::mLock held
1158              float streamVolume_l(audio_stream_type_t stream) const
1159                                { return mStreamTypes[stream].volume; }
1160              void audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2);
1161
1162              // allocate an audio_io_handle_t, session ID, or effect ID
1163              uint32_t nextUniqueId();
1164
1165              status_t moveEffectChain_l(int sessionId,
1166                                     PlaybackThread *srcThread,
1167                                     PlaybackThread *dstThread,
1168                                     bool reRegister);
1169              // return thread associated with primary hardware device, or NULL
1170              PlaybackThread *primaryPlaybackThread_l() const;
1171              uint32_t primaryOutputDevice_l() const;
1172
1173    // server side of the client's IAudioTrack
1174    class TrackHandle : public android::BnAudioTrack {
1175    public:
1176                            TrackHandle(const sp<PlaybackThread::Track>& track);
1177        virtual             ~TrackHandle();
1178        virtual sp<IMemory> getCblk() const;
1179        virtual status_t    start(pid_t tid);
1180        virtual void        stop();
1181        virtual void        flush();
1182        virtual void        mute(bool);
1183        virtual void        pause();
1184        virtual status_t    attachAuxEffect(int effectId);
1185        virtual status_t    allocateTimedBuffer(size_t size,
1186                                                sp<IMemory>* buffer);
1187        virtual status_t    queueTimedBuffer(const sp<IMemory>& buffer,
1188                                             int64_t pts);
1189        virtual status_t    setMediaTimeTransform(const LinearTransform& xform,
1190                                                  int target);
1191        virtual status_t onTransact(
1192            uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1193    private:
1194        const sp<PlaybackThread::Track> mTrack;
1195    };
1196
1197                void        removeClient_l(pid_t pid);
1198                void        removeNotificationClient(pid_t pid);
1199
1200
1201    // record thread
1202    class RecordThread : public ThreadBase, public AudioBufferProvider
1203    {
1204    public:
1205
1206        // record track
1207        class RecordTrack : public TrackBase {
1208        public:
1209                                RecordTrack(RecordThread *thread,
1210                                        const sp<Client>& client,
1211                                        uint32_t sampleRate,
1212                                        audio_format_t format,
1213                                        uint32_t channelMask,
1214                                        int frameCount,
1215                                        int sessionId);
1216            virtual             ~RecordTrack();
1217
1218            virtual status_t    start(pid_t tid,
1219                                     AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
1220                                     int triggerSession = 0);
1221            virtual void        stop();
1222
1223                    bool        overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; }
1224                    bool        setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; }
1225
1226                    void        dump(char* buffer, size_t size);
1227
1228        private:
1229            friend class AudioFlinger;  // for mState
1230
1231                                RecordTrack(const RecordTrack&);
1232                                RecordTrack& operator = (const RecordTrack&);
1233
1234            // AudioBufferProvider interface
1235            virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts = kInvalidPTS);
1236            // releaseBuffer() not overridden
1237
1238            bool                mOverflow;
1239        };
1240
1241
1242                RecordThread(const sp<AudioFlinger>& audioFlinger,
1243                        AudioStreamIn *input,
1244                        uint32_t sampleRate,
1245                        uint32_t channels,
1246                        audio_io_handle_t id,
1247                        uint32_t device);
1248                virtual     ~RecordThread();
1249
1250        // Thread
1251        virtual bool        threadLoop();
1252        virtual status_t    readyToRun();
1253
1254        // RefBase
1255        virtual void        onFirstRef();
1256
1257        virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1258                sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
1259                        const sp<AudioFlinger::Client>& client,
1260                        uint32_t sampleRate,
1261                        audio_format_t format,
1262                        int channelMask,
1263                        int frameCount,
1264                        int sessionId,
1265                        status_t *status);
1266
1267                status_t    start(RecordTrack* recordTrack, pid_t tid,
1268                                  AudioSystem::sync_event_t event,
1269                                  int triggerSession);
1270                void        stop(RecordTrack* recordTrack);
1271                status_t    dump(int fd, const Vector<String16>& args);
1272                AudioStreamIn* getInput() const;
1273                AudioStreamIn* clearInput();
1274                virtual audio_stream_t* stream() const;
1275
1276        // AudioBufferProvider interface
1277        virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
1278        virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
1279
1280        virtual bool        checkForNewParameters_l();
1281        virtual String8     getParameters(const String8& keys);
1282        virtual void        audioConfigChanged_l(int event, int param = 0);
1283                void        readInputParameters();
1284        virtual unsigned int  getInputFramesLost();
1285
1286        virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
1287        virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
1288        virtual uint32_t hasAudioSession(int sessionId);
1289                RecordTrack* track();
1290
1291        virtual status_t setSyncEvent(const sp<SyncEvent>& event);
1292        virtual bool     isValidSyncEvent(const sp<SyncEvent>& event);
1293
1294        static void syncStartEventCallback(const wp<SyncEvent>& event);
1295               void handleSyncStartEvent(const sp<SyncEvent>& event);
1296
1297    private:
1298                void clearSyncStartEvent();
1299
1300                RecordThread();
1301                AudioStreamIn                       *mInput;
1302                RecordTrack*                        mTrack;
1303                sp<RecordTrack>                     mActiveTrack;
1304                Condition                           mStartStopCond;
1305                AudioResampler                      *mResampler;
1306                int32_t                             *mRsmpOutBuffer;
1307                int16_t                             *mRsmpInBuffer;
1308                size_t                              mRsmpInIndex;
1309                size_t                              mInputBytes;
1310                const int                           mReqChannelCount;
1311                const uint32_t                      mReqSampleRate;
1312                ssize_t                             mBytesRead;
1313                // sync event triggering actual audio capture. Frames read before this event will
1314                // be dropped and therefore not read by the application.
1315                sp<SyncEvent>                       mSyncStartEvent;
1316                // number of captured frames to drop after the start sync event has been received.
1317                ssize_t                             mFramestoDrop;
1318    };
1319
1320    // server side of the client's IAudioRecord
1321    class RecordHandle : public android::BnAudioRecord {
1322    public:
1323        RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
1324        virtual             ~RecordHandle();
1325        virtual sp<IMemory> getCblk() const;
1326        virtual status_t    start(pid_t tid, int event, int triggerSession);
1327        virtual void        stop();
1328        virtual status_t onTransact(
1329            uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
1330    private:
1331        const sp<RecordThread::RecordTrack> mRecordTrack;
1332    };
1333
1334    //--- Audio Effect Management
1335
1336    // EffectModule and EffectChain classes both have their own mutex to protect
1337    // state changes or resource modifications. Always respect the following order
1338    // if multiple mutexes must be acquired to avoid cross deadlock:
1339    // AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
1340
1341    // The EffectModule class is a wrapper object controlling the effect engine implementation
1342    // in the effect library. It prevents concurrent calls to process() and command() functions
1343    // from different client threads. It keeps a list of EffectHandle objects corresponding
1344    // to all client applications using this effect and notifies applications of effect state,
1345    // control or parameter changes. It manages the activation state machine to send appropriate
1346    // reset, enable, disable commands to effect engine and provide volume
1347    // ramping when effects are activated/deactivated.
1348    // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
1349    // the attached track(s) to accumulate their auxiliary channel.
1350    class EffectModule: public RefBase {
1351    public:
1352        EffectModule(ThreadBase *thread,
1353                        const wp<AudioFlinger::EffectChain>& chain,
1354                        effect_descriptor_t *desc,
1355                        int id,
1356                        int sessionId);
1357        virtual ~EffectModule();
1358
1359        enum effect_state {
1360            IDLE,
1361            RESTART,
1362            STARTING,
1363            ACTIVE,
1364            STOPPING,
1365            STOPPED,
1366            DESTROYED
1367        };
1368
1369        int         id() const { return mId; }
1370        void process();
1371        void updateState();
1372        status_t command(uint32_t cmdCode,
1373                         uint32_t cmdSize,
1374                         void *pCmdData,
1375                         uint32_t *replySize,
1376                         void *pReplyData);
1377
1378        void reset_l();
1379        status_t configure();
1380        status_t init();
1381        effect_state state() const {
1382            return mState;
1383        }
1384        uint32_t status() {
1385            return mStatus;
1386        }
1387        int sessionId() const {
1388            return mSessionId;
1389        }
1390        status_t    setEnabled(bool enabled);
1391        bool isEnabled() const;
1392        bool isProcessEnabled() const;
1393
1394        void        setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
1395        int16_t     *inBuffer() { return mConfig.inputCfg.buffer.s16; }
1396        void        setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
1397        int16_t     *outBuffer() { return mConfig.outputCfg.buffer.s16; }
1398        void        setChain(const wp<EffectChain>& chain) { mChain = chain; }
1399        void        setThread(const wp<ThreadBase>& thread) { mThread = thread; }
1400        const wp<ThreadBase>& thread() { return mThread; }
1401
1402        status_t addHandle(const sp<EffectHandle>& handle);
1403        void disconnect(const wp<EffectHandle>& handle, bool unpinIfLast);
1404        size_t removeHandle (const wp<EffectHandle>& handle);
1405
1406        effect_descriptor_t& desc() { return mDescriptor; }
1407        wp<EffectChain>&     chain() { return mChain; }
1408
1409        status_t         setDevice(uint32_t device);
1410        status_t         setVolume(uint32_t *left, uint32_t *right, bool controller);
1411        status_t         setMode(audio_mode_t mode);
1412        status_t         start();
1413        status_t         stop();
1414        void             setSuspended(bool suspended);
1415        bool             suspended() const;
1416
1417        sp<EffectHandle> controlHandle();
1418
1419        bool             isPinned() const { return mPinned; }
1420        void             unPin() { mPinned = false; }
1421
1422        status_t         dump(int fd, const Vector<String16>& args);
1423
1424    protected:
1425        friend class AudioFlinger;      // for mHandles
1426        bool                mPinned;
1427
1428        // Maximum time allocated to effect engines to complete the turn off sequence
1429        static const uint32_t MAX_DISABLE_TIME_MS = 10000;
1430
1431        EffectModule(const EffectModule&);
1432        EffectModule& operator = (const EffectModule&);
1433
1434        status_t start_l();
1435        status_t stop_l();
1436
1437mutable Mutex               mLock;      // mutex for process, commands and handles list protection
1438        wp<ThreadBase>      mThread;    // parent thread
1439        wp<EffectChain>     mChain;     // parent effect chain
1440        int                 mId;        // this instance unique ID
1441        int                 mSessionId; // audio session ID
1442        effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
1443        effect_config_t     mConfig;    // input and output audio configuration
1444        effect_handle_t  mEffectInterface; // Effect module C API
1445        status_t            mStatus;    // initialization status
1446        effect_state        mState;     // current activation state
1447        Vector< wp<EffectHandle> > mHandles;    // list of client handles
1448                    // First handle in mHandles has highest priority and controls the effect module
1449        uint32_t mMaxDisableWaitCnt;    // maximum grace period before forcing an effect off after
1450                                        // sending disable command.
1451        uint32_t mDisableWaitCnt;       // current process() calls count during disable period.
1452        bool     mSuspended;            // effect is suspended: temporarily disabled by framework
1453    };
1454
1455    // The EffectHandle class implements the IEffect interface. It provides resources
1456    // to receive parameter updates, keeps track of effect control
1457    // ownership and state and has a pointer to the EffectModule object it is controlling.
1458    // There is one EffectHandle object for each application controlling (or using)
1459    // an effect module.
1460    // The EffectHandle is obtained by calling AudioFlinger::createEffect().
1461    class EffectHandle: public android::BnEffect {
1462    public:
1463
1464        EffectHandle(const sp<EffectModule>& effect,
1465                const sp<AudioFlinger::Client>& client,
1466                const sp<IEffectClient>& effectClient,
1467                int32_t priority);
1468        virtual ~EffectHandle();
1469
1470        // IEffect
1471        virtual status_t enable();
1472        virtual status_t disable();
1473        virtual status_t command(uint32_t cmdCode,
1474                                 uint32_t cmdSize,
1475                                 void *pCmdData,
1476                                 uint32_t *replySize,
1477                                 void *pReplyData);
1478        virtual void disconnect();
1479    private:
1480                void disconnect(bool unpinIfLast);
1481    public:
1482        virtual sp<IMemory> getCblk() const { return mCblkMemory; }
1483        virtual status_t onTransact(uint32_t code, const Parcel& data,
1484                Parcel* reply, uint32_t flags);
1485
1486
1487        // Give or take control of effect module
1488        // - hasControl: true if control is given, false if removed
1489        // - signal: true client app should be signaled of change, false otherwise
1490        // - enabled: state of the effect when control is passed
1491        void setControl(bool hasControl, bool signal, bool enabled);
1492        void commandExecuted(uint32_t cmdCode,
1493                             uint32_t cmdSize,
1494                             void *pCmdData,
1495                             uint32_t replySize,
1496                             void *pReplyData);
1497        void setEnabled(bool enabled);
1498        bool enabled() const { return mEnabled; }
1499
1500        // Getters
1501        int id() const { return mEffect->id(); }
1502        int priority() const { return mPriority; }
1503        bool hasControl() const { return mHasControl; }
1504        sp<EffectModule> effect() const { return mEffect; }
1505
1506        void dump(char* buffer, size_t size);
1507
1508    protected:
1509        friend class AudioFlinger;          // for mEffect, mHasControl, mEnabled
1510        EffectHandle(const EffectHandle&);
1511        EffectHandle& operator =(const EffectHandle&);
1512
1513        sp<EffectModule> mEffect;           // pointer to controlled EffectModule
1514        sp<IEffectClient> mEffectClient;    // callback interface for client notifications
1515        /*const*/ sp<Client> mClient;       // client for shared memory allocation, see disconnect()
1516        sp<IMemory>         mCblkMemory;    // shared memory for control block
1517        effect_param_cblk_t* mCblk;         // control block for deferred parameter setting via shared memory
1518        uint8_t*            mBuffer;        // pointer to parameter area in shared memory
1519        int mPriority;                      // client application priority to control the effect
1520        bool mHasControl;                   // true if this handle is controlling the effect
1521        bool mEnabled;                      // cached enable state: needed when the effect is
1522                                            // restored after being suspended
1523    };
1524
1525    // the EffectChain class represents a group of effects associated to one audio session.
1526    // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
1527    // The EffecChain with session ID 0 contains global effects applied to the output mix.
1528    // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to tracks)
1529    // are insert only. The EffectChain maintains an ordered list of effect module, the order corresponding
1530    // in the effect process order. When attached to a track (session ID != 0), it also provide it's own
1531    // input buffer used by the track as accumulation buffer.
1532    class EffectChain: public RefBase {
1533    public:
1534        EffectChain(const wp<ThreadBase>& wThread, int sessionId);
1535        EffectChain(ThreadBase *thread, int sessionId);
1536        virtual ~EffectChain();
1537
1538        // special key used for an entry in mSuspendedEffects keyed vector
1539        // corresponding to a suspend all request.
1540        static const int        kKeyForSuspendAll = 0;
1541
1542        // minimum duration during which we force calling effect process when last track on
1543        // a session is stopped or removed to allow effect tail to be rendered
1544        static const int        kProcessTailDurationMs = 1000;
1545
1546        void process_l();
1547
1548        void lock() {
1549            mLock.lock();
1550        }
1551        void unlock() {
1552            mLock.unlock();
1553        }
1554
1555        status_t addEffect_l(const sp<EffectModule>& handle);
1556        size_t removeEffect_l(const sp<EffectModule>& handle);
1557
1558        int sessionId() const { return mSessionId; }
1559        void setSessionId(int sessionId) { mSessionId = sessionId; }
1560
1561        sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
1562        sp<EffectModule> getEffectFromId_l(int id);
1563        sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
1564        bool setVolume_l(uint32_t *left, uint32_t *right);
1565        void setDevice_l(uint32_t device);
1566        void setMode_l(audio_mode_t mode);
1567
1568        void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
1569            mInBuffer = buffer;
1570            mOwnInBuffer = ownsBuffer;
1571        }
1572        int16_t *inBuffer() const {
1573            return mInBuffer;
1574        }
1575        void setOutBuffer(int16_t *buffer) {
1576            mOutBuffer = buffer;
1577        }
1578        int16_t *outBuffer() const {
1579            return mOutBuffer;
1580        }
1581
1582        void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
1583        void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
1584        int32_t trackCnt() const { return mTrackCnt;}
1585
1586        void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
1587                                   mTailBufferCount = mMaxTailBuffers; }
1588        void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
1589        int32_t activeTrackCnt() const { return mActiveTrackCnt;}
1590
1591        uint32_t strategy() const { return mStrategy; }
1592        void setStrategy(uint32_t strategy)
1593                { mStrategy = strategy; }
1594
1595        // suspend effect of the given type
1596        void setEffectSuspended_l(const effect_uuid_t *type,
1597                                  bool suspend);
1598        // suspend all eligible effects
1599        void setEffectSuspendedAll_l(bool suspend);
1600        // check if effects should be suspend or restored when a given effect is enable or disabled
1601        void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1602                                              bool enabled);
1603
1604        status_t dump(int fd, const Vector<String16>& args);
1605
1606    protected:
1607        friend class AudioFlinger;  // for mThread, mEffects
1608        EffectChain(const EffectChain&);
1609        EffectChain& operator =(const EffectChain&);
1610
1611        class SuspendedEffectDesc : public RefBase {
1612        public:
1613            SuspendedEffectDesc() : mRefCount(0) {}
1614
1615            int mRefCount;
1616            effect_uuid_t mType;
1617            wp<EffectModule> mEffect;
1618        };
1619
1620        // get a list of effect modules to suspend when an effect of the type
1621        // passed is enabled.
1622        void                       getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
1623
1624        // get an effect module if it is currently enable
1625        sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
1626        // true if the effect whose descriptor is passed can be suspended
1627        // OEMs can modify the rules implemented in this method to exclude specific effect
1628        // types or implementations from the suspend/restore mechanism.
1629        bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
1630
1631        wp<ThreadBase> mThread;     // parent mixer thread
1632        Mutex mLock;                // mutex protecting effect list
1633        Vector< sp<EffectModule> > mEffects; // list of effect modules
1634        int mSessionId;             // audio session ID
1635        int16_t *mInBuffer;         // chain input buffer
1636        int16_t *mOutBuffer;        // chain output buffer
1637        volatile int32_t mActiveTrackCnt;  // number of active tracks connected
1638        volatile int32_t mTrackCnt;        // number of tracks connected
1639        int32_t mTailBufferCount;   // current effect tail buffer count
1640        int32_t mMaxTailBuffers;    // maximum effect tail buffers
1641        bool mOwnInBuffer;          // true if the chain owns its input buffer
1642        int mVolumeCtrlIdx;         // index of insert effect having control over volume
1643        uint32_t mLeftVolume;       // previous volume on left channel
1644        uint32_t mRightVolume;      // previous volume on right channel
1645        uint32_t mNewLeftVolume;       // new volume on left channel
1646        uint32_t mNewRightVolume;      // new volume on right channel
1647        uint32_t mStrategy; // strategy for this effect chain
1648        // mSuspendedEffects lists all effects currently suspended in the chain.
1649        // Use effect type UUID timelow field as key. There is no real risk of identical
1650        // timeLow fields among effect type UUIDs.
1651        // Updated by updateSuspendedSessions_l() only.
1652        KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
1653    };
1654
1655    // AudioStreamOut and AudioStreamIn are immutable, so their fields are const.
1656    // For emphasis, we could also make all pointers to them be "const *",
1657    // but that would clutter the code unnecessarily.
1658
1659    struct AudioStreamOut {
1660        audio_hw_device_t*  const hwDev;
1661        audio_stream_out_t* const stream;
1662
1663        AudioStreamOut(audio_hw_device_t *dev, audio_stream_out_t *out) :
1664            hwDev(dev), stream(out) {}
1665    };
1666
1667    struct AudioStreamIn {
1668        audio_hw_device_t* const hwDev;
1669        audio_stream_in_t* const stream;
1670
1671        AudioStreamIn(audio_hw_device_t *dev, audio_stream_in_t *in) :
1672            hwDev(dev), stream(in) {}
1673    };
1674
1675    // for mAudioSessionRefs only
1676    struct AudioSessionRef {
1677        AudioSessionRef(int sessionid, pid_t pid) :
1678            mSessionid(sessionid), mPid(pid), mCnt(1) {}
1679        const int   mSessionid;
1680        const pid_t mPid;
1681        int         mCnt;
1682    };
1683
1684    enum master_volume_support {
1685        // MVS_NONE:
1686        // Audio HAL has no support for master volume, either setting or
1687        // getting.  All master volume control must be implemented in SW by the
1688        // AudioFlinger mixing core.
1689        MVS_NONE,
1690
1691        // MVS_SETONLY:
1692        // Audio HAL has support for setting master volume, but not for getting
1693        // master volume (original HAL design did not include a getter).
1694        // AudioFlinger needs to keep track of the last set master volume in
1695        // addition to needing to set an initial, default, master volume at HAL
1696        // load time.
1697        MVS_SETONLY,
1698
1699        // MVS_FULL:
1700        // Audio HAL has support both for setting and getting master volume.
1701        // AudioFlinger should send all set and get master volume requests
1702        // directly to the HAL.
1703        MVS_FULL,
1704    };
1705
1706    class AudioHwDevice {
1707    public:
1708        AudioHwDevice(const char *moduleName, audio_hw_device_t *hwDevice) :
1709            mModuleName(strdup(moduleName)), mHwDevice(hwDevice){}
1710        ~AudioHwDevice() { free((void *)mModuleName); }
1711
1712        const char *moduleName() const { return mModuleName; }
1713        audio_hw_device_t *hwDevice() const { return mHwDevice; }
1714    private:
1715        const char * const mModuleName;
1716        audio_hw_device_t * const mHwDevice;
1717    };
1718
1719    mutable     Mutex                               mLock;
1720
1721                DefaultKeyedVector< pid_t, wp<Client> >     mClients;   // see ~Client()
1722
1723                mutable     Mutex                   mHardwareLock;
1724                // NOTE: If both mLock and mHardwareLock mutexes must be held,
1725                // always take mLock before mHardwareLock
1726
1727                // These two fields are immutable after onFirstRef(), so no lock needed to access
1728                audio_hw_device_t*                  mPrimaryHardwareDev; // mAudioHwDevs[0] or NULL
1729                DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*>  mAudioHwDevs;
1730
1731    // for dump, indicates which hardware operation is currently in progress (but not stream ops)
1732    enum hardware_call_state {
1733        AUDIO_HW_IDLE = 0,              // no operation in progress
1734        AUDIO_HW_INIT,                  // init_check
1735        AUDIO_HW_OUTPUT_OPEN,           // open_output_stream
1736        AUDIO_HW_OUTPUT_CLOSE,          // unused
1737        AUDIO_HW_INPUT_OPEN,            // unused
1738        AUDIO_HW_INPUT_CLOSE,           // unused
1739        AUDIO_HW_STANDBY,               // unused
1740        AUDIO_HW_SET_MASTER_VOLUME,     // set_master_volume
1741        AUDIO_HW_GET_ROUTING,           // unused
1742        AUDIO_HW_SET_ROUTING,           // unused
1743        AUDIO_HW_GET_MODE,              // unused
1744        AUDIO_HW_SET_MODE,              // set_mode
1745        AUDIO_HW_GET_MIC_MUTE,          // get_mic_mute
1746        AUDIO_HW_SET_MIC_MUTE,          // set_mic_mute
1747        AUDIO_HW_SET_VOICE_VOLUME,      // set_voice_volume
1748        AUDIO_HW_SET_PARAMETER,         // set_parameters
1749        AUDIO_HW_GET_INPUT_BUFFER_SIZE, // get_input_buffer_size
1750        AUDIO_HW_GET_MASTER_VOLUME,     // get_master_volume
1751        AUDIO_HW_GET_PARAMETER,         // get_parameters
1752    };
1753
1754    mutable     hardware_call_state                 mHardwareStatus;    // for dump only
1755
1756
1757                DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> >  mPlaybackThreads;
1758                stream_type_t                       mStreamTypes[AUDIO_STREAM_CNT];
1759
1760                // both are protected by mLock
1761                float                               mMasterVolume;
1762                float                               mMasterVolumeSW;
1763                master_volume_support               mMasterVolumeSupportLvl;
1764                bool                                mMasterMute;
1765
1766                DefaultKeyedVector< audio_io_handle_t, sp<RecordThread> >    mRecordThreads;
1767
1768                DefaultKeyedVector< pid_t, sp<NotificationClient> >    mNotificationClients;
1769                volatile int32_t                    mNextUniqueId;  // updated by android_atomic_inc
1770                audio_mode_t                        mMode;
1771                bool                                mBtNrecIsOff;
1772
1773                // protected by mLock
1774                Vector<AudioSessionRef*> mAudioSessionRefs;
1775
1776                float       masterVolume_l() const;
1777                float       masterVolumeSW_l() const  { return mMasterVolumeSW; }
1778                bool        masterMute_l() const    { return mMasterMute; }
1779                audio_module_handle_t loadHwModule_l(const char *name);
1780
1781                Vector < sp<SyncEvent> > mPendingSyncEvents; // sync events awaiting for a session
1782                                                             // to be created
1783
1784private:
1785    sp<Client>  registerPid_l(pid_t pid);    // always returns non-0
1786
1787};
1788
1789
1790// ----------------------------------------------------------------------------
1791
1792}; // namespace android
1793
1794#endif // ANDROID_AUDIO_FLINGER_H
1795