Search in sources :

Example 1 with InvalidResponseCodeException

use of com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException 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 InvalidResponseCodeException

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

the class DefaultLoadErrorHandlingPolicyTest method getFallbackSelectionFor_responseCode500.

@Test
public void getFallbackSelectionFor_responseCode500() {
    InvalidResponseCodeException exception = buildInvalidResponseCodeException(500, "Internal server error");
    @Nullable LoadErrorHandlingPolicy.FallbackSelection defaultPolicyFallbackSelection = getDefaultPolicyFallbackSelection(exception, /* numberOfLocations= */
    1, /* numberOfExcludedLocations= */
    0, /* numberOfTracks= */
    10, /* numberOfExcludedTracks= */
    0);
    assertThat(defaultPolicyFallbackSelection.type).isEqualTo(FALLBACK_TYPE_TRACK);
    assertThat(defaultPolicyFallbackSelection.exclusionDurationMs).isEqualTo(DEFAULT_TRACK_EXCLUSION_MS);
    defaultPolicyFallbackSelection = getDefaultPolicyFallbackSelection(exception, /* numberOfLocations= */
    2, /* numberOfExcludedLocations= */
    0, /* numberOfTracks= */
    4, /* numberOfExcludedTracks= */
    1);
    assertThat(defaultPolicyFallbackSelection.type).isEqualTo(FALLBACK_TYPE_LOCATION);
    assertThat(defaultPolicyFallbackSelection.exclusionDurationMs).isEqualTo(DEFAULT_LOCATION_EXCLUSION_MS);
}
Also used : InvalidResponseCodeException(com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 3 with InvalidResponseCodeException

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

the class DefaultLoadErrorHandlingPolicyTest method getFallbackSelectionFor_responseCode403.

@Test
public void getFallbackSelectionFor_responseCode403() {
    InvalidResponseCodeException exception = buildInvalidResponseCodeException(403, "Forbidden");
    @Nullable LoadErrorHandlingPolicy.FallbackSelection defaultPolicyFallbackSelection = getDefaultPolicyFallbackSelection(exception, /* numberOfLocations= */
    1, /* numberOfExcludedLocations= */
    0, /* numberOfTracks= */
    10, /* numberOfExcludedTracks= */
    0);
    assertThat(defaultPolicyFallbackSelection.type).isEqualTo(FALLBACK_TYPE_TRACK);
    assertThat(defaultPolicyFallbackSelection.exclusionDurationMs).isEqualTo(DEFAULT_TRACK_EXCLUSION_MS);
    defaultPolicyFallbackSelection = getDefaultPolicyFallbackSelection(exception, /* numberOfLocations= */
    2, /* numberOfExcludedLocations= */
    0, /* numberOfTracks= */
    4, /* numberOfExcludedTracks= */
    1);
    assertThat(defaultPolicyFallbackSelection.type).isEqualTo(FALLBACK_TYPE_LOCATION);
    assertThat(defaultPolicyFallbackSelection.exclusionDurationMs).isEqualTo(DEFAULT_LOCATION_EXCLUSION_MS);
}
Also used : InvalidResponseCodeException(com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 4 with InvalidResponseCodeException

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

the class DefaultDashChunkSource method onChunkLoadError.

@Override
public boolean onChunkLoadError(Chunk chunk, boolean cancelable, LoadErrorHandlingPolicy.LoadErrorInfo loadErrorInfo, LoadErrorHandlingPolicy loadErrorHandlingPolicy) {
    if (!cancelable) {
        return false;
    }
    if (playerTrackEmsgHandler != null && playerTrackEmsgHandler.onChunkLoadError(chunk)) {
        return true;
    }
    // Workaround for missing segment at the end of the period
    if (!manifest.dynamic && chunk instanceof MediaChunk && loadErrorInfo.exception instanceof InvalidResponseCodeException && ((InvalidResponseCodeException) loadErrorInfo.exception).responseCode == 404) {
        RepresentationHolder representationHolder = representationHolders[trackSelection.indexOf(chunk.trackFormat)];
        long segmentCount = representationHolder.getSegmentCount();
        if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
            long lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
            if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
                missingLastSegment = true;
                return true;
            }
        }
    }
    int trackIndex = trackSelection.indexOf(chunk.trackFormat);
    RepresentationHolder representationHolder = representationHolders[trackIndex];
    @Nullable BaseUrl newBaseUrl = baseUrlExclusionList.selectBaseUrl(representationHolder.representation.baseUrls);
    if (newBaseUrl != null && !representationHolder.selectedBaseUrl.equals(newBaseUrl)) {
        // which will use the new base URL.
        return true;
    }
    LoadErrorHandlingPolicy.FallbackOptions fallbackOptions = createFallbackOptions(trackSelection, representationHolder.representation.baseUrls);
    if (!fallbackOptions.isFallbackAvailable(LoadErrorHandlingPolicy.FALLBACK_TYPE_TRACK) && !fallbackOptions.isFallbackAvailable(LoadErrorHandlingPolicy.FALLBACK_TYPE_LOCATION)) {
        return false;
    }
    @Nullable LoadErrorHandlingPolicy.FallbackSelection fallbackSelection = loadErrorHandlingPolicy.getFallbackSelectionFor(fallbackOptions, loadErrorInfo);
    if (fallbackSelection == null || !fallbackOptions.isFallbackAvailable(fallbackSelection.type)) {
        // Policy indicated to not use any fallback or a fallback type that is not available.
        return false;
    }
    boolean cancelLoad = false;
    if (fallbackSelection.type == LoadErrorHandlingPolicy.FALLBACK_TYPE_TRACK) {
        cancelLoad = trackSelection.blacklist(trackSelection.indexOf(chunk.trackFormat), fallbackSelection.exclusionDurationMs);
    } else if (fallbackSelection.type == LoadErrorHandlingPolicy.FALLBACK_TYPE_LOCATION) {
        baseUrlExclusionList.exclude(representationHolder.selectedBaseUrl, fallbackSelection.exclusionDurationMs);
        cancelLoad = true;
    }
    return cancelLoad;
}
Also used : ContainerMediaChunk(com.google.android.exoplayer2.source.chunk.ContainerMediaChunk) MediaChunk(com.google.android.exoplayer2.source.chunk.MediaChunk) SingleSampleMediaChunk(com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk) BaseUrl(com.google.android.exoplayer2.source.dash.manifest.BaseUrl) LoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy) InvalidResponseCodeException(com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException) Nullable(androidx.annotation.Nullable)

Example 5 with InvalidResponseCodeException

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

the class DefaultDashChunkSourceTest method createFakeLoadErrorInfo.

private LoadErrorHandlingPolicy.LoadErrorInfo createFakeLoadErrorInfo(DataSpec dataSpec, int httpResponseCode, int errorCount) {
    LoadEventInfo loadEventInfo = new LoadEventInfo(/* loadTaskId= */
    0, dataSpec, SystemClock.elapsedRealtime());
    MediaLoadData mediaLoadData = new MediaLoadData(C.DATA_TYPE_MEDIA);
    HttpDataSource.InvalidResponseCodeException invalidResponseCodeException = new HttpDataSource.InvalidResponseCodeException(httpResponseCode, /* responseMessage= */
    null, /* cause= */
    null, ImmutableMap.of(), dataSpec, new byte[0]);
    return new LoadErrorHandlingPolicy.LoadErrorInfo(loadEventInfo, mediaLoadData, invalidResponseCodeException, errorCount);
}
Also used : LoadEventInfo(com.google.android.exoplayer2.source.LoadEventInfo) MediaLoadData(com.google.android.exoplayer2.source.MediaLoadData) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource)

Aggregations

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