Search in sources :

Example 11 with InvalidResponseCodeException

use of androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException 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

Nullable (androidx.annotation.Nullable)9 InvalidResponseCodeException (androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException)8 Test (org.junit.Test)6 DataSourceException (androidx.media3.datasource.DataSourceException)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 List (java.util.List)2 DataSourceInputStream (androidx.media3.datasource.DataSourceInputStream)1 DataSpec (androidx.media3.datasource.DataSpec)1 HttpDataSource (androidx.media3.datasource.HttpDataSource)1 StatsDataSource (androidx.media3.datasource.StatsDataSource)1 BaseUrl (androidx.media3.exoplayer.dash.manifest.BaseUrl)1 LoadEventInfo (androidx.media3.exoplayer.source.LoadEventInfo)1 MediaLoadData (androidx.media3.exoplayer.source.MediaLoadData)1 ContainerMediaChunk (androidx.media3.exoplayer.source.chunk.ContainerMediaChunk)1 MediaChunk (androidx.media3.exoplayer.source.chunk.MediaChunk)1 SingleSampleMediaChunk (androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk)1 LoadErrorHandlingPolicy (androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy)1 SocketTimeoutException (java.net.SocketTimeoutException)1 MediaType (okhttp3.MediaType)1