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

Commit e80fd680 authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Support sound models with no data

This change allows using SoundModels with null or empty data.

Test: Manual testing of soundtrigger use-cases.
      Testing of null-/empty-data models using SoundTriggerTestService
      and mocked HAL.
Fixes: 181103994
Change-Id: I00b3c4e343a81e9f4f4580becf1128fd3ba60b14
parent 26835425
Loading
Loading
Loading
Loading
+8 −13
Original line number Original line Diff line number Diff line
@@ -34,10 +34,7 @@ import android.media.soundtrigger_middleware.SoundTriggerModuleDescriptor;
import android.media.soundtrigger_middleware.SoundTriggerModuleProperties;
import android.media.soundtrigger_middleware.SoundTriggerModuleProperties;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.os.SharedMemory;
import android.os.SharedMemory;
import android.system.ErrnoException;


import java.io.FileDescriptor;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Arrays;
import java.util.UUID;
import java.util.UUID;
@@ -111,13 +108,9 @@ class ConversionUtil {
        aidlModel.type = apiModel.getType();
        aidlModel.type = apiModel.getType();
        aidlModel.uuid = api2aidlUuid(apiModel.getUuid());
        aidlModel.uuid = api2aidlUuid(apiModel.getUuid());
        aidlModel.vendorUuid = api2aidlUuid(apiModel.getVendorUuid());
        aidlModel.vendorUuid = api2aidlUuid(apiModel.getVendorUuid());
        try {
        byte[] data = apiModel.getData();
            aidlModel.data = ParcelFileDescriptor.dup(
        aidlModel.data = byteArrayToSharedMemory(data, "SoundTrigger SoundModel");
                    byteArrayToSharedMemory(apiModel.getData(), "SoundTrigger SoundModel"));
        aidlModel.dataSize = data.length;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        aidlModel.dataSize = apiModel.getData().length;
        return aidlModel;
        return aidlModel;
    }
    }


@@ -379,7 +372,7 @@ class ConversionUtil {
        return result;
        return result;
    }
    }


    private static @Nullable FileDescriptor byteArrayToSharedMemory(byte[] data, String name) {
    private static @Nullable ParcelFileDescriptor byteArrayToSharedMemory(byte[] data, String name) {
        if (data.length == 0) {
        if (data.length == 0) {
            return null;
            return null;
        }
        }
@@ -389,8 +382,10 @@ class ConversionUtil {
            ByteBuffer buffer = shmem.mapReadWrite();
            ByteBuffer buffer = shmem.mapReadWrite();
            buffer.put(data);
            buffer.put(data);
            shmem.unmap(buffer);
            shmem.unmap(buffer);
            return shmem.getFileDescriptor();
            ParcelFileDescriptor fd = shmem.getFdDup();
        } catch (ErrnoException e) {
            shmem.close();
            return fd;
        } catch (Exception e) {
            throw new RuntimeException(e);
            throw new RuntimeException(e);
        }
        }
    }
    }
+2 −2
Original line number Original line Diff line number Diff line
@@ -32,8 +32,8 @@ parcelable SoundModel {
     * Unique vendor ID. Identifies the engine the sound model
     * Unique vendor ID. Identifies the engine the sound model
     * was build for */
     * was build for */
    String vendorUuid;
    String vendorUuid;
    /** Opaque data transparent to Android framework */
    /** Opaque data transparent to Android framework. May be null if dataSize is 0. */
    ParcelFileDescriptor data;
    @nullable ParcelFileDescriptor data;
    /** Size of the above data, in bytes. */
    /** Size of the above data, in bytes. */
    int dataSize;
    int dataSize;
}
}
+29 −12
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@ import android.media.soundtrigger_middleware.RecognitionStatus;
import android.media.soundtrigger_middleware.SoundModel;
import android.media.soundtrigger_middleware.SoundModel;
import android.media.soundtrigger_middleware.SoundModelType;
import android.media.soundtrigger_middleware.SoundModelType;
import android.media.soundtrigger_middleware.SoundTriggerModuleProperties;
import android.media.soundtrigger_middleware.SoundTriggerModuleProperties;
import android.os.HidlMemory;
import android.os.HidlMemoryUtil;
import android.os.HidlMemoryUtil;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;


@@ -199,18 +200,7 @@ class ConversionUtil {
        hidlModel.header.type = aidl2hidlSoundModelType(aidlModel.type);
        hidlModel.header.type = aidl2hidlSoundModelType(aidlModel.type);
        hidlModel.header.uuid = aidl2hidlUuid(aidlModel.uuid);
        hidlModel.header.uuid = aidl2hidlUuid(aidlModel.uuid);
        hidlModel.header.vendorUuid = aidl2hidlUuid(aidlModel.vendorUuid);
        hidlModel.header.vendorUuid = aidl2hidlUuid(aidlModel.vendorUuid);

        hidlModel.data = parcelFileDescriptorToHidlMemory(aidlModel.data, aidlModel.dataSize);
        // Extract a dup of the underlying FileDescriptor out of aidlModel.data without changing
        // the original.
        FileDescriptor fd = new FileDescriptor();
        try {
            ParcelFileDescriptor dup = aidlModel.data.dup();
            fd.setInt$(dup.detachFd());
            hidlModel.data = HidlMemoryUtil.fileDescriptorToHidlMemory(fd, aidlModel.dataSize);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return hidlModel;
        return hidlModel;
    }
    }


@@ -425,4 +415,31 @@ class ConversionUtil {
        }
        }
        return aidlCapabilities;
        return aidlCapabilities;
    }
    }

    /**
     * Convert an AIDL representation of a shared memory block (ParcelFileDescriptor + size) to the
     * HIDL representation (HidlMemory). Will not change the incoming data or any ownership
     * semantics, but rather duplicate the underlying FD.
     *
     * @param data     The incoming memory block. May be null if dataSize is 0.
     * @param dataSize The number of bytes in the block.
     * @return A HidlMemory representation of the memory block. Will be non-null even for an empty
     *         block.
     */
    private static @NonNull
    HidlMemory parcelFileDescriptorToHidlMemory(@Nullable ParcelFileDescriptor data, int dataSize) {
        if (dataSize > 0) {
            // Extract a dup of the underlying FileDescriptor out of data.
            FileDescriptor fd = new FileDescriptor();
            try {
                ParcelFileDescriptor dup = data.dup();
                fd.setInt$(dup.detachFd());
                return HidlMemoryUtil.fileDescriptorToHidlMemory(fd, dataSize);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            return HidlMemoryUtil.fileDescriptorToHidlMemory(null, 0);
        }
    }
}
}
+3 −1
Original line number Original line Diff line number Diff line
@@ -62,8 +62,10 @@ public class ValidationUtil {
        }
        }
        validateUuid(model.uuid);
        validateUuid(model.uuid);
        validateUuid(model.vendorUuid);
        validateUuid(model.vendorUuid);
        if (model.dataSize > 0) {
            Objects.requireNonNull(model.data);
            Objects.requireNonNull(model.data);
        }
        }
    }


    static void validatePhraseModel(@Nullable PhraseSoundModel model) {
    static void validatePhraseModel(@Nullable PhraseSoundModel model) {
        Objects.requireNonNull(model);
        Objects.requireNonNull(model);