com.google.android.exoplayer2.text.SubtitleDecoderException Java Examples

The following examples show how to use com.google.android.exoplayer2.text.SubtitleDecoderException. 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: TtmlDecoder.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private CellResolution parseCellResolution(XmlPullParser xmlParser, CellResolution defaultValue)
    throws SubtitleDecoderException {
  String cellResolution = xmlParser.getAttributeValue(TTP, "cellResolution");
  if (cellResolution == null) {
    return defaultValue;
  }

  Matcher cellResolutionMatcher = CELL_RESOLUTION.matcher(cellResolution);
  if (!cellResolutionMatcher.matches()) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
  try {
    int columns = Integer.parseInt(cellResolutionMatcher.group(1));
    int rows = Integer.parseInt(cellResolutionMatcher.group(2));
    if (columns == 0 || rows == 0) {
      throw new SubtitleDecoderException("Invalid cell resolution " + columns + " " + rows);
    }
    return new CellResolution(columns, rows);
  } catch (NumberFormatException e) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
}
 
Example #2
Source File: Mp4WebvttDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private static Cue parseVttCueBox(ParsableByteArray sampleData, WebvttCue.Builder builder,
      int remainingCueBoxBytes) throws SubtitleDecoderException {
  builder.reset();
  while (remainingCueBoxBytes > 0) {
    if (remainingCueBoxBytes < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete vtt cue box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    remainingCueBoxBytes -= BOX_HEADER_SIZE;
    int payloadLength = boxSize - BOX_HEADER_SIZE;
    String boxPayload =
        Util.fromUtf8Bytes(sampleData.data, sampleData.getPosition(), payloadLength);
    sampleData.skipBytes(payloadLength);
    remainingCueBoxBytes -= payloadLength;
    if (boxType == TYPE_sttg) {
      WebvttCueParser.parseCueSettingsList(boxPayload, builder);
    } else if (boxType == TYPE_payl) {
      WebvttCueParser.parseCueText(null, boxPayload.trim(), builder, Collections.emptyList());
    } else {
      // Other VTTCueBox children are still not supported and are ignored.
    }
  }
  return builder.build();
}
 
Example #3
Source File: Mp4WebvttDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Mp4WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  // Webvtt in Mp4 samples have boxes inside of them, so we have to do a traditional box parsing:
  // first 4 bytes size and then 4 bytes type.
  sampleData.reset(bytes, length);
  List<Cue> resultingCueList = new ArrayList<>();
  while (sampleData.bytesLeft() > 0) {
    if (sampleData.bytesLeft() < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete Mp4Webvtt Top Level box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    if (boxType == TYPE_vttc) {
      resultingCueList.add(parseVttCueBox(sampleData, builder, boxSize - BOX_HEADER_SIZE));
    } else {
      // Peers of the VTTCueBox are still not supported and are skipped.
      sampleData.skipBytes(boxSize - BOX_HEADER_SIZE);
    }
  }
  return new Mp4WebvttSubtitle(resultingCueList);
}
 
Example #4
Source File: Mp4WebvttDecoder.java    From K-Sonic with MIT License 6 votes vote down vote up
private static Cue parseVttCueBox(ParsableByteArray sampleData, WebvttCue.Builder builder,
      int remainingCueBoxBytes) throws SubtitleDecoderException {
  builder.reset();
  while (remainingCueBoxBytes > 0) {
    if (remainingCueBoxBytes < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete vtt cue box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    remainingCueBoxBytes -= BOX_HEADER_SIZE;
    int payloadLength = boxSize - BOX_HEADER_SIZE;
    String boxPayload = new String(sampleData.data, sampleData.getPosition(), payloadLength);
    sampleData.skipBytes(payloadLength);
    remainingCueBoxBytes -= payloadLength;
    if (boxType == TYPE_sttg) {
      WebvttCueParser.parseCueSettingsList(boxPayload, builder);
    } else if (boxType == TYPE_payl) {
      WebvttCueParser.parseCueText(null, boxPayload.trim(), builder,
          Collections.<WebvttCssStyle>emptyList());
    } else {
      // Other VTTCueBox children are still not supported and are ignored.
    }
  }
  return builder.build();
}
 
Example #5
Source File: Mp4WebvttDecoder.java    From K-Sonic with MIT License 6 votes vote down vote up
@Override
protected Mp4WebvttSubtitle decode(byte[] bytes, int length) throws SubtitleDecoderException {
  // Webvtt in Mp4 samples have boxes inside of them, so we have to do a traditional box parsing:
  // first 4 bytes size and then 4 bytes type.
  sampleData.reset(bytes, length);
  List<Cue> resultingCueList = new ArrayList<>();
  while (sampleData.bytesLeft() > 0) {
    if (sampleData.bytesLeft() < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete Mp4Webvtt Top Level box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    if (boxType == TYPE_vttc) {
      resultingCueList.add(parseVttCueBox(sampleData, builder, boxSize - BOX_HEADER_SIZE));
    } else {
      // Peers of the VTTCueBox are still not supported and are skipped.
      sampleData.skipBytes(boxSize - BOX_HEADER_SIZE);
    }
  }
  return new Mp4WebvttSubtitle(resultingCueList);
}
 
Example #6
Source File: TtmlDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private CellResolution parseCellResolution(XmlPullParser xmlParser, CellResolution defaultValue)
    throws SubtitleDecoderException {
  String cellResolution = xmlParser.getAttributeValue(TTP, "cellResolution");
  if (cellResolution == null) {
    return defaultValue;
  }

  Matcher cellResolutionMatcher = CELL_RESOLUTION.matcher(cellResolution);
  if (!cellResolutionMatcher.matches()) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
  try {
    int columns = Integer.parseInt(cellResolutionMatcher.group(1));
    int rows = Integer.parseInt(cellResolutionMatcher.group(2));
    if (columns == 0 || rows == 0) {
      throw new SubtitleDecoderException("Invalid cell resolution " + columns + " " + rows);
    }
    return new CellResolution(columns, rows);
  } catch (NumberFormatException e) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
}
 
Example #7
Source File: TtmlDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private CellResolution parseCellResolution(XmlPullParser xmlParser, CellResolution defaultValue)
    throws SubtitleDecoderException {
  String cellResolution = xmlParser.getAttributeValue(TTP, "cellResolution");
  if (cellResolution == null) {
    return defaultValue;
  }

  Matcher cellResolutionMatcher = CELL_RESOLUTION.matcher(cellResolution);
  if (!cellResolutionMatcher.matches()) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
  try {
    int columns = Integer.parseInt(cellResolutionMatcher.group(1));
    int rows = Integer.parseInt(cellResolutionMatcher.group(2));
    if (columns == 0 || rows == 0) {
      throw new SubtitleDecoderException("Invalid cell resolution " + columns + " " + rows);
    }
    return new CellResolution(columns, rows);
  } catch (NumberFormatException e) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
}
 
Example #8
Source File: Mp4WebvttDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Mp4WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  // Webvtt in Mp4 samples have boxes inside of them, so we have to do a traditional box parsing:
  // first 4 bytes size and then 4 bytes type.
  sampleData.reset(bytes, length);
  List<Cue> resultingCueList = new ArrayList<>();
  while (sampleData.bytesLeft() > 0) {
    if (sampleData.bytesLeft() < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete Mp4Webvtt Top Level box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    if (boxType == TYPE_vttc) {
      resultingCueList.add(parseVttCueBox(sampleData, builder, boxSize - BOX_HEADER_SIZE));
    } else {
      // Peers of the VTTCueBox are still not supported and are skipped.
      sampleData.skipBytes(boxSize - BOX_HEADER_SIZE);
    }
  }
  return new Mp4WebvttSubtitle(resultingCueList);
}
 
Example #9
Source File: Mp4WebvttDecoder.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private static Cue parseVttCueBox(ParsableByteArray sampleData, WebvttCue.Builder builder,
      int remainingCueBoxBytes) throws SubtitleDecoderException {
  builder.reset();
  while (remainingCueBoxBytes > 0) {
    if (remainingCueBoxBytes < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete vtt cue box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    remainingCueBoxBytes -= BOX_HEADER_SIZE;
    int payloadLength = boxSize - BOX_HEADER_SIZE;
    String boxPayload =
        Util.fromUtf8Bytes(sampleData.data, sampleData.getPosition(), payloadLength);
    sampleData.skipBytes(payloadLength);
    remainingCueBoxBytes -= payloadLength;
    if (boxType == TYPE_sttg) {
      WebvttCueParser.parseCueSettingsList(boxPayload, builder);
    } else if (boxType == TYPE_payl) {
      WebvttCueParser.parseCueText(null, boxPayload.trim(), builder, Collections.emptyList());
    } else {
      // Other VTTCueBox children are still not supported and are ignored.
    }
  }
  return builder.build();
}
 
Example #10
Source File: Mp4WebvttDecoder.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Mp4WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  // Webvtt in Mp4 samples have boxes inside of them, so we have to do a traditional box parsing:
  // first 4 bytes size and then 4 bytes type.
  sampleData.reset(bytes, length);
  List<Cue> resultingCueList = new ArrayList<>();
  while (sampleData.bytesLeft() > 0) {
    if (sampleData.bytesLeft() < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete Mp4Webvtt Top Level box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    if (boxType == TYPE_vttc) {
      resultingCueList.add(parseVttCueBox(sampleData, builder, boxSize - BOX_HEADER_SIZE));
    } else {
      // Peers of the VTTCueBox are still not supported and are skipped.
      sampleData.skipBytes(boxSize - BOX_HEADER_SIZE);
    }
  }
  return new Mp4WebvttSubtitle(resultingCueList);
}
 
Example #11
Source File: Mp4WebvttDecoder.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
protected Subtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  // Webvtt in Mp4 samples have boxes inside of them, so we have to do a traditional box parsing:
  // first 4 bytes size and then 4 bytes type.
  sampleData.reset(bytes, length);
  List<Cue> resultingCueList = new ArrayList<>();
  while (sampleData.bytesLeft() > 0) {
    if (sampleData.bytesLeft() < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete Mp4Webvtt Top Level box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    if (boxType == TYPE_vttc) {
      resultingCueList.add(parseVttCueBox(sampleData, builder, boxSize - BOX_HEADER_SIZE));
    } else {
      // Peers of the VTTCueBox are still not supported and are skipped.
      sampleData.skipBytes(boxSize - BOX_HEADER_SIZE);
    }
  }
  return new Mp4WebvttSubtitle(resultingCueList);
}
 
Example #12
Source File: Mp4WebvttDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private static Cue parseVttCueBox(ParsableByteArray sampleData, WebvttCue.Builder builder,
      int remainingCueBoxBytes) throws SubtitleDecoderException {
  builder.reset();
  while (remainingCueBoxBytes > 0) {
    if (remainingCueBoxBytes < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete vtt cue box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    remainingCueBoxBytes -= BOX_HEADER_SIZE;
    int payloadLength = boxSize - BOX_HEADER_SIZE;
    String boxPayload =
        Util.fromUtf8Bytes(sampleData.data, sampleData.getPosition(), payloadLength);
    sampleData.skipBytes(payloadLength);
    remainingCueBoxBytes -= payloadLength;
    if (boxType == TYPE_sttg) {
      WebvttCueParser.parseCueSettingsList(boxPayload, builder);
    } else if (boxType == TYPE_payl) {
      WebvttCueParser.parseCueText(null, boxPayload.trim(), builder, Collections.emptyList());
    } else {
      // Other VTTCueBox children are still not supported and are ignored.
    }
  }
  return builder.build();
}
 
Example #13
Source File: Mp4WebvttDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Mp4WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  // Webvtt in Mp4 samples have boxes inside of them, so we have to do a traditional box parsing:
  // first 4 bytes size and then 4 bytes type.
  sampleData.reset(bytes, length);
  List<Cue> resultingCueList = new ArrayList<>();
  while (sampleData.bytesLeft() > 0) {
    if (sampleData.bytesLeft() < BOX_HEADER_SIZE) {
      throw new SubtitleDecoderException("Incomplete Mp4Webvtt Top Level box header found.");
    }
    int boxSize = sampleData.readInt();
    int boxType = sampleData.readInt();
    if (boxType == TYPE_vttc) {
      resultingCueList.add(parseVttCueBox(sampleData, builder, boxSize - BOX_HEADER_SIZE));
    } else {
      // Peers of the VTTCueBox are still not supported and are skipped.
      sampleData.skipBytes(boxSize - BOX_HEADER_SIZE);
    }
  }
  return new Mp4WebvttSubtitle(resultingCueList);
}
 
Example #14
Source File: TtmlDecoder.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private CellResolution parseCellResolution(XmlPullParser xmlParser, CellResolution defaultValue)
    throws SubtitleDecoderException {
  String cellResolution = xmlParser.getAttributeValue(TTP, "cellResolution");
  if (cellResolution == null) {
    return defaultValue;
  }

  Matcher cellResolutionMatcher = CELL_RESOLUTION.matcher(cellResolution);
  if (!cellResolutionMatcher.matches()) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
  try {
    int columns = Integer.parseInt(cellResolutionMatcher.group(1));
    int rows = Integer.parseInt(cellResolutionMatcher.group(2));
    if (columns == 0 || rows == 0) {
      throw new SubtitleDecoderException("Invalid cell resolution " + columns + " " + rows);
    }
    return new CellResolution(columns, rows);
  } catch (NumberFormatException e) {
    Log.w(TAG, "Ignoring malformed cell resolution: " + cellResolution);
    return defaultValue;
  }
}
 
Example #15
Source File: PgsDecoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Subtitle decode(byte[] data, int size, boolean reset) throws SubtitleDecoderException {
  if (maybeInflateData(data, size)) {
    buffer.reset(inflatedData, inflatedDataSize);
  } else {
    buffer.reset(data, size);
  }
  cueBuilder.reset();
  ArrayList<Cue> cues = new ArrayList<>();
  while (buffer.bytesLeft() >= 3) {
    Cue cue = readNextSection(buffer, cueBuilder);
    if (cue != null) {
      cues.add(cue);
    }
  }
  return new PgsSubtitle(Collections.unmodifiableList(cues));
}
 
Example #16
Source File: TtmlDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static void parseFontSize(String expression, TtmlStyle out) throws
    SubtitleDecoderException {
  String[] expressions = Util.split(expression, "\\s+");
  Matcher matcher;
  if (expressions.length == 1) {
    matcher = FONT_SIZE.matcher(expression);
  } else if (expressions.length == 2){
    matcher = FONT_SIZE.matcher(expressions[1]);
    Log.w(TAG, "Multiple values in fontSize attribute. Picking the second value for vertical font"
        + " size and ignoring the first.");
  } else {
    throw new SubtitleDecoderException("Invalid number of entries for fontSize: "
        + expressions.length + ".");
  }

  if (matcher.matches()) {
    String unit = matcher.group(3);
    switch (unit) {
      case "px":
        out.setFontSizeUnit(TtmlStyle.FONT_SIZE_UNIT_PIXEL);
        break;
      case "em":
        out.setFontSizeUnit(TtmlStyle.FONT_SIZE_UNIT_EM);
        break;
      case "%":
        out.setFontSizeUnit(TtmlStyle.FONT_SIZE_UNIT_PERCENT);
        break;
      default:
        throw new SubtitleDecoderException("Invalid unit for fontSize: '" + unit + "'.");
    }
    out.setFontSize(Float.valueOf(matcher.group(1)));
  } else {
    throw new SubtitleDecoderException("Invalid expression for fontSize: '" + expression + "'.");
  }
}
 
Example #17
Source File: Tx3gDecoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static String readSubtitleText(ParsableByteArray parsableByteArray)
    throws SubtitleDecoderException {
  assertTrue(parsableByteArray.bytesLeft() >= SIZE_SHORT);
  int textLength = parsableByteArray.readUnsignedShort();
  if (textLength == 0) {
    return "";
  }
  if (parsableByteArray.bytesLeft() >= SIZE_BOM_UTF16) {
    char firstChar = parsableByteArray.peekChar();
    if (firstChar == BOM_UTF16_BE || firstChar == BOM_UTF16_LE) {
      return parsableByteArray.readString(textLength, Charset.forName(C.UTF16_NAME));
    }
  }
  return parsableByteArray.readString(textLength, Charset.forName(C.UTF8_NAME));
}
 
Example #18
Source File: PgsDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Subtitle decode(byte[] data, int size, boolean reset) throws SubtitleDecoderException {
  buffer.reset(data, size);
  maybeInflateData(buffer);
  cueBuilder.reset();
  ArrayList<Cue> cues = new ArrayList<>();
  while (buffer.bytesLeft() >= 3) {
    Cue cue = readNextSection(buffer, cueBuilder);
    if (cue != null) {
      cues.add(cue);
    }
  }
  return new PgsSubtitle(Collections.unmodifiableList(cues));
}
 
Example #19
Source File: CeaDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void queueInputBuffer(SubtitleInputBuffer inputBuffer) throws SubtitleDecoderException {
  Assertions.checkArgument(inputBuffer == dequeuedInputBuffer);
  if (inputBuffer.isDecodeOnly()) {
    // We can drop this buffer early (i.e. before it would be decoded) as the CEA formats allow
    // for decoding to begin mid-stream.
    releaseInputBuffer(dequeuedInputBuffer);
  } else {
    dequeuedInputBuffer.queuedInputBufferCount = queuedInputBufferCount++;
    queuedInputBuffers.add(dequeuedInputBuffer);
  }
  dequeuedInputBuffer = null;
}
 
Example #20
Source File: CeaDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SubtitleInputBuffer dequeueInputBuffer() throws SubtitleDecoderException {
  Assertions.checkState(dequeuedInputBuffer == null);
  if (availableInputBuffers.isEmpty()) {
    return null;
  }
  dequeuedInputBuffer = availableInputBuffers.pollFirst();
  return dequeuedInputBuffer;
}
 
Example #21
Source File: WebvttDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  parsableWebvttData.reset(bytes, length);
  // Initialization for consistent starting state.
  webvttCueBuilder.reset();
  definedStyles.clear();

  // Validate the first line of the header, and skip the remainder.
  try {
    WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
  } catch (ParserException e) {
    throw new SubtitleDecoderException(e);
  }
  while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {}

  int event;
  ArrayList<WebvttCue> subtitles = new ArrayList<>();
  while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
    if (event == EVENT_COMMENT) {
      skipComment(parsableWebvttData);
    } else if (event == EVENT_STYLE_BLOCK) {
      if (!subtitles.isEmpty()) {
        throw new SubtitleDecoderException("A style block was found after the first cue.");
      }
      parsableWebvttData.readLine(); // Consume the "STYLE" header.
      definedStyles.addAll(cssParser.parseBlock(parsableWebvttData));
    } else if (event == EVENT_CUE) {
      if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
        subtitles.add(webvttCueBuilder.build());
        webvttCueBuilder.reset();
      }
    }
  }
  return new WebvttSubtitle(subtitles);
}
 
Example #22
Source File: TtmlDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private FrameAndTickRate parseFrameAndTickRates(XmlPullParser xmlParser)
    throws SubtitleDecoderException {
  int frameRate = DEFAULT_FRAME_RATE;
  String frameRateString = xmlParser.getAttributeValue(TTP, "frameRate");
  if (frameRateString != null) {
    frameRate = Integer.parseInt(frameRateString);
  }

  float frameRateMultiplier = 1;
  String frameRateMultiplierString = xmlParser.getAttributeValue(TTP, "frameRateMultiplier");
  if (frameRateMultiplierString != null) {
    String[] parts = Util.split(frameRateMultiplierString, " ");
    if (parts.length != 2) {
      throw new SubtitleDecoderException("frameRateMultiplier doesn't have 2 parts");
    }
    float numerator = Integer.parseInt(parts[0]);
    float denominator = Integer.parseInt(parts[1]);
    frameRateMultiplier = numerator / denominator;
  }

  int subFrameRate = DEFAULT_FRAME_AND_TICK_RATE.subFrameRate;
  String subFrameRateString = xmlParser.getAttributeValue(TTP, "subFrameRate");
  if (subFrameRateString != null) {
    subFrameRate = Integer.parseInt(subFrameRateString);
  }

  int tickRate = DEFAULT_FRAME_AND_TICK_RATE.tickRate;
  String tickRateString = xmlParser.getAttributeValue(TTP, "tickRate");
  if (tickRateString != null) {
    tickRate = Integer.parseInt(tickRateString);
  }
  return new FrameAndTickRate(frameRate * frameRateMultiplier, subFrameRate, tickRate);
}
 
Example #23
Source File: CeaDecoder.java    From K-Sonic with MIT License 5 votes vote down vote up
@Override
public void queueInputBuffer(SubtitleInputBuffer inputBuffer) throws SubtitleDecoderException {
  Assertions.checkArgument(inputBuffer != null);
  Assertions.checkArgument(inputBuffer == dequeuedInputBuffer);
  if (inputBuffer.isDecodeOnly()) {
    // We can drop this buffer early (i.e. before it would be decoded) as the CEA formats allow
    // for decoding to begin mid-stream.
    releaseInputBuffer(inputBuffer);
  } else {
    queuedInputBuffers.add(inputBuffer);
  }
  dequeuedInputBuffer = null;
}
 
Example #24
Source File: WebvttDecoder.java    From K-Sonic with MIT License 5 votes vote down vote up
@Override
protected WebvttSubtitle decode(byte[] bytes, int length) throws SubtitleDecoderException {
  parsableWebvttData.reset(bytes, length);
  // Initialization for consistent starting state.
  webvttCueBuilder.reset();
  definedStyles.clear();

  // Validate the first line of the header, and skip the remainder.
  WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
  while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {}

  int event;
  ArrayList<WebvttCue> subtitles = new ArrayList<>();
  while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
    if (event == EVENT_COMMENT) {
      skipComment(parsableWebvttData);
    } else if (event == EVENT_STYLE_BLOCK) {
      if (!subtitles.isEmpty()) {
        throw new SubtitleDecoderException("A style block was found after the first cue.");
      }
      parsableWebvttData.readLine(); // Consume the "STYLE" header.
      WebvttCssStyle styleBlock = cssParser.parseBlock(parsableWebvttData);
      if (styleBlock != null) {
        definedStyles.add(styleBlock);
      }
    } else if (event == EVENT_CUE) {
      if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
        subtitles.add(webvttCueBuilder.build());
        webvttCueBuilder.reset();
      }
    }
  }
  return new WebvttSubtitle(subtitles);
}
 
Example #25
Source File: WebvttDecoder.java    From no-player with Apache License 2.0 5 votes vote down vote up
@Override
protected WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  parsableWebvttData.reset(bytes, length);
  // Initialization for consistent starting state.
  webvttCueBuilder.reset();
  definedStyles.clear();

  // Validate the first line of the header, and skip the remainder.
  try {
    WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
  } catch (ParserException e) {
    throw new SubtitleDecoderException(e);
  }
  while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {
  }

  int event;
  ArrayList<WebvttCue> subtitles = new ArrayList<>();
  while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
    if (event == EVENT_COMMENT) {
      skipComment(parsableWebvttData);
    } else if (event == EVENT_STYLE_BLOCK) {
      if (!subtitles.isEmpty()) {
        throw new SubtitleDecoderException("A style block was found after the first cue.");
      }
      parsableWebvttData.readLine(); // Consume the "STYLE" header.
      WebvttCssStyle styleBlock = cssParser.parseBlock(parsableWebvttData);
      if (styleBlock != null) {
        definedStyles.add(styleBlock);
      }
    } else if (event == EVENT_CUE) {
      if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
        subtitles.add(webvttCueBuilder.build());
        webvttCueBuilder.reset();
      }
    }
  }
  return new WebvttSubtitle(subtitles);
}
 
Example #26
Source File: RendererErrorMapper.java    From no-player with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"PMD.StdCyclomaticComplexity", "PMD.CyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity", "PMD.NPathComplexity"})
static NoPlayer.PlayerError map(Exception rendererException, String message) {
    if (rendererException instanceof AudioSink.ConfigurationException) {
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.AUDIO_SINK_CONFIGURATION_ERROR, message);
    }

    if (rendererException instanceof AudioSink.InitializationException) {
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.AUDIO_SINK_INITIALISATION_ERROR, message);
    }

    if (rendererException instanceof AudioSink.WriteException) {
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.AUDIO_SINK_WRITE_ERROR, message);
    }

    if (rendererException instanceof AudioProcessor.UnhandledFormatException) {
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.AUDIO_UNHANDLED_FORMAT_ERROR, message);
    }

    if (rendererException instanceof AudioDecoderException) {
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.AUDIO_DECODER_ERROR, message);
    }

    if (rendererException instanceof MediaCodecRenderer.DecoderInitializationException) {
        MediaCodecRenderer.DecoderInitializationException decoderInitializationException =
                (MediaCodecRenderer.DecoderInitializationException) rendererException;
        String fullMessage = "decoder-name:" + decoderInitializationException.decoderName + ", "
                + "mimetype:" + decoderInitializationException.mimeType + ", "
                + "secureCodeRequired:" + decoderInitializationException.secureDecoderRequired + ", "
                + "diagnosticInfo:" + decoderInitializationException.diagnosticInfo + ", "
                + "exceptionMessage:" + message;
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.INITIALISATION_ERROR, fullMessage);
    }

    if (rendererException instanceof MediaCodecUtil.DecoderQueryException) {
        return new NoPlayerError(PlayerErrorType.DEVICE_MEDIA_CAPABILITIES, DetailErrorType.UNKNOWN, message);
    }

    if (rendererException instanceof SubtitleDecoderException) {
        return new NoPlayerError(PlayerErrorType.RENDERER_DECODER, DetailErrorType.DECODING_SUBTITLE_ERROR, message);
    }

    if (rendererException instanceof UnsupportedDrmException) {
        return mapUnsupportedDrmException((UnsupportedDrmException) rendererException, message);
    }

    if (rendererException instanceof DefaultDrmSessionManager.MissingSchemeDataException) {
        return new NoPlayerError(PlayerErrorType.DRM, DetailErrorType.CANNOT_ACQUIRE_DRM_SESSION_MISSING_SCHEME_FOR_REQUIRED_UUID_ERROR, message);
    }

    if (rendererException instanceof DrmSession.DrmSessionException) {
        return new NoPlayerError(PlayerErrorType.DRM, DetailErrorType.DRM_SESSION_ERROR, message);
    }

    if (rendererException instanceof KeysExpiredException) {
        return new NoPlayerError(PlayerErrorType.DRM, DetailErrorType.DRM_KEYS_EXPIRED_ERROR, message);
    }

    if (rendererException instanceof DecryptionException) {
        return new NoPlayerError(PlayerErrorType.CONTENT_DECRYPTION, DetailErrorType.FAIL_DECRYPT_DATA_DUE_NON_PLATFORM_COMPONENT_ERROR, message);
    }

    if (rendererException instanceof MediaCodec.CryptoException) {
        return mapCryptoException((MediaCodec.CryptoException) rendererException, message);
    }

    if (rendererException instanceof IllegalStateException) {
        return new NoPlayerError(PlayerErrorType.DRM, DetailErrorType.MEDIA_REQUIRES_DRM_SESSION_MANAGER_ERROR, message);
    }

    return new NoPlayerError(PlayerErrorType.UNKNOWN, DetailErrorType.UNKNOWN, message);
}
 
Example #27
Source File: TtmlDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private FrameAndTickRate parseFrameAndTickRates(XmlPullParser xmlParser)
    throws SubtitleDecoderException {
  int frameRate = DEFAULT_FRAME_RATE;
  String frameRateString = xmlParser.getAttributeValue(TTP, "frameRate");
  if (frameRateString != null) {
    frameRate = Integer.parseInt(frameRateString);
  }

  float frameRateMultiplier = 1;
  String frameRateMultiplierString = xmlParser.getAttributeValue(TTP, "frameRateMultiplier");
  if (frameRateMultiplierString != null) {
    String[] parts = Util.split(frameRateMultiplierString, " ");
    if (parts.length != 2) {
      throw new SubtitleDecoderException("frameRateMultiplier doesn't have 2 parts");
    }
    float numerator = Integer.parseInt(parts[0]);
    float denominator = Integer.parseInt(parts[1]);
    frameRateMultiplier = numerator / denominator;
  }

  int subFrameRate = DEFAULT_FRAME_AND_TICK_RATE.subFrameRate;
  String subFrameRateString = xmlParser.getAttributeValue(TTP, "subFrameRate");
  if (subFrameRateString != null) {
    subFrameRate = Integer.parseInt(subFrameRateString);
  }

  int tickRate = DEFAULT_FRAME_AND_TICK_RATE.tickRate;
  String tickRateString = xmlParser.getAttributeValue(TTP, "tickRate");
  if (tickRateString != null) {
    tickRate = Integer.parseInt(tickRateString);
  }
  return new FrameAndTickRate(frameRate * frameRateMultiplier, subFrameRate, tickRate);
}
 
Example #28
Source File: CeaDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void queueInputBuffer(SubtitleInputBuffer inputBuffer) throws SubtitleDecoderException {
  Assertions.checkArgument(inputBuffer == dequeuedInputBuffer);
  if (inputBuffer.isDecodeOnly()) {
    // We can drop this buffer early (i.e. before it would be decoded) as the CEA formats allow
    // for decoding to begin mid-stream.
    releaseInputBuffer(dequeuedInputBuffer);
  } else {
    dequeuedInputBuffer.queuedInputBufferCount = queuedInputBufferCount++;
    queuedInputBuffers.add(dequeuedInputBuffer);
  }
  dequeuedInputBuffer = null;
}
 
Example #29
Source File: CeaDecoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void queueInputBuffer(SubtitleInputBuffer inputBuffer) throws SubtitleDecoderException {
  Assertions.checkArgument(inputBuffer == dequeuedInputBuffer);
  if (inputBuffer.isDecodeOnly()) {
    // We can drop this buffer early (i.e. before it would be decoded) as the CEA formats allow
    // for decoding to begin mid-stream.
    releaseInputBuffer(dequeuedInputBuffer);
  } else {
    dequeuedInputBuffer.queuedInputBufferCount = queuedInputBufferCount++;
    queuedInputBuffers.add(dequeuedInputBuffer);
  }
  dequeuedInputBuffer = null;
}
 
Example #30
Source File: WebvttDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  parsableWebvttData.reset(bytes, length);
  // Initialization for consistent starting state.
  webvttCueBuilder.reset();
  definedStyles.clear();

  // Validate the first line of the header, and skip the remainder.
  WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
  while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {}

  int event;
  ArrayList<WebvttCue> subtitles = new ArrayList<>();
  while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
    if (event == EVENT_COMMENT) {
      skipComment(parsableWebvttData);
    } else if (event == EVENT_STYLE_BLOCK) {
      if (!subtitles.isEmpty()) {
        throw new SubtitleDecoderException("A style block was found after the first cue.");
      }
      parsableWebvttData.readLine(); // Consume the "STYLE" header.
      WebvttCssStyle styleBlock = cssParser.parseBlock(parsableWebvttData);
      if (styleBlock != null) {
        definedStyles.add(styleBlock);
      }
    } else if (event == EVENT_CUE) {
      if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
        subtitles.add(webvttCueBuilder.build());
        webvttCueBuilder.reset();
      }
    }
  }
  return new WebvttSubtitle(subtitles);
}