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

Commit f878a8dc authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: only send timestamps if queue less than half full

This prevents the stream from getting suspended
if not read or written fast enough.

Bug: 129140370
Test: test_full_queue
Change-Id: Icc5ddc72c99fb3c518736f2a50d6c6a0276ee196
parent 235684f2
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -343,6 +343,20 @@ aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t
    return writeUpMessageQueue(&command);
}

bool AAudioServiceStreamBase::isUpMessageQueueBusy() {
    std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
    if (mUpMessageQueue == nullptr) {
        ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__);
        return true;
    }
    int32_t framesAvailable = mUpMessageQueue->getFifoBuffer()
        ->getFullFramesAvailable();
    int32_t capacity = mUpMessageQueue->getFifoBuffer()
        ->getBufferCapacityInFrames();
    // Is it half full or more
    return framesAvailable >= (capacity / 2);
}

aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
    std::lock_guard<std::mutex> lock(mUpMessageQueueLock);
    if (mUpMessageQueue == nullptr) {
@@ -366,6 +380,13 @@ aaudio_result_t AAudioServiceStreamBase::sendXRunCount(int32_t xRunCount) {

aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
    AAudioServiceMessage command;
    // It is not worth filling up the queue with timestamps.
    // That can cause the stream to get suspended.
    // So just drop the timestamp if the queue is getting full.
    if (isUpMessageQueueBusy()) {
        return AAUDIO_OK;
    }

    // Send a timestamp for the clock model.
    aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
                                                    &command.timestamp.timestamp);
+6 −0
Original line number Diff line number Diff line
@@ -312,6 +312,12 @@ protected:
    android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;

private:

    /**
     * @return true if the queue is getting full.
     */
    bool isUpMessageQueueBusy();

    aaudio_handle_t         mHandle = -1;
    bool                    mFlowing = false;