Home | History | Annotate | Download | only in audioflinger
      1 /*
      2 **
      3 ** Copyright 2012, 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 INCLUDING_FROM_AUDIOFLINGER_H
     19     #error This header file should only be included from AudioFlinger.h
     20 #endif
     21 
     22 // Checks and monitors OP_PLAY_AUDIO
     23 class OpPlayAudioMonitor : public RefBase {
     24 public:
     25     ~OpPlayAudioMonitor() override;
     26     bool hasOpPlayAudio() const;
     27 
     28     static sp<OpPlayAudioMonitor> createIfNeeded(
     29             uid_t uid, const audio_attributes_t& attr, int id, audio_stream_type_t streamType);
     30 
     31 private:
     32     OpPlayAudioMonitor(uid_t uid, audio_usage_t usage, int id);
     33     void onFirstRef() override;
     34     static void getPackagesForUid(uid_t uid, Vector<String16>& packages);
     35 
     36     AppOpsManager mAppOpsManager;
     37 
     38     class PlayAudioOpCallback : public BnAppOpsCallback {
     39     public:
     40         explicit PlayAudioOpCallback(const wp<OpPlayAudioMonitor>& monitor);
     41         void opChanged(int32_t op, const String16& packageName) override;
     42 
     43     private:
     44         const wp<OpPlayAudioMonitor> mMonitor;
     45     };
     46 
     47     sp<PlayAudioOpCallback> mOpCallback;
     48     // called by PlayAudioOpCallback when OP_PLAY_AUDIO is updated in AppOp callback
     49     void checkPlayAudioForUsage();
     50 
     51     std::atomic_bool mHasOpPlayAudio;
     52     Vector<String16> mPackages;
     53     const uid_t mUid;
     54     const int32_t mUsage; // on purpose not audio_usage_t because always checked in appOps as int32_t
     55     const int mId; // for logging purposes only
     56 };
     57 
     58 // playback track
     59 class Track : public TrackBase, public VolumeProvider {
     60 public:
     61                         Track(  PlaybackThread *thread,
     62                                 const sp<Client>& client,
     63                                 audio_stream_type_t streamType,
     64                                 const audio_attributes_t& attr,
     65                                 uint32_t sampleRate,
     66                                 audio_format_t format,
     67                                 audio_channel_mask_t channelMask,
     68                                 size_t frameCount,
     69                                 void *buffer,
     70                                 size_t bufferSize,
     71                                 const sp<IMemory>& sharedBuffer,
     72                                 audio_session_t sessionId,
     73                                 pid_t creatorPid,
     74                                 uid_t uid,
     75                                 audio_output_flags_t flags,
     76                                 track_type type,
     77                                 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
     78     virtual             ~Track();
     79     virtual status_t    initCheck() const;
     80 
     81             void        appendDumpHeader(String8& result);
     82             void        appendDump(String8& result, bool active);
     83     virtual status_t    start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
     84                               audio_session_t triggerSession = AUDIO_SESSION_NONE);
     85     virtual void        stop();
     86             void        pause();
     87 
     88             void        flush();
     89             void        destroy();
     90 
     91     virtual uint32_t    sampleRate() const;
     92 
     93             audio_stream_type_t streamType() const {
     94                 return mStreamType;
     95             }
     96             bool        isOffloaded() const
     97                                 { return (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; }
     98             bool        isDirect() const override
     99                                 { return (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0; }
    100             bool        isOffloadedOrDirect() const { return (mFlags
    101                             & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
    102                                     | AUDIO_OUTPUT_FLAG_DIRECT)) != 0; }
    103             bool        isStatic() const { return  mSharedBuffer.get() != nullptr; }
    104 
    105             status_t    setParameters(const String8& keyValuePairs);
    106             status_t    selectPresentation(int presentationId, int programId);
    107             status_t    attachAuxEffect(int EffectId);
    108             void        setAuxBuffer(int EffectId, int32_t *buffer);
    109             int32_t     *auxBuffer() const { return mAuxBuffer; }
    110             void        setMainBuffer(effect_buffer_t *buffer) { mMainBuffer = buffer; }
    111             effect_buffer_t *mainBuffer() const { return mMainBuffer; }
    112             int         auxEffectId() const { return mAuxEffectId; }
    113     virtual status_t    getTimestamp(AudioTimestamp& timestamp);
    114             void        signal();
    115 
    116 // implement FastMixerState::VolumeProvider interface
    117     virtual gain_minifloat_packed_t getVolumeLR();
    118 
    119     virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
    120 
    121     virtual bool        isFastTrack() const { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; }
    122 
    123             double      bufferLatencyMs() const override {
    124                             return isStatic() ? 0. : TrackBase::bufferLatencyMs();
    125                         }
    126 
    127 // implement volume handling.
    128     media::VolumeShaper::Status applyVolumeShaper(
    129                                 const sp<media::VolumeShaper::Configuration>& configuration,
    130                                 const sp<media::VolumeShaper::Operation>& operation);
    131     sp<media::VolumeShaper::State> getVolumeShaperState(int id);
    132     sp<media::VolumeHandler>   getVolumeHandler() { return mVolumeHandler; }
    133     /** Set the computed normalized final volume of the track.
    134      * !masterMute * masterVolume * streamVolume * averageLRVolume */
    135     void                setFinalVolume(float volume);
    136     float               getFinalVolume() const { return mFinalVolume; }
    137 
    138     /** @return true if the track has changed (metadata or volume) since
    139      *          the last time this function was called,
    140      *          true if this function was never called since the track creation,
    141      *          false otherwise.
    142      *  Thread safe.
    143      */
    144     bool            readAndClearHasChanged() { return !mChangeNotified.test_and_set(); }
    145 
    146     using SourceMetadatas = std::vector<playback_track_metadata_t>;
    147     using MetadataInserter = std::back_insert_iterator<SourceMetadatas>;
    148     /** Copy the track metadata in the provided iterator. Thread safe. */
    149     virtual void    copyMetadataTo(MetadataInserter& backInserter) const;
    150 
    151             /** Return haptic playback of the track is enabled or not, used in mixer. */
    152             bool    getHapticPlaybackEnabled() const { return mHapticPlaybackEnabled; }
    153             /** Set haptic playback of the track is enabled or not, should be
    154              *  set after query or get callback from vibrator service */
    155             void    setHapticPlaybackEnabled(bool hapticPlaybackEnabled) {
    156                 mHapticPlaybackEnabled = hapticPlaybackEnabled;
    157             }
    158             /** Return at what intensity to play haptics, used in mixer. */
    159             AudioMixer::haptic_intensity_t getHapticIntensity() const { return mHapticIntensity; }
    160             /** Set intensity of haptic playback, should be set after querying vibrator service. */
    161             void    setHapticIntensity(AudioMixer::haptic_intensity_t hapticIntensity) {
    162                 if (AudioMixer::isValidHapticIntensity(hapticIntensity)) {
    163                     mHapticIntensity = hapticIntensity;
    164                     setHapticPlaybackEnabled(mHapticIntensity != AudioMixer::HAPTIC_SCALE_MUTE);
    165                 }
    166             }
    167             sp<os::ExternalVibration> getExternalVibration() const { return mExternalVibration; }
    168 
    169             void    setTeePatches(TeePatches teePatches);
    170 
    171 protected:
    172     // for numerous
    173     friend class PlaybackThread;
    174     friend class MixerThread;
    175     friend class DirectOutputThread;
    176     friend class OffloadThread;
    177 
    178     DISALLOW_COPY_AND_ASSIGN(Track);
    179 
    180     // AudioBufferProvider interface
    181     status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override;
    182     void releaseBuffer(AudioBufferProvider::Buffer* buffer) override;
    183 
    184     // ExtendedAudioBufferProvider interface
    185     virtual size_t framesReady() const;
    186     virtual int64_t framesReleased() const;
    187     virtual void onTimestamp(const ExtendedTimestamp &timestamp);
    188 
    189     bool isPausing() const { return mState == PAUSING; }
    190     bool isPaused() const { return mState == PAUSED; }
    191     bool isResuming() const { return mState == RESUMING; }
    192     bool isReady() const;
    193     void setPaused() { mState = PAUSED; }
    194     void reset();
    195     bool isFlushPending() const { return mFlushHwPending; }
    196     void flushAck();
    197     bool isResumePending();
    198     void resumeAck();
    199     void updateTrackFrameInfo(int64_t trackFramesReleased, int64_t sinkFramesWritten,
    200             uint32_t halSampleRate, const ExtendedTimestamp &timeStamp);
    201 
    202     sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
    203 
    204     // framesWritten is cumulative, never reset, and is shared all tracks
    205     // audioHalFrames is derived from output latency
    206     // FIXME parameters not needed, could get them from the thread
    207     bool presentationComplete(int64_t framesWritten, size_t audioHalFrames);
    208     void signalClientFlag(int32_t flag);
    209 
    210     /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */
    211     void setMetadataHasChanged() { mChangeNotified.clear(); }
    212 public:
    213     void triggerEvents(AudioSystem::sync_event_t type);
    214     virtual void invalidate();
    215     void disable();
    216 
    217     int fastIndex() const { return mFastIndex; }
    218 
    219     bool isPlaybackRestricted() const {
    220         // The monitor is only created for tracks that can be silenced.
    221         return mOpPlayAudioMonitor ? !mOpPlayAudioMonitor->hasOpPlayAudio() : false; }
    222 
    223 protected:
    224 
    225     // FILLED state is used for suppressing volume ramp at begin of playing
    226     enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE};
    227     mutable uint8_t     mFillingUpStatus;
    228     int8_t              mRetryCount;
    229 
    230     // see comment at AudioFlinger::PlaybackThread::Track::~Track for why this can't be const
    231     sp<IMemory>         mSharedBuffer;
    232 
    233     bool                mResetDone;
    234     const audio_stream_type_t mStreamType;
    235     effect_buffer_t     *mMainBuffer;
    236 
    237     int32_t             *mAuxBuffer;
    238     int                 mAuxEffectId;
    239     bool                mHasVolumeController;
    240     size_t              mPresentationCompleteFrames; // number of frames written to the
    241                                     // audio HAL when this track will be fully rendered
    242                                     // zero means not monitoring
    243 
    244     // access these three variables only when holding thread lock.
    245     LinearMap<int64_t> mFrameMap;           // track frame to server frame mapping
    246 
    247     ExtendedTimestamp  mSinkTimestamp;
    248 
    249     sp<media::VolumeHandler>  mVolumeHandler; // handles multiple VolumeShaper configs and operations
    250 
    251     sp<OpPlayAudioMonitor>  mOpPlayAudioMonitor;
    252 
    253     bool                mHapticPlaybackEnabled = false; // indicates haptic playback enabled or not
    254     // intensity to play haptic data
    255     AudioMixer::haptic_intensity_t mHapticIntensity = AudioMixer::HAPTIC_SCALE_MUTE;
    256     class AudioVibrationController : public os::BnExternalVibrationController {
    257     public:
    258         explicit AudioVibrationController(Track* track) : mTrack(track) {}
    259         binder::Status mute(/*out*/ bool *ret) override;
    260         binder::Status unmute(/*out*/ bool *ret) override;
    261     private:
    262         Track* const mTrack;
    263     };
    264     sp<AudioVibrationController> mAudioVibrationController;
    265     sp<os::ExternalVibration>    mExternalVibration;
    266 
    267 private:
    268     void                interceptBuffer(const AudioBufferProvider::Buffer& buffer);
    269     /** Write the source data in the buffer provider. @return written frame count. */
    270     size_t              writeFrames(AudioBufferProvider* dest, const void* src, size_t frameCount);
    271     template <class F>
    272     void                forEachTeePatchTrack(F f) {
    273         for (auto& tp : mTeePatches) { f(tp.patchTrack); }
    274     };
    275 
    276     // The following fields are only for fast tracks, and should be in a subclass
    277     int                 mFastIndex; // index within FastMixerState::mFastTracks[];
    278                                     // either mFastIndex == -1 if not isFastTrack()
    279                                     // or 0 < mFastIndex < FastMixerState::kMaxFast because
    280                                     // index 0 is reserved for normal mixer's submix;
    281                                     // index is allocated statically at track creation time
    282                                     // but the slot is only used if track is active
    283     FastTrackUnderruns  mObservedUnderruns; // Most recently observed value of
    284                                     // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns
    285     volatile float      mCachedVolume;  // combined master volume and stream type volume;
    286                                         // 'volatile' means accessed without lock or
    287                                         // barrier, but is read/written atomically
    288     float               mFinalVolume; // combine master volume, stream type volume and track volume
    289     sp<AudioTrackServerProxy>  mAudioTrackServerProxy;
    290     bool                mResumeToStopping; // track was paused in stopping state.
    291     bool                mFlushHwPending; // track requests for thread flush
    292     audio_output_flags_t mFlags;
    293     // If the last track change was notified to the client with readAndClearHasChanged
    294     std::atomic_flag     mChangeNotified = ATOMIC_FLAG_INIT;
    295     TeePatches  mTeePatches;
    296 };  // end of Track
    297 
    298 
    299 // playback track, used by DuplicatingThread
    300 class OutputTrack : public Track {
    301 public:
    302 
    303     class Buffer : public AudioBufferProvider::Buffer {
    304     public:
    305         void *mBuffer;
    306     };
    307 
    308                         OutputTrack(PlaybackThread *thread,
    309                                 DuplicatingThread *sourceThread,
    310                                 uint32_t sampleRate,
    311                                 audio_format_t format,
    312                                 audio_channel_mask_t channelMask,
    313                                 size_t frameCount,
    314                                 uid_t uid);
    315     virtual             ~OutputTrack();
    316 
    317     virtual status_t    start(AudioSystem::sync_event_t event =
    318                                     AudioSystem::SYNC_EVENT_NONE,
    319                              audio_session_t triggerSession = AUDIO_SESSION_NONE);
    320     virtual void        stop();
    321             ssize_t     write(void* data, uint32_t frames);
    322             bool        bufferQueueEmpty() const { return mBufferQueue.size() == 0; }
    323             bool        isActive() const { return mActive; }
    324     const wp<ThreadBase>& thread() const { return mThread; }
    325 
    326             void        copyMetadataTo(MetadataInserter& backInserter) const override;
    327     /** Set the metadatas of the upstream tracks. Thread safe. */
    328             void        setMetadatas(const SourceMetadatas& metadatas);
    329     /** returns client timestamp to the upstream duplicating thread. */
    330     ExtendedTimestamp   getClientProxyTimestamp() const {
    331                             // server - kernel difference is not true latency when drained
    332                             // i.e. mServerProxy->isDrained().
    333                             ExtendedTimestamp timestamp;
    334                             (void) mClientProxy->getTimestamp(&timestamp);
    335                             // On success, the timestamp LOCATION_SERVER and LOCATION_KERNEL
    336                             // entries will be properly filled. If getTimestamp()
    337                             // is unsuccessful, then a default initialized timestamp
    338                             // (with mTimeNs[] filled with -1's) is returned.
    339                             return timestamp;
    340                         }
    341 
    342 private:
    343     status_t            obtainBuffer(AudioBufferProvider::Buffer* buffer,
    344                                      uint32_t waitTimeMs);
    345     void                clearBufferQueue();
    346 
    347     void                restartIfDisabled();
    348 
    349     // Maximum number of pending buffers allocated by OutputTrack::write()
    350     static const uint8_t kMaxOverFlowBuffers = 10;
    351 
    352     Vector < Buffer* >          mBufferQueue;
    353     AudioBufferProvider::Buffer mOutBuffer;
    354     bool                        mActive;
    355     DuplicatingThread* const    mSourceThread; // for waitTimeMs() in write()
    356     sp<AudioTrackClientProxy>   mClientProxy;
    357 
    358     /** Attributes of the source tracks.
    359      *
    360      * This member must be accessed with mTrackMetadatasMutex taken.
    361      * There is one writer (duplicating thread) and one reader (downstream mixer).
    362      *
    363      * That means that the duplicating thread can block the downstream mixer
    364      * thread and vice versa for the time of the copy.
    365      * If this becomes an issue, the metadata could be stored in an atomic raw pointer,
    366      * and a exchange with nullptr and delete can be used.
    367      * Alternatively a read-copy-update might be implemented.
    368      */
    369     SourceMetadatas mTrackMetadatas;
    370     /** Protects mTrackMetadatas against concurrent access. */
    371     mutable std::mutex mTrackMetadatasMutex;
    372 };  // end of OutputTrack
    373 
    374 // playback track, used by PatchPanel
    375 class PatchTrack : public Track, public PatchTrackBase {
    376 public:
    377 
    378                         PatchTrack(PlaybackThread *playbackThread,
    379                                    audio_stream_type_t streamType,
    380                                    uint32_t sampleRate,
    381                                    audio_channel_mask_t channelMask,
    382                                    audio_format_t format,
    383                                    size_t frameCount,
    384                                    void *buffer,
    385                                    size_t bufferSize,
    386                                    audio_output_flags_t flags,
    387                                    const Timeout& timeout = {});
    388     virtual             ~PatchTrack();
    389 
    390     virtual status_t    start(AudioSystem::sync_event_t event =
    391                                     AudioSystem::SYNC_EVENT_NONE,
    392                              audio_session_t triggerSession = AUDIO_SESSION_NONE);
    393 
    394     // AudioBufferProvider interface
    395     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
    396     virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
    397 
    398     // PatchProxyBufferProvider interface
    399     virtual status_t    obtainBuffer(Proxy::Buffer* buffer,
    400                                      const struct timespec *timeOut = NULL);
    401     virtual void        releaseBuffer(Proxy::Buffer* buffer);
    402 
    403 private:
    404             void restartIfDisabled();
    405 
    406 };  // end of PatchTrack
    407