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

Commit 62750c2f authored by jiabin's avatar jiabin
Browse files

libaudiohal: call prepareToDisconnectExternalDevice when it is available

Bug: 279824103
Test: atest audiosystem_tests
Change-Id: I03149b454f02aa670c50f2345d64b3f52d972b2a
parent f57a2e0f
Loading
Loading
Loading
Loading
+37 −14
Original line number Diff line number Diff line
@@ -903,15 +903,39 @@ status_t DeviceHalAidl::getSoundDoseInterface(const std::string& module,
}

status_t DeviceHalAidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) {
    // There is not AIDL API defined for `prepareToDisconnectExternalDevice`.
    ALOGD("%p %s::%s", this, getClassName().c_str(), __func__);
    TIME_CHECK();
    if (mModule == nullptr) return NO_INIT;
    if (port == nullptr) {
        return BAD_VALUE;
    }
    const bool isInput = VALUE_OR_RETURN_STATUS(
            ::aidl::android::portDirection(port->role, port->type)) ==
                    ::aidl::android::AudioPortDirection::INPUT;
    AudioPort aidlPort = VALUE_OR_RETURN_STATUS(
            ::aidl::android::legacy2aidl_audio_port_v7_AudioPort(*port, isInput));
    if (aidlPort.ext.getTag() != AudioPortExt::device) {
        ALOGE("%s: provided port is not a device port (module %s): %s",
              __func__, mInstance.c_str(), aidlPort.toString().c_str());
        return BAD_VALUE;
    }
    status_t status = NO_ERROR;
    {
        std::lock_guard l(mLock);
        status = mMapper.prepareToDisconnectExternalDevice(aidlPort);
    }
    if (status == UNKNOWN_TRANSACTION) {
        // If there is not AIDL API defined for `prepareToDisconnectExternalDevice`.
        // Call `setConnectedState` instead.
    // TODO(b/279824103): call prepareToDisconnectExternalDevice when it is added.
        RETURN_STATUS_IF_ERROR(setConnectedState(port, false /*connected*/));
        std::lock_guard l(mLock);
        mDeviceDisconnectionNotified.insert(port->id);
        // Return that there was no error as otherwise the disconnection procedure will not be
        // considered complete for upper layers, and 'setConnectedState' will not be called again
        return OK;
    } else {
        return status;
    }
}

status_t DeviceHalAidl::setConnectedState(const struct audio_port_v7 *port, bool connected) {
@@ -925,11 +949,10 @@ status_t DeviceHalAidl::setConnectedState(const struct audio_port_v7 *port, bool
        std::lock_guard l(mLock);
        if (mDeviceDisconnectionNotified.erase(port->id) > 0) {
            // For device disconnection, APM will first call `prepareToDisconnectExternalDevice`
            // and then call `setConnectedState`. However, there is no API for
            // `prepareToDisconnectExternalDevice` yet. In that case, `setConnectedState` will be
            // called when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if
            // previous call is successful. Also remove the cache here to avoid a large cache after
            // a long run.
            // and then call `setConnectedState`. If `prepareToDisconnectExternalDevice` doesn't
            // exit, `setConnectedState` will be called when calling
            // `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous call is
            // successful. Also remove the cache here to avoid a large cache after a long run.
            return OK;
        }
    }
+8 −0
Original line number Diff line number Diff line
@@ -666,6 +666,14 @@ bool Hal2AidlMapper::isPortBeingHeld(int32_t portId) {
    return false;
}

status_t Hal2AidlMapper::prepareToDisconnectExternalDevice(const AudioPort& devicePort) {
    auto portsIt = findPort(devicePort.ext.get<AudioPortExt::device>().device);
    if (portsIt == mPorts.end()) {
        return BAD_VALUE;
    }
    return statusTFromBinderStatus(mModule->prepareToDisconnectExternalDevice(portsIt->second.id));
}

status_t Hal2AidlMapper::prepareToOpenStream(
        int32_t ioHandle, const AudioDevice& device, const AudioIoFlags& flags,
        AudioSource source, Cleanups* cleanups, AudioConfig* config,
+2 −0
Original line number Diff line number Diff line
@@ -72,6 +72,8 @@ class Hal2AidlMapper {
        return ::aidl::android::convertContainer(mRoutes, routes, converter);
    }
    status_t initialize();
    status_t prepareToDisconnectExternalDevice(
            const ::aidl::android::media::audio::common::AudioPort& devicePort);
    // If the resulting 'mixPortConfig->id' is 0, that means the stream was not created,
    // and 'config' is a suggested config.
    status_t prepareToOpenStream(