Java Code Examples for com.google.android.exoplayer2.util.ParsableByteArray#readLine()

The following examples show how to use com.google.android.exoplayer2.util.ParsableByteArray#readLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SsaDecoder.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the event body of the subtitle.
 *
 * @param data A {@link ParsableByteArray} from which the body should be read.
 * @param cues A list to which parsed cues will be added.
 * @param cueTimesUs A sorted list to which parsed cue timestamps will be added.
 */
private void parseEventBody(ParsableByteArray data, List<List<Cue>> cues, List<Long> cueTimesUs) {
  @Nullable
  SsaDialogueFormat format = haveInitializationData ? dialogueFormatFromInitializationData : null;
  @Nullable String currentLine;
  while ((currentLine = data.readLine()) != null) {
    if (currentLine.startsWith(FORMAT_LINE_PREFIX)) {
      format = SsaDialogueFormat.fromFormatLine(currentLine);
    } else if (currentLine.startsWith(DIALOGUE_LINE_PREFIX)) {
      if (format == null) {
        Log.w(TAG, "Skipping dialogue line before complete format: " + currentLine);
        continue;
      }
      parseDialogueLine(currentLine, format, cues, cueTimesUs);
    }
  }
}
 
Example 2
Source File: WebvttDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Positions the input right before the next event, and returns the kind of event found. Does not
 * consume any data from such event, if any.
 *
 * @return The kind of event found.
 */
private static int getNextEvent(ParsableByteArray parsableWebvttData) {
  int foundEvent = EVENT_NONE;
  int currentInputPosition = 0;
  while (foundEvent == EVENT_NONE) {
    currentInputPosition = parsableWebvttData.getPosition();
    String line = parsableWebvttData.readLine();
    if (line == null) {
      foundEvent = EVENT_END_OF_FILE;
    } else if (STYLE_START.equals(line)) {
      foundEvent = EVENT_STYLE_BLOCK;
    } else if (line.startsWith(COMMENT_START)) {
      foundEvent = EVENT_COMMENT;
    } else {
      foundEvent = EVENT_CUE;
    }
  }
  parsableWebvttData.setPosition(currentInputPosition);
  return foundEvent;
}
 
Example 3
Source File: WebvttParserUtil.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads lines up to and including the next WebVTT cue header.
 *
 * @param input The input from which lines should be read.
 * @return A {@link Matcher} for the WebVTT cue header, or null if the end of the input was
 *     reached without a cue header being found. In the case that a cue header is found, groups 1,
 *     2 and 3 of the returned matcher contain the start time, end time and settings list.
 */
public static Matcher findNextCueHeader(ParsableByteArray input) {
  String line;
  while ((line = input.readLine()) != null) {
    if (COMMENT.matcher(line).matches()) {
      // Skip until the end of the comment block.
      while ((line = input.readLine()) != null && !line.isEmpty()) {}
    } else {
      Matcher cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(line);
      if (cueHeaderMatcher.matches()) {
        return cueHeaderMatcher;
      }
    }
  }
  return null;
}
 
Example 4
Source File: WebvttDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Positions the input right before the next event, and returns the kind of event found. Does not
 * consume any data from such event, if any.
 *
 * @return The kind of event found.
 */
private static int getNextEvent(ParsableByteArray parsableWebvttData) {
  int foundEvent = EVENT_NONE;
  int currentInputPosition = 0;
  while (foundEvent == EVENT_NONE) {
    currentInputPosition = parsableWebvttData.getPosition();
    String line = parsableWebvttData.readLine();
    if (line == null) {
      foundEvent = EVENT_END_OF_FILE;
    } else if (STYLE_START.equals(line)) {
      foundEvent = EVENT_STYLE_BLOCK;
    } else if (COMMENT_START.startsWith(line)) {
      foundEvent = EVENT_COMMENT;
    } else {
      foundEvent = EVENT_CUE;
    }
  }
  parsableWebvttData.setPosition(currentInputPosition);
  return foundEvent;
}
 
Example 5
Source File: WebvttDecoder.java    From no-player with Apache License 2.0 6 votes vote down vote up
/**
 * Positions the input right before the next event, and returns the kind of event found. Does not
 * consume any data from such event, if any.
 *
 * @return The kind of event found.
 */
private static int getNextEvent(ParsableByteArray parsableWebvttData) {
  int foundEvent = EVENT_NONE;
  int currentInputPosition = 0;
  while (foundEvent == EVENT_NONE) {
    currentInputPosition = parsableWebvttData.getPosition();
    String line = parsableWebvttData.readLine();
    if (line == null) {
      foundEvent = EVENT_END_OF_FILE;
    } else if (STYLE_START.equals(line)) {
      foundEvent = EVENT_STYLE_BLOCK;
    } else if (COMMENT_START.startsWith(line)) {
      foundEvent = EVENT_COMMENT;
    } else {
      foundEvent = EVENT_CUE;
    }
  }
  parsableWebvttData.setPosition(currentInputPosition);
  return foundEvent;
}
 
Example 6
Source File: SsaDecoder.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the {@code [V4+ Styles]} section.
 *
 * <p>When this returns, {@code data.position} will be set to the beginning of the first line that
 * starts with {@code [} (i.e. the title of the next section).
 *
 * @param data A {@link ParsableByteArray} with {@link ParsableByteArray#getPosition()} pointing
 *     at the beginning of of the first line after {@code [V4+ Styles]}.
 */
private static Map<String, SsaStyle> parseStyles(ParsableByteArray data) {
  Map<String, SsaStyle> styles = new LinkedHashMap<>();
  @Nullable SsaStyle.Format formatInfo = null;
  @Nullable String currentLine;
  while ((currentLine = data.readLine()) != null
      && (data.bytesLeft() == 0 || data.peekUnsignedByte() != '[')) {
    if (currentLine.startsWith(FORMAT_LINE_PREFIX)) {
      formatInfo = SsaStyle.Format.fromFormatLine(currentLine);
    } else if (currentLine.startsWith(STYLE_LINE_PREFIX)) {
      if (formatInfo == null) {
        Log.w(TAG, "Skipping 'Style:' line before 'Format:' line: " + currentLine);
        continue;
      }
      @Nullable SsaStyle style = SsaStyle.fromStyleLine(currentLine, formatInfo);
      if (style != null) {
        styles.put(style.name, style);
      }
    }
  }
  return styles;
}
 
Example 7
Source File: WebvttParserUtil.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads lines up to and including the next WebVTT cue header.
 *
 * @param input The input from which lines should be read.
 * @return A {@link Matcher} for the WebVTT cue header, or null if the end of the input was
 *     reached without a cue header being found. In the case that a cue header is found, groups 1,
 *     2 and 3 of the returned matcher contain the start time, end time and settings list.
 */
public static Matcher findNextCueHeader(ParsableByteArray input) {
  String line;
  while ((line = input.readLine()) != null) {
    if (COMMENT.matcher(line).matches()) {
      // Skip until the end of the comment block.
      while ((line = input.readLine()) != null && !line.isEmpty()) {}
    } else {
      Matcher cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(line);
      if (cueHeaderMatcher.matches()) {
        return cueHeaderMatcher;
      }
    }
  }
  return null;
}
 
Example 8
Source File: WebvttCueParser.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses the next valid WebVTT cue in a parsable array, including timestamps, settings and text.
 *
 * @param webvttData Parsable WebVTT file data.
 * @param builder Builder for WebVTT Cues.
 * @param styles List of styles defined by the CSS style blocks preceeding the cues.
 * @return Whether a valid Cue was found.
 */
public boolean parseCue(ParsableByteArray webvttData, WebvttCue.Builder builder,
    List<WebvttCssStyle> styles) {
  String firstLine = webvttData.readLine();
  if (firstLine == null) {
    return false;
  }
  Matcher cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(firstLine);
  if (cueHeaderMatcher.matches()) {
    // We have found the timestamps in the first line. No id present.
    return parseCue(null, cueHeaderMatcher, webvttData, builder, textBuilder, styles);
  }
  // The first line is not the timestamps, but could be the cue id.
  String secondLine = webvttData.readLine();
  if (secondLine == null) {
    return false;
  }
  cueHeaderMatcher = WebvttCueParser.CUE_HEADER_PATTERN.matcher(secondLine);
  if (cueHeaderMatcher.matches()) {
    // We can do the rest of the parsing, including the id.
    return parseCue(firstLine.trim(), cueHeaderMatcher, webvttData, builder, textBuilder,
        styles);
  }
  return false;
}
 
Example 9
Source File: WebvttParserUtil.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Reads and validates the first line of a WebVTT file.
 *
 * @param input The input from which the line should be read.
 * @throws SubtitleDecoderException If the line isn't the start of a valid WebVTT file.
 */
public static void validateWebvttHeaderLine(ParsableByteArray input)
    throws SubtitleDecoderException {
  String line = input.readLine();
  if (line == null || !HEADER.matcher(line).matches()) {
    throw new SubtitleDecoderException("Expected WEBVTT. Got " + line);
  }
}
 
Example 10
Source File: WebvttParserUtil.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Reads and validates the first line of a WebVTT file.
 *
 * @param input The input from which the line should be read.
 * @throws ParserException If the line isn't the start of a valid WebVTT file.
 */
public static void validateWebvttHeaderLine(ParsableByteArray input) throws ParserException {
  int startPosition = input.getPosition();
  if (!isWebvttHeaderLine(input)) {
    input.setPosition(startPosition);
    throw new ParserException("Expected WEBVTT. Got " + input.readLine());
  }
}
 
Example 11
Source File: CssParser.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
static void skipStyleBlock(ParsableByteArray input) {
  // The style block cannot contain empty lines, so we assume the input ends when a empty line
  // is found.
  String line;
  do {
    line = input.readLine();
  } while (!TextUtils.isEmpty(line));
}
 
Example 12
Source File: CssParser.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
static void skipStyleBlock(ParsableByteArray input) {
  // The style block cannot contain empty lines, so we assume the input ends when a empty line
  // is found.
  String line;
  do {
    line = input.readLine();
  } while (!TextUtils.isEmpty(line));
}
 
Example 13
Source File: CssParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
static void skipStyleBlock(ParsableByteArray input) {
  // The style block cannot contain empty lines, so we assume the input ends when a empty line
  // is found.
  String line;
  do {
    line = input.readLine();
  } while (!TextUtils.isEmpty(line));
}
 
Example 14
Source File: CssParser.java    From no-player with Apache License 2.0 5 votes vote down vote up
static void skipStyleBlock(ParsableByteArray input) {
  // The style block cannot contain empty lines, so we assume the input ends when a empty line
  // is found.
  String line;
  do {
    line = input.readLine();
  } while (!TextUtils.isEmpty(line));
}
 
Example 15
Source File: WebvttCueParser.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private static boolean parseCue(
    @Nullable String id,
    Matcher cueHeaderMatcher,
    ParsableByteArray webvttData,
    WebvttCue.Builder builder,
    StringBuilder textBuilder,
    List<WebvttCssStyle> styles) {
  try {
    // Parse the cue start and end times.
    builder.setStartTime(WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1)))
        .setEndTime(WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(2)));
  } catch (NumberFormatException e) {
    Log.w(TAG, "Skipping cue with bad header: " + cueHeaderMatcher.group());
    return false;
  }

  parseCueSettingsList(cueHeaderMatcher.group(3), builder);

  // Parse the cue text.
  textBuilder.setLength(0);
  for (String line = webvttData.readLine();
      !TextUtils.isEmpty(line);
      line = webvttData.readLine()) {
    if (textBuilder.length() > 0) {
      textBuilder.append("\n");
    }
    textBuilder.append(line.trim());
  }
  parseCueText(id, textBuilder.toString(), builder, styles);
  return true;
}
 
Example 16
Source File: SsaDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses the event body of the subtitle.
 *
 * @param data A {@link ParsableByteArray} from which the body should be read.
 * @param cues A list to which parsed cues will be added.
 * @param cueTimesUs An array to which parsed cue timestamps will be added.
 */
private void parseEventBody(ParsableByteArray data, List<Cue> cues, LongArray cueTimesUs) {
  String currentLine;
  while ((currentLine = data.readLine()) != null) {
    if (!haveInitializationData && currentLine.startsWith(FORMAT_LINE_PREFIX)) {
      parseFormatLine(currentLine);
    } else if (currentLine.startsWith(DIALOGUE_LINE_PREFIX)) {
      parseDialogueLine(currentLine, cues, cueTimesUs);
    }
  }
}
 
Example 17
Source File: SubripDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length, boolean reset) {
  ArrayList<Cue> cues = new ArrayList<>();
  LongArray cueTimesUs = new LongArray();
  ParsableByteArray subripData = new ParsableByteArray(bytes, length);
  String currentLine;

  while ((currentLine = subripData.readLine()) != null) {
    if (currentLine.length() == 0) {
      // Skip blank lines.
      continue;
    }

    // Parse the index line as a sanity check.
    try {
      Integer.parseInt(currentLine);
    } catch (NumberFormatException e) {
      Log.w(TAG, "Skipping invalid index: " + currentLine);
      continue;
    }

    // Read and parse the timing line.
    boolean haveEndTimecode = false;
    currentLine = subripData.readLine();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
    if (matcher.matches()) {
      cueTimesUs.add(parseTimecode(matcher, 1));
      if (!TextUtils.isEmpty(matcher.group(6))) {
        haveEndTimecode = true;
        cueTimesUs.add(parseTimecode(matcher, 6));
      }
    } else {
      Log.w(TAG, "Skipping invalid timing: " + currentLine);
      continue;
    }

    // Read and parse the text.
    textBuilder.setLength(0);
    while (!TextUtils.isEmpty(currentLine = subripData.readLine())) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(currentLine.trim());
    }

    Spanned text = Html.fromHtml(textBuilder.toString());
    cues.add(new Cue(text));
    if (haveEndTimecode) {
      cues.add(null);
    }
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
 
Example 18
Source File: SubripDecoder.java    From K-Sonic with MIT License 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length) {
  ArrayList<Cue> cues = new ArrayList<>();
  LongArray cueTimesUs = new LongArray();
  ParsableByteArray subripData = new ParsableByteArray(bytes, length);
  String currentLine;

  while ((currentLine = subripData.readLine()) != null) {
    if (currentLine.length() == 0) {
      // Skip blank lines.
      continue;
    }

    // Parse the index line as a sanity check.
    try {
      Integer.parseInt(currentLine);
    } catch (NumberFormatException e) {
      Log.w(TAG, "Skipping invalid index: " + currentLine);
      continue;
    }

    // Read and parse the timing line.
    boolean haveEndTimecode = false;
    currentLine = subripData.readLine();
    Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
    if (matcher.matches()) {
      cueTimesUs.add(parseTimecode(matcher, 1));
      if (!TextUtils.isEmpty(matcher.group(6))) {
        haveEndTimecode = true;
        cueTimesUs.add(parseTimecode(matcher, 6));
      }
    } else {
      Log.w(TAG, "Skipping invalid timing: " + currentLine);
      continue;
    }

    // Read and parse the text.
    textBuilder.setLength(0);
    while (!TextUtils.isEmpty(currentLine = subripData.readLine())) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(currentLine.trim());
    }

    Spanned text = Html.fromHtml(textBuilder.toString());
    cues.add(new Cue(text));
    if (haveEndTimecode) {
      cues.add(null);
    }
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
 
Example 19
Source File: SubripDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected SubripSubtitle decode(byte[] bytes, int length, boolean reset) {
  ArrayList<Cue> cues = new ArrayList<>();
  LongArray cueTimesUs = new LongArray();
  ParsableByteArray subripData = new ParsableByteArray(bytes, length);
  String currentLine;

  while ((currentLine = subripData.readLine()) != null) {
    if (currentLine.length() == 0) {
      // Skip blank lines.
      continue;
    }

    // Parse the index line as a sanity check.
    try {
      Integer.parseInt(currentLine);
    } catch (NumberFormatException e) {
      Log.w(TAG, "Skipping invalid index: " + currentLine);
      continue;
    }

    // Read and parse the timing line.
    boolean haveEndTimecode = false;
    currentLine = subripData.readLine();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
    if (matcher.matches()) {
      cueTimesUs.add(parseTimecode(matcher, 1));
      if (!TextUtils.isEmpty(matcher.group(6))) {
        haveEndTimecode = true;
        cueTimesUs.add(parseTimecode(matcher, 6));
      }
    } else {
      Log.w(TAG, "Skipping invalid timing: " + currentLine);
      continue;
    }

    // Read and parse the text.
    textBuilder.setLength(0);
    while (!TextUtils.isEmpty(currentLine = subripData.readLine())) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(currentLine.trim());
    }

    Spanned text = Html.fromHtml(textBuilder.toString());
    cues.add(new Cue(text));
    if (haveEndTimecode) {
      cues.add(null);
    }
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
 
Example 20
Source File: SubripDecoder.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
protected Subtitle decode(byte[] bytes, int length, boolean reset) {
  ArrayList<Cue> cues = new ArrayList<>();
  LongArray cueTimesUs = new LongArray();
  ParsableByteArray subripData = new ParsableByteArray(bytes, length);

  @Nullable String currentLine;
  while ((currentLine = subripData.readLine()) != null) {
    if (currentLine.length() == 0) {
      // Skip blank lines.
      continue;
    }

    // Parse the index line as a sanity check.
    try {
      Integer.parseInt(currentLine);
    } catch (NumberFormatException e) {
      Log.w(TAG, "Skipping invalid index: " + currentLine);
      continue;
    }

    // Read and parse the timing line.
    currentLine = subripData.readLine();
    if (currentLine == null) {
      Log.w(TAG, "Unexpected end");
      break;
    }

    Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
    if (matcher.matches()) {
      cueTimesUs.add(parseTimecode(matcher, /* groupOffset= */ 1));
      cueTimesUs.add(parseTimecode(matcher, /* groupOffset= */ 6));
    } else {
      Log.w(TAG, "Skipping invalid timing: " + currentLine);
      continue;
    }

    // Read and parse the text and tags.
    textBuilder.setLength(0);
    tags.clear();
    currentLine = subripData.readLine();
    while (!TextUtils.isEmpty(currentLine)) {
      if (textBuilder.length() > 0) {
        textBuilder.append("<br>");
      }
      textBuilder.append(processLine(currentLine, tags));
      currentLine = subripData.readLine();
    }

    Spanned text = Html.fromHtml(textBuilder.toString());

    @Nullable String alignmentTag = null;
    for (int i = 0; i < tags.size(); i++) {
      String tag = tags.get(i);
      if (tag.matches(SUBRIP_ALIGNMENT_TAG)) {
        alignmentTag = tag;
        // Subsequent alignment tags should be ignored.
        break;
      }
    }
    cues.add(buildCue(text, alignmentTag));
    cues.add(Cue.EMPTY);
  }

  Cue[] cuesArray = new Cue[cues.size()];
  cues.toArray(cuesArray);
  long[] cueTimesUsArray = cueTimesUs.toArray();
  return new SubripSubtitle(cuesArray, cueTimesUsArray);
}