Java Code Examples for com.google.android.exoplayer2.util.Util#split()

The following examples show how to use com.google.android.exoplayer2.util.Util#split() . 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: WebvttCueParser.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
public static StartTag buildStartTag(String fullTagExpression, int position) {
  fullTagExpression = fullTagExpression.trim();
  if (fullTagExpression.isEmpty()) {
    return null;
  }
  int voiceStartIndex = fullTagExpression.indexOf(" ");
  String voice;
  if (voiceStartIndex == -1) {
    voice = "";
  } else {
    voice = fullTagExpression.substring(voiceStartIndex).trim();
    fullTagExpression = fullTagExpression.substring(0, voiceStartIndex);
  }
  String[] nameAndClasses = Util.split(fullTagExpression, "\\.");
  String name = nameAndClasses[0];
  String[] classes;
  if (nameAndClasses.length > 1) {
    classes = Arrays.copyOfRange(nameAndClasses, 1, nameAndClasses.length);
  } else {
    classes = NO_CLASSES;
  }
  return new StartTag(name, position, voice, classes);
}
 
Example 2
Source File: CssParser.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets the target of a {@link WebvttCssStyle} by splitting a selector of the form
 * {@code ::cue(tag#id.class1.class2[voice="someone"]}, where every element is optional.
 */
private void applySelectorToStyle(WebvttCssStyle style, String selector) {
  if ("".equals(selector)) {
    return; // Universal selector.
  }
  int voiceStartIndex = selector.indexOf('[');
  if (voiceStartIndex != -1) {
    Matcher matcher = VOICE_NAME_PATTERN.matcher(selector.substring(voiceStartIndex));
    if (matcher.matches()) {
      style.setTargetVoice(matcher.group(1));
    }
    selector = selector.substring(0, voiceStartIndex);
  }
  String[] classDivision = Util.split(selector, "\\.");
  String tagAndIdDivision = classDivision[0];
  int idPrefixIndex = tagAndIdDivision.indexOf('#');
  if (idPrefixIndex != -1) {
    style.setTargetTagName(tagAndIdDivision.substring(0, idPrefixIndex));
    style.setTargetId(tagAndIdDivision.substring(idPrefixIndex + 1)); // We discard the '#'.
  } else {
    style.setTargetTagName(tagAndIdDivision);
  }
  if (classDivision.length > 1) {
    style.setTargetClasses(Arrays.copyOfRange(classDivision, 1, classDivision.length));
  }
}
 
Example 3
Source File: WebvttCueParser.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public static StartTag buildStartTag(String fullTagExpression, int position) {
  fullTagExpression = fullTagExpression.trim();
  if (fullTagExpression.isEmpty()) {
    return null;
  }
  int voiceStartIndex = fullTagExpression.indexOf(" ");
  String voice;
  if (voiceStartIndex == -1) {
    voice = "";
  } else {
    voice = fullTagExpression.substring(voiceStartIndex).trim();
    fullTagExpression = fullTagExpression.substring(0, voiceStartIndex);
  }
  String[] nameAndClasses = Util.split(fullTagExpression, "\\.");
  String name = nameAndClasses[0];
  String[] classes;
  if (nameAndClasses.length > 1) {
    classes = Arrays.copyOfRange(nameAndClasses, 1, nameAndClasses.length);
  } else {
    classes = NO_CLASSES;
  }
  return new StartTag(name, position, voice, classes);
}
 
Example 4
Source File: DefaultDownloadIndex.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private static List<StreamKey> decodeStreamKeys(String encodedStreamKeys) {
  ArrayList<StreamKey> streamKeys = new ArrayList<>();
  if (encodedStreamKeys.isEmpty()) {
    return streamKeys;
  }
  String[] streamKeysStrings = Util.split(encodedStreamKeys, ",");
  for (String streamKeysString : streamKeysStrings) {
    String[] indices = Util.split(streamKeysString, "\\.");
    Assertions.checkState(indices.length == 3);
    streamKeys.add(
        new StreamKey(
            Integer.parseInt(indices[0]),
            Integer.parseInt(indices[1]),
            Integer.parseInt(indices[2])));
  }
  return streamKeys;
}
 
Example 5
Source File: WebvttParserUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a WebVTT timestamp.
 *
 * @param timestamp The timestamp string.
 * @return The parsed timestamp in microseconds.
 * @throws NumberFormatException If the timestamp could not be parsed.
 */
public static long parseTimestampUs(String timestamp) throws NumberFormatException {
  long value = 0;
  String[] parts = Util.splitAtFirst(timestamp, "\\.");
  String[] subparts = Util.split(parts[0], ":");
  for (String subpart : subparts) {
    value = (value * 60) + Long.parseLong(subpart);
  }
  value *= 1000;
  if (parts.length == 2) {
    value += Long.parseLong(parts[1]);
  }
  return value * 1000;
}
 
Example 6
Source File: DataSchemeDataSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws IOException {
  transferInitializing(dataSpec);
  this.dataSpec = dataSpec;
  readPosition = (int) dataSpec.position;
  Uri uri = dataSpec.uri;
  String scheme = uri.getScheme();
  if (!SCHEME_DATA.equals(scheme)) {
    throw new ParserException("Unsupported scheme: " + scheme);
  }
  String[] uriParts = Util.split(uri.getSchemeSpecificPart(), ",");
  if (uriParts.length != 2) {
    throw new ParserException("Unexpected URI format: " + uri);
  }
  String dataString = uriParts[1];
  if (uriParts[0].contains(";base64")) {
    try {
      data = Base64.decode(dataString, 0);
    } catch (IllegalArgumentException e) {
      throw new ParserException("Error while parsing Base64 encoded string: " + dataString, e);
    }
  } else {
    // TODO: Add support for other charsets.
    data = Util.getUtf8Bytes(URLDecoder.decode(dataString, C.ASCII_NAME));
  }
  endPosition =
      dataSpec.length != C.LENGTH_UNSET ? (int) dataSpec.length + readPosition : data.length;
  if (endPosition > data.length || readPosition > endPosition) {
    data = null;
    throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE);
  }
  transferStarted(dataSpec);
  return (long) endPosition - readPosition;
}
 
Example 7
Source File: WebvttParserUtil.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a WebVTT timestamp.
 *
 * @param timestamp The timestamp string.
 * @return The parsed timestamp in microseconds.
 * @throws NumberFormatException If the timestamp could not be parsed.
 */
public static long parseTimestampUs(String timestamp) throws NumberFormatException {
  long value = 0;
  String[] parts = Util.splitAtFirst(timestamp, "\\.");
  String[] subparts = Util.split(parts[0], ":");
  for (String subpart : subparts) {
    value = (value * 60) + Long.parseLong(subpart);
  }
  value *= 1000;
  if (parts.length == 2) {
    value += Long.parseLong(parts[1]);
  }
  return value * 1000;
}
 
Example 8
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 9
Source File: WebvttParserUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a WebVTT timestamp.
 *
 * @param timestamp The timestamp string.
 * @return The parsed timestamp in microseconds.
 * @throws NumberFormatException If the timestamp could not be parsed.
 */
public static long parseTimestampUs(String timestamp) throws NumberFormatException {
  long value = 0;
  String[] parts = Util.splitAtFirst(timestamp, "\\.");
  String[] subparts = Util.split(parts[0], ":");
  for (String subpart : subparts) {
    value = (value * 60) + Long.parseLong(subpart);
  }
  value *= 1000;
  if (parts.length == 2) {
    value += Long.parseLong(parts[1]);
  }
  return value * 1000;
}
 
Example 10
Source File: DashMediaPeriod.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private static Format[] getCea608TrackFormats(
    List<AdaptationSet> adaptationSets, int[] adaptationSetIndices) {
  for (int i : adaptationSetIndices) {
    AdaptationSet adaptationSet = adaptationSets.get(i);
    List<Descriptor> descriptors = adaptationSets.get(i).accessibilityDescriptors;
    for (int j = 0; j < descriptors.size(); j++) {
      Descriptor descriptor = descriptors.get(j);
      if ("urn:scte:dash:cc:cea-608:2015".equals(descriptor.schemeIdUri)) {
        String value = descriptor.value;
        if (value == null) {
          // There are embedded CEA-608 tracks, but service information is not declared.
          return new Format[] {buildCea608TrackFormat(adaptationSet.id)};
        }
        String[] services = Util.split(value, ";");
        Format[] formats = new Format[services.length];
        for (int k = 0; k < services.length; k++) {
          Matcher matcher = CEA608_SERVICE_DESCRIPTOR_REGEX.matcher(services[k]);
          if (!matcher.matches()) {
            // If we can't parse service information for all services, assume a single track.
            return new Format[] {buildCea608TrackFormat(adaptationSet.id)};
          }
          formats[k] =
              buildCea608TrackFormat(
                  adaptationSet.id,
                  /* language= */ matcher.group(2),
                  /* accessibilityChannel= */ Integer.parseInt(matcher.group(1)));
        }
        return formats;
      }
    }
  }
  return new Format[0];
}
 
Example 11
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 12
Source File: TtmlDecoder.java    From Telegram 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 13
Source File: TtmlDecoder.java    From MediaSDK with Apache License 2.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 14
Source File: WebvttParserUtil.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a WebVTT timestamp.
 *
 * @param timestamp The timestamp string.
 * @return The parsed timestamp in microseconds.
 * @throws NumberFormatException If the timestamp could not be parsed.
 */
public static long parseTimestampUs(String timestamp) throws NumberFormatException {
  long value = 0;
  String[] parts = Util.splitAtFirst(timestamp, "\\.");
  String[] subparts = Util.split(parts[0], ":");
  for (String subpart : subparts) {
    value = (value * 60) + Long.parseLong(subpart);
  }
  value *= 1000;
  if (parts.length == 2) {
    value += Long.parseLong(parts[1]);
  }
  return value * 1000;
}
 
Example 15
Source File: DataSchemeDataSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws IOException {
  transferInitializing(dataSpec);
  this.dataSpec = dataSpec;
  readPosition = (int) dataSpec.position;
  Uri uri = dataSpec.uri;
  String scheme = uri.getScheme();
  if (!SCHEME_DATA.equals(scheme)) {
    throw new ParserException("Unsupported scheme: " + scheme);
  }
  String[] uriParts = Util.split(uri.getSchemeSpecificPart(), ",");
  if (uriParts.length != 2) {
    throw new ParserException("Unexpected URI format: " + uri);
  }
  String dataString = uriParts[1];
  if (uriParts[0].contains(";base64")) {
    try {
      data = Base64.decode(dataString, 0);
    } catch (IllegalArgumentException e) {
      throw new ParserException("Error while parsing Base64 encoded string: " + dataString, e);
    }
  } else {
    // TODO: Add support for other charsets.
    data = Util.getUtf8Bytes(URLDecoder.decode(dataString, C.ASCII_NAME));
  }
  endPosition =
      dataSpec.length != C.LENGTH_UNSET ? (int) dataSpec.length + readPosition : data.length;
  if (endPosition > data.length || readPosition > endPosition) {
    data = null;
    throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE);
  }
  transferStarted(dataSpec);
  return (long) endPosition - readPosition;
}
 
Example 16
Source File: TtmlDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private String[] parseStyleIds(String parentStyleIds) {
  parentStyleIds = parentStyleIds.trim();
  return parentStyleIds.isEmpty() ? new String[0] : Util.split(parentStyleIds, "\\s+");
}
 
Example 17
Source File: DashMediaPeriod.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static int[][] getGroupedAdaptationSetIndices(List<AdaptationSet> adaptationSets) {
  int adaptationSetCount = adaptationSets.size();
  SparseIntArray idToIndexMap = new SparseIntArray(adaptationSetCount);
  for (int i = 0; i < adaptationSetCount; i++) {
    idToIndexMap.put(adaptationSets.get(i).id, i);
  }

  int[][] groupedAdaptationSetIndices = new int[adaptationSetCount][];
  boolean[] adaptationSetUsedFlags = new boolean[adaptationSetCount];

  int groupCount = 0;
  for (int i = 0; i < adaptationSetCount; i++) {
    if (adaptationSetUsedFlags[i]) {
      // This adaptation set has already been included in a group.
      continue;
    }
    adaptationSetUsedFlags[i] = true;
    Descriptor adaptationSetSwitchingProperty = findAdaptationSetSwitchingProperty(
        adaptationSets.get(i).supplementalProperties);
    if (adaptationSetSwitchingProperty == null) {
      groupedAdaptationSetIndices[groupCount++] = new int[] {i};
    } else {
      String[] extraAdaptationSetIds = Util.split(adaptationSetSwitchingProperty.value, ",");
      int[] adaptationSetIndices = new int[1 + extraAdaptationSetIds.length];
      adaptationSetIndices[0] = i;
      int outputIndex = 1;
      for (String adaptationSetId : extraAdaptationSetIds) {
        int extraIndex =
            idToIndexMap.get(Integer.parseInt(adaptationSetId), /* valueIfKeyNotFound= */ -1);
        if (extraIndex != -1) {
          adaptationSetUsedFlags[extraIndex] = true;
          adaptationSetIndices[outputIndex] = extraIndex;
          outputIndex++;
        }
      }
      if (outputIndex < adaptationSetIndices.length) {
        adaptationSetIndices = Arrays.copyOf(adaptationSetIndices, outputIndex);
      }
      groupedAdaptationSetIndices[groupCount++] = adaptationSetIndices;
    }
  }

  return groupCount < adaptationSetCount
      ? Arrays.copyOf(groupedAdaptationSetIndices, groupCount) : groupedAdaptationSetIndices;
}
 
Example 18
Source File: TtmlDecoder.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private String[] parseStyleIds(String parentStyleIds) {
  parentStyleIds = parentStyleIds.trim();
  return parentStyleIds.isEmpty() ? new String[0] : Util.split(parentStyleIds, "\\s+");
}
 
Example 19
Source File: TtmlDecoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private String[] parseStyleIds(String parentStyleIds) {
  parentStyleIds = parentStyleIds.trim();
  return parentStyleIds.isEmpty() ? new String[0] : Util.split(parentStyleIds, "\\s+");
}
 
Example 20
Source File: TtmlDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private String[] parseStyleIds(String parentStyleIds) {
  parentStyleIds = parentStyleIds.trim();
  return parentStyleIds.isEmpty() ? new String[0] : Util.split(parentStyleIds, "\\s+");
}