Skip to content

Commit

Permalink
Add default implementations for ExoMediaDrm.Provider
Browse files Browse the repository at this point in the history
Issue:#4721
PiperOrigin-RevId: 269378440
  • Loading branch information
AquilesCanta authored and ojw28 committed Sep 16, 2019
1 parent 5630273 commit 6ae3e09
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ interface Provider<T extends ExoMediaCrypto> {
ExoMediaDrm<T> acquireExoMediaDrm(UUID uuid);
}

/**
* {@link Provider} implementation which provides an {@link ExoMediaDrm} instance owned by the
* app.
*
* <p>This provider should be used to manually handle {@link ExoMediaDrm} resources.
*/
final class AppManagedProvider<T extends ExoMediaCrypto> implements Provider<T> {

private final ExoMediaDrm<T> exoMediaDrm;

/** Creates an instance, which provides the given {@link ExoMediaDrm}. */
public AppManagedProvider(ExoMediaDrm<T> exoMediaDrm) {
this.exoMediaDrm = exoMediaDrm;
}

@Override
public ExoMediaDrm<T> acquireExoMediaDrm(UUID uuid) {
exoMediaDrm.acquire();
return exoMediaDrm;
}
}

/** @see MediaDrm#EVENT_KEY_REQUIRED */
@SuppressWarnings("InlinedApi")
int EVENT_KEY_REQUIRED = MediaDrm.EVENT_KEY_REQUIRED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
@TargetApi(23)
public final class FrameworkMediaDrm implements ExoMediaDrm<FrameworkMediaCrypto> {

/**
* {@link ExoMediaDrm.Provider} that returns a new {@link FrameworkMediaDrm} for the requested
* UUID. Returns a {@link DummyExoMediaDrm} if the protection scheme identified by the given UUID
* is not supported by the device.
*
* <p>This provider should be used to make ExoPlayer handle {@link ExoMediaDrm} resources.
*/
public static final Provider<FrameworkMediaCrypto> DEFAULT_PROVIDER =
uuid -> {
try {
return newInstance(uuid);
} catch (UnsupportedDrmException e) {
return new DummyExoMediaDrm<>();
}
};

private static final String CENC_SCHEME_MIME_TYPE = "cenc";
private static final String MOCK_LA_URL_VALUE = "https://x";
private static final String MOCK_LA_URL = "<LA_URL>" + MOCK_LA_URL_VALUE + "</LA_URL>";
Expand All @@ -56,9 +72,11 @@ public final class FrameworkMediaDrm implements ExoMediaDrm<FrameworkMediaCrypto

private final UUID uuid;
private final MediaDrm mediaDrm;
private int referenceCount;

/**
* Creates an instance for the specified scheme UUID.
* Creates an instance with an {@link #acquire() acquired reference} for the specified scheme
* UUID.
*
* @param uuid The scheme uuid.
* @return The created instance.
Expand All @@ -79,6 +97,8 @@ private FrameworkMediaDrm(UUID uuid) throws UnsupportedSchemeException {
Assertions.checkArgument(!C.COMMON_PSSH_UUID.equals(uuid), "Use C.CLEARKEY_UUID instead");
this.uuid = uuid;
this.mediaDrm = new MediaDrm(adjustUuid(uuid));
// Creators of an instance automatically acquire ownership of the created instance.
referenceCount = 1;
if (C.WIDEVINE_UUID.equals(uuid) && needsForceWidevineL3Workaround()) {
forceWidevineL3(mediaDrm);
}
Expand Down Expand Up @@ -186,13 +206,16 @@ public Map<String, String> queryKeyStatus(byte[] sessionId) {
}

@Override
public void acquire() {
// TODO: Implement reference counting.
public synchronized void acquire() {
Assertions.checkState(referenceCount > 0);
referenceCount++;
}

@Override
public void release() {
mediaDrm.release();
public synchronized void release() {
if (--referenceCount == 0) {
mediaDrm.release();
}
}

@Override
Expand Down

0 comments on commit 6ae3e09

Please sign in to comment.