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

Commit 3c7cd349 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi Committed by Android (Google) Code Review
Browse files

Merge "AudioService: SpatializerHelper queries spatialized masks" into main

parents 077e8b2f 8a3bfcc9
Loading
Loading
Loading
Loading
+87 −2
Original line number Diff line number Diff line
@@ -714,7 +714,7 @@ public final class AudioFormat implements Parcelable {
    /**
     * @hide
     * Return a channel mask ready to be used by native code
     * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
     * @param javaMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
     * @return a native channel mask
     */
    public static int convertChannelOutMaskToNativeMask(int javaMask) {
@@ -724,13 +724,98 @@ public final class AudioFormat implements Parcelable {
    /**
     * @hide
     * Return a java output channel mask
     * @param mask a native channel mask
     * @param nativeMask a native channel mask
     * @return a combination of the CHANNEL_OUT_* definitions
     */
    public static int convertNativeChannelMaskToOutMask(int nativeMask) {
        return (nativeMask << 2);
    }

    /**
     * @hide
     * Return a human-readable string from a java channel mask
     * @param javaMask a bit field of CHANNEL_OUT_* values
     * @return a string in the "mono", "stereo", "5.1" style, or the hex version when not a standard
     *   mask.
     */
    public static String javaChannelOutMaskToString(int javaMask) {
        // save haptics info for end of string
        int haptics = javaMask & (CHANNEL_OUT_HAPTIC_A | CHANNEL_OUT_HAPTIC_B);
        // continue without looking at haptic channels
        javaMask &= ~(CHANNEL_OUT_HAPTIC_A | CHANNEL_OUT_HAPTIC_B);
        StringBuilder result = new StringBuilder("");
        switch (javaMask) {
            case CHANNEL_OUT_MONO:
                result.append("mono");
                break;
            case CHANNEL_OUT_STEREO:
                result.append("stereo");
                break;
            case CHANNEL_OUT_QUAD:
                result.append("quad");
                break;
            case CHANNEL_OUT_QUAD_SIDE:
                result.append("quad side");
                break;
            case CHANNEL_OUT_SURROUND:
                result.append("4.0");
                break;
            case CHANNEL_OUT_5POINT1:
                result.append("5.1");
                break;
            case CHANNEL_OUT_6POINT1:
                result.append("6.1");
                break;
            case CHANNEL_OUT_5POINT1_SIDE:
                result.append("5.1 side");
                break;
            case CHANNEL_OUT_7POINT1:
                result.append("7.1 (5 fronts)");
                break;
            case CHANNEL_OUT_7POINT1_SURROUND:
                result.append("7.1");
                break;
            case CHANNEL_OUT_5POINT1POINT2:
                result.append("5.1.2");
                break;
            case CHANNEL_OUT_5POINT1POINT4:
                result.append("5.1.4");
                break;
            case CHANNEL_OUT_7POINT1POINT2:
                result.append("7.1.2");
                break;
            case CHANNEL_OUT_7POINT1POINT4:
                result.append("7.1.4");
                break;
            case CHANNEL_OUT_9POINT1POINT4:
                result.append("9.1.4");
                break;
            case CHANNEL_OUT_9POINT1POINT6:
                result.append("9.1.6");
                break;
            case CHANNEL_OUT_13POINT_360RA:
                result.append("360RA 13ch");
                break;
            case CHANNEL_OUT_22POINT2:
                result.append("22.2");
                break;
            default:
                result.append("0x").append(Integer.toHexString(javaMask));
                break;
        }
        if ((haptics & (CHANNEL_OUT_HAPTIC_A | CHANNEL_OUT_HAPTIC_B)) != 0) {
            result.append("(+haptic ");
            if ((haptics & CHANNEL_OUT_HAPTIC_A) == CHANNEL_OUT_HAPTIC_A) {
                result.append("A");
            }
            if ((haptics & CHANNEL_OUT_HAPTIC_B) == CHANNEL_OUT_HAPTIC_B) {
                result.append("B");
            }
            result.append(")");
        }
        return result.toString();
    }

    public static final int CHANNEL_IN_DEFAULT = 1;
    // These directly match native
    public static final int CHANNEL_IN_LEFT = 0x4;
+22 −14
Original line number Diff line number Diff line
@@ -129,6 +129,8 @@ public class SpatializerHelper {
    /** current level as reported by native Spatializer in callback */
    private int mSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
    private int mCapableSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
    /** cached version of Spatializer.getSpatializedChannelMasks */
    private List<Integer> mSpatializedChannelMasks = Collections.emptyList();

    private boolean mTransauralSupported = false;
    private boolean mBinauralSupported = false;
@@ -1029,6 +1031,17 @@ public class SpatializerHelper {
                postReset();
                return;
            }
            try {
                final int[] nativeMasks = mSpat.getSpatializedChannelMasks();
                for (int i = 0; i < nativeMasks.length; i++) {
                    nativeMasks[i] = AudioFormat.convertNativeChannelMaskToOutMask(nativeMasks[i]);
                }
                mSpatializedChannelMasks = Arrays.stream(nativeMasks).boxed().toList();

            } catch (Exception e) { // just catch Exception in case nativeMasks is null
                Log.e(TAG, "Error calling getSpatializedChannelMasks", e);
                mSpatializedChannelMasks = Collections.emptyList();
            }
            try {
                //TODO: register heatracking callback only when sensors are registered
                if (mIsHeadTrackingSupported) {
@@ -1103,20 +1116,7 @@ public class SpatializerHelper {
    }

    synchronized @NonNull List<Integer> getSpatializedChannelMasks() {
        if (!checkSpatializer("getSpatializedChannelMasks")) {
            return Collections.emptyList();
        }
        try {
            final int[] nativeMasks = new int[0]; // FIXME mSpat query goes here
            for (int i = 0; i < nativeMasks.length; i++) {
                nativeMasks[i] = AudioFormat.convertNativeChannelMaskToOutMask(nativeMasks[i]);
            }
            final List<Integer> masks = Arrays.stream(nativeMasks).boxed().toList();
            return masks;
        } catch (Exception e) { // just catch Exception in case nativeMasks is null
            Log.e(TAG, "Error calling getSpatializedChannelMasks", e);
            return Collections.emptyList();
        }
        return mSpatializedChannelMasks;
    }

    //------------------------------------------------------
@@ -1622,6 +1622,14 @@ public class SpatializerHelper {
        pw.println("\tmState:" + mState);
        pw.println("\tmSpatLevel:" + mSpatLevel);
        pw.println("\tmCapableSpatLevel:" + mCapableSpatLevel);
        List<Integer> speakerMasks = getSpatializedChannelMasks();
        StringBuilder masks = speakerMasks.isEmpty()
                ? new StringBuilder("none") : new StringBuilder("");
        for (Integer mask : speakerMasks) {
            masks.append(AudioFormat.javaChannelOutMaskToString(mask)).append(" ");
        }
        pw.println("\tspatialized speaker masks: " + masks);

        pw.println("\tmIsHeadTrackingSupported:" + mIsHeadTrackingSupported);
        StringBuilder modesString = new StringBuilder();
        for (int mode : mSupportedHeadTrackingModes) {