Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e4c1de7b authored by Andy Hung's avatar Andy Hung
Browse files

AudioFlinger: Add Track interfaces

Add new interfaces

IAfTrackBase
IAfTrack
IAfOutputTrack
IAfMmapTrack
IAfRecordTrack

Test: atest audiorecord_tests audiotrack_tests audiorouting_tests trackplayerbase_tests audiosystem_tests
Test: atest AudioTrackTest AudioRecordTest
Test: YouTube Camera
Bug: 288339104
Bug: 288468076
Merged-In: Iee8fd68fcd1c430da09b11d68a57fc62ba4c6f75
Change-Id: Iee8fd68fcd1c430da09b11d68a57fc62ba4c6f75
(cherry picked from commit d29af631)
parent c640910e
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -435,7 +435,7 @@ status_t AudioFlinger::updateSecondaryOutputs(
        for (; i < mPlaybackThreads.size(); ++i) {
            PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
            Mutex::Autolock _tl(thread->mLock);
            sp<PlaybackThread::Track> track = thread->getTrackById_l(trackId);
            sp<IAfTrack> track = thread->getTrackById_l(trackId);
            if (track != nullptr) {
                ALOGD("%s trackId: %u", __func__, trackId);
                updateSecondaryOutputsForTrack_l(track.get(), thread, secondaryOutputs);
@@ -1107,7 +1107,7 @@ status_t AudioFlinger::createTrack(const media::CreateTrackRequest& _input,
    CreateTrackInput input = VALUE_OR_RETURN_STATUS(CreateTrackInput::fromAidl(_input));
    CreateTrackOutput output;

    sp<PlaybackThread::Track> track;
    sp<IAfTrack> track;
    sp<Client> client;
    status_t lStatus;
    audio_stream_type_t streamType;
@@ -1297,7 +1297,7 @@ status_t AudioFlinger::createTrack(const media::CreateTrackRequest& _input,
        AudioSystem::moveEffectsToIo(effectIds, effectThreadId);
    }

    output.audioTrack = PlaybackThread::Track::createIAudioTrackAdapter(track);
    output.audioTrack = IAfTrack::createIAudioTrackAdapter(track);
    _output = VALUE_OR_FATAL(output.toAidl());

Exit:
@@ -2549,7 +2549,7 @@ status_t AudioFlinger::createRecord(const media::CreateRecordRequest& _input,
    output.buffers = recordTrack->getBuffers();
    output.portId = portId;

    output.audioRecord = RecordThread::RecordTrack::createIAudioRecordAdapter(recordTrack);
    output.audioRecord = IAfRecordTrack::createIAudioRecordAdapter(recordTrack);
    _output = VALUE_OR_FATAL(output.toAidl());

Exit:
@@ -3899,7 +3899,7 @@ AudioFlinger::ThreadBase *AudioFlinger::hapticPlaybackThread_l() const {
}

void AudioFlinger::updateSecondaryOutputsForTrack_l(
        PlaybackThread::Track* track,
        IAfTrack* track,
        PlaybackThread* thread,
        const std::vector<audio_io_handle_t> &secondaryOutputs) const {
    TeePatches teePatches;
@@ -3989,14 +3989,14 @@ void AudioFlinger::updateSecondaryOutputsForTrack_l(
        patchTrack->setPeerProxy(patchRecord, true /* holdReference */);
        patchRecord->setPeerProxy(patchTrack, false /* holdReference */);
    }
    track->setTeePatchesToUpdate(std::move(teePatches));
    track->setTeePatchesToUpdate(&teePatches);  // TODO(b/288339104) void* to std::move()
}

sp<audioflinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
                                    audio_session_t triggerSession,
                                    audio_session_t listenerSession,
                                    const audioflinger::SyncEventCallback& callBack,
                                    const wp<RefBase>& cookie)
                                    const wp<IAfTrackBase>& cookie)
{
    Mutex::Autolock _l(mLock);

+3 −2
Original line number Diff line number Diff line
@@ -121,6 +121,7 @@

// include AudioFlinger component interfaces
#include "IAfEffect.h"
#include "IAfTrack.h"

namespace android {

@@ -397,7 +398,7 @@ public:
                                        audio_session_t triggerSession,
                                        audio_session_t listenerSession,
                                        const audioflinger::SyncEventCallback& callBack,
                                        const wp<RefBase>& cookie);
                                        const wp<IAfTrackBase>& cookie);

    bool        btNrecIsOff() const { return mBtNrecIsOff.load(); }

@@ -729,7 +730,7 @@ private:
              ThreadBase *hapticPlaybackThread_l() const;

              void updateSecondaryOutputsForTrack_l(
                      PlaybackThread::Track* track,
                      IAfTrack* track,
                      PlaybackThread* thread,
                      const std::vector<audio_io_handle_t>& secondaryOutputs) const;

+379 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

namespace android {

// Common interface to all Playback and Record tracks.
class IAfTrackBase : public virtual RefBase {
public:
    enum track_state : int32_t {
        IDLE,
        FLUSHED,  // for PlaybackTracks only
        STOPPED,
        // next 2 states are currently used for fast tracks
        // and offloaded tracks only
        STOPPING_1,  // waiting for first underrun
        STOPPING_2,  // waiting for presentation complete
        RESUMING,    // for PlaybackTracks only
        ACTIVE,
        PAUSING,
        PAUSED,
        STARTING_1,  // for RecordTrack only
        STARTING_2,  // for RecordTrack only
    };

    // where to allocate the data buffer
    enum alloc_type {
        ALLOC_CBLK,      // allocate immediately after control block
        ALLOC_READONLY,  // allocate from a separate read-only heap per thread
        ALLOC_PIPE,      // do not allocate; use the pipe buffer
        ALLOC_LOCAL,     // allocate a local buffer
        ALLOC_NONE,      // do not allocate:use the buffer passed to TrackBase constructor
    };

    enum track_type {
        TYPE_DEFAULT,
        TYPE_OUTPUT,
        TYPE_PATCH,
    };

    virtual status_t initCheck() const = 0;
    virtual status_t start(
            AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
            audio_session_t triggerSession = AUDIO_SESSION_NONE) = 0;
    virtual void stop() = 0;
    virtual sp<IMemory> getCblk() const = 0;
    virtual audio_track_cblk_t* cblk() const = 0;
    virtual audio_session_t sessionId() const = 0;
    virtual uid_t uid() const = 0;
    virtual pid_t creatorPid() const = 0;
    virtual uint32_t sampleRate() const = 0;
    virtual size_t frameSize() const = 0;
    virtual audio_port_handle_t portId() const = 0;
    virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) = 0;
    virtual track_state state() const = 0;
    virtual void setState(track_state state) = 0;
    virtual sp<IMemory> getBuffers() const = 0;
    virtual void* buffer() const = 0;
    virtual size_t bufferSize() const = 0;
    virtual bool isFastTrack() const = 0;
    virtual bool isDirect() const = 0;
    virtual bool isOutputTrack() const = 0;
    virtual bool isPatchTrack() const = 0;
    virtual bool isExternalTrack() const = 0;

    virtual void invalidate() = 0;
    virtual bool isInvalid() const = 0;

    virtual void terminate() = 0;
    virtual bool isTerminated() const = 0;

    virtual audio_attributes_t attributes() const = 0;
    virtual bool isSpatialized() const = 0;
    virtual bool isBitPerfect() const = 0;

    // not currently implemented in TrackBase, but overridden.
    virtual void destroy() {};  // MmapTrack doesn't implement.
    virtual void appendDumpHeader(String8& result) const = 0;
    virtual void appendDump(String8& result, bool active) const = 0;

    // Dup with AudioBufferProvider interface
    virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
    virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer) = 0;

    // Added for RecordTrack and OutputTrack
    // TODO(b/288339104) type
    virtual wp<Thread> thread() const = 0;
    virtual const sp<ServerProxy>& serverProxy() const = 0;

    // TEE_SINK
    virtual void dumpTee(int fd __unused, const std::string& reason __unused) const {};

    /** returns the buffer contents size converted to time in milliseconds
     * for PCM Playback or Record streaming tracks. The return value is zero for
     * PCM static tracks and not defined for non-PCM tracks.
     *
     * This may be called without the thread lock.
     */
    virtual double bufferLatencyMs() const = 0;

    /** returns whether the track supports server latency computation.
     * This is set in the constructor and constant throughout the track lifetime.
     */
    virtual bool isServerLatencySupported() const = 0;

    /** computes the server latency for PCM Playback or Record track
     * to the device sink/source.  This is the time for the next frame in the track buffer
     * written or read from the server thread to the device source or sink.
     *
     * This may be called without the thread lock, but latencyMs and fromTrack
     * may be not be synchronized. For example PatchPanel may not obtain the
     * thread lock before calling.
     *
     * \param latencyMs on success is set to the latency in milliseconds of the
     *        next frame written/read by the server thread to/from the track buffer
     *        from the device source/sink.
     * \param fromTrack on success is set to true if latency was computed directly
     *        from the track timestamp; otherwise set to false if latency was
     *        estimated from the server timestamp.
     *        fromTrack may be nullptr or omitted if not required.
     *
     * \returns OK or INVALID_OPERATION on failure.
     */
    virtual status_t getServerLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;

    /** computes the total client latency for PCM Playback or Record tracks
     * for the next client app access to the device sink/source; i.e. the
     * server latency plus the buffer latency.
     *
     * This may be called without the thread lock, but latencyMs and fromTrack
     * may be not be synchronized. For example PatchPanel may not obtain the
     * thread lock before calling.
     *
     * \param latencyMs on success is set to the latency in milliseconds of the
     *        next frame written/read by the client app to/from the track buffer
     *        from the device sink/source.
     * \param fromTrack on success is set to true if latency was computed directly
     *        from the track timestamp; otherwise set to false if latency was
     *        estimated from the server timestamp.
     *        fromTrack may be nullptr or omitted if not required.
     *
     * \returns OK or INVALID_OPERATION on failure.
     */
    virtual status_t getTrackLatencyMs(double* latencyMs, bool* fromTrack = nullptr) const = 0;

    // TODO: Consider making this external.
    struct FrameTime {
        int64_t frames;
        int64_t timeNs;
    };

    // KernelFrameTime is updated per "mix" period even for non-pcm tracks.
    virtual void getKernelFrameTime(FrameTime* ft) const = 0;

    virtual audio_format_t format() const = 0;
    virtual int id() const = 0;

    virtual const char* getTrackStateAsString() const = 0;

    // Called by the PlaybackThread to indicate that the track is becoming active
    // and a new interval should start with a given device list.
    virtual void logBeginInterval(const std::string& devices) = 0;

    // Called by the PlaybackThread to indicate the track is no longer active.
    virtual void logEndInterval() = 0;

    // Called to tally underrun frames in playback.
    virtual void tallyUnderrunFrames(size_t frames) = 0;

    virtual audio_channel_mask_t channelMask() const = 0;

    /** @return true if the track has changed (metadata or volume) since
     *          the last time this function was called,
     *          true if this function was never called since the track creation,
     *          false otherwise.
     *  Thread safe.
     */
    virtual bool readAndClearHasChanged() = 0;

    /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */
    virtual void setMetadataHasChanged() = 0;

    /**
     * For RecordTrack
     * TODO(b/288339104) either use this or add asRecordTrack or asTrack etc.
     */
    virtual void handleSyncStartEvent(const sp<audioflinger::SyncEvent>& event __unused){};

    // For Thread use, fast tracks and offloaded tracks only
    // TODO(b/288339104) rearrange to IAfTrack.
    virtual bool isStopped() const = 0;
    virtual bool isStopping() const = 0;
    virtual bool isStopping_1() const = 0;
    virtual bool isStopping_2() const = 0;
};

// Common interface for Playback tracks.
class IAfTrack : public virtual IAfTrackBase {
public:
    // createIAudioTrackAdapter() is a static constructor which creates an
    // IAudioTrack AIDL interface adapter from the Track object that
    // may be passed back to the client (if needed).
    //
    // Only one AIDL IAudioTrack interface adapter should be created per Track.
    static sp<media::IAudioTrack> createIAudioTrackAdapter(const sp<IAfTrack>& track);

    virtual void pause() = 0;
    virtual void flush() = 0;
    virtual audio_stream_type_t streamType() const = 0;
    virtual bool isOffloaded() const = 0;
    virtual bool isOffloadedOrDirect() const = 0;
    virtual bool isStatic() const = 0;
    virtual status_t setParameters(const String8& keyValuePairs) = 0;
    virtual status_t selectPresentation(int presentationId, int programId) = 0;
    virtual status_t attachAuxEffect(int EffectId) = 0;
    virtual void setAuxBuffer(int EffectId, int32_t* buffer) = 0;
    virtual int32_t* auxBuffer() const = 0;
    virtual void setMainBuffer(float* buffer) = 0;
    virtual float* mainBuffer() const = 0;
    virtual int auxEffectId() const = 0;
    virtual status_t getTimestamp(AudioTimestamp& timestamp) = 0;
    virtual void signal() = 0;
    virtual status_t getDualMonoMode(audio_dual_mono_mode_t* mode) const = 0;
    virtual status_t setDualMonoMode(audio_dual_mono_mode_t mode) = 0;
    virtual status_t getAudioDescriptionMixLevel(float* leveldB) const = 0;
    virtual status_t setAudioDescriptionMixLevel(float leveldB) = 0;
    virtual status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) const = 0;
    virtual status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) = 0;

    // implement FastMixerState::VolumeProvider interface
    virtual gain_minifloat_packed_t getVolumeLR() const = 0;

    // implement volume handling.
    virtual media::VolumeShaper::Status applyVolumeShaper(
            const sp<media::VolumeShaper::Configuration>& configuration,
            const sp<media::VolumeShaper::Operation>& operation) = 0;
    virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) const = 0;
    virtual sp<media::VolumeHandler> getVolumeHandler() const = 0;
    /** Set the computed normalized final volume of the track.
     * !masterMute * masterVolume * streamVolume * averageLRVolume */
    virtual void setFinalVolume(float volumeLeft, float volumeRight) = 0;
    virtual float getFinalVolume() const = 0;
    virtual void getFinalVolume(float* left, float* right) const = 0;

    using SourceMetadatas = std::vector<playback_track_metadata_v7_t>;
    using MetadataInserter = std::back_insert_iterator<SourceMetadatas>;
    /** Copy the track metadata in the provided iterator. Thread safe. */
    virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0;

    /** Return haptic playback of the track is enabled or not, used in mixer. */
    virtual bool getHapticPlaybackEnabled() const = 0;
    /** Set haptic playback of the track is enabled or not, should be
     * set after query or get callback from vibrator service */
    virtual void setHapticPlaybackEnabled(bool hapticPlaybackEnabled) = 0;
    /** Return at what intensity to play haptics, used in mixer. */
    virtual os::HapticScale getHapticIntensity() const = 0;
    /** Return the maximum amplitude allowed for haptics data, used in mixer. */
    virtual float getHapticMaxAmplitude() const = 0;
    /** Set intensity of haptic playback, should be set after querying vibrator service. */
    virtual void setHapticIntensity(os::HapticScale hapticIntensity) = 0;
    /** Set maximum amplitude allowed for haptic data, should be set after querying
     *  vibrator service.
     */
    virtual void setHapticMaxAmplitude(float maxAmplitude) = 0;
    virtual sp<os::ExternalVibration> getExternalVibration() const = 0;

    // This function should be called with holding thread lock.
    virtual void updateTeePatches() = 0;

    // TODO(b/288339104) type
    virtual void setTeePatchesToUpdate(
            const void* teePatchesToUpdate /* TeePatches& teePatchesToUpdate */) = 0;

    static bool checkServerLatencySupported(audio_format_t format, audio_output_flags_t flags) {
        return audio_is_linear_pcm(format) && (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) == 0;
    }

    virtual audio_output_flags_t getOutputFlags() const = 0;
    virtual float getSpeed() const = 0;

    /**
     * Updates the mute state and notifies the audio service. Call this only when holding player
     * thread lock.
     */
    virtual void processMuteEvent_l(
            const sp<IAudioManager>& audioManager, mute_state_t muteState) = 0;

    virtual void triggerEvents(AudioSystem::sync_event_t type) = 0;

    virtual void disable() = 0;
    virtual int& fastIndex() = 0;
    virtual bool isPlaybackRestricted() const = 0;
};

// playback track, used by DuplicatingThread
class IAfOutputTrack : public virtual IAfTrack {
public:
    virtual ssize_t write(void* data, uint32_t frames) = 0;
    virtual bool bufferQueueEmpty() const = 0;
    virtual bool isActive() const = 0;

    /** Set the metadatas of the upstream tracks. Thread safe. */
    virtual void setMetadatas(const SourceMetadatas& metadatas) = 0;
    /** returns client timestamp to the upstream duplicating thread. */
    virtual ExtendedTimestamp getClientProxyTimestamp() const = 0;
};

class IAfMmapTrack : public virtual IAfTrackBase {
public:
    // protected by MMapThread::mLock
    virtual void setSilenced_l(bool silenced) = 0;
    // protected by MMapThread::mLock
    virtual bool isSilenced_l() const = 0;
    // protected by MMapThread::mLock
    virtual bool getAndSetSilencedNotified_l() = 0;

    /**
     * Updates the mute state and notifies the audio service. Call this only when holding player
     * thread lock.
     */
    virtual void processMuteEvent_l(  // see IAfTrack
            const sp<IAudioManager>& audioManager, mute_state_t muteState) = 0;
};

class IAfRecordTrack : public virtual IAfTrackBase {
public:
    // createIAudioRecordAdapter() is a static constructor which creates an
    // IAudioRecord AIDL interface adapter from the RecordTrack object that
    // may be passed back to the client (if needed).
    //
    // Only one AIDL IAudioRecord interface adapter should be created per RecordTrack.
    static sp<media::IAudioRecord> createIAudioRecordAdapter(const sp<IAfRecordTrack>& recordTrack);

    // clear the buffer overflow flag
    virtual void clearOverflow() = 0;
    // set the buffer overflow flag and return previous value
    virtual bool setOverflow() = 0;

    // TODO(b/288339104) handleSyncStartEvent in IAfTrackBase should move here.
    virtual void clearSyncStartEvent() = 0;
    virtual void updateTrackFrameInfo(
            int64_t trackFramesReleased, int64_t sourceFramesRead, uint32_t halSampleRate,
            const ExtendedTimestamp& timestamp) = 0;

    virtual void setSilenced(bool silenced) = 0;
    virtual bool isSilenced() const = 0;
    virtual status_t getActiveMicrophones(
            std::vector<media::MicrophoneInfoFw>* activeMicrophones) const = 0;

    virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) = 0;
    virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
    virtual status_t shareAudioHistory(
            const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) = 0;
    virtual int32_t startFrames() const = 0;

    static bool checkServerLatencySupported(audio_format_t format, audio_input_flags_t flags) {
        return audio_is_linear_pcm(format) && (flags & AUDIO_INPUT_FLAG_HW_AV_SYNC) == 0;
    }

    using SinkMetadatas = std::vector<record_track_metadata_v7_t>;
    using MetadataInserter = std::back_insert_iterator<SinkMetadatas>;
    virtual void copyMetadataTo(MetadataInserter& backInserter) const = 0; // see IAfTrack
};

}  // namespace android
+18 −19
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@
#endif

// playback track
class MmapTrack : public TrackBase {
class MmapTrack : public TrackBase, public IAfMmapTrack {
public:
                MmapTrack(ThreadBase *thread,
                            const audio_attributes_t& attr,
@@ -32,26 +32,25 @@ public:
                            const android::content::AttributionSourceState& attributionSource,
                            pid_t creatorPid,
                            audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
    virtual             ~MmapTrack();
    ~MmapTrack() override;

                        // TrackBase virtual
    virtual status_t    initCheck() const;
    virtual status_t    start(AudioSystem::sync_event_t event,
                              audio_session_t triggerSession);
    virtual void        stop();
    virtual bool        isFastTrack() const { return false; }
            bool        isDirect() const override { return true; }
    status_t initCheck() const final;
    status_t start(
            AudioSystem::sync_event_t event, audio_session_t triggerSession) final;
    void stop() final;
    bool isFastTrack() const final { return false; }
    bool isDirect() const final { return true; }

            void        appendDumpHeader(String8& result);
            void        appendDump(String8& result, bool active);
    void appendDumpHeader(String8& result) const final;
    void appendDump(String8& result, bool active) const final;

                        // protected by MMapThread::mLock
            void        setSilenced_l(bool silenced) { mSilenced = silenced;
    void setSilenced_l(bool silenced) final { mSilenced = silenced;
                                                       mSilencedNotified = false;}
                        // protected by MMapThread::mLock
            bool        isSilenced_l() const { return mSilenced; }
    bool isSilenced_l() const final { return mSilenced; }
                        // protected by MMapThread::mLock
            bool        getAndSetSilencedNotified_l() { bool silencedNotified = mSilencedNotified;
    bool getAndSetSilencedNotified_l() final { bool silencedNotified = mSilencedNotified;
                                                        mSilencedNotified = true;
                                                        return silencedNotified; }

@@ -61,7 +60,7 @@ public:
     */
    void processMuteEvent_l(const sp<IAudioManager>& audioManager,
                            mute_state_t muteState)
                            REQUIRES(AudioFlinger::MmapPlaybackThread::mLock);
                            REQUIRES(AudioFlinger::MmapPlaybackThread::mLock) final;
private:
    friend class MmapThread;

@@ -72,11 +71,11 @@ private:
    // releaseBuffer() not overridden

    // ExtendedAudioBufferProvider interface
    virtual size_t framesReady() const;
    virtual int64_t framesReleased() const;
    virtual void onTimestamp(const ExtendedTimestamp &timestamp);
    size_t framesReady() const final;
    int64_t framesReleased() const final;
    void onTimestamp(const ExtendedTimestamp &timestamp) final;

    pid_t mPid;
    const pid_t mPid;
    bool  mSilenced;            // protected by MMapThread::mLock
    bool  mSilencedNotified;    // protected by MMapThread::mLock

+1 −1
Original line number Diff line number Diff line
@@ -679,7 +679,7 @@ status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
    // If so, do a frame diff and time difference computation to estimate
    // the total patch latency. This requires that frame counts are reported by the
    // HAL are matched properly in the case of record overruns and playback underruns.
    ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
    IAfTrack::FrameTime recordFT{}, playFT{};
    recordTrack->getKernelFrameTime(&recordFT);
    playbackTrack->getKernelFrameTime(&playFT);
    if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
Loading