From 783719c72e6659e20c48fd57171ac957df5a148b Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 12 Nov 2023 12:48:56 +0100 Subject: [PATCH] Fix OPUS packet in an endian-independent way Reading the header id as an int assumed that the current endianness was little endian. Read to a byte array to remove this assumption. --- .../src/main/java/com/genymobile/scrcpy/Streamer.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Streamer.java b/server/src/main/java/com/genymobile/scrcpy/Streamer.java index 39f74fb6..c3f1c6ee 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Streamer.java +++ b/server/src/main/java/com/genymobile/scrcpy/Streamer.java @@ -5,14 +5,13 @@ import android.media.MediaCodec; import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.Arrays; public final class Streamer { private static final long PACKET_FLAG_CONFIG = 1L << 63; private static final long PACKET_FLAG_KEY_FRAME = 1L << 62; - private static final long AOPUSHDR = 0x5244485355504F41L; // "AOPUSHDR" in ASCII (little-endian) - private final FileDescriptor fd; private final Codec codec; private final boolean sendCodecMeta; @@ -120,11 +119,14 @@ public final class Streamer { throw new IOException("Not enough data in OPUS config packet"); } - long id = buffer.getLong(); - if (id != AOPUSHDR) { + final byte[] opusHeaderId = {'A', 'O', 'P', 'U', 'S', 'H', 'D', 'R'}; + byte[] idBuffer = new byte[8]; + buffer.get(idBuffer); + if (!Arrays.equals(idBuffer, opusHeaderId)) { throw new IOException("OPUS header not found"); } + // The size is in native byte-order long sizeLong = buffer.getLong(); if (sizeLong < 0 || sizeLong >= 0x7FFFFFFF) { throw new IOException("Invalid block size in OPUS header: " + sizeLong);