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

Commit 068e08ee authored by Andy Hung's avatar Andy Hung
Browse files

SyncEvent: modernize C++

And add unit test mediasyncevent_tests.

Test: atest mediasyncevent_tests
Bug: 283021652
Merged-In: I37711f4271c68042f178db9370549c0c3260ace8
Change-Id: I37711f4271c68042f178db9370549c0c3260ace8
parent 9a820085
Loading
Loading
Loading
Loading
+13 −11
Original line number Original line Diff line number Diff line
@@ -1236,18 +1236,19 @@ status_t AudioFlinger::createTrack(const media::CreateTrackRequest& _input,
        }
        }


        // Look for sync events awaiting for a session to be used.
        // Look for sync events awaiting for a session to be used.
        for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
        for (auto it = mPendingSyncEvents.begin(); it != mPendingSyncEvents.end();) {
            if (mPendingSyncEvents[i]->triggerSession() == sessionId) {
            if ((*it)->triggerSession() == sessionId) {
                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
                if (thread->isValidSyncEvent(*it)) {
                    if (lStatus == NO_ERROR) {
                    if (lStatus == NO_ERROR) {
                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
                        (void) track->setSyncEvent(*it);
                    } else {
                    } else {
                        mPendingSyncEvents[i]->cancel();
                        (*it)->cancel();
                    }
                    }
                    mPendingSyncEvents.removeAt(i);
                    it = mPendingSyncEvents.erase(it);
                    i--;
                    continue;
                }
                }
            }
            }
            ++it;
        }
        }
        if ((output.flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) == AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
        if ((output.flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) == AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
            setAudioHwSyncForSession_l(thread, sessionId);
            setAudioHwSyncForSession_l(thread, sessionId);
@@ -3919,15 +3920,16 @@ void AudioFlinger::updateSecondaryOutputsForTrack_l(
    track->setTeePatches(std::move(teePatches));
    track->setTeePatches(std::move(teePatches));
}
}


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


    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
    auto event = sp<audioflinger::SyncEvent>::make(
            type, triggerSession, listenerSession, callBack, cookie);
    status_t playStatus = NAME_NOT_FOUND;
    status_t playStatus = NAME_NOT_FOUND;
    status_t recStatus = NAME_NOT_FOUND;
    status_t recStatus = NAME_NOT_FOUND;
    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
@@ -3943,7 +3945,7 @@ sp<SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
        }
        }
    }
    }
    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
        mPendingSyncEvents.add(event);
        mPendingSyncEvents.emplace_back(event);
    } else {
    } else {
        ALOGV("createSyncEvent() invalid event %d", event->type());
        ALOGV("createSyncEvent() invalid event %d", event->type());
        event.clear();
        event.clear();
+4 −4
Original line number Original line Diff line number Diff line
@@ -375,10 +375,10 @@ public:


    static inline std::atomic<AudioFlinger *> gAudioFlinger = nullptr;
    static inline std::atomic<AudioFlinger *> gAudioFlinger = nullptr;


    sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
    sp<audioflinger::SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
                                        audio_session_t triggerSession,
                                        audio_session_t triggerSession,
                                        audio_session_t listenerSession,
                                        audio_session_t listenerSession,
                                        sync_event_callback_t callBack,
                                        audioflinger::SyncEventCallback callBack,
                                        const wp<RefBase>& cookie);
                                        const wp<RefBase>& cookie);


    bool        btNrecIsOff() const { return mBtNrecIsOff.load(); }
    bool        btNrecIsOff() const { return mBtNrecIsOff.load(); }
@@ -939,8 +939,8 @@ using effect_buffer_t = int16_t;
                bool        masterMute_l() const;
                bool        masterMute_l() const;
                AudioHwDevice* loadHwModule_l(const char *name);
                AudioHwDevice* loadHwModule_l(const char *name);


                Vector < sp<SyncEvent> > mPendingSyncEvents; // sync events awaiting for a session
                // sync events awaiting for a session to be created.
                                                             // to be created
                std::list<sp<audioflinger::SyncEvent>> mPendingSyncEvents;


                // Effect chains without a valid thread
                // Effect chains without a valid thread
                DefaultKeyedVector< audio_session_t , sp<EffectChain> > mOrphanEffectChains;
                DefaultKeyedVector< audio_session_t , sp<EffectChain> > mOrphanEffectChains;
+1 −1
Original line number Original line Diff line number Diff line
@@ -131,7 +131,7 @@ public:
// implement FastMixerState::VolumeProvider interface
// implement FastMixerState::VolumeProvider interface
    virtual gain_minifloat_packed_t getVolumeLR();
    virtual gain_minifloat_packed_t getVolumeLR();


    virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
            status_t    setSyncEvent(const sp<audioflinger::SyncEvent>& event) override;


    virtual bool        isFastTrack() const { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; }
    virtual bool        isFastTrack() const { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; }


+2 −2
Original line number Original line Diff line number Diff line
@@ -58,7 +58,7 @@ public:
            void        appendDumpHeader(String8& result);
            void        appendDumpHeader(String8& result);
            void        appendDump(String8& result, bool active);
            void        appendDump(String8& result, bool active);


            void        handleSyncStartEvent(const sp<SyncEvent>& event);
            void        handleSyncStartEvent(const sp<audioflinger::SyncEvent>& event);
            void        clearSyncStartEvent();
            void        clearSyncStartEvent();


            void        updateTrackFrameInfo(int64_t trackFramesReleased,
            void        updateTrackFrameInfo(int64_t trackFramesReleased,
@@ -107,7 +107,7 @@ private:


            // sync event triggering actual audio capture. Frames read before this event will
            // sync event triggering actual audio capture. Frames read before this event will
            // be dropped and therefore not read by the application.
            // be dropped and therefore not read by the application.
            sp<SyncEvent>                       mSyncStartEvent;
            sp<audioflinger::SyncEvent>        mSyncStartEvent;


            // number of captured frames to drop after the start sync event has been received.
            // number of captured frames to drop after the start sync event has been received.
            // when < 0, maximum frames to drop before starting capture even if sync event is
            // when < 0, maximum frames to drop before starting capture even if sync event is
+12 −8
Original line number Original line Diff line number Diff line
@@ -3278,7 +3278,7 @@ uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
    return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
    return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
}
}


status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<audioflinger::SyncEvent>& event)
{
{
    if (!isValidSyncEvent(event)) {
    if (!isValidSyncEvent(event)) {
        return BAD_VALUE;
        return BAD_VALUE;
@@ -3297,7 +3297,8 @@ status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
    return NAME_NOT_FOUND;
    return NAME_NOT_FOUND;
}
}


bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
bool AudioFlinger::PlaybackThread::isValidSyncEvent(
        const sp<audioflinger::SyncEvent>& event) const
{
{
    return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
    return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
}
}
@@ -8680,9 +8681,9 @@ status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrac
    }
    }
}
}


void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
void AudioFlinger::RecordThread::syncStartEventCallback(const wp<audioflinger::SyncEvent>& event)
{
{
    sp<SyncEvent> strongEvent = event.promote();
    sp<audioflinger::SyncEvent> strongEvent = event.promote();


    if (strongEvent != 0) {
    if (strongEvent != 0) {
        sp<RefBase> ptr = strongEvent->cookie().promote();
        sp<RefBase> ptr = strongEvent->cookie().promote();
@@ -8721,12 +8722,14 @@ bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
    return false;
    return false;
}
}


bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
bool AudioFlinger::RecordThread::isValidSyncEvent(
        const sp<audioflinger::SyncEvent>& /* event */) const
{
{
    return false;
    return false;
}
}


status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused)
status_t AudioFlinger::RecordThread::setSyncEvent(
        const sp<audioflinger::SyncEvent>& event __unused)
{
{
#if 0   // This branch is currently dead code, but is preserved in case it will be needed in future
#if 0   // This branch is currently dead code, but is preserved in case it will be needed in future
    if (!isValidSyncEvent(event)) {
    if (!isValidSyncEvent(event)) {
@@ -10259,12 +10262,13 @@ void AudioFlinger::MmapThread::threadLoop_exit()
    // and because it can cause a recursive mutex lock on stop().
    // and because it can cause a recursive mutex lock on stop().
}
}


status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused)
status_t AudioFlinger::MmapThread::setSyncEvent(const sp<audioflinger::SyncEvent>& /* event */)
{
{
    return BAD_VALUE;
    return BAD_VALUE;
}
}


bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
bool AudioFlinger::MmapThread::isValidSyncEvent(
        const sp<audioflinger::SyncEvent>& /* event */) const
{
{
    return false;
    return false;
}
}
Loading