/*
**
** Copyright 2007, 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.
*/

#define LOG_TAG "IAudioFlinger"
//#define LOG_NDEBUG 0

#include <utils/Log.h>

#include <stdint.h>
#include <sys/types.h>
#include "IAudioFlinger.h"
#include <binder/IPCThreadState.h>
#include <binder/Parcel.h>
#include <system/thread_defs.h>

namespace android {

using aidl_utils::statusTFromBinderStatus;
using binder::Status;
using media::audio::common::AudioChannelLayout;
using media::audio::common::AudioFormatDescription;
using media::audio::common::AudioMMapPolicyInfo;
using media::audio::common::AudioMMapPolicyType;
using media::audio::common::AudioMode;
using media::audio::common::AudioStreamType;
using media::audio::common::AudioUuid;

#define MAX_ITEMS_PER_LIST 1024

#define VALUE_OR_RETURN_BINDER(x)                                 \
    ({                                                            \
       auto _tmp = (x);                                           \
       if (!_tmp.ok()) return Status::fromStatusT(_tmp.error());  \
       std::move(_tmp.value()); \
     })

#define RETURN_BINDER_IF_ERROR(x)                         \
    {                                                     \
       auto _tmp = (x);                                   \
       if (_tmp != OK) return Status::fromStatusT(_tmp);  \
    }

ConversionResult<media::CreateTrackRequest> IAudioFlinger::CreateTrackInput::toAidl() const {
    media::CreateTrackRequest aidl;
    aidl.attr = VALUE_OR_RETURN(legacy2aidl_audio_attributes_t_AudioAttributes(attr));
    // Do not be mislead by 'Input'--this is an input to 'createTrack', which creates output tracks.
    aidl.config = VALUE_OR_RETURN(legacy2aidl_audio_config_t_AudioConfig(
                    config, false /*isInput*/));
    aidl.clientInfo = VALUE_OR_RETURN(legacy2aidl_AudioClient_AudioClient(clientInfo));
    aidl.sharedBuffer = VALUE_OR_RETURN(legacy2aidl_NullableIMemory_SharedFileRegion(sharedBuffer));
    aidl.notificationsPerBuffer = VALUE_OR_RETURN(convertIntegral<int32_t>(notificationsPerBuffer));
    aidl.speed = speed;
    aidl.audioTrackCallback = audioTrackCallback;
    aidl.flags = VALUE_OR_RETURN(legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
    aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(frameCount));
    aidl.notificationFrameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(notificationFrameCount));
    aidl.selectedDeviceId = VALUE_OR_RETURN(
            legacy2aidl_audio_port_handle_t_int32_t(selectedDeviceId));
    aidl.sessionId = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(sessionId));
    return aidl;
}

ConversionResult<IAudioFlinger::CreateTrackInput>
IAudioFlinger::CreateTrackInput::fromAidl(const media::CreateTrackRequest& aidl) {
    IAudioFlinger::CreateTrackInput legacy;
    legacy.attr = VALUE_OR_RETURN(aidl2legacy_AudioAttributes_audio_attributes_t(aidl.attr));
    // Do not be mislead by 'Input'--this is an input to 'createTrack', which creates output tracks.
    legacy.config = VALUE_OR_RETURN(
            aidl2legacy_AudioConfig_audio_config_t(aidl.config, false /*isInput*/));
    legacy.clientInfo = VALUE_OR_RETURN(aidl2legacy_AudioClient_AudioClient(aidl.clientInfo));
    legacy.sharedBuffer = VALUE_OR_RETURN(aidl2legacy_NullableSharedFileRegion_IMemory(aidl.sharedBuffer));
    legacy.notificationsPerBuffer = VALUE_OR_RETURN(
            convertIntegral<uint32_t>(aidl.notificationsPerBuffer));
    legacy.speed = aidl.speed;
    legacy.audioTrackCallback = aidl.audioTrackCallback;
    legacy.flags = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_output_flags_t_mask(aidl.flags));
    legacy.frameCount = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCount));
    legacy.notificationFrameCount = VALUE_OR_RETURN(
            convertIntegral<size_t>(aidl.notificationFrameCount));
    legacy.selectedDeviceId = VALUE_OR_RETURN(
            aidl2legacy_int32_t_audio_port_handle_t(aidl.selectedDeviceId));
    legacy.sessionId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.sessionId));
    return legacy;
}

ConversionResult<media::CreateTrackResponse>
IAudioFlinger::CreateTrackOutput::toAidl() const {
    media::CreateTrackResponse aidl;
    aidl.flags = VALUE_OR_RETURN(legacy2aidl_audio_output_flags_t_int32_t_mask(flags));
    aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(frameCount));
    aidl.notificationFrameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(notificationFrameCount));
    aidl.selectedDeviceIds = VALUE_OR_RETURN(convertContainer<std::vector<int32_t>>(
            selectedDeviceIds, legacy2aidl_audio_port_handle_t_int32_t));
    aidl.sessionId = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(sessionId));
    aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(sampleRate));
    aidl.streamType =  VALUE_OR_RETURN(
            legacy2aidl_audio_stream_type_t_AudioStreamType(streamType));
    aidl.afFrameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(afFrameCount));
    aidl.afSampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(afSampleRate));
    aidl.afLatencyMs = VALUE_OR_RETURN(convertIntegral<int32_t>(afLatencyMs));
    aidl.afChannelMask = VALUE_OR_RETURN(
            legacy2aidl_audio_channel_mask_t_AudioChannelLayout(afChannelMask, false /*isInput*/));
    aidl.afFormat = VALUE_OR_RETURN(
            legacy2aidl_audio_format_t_AudioFormatDescription(afFormat));
    aidl.afTrackFlags = VALUE_OR_RETURN(
            legacy2aidl_audio_output_flags_t_int32_t_mask(afTrackFlags));
    aidl.outputId = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(outputId));
    aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(portId));
    aidl.audioTrack = audioTrack;
    return aidl;
}

ConversionResult<IAudioFlinger::CreateTrackOutput>
IAudioFlinger::CreateTrackOutput::fromAidl(
        const media::CreateTrackResponse& aidl) {
    IAudioFlinger::CreateTrackOutput legacy;
    legacy.flags = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_output_flags_t_mask(aidl.flags));
    legacy.frameCount = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCount));
    legacy.notificationFrameCount = VALUE_OR_RETURN(
            convertIntegral<size_t>(aidl.notificationFrameCount));
    legacy.selectedDeviceIds = VALUE_OR_RETURN(convertContainer<DeviceIdVector>(
            aidl.selectedDeviceIds, aidl2legacy_int32_t_audio_port_handle_t));
    legacy.sessionId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.sessionId));
    legacy.sampleRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate));
    legacy.streamType = VALUE_OR_RETURN(
            aidl2legacy_AudioStreamType_audio_stream_type_t(aidl.streamType));
    legacy.afFrameCount = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.afFrameCount));
    legacy.afSampleRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.afSampleRate));
    legacy.afLatencyMs = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.afLatencyMs));
    legacy.afChannelMask = VALUE_OR_RETURN(
            aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.afChannelMask,
                                                                false /*isInput*/));
    legacy.afFormat = VALUE_OR_RETURN(
            aidl2legacy_AudioFormatDescription_audio_format_t(aidl.afFormat));
    legacy.afTrackFlags = VALUE_OR_RETURN(
            aidl2legacy_int32_t_audio_output_flags_t_mask(aidl.afTrackFlags));
    legacy.outputId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.outputId));
    legacy.portId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
    legacy.audioTrack = aidl.audioTrack;
    return legacy;
}

ConversionResult<media::CreateRecordRequest>
IAudioFlinger::CreateRecordInput::toAidl() const {
    media::CreateRecordRequest aidl;
    aidl.attr = VALUE_OR_RETURN(legacy2aidl_audio_attributes_t_AudioAttributes(attr));
    aidl.config = VALUE_OR_RETURN(
            legacy2aidl_audio_config_base_t_AudioConfigBase(config, true /*isInput*/));
    aidl.clientInfo = VALUE_OR_RETURN(legacy2aidl_AudioClient_AudioClient(clientInfo));
    aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(riid));
    aidl.maxSharedAudioHistoryMs = VALUE_OR_RETURN(
            convertIntegral<int32_t>(maxSharedAudioHistoryMs));
    aidl.flags = VALUE_OR_RETURN(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
    aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(frameCount));
    aidl.notificationFrameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(notificationFrameCount));
    aidl.selectedDeviceId = VALUE_OR_RETURN(
            legacy2aidl_audio_port_handle_t_int32_t(selectedDeviceId));
    aidl.sessionId = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(sessionId));
    return aidl;
}

ConversionResult<IAudioFlinger::CreateRecordInput>
IAudioFlinger::CreateRecordInput::fromAidl(
        const media::CreateRecordRequest& aidl) {
    IAudioFlinger::CreateRecordInput legacy;
    legacy.attr = VALUE_OR_RETURN(
            aidl2legacy_AudioAttributes_audio_attributes_t(aidl.attr));
    legacy.config = VALUE_OR_RETURN(
            aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.config, true /*isInput*/));
    legacy.clientInfo = VALUE_OR_RETURN(aidl2legacy_AudioClient_AudioClient(aidl.clientInfo));
    legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
    legacy.maxSharedAudioHistoryMs = VALUE_OR_RETURN(
            convertIntegral<int32_t>(aidl.maxSharedAudioHistoryMs));
    legacy.flags = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_input_flags_t_mask(aidl.flags));
    legacy.frameCount = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCount));
    legacy.notificationFrameCount = VALUE_OR_RETURN(
            convertIntegral<size_t>(aidl.notificationFrameCount));
    legacy.selectedDeviceId = VALUE_OR_RETURN(
            aidl2legacy_int32_t_audio_port_handle_t(aidl.selectedDeviceId));
    legacy.sessionId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.sessionId));
    return legacy;
}

ConversionResult<media::CreateRecordResponse>
IAudioFlinger::CreateRecordOutput::toAidl() const {
    media::CreateRecordResponse aidl;
    aidl.flags = VALUE_OR_RETURN(legacy2aidl_audio_input_flags_t_int32_t_mask(flags));
    aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(frameCount));
    aidl.notificationFrameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(notificationFrameCount));
    aidl.selectedDeviceId = VALUE_OR_RETURN(
            legacy2aidl_audio_port_handle_t_int32_t(selectedDeviceId));
    aidl.sessionId = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(sessionId));
    aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(sampleRate));
    aidl.inputId = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(inputId));
    aidl.cblk = VALUE_OR_RETURN(legacy2aidl_NullableIMemory_SharedFileRegion(cblk));
    aidl.buffers = VALUE_OR_RETURN(legacy2aidl_NullableIMemory_SharedFileRegion(buffers));
    aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(portId));
    aidl.audioRecord = audioRecord;
    aidl.serverConfig = VALUE_OR_RETURN(
            legacy2aidl_audio_config_base_t_AudioConfigBase(serverConfig, true /*isInput*/));
    aidl.halConfig = VALUE_OR_RETURN(
        legacy2aidl_audio_config_base_t_AudioConfigBase(halConfig, true /*isInput*/));
    return aidl;
}

ConversionResult<IAudioFlinger::CreateRecordOutput>
IAudioFlinger::CreateRecordOutput::fromAidl(
        const media::CreateRecordResponse& aidl) {
    IAudioFlinger::CreateRecordOutput legacy;
    legacy.flags = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_input_flags_t_mask(aidl.flags));
    legacy.frameCount = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCount));
    legacy.notificationFrameCount = VALUE_OR_RETURN(
            convertIntegral<size_t>(aidl.notificationFrameCount));
    legacy.selectedDeviceId = VALUE_OR_RETURN(
            aidl2legacy_int32_t_audio_port_handle_t(aidl.selectedDeviceId));
    legacy.sessionId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.sessionId));
    legacy.sampleRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate));
    legacy.inputId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.inputId));
    legacy.cblk = VALUE_OR_RETURN(aidl2legacy_NullableSharedFileRegion_IMemory(aidl.cblk));
    legacy.buffers = VALUE_OR_RETURN(aidl2legacy_NullableSharedFileRegion_IMemory(aidl.buffers));
    legacy.portId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
    legacy.audioRecord = aidl.audioRecord;
    legacy.serverConfig = VALUE_OR_RETURN(
            aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.serverConfig, true /*isInput*/));
    legacy.halConfig = VALUE_OR_RETURN(
        aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.halConfig, true /*isInput*/));
    return legacy;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// AudioFlingerClientAdapter

AudioFlingerClientAdapter::AudioFlingerClientAdapter(
        const sp<media::IAudioFlingerService> delegate) : mDelegate(delegate) {}

status_t AudioFlingerClientAdapter::createTrack(const media::CreateTrackRequest& input,
                                                media::CreateTrackResponse& output) {
    return statusTFromBinderStatus(mDelegate->createTrack(input, &output));
}

status_t AudioFlingerClientAdapter::createRecord(const media::CreateRecordRequest& input,
                                                 media::CreateRecordResponse& output) {
    return statusTFromBinderStatus(mDelegate->createRecord(input, &output));
}

uint32_t AudioFlingerClientAdapter::sampleRate(audio_io_handle_t ioHandle) const {
    auto result = [&]() -> ConversionResult<uint32_t> {
        int32_t ioHandleAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(ioHandle));
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->sampleRate(ioHandleAidl, &aidlRet)));
        return convertIntegral<uint32_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

audio_format_t AudioFlingerClientAdapter::format(audio_io_handle_t output) const {
    auto result = [&]() -> ConversionResult<audio_format_t> {
        int32_t outputAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(output));
        AudioFormatDescription aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->format(outputAidl, &aidlRet)));
        return aidl2legacy_AudioFormatDescription_audio_format_t(aidlRet);
    }();
    return result.value_or(AUDIO_FORMAT_INVALID);
}

size_t AudioFlingerClientAdapter::frameCount(audio_io_handle_t ioHandle) const {
    auto result = [&]() -> ConversionResult<size_t> {
        int32_t ioHandleAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(ioHandle));
        int64_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->frameCount(ioHandleAidl, &aidlRet)));
        return convertIntegral<size_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

uint32_t AudioFlingerClientAdapter::latency(audio_io_handle_t output) const {
    auto result = [&]() -> ConversionResult<uint32_t> {
        int32_t outputAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(output));
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->latency(outputAidl, &aidlRet)));
        return convertIntegral<uint32_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

status_t AudioFlingerClientAdapter::setMasterVolume(float value) {
    return statusTFromBinderStatus(mDelegate->setMasterVolume(value));
}

status_t AudioFlingerClientAdapter::setMasterMute(bool muted) {
    return statusTFromBinderStatus(mDelegate->setMasterMute(muted));
}

float AudioFlingerClientAdapter::masterVolume() const {
    auto result = [&]() -> ConversionResult<float> {
        float aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->masterVolume(&aidlRet)));
        return aidlRet;
    }();
    // Failure is ignored.
    return result.value_or(0.f);
}

bool AudioFlingerClientAdapter::masterMute() const {
    auto result = [&]() -> ConversionResult<bool> {
        bool aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->masterMute(&aidlRet)));
        return aidlRet;
    }();
    // Failure is ignored.
    return result.value_or(false);
}

status_t AudioFlingerClientAdapter::setMasterBalance(float balance) {
    return statusTFromBinderStatus(mDelegate->setMasterBalance(balance));
}

status_t AudioFlingerClientAdapter::getMasterBalance(float* balance) const{
    return statusTFromBinderStatus(mDelegate->getMasterBalance(balance));
}

status_t AudioFlingerClientAdapter::setStreamVolume(audio_stream_type_t stream, float value,
                                                    bool muted, audio_io_handle_t output) {
    AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    return statusTFromBinderStatus(
            mDelegate->setStreamVolume(streamAidl, value, muted, outputAidl));
}

status_t AudioFlingerClientAdapter::setStreamMute(audio_stream_type_t stream, bool muted) {
    AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_stream_type_t_AudioStreamType(stream));
    return statusTFromBinderStatus(mDelegate->setStreamMute(streamAidl, muted));
}

status_t AudioFlingerClientAdapter::setPortsVolume(
        const std::vector<audio_port_handle_t> &portIds, float volume, bool muted,
        audio_io_handle_t output) {
    std::vector<int32_t> portIdsAidl = VALUE_OR_RETURN_STATUS(
            convertContainer<std::vector<int32_t>>(
                    portIds, legacy2aidl_audio_port_handle_t_int32_t));
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    return statusTFromBinderStatus(
            mDelegate->setPortsVolume(portIdsAidl, volume, muted, outputAidl));
}

status_t AudioFlingerClientAdapter::setMode(audio_mode_t mode) {
    AudioMode modeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(mode));
    return statusTFromBinderStatus(mDelegate->setMode(modeAidl));
}

status_t AudioFlingerClientAdapter::setMicMute(bool state) {
    return statusTFromBinderStatus(mDelegate->setMicMute(state));
}

bool AudioFlingerClientAdapter::getMicMute() const {
    auto result = [&]() -> ConversionResult<bool> {
        bool aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getMicMute(&aidlRet)));
        return aidlRet;
    }();
    // Failure is ignored.
    return result.value_or(false);
}

void AudioFlingerClientAdapter::setRecordSilenced(audio_port_handle_t portId, bool silenced) {
    auto result = [&]() -> status_t {
        int32_t portIdAidl = VALUE_OR_RETURN_STATUS(
                legacy2aidl_audio_port_handle_t_int32_t(portId));
        return statusTFromBinderStatus(mDelegate->setRecordSilenced(portIdAidl, silenced));
    }();
    // Failure is ignored.
    (void) result;
}

status_t AudioFlingerClientAdapter::setParameters(audio_io_handle_t ioHandle,
                                                  const String8& keyValuePairs) {
    int32_t ioHandleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(ioHandle));
    std::string keyValuePairsAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_String8_string(keyValuePairs));
    return statusTFromBinderStatus(mDelegate->setParameters(ioHandleAidl, keyValuePairsAidl));
}

String8 AudioFlingerClientAdapter::getParameters(audio_io_handle_t ioHandle, const String8& keys)
const {
    auto result = [&]() -> ConversionResult<String8> {
        int32_t ioHandleAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(ioHandle));
        std::string keysAidl = VALUE_OR_RETURN(legacy2aidl_String8_string(keys));
        std::string aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getParameters(ioHandleAidl, keysAidl, &aidlRet)));
        return aidl2legacy_string_view_String8(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(String8());
}

void AudioFlingerClientAdapter::registerClient(const sp<media::IAudioFlingerClient>& client) {
    mDelegate->registerClient(client);
    // Failure is ignored.
}

size_t AudioFlingerClientAdapter::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
                                                     audio_channel_mask_t channelMask) const {
    auto result = [&]() -> ConversionResult<size_t> {
        int32_t sampleRateAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(sampleRate));
        AudioFormatDescription formatAidl = VALUE_OR_RETURN(
                legacy2aidl_audio_format_t_AudioFormatDescription(format));
        AudioChannelLayout channelMaskAidl = VALUE_OR_RETURN(
                legacy2aidl_audio_channel_mask_t_AudioChannelLayout(channelMask, true /*isInput*/));
        int64_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getInputBufferSize(sampleRateAidl, formatAidl, channelMaskAidl,
                                              &aidlRet)));
        return convertIntegral<size_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

status_t AudioFlingerClientAdapter::openOutput(const media::OpenOutputRequest& request,
                                               media::OpenOutputResponse* response) {
    return statusTFromBinderStatus(mDelegate->openOutput(request, response));
}

audio_io_handle_t AudioFlingerClientAdapter::openDuplicateOutput(audio_io_handle_t output1,
                                                                 audio_io_handle_t output2) {
    auto result = [&]() -> ConversionResult<audio_io_handle_t> {
        int32_t output1Aidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(output1));
        int32_t output2Aidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(output2));
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->openDuplicateOutput(output1Aidl, output2Aidl, &aidlRet)));
        return aidl2legacy_int32_t_audio_io_handle_t(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

status_t AudioFlingerClientAdapter::closeOutput(audio_io_handle_t output) {
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    return statusTFromBinderStatus(mDelegate->closeOutput(outputAidl));
}

status_t AudioFlingerClientAdapter::suspendOutput(audio_io_handle_t output) {
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    return statusTFromBinderStatus(mDelegate->suspendOutput(outputAidl));
}

status_t AudioFlingerClientAdapter::restoreOutput(audio_io_handle_t output) {
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    return statusTFromBinderStatus(mDelegate->restoreOutput(outputAidl));
}

status_t AudioFlingerClientAdapter::openInput(const media::OpenInputRequest& request,
                                              media::OpenInputResponse* response) {
    return statusTFromBinderStatus(mDelegate->openInput(request, response));
}

status_t AudioFlingerClientAdapter::closeInput(audio_io_handle_t input) {
    int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(input));
    return statusTFromBinderStatus(mDelegate->closeInput(inputAidl));
}

status_t AudioFlingerClientAdapter::setVoiceVolume(float volume) {
    return statusTFromBinderStatus(mDelegate->setVoiceVolume(volume));
}

status_t AudioFlingerClientAdapter::getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames,
                                                      audio_io_handle_t output) const {
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    media::RenderPosition aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->getRenderPosition(outputAidl, &aidlRet)));
    if (halFrames != nullptr) {
        *halFrames = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(aidlRet.halFrames));
    }
    if (dspFrames != nullptr) {
        *dspFrames = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(aidlRet.dspFrames));
    }
    return OK;
}

uint32_t AudioFlingerClientAdapter::getInputFramesLost(audio_io_handle_t ioHandle) const {
    auto result = [&]() -> ConversionResult<uint32_t> {
        int32_t ioHandleAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(ioHandle));
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getInputFramesLost(ioHandleAidl, &aidlRet)));
        return convertIntegral<uint32_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

audio_unique_id_t AudioFlingerClientAdapter::newAudioUniqueId(audio_unique_id_use_t use) {
    auto result = [&]() -> ConversionResult<audio_unique_id_t> {
        media::AudioUniqueIdUse useAidl = VALUE_OR_RETURN(
                legacy2aidl_audio_unique_id_use_t_AudioUniqueIdUse(use));
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->newAudioUniqueId(useAidl, &aidlRet)));
        return aidl2legacy_int32_t_audio_unique_id_t(aidlRet);
    }();
    return result.value_or(AUDIO_UNIQUE_ID_ALLOCATE);
}

void AudioFlingerClientAdapter::acquireAudioSessionId(audio_session_t audioSession, pid_t pid,
                                                      uid_t uid) {
    [&]() -> status_t {
        int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS(
                legacy2aidl_audio_session_t_int32_t(audioSession));
        int32_t pidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(pid));
        int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(uid));
        return statusTFromBinderStatus(
                mDelegate->acquireAudioSessionId(audioSessionAidl, pidAidl, uidAidl));
    }();
    // Failure is ignored.
}

void AudioFlingerClientAdapter::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) {
    [&]() -> status_t {
        int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS(
                legacy2aidl_audio_session_t_int32_t(audioSession));
        int32_t pidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(pid));
        return statusTFromBinderStatus(
                mDelegate->releaseAudioSessionId(audioSessionAidl, pidAidl));
    }();
    // Failure is ignored.
}

status_t AudioFlingerClientAdapter::queryNumberEffects(uint32_t* numEffects) const {
    int32_t aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->queryNumberEffects(&aidlRet)));
    if (numEffects != nullptr) {
        *numEffects = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(aidlRet));
    }
    return OK;
}

status_t
AudioFlingerClientAdapter::queryEffect(uint32_t index, effect_descriptor_t* pDescriptor) const {
    int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index));
    media::EffectDescriptor aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->queryEffect(indexAidl, &aidlRet)));
    if (pDescriptor != nullptr) {
        *pDescriptor = VALUE_OR_RETURN_STATUS(
                aidl2legacy_EffectDescriptor_effect_descriptor_t(aidlRet));
    }
    return OK;
}

status_t AudioFlingerClientAdapter::getEffectDescriptor(const effect_uuid_t* pEffectUUID,
                                                        const effect_uuid_t* pTypeUUID,
                                                        uint32_t preferredTypeFlag,
                                                        effect_descriptor_t* pDescriptor) const {
    AudioUuid effectUuidAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_uuid_t_AudioUuid(*pEffectUUID));
    AudioUuid typeUuidAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_uuid_t_AudioUuid(*pTypeUUID));
    int32_t preferredTypeFlagAidl = VALUE_OR_RETURN_STATUS(
            convertReinterpret<int32_t>(preferredTypeFlag));
    media::EffectDescriptor aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->getEffectDescriptor(effectUuidAidl, typeUuidAidl, preferredTypeFlagAidl,
                                           &aidlRet)));
    if (pDescriptor != nullptr) {
        *pDescriptor = VALUE_OR_RETURN_STATUS(
                aidl2legacy_EffectDescriptor_effect_descriptor_t(aidlRet));
    }
    return OK;
}

status_t AudioFlingerClientAdapter::createEffect(const media::CreateEffectRequest& request,
                                                 media::CreateEffectResponse* response) {
    return statusTFromBinderStatus(mDelegate->createEffect(request, response));
}

status_t
AudioFlingerClientAdapter::moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
                                       audio_io_handle_t dstOutput) {
    int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session));
    int32_t srcOutputAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_io_handle_t_int32_t(srcOutput));
    int32_t dstOutputAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_io_handle_t_int32_t(dstOutput));
    return statusTFromBinderStatus(
            mDelegate->moveEffects(sessionAidl, srcOutputAidl, dstOutputAidl));
}

void AudioFlingerClientAdapter::setEffectSuspended(int effectId,
                                                   audio_session_t sessionId,
                                                   bool suspended) {
    [&]() -> status_t {
        int32_t effectIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(effectId));
        int32_t sessionIdAidl = VALUE_OR_RETURN_STATUS(
                legacy2aidl_audio_session_t_int32_t(sessionId));
        return statusTFromBinderStatus(
                mDelegate->setEffectSuspended(effectIdAidl, sessionIdAidl, suspended));
    }();
    // Failure is ignored.
}

audio_module_handle_t AudioFlingerClientAdapter::loadHwModule(const char* name) {
    auto result = [&]() -> ConversionResult<audio_module_handle_t> {
        std::string nameAidl(name);
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->loadHwModule(nameAidl, &aidlRet)));
        return aidl2legacy_int32_t_audio_module_handle_t(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

uint32_t AudioFlingerClientAdapter::getPrimaryOutputSamplingRate() const {
    auto result = [&]() -> ConversionResult<uint32_t> {
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getPrimaryOutputSamplingRate(&aidlRet)));
        return convertIntegral<uint32_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

size_t AudioFlingerClientAdapter::getPrimaryOutputFrameCount() const {
    auto result = [&]() -> ConversionResult<size_t> {
        int64_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getPrimaryOutputFrameCount(&aidlRet)));
        return convertIntegral<size_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

status_t AudioFlingerClientAdapter::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
    return statusTFromBinderStatus(mDelegate->setLowRamDevice(isLowRamDevice, totalMemory));
}

status_t AudioFlingerClientAdapter::getAudioPort(struct audio_port_v7* port) const {
    media::AudioPortFw portAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_port_v7_AudioPortFw(*port));
    media::AudioPortFw aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->getAudioPort(portAidl, &aidlRet)));
    *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(aidlRet));
    return OK;
}

status_t AudioFlingerClientAdapter::createAudioPatch(const struct audio_patch* patch,
                                                     audio_patch_handle_t* handle) {
    media::AudioPatchFw patchAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_patch_AudioPatchFw(*patch));
    int32_t aidlRet = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(
                    AUDIO_PATCH_HANDLE_NONE));
    if (handle != nullptr) {
        aidlRet = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle));
    }
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->createAudioPatch(patchAidl, &aidlRet)));
    if (handle != nullptr) {
        *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(aidlRet));
    }
    return OK;
}

status_t AudioFlingerClientAdapter::releaseAudioPatch(audio_patch_handle_t handle) {
    int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle));
    return statusTFromBinderStatus(mDelegate->releaseAudioPatch(handleAidl));
}

status_t AudioFlingerClientAdapter::listAudioPatches(unsigned int* num_patches,
                                                     struct audio_patch* patches) const {
    std::vector<media::AudioPatchFw> aidlRet;
    int32_t maxPatches = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches));
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->listAudioPatches(maxPatches, &aidlRet)));
    *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(aidlRet.size()));
    return convertRange(aidlRet.begin(), aidlRet.end(), patches,
                        aidl2legacy_AudioPatchFw_audio_patch);
}

status_t AudioFlingerClientAdapter::setAudioPortConfig(const struct audio_port_config* config) {
    media::AudioPortConfigFw configAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_port_config_AudioPortConfigFw(*config));
    return statusTFromBinderStatus(mDelegate->setAudioPortConfig(configAidl));
}

audio_hw_sync_t AudioFlingerClientAdapter::getAudioHwSyncForSession(audio_session_t sessionId) {
    auto result = [&]() -> ConversionResult<audio_hw_sync_t> {
        int32_t sessionIdAidl = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(sessionId));
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getAudioHwSyncForSession(sessionIdAidl, &aidlRet)));
        return aidl2legacy_int32_t_audio_hw_sync_t(aidlRet);
    }();
    return result.value_or(AUDIO_HW_SYNC_INVALID);
}

status_t AudioFlingerClientAdapter::systemReady() {
    return statusTFromBinderStatus(mDelegate->systemReady());
}

status_t AudioFlingerClientAdapter::audioPolicyReady() {
    return statusTFromBinderStatus(mDelegate->audioPolicyReady());
}

size_t AudioFlingerClientAdapter::frameCountHAL(audio_io_handle_t ioHandle) const {
    auto result = [&]() -> ConversionResult<size_t> {
        int32_t ioHandleAidl = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(ioHandle));
        int64_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->frameCountHAL(ioHandleAidl, &aidlRet)));
        return convertIntegral<size_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

status_t
AudioFlingerClientAdapter::getMicrophones(
        std::vector<media::MicrophoneInfoFw>* microphones) const {
    std::vector<media::MicrophoneInfoFw> aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mDelegate->getMicrophones(&aidlRet)));
    if (microphones != nullptr) {
        *microphones = std::move(aidlRet);
    }
    return OK;
}

status_t AudioFlingerClientAdapter::setAudioHalPids(const std::vector<pid_t>& pids) {
    std::vector<int32_t> pidsAidl = VALUE_OR_RETURN_STATUS(
            convertContainer<std::vector<int32_t>>(pids, legacy2aidl_pid_t_int32_t));
    return statusTFromBinderStatus(mDelegate->setAudioHalPids(pidsAidl));
}

status_t AudioFlingerClientAdapter::setVibratorInfos(
        const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
    return statusTFromBinderStatus(mDelegate->setVibratorInfos(vibratorInfos));
}

status_t AudioFlingerClientAdapter::updateSecondaryOutputs(
        const TrackSecondaryOutputsMap& trackSecondaryOutputs) {
    std::vector<media::TrackSecondaryOutputInfo> trackSecondaryOutputInfos =
            VALUE_OR_RETURN_STATUS(
                    convertContainer<std::vector<media::TrackSecondaryOutputInfo>>(
                            trackSecondaryOutputs,
                            legacy2aidl_TrackSecondaryOutputInfoPair_TrackSecondaryOutputInfo));
    return statusTFromBinderStatus(mDelegate->updateSecondaryOutputs(trackSecondaryOutputInfos));
}

status_t AudioFlingerClientAdapter::getMmapPolicyInfos(
        AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) {
    return statusTFromBinderStatus(mDelegate->getMmapPolicyInfos(policyType, policyInfos));
}

int32_t AudioFlingerClientAdapter::getAAudioMixerBurstCount() const {
    auto result = [&]() -> ConversionResult<int32_t> {
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(mDelegate->getAAudioMixerBurstCount(&aidlRet)));
        return convertIntegral<int32_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

int32_t AudioFlingerClientAdapter::getAAudioHardwareBurstMinUsec() const {
    auto result = [&]() -> ConversionResult<int32_t> {
        int32_t aidlRet;
        RETURN_IF_ERROR(statusTFromBinderStatus(
                mDelegate->getAAudioHardwareBurstMinUsec(&aidlRet)));
        return convertIntegral<int32_t>(aidlRet);
    }();
    // Failure is ignored.
    return result.value_or(0);
}

status_t AudioFlingerClientAdapter::setDeviceConnectedState(
        const struct audio_port_v7 *port, media::DeviceConnectedState state) {
    media::AudioPortFw aidlPort = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_port_v7_AudioPortFw(*port));
    return statusTFromBinderStatus(mDelegate->setDeviceConnectedState(aidlPort, state));
}

status_t AudioFlingerClientAdapter::setSimulateDeviceConnections(bool enabled) {
    return statusTFromBinderStatus(mDelegate->setSimulateDeviceConnections(enabled));
}

status_t AudioFlingerClientAdapter::setRequestedLatencyMode(
        audio_io_handle_t output, audio_latency_mode_t mode) {
    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    media::audio::common::AudioLatencyMode modeAidl  = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_latency_mode_t_AudioLatencyMode(mode));
    return statusTFromBinderStatus(mDelegate->setRequestedLatencyMode(outputAidl, modeAidl));
}

status_t AudioFlingerClientAdapter::getSupportedLatencyModes(
        audio_io_handle_t output, std::vector<audio_latency_mode_t>* modes) const {
    if (modes == nullptr) {
        return BAD_VALUE;
    }

    int32_t outputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(output));
    std::vector<media::audio::common::AudioLatencyMode> modesAidl;

    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->getSupportedLatencyModes(outputAidl, &modesAidl)));

    *modes = VALUE_OR_RETURN_STATUS(
            convertContainer<std::vector<audio_latency_mode_t>>(modesAidl,
                     aidl2legacy_AudioLatencyMode_audio_latency_mode_t));

    return NO_ERROR;
}

status_t AudioFlingerClientAdapter::setBluetoothVariableLatencyEnabled(bool enabled) {
    return statusTFromBinderStatus(mDelegate->setBluetoothVariableLatencyEnabled(enabled));
}

status_t AudioFlingerClientAdapter::isBluetoothVariableLatencyEnabled(bool* enabled) const {
    if (enabled == nullptr) {
        return BAD_VALUE;
    }

    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->isBluetoothVariableLatencyEnabled(enabled)));

    return NO_ERROR;
}

status_t AudioFlingerClientAdapter::supportsBluetoothVariableLatency(bool* support) const {
    if (support == nullptr) {
        return BAD_VALUE;
    }

    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->supportsBluetoothVariableLatency(support)));

    return NO_ERROR;
}

status_t AudioFlingerClientAdapter::getSoundDoseInterface(
        const sp<media::ISoundDoseCallback> &callback,
        sp<media::ISoundDose>* soundDose) const {
    return statusTFromBinderStatus(mDelegate->getSoundDoseInterface(callback, soundDose));
}

status_t AudioFlingerClientAdapter::invalidateTracks(
        const std::vector<audio_port_handle_t>& portIds) {
    std::vector<int32_t> portIdsAidl = VALUE_OR_RETURN_STATUS(
            convertContainer<std::vector<int32_t>>(
                    portIds, legacy2aidl_audio_port_handle_t_int32_t));
    return statusTFromBinderStatus(mDelegate->invalidateTracks(portIdsAidl));
}

status_t AudioFlingerClientAdapter::getAudioPolicyConfig(media::AudioPolicyConfig *config) {
    if (config == nullptr) {
        return BAD_VALUE;
    }

    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mDelegate->getAudioPolicyConfig(config)));

    return NO_ERROR;
}

status_t AudioFlingerClientAdapter::getAudioMixPort(const struct audio_port_v7 *devicePort,
                                                    struct audio_port_v7 *mixPort) const {
    if (devicePort == nullptr || mixPort == nullptr) {
        return BAD_VALUE;
    }
    media::AudioPortFw devicePortAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_port_v7_AudioPortFw(*devicePort));
    media::AudioPortFw mixPortAidl = VALUE_OR_RETURN_STATUS(
            legacy2aidl_audio_port_v7_AudioPortFw(*mixPort));
    media::AudioPortFw aidlRet;
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
            mDelegate->getAudioMixPort(devicePortAidl, mixPortAidl, &aidlRet)));
    *mixPort = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortFw_audio_port_v7(aidlRet));
    return OK;
}

status_t AudioFlingerClientAdapter::setTracksInternalMute(
        const std::vector<media::TrackInternalMuteInfo>& tracksInternalMuted) {
    return statusTFromBinderStatus(mDelegate->setTracksInternalMute(tracksInternalMuted));
}

status_t AudioFlingerClientAdapter::resetReferencesForTest() {
    RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mDelegate->resetReferencesForTest()));
    return OK;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// AudioFlingerServerAdapter
AudioFlingerServerAdapter::AudioFlingerServerAdapter(
        const sp<AudioFlingerServerAdapter::Delegate>& delegate) : mDelegate(delegate) {
    setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
    setInheritRt(true);
}

status_t AudioFlingerServerAdapter::onTransact(uint32_t code,
                                               const Parcel& data,
                                               Parcel* reply,
                                               uint32_t flags) {
    return mDelegate->onTransactWrapper(static_cast<Delegate::TransactionCode>(code),
                                        data,
                                        flags,
                                        [&] {
                                            return BnAudioFlingerService::onTransact(
                                                    code,
                                                    data,
                                                    reply,
                                                    flags);
                                        });
}

status_t AudioFlingerServerAdapter::dump(int fd, const Vector<String16>& args) {
    return mDelegate->dump(fd, args);
}

Status AudioFlingerServerAdapter::createTrack(const media::CreateTrackRequest& request,
                                              media::CreateTrackResponse* _aidl_return) {
    return Status::fromStatusT(mDelegate->createTrack(request, *_aidl_return));
}

Status AudioFlingerServerAdapter::createRecord(const media::CreateRecordRequest& request,
                                               media::CreateRecordResponse* _aidl_return) {
    return Status::fromStatusT(mDelegate->createRecord(request, *_aidl_return));
}

Status AudioFlingerServerAdapter::sampleRate(int32_t ioHandle, int32_t* _aidl_return) {
    audio_io_handle_t ioHandleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(ioHandle));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int32_t>(mDelegate->sampleRate(ioHandleLegacy)));
    return Status::ok();
}

Status AudioFlingerServerAdapter::format(int32_t output,
                                         AudioFormatDescription* _aidl_return) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            legacy2aidl_audio_format_t_AudioFormatDescription(mDelegate->format(outputLegacy)));
    return Status::ok();
}

Status AudioFlingerServerAdapter::frameCount(int32_t ioHandle, int64_t* _aidl_return) {
    audio_io_handle_t ioHandleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(ioHandle));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int64_t>(mDelegate->frameCount(ioHandleLegacy)));
    return Status::ok();
}

Status AudioFlingerServerAdapter::latency(int32_t output, int32_t* _aidl_return) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int32_t>(mDelegate->latency(outputLegacy)));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setMasterVolume(float value) {
    return Status::fromStatusT(mDelegate->setMasterVolume(value));
}

Status AudioFlingerServerAdapter::setMasterMute(bool muted) {
    return Status::fromStatusT(mDelegate->setMasterMute(muted));
}

Status AudioFlingerServerAdapter::masterVolume(float* _aidl_return) {
    *_aidl_return = mDelegate->masterVolume();
    return Status::ok();
}

Status AudioFlingerServerAdapter::masterMute(bool* _aidl_return) {
    *_aidl_return = mDelegate->masterMute();
    return Status::ok();
}

Status AudioFlingerServerAdapter::setMasterBalance(float balance) {
    return Status::fromStatusT(mDelegate->setMasterBalance(balance));
}

Status AudioFlingerServerAdapter::getMasterBalance(float* _aidl_return) {
    return Status::fromStatusT(mDelegate->getMasterBalance(_aidl_return));
}

Status AudioFlingerServerAdapter::setStreamVolume(AudioStreamType stream, float value,
                                                  bool muted, int32_t output) {
    audio_stream_type_t streamLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioStreamType_audio_stream_type_t(stream));
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    return Status::fromStatusT(
            mDelegate->setStreamVolume(streamLegacy, value, muted, outputLegacy));
}

Status AudioFlingerServerAdapter::setStreamMute(AudioStreamType stream, bool muted) {
    audio_stream_type_t streamLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioStreamType_audio_stream_type_t(stream));
    return Status::fromStatusT(mDelegate->setStreamMute(streamLegacy, muted));
}

Status AudioFlingerServerAdapter::setPortsVolume(
        const std::vector<int32_t>& portIds, float volume, bool muted, int32_t output) {
    std::vector<audio_port_handle_t> portIdsLegacy = VALUE_OR_RETURN_BINDER(
            convertContainer<std::vector<audio_port_handle_t>>(
                    portIds, aidl2legacy_int32_t_audio_port_handle_t));
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    return Status::fromStatusT(
            mDelegate->setPortsVolume(portIdsLegacy, volume, muted, outputLegacy));
}

Status AudioFlingerServerAdapter::setMode(AudioMode mode) {
    audio_mode_t modeLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_AudioMode_audio_mode_t(mode));
    return Status::fromStatusT(mDelegate->setMode(modeLegacy));
}

Status AudioFlingerServerAdapter::setMicMute(bool state) {
    return Status::fromStatusT(mDelegate->setMicMute(state));
}

Status AudioFlingerServerAdapter::getMicMute(bool* _aidl_return) {
    *_aidl_return = mDelegate->getMicMute();
    return Status::ok();
}

Status AudioFlingerServerAdapter::setRecordSilenced(int32_t portId, bool silenced) {
    audio_port_handle_t portIdLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_port_handle_t(portId));
    mDelegate->setRecordSilenced(portIdLegacy, silenced);
    return Status::ok();
}

Status
AudioFlingerServerAdapter::setParameters(int32_t ioHandle, const std::string& keyValuePairs) {
    audio_io_handle_t ioHandleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(ioHandle));
    String8 keyValuePairsLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_string_view_String8(keyValuePairs));
    return Status::fromStatusT(mDelegate->setParameters(ioHandleLegacy, keyValuePairsLegacy));
}

Status AudioFlingerServerAdapter::getParameters(int32_t ioHandle, const std::string& keys,
                                                std::string* _aidl_return) {
    audio_io_handle_t ioHandleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(ioHandle));
    String8 keysLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_string_view_String8(keys));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            legacy2aidl_String8_string(mDelegate->getParameters(ioHandleLegacy, keysLegacy)));
    return Status::ok();
}

Status AudioFlingerServerAdapter::registerClient(const sp<media::IAudioFlingerClient>& client) {
    mDelegate->registerClient(client);
    return Status::ok();
}

Status AudioFlingerServerAdapter::getInputBufferSize(int32_t sampleRate,
                                                     const AudioFormatDescription& format,
                                                     const AudioChannelLayout& channelMask,
                                                     int64_t* _aidl_return) {
    uint32_t sampleRateLegacy = VALUE_OR_RETURN_BINDER(convertIntegral<uint32_t>(sampleRate));
    audio_format_t formatLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioFormatDescription_audio_format_t(format));
    audio_channel_mask_t channelMaskLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioChannelLayout_audio_channel_mask_t(channelMask, true /*isInput*/));
    size_t size = mDelegate->getInputBufferSize(sampleRateLegacy, formatLegacy, channelMaskLegacy);
    *_aidl_return = VALUE_OR_RETURN_BINDER(convertIntegral<int64_t>(size));
    return Status::ok();
}

Status AudioFlingerServerAdapter::openOutput(const media::OpenOutputRequest& request,
                                             media::OpenOutputResponse* _aidl_return) {
    return Status::fromStatusT(mDelegate->openOutput(request, _aidl_return));
}

Status AudioFlingerServerAdapter::openDuplicateOutput(int32_t output1, int32_t output2,
                                                      int32_t* _aidl_return) {
    audio_io_handle_t output1Legacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output1));
    audio_io_handle_t output2Legacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output2));
    audio_io_handle_t result = mDelegate->openDuplicateOutput(output1Legacy, output2Legacy);
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_io_handle_t_int32_t(result));
    return Status::ok();
}

Status AudioFlingerServerAdapter::closeOutput(int32_t output) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    return Status::fromStatusT(mDelegate->closeOutput(outputLegacy));
}

Status AudioFlingerServerAdapter::suspendOutput(int32_t output) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    return Status::fromStatusT(mDelegate->suspendOutput(outputLegacy));
}

Status AudioFlingerServerAdapter::restoreOutput(int32_t output) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    return Status::fromStatusT(mDelegate->restoreOutput(outputLegacy));
}

Status AudioFlingerServerAdapter::openInput(const media::OpenInputRequest& request,
                                            media::OpenInputResponse* _aidl_return) {
    return Status::fromStatusT(mDelegate->openInput(request, _aidl_return));
}

Status AudioFlingerServerAdapter::closeInput(int32_t input) {
    audio_io_handle_t inputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(input));
    return Status::fromStatusT(mDelegate->closeInput(inputLegacy));
}

Status AudioFlingerServerAdapter::setVoiceVolume(float volume) {
    return Status::fromStatusT(mDelegate->setVoiceVolume(volume));
}

Status
AudioFlingerServerAdapter::getRenderPosition(int32_t output, media::RenderPosition* _aidl_return) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    uint32_t halFramesLegacy;
    uint32_t dspFramesLegacy;
    RETURN_BINDER_IF_ERROR(
            mDelegate->getRenderPosition(&halFramesLegacy, &dspFramesLegacy, outputLegacy));
    _aidl_return->halFrames = VALUE_OR_RETURN_BINDER(convertIntegral<int32_t>(halFramesLegacy));
    _aidl_return->dspFrames = VALUE_OR_RETURN_BINDER(convertIntegral<int32_t>(dspFramesLegacy));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getInputFramesLost(int32_t ioHandle, int32_t* _aidl_return) {
    audio_io_handle_t ioHandleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(ioHandle));
    uint32_t result = mDelegate->getInputFramesLost(ioHandleLegacy);
    *_aidl_return = VALUE_OR_RETURN_BINDER(convertIntegral<int32_t>(result));
    return Status::ok();
}

Status
AudioFlingerServerAdapter::newAudioUniqueId(media::AudioUniqueIdUse use, int32_t* _aidl_return) {
    audio_unique_id_use_t useLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioUniqueIdUse_audio_unique_id_use_t(use));
    audio_unique_id_t result = mDelegate->newAudioUniqueId(useLegacy);
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_unique_id_t_int32_t(result));
    return Status::ok();
}

Status
AudioFlingerServerAdapter::acquireAudioSessionId(int32_t audioSession, int32_t pid, int32_t uid) {
    audio_session_t audioSessionLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_session_t(audioSession));
    pid_t pidLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_int32_t_pid_t(pid));
    uid_t uidLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_int32_t_uid_t(uid));
    mDelegate->acquireAudioSessionId(audioSessionLegacy, pidLegacy, uidLegacy);
    return Status::ok();
}

Status AudioFlingerServerAdapter::releaseAudioSessionId(int32_t audioSession, int32_t pid) {
    audio_session_t audioSessionLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_session_t(audioSession));
    pid_t pidLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_int32_t_pid_t(pid));
    mDelegate->releaseAudioSessionId(audioSessionLegacy, pidLegacy);
    return Status::ok();
}

Status AudioFlingerServerAdapter::queryNumberEffects(int32_t* _aidl_return) {
    uint32_t result;
    RETURN_BINDER_IF_ERROR(mDelegate->queryNumberEffects(&result));
    *_aidl_return = VALUE_OR_RETURN_BINDER(convertIntegral<uint32_t>(result));
    return Status::ok();
}

Status
AudioFlingerServerAdapter::queryEffect(int32_t index, media::EffectDescriptor* _aidl_return) {
    uint32_t indexLegacy = VALUE_OR_RETURN_BINDER(convertIntegral<uint32_t>(index));
    effect_descriptor_t result;
    RETURN_BINDER_IF_ERROR(mDelegate->queryEffect(indexLegacy, &result));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            legacy2aidl_effect_descriptor_t_EffectDescriptor(result));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getEffectDescriptor(const AudioUuid& effectUUID,
                                                      const AudioUuid& typeUUID,
                                                      int32_t preferredTypeFlag,
                                                      media::EffectDescriptor* _aidl_return) {
    effect_uuid_t effectUuidLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioUuid_audio_uuid_t(effectUUID));
    effect_uuid_t typeUuidLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioUuid_audio_uuid_t(typeUUID));
    uint32_t preferredTypeFlagLegacy = VALUE_OR_RETURN_BINDER(
            convertReinterpret<uint32_t>(preferredTypeFlag));
    effect_descriptor_t result;
    RETURN_BINDER_IF_ERROR(mDelegate->getEffectDescriptor(&effectUuidLegacy, &typeUuidLegacy,
                                                          preferredTypeFlagLegacy, &result));
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            legacy2aidl_effect_descriptor_t_EffectDescriptor(result));
    return Status::ok();
}

Status AudioFlingerServerAdapter::createEffect(const media::CreateEffectRequest& request,
                                               media::CreateEffectResponse* _aidl_return) {
    return Status::fromStatusT(mDelegate->createEffect(request, _aidl_return));
}

Status
AudioFlingerServerAdapter::moveEffects(int32_t session, int32_t srcOutput, int32_t dstOutput) {
    audio_session_t sessionLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_session_t(session));
    audio_io_handle_t srcOutputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(srcOutput));
    audio_io_handle_t dstOutputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(dstOutput));
    return Status::fromStatusT(
            mDelegate->moveEffects(sessionLegacy, srcOutputLegacy, dstOutputLegacy));
}

Status AudioFlingerServerAdapter::setEffectSuspended(int32_t effectId, int32_t sessionId,
                                                     bool suspended) {
    int effectIdLegacy = VALUE_OR_RETURN_BINDER(convertReinterpret<int>(effectId));
    audio_session_t sessionIdLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_session_t(sessionId));
    mDelegate->setEffectSuspended(effectIdLegacy, sessionIdLegacy, suspended);
    return Status::ok();
}

Status AudioFlingerServerAdapter::loadHwModule(const std::string& name, int32_t* _aidl_return) {
    audio_module_handle_t result = mDelegate->loadHwModule(name.c_str());
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_module_handle_t_int32_t(result));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getPrimaryOutputSamplingRate(int32_t* _aidl_return) {
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int32_t>(mDelegate->getPrimaryOutputSamplingRate()));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getPrimaryOutputFrameCount(int64_t* _aidl_return) {
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int64_t>(mDelegate->getPrimaryOutputFrameCount()));
    return Status::ok();

}

Status AudioFlingerServerAdapter::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) {
    return Status::fromStatusT(mDelegate->setLowRamDevice(isLowRamDevice, totalMemory));
}

Status AudioFlingerServerAdapter::getAudioPort(const media::AudioPortFw& port,
                                               media::AudioPortFw* _aidl_return) {
    audio_port_v7 portLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_AudioPortFw_audio_port_v7(port));
    RETURN_BINDER_IF_ERROR(mDelegate->getAudioPort(&portLegacy));
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_port_v7_AudioPortFw(portLegacy));
    return Status::ok();
}

Status AudioFlingerServerAdapter::createAudioPatch(const media::AudioPatchFw& patch,
                                                   int32_t* _aidl_return) {
    audio_patch patchLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_AudioPatchFw_audio_patch(patch));
    audio_patch_handle_t handleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_patch_handle_t(*_aidl_return));
    RETURN_BINDER_IF_ERROR(mDelegate->createAudioPatch(&patchLegacy, &handleLegacy));
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_patch_handle_t_int32_t(handleLegacy));
    return Status::ok();
}

Status AudioFlingerServerAdapter::releaseAudioPatch(int32_t handle) {
    audio_patch_handle_t handleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_patch_handle_t(handle));
    return Status::fromStatusT(mDelegate->releaseAudioPatch(handleLegacy));
}

Status AudioFlingerServerAdapter::listAudioPatches(int32_t maxCount,
                            std::vector<media::AudioPatchFw>* _aidl_return) {
    unsigned int count = VALUE_OR_RETURN_BINDER(convertIntegral<unsigned int>(maxCount));
    count = std::min(count, static_cast<unsigned int>(MAX_ITEMS_PER_LIST));
    std::unique_ptr<audio_patch[]> patchesLegacy(new audio_patch[count]);
    RETURN_BINDER_IF_ERROR(mDelegate->listAudioPatches(&count, patchesLegacy.get()));
    RETURN_BINDER_IF_ERROR(convertRange(&patchesLegacy[0],
                           &patchesLegacy[count],
                           std::back_inserter(*_aidl_return),
                           legacy2aidl_audio_patch_AudioPatchFw));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setAudioPortConfig(const media::AudioPortConfigFw& config) {
    audio_port_config configLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioPortConfigFw_audio_port_config(config));
    return Status::fromStatusT(mDelegate->setAudioPortConfig(&configLegacy));
}

Status AudioFlingerServerAdapter::getAudioHwSyncForSession(int32_t sessionId,
                                                           int32_t* _aidl_return) {
    audio_session_t sessionIdLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_session_t(sessionId));
    audio_hw_sync_t result = mDelegate->getAudioHwSyncForSession(sessionIdLegacy);
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_hw_sync_t_int32_t(result));
    return Status::ok();
}

Status AudioFlingerServerAdapter::systemReady() {
    return Status::fromStatusT(mDelegate->systemReady());
}

Status AudioFlingerServerAdapter::audioPolicyReady() {
    mDelegate->audioPolicyReady();
    return Status::ok();
}

Status AudioFlingerServerAdapter::frameCountHAL(int32_t ioHandle, int64_t* _aidl_return) {
    audio_io_handle_t ioHandleLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(ioHandle));
    size_t result = mDelegate->frameCountHAL(ioHandleLegacy);
    *_aidl_return = VALUE_OR_RETURN_BINDER(convertIntegral<int64_t>(result));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getMicrophones(
        std::vector<media::MicrophoneInfoFw>* _aidl_return) {
    RETURN_BINDER_IF_ERROR(mDelegate->getMicrophones(_aidl_return));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setAudioHalPids(const std::vector<int32_t>& pids) {
    std::vector<pid_t> pidsLegacy = VALUE_OR_RETURN_BINDER(
            convertContainer<std::vector<pid_t>>(pids, aidl2legacy_int32_t_pid_t));
    RETURN_BINDER_IF_ERROR(mDelegate->setAudioHalPids(pidsLegacy));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setVibratorInfos(
        const std::vector<media::AudioVibratorInfo>& vibratorInfos) {
    return Status::fromStatusT(mDelegate->setVibratorInfos(vibratorInfos));
}

Status AudioFlingerServerAdapter::updateSecondaryOutputs(
        const std::vector<media::TrackSecondaryOutputInfo>& trackSecondaryOutputInfos) {
    TrackSecondaryOutputsMap trackSecondaryOutputs =
            VALUE_OR_RETURN_BINDER(convertContainer<TrackSecondaryOutputsMap>(
                    trackSecondaryOutputInfos,
                    aidl2legacy_TrackSecondaryOutputInfo_TrackSecondaryOutputInfoPair));
    return Status::fromStatusT(mDelegate->updateSecondaryOutputs(trackSecondaryOutputs));
}

Status AudioFlingerServerAdapter::getMmapPolicyInfos(
        AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *_aidl_return) {
    return Status::fromStatusT(mDelegate->getMmapPolicyInfos(policyType, _aidl_return));
}

Status AudioFlingerServerAdapter::getAAudioMixerBurstCount(int32_t* _aidl_return) {
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int32_t>(mDelegate->getAAudioMixerBurstCount()));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertIntegral<int32_t>(mDelegate->getAAudioHardwareBurstMinUsec()));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setDeviceConnectedState(
        const media::AudioPortFw& port, media::DeviceConnectedState state) {
    audio_port_v7 portLegacy = VALUE_OR_RETURN_BINDER(aidl2legacy_AudioPortFw_audio_port_v7(port));
    return Status::fromStatusT(mDelegate->setDeviceConnectedState(&portLegacy, state));
}

Status AudioFlingerServerAdapter::setSimulateDeviceConnections(bool enabled) {
    return Status::fromStatusT(mDelegate->setSimulateDeviceConnections(enabled));
}

Status AudioFlingerServerAdapter::setRequestedLatencyMode(
        int32_t output, media::audio::common::AudioLatencyMode modeAidl) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    audio_latency_mode_t modeLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioLatencyMode_audio_latency_mode_t(modeAidl));
    return Status::fromStatusT(mDelegate->setRequestedLatencyMode(
            outputLegacy, modeLegacy));
}

Status AudioFlingerServerAdapter::getSupportedLatencyModes(
        int output, std::vector<media::audio::common::AudioLatencyMode>* _aidl_return) {
    audio_io_handle_t outputLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_int32_t_audio_io_handle_t(output));
    std::vector<audio_latency_mode_t> modesLegacy;

    RETURN_BINDER_IF_ERROR(mDelegate->getSupportedLatencyModes(outputLegacy, &modesLegacy));

    *_aidl_return = VALUE_OR_RETURN_BINDER(
            convertContainer<std::vector<media::audio::common::AudioLatencyMode>>(
                    modesLegacy, legacy2aidl_audio_latency_mode_t_AudioLatencyMode));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setBluetoothVariableLatencyEnabled(bool enabled) {
    return Status::fromStatusT(mDelegate->setBluetoothVariableLatencyEnabled(enabled));
}

Status AudioFlingerServerAdapter::isBluetoothVariableLatencyEnabled(bool *enabled) {
    return Status::fromStatusT(mDelegate->isBluetoothVariableLatencyEnabled(enabled));
}

Status AudioFlingerServerAdapter::supportsBluetoothVariableLatency(bool *support) {
    return Status::fromStatusT(mDelegate->supportsBluetoothVariableLatency(support));
}

Status AudioFlingerServerAdapter::getSoundDoseInterface(
        const sp<media::ISoundDoseCallback>& callback,
        sp<media::ISoundDose>* soundDose)
{
    return Status::fromStatusT(mDelegate->getSoundDoseInterface(callback, soundDose));
}

Status AudioFlingerServerAdapter::invalidateTracks(const std::vector<int32_t>& portIds) {
    std::vector<audio_port_handle_t> portIdsLegacy = VALUE_OR_RETURN_BINDER(
            convertContainer<std::vector<audio_port_handle_t>>(
                    portIds, aidl2legacy_int32_t_audio_port_handle_t));
    RETURN_BINDER_IF_ERROR(mDelegate->invalidateTracks(portIdsLegacy));
    return Status::ok();
}

Status AudioFlingerServerAdapter::getAudioPolicyConfig(media::AudioPolicyConfig* _aidl_return) {
    return Status::fromStatusT(mDelegate->getAudioPolicyConfig(_aidl_return));
}

Status AudioFlingerServerAdapter::getAudioMixPort(const media::AudioPortFw &devicePort,
                                                  const media::AudioPortFw &mixPort,
                                                  media::AudioPortFw *_aidl_return) {
    audio_port_v7 devicePortLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioPortFw_audio_port_v7(devicePort));
    audio_port_v7 mixPortLegacy = VALUE_OR_RETURN_BINDER(
            aidl2legacy_AudioPortFw_audio_port_v7(mixPort));
    RETURN_BINDER_IF_ERROR(mDelegate->getAudioMixPort(&devicePortLegacy, &mixPortLegacy));
    *_aidl_return = VALUE_OR_RETURN_BINDER(legacy2aidl_audio_port_v7_AudioPortFw(mixPortLegacy));
    return Status::ok();
}

Status AudioFlingerServerAdapter::setTracksInternalMute(
        const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) {
    return Status::fromStatusT(mDelegate->setTracksInternalMute(tracksInternalMute));
}

Status AudioFlingerServerAdapter::resetReferencesForTest() {
    RETURN_BINDER_IF_ERROR(mDelegate->resetReferencesForTest());
    return Status::ok();
}

} // namespace android
