Search in sources :

Example 1 with DataSourceInputStream

use of androidx.media3.datasource.DataSourceInputStream in project media by androidx.

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(androidx.media3.datasource.DataSourceInputStream)

Example 2 with DataSourceInputStream

use of androidx.media3.datasource.DataSourceInputStream in project media by androidx.

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(androidx.media3.test.utils.FakeDataSource)

Example 3 with DataSourceInputStream

use of androidx.media3.datasource.DataSourceInputStream in project media by androidx.

the class ParsingLoadable method load.

@Override
public final void load() throws IOException {
    // We always load from the beginning, so reset bytesRead to 0.
    dataSource.resetBytesRead();
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
        inputStream.open();
        Uri dataSourceUri = Assertions.checkNotNull(dataSource.getUri());
        result = parser.parse(dataSourceUri, inputStream);
    } finally {
        Util.closeQuietly(inputStream);
    }
}
Also used : Uri(android.net.Uri) DataSourceInputStream(androidx.media3.datasource.DataSourceInputStream)

Example 4 with DataSourceInputStream

use of androidx.media3.datasource.DataSourceInputStream in project media by androidx.

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(androidx.media3.datasource.DataSourceInputStream)

Example 5 with DataSourceInputStream

use of androidx.media3.datasource.DataSourceInputStream in project media by androidx.

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(androidx.media3.datasource.StatsDataSource) DataSpec(androidx.media3.datasource.DataSpec) InvalidResponseCodeException(androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException) InvalidResponseCodeException(androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException) DataSourceInputStream(androidx.media3.datasource.DataSourceInputStream)

Aggregations

DataSourceInputStream (androidx.media3.datasource.DataSourceInputStream)4 Uri (android.net.Uri)1 DataSpec (androidx.media3.datasource.DataSpec)1 InvalidResponseCodeException (androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException)1 StatsDataSource (androidx.media3.datasource.StatsDataSource)1 FakeDataSource (androidx.media3.test.utils.FakeDataSource)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