Commit b6b97a86 by aquilescanta Committed by Oliver Woodman

Expose cue settings parser

This CL exposes the cue settings parser in order to allow its usage from the MP4Webvtt extractor. Also fixes a few mistakes from the previous related CL.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=112145806
parent 6b9a1b16
......@@ -18,6 +18,7 @@ package com.google.android.exoplayer.text.webvtt;
import com.google.android.exoplayer.text.Cue;
import android.text.Layout.Alignment;
import android.util.Log;
/**
* A representation of a WebVTT cue.
......@@ -53,4 +54,127 @@ import android.text.Layout.Alignment;
return (line == DIMEN_UNSET && position == DIMEN_UNSET);
}
/**
* Builder for WebVTT cues.
*/
@SuppressWarnings("hiding")
public static final class Builder {
private static final String TAG = "WebvttCueBuilder";
private long startTime;
private long endTime;
private CharSequence text;
private Alignment textAlignment;
private float line;
private int lineType;
private int lineAnchor;
private float position;
private int positionAnchor;
private float width;
// Initialization methods
public Builder() {
reset();
}
public void reset() {
startTime = 0;
endTime = 0;
text = null;
textAlignment = null;
line = Cue.DIMEN_UNSET;
lineType = Cue.TYPE_UNSET;
lineAnchor = Cue.TYPE_UNSET;
position = Cue.DIMEN_UNSET;
positionAnchor = Cue.TYPE_UNSET;
width = Cue.DIMEN_UNSET;
}
// Construction methods
public WebvttCue build() {
if (position != Cue.DIMEN_UNSET && positionAnchor == Cue.TYPE_UNSET) {
derivePositionAnchorFromAlignment();
}
return new WebvttCue(startTime, endTime, text, textAlignment, line, lineType, lineAnchor,
position, positionAnchor, width);
}
public Builder setStartTime(long time) {
startTime = time;
return this;
}
public Builder setEndTime(long time) {
endTime = time;
return this;
}
public Builder setText(CharSequence aText) {
text = aText;
return this;
}
public Builder setTextAlignment(Alignment textAlignment) {
this.textAlignment = textAlignment;
return this;
}
public Builder setLine(float line) {
this.line = line;
return this;
}
public Builder setLineType(int lineType) {
this.lineType = lineType;
return this;
}
public Builder setLineAnchor(int lineAnchor) {
this.lineAnchor = lineAnchor;
return this;
}
public Builder setPosition(float position) {
this.position = position;
return this;
}
public Builder setPositionAnchor(int positionAnchor) {
this.positionAnchor = positionAnchor;
return this;
}
public Builder setWidth(float width) {
this.width = width;
return this;
}
private Builder derivePositionAnchorFromAlignment() {
if (textAlignment == null) {
positionAnchor = Cue.TYPE_UNSET;
} else {
switch (textAlignment) {
case ALIGN_NORMAL:
positionAnchor = Cue.ANCHOR_TYPE_START;
break;
case ALIGN_CENTER:
positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
break;
case ALIGN_OPPOSITE:
positionAnchor = Cue.ANCHOR_TYPE_END;
break;
default:
Log.w(TAG, "Unrecognized alignment: " + textAlignment);
positionAnchor = Cue.ANCHOR_TYPE_START;
break;
}
}
return this;
}
}
}
......@@ -33,10 +33,12 @@ public final class WebvttParser implements SubtitleParser {
private final WebvttCueParser cueParser;
private final ParsableByteArray parsableWebvttData;
private final WebvttCue.Builder webvttCueBuilder;
public WebvttParser() {
cueParser = new WebvttCueParser();
parsableWebvttData = new ParsableByteArray();
webvttCueBuilder = new WebvttCue.Builder();
}
@Override
......@@ -48,6 +50,7 @@ public final class WebvttParser implements SubtitleParser {
public final WebvttSubtitle parse(byte[] bytes, int offset, int length) throws ParserException {
parsableWebvttData.reset(bytes, offset + length);
parsableWebvttData.setPosition(offset);
webvttCueBuilder.reset(); // In case a previous parse run failed with a ParserException.
// Validate the first line of the header, and skip the remainder.
WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
......@@ -55,9 +58,9 @@ public final class WebvttParser implements SubtitleParser {
// Extract Cues
ArrayList<WebvttCue> subtitles = new ArrayList<>();
WebvttCue currentWebvttCue;
while ((currentWebvttCue = cueParser.parseNextValidCue(parsableWebvttData)) != null) {
subtitles.add(currentWebvttCue);
while (cueParser.parseNextValidCue(parsableWebvttData, webvttCueBuilder)) {
subtitles.add(webvttCueBuilder.build());
webvttCueBuilder.reset();
}
return new WebvttSubtitle(subtitles);
}
......
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