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

Commit 521fc49f authored by Mikhail Naganov's avatar Mikhail Naganov
Browse files

audio: Create ModulePrimary and ModuleStub

Make 'Module' more abstract by moving stream creation
methods to more concrete 'ModulePrimary' and 'ModuleStub'.
'ModulePrimary' is now closer to the CF primary module:
it was stripped off USB devices from its configuration,
these got moved to 'ModuleUsb', and got rid of BT A2DP
and LE interfaces, these will be on 'ModuleBluetooth'.
Note that 'ModulePrimary' still uses stub streams, this
will be changed in subsequent patches.

'ModuleStub' is what 'Module' used to be, just a module
for improving test coverage. It includes simulation of
offload streams and dummy BT objects.

Bug: 264712385
Test: atest VtsHalAudioCoreTargetTest
Change-Id: I5e4da0c32c00d65688f2eda78b2c79594e4e4671
parent c337a879
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -73,9 +73,9 @@ cc_library {
        "Configuration.cpp",
        "EngineConfigXmlConverter.cpp",
        "Module.cpp",
        "ModulePrimary.cpp",
        "SoundDose.cpp",
        "Stream.cpp",
        "StreamStub.cpp",
        "Telephony.cpp",
        "alsa/Mixer.cpp",
        "alsa/ModuleAlsa.cpp",
@@ -85,6 +85,8 @@ cc_library {
        "r_submix/RemoteSubmixUtils.cpp",
        "r_submix/SubmixRoute.cpp",
        "r_submix/StreamRemoteSubmix.cpp",
        "stub/ModuleStub.cpp",
        "stub/StreamStub.cpp",
        "usb/ModuleUsb.cpp",
        "usb/StreamUsb.cpp",
        "usb/UsbAlsaMixerControl.cpp",
+130 −41
Original line number Diff line number Diff line
@@ -144,10 +144,6 @@ static AudioRoute createRoute(const std::vector<AudioPort>& sources, const Audio
//    - no profiles specified
//  * "FM Tuner", IN_FM_TUNER
//    - no profiles specified
//  * "USB Out", OUT_DEVICE, CONNECTION_USB
//    - no profiles specified
//  * "USB In", IN_DEVICE, CONNECTION_USB
//    - no profiles specified
//
// Mix ports:
//  * "primary output", PRIMARY, 1 max open, 1 max active stream
@@ -172,8 +168,7 @@ static AudioRoute createRoute(const std::vector<AudioPort>& sources, const Audio
//
// Routes:
//  "primary out", "compressed offload" -> "Speaker"
//  "primary out", "compressed offload" -> "USB Out"
//  "Built-in Mic", "USB In" -> "primary input"
//  "Built-in Mic" -> "primary input"
//  "telephony_tx" -> "Telephony Tx"
//  "Telephony Rx" -> "telephony_rx"
//  "FM Tuner" -> "fm_tuner"
@@ -185,14 +180,6 @@ static AudioRoute createRoute(const std::vector<AudioPort>& sources, const Audio
//  * "Telephony Rx" device port: PCM 24-bit; MONO; 48000
//  * "FM Tuner" device port: PCM 24-bit; STEREO; 48000
//
// Profiles for device port connected state:
//  * USB Out":
//    - profile PCM 16-bit; MONO, STEREO; 8000, 11025, 16000, 32000, 44100, 48000
//    - profile PCM 24-bit; MONO, STEREO; 8000, 11025, 16000, 32000, 44100, 48000
//  * USB In":
//    - profile PCM 16-bit; MONO, STEREO; 8000, 11025, 16000, 32000, 44100, 48000
//    - profile PCM 24-bit; MONO, STEREO; 8000, 11025, 16000, 32000, 44100, 48000
//
std::unique_ptr<Configuration> getPrimaryConfiguration() {
    static const Configuration configuration = []() {
        const std::vector<AudioProfile> standardPcmAudioProfiles = {
@@ -252,19 +239,6 @@ std::unique_ptr<Configuration> getPrimaryConfiguration() {
                                 AudioChannelLayout::LAYOUT_STEREO, 48000, 0, true,
                                 createDeviceExt(AudioDeviceType::IN_FM_TUNER, 0)));

        AudioPort usbOutDevice =
                createPort(c.nextPortId++, "USB Out", 0, false,
                           createDeviceExt(AudioDeviceType::OUT_DEVICE, 0,
                                           AudioDeviceDescription::CONNECTION_USB));
        c.ports.push_back(usbOutDevice);
        c.connectedProfiles[usbOutDevice.id] = standardPcmAudioProfiles;

        AudioPort usbInDevice = createPort(c.nextPortId++, "USB In", 0, true,
                                           createDeviceExt(AudioDeviceType::IN_DEVICE, 0,
                                                           AudioDeviceDescription::CONNECTION_USB));
        c.ports.push_back(usbInDevice);
        c.connectedProfiles[usbInDevice.id] = standardPcmAudioProfiles;

        // Mix ports

        AudioPort primaryOutMix = createPort(c.nextPortId++, "primary output",
@@ -323,8 +297,7 @@ std::unique_ptr<Configuration> getPrimaryConfiguration() {
        c.ports.push_back(fmTunerInMix);

        c.routes.push_back(createRoute({primaryOutMix, compressedOffloadOutMix}, speakerOutDevice));
        c.routes.push_back(createRoute({primaryOutMix, compressedOffloadOutMix}, usbOutDevice));
        c.routes.push_back(createRoute({micInDevice, usbInDevice}, primaryInMix));
        c.routes.push_back(createRoute({micInDevice}, primaryInMix));
        c.routes.push_back(createRoute({telephonyTxOutMix}, telephonyTxOutDevice));
        c.routes.push_back(createRoute({telephonyRxInDevice}, telephonyRxInMix));
        c.routes.push_back(createRoute({fmTunerInDevice}, fmTunerInMix));
@@ -406,22 +379,31 @@ std::unique_ptr<Configuration> getRSubmixConfiguration() {
// Usb configuration:
//
// Device ports:
//  * "USB Device Out", OUT_DEVICE, CONNECTION_USB
//    - no profiles specified
//  * "USB Headset Out", OUT_HEADSET, CONNECTION_USB
//    - no profiles specified
//  * "USB Device In", IN_DEVICE, CONNECTION_USB
//    - no profiles specified
//  * "USB Headset In", IN_HEADSET, CONNECTION_USB
//    - no profiles specified
//
// Mix ports:
//  * "usb_headset output", 1 max open, 1 max active stream
//  * "usb_device output", 1 max open, 1 max active stream
//    - no profiles specified
//  * "usb_headset input", 1 max open, 1 max active stream
//  * "usb_device input", 1 max open, 1 max active stream
//    - no profiles specified
//
// Routes:
//  * "usb_device output" -> "USB Device Out"
//  * "usb_device output" -> "USB Headset Out"
//  * "USB Device In", "USB Headset In" -> "usb_device input"
//
// Profiles for device port connected state:
//  * USB Headset Out":
//  * "USB Device Out", "USB Headset Out":
//    - profile PCM 16-bit; MONO, STEREO, INDEX_MASK_1, INDEX_MASK_2; 44100, 48000
//    - profile PCM 24-bit; MONO, STEREO, INDEX_MASK_1, INDEX_MASK_2; 44100, 48000
//  * USB Headset In":
//  * "USB Device In", "USB Headset In":
//    - profile PCM 16-bit; MONO, STEREO, INDEX_MASK_1, INDEX_MASK_2; 44100, 48000
//    - profile PCM 24-bit; MONO, STEREO, INDEX_MASK_1, INDEX_MASK_2; 44100, 48000
//
@@ -440,6 +422,13 @@ std::unique_ptr<Configuration> getUsbConfiguration() {

        // Device ports

        AudioPort usbOutDevice =
                createPort(c.nextPortId++, "USB Device Out", 0, false,
                           createDeviceExt(AudioDeviceType::OUT_DEVICE, 0,
                                           AudioDeviceDescription::CONNECTION_USB));
        c.ports.push_back(usbOutDevice);
        c.connectedProfiles[usbOutDevice.id] = standardPcmAudioProfiles;

        AudioPort usbOutHeadset =
                createPort(c.nextPortId++, "USB Headset Out", 0, false,
                           createDeviceExt(AudioDeviceType::OUT_HEADSET, 0,
@@ -447,6 +436,12 @@ std::unique_ptr<Configuration> getUsbConfiguration() {
        c.ports.push_back(usbOutHeadset);
        c.connectedProfiles[usbOutHeadset.id] = standardPcmAudioProfiles;

        AudioPort usbInDevice = createPort(c.nextPortId++, "USB Device In", 0, true,
                                           createDeviceExt(AudioDeviceType::IN_DEVICE, 0,
                                                           AudioDeviceDescription::CONNECTION_USB));
        c.ports.push_back(usbInDevice);
        c.connectedProfiles[usbInDevice.id] = standardPcmAudioProfiles;

        AudioPort usbInHeadset =
                createPort(c.nextPortId++, "USB Headset In", 0, true,
                           createDeviceExt(AudioDeviceType::IN_HEADSET, 0,
@@ -456,16 +451,110 @@ std::unique_ptr<Configuration> getUsbConfiguration() {

        // Mix ports

        AudioPort usbHeadsetOutMix =
                createPort(c.nextPortId++, "usb_headset output", 0, false, createPortMixExt(1, 1));
        c.ports.push_back(usbHeadsetOutMix);
        AudioPort usbDeviceOutMix =
                createPort(c.nextPortId++, "usb_device output", 0, false, createPortMixExt(1, 1));
        c.ports.push_back(usbDeviceOutMix);

        AudioPort usbDeviceInMix =
                createPort(c.nextPortId++, "usb_device input", 0, true, createPortMixExt(1, 1));
        c.ports.push_back(usbDeviceInMix);

        c.routes.push_back(createRoute({usbDeviceOutMix}, usbOutDevice));
        c.routes.push_back(createRoute({usbDeviceOutMix}, usbOutHeadset));
        c.routes.push_back(createRoute({usbInDevice, usbInHeadset}, usbDeviceInMix));

        AudioPort usbHeadsetInMix =
                createPort(c.nextPortId++, "usb_headset input", 0, true, createPortMixExt(1, 1));
        c.ports.push_back(usbHeadsetInMix);
        return c;
    }();
    return std::make_unique<Configuration>(configuration);
}

        c.routes.push_back(createRoute({usbHeadsetOutMix}, usbOutHeadset));
        c.routes.push_back(createRoute({usbInHeadset}, usbHeadsetInMix));
// Stub configuration:
//
// Device ports:
//  * "Test Out", OUT_AFE_PROXY
//    - no profiles specified
//  * "Test In", IN_AFE_PROXY
//    - no profiles specified
//
// Mix ports:
//  * "test output", 1 max open, 1 max active stream
//    - profile PCM 24-bit; MONO, STEREO; 8000, 11025, 16000, 32000, 44100, 48000
//  * "compressed offload", DIRECT|COMPRESS_OFFLOAD|NON_BLOCKING, 1 max open, 1 max active stream
//    - profile MP3; MONO, STEREO; 44100, 48000
//  * "test input", 2 max open, 2 max active streams
//    - profile PCM 24-bit; MONO, STEREO, FRONT_BACK;
//        8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
//
// Routes:
//  "test output", "compressed offload" -> "Test Out"
//  "Test In" -> "test input"
//
// Initial port configs:
//  * "Test Out" device port: PCM 24-bit; STEREO; 48000
//  * "Test In" device port: PCM 24-bit; MONO; 48000
//
std::unique_ptr<Configuration> getStubConfiguration() {
    static const Configuration configuration = []() {
        Configuration c;

        // Device ports

        AudioPort testOutDevice = createPort(c.nextPortId++, "Test Out", 0, false,
                                             createDeviceExt(AudioDeviceType::OUT_AFE_PROXY, 0));
        c.ports.push_back(testOutDevice);
        c.initialConfigs.push_back(
                createPortConfig(testOutDevice.id, testOutDevice.id, PcmType::INT_24_BIT,
                                 AudioChannelLayout::LAYOUT_STEREO, 48000, 0, false,
                                 createDeviceExt(AudioDeviceType::OUT_AFE_PROXY, 0)));

        AudioPort testInDevice = createPort(c.nextPortId++, "Test In", 0, true,
                                            createDeviceExt(AudioDeviceType::IN_AFE_PROXY, 0));
        c.ports.push_back(testInDevice);
        c.initialConfigs.push_back(
                createPortConfig(testInDevice.id, testInDevice.id, PcmType::INT_24_BIT,
                                 AudioChannelLayout::LAYOUT_MONO, 48000, 0, true,
                                 createDeviceExt(AudioDeviceType::IN_AFE_PROXY, 0)));

        // Mix ports

        AudioPort testOutMix =
                createPort(c.nextPortId++, "test output", 0, false, createPortMixExt(1, 1));
        testOutMix.profiles.push_back(
                createProfile(PcmType::INT_24_BIT,
                              {AudioChannelLayout::LAYOUT_MONO, AudioChannelLayout::LAYOUT_STEREO},
                              {8000, 11025, 16000, 32000, 44100, 48000}));
        c.ports.push_back(testOutMix);

        AudioPort compressedOffloadOutMix =
                createPort(c.nextPortId++, "compressed offload",
                           makeBitPositionFlagMask({AudioOutputFlags::DIRECT,
                                                    AudioOutputFlags::COMPRESS_OFFLOAD,
                                                    AudioOutputFlags::NON_BLOCKING}),
                           false, createPortMixExt(1, 1));
        compressedOffloadOutMix.profiles.push_back(
                createProfile(::android::MEDIA_MIMETYPE_AUDIO_MPEG,
                              {AudioChannelLayout::LAYOUT_MONO, AudioChannelLayout::LAYOUT_STEREO},
                              {44100, 48000}));
        c.ports.push_back(compressedOffloadOutMix);

        AudioPort testInMIx =
                createPort(c.nextPortId++, "test input", 0, true, createPortMixExt(2, 2));
        testInMIx.profiles.push_back(
                createProfile(PcmType::INT_16_BIT,
                              {AudioChannelLayout::LAYOUT_MONO, AudioChannelLayout::LAYOUT_STEREO,
                               AudioChannelLayout::LAYOUT_FRONT_BACK},
                              {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000}));
        testInMIx.profiles.push_back(
                createProfile(PcmType::INT_24_BIT,
                              {AudioChannelLayout::LAYOUT_MONO, AudioChannelLayout::LAYOUT_STEREO,
                               AudioChannelLayout::LAYOUT_FRONT_BACK},
                              {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000}));
        c.ports.push_back(testInMIx);

        c.routes.push_back(createRoute({testOutMix, compressedOffloadOutMix}, testOutDevice));
        c.routes.push_back(createRoute({testInDevice}, testInMIx));

        c.portConfigs.insert(c.portConfigs.end(), c.initialConfigs.begin(), c.initialConfigs.end());

        return c;
    }();
+23 −45
Original line number Diff line number Diff line
@@ -25,13 +25,12 @@
#include <android/binder_ibinder_platform.h>
#include <error/expected_utils.h>

#include "core-impl/Bluetooth.h"
#include "core-impl/Module.h"
#include "core-impl/ModulePrimary.h"
#include "core-impl/ModuleRemoteSubmix.h"
#include "core-impl/ModuleStub.h"
#include "core-impl/ModuleUsb.h"
#include "core-impl/SoundDose.h"
#include "core-impl/StreamStub.h"
#include "core-impl/Telephony.h"
#include "core-impl/utils.h"

using aidl::android::hardware::audio::common::getFrameSizeInBytes;
@@ -110,13 +109,14 @@ bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& forma
// static
std::shared_ptr<Module> Module::createInstance(Type type) {
    switch (type) {
        case Module::Type::USB:
            return ndk::SharedRefBase::make<ModuleUsb>(type);
        case Type::R_SUBMIX:
            return ndk::SharedRefBase::make<ModuleRemoteSubmix>(type);
        case Type::DEFAULT:
        default:
            return ndk::SharedRefBase::make<Module>(type);
            return ndk::SharedRefBase::make<ModulePrimary>();
        case Type::R_SUBMIX:
            return ndk::SharedRefBase::make<ModuleRemoteSubmix>();
        case Type::STUB:
            return ndk::SharedRefBase::make<ModuleStub>();
        case Type::USB:
            return ndk::SharedRefBase::make<ModuleUsb>();
    }
}

@@ -128,6 +128,9 @@ std::ostream& operator<<(std::ostream& os, Module::Type t) {
        case Module::Type::R_SUBMIX:
            os << "r_submix";
            break;
        case Module::Type::STUB:
            os << "stub";
            break;
        case Module::Type::USB:
            os << "usb";
            break;
@@ -292,6 +295,9 @@ internal::Configuration& Module::getConfig() {
            case Type::R_SUBMIX:
                mConfig = std::move(internal::getRSubmixConfiguration());
                break;
            case Type::STUB:
                mConfig = std::move(internal::getStubConfiguration());
                break;
            case Type::USB:
                mConfig = std::move(internal::getUsbConfiguration());
                break;
@@ -395,38 +401,26 @@ ndk::ScopedAStatus Module::setModuleDebug(
}

ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
    if (!mTelephony) {
        mTelephony = ndk::SharedRefBase::make<Telephony>();
    }
    *_aidl_return = mTelephony.getPtr();
    LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
    *_aidl_return = nullptr;
    LOG(DEBUG) << __func__ << ": returning null";
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
    if (!mBluetooth) {
        mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
    }
    *_aidl_return = mBluetooth.getPtr();
    LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
    *_aidl_return = nullptr;
    LOG(DEBUG) << __func__ << ": returning null";
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
    if (!mBluetoothA2dp) {
        mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
    }
    *_aidl_return = mBluetoothA2dp.getPtr();
    LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
    *_aidl_return = nullptr;
    LOG(DEBUG) << __func__ << ": returning null";
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
    if (!mBluetoothLe) {
        mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
    }
    *_aidl_return = mBluetoothLe.getPtr();
    LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get();
    *_aidl_return = nullptr;
    LOG(DEBUG) << __func__ << ": returning null";
    return ndk::ScopedAStatus::ok();
}

@@ -1334,22 +1328,6 @@ bool Module::isMmapSupported() {
    return mIsMmapSupported.value();
}

ndk::ScopedAStatus Module::createInputStream(const SinkMetadata& sinkMetadata,
                                             StreamContext&& context,
                                             const std::vector<MicrophoneInfo>& microphones,
                                             std::shared_ptr<StreamIn>* result) {
    return createStreamInstance<StreamInStub>(result, sinkMetadata, std::move(context),
                                              microphones);
}

ndk::ScopedAStatus Module::createOutputStream(const SourceMetadata& sourceMetadata,
                                              StreamContext&& context,
                                              const std::optional<AudioOffloadInfo>& offloadInfo,
                                              std::shared_ptr<StreamOut>* result) {
    return createStreamInstance<StreamOutStub>(result, sourceMetadata, std::move(context),
                                               offloadInfo);
}

ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
    LOG(VERBOSE) << __func__ << ": do nothing and return ok";
    return ndk::ScopedAStatus::ok();
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.
 */

#include <vector>

#define LOG_TAG "AHAL_ModulePrimary"
#include <Utils.h>
#include <android-base/logging.h>

#include "core-impl/ModulePrimary.h"
#include "core-impl/StreamStub.h"
#include "core-impl/Telephony.h"

using aidl::android::hardware::audio::common::SinkMetadata;
using aidl::android::hardware::audio::common::SourceMetadata;
using aidl::android::media::audio::common::AudioOffloadInfo;
using aidl::android::media::audio::common::AudioPort;
using aidl::android::media::audio::common::AudioPortConfig;
using aidl::android::media::audio::common::MicrophoneInfo;

namespace aidl::android::hardware::audio::core {

ndk::ScopedAStatus ModulePrimary::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
    if (!mTelephony) {
        mTelephony = ndk::SharedRefBase::make<Telephony>();
    }
    *_aidl_return = mTelephony.getPtr();
    LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus ModulePrimary::createInputStream(const SinkMetadata& sinkMetadata,
                                                    StreamContext&& context,
                                                    const std::vector<MicrophoneInfo>& microphones,
                                                    std::shared_ptr<StreamIn>* result) {
    return createStreamInstance<StreamInStub>(result, sinkMetadata, std::move(context),
                                              microphones);
}

ndk::ScopedAStatus ModulePrimary::createOutputStream(
        const SourceMetadata& sourceMetadata, StreamContext&& context,
        const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) {
    return createStreamInstance<StreamOutStub>(result, sourceMetadata, std::move(context),
                                               offloadInfo);
}

}  // namespace aidl::android::hardware::audio::core
+5 −0
Original line number Diff line number Diff line
@@ -9,6 +9,11 @@
    <version>1</version>
    <fqname>IModule/r_submix</fqname>
  </hal>
  <hal format="aidl">
    <name>android.hardware.audio.core</name>
    <version>1</version>
    <fqname>IModule/stub</fqname>
  </hal>
  <hal format="aidl">
    <name>android.hardware.audio.core</name>
    <version>1</version>
Loading