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; ...@@ -52,7 +52,7 @@ import java.util.ArrayDeque;
private final ArrayDeque<MasterElement> masterElementsStack; private final ArrayDeque<MasterElement> masterElementsStack;
private final VarintReader varintReader; private final VarintReader varintReader;
private EbmlReaderOutput output; private EbmlProcessor processor;
private @ElementState int elementState; private @ElementState int elementState;
private int elementId; private int elementId;
private long elementContentSize; private long elementContentSize;
...@@ -64,8 +64,8 @@ import java.util.ArrayDeque; ...@@ -64,8 +64,8 @@ import java.util.ArrayDeque;
} }
@Override @Override
public void init(EbmlReaderOutput eventHandler) { public void init(EbmlProcessor processor) {
this.output = eventHandler; this.processor = processor;
} }
@Override @Override
...@@ -77,11 +77,11 @@ import java.util.ArrayDeque; ...@@ -77,11 +77,11 @@ import java.util.ArrayDeque;
@Override @Override
public boolean read(ExtractorInput input) throws IOException, InterruptedException { public boolean read(ExtractorInput input) throws IOException, InterruptedException {
Assertions.checkState(output != null); Assertions.checkNotNull(processor);
while (true) { while (true) {
if (!masterElementsStack.isEmpty() if (!masterElementsStack.isEmpty()
&& input.getPosition() >= masterElementsStack.peek().elementEndPosition) { && input.getPosition() >= masterElementsStack.peek().elementEndPosition) {
output.endMasterElement(masterElementsStack.pop().elementId); processor.endMasterElement(masterElementsStack.pop().elementId);
return true; return true;
} }
...@@ -103,42 +103,42 @@ import java.util.ArrayDeque; ...@@ -103,42 +103,42 @@ import java.util.ArrayDeque;
elementState = ELEMENT_STATE_READ_CONTENT; elementState = ELEMENT_STATE_READ_CONTENT;
} }
@EbmlReaderOutput.ElementType int type = output.getElementType(elementId); @EbmlProcessor.ElementType int type = processor.getElementType(elementId);
switch (type) { switch (type) {
case EbmlReaderOutput.TYPE_MASTER: case EbmlProcessor.ELEMENT_TYPE_MASTER:
long elementContentPosition = input.getPosition(); long elementContentPosition = input.getPosition();
long elementEndPosition = elementContentPosition + elementContentSize; long elementEndPosition = elementContentPosition + elementContentSize;
masterElementsStack.push(new MasterElement(elementId, elementEndPosition)); masterElementsStack.push(new MasterElement(elementId, elementEndPosition));
output.startMasterElement(elementId, elementContentPosition, elementContentSize); processor.startMasterElement(elementId, elementContentPosition, elementContentSize);
elementState = ELEMENT_STATE_READ_ID; elementState = ELEMENT_STATE_READ_ID;
return true; return true;
case EbmlReaderOutput.TYPE_UNSIGNED_INT: case EbmlProcessor.ELEMENT_TYPE_UNSIGNED_INT:
if (elementContentSize > MAX_INTEGER_ELEMENT_SIZE_BYTES) { if (elementContentSize > MAX_INTEGER_ELEMENT_SIZE_BYTES) {
throw new ParserException("Invalid integer size: " + elementContentSize); 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; elementState = ELEMENT_STATE_READ_ID;
return true; return true;
case EbmlReaderOutput.TYPE_FLOAT: case EbmlProcessor.ELEMENT_TYPE_FLOAT:
if (elementContentSize != VALID_FLOAT32_ELEMENT_SIZE_BYTES if (elementContentSize != VALID_FLOAT32_ELEMENT_SIZE_BYTES
&& elementContentSize != VALID_FLOAT64_ELEMENT_SIZE_BYTES) { && elementContentSize != VALID_FLOAT64_ELEMENT_SIZE_BYTES) {
throw new ParserException("Invalid float size: " + elementContentSize); 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; elementState = ELEMENT_STATE_READ_ID;
return true; return true;
case EbmlReaderOutput.TYPE_STRING: case EbmlProcessor.ELEMENT_TYPE_STRING:
if (elementContentSize > Integer.MAX_VALUE) { if (elementContentSize > Integer.MAX_VALUE) {
throw new ParserException("String element size: " + elementContentSize); 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; elementState = ELEMENT_STATE_READ_ID;
return true; return true;
case EbmlReaderOutput.TYPE_BINARY: case EbmlProcessor.ELEMENT_TYPE_BINARY:
output.binaryElement(elementId, (int) elementContentSize, input); processor.binaryElement(elementId, (int) elementContentSize, input);
elementState = ELEMENT_STATE_READ_ID; elementState = ELEMENT_STATE_READ_ID;
return true; return true;
case EbmlReaderOutput.TYPE_UNKNOWN: case EbmlProcessor.ELEMENT_TYPE_UNKNOWN:
input.skipFully((int) elementContentSize); input.skipFully((int) elementContentSize);
elementState = ELEMENT_STATE_READ_ID; elementState = ELEMENT_STATE_READ_ID;
break; break;
...@@ -167,7 +167,7 @@ import java.util.ArrayDeque; ...@@ -167,7 +167,7 @@ import java.util.ArrayDeque;
int varintLength = VarintReader.parseUnsignedVarintLength(scratch[0]); int varintLength = VarintReader.parseUnsignedVarintLength(scratch[0]);
if (varintLength != C.LENGTH_UNSET && varintLength <= MAX_ID_BYTES) { if (varintLength != C.LENGTH_UNSET && varintLength <= MAX_ID_BYTES) {
int potentialId = (int) VarintReader.assembleVarint(scratch, varintLength, false); int potentialId = (int) VarintReader.assembleVarint(scratch, varintLength, false);
if (output.isLevel1Element(potentialId)) { if (processor.isLevel1Element(potentialId)) {
input.skipFully(varintLength); input.skipFully(varintLength);
return potentialId; return potentialId;
} }
...@@ -243,7 +243,7 @@ import java.util.ArrayDeque; ...@@ -243,7 +243,7 @@ import java.util.ArrayDeque;
/** /**
* Used in {@link #masterElementsStack} to track when the current master element ends, so that * 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 { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,41 +23,48 @@ import java.lang.annotation.Documented; ...@@ -23,41 +23,48 @@ import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
/** /** Defines EBML element IDs/types and processes events. */
* Defines EBML element IDs/types and reacts to events. public interface EbmlProcessor {
*/
/* package */ interface EbmlReaderOutput {
/** /**
* EBML element types. One of {@link #TYPE_UNKNOWN}, {@link #TYPE_MASTER}, {@link * EBML element types. One of {@link #ELEMENT_TYPE_UNKNOWN}, {@link #ELEMENT_TYPE_MASTER}, {@link
* #TYPE_UNSIGNED_INT}, {@link #TYPE_STRING}, {@link #TYPE_BINARY} or {@link #TYPE_FLOAT}. * #ELEMENT_TYPE_UNSIGNED_INT}, {@link #ELEMENT_TYPE_STRING}, {@link #ELEMENT_TYPE_BINARY} or
* {@link #ELEMENT_TYPE_FLOAT}.
*/ */
@Documented @Documented
@Retention(RetentionPolicy.SOURCE) @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 {} @interface ElementType {}
/** Type for unknown elements. */ /** Type for unknown elements. */
int TYPE_UNKNOWN = 0; int ELEMENT_TYPE_UNKNOWN = 0;
/** Type for elements that contain child elements. */ /** 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. */ /** 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. */ /** Type for string elements. */
int TYPE_STRING = 3; int ELEMENT_TYPE_STRING = 3;
/** Type for binary elements. */ /** 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. */ /** 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. * 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 * <p>If {@link #ELEMENT_TYPE_UNKNOWN} is returned then the element is skipped. Note that all
* a skipped element are also skipped. * children of a skipped element are also skipped.
* *
* @param id The element ID to map. * @param id The element ID to map.
* @return One of {@link #TYPE_UNKNOWN}, {@link #TYPE_MASTER}, {@link #TYPE_UNSIGNED_INT}, {@link * @return One of {@link #ELEMENT_TYPE_UNKNOWN}, {@link #ELEMENT_TYPE_MASTER}, {@link
* #TYPE_STRING}, {@link #TYPE_BINARY} and {@link #TYPE_FLOAT}. * #ELEMENT_TYPE_UNSIGNED_INT}, {@link #ELEMENT_TYPE_STRING}, {@link #ELEMENT_TYPE_BINARY} and
* {@link #ELEMENT_TYPE_FLOAT}.
*/ */
@ElementType @ElementType
int getElementType(int id); int getElementType(int id);
......
...@@ -20,20 +20,20 @@ import com.google.android.exoplayer2.extractor.ExtractorInput; ...@@ -20,20 +20,20 @@ import com.google.android.exoplayer2.extractor.ExtractorInput;
import java.io.IOException; import java.io.IOException;
/** /**
* Event-driven EBML reader that delivers events to an {@link EbmlReaderOutput}. * 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 * <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 * originally designed for the Matroska container format. More information about EBML and Matroska
* Matroska is available <a href="http://www.matroska.org/technical/specs/index.html">here</a>. * is available <a href="http://www.matroska.org/technical/specs/index.html">here</a>.
*/ */
/* package */ interface EbmlReader { /* 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. * Resets the state of the reader.
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.extractor.mkv; package com.google.android.exoplayer2.extractor.mkv;
import android.support.annotation.CallSuper;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Pair; import android.util.Pair;
...@@ -57,10 +58,8 @@ import java.util.List; ...@@ -57,10 +58,8 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.UUID; import java.util.UUID;
/** /** Extracts data from the Matroska and WebM container formats. */
* Extracts data from the Matroska and WebM container formats. public class MatroskaExtractor implements Extractor {
*/
public final class MatroskaExtractor implements Extractor {
/** Factory for {@link MatroskaExtractor} instances. */ /** Factory for {@link MatroskaExtractor} instances. */
public static final ExtractorsFactory FACTORY = () -> new Extractor[] {new MatroskaExtractor()}; public static final ExtractorsFactory FACTORY = () -> new Extractor[] {new MatroskaExtractor()};
...@@ -387,7 +386,7 @@ public final class MatroskaExtractor implements Extractor { ...@@ -387,7 +386,7 @@ public final class MatroskaExtractor implements Extractor {
/* package */ MatroskaExtractor(EbmlReader reader, @Flags int flags) { /* package */ MatroskaExtractor(EbmlReader reader, @Flags int flags) {
this.reader = reader; this.reader = reader;
this.reader.init(new InnerEbmlReaderOutput()); this.reader.init(new InnerEbmlProcessor());
seekForCuesEnabled = (flags & FLAG_DISABLE_SEEK_FOR_CUES) == 0; seekForCuesEnabled = (flags & FLAG_DISABLE_SEEK_FOR_CUES) == 0;
varintReader = new VarintReader(); varintReader = new VarintReader();
tracks = new SparseArray<>(); tracks = new SparseArray<>();
...@@ -403,17 +402,17 @@ public final class MatroskaExtractor implements Extractor { ...@@ -403,17 +402,17 @@ public final class MatroskaExtractor implements Extractor {
} }
@Override @Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException { public final boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
return new Sniffer().sniff(input); return new Sniffer().sniff(input);
} }
@Override @Override
public void init(ExtractorOutput output) { public final void init(ExtractorOutput output) {
extractorOutput = output; extractorOutput = output;
} }
@Override @Override
public void seek(long position, long timeUs) { public final void seek(long position, long timeUs) {
clusterTimecodeUs = C.TIME_UNSET; clusterTimecodeUs = C.TIME_UNSET;
blockState = BLOCK_STATE_START; blockState = BLOCK_STATE_START;
reader.reset(); reader.reset();
...@@ -425,13 +424,13 @@ public final class MatroskaExtractor implements Extractor { ...@@ -425,13 +424,13 @@ public final class MatroskaExtractor implements Extractor {
} }
@Override @Override
public void release() { public final void release() {
// Do nothing // Do nothing
} }
@Override @Override
public int read(ExtractorInput input, PositionHolder seekPosition) throws IOException, public final int read(ExtractorInput input, PositionHolder seekPosition)
InterruptedException { throws IOException, InterruptedException {
sampleRead = false; sampleRead = false;
boolean continueReading = true; boolean continueReading = true;
while (continueReading && !sampleRead) { while (continueReading && !sampleRead) {
...@@ -449,7 +448,125 @@ public final class MatroskaExtractor implements Extractor { ...@@ -449,7 +448,125 @@ public final class MatroskaExtractor implements Extractor {
return Extractor.RESULT_CONTINUE; return Extractor.RESULT_CONTINUE;
} }
/* package */ void startMasterElement(int id, long contentPosition, long contentSize) /**
* Maps an element ID to a corresponding type.
*
* @see EbmlProcessor#getElementType(int)
*/
@CallSuper
@EbmlProcessor.ElementType
protected int getElementType(int id) {
switch (id) {
case ID_EBML:
case ID_SEGMENT:
case ID_SEEK_HEAD:
case ID_SEEK:
case ID_INFO:
case ID_CLUSTER:
case ID_TRACKS:
case ID_TRACK_ENTRY:
case ID_AUDIO:
case ID_VIDEO:
case ID_CONTENT_ENCODINGS:
case ID_CONTENT_ENCODING:
case ID_CONTENT_COMPRESSION:
case ID_CONTENT_ENCRYPTION:
case ID_CONTENT_ENCRYPTION_AES_SETTINGS:
case ID_CUES:
case ID_CUE_POINT:
case ID_CUE_TRACK_POSITIONS:
case ID_BLOCK_GROUP:
case ID_PROJECTION:
case ID_COLOUR:
case ID_MASTERING_METADATA:
return EbmlProcessor.ELEMENT_TYPE_MASTER;
case ID_EBML_READ_VERSION:
case ID_DOC_TYPE_READ_VERSION:
case ID_SEEK_POSITION:
case ID_TIMECODE_SCALE:
case ID_TIME_CODE:
case ID_BLOCK_DURATION:
case ID_PIXEL_WIDTH:
case ID_PIXEL_HEIGHT:
case ID_DISPLAY_WIDTH:
case ID_DISPLAY_HEIGHT:
case ID_DISPLAY_UNIT:
case ID_TRACK_NUMBER:
case ID_TRACK_TYPE:
case ID_FLAG_DEFAULT:
case ID_FLAG_FORCED:
case ID_DEFAULT_DURATION:
case ID_CODEC_DELAY:
case ID_SEEK_PRE_ROLL:
case ID_CHANNELS:
case ID_AUDIO_BIT_DEPTH:
case ID_CONTENT_ENCODING_ORDER:
case ID_CONTENT_ENCODING_SCOPE:
case ID_CONTENT_COMPRESSION_ALGORITHM:
case ID_CONTENT_ENCRYPTION_ALGORITHM:
case ID_CONTENT_ENCRYPTION_AES_SETTINGS_CIPHER_MODE:
case ID_CUE_TIME:
case ID_CUE_CLUSTER_POSITION:
case ID_REFERENCE_BLOCK:
case ID_STEREO_MODE:
case ID_COLOUR_RANGE:
case ID_COLOUR_TRANSFER:
case ID_COLOUR_PRIMARIES:
case ID_MAX_CLL:
case ID_MAX_FALL:
case ID_PROJECTION_TYPE:
return EbmlProcessor.ELEMENT_TYPE_UNSIGNED_INT;
case ID_DOC_TYPE:
case ID_NAME:
case ID_CODEC_ID:
case ID_LANGUAGE:
return EbmlProcessor.ELEMENT_TYPE_STRING;
case ID_SEEK_ID:
case ID_CONTENT_COMPRESSION_SETTINGS:
case ID_CONTENT_ENCRYPTION_KEY_ID:
case ID_SIMPLE_BLOCK:
case ID_BLOCK:
case ID_CODEC_PRIVATE:
case ID_PROJECTION_PRIVATE:
return EbmlProcessor.ELEMENT_TYPE_BINARY;
case ID_DURATION:
case ID_SAMPLING_FREQUENCY:
case ID_PRIMARY_R_CHROMATICITY_X:
case ID_PRIMARY_R_CHROMATICITY_Y:
case ID_PRIMARY_G_CHROMATICITY_X:
case ID_PRIMARY_G_CHROMATICITY_Y:
case ID_PRIMARY_B_CHROMATICITY_X:
case ID_PRIMARY_B_CHROMATICITY_Y:
case ID_WHITE_POINT_CHROMATICITY_X:
case ID_WHITE_POINT_CHROMATICITY_Y:
case ID_LUMNINANCE_MAX:
case ID_LUMNINANCE_MIN:
case ID_PROJECTION_POSE_YAW:
case ID_PROJECTION_POSE_PITCH:
case ID_PROJECTION_POSE_ROLL:
return EbmlProcessor.ELEMENT_TYPE_FLOAT;
default:
return EbmlProcessor.ELEMENT_TYPE_UNKNOWN;
}
}
/**
* Checks if the given id is that of a level 1 element.
*
* @see EbmlProcessor#isLevel1Element(int)
*/
@CallSuper
protected boolean isLevel1Element(int id) {
return id == ID_SEGMENT_INFO || id == ID_CLUSTER || id == ID_CUES || id == ID_TRACKS;
}
/**
* Called when the start of a master element is encountered.
*
* @see EbmlProcessor#startMasterElement(int, long, long)
*/
@CallSuper
protected void startMasterElement(int id, long contentPosition, long contentSize)
throws ParserException { throws ParserException {
switch (id) { switch (id) {
case ID_SEGMENT: case ID_SEGMENT:
...@@ -505,7 +622,13 @@ public final class MatroskaExtractor implements Extractor { ...@@ -505,7 +622,13 @@ public final class MatroskaExtractor implements Extractor {
} }
} }
/* package */ void endMasterElement(int id) throws ParserException { /**
* Called when the end of a master element is encountered.
*
* @see EbmlProcessor#endMasterElement(int)
*/
@CallSuper
protected void endMasterElement(int id) throws ParserException {
switch (id) { switch (id) {
case ID_SEGMENT_INFO: case ID_SEGMENT_INFO:
if (timecodeScale == C.TIME_UNSET) { if (timecodeScale == C.TIME_UNSET) {
...@@ -576,7 +699,13 @@ public final class MatroskaExtractor implements Extractor { ...@@ -576,7 +699,13 @@ public final class MatroskaExtractor implements Extractor {
} }
} }
/* package */ void integerElement(int id, long value) throws ParserException { /**
* Called when an integer element is encountered.
*
* @see EbmlProcessor#integerElement(int, long)
*/
@CallSuper
protected void integerElement(int id, long value) throws ParserException {
switch (id) { switch (id) {
case ID_EBML_READ_VERSION: case ID_EBML_READ_VERSION:
// Validate that EBMLReadVersion is supported. This extractor only supports v1. // Validate that EBMLReadVersion is supported. This extractor only supports v1.
...@@ -787,7 +916,13 @@ public final class MatroskaExtractor implements Extractor { ...@@ -787,7 +916,13 @@ public final class MatroskaExtractor implements Extractor {
} }
} }
/* package */ void floatElement(int id, double value) { /**
* Called when a float element is encountered.
*
* @see EbmlProcessor#floatElement(int, double)
*/
@CallSuper
protected void floatElement(int id, double value) throws ParserException {
switch (id) { switch (id) {
case ID_DURATION: case ID_DURATION:
durationTimecode = (long) value; durationTimecode = (long) value;
...@@ -839,7 +974,13 @@ public final class MatroskaExtractor implements Extractor { ...@@ -839,7 +974,13 @@ public final class MatroskaExtractor implements Extractor {
} }
} }
/* package */ void stringElement(int id, String value) throws ParserException { /**
* Called when a string element is encountered.
*
* @see EbmlProcessor#stringElement(int, String)
*/
@CallSuper
protected void stringElement(int id, String value) throws ParserException {
switch (id) { switch (id) {
case ID_DOC_TYPE: case ID_DOC_TYPE:
// Validate that DocType is supported. // Validate that DocType is supported.
...@@ -861,7 +1002,13 @@ public final class MatroskaExtractor implements Extractor { ...@@ -861,7 +1002,13 @@ public final class MatroskaExtractor implements Extractor {
} }
} }
/* package */ void binaryElement(int id, int contentSize, ExtractorInput input) /**
* Called when a binary element is encountered.
*
* @see EbmlProcessor#binaryElement(int, int, ExtractorInput)
*/
@CallSuper
protected void binaryElement(int id, int contentSize, ExtractorInput input)
throws IOException, InterruptedException { throws IOException, InterruptedException {
switch (id) { switch (id) {
case ID_SEEK_ID: case ID_SEEK_ID:
...@@ -1431,110 +1578,18 @@ public final class MatroskaExtractor implements Extractor { ...@@ -1431,110 +1578,18 @@ public final class MatroskaExtractor implements Extractor {
} }
} }
/** /** Passes events through to the outer {@link MatroskaExtractor}. */
* Passes events through to the outer {@link MatroskaExtractor}. private final class InnerEbmlProcessor implements EbmlProcessor {
*/
private final class InnerEbmlReaderOutput implements EbmlReaderOutput {
@Override @Override
@ElementType
public int getElementType(int id) { public int getElementType(int id) {
switch (id) { return MatroskaExtractor.this.getElementType(id);
case ID_EBML:
case ID_SEGMENT:
case ID_SEEK_HEAD:
case ID_SEEK:
case ID_INFO:
case ID_CLUSTER:
case ID_TRACKS:
case ID_TRACK_ENTRY:
case ID_AUDIO:
case ID_VIDEO:
case ID_CONTENT_ENCODINGS:
case ID_CONTENT_ENCODING:
case ID_CONTENT_COMPRESSION:
case ID_CONTENT_ENCRYPTION:
case ID_CONTENT_ENCRYPTION_AES_SETTINGS:
case ID_CUES:
case ID_CUE_POINT:
case ID_CUE_TRACK_POSITIONS:
case ID_BLOCK_GROUP:
case ID_PROJECTION:
case ID_COLOUR:
case ID_MASTERING_METADATA:
return TYPE_MASTER;
case ID_EBML_READ_VERSION:
case ID_DOC_TYPE_READ_VERSION:
case ID_SEEK_POSITION:
case ID_TIMECODE_SCALE:
case ID_TIME_CODE:
case ID_BLOCK_DURATION:
case ID_PIXEL_WIDTH:
case ID_PIXEL_HEIGHT:
case ID_DISPLAY_WIDTH:
case ID_DISPLAY_HEIGHT:
case ID_DISPLAY_UNIT:
case ID_TRACK_NUMBER:
case ID_TRACK_TYPE:
case ID_FLAG_DEFAULT:
case ID_FLAG_FORCED:
case ID_DEFAULT_DURATION:
case ID_CODEC_DELAY:
case ID_SEEK_PRE_ROLL:
case ID_CHANNELS:
case ID_AUDIO_BIT_DEPTH:
case ID_CONTENT_ENCODING_ORDER:
case ID_CONTENT_ENCODING_SCOPE:
case ID_CONTENT_COMPRESSION_ALGORITHM:
case ID_CONTENT_ENCRYPTION_ALGORITHM:
case ID_CONTENT_ENCRYPTION_AES_SETTINGS_CIPHER_MODE:
case ID_CUE_TIME:
case ID_CUE_CLUSTER_POSITION:
case ID_REFERENCE_BLOCK:
case ID_STEREO_MODE:
case ID_COLOUR_RANGE:
case ID_COLOUR_TRANSFER:
case ID_COLOUR_PRIMARIES:
case ID_MAX_CLL:
case ID_MAX_FALL:
case ID_PROJECTION_TYPE:
return TYPE_UNSIGNED_INT;
case ID_DOC_TYPE:
case ID_NAME:
case ID_CODEC_ID:
case ID_LANGUAGE:
return TYPE_STRING;
case ID_SEEK_ID:
case ID_CONTENT_COMPRESSION_SETTINGS:
case ID_CONTENT_ENCRYPTION_KEY_ID:
case ID_SIMPLE_BLOCK:
case ID_BLOCK:
case ID_CODEC_PRIVATE:
case ID_PROJECTION_PRIVATE:
return TYPE_BINARY;
case ID_DURATION:
case ID_SAMPLING_FREQUENCY:
case ID_PRIMARY_R_CHROMATICITY_X:
case ID_PRIMARY_R_CHROMATICITY_Y:
case ID_PRIMARY_G_CHROMATICITY_X:
case ID_PRIMARY_G_CHROMATICITY_Y:
case ID_PRIMARY_B_CHROMATICITY_X:
case ID_PRIMARY_B_CHROMATICITY_Y:
case ID_WHITE_POINT_CHROMATICITY_X:
case ID_WHITE_POINT_CHROMATICITY_Y:
case ID_LUMNINANCE_MAX:
case ID_LUMNINANCE_MIN:
case ID_PROJECTION_POSE_YAW:
case ID_PROJECTION_POSE_PITCH:
case ID_PROJECTION_POSE_ROLL:
return TYPE_FLOAT;
default:
return TYPE_UNKNOWN;
}
} }
@Override @Override
public boolean isLevel1Element(int id) { public boolean isLevel1Element(int id) {
return id == ID_SEGMENT_INFO || id == ID_CLUSTER || id == ID_CUES || id == ID_TRACKS; return MatroskaExtractor.this.isLevel1Element(id);
} }
@Override @Override
......
...@@ -37,19 +37,19 @@ public class DefaultEbmlReaderTest { ...@@ -37,19 +37,19 @@ public class DefaultEbmlReaderTest {
@Test @Test
public void testMasterElement() throws IOException, InterruptedException { public void testMasterElement() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x1A, 0x45, 0xDF, 0xA3, 0x84, 0x42, 0x85, 0x81, 0x01); ExtractorInput input = createTestInput(0x1A, 0x45, 0xDF, 0xA3, 0x84, 0x42, 0x85, 0x81, 0x01);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.startMasterElement(TestOutput.ID_EBML, 5, 4); expected.startMasterElement(TestProcessor.ID_EBML, 5, 4);
expected.integerElement(TestOutput.ID_DOC_TYPE_READ_VERSION, 1); expected.integerElement(TestProcessor.ID_DOC_TYPE_READ_VERSION, 1);
expected.endMasterElement(TestOutput.ID_EBML); expected.endMasterElement(TestProcessor.ID_EBML);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
@Test @Test
public void testMasterElementEmpty() throws IOException, InterruptedException { public void testMasterElementEmpty() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x18, 0x53, 0x80, 0x67, 0x80); ExtractorInput input = createTestInput(0x18, 0x53, 0x80, 0x67, 0x80);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.startMasterElement(TestOutput.ID_SEGMENT, 5, 0); expected.startMasterElement(TestProcessor.ID_SEGMENT, 5, 0);
expected.endMasterElement(TestOutput.ID_SEGMENT); expected.endMasterElement(TestProcessor.ID_SEGMENT);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -57,8 +57,8 @@ public class DefaultEbmlReaderTest { ...@@ -57,8 +57,8 @@ public class DefaultEbmlReaderTest {
public void testUnsignedIntegerElement() throws IOException, InterruptedException { public void testUnsignedIntegerElement() throws IOException, InterruptedException {
// 0xFE is chosen because for signed integers it should be interpreted as -2 // 0xFE is chosen because for signed integers it should be interpreted as -2
ExtractorInput input = createTestInput(0x42, 0xF7, 0x81, 0xFE); ExtractorInput input = createTestInput(0x42, 0xF7, 0x81, 0xFE);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.integerElement(TestOutput.ID_EBML_READ_VERSION, 254); expected.integerElement(TestProcessor.ID_EBML_READ_VERSION, 254);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -66,8 +66,8 @@ public class DefaultEbmlReaderTest { ...@@ -66,8 +66,8 @@ public class DefaultEbmlReaderTest {
public void testUnsignedIntegerElementLarge() throws IOException, InterruptedException { public void testUnsignedIntegerElementLarge() throws IOException, InterruptedException {
ExtractorInput input = ExtractorInput input =
createTestInput(0x42, 0xF7, 0x88, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); createTestInput(0x42, 0xF7, 0x88, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.integerElement(TestOutput.ID_EBML_READ_VERSION, Long.MAX_VALUE); expected.integerElement(TestProcessor.ID_EBML_READ_VERSION, Long.MAX_VALUE);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -76,32 +76,32 @@ public class DefaultEbmlReaderTest { ...@@ -76,32 +76,32 @@ public class DefaultEbmlReaderTest {
throws IOException, InterruptedException { throws IOException, InterruptedException {
ExtractorInput input = ExtractorInput input =
createTestInput(0x42, 0xF7, 0x88, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); createTestInput(0x42, 0xF7, 0x88, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.integerElement(TestOutput.ID_EBML_READ_VERSION, -1); expected.integerElement(TestProcessor.ID_EBML_READ_VERSION, -1);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
@Test @Test
public void testStringElement() throws IOException, InterruptedException { public void testStringElement() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x31, 0x32, 0x33); ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x31, 0x32, 0x33);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.stringElement(TestOutput.ID_DOC_TYPE, "Abc123"); expected.stringElement(TestProcessor.ID_DOC_TYPE, "Abc123");
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
@Test @Test
public void testStringElementWithZeroPadding() throws IOException, InterruptedException { public void testStringElementWithZeroPadding() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x00, 0x00, 0x00); ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x00, 0x00, 0x00);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.stringElement(TestOutput.ID_DOC_TYPE, "Abc"); expected.stringElement(TestProcessor.ID_DOC_TYPE, "Abc");
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
@Test @Test
public void testStringElementEmpty() throws IOException, InterruptedException { public void testStringElementEmpty() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x80); ExtractorInput input = createTestInput(0x42, 0x82, 0x80);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.stringElement(TestOutput.ID_DOC_TYPE, ""); expected.stringElement(TestProcessor.ID_DOC_TYPE, "");
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -109,8 +109,8 @@ public class DefaultEbmlReaderTest { ...@@ -109,8 +109,8 @@ public class DefaultEbmlReaderTest {
public void testFloatElementFourBytes() throws IOException, InterruptedException { public void testFloatElementFourBytes() throws IOException, InterruptedException {
ExtractorInput input = ExtractorInput input =
createTestInput(0x44, 0x89, 0x84, 0x3F, 0x80, 0x00, 0x00); createTestInput(0x44, 0x89, 0x84, 0x3F, 0x80, 0x00, 0x00);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.floatElement(TestOutput.ID_DURATION, 1.0); expected.floatElement(TestProcessor.ID_DURATION, 1.0);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -118,8 +118,8 @@ public class DefaultEbmlReaderTest { ...@@ -118,8 +118,8 @@ public class DefaultEbmlReaderTest {
public void testFloatElementEightBytes() throws IOException, InterruptedException { public void testFloatElementEightBytes() throws IOException, InterruptedException {
ExtractorInput input = ExtractorInput input =
createTestInput(0x44, 0x89, 0x88, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); createTestInput(0x44, 0x89, 0x88, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.floatElement(TestOutput.ID_DURATION, -2.0); expected.floatElement(TestProcessor.ID_DURATION, -2.0);
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -127,8 +127,10 @@ public class DefaultEbmlReaderTest { ...@@ -127,8 +127,10 @@ public class DefaultEbmlReaderTest {
public void testBinaryElement() throws IOException, InterruptedException { public void testBinaryElement() throws IOException, InterruptedException {
ExtractorInput input = ExtractorInput input =
createTestInput(0xA3, 0x88, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08); createTestInput(0xA3, 0x88, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);
TestOutput expected = new TestOutput(); TestProcessor expected = new TestProcessor();
expected.binaryElement(TestOutput.ID_SIMPLE_BLOCK, 8, expected.binaryElement(
TestProcessor.ID_SIMPLE_BLOCK,
8,
createTestInput(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)); createTestInput(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08));
assertEvents(input, expected.events); assertEvents(input, expected.events);
} }
...@@ -136,7 +138,7 @@ public class DefaultEbmlReaderTest { ...@@ -136,7 +138,7 @@ public class DefaultEbmlReaderTest {
private static void assertEvents(ExtractorInput input, List<String> expectedEvents) private static void assertEvents(ExtractorInput input, List<String> expectedEvents)
throws IOException, InterruptedException { throws IOException, InterruptedException {
DefaultEbmlReader reader = new DefaultEbmlReader(); DefaultEbmlReader reader = new DefaultEbmlReader();
TestOutput output = new TestOutput(); TestProcessor output = new TestProcessor();
reader.init(output); reader.init(output);
// We expect the number of successful reads to equal the number of expected events. // We expect the number of successful reads to equal the number of expected events.
...@@ -164,10 +166,8 @@ public class DefaultEbmlReaderTest { ...@@ -164,10 +166,8 @@ public class DefaultEbmlReaderTest {
.build(); .build();
} }
/** /** An {@link EbmlProcessor} that records each event callback. */
* An {@link EbmlReaderOutput} that records each event callback. private static final class TestProcessor implements EbmlProcessor {
*/
private static final class TestOutput implements EbmlReaderOutput {
// Element IDs // Element IDs
private static final int ID_EBML = 0x1A45DFA3; private static final int ID_EBML = 0x1A45DFA3;
...@@ -182,22 +182,23 @@ public class DefaultEbmlReaderTest { ...@@ -182,22 +182,23 @@ public class DefaultEbmlReaderTest {
private final List<String> events = new ArrayList<>(); private final List<String> events = new ArrayList<>();
@Override @Override
public @ElementType int getElementType(int id) { @EbmlProcessor.ElementType
public int getElementType(int id) {
switch (id) { switch (id) {
case ID_EBML: case ID_EBML:
case ID_SEGMENT: case ID_SEGMENT:
return TYPE_MASTER; return EbmlProcessor.ELEMENT_TYPE_MASTER;
case ID_EBML_READ_VERSION: case ID_EBML_READ_VERSION:
case ID_DOC_TYPE_READ_VERSION: case ID_DOC_TYPE_READ_VERSION:
return TYPE_UNSIGNED_INT; return EbmlProcessor.ELEMENT_TYPE_UNSIGNED_INT;
case ID_DOC_TYPE: case ID_DOC_TYPE:
return TYPE_STRING; return EbmlProcessor.ELEMENT_TYPE_STRING;
case ID_SIMPLE_BLOCK: case ID_SIMPLE_BLOCK:
return TYPE_BINARY; return EbmlProcessor.ELEMENT_TYPE_BINARY;
case ID_DURATION: case ID_DURATION:
return TYPE_FLOAT; return EbmlProcessor.ELEMENT_TYPE_FLOAT;
default: default:
return TYPE_UNKNOWN; return EbmlProcessor.ELEMENT_TYPE_UNKNOWN;
} }
} }
...@@ -219,12 +220,12 @@ public class DefaultEbmlReaderTest { ...@@ -219,12 +220,12 @@ public class DefaultEbmlReaderTest {
@Override @Override
public void integerElement(int id, long value) { public void integerElement(int id, long value) {
events.add(formatEvent(id, "integer=" + String.valueOf(value))); events.add(formatEvent(id, "integer=" + value));
} }
@Override @Override
public void floatElement(int id, double value) { public void floatElement(int id, double value) {
events.add(formatEvent(id, "float=" + String.valueOf(value))); events.add(formatEvent(id, "float=" + value));
} }
@Override @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