Skip to content

Commit

Permalink
Introduce LoadErrorHandling policy in DefaultDrmSession
Browse files Browse the repository at this point in the history
This is a no-op interim change to introduce key request error
handling customization. Following changes will allow users to
inject their own LoadErrorHandlingPolicy implementations.

Issue:#6334
PiperOrigin-RevId: 266344399
  • Loading branch information
AquilesCanta authored and ojw28 committed Aug 30, 2019
1 parent 4855555 commit 9508146
Showing 1 changed file with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import com.google.android.exoplayer2.drm.DrmInitData.SchemeData;
import com.google.android.exoplayer2.drm.ExoMediaDrm.KeyRequest;
import com.google.android.exoplayer2.drm.ExoMediaDrm.ProvisionRequest;
import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.EventDispatcher;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -47,6 +50,14 @@
@TargetApi(18)
public class DefaultDrmSession<T extends ExoMediaCrypto> implements DrmSession<T> {

/** Thrown when an unexpected exception or error is thrown during provisioning or key requests. */
public static final class UnexpectedDrmSessionException extends IOException {

public UnexpectedDrmSessionException(Throwable cause) {
super("Unexpected " + cause.getClass().getSimpleName() + ": " + cause.getMessage(), cause);
}
}

/** Manages provisioning requests. */
public interface ProvisioningManager<T extends ExoMediaCrypto> {

Expand Down Expand Up @@ -97,7 +108,7 @@ public interface ReleaseCallback<T extends ExoMediaCrypto> {
private final @DefaultDrmSessionManager.Mode int mode;
@Nullable private final HashMap<String, String> optionalKeyRequestParameters;
private final EventDispatcher<DefaultDrmSessionEventListener> eventDispatcher;
private final int initialDrmRequestRetryCount;
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;

/* package */ final MediaDrmCallback callback;
/* package */ final UUID uuid;
Expand Down Expand Up @@ -164,8 +175,10 @@ public DefaultDrmSession(
}
this.optionalKeyRequestParameters = optionalKeyRequestParameters;
this.callback = callback;
this.initialDrmRequestRetryCount = initialDrmRequestRetryCount;
this.eventDispatcher = eventDispatcher;
loadErrorHandlingPolicy =
new DefaultLoadErrorHandlingPolicy(
/* minimumLoadableRetryCount= */ initialDrmRequestRetryCount);
state = STATE_OPENING;
postResponseHandler = new PostResponseHandler(playbackLooper);
}
Expand Down Expand Up @@ -531,31 +544,35 @@ public void handleMessage(Message msg) {
throw new RuntimeException();
}
} catch (Exception e) {
if (maybeRetryRequest(msg)) {
if (maybeRetryRequest(msg, e)) {
return;
}
response = e;
}
postResponseHandler.obtainMessage(msg.what, Pair.create(request, response)).sendToTarget();
}

private boolean maybeRetryRequest(Message originalMsg) {
private boolean maybeRetryRequest(Message originalMsg, Exception e) {
boolean allowRetry = originalMsg.arg1 == 1;
if (!allowRetry) {
return false;
}
int errorCount = originalMsg.arg2 + 1;
if (errorCount > initialDrmRequestRetryCount) {
if (errorCount > loadErrorHandlingPolicy.getMinimumLoadableRetryCount(C.DATA_TYPE_DRM)) {
return false;
}
Message retryMsg = Message.obtain(originalMsg);
retryMsg.arg2 = errorCount;
sendMessageDelayed(retryMsg, getRetryDelayMillis(errorCount));
return true;
}

private long getRetryDelayMillis(int errorCount) {
return Math.min((errorCount - 1) * 1000, 5000);
IOException ioException =
e instanceof IOException ? (IOException) e : new UnexpectedDrmSessionException(e);
// TODO: Add loadDurationMs calculation before allowing user-provided load error handling
// policies.
long retryDelayMs =
loadErrorHandlingPolicy.getRetryDelayMsFor(
C.DATA_TYPE_DRM, /* loadDurationMs= */ C.TIME_UNSET, ioException, errorCount);
sendMessageDelayed(retryMsg, retryDelayMs);
return true;
}
}
}

0 comments on commit 9508146

Please sign in to comment.