Commit 9c3ac92a by andrewlewis Committed by Andrew Lewis

Allow extension of MatroskaExtractor

Subclasses can handle arbitrary elements.

PiperOrigin-RevId: 233057241
parent 36da5fb5
......@@ -52,7 +52,7 @@ import java.util.ArrayDeque;
private final ArrayDeque<MasterElement> masterElementsStack;
private final VarintReader varintReader;
private EbmlReaderOutput output;
private EbmlProcessor processor;
private @ElementState int elementState;
private int elementId;
private long elementContentSize;
......@@ -64,8 +64,8 @@ import java.util.ArrayDeque;
}
@Override
public void init(EbmlReaderOutput eventHandler) {
this.output = eventHandler;
public void init(EbmlProcessor processor) {
this.processor = processor;
}
@Override
......@@ -77,11 +77,11 @@ import java.util.ArrayDeque;
@Override
public boolean read(ExtractorInput input) throws IOException, InterruptedException {
Assertions.checkState(output != null);
Assertions.checkNotNull(processor);
while (true) {
if (!masterElementsStack.isEmpty()
&& input.getPosition() >= masterElementsStack.peek().elementEndPosition) {
output.endMasterElement(masterElementsStack.pop().elementId);
processor.endMasterElement(masterElementsStack.pop().elementId);
return true;
}
......@@ -103,42 +103,42 @@ import java.util.ArrayDeque;
elementState = ELEMENT_STATE_READ_CONTENT;
}
@EbmlReaderOutput.ElementType int type = output.getElementType(elementId);
@EbmlProcessor.ElementType int type = processor.getElementType(elementId);
switch (type) {
case EbmlReaderOutput.TYPE_MASTER:
case EbmlProcessor.ELEMENT_TYPE_MASTER:
long elementContentPosition = input.getPosition();
long elementEndPosition = elementContentPosition + elementContentSize;
masterElementsStack.push(new MasterElement(elementId, elementEndPosition));
output.startMasterElement(elementId, elementContentPosition, elementContentSize);
processor.startMasterElement(elementId, elementContentPosition, elementContentSize);
elementState = ELEMENT_STATE_READ_ID;
return true;
case EbmlReaderOutput.TYPE_UNSIGNED_INT:
case EbmlProcessor.ELEMENT_TYPE_UNSIGNED_INT:
if (elementContentSize > MAX_INTEGER_ELEMENT_SIZE_BYTES) {
throw new ParserException("Invalid integer size: " + elementContentSize);
}
output.integerElement(elementId, readInteger(input, (int) elementContentSize));
processor.integerElement(elementId, readInteger(input, (int) elementContentSize));
elementState = ELEMENT_STATE_READ_ID;
return true;
case EbmlReaderOutput.TYPE_FLOAT:
case EbmlProcessor.ELEMENT_TYPE_FLOAT:
if (elementContentSize != VALID_FLOAT32_ELEMENT_SIZE_BYTES
&& elementContentSize != VALID_FLOAT64_ELEMENT_SIZE_BYTES) {
throw new ParserException("Invalid float size: " + elementContentSize);
}
output.floatElement(elementId, readFloat(input, (int) elementContentSize));
processor.floatElement(elementId, readFloat(input, (int) elementContentSize));
elementState = ELEMENT_STATE_READ_ID;
return true;
case EbmlReaderOutput.TYPE_STRING:
case EbmlProcessor.ELEMENT_TYPE_STRING:
if (elementContentSize > Integer.MAX_VALUE) {
throw new ParserException("String element size: " + elementContentSize);
}
output.stringElement(elementId, readString(input, (int) elementContentSize));
processor.stringElement(elementId, readString(input, (int) elementContentSize));
elementState = ELEMENT_STATE_READ_ID;
return true;
case EbmlReaderOutput.TYPE_BINARY:
output.binaryElement(elementId, (int) elementContentSize, input);
case EbmlProcessor.ELEMENT_TYPE_BINARY:
processor.binaryElement(elementId, (int) elementContentSize, input);
elementState = ELEMENT_STATE_READ_ID;
return true;
case EbmlReaderOutput.TYPE_UNKNOWN:
case EbmlProcessor.ELEMENT_TYPE_UNKNOWN:
input.skipFully((int) elementContentSize);
elementState = ELEMENT_STATE_READ_ID;
break;
......@@ -167,7 +167,7 @@ import java.util.ArrayDeque;
int varintLength = VarintReader.parseUnsignedVarintLength(scratch[0]);
if (varintLength != C.LENGTH_UNSET && varintLength <= MAX_ID_BYTES) {
int potentialId = (int) VarintReader.assembleVarint(scratch, varintLength, false);
if (output.isLevel1Element(potentialId)) {
if (processor.isLevel1Element(potentialId)) {
input.skipFully(varintLength);
return potentialId;
}
......@@ -243,7 +243,7 @@ import java.util.ArrayDeque;
/**
* Used in {@link #masterElementsStack} to track when the current master element ends, so that
* {@link EbmlReaderOutput#endMasterElement(int)} can be called.
* {@link EbmlProcessor#endMasterElement(int)} can be called.
*/
private static final class MasterElement {
......
/*
* Copyright (C) 2016 The Android Open Source Project
* 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.
......@@ -23,41 +23,48 @@ import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Defines EBML element IDs/types and reacts to events.
*/
/* package */ interface EbmlReaderOutput {
/** Defines EBML element IDs/types and processes events. */
public interface EbmlProcessor {
/**
* EBML element types. One of {@link #TYPE_UNKNOWN}, {@link #TYPE_MASTER}, {@link
* #TYPE_UNSIGNED_INT}, {@link #TYPE_STRING}, {@link #TYPE_BINARY} or {@link #TYPE_FLOAT}.
* EBML element types. One of {@link #ELEMENT_TYPE_UNKNOWN}, {@link #ELEMENT_TYPE_MASTER}, {@link
* #ELEMENT_TYPE_UNSIGNED_INT}, {@link #ELEMENT_TYPE_STRING}, {@link #ELEMENT_TYPE_BINARY} or
* {@link #ELEMENT_TYPE_FLOAT}.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({TYPE_UNKNOWN, TYPE_MASTER, TYPE_UNSIGNED_INT, TYPE_STRING, TYPE_BINARY, TYPE_FLOAT})
@IntDef({
ELEMENT_TYPE_UNKNOWN,
ELEMENT_TYPE_MASTER,
ELEMENT_TYPE_UNSIGNED_INT,
ELEMENT_TYPE_STRING,
ELEMENT_TYPE_BINARY,
ELEMENT_TYPE_FLOAT
})
@interface ElementType {}
/** Type for unknown elements. */
int TYPE_UNKNOWN = 0;
int ELEMENT_TYPE_UNKNOWN = 0;
/** Type for elements that contain child elements. */
int TYPE_MASTER = 1;
int ELEMENT_TYPE_MASTER = 1;
/** Type for integer value elements of up to 8 bytes. */
int TYPE_UNSIGNED_INT = 2;
int ELEMENT_TYPE_UNSIGNED_INT = 2;
/** Type for string elements. */
int TYPE_STRING = 3;
int ELEMENT_TYPE_STRING = 3;
/** Type for binary elements. */
int TYPE_BINARY = 4;
int ELEMENT_TYPE_BINARY = 4;
/** Type for IEEE floating point value elements of either 4 or 8 bytes. */
int TYPE_FLOAT = 5;
int ELEMENT_TYPE_FLOAT = 5;
/**
* Maps an element ID to a corresponding type.
*
* <p>If {@link #TYPE_UNKNOWN} is returned then the element is skipped. Note that all children of
* a skipped element are also skipped.
* <p>If {@link #ELEMENT_TYPE_UNKNOWN} is returned then the element is skipped. Note that all
* children of a skipped element are also skipped.
*
* @param id The element ID to map.
* @return One of {@link #TYPE_UNKNOWN}, {@link #TYPE_MASTER}, {@link #TYPE_UNSIGNED_INT}, {@link
* #TYPE_STRING}, {@link #TYPE_BINARY} and {@link #TYPE_FLOAT}.
* @return One of {@link #ELEMENT_TYPE_UNKNOWN}, {@link #ELEMENT_TYPE_MASTER}, {@link
* #ELEMENT_TYPE_UNSIGNED_INT}, {@link #ELEMENT_TYPE_STRING}, {@link #ELEMENT_TYPE_BINARY} and
* {@link #ELEMENT_TYPE_FLOAT}.
*/
@ElementType
int getElementType(int id);
......
......@@ -20,20 +20,20 @@ import com.google.android.exoplayer2.extractor.ExtractorInput;
import java.io.IOException;
/**
* Event-driven EBML reader that delivers events to an {@link EbmlReaderOutput}.
* <p>
* EBML can be summarized as a binary XML format somewhat similar to Protocol Buffers. It was
* originally designed for the Matroska container format. More information about EBML and
* Matroska is available <a href="http://www.matroska.org/technical/specs/index.html">here</a>.
* Event-driven EBML reader that delivers events to an {@link EbmlProcessor}.
*
* <p>EBML can be summarized as a binary XML format somewhat similar to Protocol Buffers. It was
* originally designed for the Matroska container format. More information about EBML and Matroska
* is available <a href="http://www.matroska.org/technical/specs/index.html">here</a>.
*/
/* package */ interface EbmlReader {
/**
* Initializes the extractor with an {@link EbmlReaderOutput}.
* Initializes the extractor with an {@link EbmlProcessor}.
*
* @param output An {@link EbmlReaderOutput} to receive events.
* @param processor An {@link EbmlProcessor} to process events.
*/
void init(EbmlReaderOutput output);
void init(EbmlProcessor processor);
/**
* Resets the state of the reader.
......
......@@ -37,19 +37,19 @@ public class DefaultEbmlReaderTest {
@Test
public void testMasterElement() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x1A, 0x45, 0xDF, 0xA3, 0x84, 0x42, 0x85, 0x81, 0x01);
TestOutput expected = new TestOutput();
expected.startMasterElement(TestOutput.ID_EBML, 5, 4);
expected.integerElement(TestOutput.ID_DOC_TYPE_READ_VERSION, 1);
expected.endMasterElement(TestOutput.ID_EBML);
TestProcessor expected = new TestProcessor();
expected.startMasterElement(TestProcessor.ID_EBML, 5, 4);
expected.integerElement(TestProcessor.ID_DOC_TYPE_READ_VERSION, 1);
expected.endMasterElement(TestProcessor.ID_EBML);
assertEvents(input, expected.events);
}
@Test
public void testMasterElementEmpty() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x18, 0x53, 0x80, 0x67, 0x80);
TestOutput expected = new TestOutput();
expected.startMasterElement(TestOutput.ID_SEGMENT, 5, 0);
expected.endMasterElement(TestOutput.ID_SEGMENT);
TestProcessor expected = new TestProcessor();
expected.startMasterElement(TestProcessor.ID_SEGMENT, 5, 0);
expected.endMasterElement(TestProcessor.ID_SEGMENT);
assertEvents(input, expected.events);
}
......@@ -57,8 +57,8 @@ public class DefaultEbmlReaderTest {
public void testUnsignedIntegerElement() throws IOException, InterruptedException {
// 0xFE is chosen because for signed integers it should be interpreted as -2
ExtractorInput input = createTestInput(0x42, 0xF7, 0x81, 0xFE);
TestOutput expected = new TestOutput();
expected.integerElement(TestOutput.ID_EBML_READ_VERSION, 254);
TestProcessor expected = new TestProcessor();
expected.integerElement(TestProcessor.ID_EBML_READ_VERSION, 254);
assertEvents(input, expected.events);
}
......@@ -66,8 +66,8 @@ public class DefaultEbmlReaderTest {
public void testUnsignedIntegerElementLarge() throws IOException, InterruptedException {
ExtractorInput input =
createTestInput(0x42, 0xF7, 0x88, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
TestOutput expected = new TestOutput();
expected.integerElement(TestOutput.ID_EBML_READ_VERSION, Long.MAX_VALUE);
TestProcessor expected = new TestProcessor();
expected.integerElement(TestProcessor.ID_EBML_READ_VERSION, Long.MAX_VALUE);
assertEvents(input, expected.events);
}
......@@ -76,32 +76,32 @@ public class DefaultEbmlReaderTest {
throws IOException, InterruptedException {
ExtractorInput input =
createTestInput(0x42, 0xF7, 0x88, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
TestOutput expected = new TestOutput();
expected.integerElement(TestOutput.ID_EBML_READ_VERSION, -1);
TestProcessor expected = new TestProcessor();
expected.integerElement(TestProcessor.ID_EBML_READ_VERSION, -1);
assertEvents(input, expected.events);
}
@Test
public void testStringElement() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x31, 0x32, 0x33);
TestOutput expected = new TestOutput();
expected.stringElement(TestOutput.ID_DOC_TYPE, "Abc123");
TestProcessor expected = new TestProcessor();
expected.stringElement(TestProcessor.ID_DOC_TYPE, "Abc123");
assertEvents(input, expected.events);
}
@Test
public void testStringElementWithZeroPadding() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x00, 0x00, 0x00);
TestOutput expected = new TestOutput();
expected.stringElement(TestOutput.ID_DOC_TYPE, "Abc");
TestProcessor expected = new TestProcessor();
expected.stringElement(TestProcessor.ID_DOC_TYPE, "Abc");
assertEvents(input, expected.events);
}
@Test
public void testStringElementEmpty() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x80);
TestOutput expected = new TestOutput();
expected.stringElement(TestOutput.ID_DOC_TYPE, "");
TestProcessor expected = new TestProcessor();
expected.stringElement(TestProcessor.ID_DOC_TYPE, "");
assertEvents(input, expected.events);
}
......@@ -109,8 +109,8 @@ public class DefaultEbmlReaderTest {
public void testFloatElementFourBytes() throws IOException, InterruptedException {
ExtractorInput input =
createTestInput(0x44, 0x89, 0x84, 0x3F, 0x80, 0x00, 0x00);
TestOutput expected = new TestOutput();
expected.floatElement(TestOutput.ID_DURATION, 1.0);
TestProcessor expected = new TestProcessor();
expected.floatElement(TestProcessor.ID_DURATION, 1.0);
assertEvents(input, expected.events);
}
......@@ -118,8 +118,8 @@ public class DefaultEbmlReaderTest {
public void testFloatElementEightBytes() throws IOException, InterruptedException {
ExtractorInput input =
createTestInput(0x44, 0x89, 0x88, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
TestOutput expected = new TestOutput();
expected.floatElement(TestOutput.ID_DURATION, -2.0);
TestProcessor expected = new TestProcessor();
expected.floatElement(TestProcessor.ID_DURATION, -2.0);
assertEvents(input, expected.events);
}
......@@ -127,8 +127,10 @@ public class DefaultEbmlReaderTest {
public void testBinaryElement() throws IOException, InterruptedException {
ExtractorInput input =
createTestInput(0xA3, 0x88, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);
TestOutput expected = new TestOutput();
expected.binaryElement(TestOutput.ID_SIMPLE_BLOCK, 8,
TestProcessor expected = new TestProcessor();
expected.binaryElement(
TestProcessor.ID_SIMPLE_BLOCK,
8,
createTestInput(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08));
assertEvents(input, expected.events);
}
......@@ -136,7 +138,7 @@ public class DefaultEbmlReaderTest {
private static void assertEvents(ExtractorInput input, List<String> expectedEvents)
throws IOException, InterruptedException {
DefaultEbmlReader reader = new DefaultEbmlReader();
TestOutput output = new TestOutput();
TestProcessor output = new TestProcessor();
reader.init(output);
// We expect the number of successful reads to equal the number of expected events.
......@@ -164,10 +166,8 @@ public class DefaultEbmlReaderTest {
.build();
}
/**
* An {@link EbmlReaderOutput} that records each event callback.
*/
private static final class TestOutput implements EbmlReaderOutput {
/** An {@link EbmlProcessor} that records each event callback. */
private static final class TestProcessor implements EbmlProcessor {
// Element IDs
private static final int ID_EBML = 0x1A45DFA3;
......@@ -182,22 +182,23 @@ public class DefaultEbmlReaderTest {
private final List<String> events = new ArrayList<>();
@Override
public @ElementType int getElementType(int id) {
@EbmlProcessor.ElementType
public int getElementType(int id) {
switch (id) {
case ID_EBML:
case ID_SEGMENT:
return TYPE_MASTER;
return EbmlProcessor.ELEMENT_TYPE_MASTER;
case ID_EBML_READ_VERSION:
case ID_DOC_TYPE_READ_VERSION:
return TYPE_UNSIGNED_INT;
return EbmlProcessor.ELEMENT_TYPE_UNSIGNED_INT;
case ID_DOC_TYPE:
return TYPE_STRING;
return EbmlProcessor.ELEMENT_TYPE_STRING;
case ID_SIMPLE_BLOCK:
return TYPE_BINARY;
return EbmlProcessor.ELEMENT_TYPE_BINARY;
case ID_DURATION:
return TYPE_FLOAT;
return EbmlProcessor.ELEMENT_TYPE_FLOAT;
default:
return TYPE_UNKNOWN;
return EbmlProcessor.ELEMENT_TYPE_UNKNOWN;
}
}
......@@ -219,12 +220,12 @@ public class DefaultEbmlReaderTest {
@Override
public void integerElement(int id, long value) {
events.add(formatEvent(id, "integer=" + String.valueOf(value)));
events.add(formatEvent(id, "integer=" + value));
}
@Override
public void floatElement(int id, double value) {
events.add(formatEvent(id, "float=" + String.valueOf(value)));
events.add(formatEvent(id, "float=" + value));
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment