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;
}
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));
}
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);
}
}
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);
}
}
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);
}
}
Aggregations