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