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

Commit ee0d4304 authored by Jiabin Huang's avatar Jiabin Huang Committed by Android (Google) Code Review
Browse files

Merge "Move start/stopClient to command queue." into main

parents 2b462802 b86b403e
Loading
Loading
Loading
Loading
+35 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <iostream>
#include <mutex>

#include <com_android_media_aaudio.h>
#include <media/MediaMetricsItem.h>
#include <media/TypeConverter.h>
#include <mediautils/SchedulingPolicyService.h>
@@ -219,7 +220,7 @@ aaudio_result_t AAudioServiceStreamBase::close_l() {
    return closeAndClear();
}

aaudio_result_t AAudioServiceStreamBase::startDevice() {
aaudio_result_t AAudioServiceStreamBase::startDevice_l() {
    mClientHandle = AUDIO_PORT_HANDLE_NONE;
    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
    if (endpoint == nullptr) {
@@ -274,7 +275,7 @@ aaudio_result_t AAudioServiceStreamBase::start_l() {
    mAtomicStreamTimestamp.clear();

    mClientHandle = AUDIO_PORT_HANDLE_NONE;
    result = startDevice();
    result = startDevice_l();
    if (result != AAUDIO_OK) goto error;

    // This should happen at the end of the start.
@@ -520,6 +521,18 @@ void AAudioServiceStreamBase::run() {
                                                       : exitStandby_l(param->mParcelable);
                    standbyTime = AudioClock::getNanoseconds() + IDLE_TIMEOUT_NANOS;
                } break;
                case START_CLIENT: {
                    auto param = (StartClientParam *) command->parameter.get();
                    command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
                                                       : startClient_l(param->mClient,
                                                                       param->mAttr,
                                                                       param->mClientHandle);
                } break;
                case STOP_CLIENT: {
                    auto param = (StopClientParam *) command->parameter.get();
                    command->result = param == nullptr ? AAUDIO_ERROR_ILLEGAL_ARGUMENT
                                                       : stopClient_l(param->mClientHandle);
                } break;
                default:
                    ALOGE("Invalid command op code: %d", command->operationCode);
                    break;
@@ -732,6 +745,26 @@ aaudio_result_t AAudioServiceStreamBase::exitStandby(AudioEndpointParcelable *pa
    return mCommandQueue.sendCommand(command);
}

aaudio_result_t AAudioServiceStreamBase::sendStartClientCommand(const android::AudioClient &client,
                                                                const audio_attributes_t *attr,
                                                                audio_port_handle_t *clientHandle) {
    auto command = std::make_shared<AAudioCommand>(
            START_CLIENT,
            std::make_shared<StartClientParam>(client, attr, clientHandle),
            true /*waitForReply*/,
            TIMEOUT_NANOS);
    return mCommandQueue.sendCommand(command);
}

aaudio_result_t AAudioServiceStreamBase::sendStopClientCommand(audio_port_handle_t clientHandle) {
    auto command = std::make_shared<AAudioCommand>(
            STOP_CLIENT,
            std::make_shared<StopClientParam>(clientHandle),
            true /*waitForReply*/,
            TIMEOUT_NANOS);
    return mCommandQueue.sendCommand(command);
}

void AAudioServiceStreamBase::onVolumeChanged(float volume) {
    sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
}
+43 −1
Original line number Diff line number Diff line
@@ -279,7 +279,7 @@ protected:
     * Device specific startup.
     * @return AAUDIO_OK or negative error.
     */
    virtual aaudio_result_t startDevice();
    virtual aaudio_result_t startDevice_l() REQUIRES(mLock);

    aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command)
            EXCLUDES(mUpMessageQueueLock);
@@ -288,6 +288,12 @@ protected:

    aaudio_result_t sendXRunCount(int32_t xRunCount);

    aaudio_result_t sendStartClientCommand(const android::AudioClient& client,
                                           const audio_attributes_t *attr,
                                           audio_port_handle_t *clientHandle) EXCLUDES(mLock);

    aaudio_result_t sendStopClientCommand(audio_port_handle_t clientHandle) EXCLUDES(mLock);

    /**
     * @param positionFrames
     * @param timeNanos
@@ -342,6 +348,40 @@ protected:
    }
    virtual void reportData_l() REQUIRES(mLock) { return; }

    class StartClientParam : public AAudioCommandParam {
    public:
        StartClientParam(const android::AudioClient& client, const audio_attributes_t* attr,
                         audio_port_handle_t* clientHandle)
                : AAudioCommandParam(), mClient(client), mAttr(attr), mClientHandle(clientHandle) {
        }
        ~StartClientParam() override = default;

        android::AudioClient mClient;
        const audio_attributes_t* mAttr;
        audio_port_handle_t* mClientHandle;
    };
    virtual aaudio_result_t startClient_l(
            const android::AudioClient& client,
            const audio_attributes_t *attr __unused,
            audio_port_handle_t *clientHandle __unused) REQUIRES(mLock) {
        ALOGD("AAudioServiceStreamBase::startClient_l(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
        return AAUDIO_ERROR_UNAVAILABLE;
    }

    class StopClientParam : public AAudioCommandParam {
    public:
        explicit StopClientParam(audio_port_handle_t clientHandle)
                : AAudioCommandParam(), mClientHandle(clientHandle) {
        }
        ~StopClientParam() override = default;

        audio_port_handle_t mClientHandle;
    };
    virtual aaudio_result_t stopClient_l(audio_port_handle_t clientHandle) REQUIRES(mLock) {
        ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
        return AAUDIO_ERROR_UNAVAILABLE;
    }

    pid_t                   mRegisteredClientThread = ILLEGAL_THREAD_ID;

    std::mutex              mUpMessageQueueLock;
@@ -358,6 +398,8 @@ protected:
        UNREGISTER_AUDIO_THREAD,
        GET_DESCRIPTION,
        EXIT_STANDBY,
        START_CLIENT,
        STOP_CLIENT,
    };
    AAudioThread            mCommandThread;
    std::atomic_bool        mThreadEnabled{false};
+39 −6
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <iostream>
#include <stdint.h>

#include <com_android_media_aaudio.h>
#include <utils/String16.h>
#include <media/nbaio/AudioStreamOutSink.h>
#include <media/MmapStreamInterface.h>
@@ -83,11 +84,12 @@ aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest
}

// Start the flow of data.
aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
    aaudio_result_t result = AAudioServiceStreamBase::startDevice();
aaudio_result_t AAudioServiceStreamMMAP::startDevice_l() {
    aaudio_result_t result = AAudioServiceStreamBase::startDevice_l();
    if (!mInService && result == AAUDIO_OK) {
        // Note that this can sometimes take 200 to 300 msec for a cold start!
        result = startClient(mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle);
        result = startClient_l(
                mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle);
    }
    return result;
}
@@ -100,7 +102,7 @@ aaudio_result_t AAudioServiceStreamMMAP::pause_l() {
    aaudio_result_t result = AAudioServiceStreamBase::pause_l();
    // TODO put before base::pause()?
    if (!mInService) {
        (void) stopClient(mClientHandle);
        (void) stopClient_l(mClientHandle);
    }
    return result;
}
@@ -112,7 +114,7 @@ aaudio_result_t AAudioServiceStreamMMAP::stop_l() {
    aaudio_result_t result = AAudioServiceStreamBase::stop_l();
    // TODO put before base::stop()?
    if (!mInService) {
        (void) stopClient(mClientHandle);
        (void) stopClient_l(mClientHandle);
    }
    return result;
}
@@ -149,6 +151,9 @@ aaudio_result_t AAudioServiceStreamMMAP::exitStandby_l(AudioEndpointParcelable*
aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
                                                     const audio_attributes_t *attr,
                                                     audio_port_handle_t *clientHandle) {
    if (com::android::media::aaudio::start_stop_client_from_command_thread()) {
        return sendStartClientCommand(client, attr, clientHandle);
    } else {
        sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
        if (endpoint == nullptr) {
            ALOGE("%s() has no endpoint", __func__);
@@ -158,8 +163,36 @@ aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient&
        aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
        return result;
    }
}

aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
    if (com::android::media::aaudio::start_stop_client_from_command_thread()) {
        return sendStopClientCommand(clientHandle);
    } else {
        sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
        if (endpoint == nullptr) {
            ALOGE("%s() has no endpoint", __func__);
            return AAUDIO_ERROR_INVALID_STATE;
        }
        aaudio_result_t result = endpoint->stopClient(clientHandle);
        return result;
    }
}

aaudio_result_t AAudioServiceStreamMMAP::startClient_l(const android::AudioClient& client,
                                                       const audio_attributes_t *attr,
                                                       audio_port_handle_t *clientHandle) {
    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
    if (endpoint == nullptr) {
        ALOGE("%s() has no endpoint", __func__);
        return AAUDIO_ERROR_INVALID_STATE;
    }
    // Start the client on behalf of the application. Generate a new porthandle.
    aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
    return result;
}

aaudio_result_t AAudioServiceStreamMMAP::stopClient_l(audio_port_handle_t clientHandle) {
    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
    if (endpoint == nullptr) {
        ALOGE("%s() has no endpoint", __func__);
+7 −1
Original line number Diff line number Diff line
@@ -93,7 +93,13 @@ protected:
     * Device specific startup.
     * @return AAUDIO_OK or negative error.
     */
    aaudio_result_t startDevice() override;
    aaudio_result_t startDevice_l() REQUIRES(mLock) override;

    aaudio_result_t startClient_l(const android::AudioClient& client,
                                  const audio_attributes_t *attr,
                                  audio_port_handle_t *clientHandle) REQUIRES(mLock) override;

    aaudio_result_t stopClient_l(audio_port_handle_t clientHandle) REQUIRES(mLock) override;

private: