summaryrefslogtreecommitdiffstats
path: root/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg
diff options
context:
space:
mode:
Diffstat (limited to 'mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg')
-rw-r--r--mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessage.java202
-rw-r--r--mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java47
-rw-r--r--mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java73
-rw-r--r--mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/package-info.java19
4 files changed, 341 insertions, 0 deletions
diff --git a/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessage.java b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessage.java
new file mode 100644
index 0000000000..01aac27a27
--- /dev/null
+++ b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessage.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.emsg;
+
+import static org.mozilla.thirdparty.com.google.android.exoplayer2.util.Util.castNonNull;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import androidx.annotation.Nullable;
+import androidx.annotation.VisibleForTesting;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.Format;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.Metadata;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.util.MimeTypes;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.util.Util;
+import java.util.Arrays;
+
+/** An Event Message (emsg) as defined in ISO 23009-1. */
+public final class EventMessage implements Metadata.Entry {
+
+ /**
+ * emsg scheme_id_uri from the <a href="https://aomediacodec.github.io/av1-id3/#semantics">CMAF
+ * spec</a>.
+ */
+ @VisibleForTesting public static final String ID3_SCHEME_ID_AOM = "https://aomedia.org/emsg/ID3";
+
+ /**
+ * The Apple-hosted scheme_id equivalent to {@code ID3_SCHEME_ID_AOM} - used before AOM adoption.
+ */
+ private static final String ID3_SCHEME_ID_APPLE =
+ "https://developer.apple.com/streaming/emsg-id3";
+
+ /**
+ * scheme_id_uri from section 7.3.2 of <a
+ * href="https://www.scte.org/SCTEDocs/Standards/ANSI_SCTE%20214-3%202015.pdf">SCTE 214-3
+ * 2015</a>.
+ */
+ @VisibleForTesting public static final String SCTE35_SCHEME_ID = "urn:scte:scte35:2014:bin";
+
+ private static final Format ID3_FORMAT =
+ Format.createSampleFormat(
+ /* id= */ null, MimeTypes.APPLICATION_ID3, Format.OFFSET_SAMPLE_RELATIVE);
+ private static final Format SCTE35_FORMAT =
+ Format.createSampleFormat(
+ /* id= */ null, MimeTypes.APPLICATION_SCTE35, Format.OFFSET_SAMPLE_RELATIVE);
+
+ /** The message scheme. */
+ public final String schemeIdUri;
+
+ /**
+ * The value for the event.
+ */
+ public final String value;
+
+ /**
+ * The duration of the event in milliseconds.
+ */
+ public final long durationMs;
+
+ /**
+ * The instance identifier.
+ */
+ public final long id;
+
+ /**
+ * The body of the message.
+ */
+ public final byte[] messageData;
+
+ // Lazily initialized hashcode.
+ private int hashCode;
+
+ /**
+ * @param schemeIdUri The message scheme.
+ * @param value The value for the event.
+ * @param durationMs The duration of the event in milliseconds.
+ * @param id The instance identifier.
+ * @param messageData The body of the message.
+ */
+ public EventMessage(
+ String schemeIdUri, String value, long durationMs, long id, byte[] messageData) {
+ this.schemeIdUri = schemeIdUri;
+ this.value = value;
+ this.durationMs = durationMs;
+ this.id = id;
+ this.messageData = messageData;
+ }
+
+ /* package */ EventMessage(Parcel in) {
+ schemeIdUri = castNonNull(in.readString());
+ value = castNonNull(in.readString());
+ durationMs = in.readLong();
+ id = in.readLong();
+ messageData = castNonNull(in.createByteArray());
+ }
+
+ @Override
+ @Nullable
+ public Format getWrappedMetadataFormat() {
+ switch (schemeIdUri) {
+ case ID3_SCHEME_ID_AOM:
+ case ID3_SCHEME_ID_APPLE:
+ return ID3_FORMAT;
+ case SCTE35_SCHEME_ID:
+ return SCTE35_FORMAT;
+ default:
+ return null;
+ }
+ }
+
+ @Override
+ @Nullable
+ public byte[] getWrappedMetadataBytes() {
+ return getWrappedMetadataFormat() != null ? messageData : null;
+ }
+
+ @Override
+ public int hashCode() {
+ if (hashCode == 0) {
+ int result = 17;
+ result = 31 * result + (schemeIdUri != null ? schemeIdUri.hashCode() : 0);
+ result = 31 * result + (value != null ? value.hashCode() : 0);
+ result = 31 * result + (int) (durationMs ^ (durationMs >>> 32));
+ result = 31 * result + (int) (id ^ (id >>> 32));
+ result = 31 * result + Arrays.hashCode(messageData);
+ hashCode = result;
+ }
+ return hashCode;
+ }
+
+ @Override
+ public boolean equals(@Nullable Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ EventMessage other = (EventMessage) obj;
+ return durationMs == other.durationMs
+ && id == other.id
+ && Util.areEqual(schemeIdUri, other.schemeIdUri)
+ && Util.areEqual(value, other.value)
+ && Arrays.equals(messageData, other.messageData);
+ }
+
+ @Override
+ public String toString() {
+ return "EMSG: scheme="
+ + schemeIdUri
+ + ", id="
+ + id
+ + ", durationMs="
+ + durationMs
+ + ", value="
+ + value;
+ }
+
+ // Parcelable implementation.
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(Parcel dest, int flags) {
+ dest.writeString(schemeIdUri);
+ dest.writeString(value);
+ dest.writeLong(durationMs);
+ dest.writeLong(id);
+ dest.writeByteArray(messageData);
+ }
+
+ public static final Parcelable.Creator<EventMessage> CREATOR =
+ new Parcelable.Creator<EventMessage>() {
+
+ @Override
+ public EventMessage createFromParcel(Parcel in) {
+ return new EventMessage(in);
+ }
+
+ @Override
+ public EventMessage[] newArray(int size) {
+ return new EventMessage[size];
+ }
+
+ };
+
+}
diff --git a/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java
new file mode 100644
index 0000000000..09b0a69395
--- /dev/null
+++ b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageDecoder.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.emsg;
+
+import org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.Metadata;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.MetadataDecoder;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.MetadataInputBuffer;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.util.Assertions;
+import org.mozilla.thirdparty.com.google.android.exoplayer2.util.ParsableByteArray;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+/** Decodes data encoded by {@link EventMessageEncoder}. */
+public final class EventMessageDecoder implements MetadataDecoder {
+
+ @SuppressWarnings("ByteBufferBackingArray")
+ @Override
+ public Metadata decode(MetadataInputBuffer inputBuffer) {
+ ByteBuffer buffer = Assertions.checkNotNull(inputBuffer.data);
+ byte[] data = buffer.array();
+ int size = buffer.limit();
+ return new Metadata(decode(new ParsableByteArray(data, size)));
+ }
+
+ 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();
+ byte[] messageData =
+ Arrays.copyOfRange(emsgData.data, emsgData.getPosition(), emsgData.limit());
+ return new EventMessage(schemeIdUri, value, durationMs, id, messageData);
+ }
+}
diff --git a/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java
new file mode 100644
index 0000000000..261e39ae70
--- /dev/null
+++ b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/EventMessageEncoder.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.emsg;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+/**
+ * Encodes data that can be decoded by {@link EventMessageDecoder}. This class isn't thread safe.
+ */
+public final class EventMessageEncoder {
+
+ private final ByteArrayOutputStream byteArrayOutputStream;
+ private final DataOutputStream dataOutputStream;
+
+ public EventMessageEncoder() {
+ byteArrayOutputStream = new ByteArrayOutputStream(512);
+ dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+ }
+
+ /**
+ * Encodes an {@link EventMessage} to a byte array that can be decoded by {@link
+ * EventMessageDecoder}.
+ *
+ * @param eventMessage The event message to be encoded.
+ * @return The serialized byte array.
+ */
+ public byte[] encode(EventMessage eventMessage) {
+ byteArrayOutputStream.reset();
+ try {
+ writeNullTerminatedString(dataOutputStream, eventMessage.schemeIdUri);
+ String nonNullValue = eventMessage.value != null ? eventMessage.value : "";
+ writeNullTerminatedString(dataOutputStream, nonNullValue);
+ writeUnsignedInt(dataOutputStream, eventMessage.durationMs);
+ writeUnsignedInt(dataOutputStream, eventMessage.id);
+ dataOutputStream.write(eventMessage.messageData);
+ dataOutputStream.flush();
+ return byteArrayOutputStream.toByteArray();
+ } catch (IOException e) {
+ // Should never happen.
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static void writeNullTerminatedString(DataOutputStream dataOutputStream, String value)
+ throws IOException {
+ 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/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/package-info.java b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/package-info.java
new file mode 100644
index 0000000000..3e54b59a8c
--- /dev/null
+++ b/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/metadata/emsg/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+@NonNullApi
+package org.mozilla.thirdparty.com.google.android.exoplayer2.metadata.emsg;
+
+import org.mozilla.thirdparty.com.google.android.exoplayer2.util.NonNullApi;