com.google.android.exoplayer2.upstream.DataSourceInputStream Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.DataSourceInputStream. 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: SampleChooserActivity.java    From ExoPlayer-Offline with Apache License 2.0 6 votes vote down vote up
@Override
protected List<SampleGroup> doInBackground(String... uris) {
  List<SampleGroup> result = new ArrayList<>();
  Context context = getApplicationContext();
  String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
  DataSource dataSource = new DefaultDataSource(context, null, userAgent, false);
  for (String uri : uris) {
    DataSpec dataSpec = new DataSpec(Uri.parse(uri));
    InputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      readSampleGroups(new JsonReader(new InputStreamReader(inputStream, "UTF-8")), result);
    } catch (Exception e) {
      Log.e(TAG, "Error loading sample list: " + uri, e);
      sawError = true;
    } finally {
      Util.closeQuietly(dataSource);
    }
  }
  return result;
}
 
Example #2
Source File: CustomDrmCallback.java    From ExoPlayer-Offline with Apache License 2.0 6 votes vote down vote up
private byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws IOException {
    HttpDataSource dataSource = dataSourceFactory.createDataSource();
    if (requestProperties != null) {
        for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
            dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
        }
    }
    DataSpec dataSpec = new DataSpec(Uri.parse(url), data, 0, 0, C.LENGTH_UNSET, null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
        return Util.toByteArray(inputStream);
    } finally {
        Util.closeQuietly(inputStream);
    }
}
 
Example #3
Source File: HttpMediaDrmCallback.java    From K-Sonic with MIT License 6 votes vote down vote up
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
    byte[] data, Map<String, String> requestProperties) throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }
  DataSpec dataSpec = new DataSpec(Uri.parse(url), data, 0, 0, C.LENGTH_UNSET, null,
      DataSpec.FLAG_ALLOW_GZIP);
  DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
  try {
    return Util.toByteArray(inputStream);
  } finally {
    Util.closeQuietly(inputStream);
  }
}
 
Example #4
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
    byte[] data, Map<String, String> requestProperties) throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      url = manuallyRedirect ? getRedirectUrl(e) : null;
      if (url == null) {
        throw e;
      }
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #5
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
    byte[] data, Map<String, String> requestProperties) throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      url = manuallyRedirect ? getRedirectUrl(e) : null;
      if (url == null) {
        throw e;
      }
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #6
Source File: DashUtil.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Loads a DASH manifest.
 *
 * @param dataSource The {@link HttpDataSource} from which the manifest should be read.
 * @param manifestUriString The URI of the manifest to be read.
 * @return An instance of {@link DashManifest}.
 * @throws IOException If an error occurs reading data from the stream.
 * @see DashManifestParser
 */
public static DashManifest loadManifest(DataSource dataSource, String manifestUriString)
    throws IOException {
  DataSourceInputStream inputStream = new DataSourceInputStream(dataSource,
      new DataSpec(Uri.parse(manifestUriString), DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH));
  try {
    inputStream.open();
    DashManifestParser parser = new DashManifestParser();
    return parser.parse(dataSource.getUri(), inputStream);
  } finally {
    inputStream.close();
  }
}
 
Example #7
Source File: HttpMediaDrmCallback.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static byte[] executePost(
    HttpDataSource.Factory dataSourceFactory,
    String url,
    @Nullable byte[] httpBody,
    @Nullable Map<String, String> requestProperties)
    throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            DataSpec.HTTP_METHOD_POST,
            httpBody,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      String redirectUrl = manuallyRedirect ? getRedirectUrl(e) : null;
      if (redirectUrl == null) {
        throw e;
      }
      url = redirectUrl;
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #8
Source File: HttpMediaDrmCallback.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private static byte[] executePost(
    HttpDataSource.Factory dataSourceFactory,
    String url,
    byte[] data,
    @Nullable Map<String, String> requestProperties)
    throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      String redirectUrl = manuallyRedirect ? getRedirectUrl(e) : null;
      if (redirectUrl == null) {
        throw e;
      }
      url = redirectUrl;
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #9
Source File: HttpMediaDrmCallback.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private static byte[] executePost(
    HttpDataSource.Factory dataSourceFactory,
    String url,
    byte[] data,
    @Nullable Map<String, String> requestProperties)
    throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      String redirectUrl = manuallyRedirect ? getRedirectUrl(e) : null;
      if (redirectUrl == null) {
        throw e;
      }
      url = redirectUrl;
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}