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

Commit 39c12fab authored by Sandeep Siddhartha's avatar Sandeep Siddhartha
Browse files

Use blob (shared memory) for large data in sound model/recognition event/config

Also add a missing null check in writeBlob

Bug: 16516353
Change-Id: Ie702f8daae541cab7c2cee6e13d49e7fc84c84e1
parent e0b8c378
Loading
Loading
Loading
Loading
+3 −1
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package android.hardware.soundtrigger;
package android.hardware.soundtrigger;


import android.hardware.soundtrigger.SoundTrigger;

/**
/**
 * @hide
 * @hide
 */
 */
@@ -27,7 +29,7 @@ oneway interface IRecognitionStatusCallback {
     *        TODO: See if the data being passed in works well, if not use shared memory.
     *        TODO: See if the data being passed in works well, if not use shared memory.
     *        This *MUST* not exceed 100K.
     *        This *MUST* not exceed 100K.
     */
     */
    void onDetected(in byte[] data);
    void onDetected(in SoundTrigger.RecognitionEvent recognitionEvent);
    /**
    /**
     * Called when the detection for the associated keyphrase stops.
     * Called when the detection for the associated keyphrase stops.
     */
     */
+2 −1
Original line number Original line Diff line number Diff line
@@ -22,3 +22,4 @@ parcelable SoundTrigger.KeyphraseRecognitionExtra;
parcelable SoundTrigger.KeyphraseSoundModel;
parcelable SoundTrigger.KeyphraseSoundModel;
parcelable SoundTrigger.ModuleProperties;
parcelable SoundTrigger.ModuleProperties;
parcelable SoundTrigger.RecognitionConfig;
parcelable SoundTrigger.RecognitionConfig;
parcelable SoundTrigger.RecognitionEvent;
 No newline at end of file
+85 −26
Original line number Original line Diff line number Diff line
@@ -347,12 +347,7 @@ public class SoundTrigger {


        private static KeyphraseSoundModel fromParcel(Parcel in) {
        private static KeyphraseSoundModel fromParcel(Parcel in) {
            UUID uuid = UUID.fromString(in.readString());
            UUID uuid = UUID.fromString(in.readString());
            byte[] data = null;
            byte[] data = in.readBlob();
            int dataLength = in.readInt();
            if (dataLength >= 0) {
                data = new byte[dataLength];
                in.readByteArray(data);
            }
            Keyphrase[] keyphrases = in.createTypedArray(Keyphrase.CREATOR);
            Keyphrase[] keyphrases = in.createTypedArray(Keyphrase.CREATOR);
            return new KeyphraseSoundModel(uuid, data, keyphrases);
            return new KeyphraseSoundModel(uuid, data, keyphrases);
        }
        }
@@ -365,12 +360,7 @@ public class SoundTrigger {
        @Override
        @Override
        public void writeToParcel(Parcel dest, int flags) {
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(uuid.toString());
            dest.writeString(uuid.toString());
            if (data != null) {
            dest.writeBlob(data);
                dest.writeInt(data.length);
                dest.writeByteArray(data);
            } else {
                dest.writeInt(-1);
            }
            dest.writeTypedArray(keyphrases, 0);
            dest.writeTypedArray(keyphrases, 0);
        }
        }


@@ -406,7 +396,7 @@ public class SoundTrigger {
     *  {@link StatusListener#onRecognition(RecognitionEvent)}
     *  {@link StatusListener#onRecognition(RecognitionEvent)}
     *  callback upon recognition success or failure.
     *  callback upon recognition success or failure.
     */
     */
    public static class RecognitionEvent {
    public static class RecognitionEvent implements Parcelable {
        /** Recognition status e.g {@link #RECOGNITION_STATUS_SUCCESS} */
        /** Recognition status e.g {@link #RECOGNITION_STATUS_SUCCESS} */
        public final int status;
        public final int status;
        /** Sound Model corresponding to this event callback */
        /** Sound Model corresponding to this event callback */
@@ -425,7 +415,7 @@ public class SoundTrigger {
         * typically during enrollment. */
         * typically during enrollment. */
        public final byte[] data;
        public final byte[] data;


        RecognitionEvent(int status, int soundModelHandle, boolean captureAvailable,
        public RecognitionEvent(int status, int soundModelHandle, boolean captureAvailable,
                int captureSession, int captureDelayMs, int capturePreambleMs, byte[] data) {
                int captureSession, int captureDelayMs, int capturePreambleMs, byte[] data) {
            this.status = status;
            this.status = status;
            this.soundModelHandle = soundModelHandle;
            this.soundModelHandle = soundModelHandle;
@@ -435,6 +425,85 @@ public class SoundTrigger {
            this.capturePreambleMs = capturePreambleMs;
            this.capturePreambleMs = capturePreambleMs;
            this.data = data;
            this.data = data;
        }
        }

        public static final Parcelable.Creator<RecognitionEvent> CREATOR
                = new Parcelable.Creator<RecognitionEvent>() {
            public RecognitionEvent createFromParcel(Parcel in) {
                return RecognitionEvent.fromParcel(in);
            }

            public RecognitionEvent[] newArray(int size) {
                return new RecognitionEvent[size];
            }
        };

        private static RecognitionEvent fromParcel(Parcel in) {
            int status = in.readInt();
            int soundModelHandle = in.readInt();
            boolean captureAvailable = in.readByte() == 1;
            int captureSession = in.readInt();
            int captureDelayMs = in.readInt();
            int capturePreambleMs = in.readInt();
            byte[] data = in.readBlob();
            return new RecognitionEvent(status, soundModelHandle, captureAvailable, captureSession,
                    captureDelayMs, capturePreambleMs, data);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(status);
            dest.writeInt(soundModelHandle);
            dest.writeByte((byte) (captureAvailable ? 1 : 0));
            dest.writeInt(captureSession);
            dest.writeInt(captureDelayMs);
            dest.writeInt(capturePreambleMs);
            dest.writeBlob(data);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (captureAvailable ? 1231 : 1237);
            result = prime * result + captureDelayMs;
            result = prime * result + capturePreambleMs;
            result = prime * result + captureSession;
            result = prime * result + Arrays.hashCode(data);
            result = prime * result + soundModelHandle;
            result = prime * result + status;
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            RecognitionEvent other = (RecognitionEvent) obj;
            if (captureAvailable != other.captureAvailable)
                return false;
            if (captureDelayMs != other.captureDelayMs)
                return false;
            if (capturePreambleMs != other.capturePreambleMs)
                return false;
            if (captureSession != other.captureSession)
                return false;
            if (!Arrays.equals(data, other.data))
                return false;
            if (soundModelHandle != other.soundModelHandle)
                return false;
            if (status != other.status)
                return false;
            return true;
        }
    }
    }


    /**
    /**
@@ -475,12 +544,7 @@ public class SoundTrigger {
            boolean captureRequested = in.readByte() == 1;
            boolean captureRequested = in.readByte() == 1;
            KeyphraseRecognitionExtra[] keyphrases =
            KeyphraseRecognitionExtra[] keyphrases =
                    in.createTypedArray(KeyphraseRecognitionExtra.CREATOR);
                    in.createTypedArray(KeyphraseRecognitionExtra.CREATOR);
            byte[] data = null;
            byte[] data = in.readBlob();
            int dataLength = in.readInt();
            if (dataLength >= 0) {
                data = new byte[dataLength];
                in.readByteArray(data);
            }
            return new RecognitionConfig(captureRequested, keyphrases, data);
            return new RecognitionConfig(captureRequested, keyphrases, data);
        }
        }


@@ -488,12 +552,7 @@ public class SoundTrigger {
        public void writeToParcel(Parcel dest, int flags) {
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeByte((byte) (captureRequested ? 1 : 0));
            dest.writeByte((byte) (captureRequested ? 1 : 0));
            dest.writeTypedArray(keyphrases, 0);
            dest.writeTypedArray(keyphrases, 0);
            if (data != null) {
            dest.writeBlob(data);
                dest.writeInt(data.length);
                dest.writeByteArray(data);
            } else {
                dest.writeInt(-1);
            }
        }
        }


        @Override
        @Override
+2 −0
Original line number Original line Diff line number Diff line
@@ -485,6 +485,7 @@ public final class Parcel {
     * growing {@link #dataCapacity} if needed.
     * growing {@link #dataCapacity} if needed.
     * @param b Bytes to place into the parcel.
     * @param b Bytes to place into the parcel.
     * {@hide}
     * {@hide}
     * {@SystemApi}
     */
     */
    public final void writeBlob(byte[] b) {
    public final void writeBlob(byte[] b) {
        nativeWriteBlob(mNativePtr, b, 0, (b != null) ? b.length : 0);
        nativeWriteBlob(mNativePtr, b, 0, (b != null) ? b.length : 0);
@@ -1714,6 +1715,7 @@ public final class Parcel {
    /**
    /**
     * Read a blob of data from the parcel and return it as a byte array.
     * Read a blob of data from the parcel and return it as a byte array.
     * {@hide}
     * {@hide}
     * {@SystemApi}
     */
     */
    public final byte[] readBlob() {
    public final byte[] readBlob() {
        return nativeReadBlob(mNativePtr);
        return nativeReadBlob(mNativePtr);
+3 −2
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ import android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionExtra;
import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
import android.hardware.soundtrigger.SoundTrigger.ModuleProperties;
import android.hardware.soundtrigger.SoundTrigger.ModuleProperties;
import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig;
import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig;
import android.hardware.soundtrigger.SoundTrigger.RecognitionEvent;
import android.os.AsyncTask;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Handler;
import android.os.Message;
import android.os.Message;
@@ -380,10 +381,10 @@ public class AlwaysOnHotwordDetector {
        }
        }


        @Override
        @Override
        public void onDetected(byte[] data) {
        public void onDetected(RecognitionEvent recognitionEvent) {
            Slog.i(TAG, "onDetected");
            Slog.i(TAG, "onDetected");
            Message message = Message.obtain(mHandler, MSG_HOTWORD_DETECTED);
            Message message = Message.obtain(mHandler, MSG_HOTWORD_DETECTED);
            message.obj = data;
            message.obj = recognitionEvent.data;
            message.sendToTarget();
            message.sendToTarget();
        }
        }


Loading