Skip to content

Commit

Permalink
Encode emsg duration & ID as int64 instead of uint32
Browse files Browse the repository at this point in the history
The serialization scheme used here is custom, it doesn't need
to be compatible with emsg-v0 or emsg-v1 (since
97183ef).

This means that C.TIME_UNSET will propagate correctly through the
serialization.

Issue: #9123
PiperOrigin-RevId: 382762873
  • Loading branch information
icbaker committed Jul 16, 2021
1 parent f5d8efb commit 3430912
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit 3430912

Please sign in to comment.