Search in sources :

Example 1 with DataSourceInputStream

use of com.google.android.exoplayer2.upstream.DataSourceInputStream in project ExoPlayer by google.

the class HttpMediaDrmCallback method executePost.

private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url, @Nullable byte[] httpBody, Map<String, String> requestProperties) throws MediaDrmCallbackException {
    StatsDataSource dataSource = new StatsDataSource(dataSourceFactory.createDataSource());
    int manualRedirectCount = 0;
    DataSpec dataSpec = new DataSpec.Builder().setUri(url).setHttpRequestHeaders(requestProperties).setHttpMethod(DataSpec.HTTP_METHOD_POST).setHttpBody(httpBody).setFlags(DataSpec.FLAG_ALLOW_GZIP).build();
    DataSpec originalDataSpec = dataSpec;
    try {
        while (true) {
            DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
            try {
                return Util.toByteArray(inputStream);
            } catch (InvalidResponseCodeException e) {
                @Nullable String redirectUrl = getRedirectUrl(e, manualRedirectCount);
                if (redirectUrl == null) {
                    throw e;
                }
                manualRedirectCount++;
                dataSpec = dataSpec.buildUpon().setUri(redirectUrl).build();
            } finally {
                Util.closeQuietly(inputStream);
            }
        }
    } catch (Exception e) {
        throw new MediaDrmCallbackException(originalDataSpec, Assertions.checkNotNull(dataSource.getLastOpenedUri()), dataSource.getResponseHeaders(), dataSource.getBytesRead(), /* cause= */
        e);
    }
}
Also used : StatsDataSource(com.google.android.exoplayer2.upstream.StatsDataSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) InvalidResponseCodeException(com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException) InvalidResponseCodeException(com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Example 2 with DataSourceInputStream

use of com.google.android.exoplayer2.upstream.DataSourceInputStream in project ExoPlayer by google.

the class DataSourceInputStreamTest method buildTestInputStream.

private static DataSourceInputStream buildTestInputStream() {
    FakeDataSource fakeDataSource = new FakeDataSource();
    fakeDataSource.getDataSet().newDefaultData().appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 5)).appendReadData(Arrays.copyOfRange(TEST_DATA, 5, 10)).appendReadData(Arrays.copyOfRange(TEST_DATA, 10, 15)).appendReadData(Arrays.copyOfRange(TEST_DATA, 15, TEST_DATA.length));
    return new DataSourceInputStream(fakeDataSource, new DataSpec(Uri.EMPTY));
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource)

Example 3 with DataSourceInputStream

use of com.google.android.exoplayer2.upstream.DataSourceInputStream in project ExoPlayer by google.

the class Aes128DataSource method open.

@Override
public final long open(DataSpec dataSpec) throws IOException {
    Cipher cipher;
    try {
        cipher = getCipherInstance();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
    Key cipherKey = new SecretKeySpec(encryptionKey, "AES");
    AlgorithmParameterSpec cipherIV = new IvParameterSpec(encryptionIv);
    try {
        cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherIV);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }
    DataSourceInputStream inputStream = new DataSourceInputStream(upstream, dataSpec);
    cipherInputStream = new CipherInputStream(inputStream, cipher);
    inputStream.open();
    return C.LENGTH_UNSET;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Example 4 with DataSourceInputStream

use of com.google.android.exoplayer2.upstream.DataSourceInputStream in project ExoPlayer by google.

the class CacheAsserts method assertReadData.

/**
 * Asserts that the read data from {@code dataSource} specified by {@code dataSpec} is equal to
 * {@code expected} or not.
 *
 * @throws IOException If an error occurred reading from the Cache.
 */
public static void assertReadData(DataSource dataSource, DataSpec dataSpec, byte[] expected) throws IOException {
    try (DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec)) {
        byte[] bytes = Util.toByteArray(inputStream);
        assertThat(bytes).isEqualTo(expected);
    }
}
Also used : DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Example 5 with DataSourceInputStream

use of com.google.android.exoplayer2.upstream.DataSourceInputStream in project ExoPlayer by google.

the class DashUtil method loadManifest.

/**
   * 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();
    }
}
Also used : DashManifestParser(com.google.android.exoplayer2.source.dash.manifest.DashManifestParser) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Aggregations

DataSourceInputStream (com.google.android.exoplayer2.upstream.DataSourceInputStream)4 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)2 DashManifestParser (com.google.android.exoplayer2.source.dash.manifest.DashManifestParser)1 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)1 InvalidResponseCodeException (com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException)1 StatsDataSource (com.google.android.exoplayer2.upstream.StatsDataSource)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 Key (java.security.Key)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 Cipher (javax.crypto.Cipher)1 CipherInputStream (javax.crypto.CipherInputStream)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1