From 6aacbc6bbb11a5a55ec812cc93e0bb1b6810749e Mon Sep 17 00:00:00 2001 From: ibaker Date: Mon, 24 Apr 2023 10:41:13 +0100 Subject: [PATCH] Ensure `DrmSessionManager.setPlayer()` is called before `prepare()` `prepare()` now logs a warning if it's called before `setPlayer()` because it's not possible to tell if it's being called on the wrong thread (since https://github.com/androidx/media/commit/3480a27994ef9e06bd7574bad4656eb8c7677971). This change finds all the places one is called immediately after the other and flips the order to be more correct. Issue: androidx/media#350 #minor-release PiperOrigin-RevId: 526582294 --- .../source/ProgressiveMediaSource.java | 2 +- .../drm/DefaultDrmSessionManagerTest.java | 42 +++++++++---------- .../exoplayer/dash/DashMediaSource.java | 2 +- .../media3/exoplayer/hls/HlsMediaSource.java | 2 +- .../smoothstreaming/SsMediaSource.java | 2 +- .../media3/test/utils/FakeMediaSource.java | 2 +- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java index a97818f146..47757865ed 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java @@ -278,9 +278,9 @@ public MediaItem getMediaItem() { @Override protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { transferListener = mediaTransferListener; - drmSessionManager.prepare(); drmSessionManager.setPlayer( /* playbackLooper= */ checkNotNull(Looper.myLooper()), getPlayerId()); + drmSessionManager.prepare(); notifySourceInfoRefreshed(); } diff --git a/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/drm/DefaultDrmSessionManagerTest.java b/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/drm/DefaultDrmSessionManagerTest.java index 3bd3545968..1103ee7178 100644 --- a/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/drm/DefaultDrmSessionManagerTest.java +++ b/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/drm/DefaultDrmSessionManagerTest.java @@ -68,8 +68,8 @@ public void acquireSession_triggersKeyLoadAndSessionIsOpened() throws Exception new DefaultDrmSessionManager.Builder() .setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm()) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -91,8 +91,8 @@ public void keepaliveEnabled_sessionsKeptForRequestedTime() throws Exception { .setSessionKeepaliveMs(10_000) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -116,8 +116,8 @@ public void keepaliveDisabled_sessionsReleasedImmediately() throws Exception { .setSessionKeepaliveMs(C.TIME_UNSET) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -138,8 +138,8 @@ public void managerRelease_allKeepaliveSessionsImmediatelyReleased() throws Exce .setSessionKeepaliveMs(10_000) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -162,8 +162,8 @@ public void managerRelease_keepaliveDisabled_doesntReleaseAnySessions() throws E .setSessionKeepaliveMs(C.TIME_UNSET) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -188,8 +188,8 @@ public void managerRelease_mediaDrmNotReleasedUntilLastSessionReleased() throws .setSessionKeepaliveMs(10_000) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -233,8 +233,8 @@ public void managerRelease_mediaDrmNotReleasedUntilLastSessionReleased() throws .setSessionKeepaliveMs(10_000) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -272,8 +272,8 @@ public void maxConcurrentSessionsExceeded_allKeepAliveSessionsEagerlyReleased() .setMultiSession(true) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession firstDrmSession = checkNotNull( drmSessionManager.acquireSession( @@ -313,8 +313,8 @@ public void maxConcurrentSessionsExceeded_allPreacquiredAndKeepaliveSessionsEage .setMultiSession(true) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSessionReference firstDrmSessionReference = checkNotNull( drmSessionManager.preacquireSession( @@ -358,8 +358,8 @@ public void sessionReacquired_keepaliveTimeOutCancelled() throws Exception { .setSessionKeepaliveMs(10_000) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession firstDrmSession = checkNotNull( drmSessionManager.acquireSession( @@ -405,8 +405,8 @@ public void onDrmKeysLoaded( .setSessionKeepaliveMs(C.TIME_UNSET) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSessionReference sessionReference = drmSessionManager.preacquireSession(eventDispatcher, FORMAT_WITH_DRM_INIT_DATA); @@ -450,8 +450,8 @@ public void onDrmKeysLoaded( .setSessionKeepaliveMs(C.TIME_UNSET) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSessionReference sessionReference = drmSessionManager.preacquireSession(/* eventDispatcher= */ null, FORMAT_WITH_DRM_INIT_DATA); @@ -486,8 +486,8 @@ public void preacquireSession_releaseManagerBeforeAcquisition_acquisitionDoesntH .setSessionKeepaliveMs(C.TIME_UNSET) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSessionReference sessionReference = drmSessionManager.preacquireSession(/* eventDispatcher= */ null, FORMAT_WITH_DRM_INIT_DATA); @@ -530,8 +530,8 @@ public void keyRefreshEvent_triggersKeyRefresh() throws Exception { .setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, new AppManagedProvider(exoMediaDrm)) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DefaultDrmSession drmSession = (DefaultDrmSession) @@ -571,8 +571,8 @@ public void keyRefreshEvent_whileManagerIsReleasing_triggersKeyRefresh() throws .setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, new AppManagedProvider(exoMediaDrm)) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DefaultDrmSession drmSession = (DefaultDrmSession) @@ -615,8 +615,8 @@ public void keyRefreshEvent_whileManagerIsReleasing_triggersKeyRefresh() throws DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm.Builder().setProvisionsRequired(1).build()) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -648,8 +648,8 @@ public void keyRefreshEvent_whileManagerIsReleasing_triggersKeyRefresh() throws .throwNotProvisionedExceptionFromGetKeyRequest() .build()) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -674,8 +674,8 @@ public void deviceNotProvisioned_doubleProvisioningHandledAndOpenSessionRetried( DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm.Builder().setProvisionsRequired(2).build()) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -702,8 +702,8 @@ public void keyResponseIndicatesProvisioningRequired_provisioningDone() { .setUuidAndExoMediaDrmProvider( DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm.Builder().build()) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -728,8 +728,8 @@ public void provisioningUndoneWhileManagerIsActive_deviceReprovisioned() { .setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, new AppManagedProvider(mediaDrm)) .setSessionKeepaliveMs(C.TIME_UNSET) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( @@ -783,8 +783,8 @@ public void managerReleasing_acquireSessionAndPreacquireSessionFail() throws Exc .setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm()) .build(/* mediaDrmCallback= */ licenseServer); - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), PlayerId.UNSET); + drmSessionManager.prepare(); DrmSession drmSession = checkNotNull( drmSessionManager.acquireSession( diff --git a/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/DashMediaSource.java b/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/DashMediaSource.java index 0e8f472732..4936899d9d 100644 --- a/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/DashMediaSource.java +++ b/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/DashMediaSource.java @@ -449,8 +449,8 @@ public MediaItem getMediaItem() { @Override protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { this.mediaTransferListener = mediaTransferListener; - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), getPlayerId()); + drmSessionManager.prepare(); if (sideloadedManifest) { processManifest(false); } else { diff --git a/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/HlsMediaSource.java b/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/HlsMediaSource.java index 256cda9c79..57ad6b5d4c 100644 --- a/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/HlsMediaSource.java +++ b/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/HlsMediaSource.java @@ -417,9 +417,9 @@ public MediaItem getMediaItem() { @Override protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { this.mediaTransferListener = mediaTransferListener; - drmSessionManager.prepare(); drmSessionManager.setPlayer( /* playbackLooper= */ checkNotNull(Looper.myLooper()), getPlayerId()); + drmSessionManager.prepare(); MediaSourceEventListener.EventDispatcher eventDispatcher = createEventDispatcher(/* mediaPeriodId= */ null); playlistTracker.start( diff --git a/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/SsMediaSource.java b/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/SsMediaSource.java index b92deea4ac..edbb43c61d 100644 --- a/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/SsMediaSource.java +++ b/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/SsMediaSource.java @@ -374,8 +374,8 @@ public MediaItem getMediaItem() { @Override protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { this.mediaTransferListener = mediaTransferListener; - drmSessionManager.prepare(); drmSessionManager.setPlayer(/* playbackLooper= */ Looper.myLooper(), getPlayerId()); + drmSessionManager.prepare(); if (sideloadedManifest) { manifestLoaderErrorThrower = new LoaderErrorThrower.Dummy(); processManifest(); diff --git a/libraries/test_utils/src/main/java/androidx/media3/test/utils/FakeMediaSource.java b/libraries/test_utils/src/main/java/androidx/media3/test/utils/FakeMediaSource.java index a5150ac58c..1ecc9fec67 100644 --- a/libraries/test_utils/src/main/java/androidx/media3/test/utils/FakeMediaSource.java +++ b/libraries/test_utils/src/main/java/androidx/media3/test/utils/FakeMediaSource.java @@ -215,9 +215,9 @@ public boolean isSingleWindow() { public synchronized void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { assertThat(preparedSource).isFalse(); transferListener = mediaTransferListener; - drmSessionManager.prepare(); drmSessionManager.setPlayer( /* playbackLooper= */ checkNotNull(Looper.myLooper()), getPlayerId()); + drmSessionManager.prepare(); preparedSource = true; releasedSource = false; sourceInfoRefreshHandler = Util.createHandlerForCurrentLooper();