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

The following examples show how to use com.google.android.exoplayer2.util.Util#fromUtf8Bytes() . 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: 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 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 MediaSDK with Apache License 2.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 4
Source File: Mp4WebvttDecoder.java    From Telegram-FOSS 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 5
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 6
Source File: DownloadManager.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static String toString(byte[] data) {
  if (data.length > 100) {
    return "<data is too long>";
  } else {
    return '\'' + Util.fromUtf8Bytes(data) + '\'';
  }
}
 
Example 7
Source File: ClearKeyUtil.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adjusts ClearKey response data to be suitable for providing to the Android ClearKey CDM.
 *
 * @param response The response data.
 * @return The adjusted response data.
 */
public static byte[] adjustResponseData(byte[] response) {
  if (Util.SDK_INT >= 27) {
    return response;
  }
  // Prior to O-MR1 the ClearKey CDM expected Base64 encoding rather than Base64Url encoding for
  // the "k" and "kid" strings. See [Internal: b/64388098]. We know that the ClearKey CDM only
  // looks at the k, kid and kty parameters in each key, so can ignore the rest of the response.
  try {
    JSONObject responseJson = new JSONObject(Util.fromUtf8Bytes(response));
    StringBuilder adjustedResponseBuilder = new StringBuilder("{\"keys\":[");
    JSONArray keysArray = responseJson.getJSONArray("keys");
    for (int i = 0; i < keysArray.length(); i++) {
      if (i != 0) {
        adjustedResponseBuilder.append(",");
      }
      JSONObject key = keysArray.getJSONObject(i);
      adjustedResponseBuilder.append("{\"k\":\"");
      adjustedResponseBuilder.append(base64UrlToBase64(key.getString("k")));
      adjustedResponseBuilder.append("\",\"kid\":\"");
      adjustedResponseBuilder.append(base64UrlToBase64(key.getString("kid")));
      adjustedResponseBuilder.append("\",\"kty\":\"");
      adjustedResponseBuilder.append(key.getString("kty"));
      adjustedResponseBuilder.append("\"}");
    }
    adjustedResponseBuilder.append("]}");
    return Util.getUtf8Bytes(adjustedResponseBuilder.toString());
  } catch (JSONException e) {
    Log.e(TAG, "Failed to adjust response data: " + Util.fromUtf8Bytes(response), e);
    return response;
  }
}
 
Example 8
Source File: ClearKeyUtil.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adjusts ClearKey request data obtained from the Android ClearKey CDM to be spec compliant.
 *
 * @param request The request data.
 * @return The adjusted request data.
 */
public static byte[] adjustRequestData(byte[] request) {
  if (Util.SDK_INT >= 27) {
    return request;
  }
  // Prior to O-MR1 the ClearKey CDM encoded the values in the "kids" array using Base64 encoding
  // rather than Base64Url encoding. See [Internal: b/64388098]. We know the exact request format
  // from the platform's InitDataParser.cpp. Since there aren't any "+" or "/" symbols elsewhere
  // in the request, it's safe to fix the encoding by replacement through the whole request.
  String requestString = Util.fromUtf8Bytes(request);
  return Util.getUtf8Bytes(base64ToBase64Url(requestString));
}
 
Example 9
Source File: ClearKeyUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adjusts ClearKey request data obtained from the Android ClearKey CDM to be spec compliant.
 *
 * @param request The request data.
 * @return The adjusted request data.
 */
public static byte[] adjustRequestData(byte[] request) {
  if (Util.SDK_INT >= 27) {
    return request;
  }
  // Prior to O-MR1 the ClearKey CDM encoded the values in the "kids" array using Base64 encoding
  // rather than Base64Url encoding. See [Internal: b/64388098]. We know the exact request format
  // from the platform's InitDataParser.cpp. Since there aren't any "+" or "/" symbols elsewhere
  // in the request, it's safe to fix the encoding by replacement through the whole request.
  String requestString = Util.fromUtf8Bytes(request);
  return Util.getUtf8Bytes(base64ToBase64Url(requestString));
}
 
Example 10
Source File: DownloadManager.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static String toString(byte[] data) {
  if (data.length > 100) {
    return "<data is too long>";
  } else {
    return '\'' + Util.fromUtf8Bytes(data) + '\'';
  }
}
 
Example 11
Source File: SsaDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param initializationData Optional initialization data for the decoder. If not null or empty,
 *     the initialization data must consist of two byte arrays. The first must contain an SSA
 *     format line. The second must contain an SSA header that will be assumed common to all
 *     samples.
 */
public SsaDecoder(List<byte[]> initializationData) {
  super("SsaDecoder");
  if (initializationData != null && !initializationData.isEmpty()) {
    haveInitializationData = true;
    String formatLine = Util.fromUtf8Bytes(initializationData.get(0));
    Assertions.checkArgument(formatLine.startsWith(FORMAT_LINE_PREFIX));
    parseFormatLine(formatLine);
    parseHeader(new ParsableByteArray(initializationData.get(1)));
  } else {
    haveInitializationData = false;
  }
}
 
Example 12
Source File: Tx3gDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void decodeInitializationData(List<byte[]> initializationData) {
  if (initializationData != null && initializationData.size() == 1
      && (initializationData.get(0).length == 48 || initializationData.get(0).length == 53)) {
    byte[] initializationBytes = initializationData.get(0);
    defaultFontFace = initializationBytes[24];
    defaultColorRgba = ((initializationBytes[26] & 0xFF) << 24)
        | ((initializationBytes[27] & 0xFF) << 16)
        | ((initializationBytes[28] & 0xFF) << 8)
        | (initializationBytes[29] & 0xFF);
    String fontFamily =
        Util.fromUtf8Bytes(initializationBytes, 43, initializationBytes.length - 43);
    defaultFontFamily = TX3G_SERIF.equals(fontFamily) ? C.SERIF_NAME : C.SANS_SERIF_NAME;
    //font size (initializationBytes[25]) is 5% of video height
    calculatedVideoTrackHeight = 20 * initializationBytes[25];
    customVerticalPlacement = (initializationBytes[0] & 0x20) != 0;
    if (customVerticalPlacement) {
      int requestedVerticalPlacement = ((initializationBytes[10] & 0xFF) << 8)
          | (initializationBytes[11] & 0xFF);
      defaultVerticalPlacement = (float) requestedVerticalPlacement / calculatedVideoTrackHeight;
      defaultVerticalPlacement = Util.constrainValue(defaultVerticalPlacement, 0.0f, 0.95f);
    } else {
      defaultVerticalPlacement = DEFAULT_VERTICAL_PLACEMENT;
    }
  } else {
    defaultFontFace = DEFAULT_FONT_FACE;
    defaultColorRgba = DEFAULT_COLOR;
    defaultFontFamily = DEFAULT_FONT_FAMILY;
    customVerticalPlacement = false;
    defaultVerticalPlacement = DEFAULT_VERTICAL_PLACEMENT;
  }
}
 
Example 13
Source File: ClearKeyUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adjusts ClearKey response data to be suitable for providing to the Android ClearKey CDM.
 *
 * @param response The response data.
 * @return The adjusted response data.
 */
public static byte[] adjustResponseData(byte[] response) {
  if (Util.SDK_INT >= 27) {
    return response;
  }
  // Prior to O-MR1 the ClearKey CDM expected Base64 encoding rather than Base64Url encoding for
  // the "k" and "kid" strings. See [Internal: b/64388098]. We know that the ClearKey CDM only
  // looks at the k, kid and kty parameters in each key, so can ignore the rest of the response.
  try {
    JSONObject responseJson = new JSONObject(Util.fromUtf8Bytes(response));
    StringBuilder adjustedResponseBuilder = new StringBuilder("{\"keys\":[");
    JSONArray keysArray = responseJson.getJSONArray("keys");
    for (int i = 0; i < keysArray.length(); i++) {
      if (i != 0) {
        adjustedResponseBuilder.append(",");
      }
      JSONObject key = keysArray.getJSONObject(i);
      adjustedResponseBuilder.append("{\"k\":\"");
      adjustedResponseBuilder.append(base64UrlToBase64(key.getString("k")));
      adjustedResponseBuilder.append("\",\"kid\":\"");
      adjustedResponseBuilder.append(base64UrlToBase64(key.getString("kid")));
      adjustedResponseBuilder.append("\",\"kty\":\"");
      adjustedResponseBuilder.append(key.getString("kty"));
      adjustedResponseBuilder.append("\"}");
    }
    adjustedResponseBuilder.append("]}");
    return Util.getUtf8Bytes(adjustedResponseBuilder.toString());
  } catch (JSONException e) {
    Log.e(TAG, "Failed to adjust response data: " + Util.fromUtf8Bytes(response), e);
    return response;
  }
}
 
Example 14
Source File: SsaDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param initializationData Optional initialization data for the decoder. If not null or empty,
 *     the initialization data must consist of two byte arrays. The first must contain an SSA
 *     format line. The second must contain an SSA header that will be assumed common to all
 *     samples.
 */
public SsaDecoder(@Nullable List<byte[]> initializationData) {
  super("SsaDecoder");
  if (initializationData != null && !initializationData.isEmpty()) {
    haveInitializationData = true;
    String formatLine = Util.fromUtf8Bytes(initializationData.get(0));
    Assertions.checkArgument(formatLine.startsWith(FORMAT_LINE_PREFIX));
    parseFormatLine(formatLine);
    parseHeader(new ParsableByteArray(initializationData.get(1)));
  } else {
    haveInitializationData = false;
  }
}
 
Example 15
Source File: SsaDecoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param initializationData Optional initialization data for the decoder. If not null or empty,
 *     the initialization data must consist of two byte arrays. The first must contain an SSA
 *     format line. The second must contain an SSA header that will be assumed common to all
 *     samples.
 */
public SsaDecoder(@Nullable List<byte[]> initializationData) {
  super("SsaDecoder");
  if (initializationData != null && !initializationData.isEmpty()) {
    haveInitializationData = true;
    String formatLine = Util.fromUtf8Bytes(initializationData.get(0));
    Assertions.checkArgument(formatLine.startsWith(FORMAT_LINE_PREFIX));
    parseFormatLine(formatLine);
    parseHeader(new ParsableByteArray(initializationData.get(1)));
  } else {
    haveInitializationData = false;
  }
}
 
Example 16
Source File: ClearKeyUtil.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Adjusts ClearKey response data to be suitable for providing to the Android ClearKey CDM.
 *
 * @param response The response data.
 * @return The adjusted response data.
 */
public static byte[] adjustResponseData(byte[] response) {
  if (Util.SDK_INT >= 27) {
    return response;
  }
  // Prior to O-MR1 the ClearKey CDM expected Base64 encoding rather than Base64Url encoding for
  // the "k" and "kid" strings. See [Internal: b/64388098]. We know that the ClearKey CDM only
  // looks at the k, kid and kty parameters in each key, so can ignore the rest of the response.
  try {
    JSONObject responseJson = new JSONObject(Util.fromUtf8Bytes(response));
    StringBuilder adjustedResponseBuilder = new StringBuilder("{\"keys\":[");
    JSONArray keysArray = responseJson.getJSONArray("keys");
    for (int i = 0; i < keysArray.length(); i++) {
      if (i != 0) {
        adjustedResponseBuilder.append(",");
      }
      JSONObject key = keysArray.getJSONObject(i);
      adjustedResponseBuilder.append("{\"k\":\"");
      adjustedResponseBuilder.append(base64UrlToBase64(key.getString("k")));
      adjustedResponseBuilder.append("\",\"kid\":\"");
      adjustedResponseBuilder.append(base64UrlToBase64(key.getString("kid")));
      adjustedResponseBuilder.append("\",\"kty\":\"");
      adjustedResponseBuilder.append(key.getString("kty"));
      adjustedResponseBuilder.append("\"}");
    }
    adjustedResponseBuilder.append("]}");
    return Util.getUtf8Bytes(adjustedResponseBuilder.toString());
  } catch (JSONException e) {
    Log.e(TAG, "Failed to adjust response data: " + Util.fromUtf8Bytes(response), e);
    return response;
  }
}
 
Example 17
Source File: Tx3gDecoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets up a new {@link Tx3gDecoder} with default values.
 *
 * @param initializationData Sample description atom ('stsd') data with default subtitle styles.
 */
public Tx3gDecoder(List<byte[]> initializationData) {
  super("Tx3gDecoder");
  parsableByteArray = new ParsableByteArray();

  if (initializationData != null && initializationData.size() == 1
      && (initializationData.get(0).length == 48 || initializationData.get(0).length == 53)) {
    byte[] initializationBytes = initializationData.get(0);
    defaultFontFace = initializationBytes[24];
    defaultColorRgba = ((initializationBytes[26] & 0xFF) << 24)
        | ((initializationBytes[27] & 0xFF) << 16)
        | ((initializationBytes[28] & 0xFF) << 8)
        | (initializationBytes[29] & 0xFF);
    String fontFamily =
        Util.fromUtf8Bytes(initializationBytes, 43, initializationBytes.length - 43);
    defaultFontFamily = TX3G_SERIF.equals(fontFamily) ? C.SERIF_NAME : C.SANS_SERIF_NAME;
    //font size (initializationBytes[25]) is 5% of video height
    calculatedVideoTrackHeight = 20 * initializationBytes[25];
    customVerticalPlacement = (initializationBytes[0] & 0x20) != 0;
    if (customVerticalPlacement) {
      int requestedVerticalPlacement = ((initializationBytes[10] & 0xFF) << 8)
          | (initializationBytes[11] & 0xFF);
      defaultVerticalPlacement = (float) requestedVerticalPlacement / calculatedVideoTrackHeight;
      defaultVerticalPlacement = Util.constrainValue(defaultVerticalPlacement, 0.0f, 0.95f);
    } else {
      defaultVerticalPlacement = DEFAULT_VERTICAL_PLACEMENT;
    }
  } else {
    defaultFontFace = DEFAULT_FONT_FACE;
    defaultColorRgba = DEFAULT_COLOR;
    defaultFontFamily = DEFAULT_FONT_FAMILY;
    customVerticalPlacement = false;
    defaultVerticalPlacement = DEFAULT_VERTICAL_PLACEMENT;
  }
}
 
Example 18
Source File: HttpMediaDrmCallback.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public byte[] executeProvisionRequest(UUID uuid, ProvisionRequest request) throws IOException {
  String url =
      request.getDefaultUrl() + "&signedRequest=" + Util.fromUtf8Bytes(request.getData());
  return executePost(dataSourceFactory, url, Util.EMPTY_BYTE_ARRAY, null);
}
 
Example 19
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public byte[] executeProvisionRequest(UUID uuid, ProvisionRequest request) throws IOException {
  String url =
      request.getDefaultUrl() + "&signedRequest=" + Util.fromUtf8Bytes(request.getData());
  return executePost(dataSourceFactory, url, new byte[0], null);
}
 
Example 20
Source File: HttpMediaDrmCallback.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] executeProvisionRequest(UUID uuid, ProvisionRequest request) throws IOException {
  String url =
      request.getDefaultUrl() + "&signedRequest=" + Util.fromUtf8Bytes(request.getData());
  return executePost(dataSourceFactory, url, /* httpBody= */ null, /* requestProperties= */ null);
}