From 3430912581b60a651082c51e6e2b9549e3cdefc2 Mon Sep 17 00:00:00 2001 From: ibaker Date: Fri, 2 Jul 2021 18:07:15 +0100 Subject: [PATCH] Encode emsg duration & ID as int64 instead of uint32 The serialization scheme used here is custom, it doesn't need to be compatible with emsg-v0 or emsg-v1 (since https://github.com/google/ExoPlayer/commit/97183ef55866170807910cd626264d82d41d46d4). This means that C.TIME_UNSET will propagate correctly through the serialization. Issue: #9123 PiperOrigin-RevId: 382762873 --- RELEASENOTES.md | 3 +++ .../metadata/emsg/EventMessageDecoder.java | 4 ++-- .../metadata/emsg/EventMessageEncoder.java | 13 ++--------- .../emsg/EventMessageDecoderTest.java | 4 ++-- .../emsg/EventMessageEncoderTest.java | 22 +++++++++++++++---- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 12583fb132a..5f9846a0613 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -37,6 +37,9 @@ ([#8960](https://github.com/google/ExoPlayer/issues/8960)). * DRM: * Allow repeated provisioning in `DefaultDrmSession(Manager)`. +* Metadata: + * Fix handling of emsg messages with an unset duration + ([#9123](https://github.com/google/ExoPlayer/issues/9123)). * UI: * Add `PendingIntent.FLAG_IMMUTABLE` flag when creating a broadcast intent in `PlayerNotificationManager`. This is required to avoid an error on diff --git a/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java b/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java index 8a7e1851c6b..999f0228bd5 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java @@ -35,8 +35,8 @@ protected Metadata decode(MetadataInputBuffer inputBuffer, ByteBuffer buffer) { public EventMessage decode(ParsableByteArray emsgData) { String schemeIdUri = Assertions.checkNotNull(emsgData.readNullTerminatedString()); String value = Assertions.checkNotNull(emsgData.readNullTerminatedString()); - long durationMs = emsgData.readUnsignedInt(); - long id = emsgData.readUnsignedInt(); + long durationMs = emsgData.readLong(); + long id = emsgData.readLong(); byte[] messageData = Arrays.copyOfRange(emsgData.getData(), emsgData.getPosition(), emsgData.limit()); return new EventMessage(schemeIdUri, value, durationMs, id, messageData); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java b/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java index 4fa3f71b32d..81c11ba48c7 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java @@ -45,8 +45,8 @@ public byte[] encode(EventMessage eventMessage) { writeNullTerminatedString(dataOutputStream, eventMessage.schemeIdUri); String nonNullValue = eventMessage.value != null ? eventMessage.value : ""; writeNullTerminatedString(dataOutputStream, nonNullValue); - writeUnsignedInt(dataOutputStream, eventMessage.durationMs); - writeUnsignedInt(dataOutputStream, eventMessage.id); + dataOutputStream.writeLong(eventMessage.durationMs); + dataOutputStream.writeLong(eventMessage.id); dataOutputStream.write(eventMessage.messageData); dataOutputStream.flush(); return byteArrayOutputStream.toByteArray(); @@ -61,13 +61,4 @@ private static void writeNullTerminatedString(DataOutputStream dataOutputStream, dataOutputStream.writeBytes(value); dataOutputStream.writeByte(0); } - - private static void writeUnsignedInt(DataOutputStream outputStream, long value) - throws IOException { - outputStream.writeByte((int) (value >>> 24) & 0xFF); - outputStream.writeByte((int) (value >>> 16) & 0xFF); - outputStream.writeByte((int) (value >>> 8) & 0xFF); - outputStream.writeByte((int) value & 0xFF); - } - } diff --git a/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoderTest.java b/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoderTest.java index ed06eb0aff8..2bd02446daa 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoderTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoderTest.java @@ -37,8 +37,8 @@ public void decodeEventMessage() { Bytes.concat( createByteArray(117, 114, 110, 58, 116, 101, 115, 116, 0), // scheme_id_uri = "urn:test" createByteArray(49, 50, 51, 0), // value = "123" - createByteArray(0, 0, 11, 184), // event_duration_ms = 3000 - createByteArray(0, 15, 67, 211), // id = 1000403 + createByteArray(0, 0, 0, 0, 0, 0, 11, 184), // event_duration_ms = 3000 + createByteArray(0, 0, 0, 0, 0, 15, 67, 211), // id = 1000403 createByteArray(0, 1, 2, 3, 4)); // message_data = {0, 1, 2, 3, 4} EventMessageDecoder decoder = new EventMessageDecoder(); diff --git a/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoderTest.java b/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoderTest.java index c6d2231eb2d..4a484943b8e 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoderTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoderTest.java @@ -18,9 +18,11 @@ import static com.google.common.truth.Truth.assertThat; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.metadata.MetadataInputBuffer; import com.google.android.exoplayer2.util.Assertions; +import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.common.primitives.Bytes; import java.io.IOException; import java.nio.ByteBuffer; @@ -38,8 +40,8 @@ public final class EventMessageEncoderTest { Bytes.concat( createByteArray(117, 114, 110, 58, 116, 101, 115, 116, 0), // scheme_id_uri = "urn:test" createByteArray(49, 50, 51, 0), // value = "123" - createByteArray(0, 0, 11, 184), // event_duration_ms = 3000 - createByteArray(0, 15, 67, 211), // id = 1000403 + createByteArray(0, 0, 0, 0, 0, 0, 11, 184), // event_duration_ms = 3000 + createByteArray(0, 0, 0, 0, 0, 15, 67, 211), // id = 1000403 createByteArray(0, 1, 2, 3, 4)); // message_data = {0, 1, 2, 3, 4} @Test @@ -67,8 +69,8 @@ public void encodeEventStreamMultipleTimesWorkingCorrectly() throws IOException Bytes.concat( createByteArray(117, 114, 110, 58, 116, 101, 115, 116, 0), // scheme_id_uri = "urn:test" createByteArray(49, 50, 51, 0), // value = "123" - createByteArray(0, 0, 11, 184), // event_duration_ms = 3000 - createByteArray(0, 15, 67, 210), // id = 1000402 + createByteArray(0, 0, 0, 0, 0, 0, 11, 184), // event_duration_ms = 3000 + createByteArray(0, 0, 0, 0, 0, 15, 67, 210), // id = 1000402 createByteArray(4, 3, 2, 1, 0)); // message_data = {4, 3, 2, 1, 0} EventMessageEncoder eventMessageEncoder = new EventMessageEncoder(); @@ -78,6 +80,18 @@ public void encodeEventStreamMultipleTimesWorkingCorrectly() throws IOException assertThat(encodedByteArray1).isEqualTo(expectedEmsgBody1); } + // https://github.com/google/ExoPlayer/issues/9123 + @Test + public void encodeDecodeEventMessage_durationNotSet() { + EventMessage originalMessage = + new EventMessage("urn:test", "456", C.TIME_UNSET, 99, new byte[] {7, 8, 9}); + byte[] encodedMessage = new EventMessageEncoder().encode(originalMessage); + EventMessage decodedMessage = + new EventMessageDecoder().decode(new ParsableByteArray(encodedMessage)); + + assertThat(decodedMessage).isEqualTo(originalMessage); + } + /** Converts an array of integers in the range [0, 255] into an equivalent byte array. */ // TODO(internal b/161804035): Move to a single file. private static byte[] createByteArray(int... bytes) {