Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit adcddc6

Browse files
author
Jimmy Dahlqvist
committed
Reuse MediaCrypto objects
Reuse MediaCrypto objects for the Audio / Video rendering and only release the MediaCrypto when the Player is released. This to prevent the DRMSession from being incorrectly closed.
1 parent b1dd304 commit adcddc6

5 files changed

Lines changed: 63 additions & 8 deletions

File tree

library/src/main/java/com/sonymobile/android/media/internal/AudioThread.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public final class AudioThread extends CodecThread implements Clock {
6161

6262
private static final String TAG = "AudioThread";
6363

64+
private static final String MEDIA_CRYPTO_KEY = "AudioMediaCrypto";
65+
6466
private static final long MICROS_PER_SECOND = 1000000L;
6567

6668
private static final long INVALID_TIMESTAMP_LIMIT = 4293000000L;
@@ -559,7 +561,7 @@ private void doSetup(MediaFormat format) {
559561

560562
if (mDrmSession != null) {
561563
try {
562-
mMediaCrypto = mDrmSession.getMediaCrypto();
564+
mMediaCrypto = mDrmSession.getMediaCrypto(MEDIA_CRYPTO_KEY);
563565
} catch (IllegalStateException e) {
564566
if (LOGS_ENABLED) Log.e(TAG, "Exception when obtaining MediaCrypto", e);
565567
mCallbacks.obtainMessage(Player.MSG_CODEC_NOTIFY, CODEC_ERROR,
@@ -689,7 +691,8 @@ private void doStop() {
689691
mAudioTrack = null;
690692
}
691693
mCodec.release();
692-
if (mMediaCrypto != null) {
694+
if (mMediaCrypto != null && mDrmSession == null) {
695+
// Only release the MediaCrypto object if not handled by a DRMSession.
693696
mMediaCrypto.release();
694697
}
695698

library/src/main/java/com/sonymobile/android/media/internal/VideoThread.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public final class VideoThread extends VideoCodecThread {
5252

5353
private static final String TAG = "VideoThread";
5454

55+
private static final String MEDIA_CRYPTO_KEY = "VideoMediaCrypto";
56+
5557
private static final int LATE_FRAME_TIME_MS = -40;
5658

5759
private HandlerThread mEventThread;
@@ -250,7 +252,7 @@ private void doSetup(MediaFormat format) {
250252

251253
if (mDrmSession != null) {
252254
try {
253-
mMediaCrypto = mDrmSession.getMediaCrypto();
255+
mMediaCrypto = mDrmSession.getMediaCrypto(MEDIA_CRYPTO_KEY);
254256
} catch (IllegalStateException e) {
255257
if (LOGS_ENABLED)
256258
Log.e(TAG, "Exception when obtaining MediaCrypto", e);
@@ -380,7 +382,8 @@ private void doStop() {
380382
if (mCodec != null) {
381383
mCodec.release();
382384
}
383-
if (mMediaCrypto != null) {
385+
if (mMediaCrypto != null && mDrmSession == null) {
386+
// Only release the MediaCrypto object if not handled by a DRMSession.
384387
mMediaCrypto.release();
385388
}
386389

library/src/main/java/com/sonymobile/android/media/internal/drm/DrmSession.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,18 @@ public int getErrorCode() {
102102
* @throws MediaCryptoException
103103
* @throws IllegalStateException
104104
*/
105-
public abstract MediaCrypto getMediaCrypto() throws IllegalStateException,
105+
public abstract MediaCrypto getMediaCrypto(String key) throws IllegalStateException,
106106
MediaCryptoException;
107107

108+
/**
109+
* Release a MediaCrypto for the current DRM session.
110+
*
111+
* @return MediaCrypto
112+
* @throws MediaCryptoException
113+
* @throws IllegalStateException
114+
*/
115+
public abstract void releaseMediaCrypto(String key) throws MediaCryptoException;
116+
108117
/**
109118
* Get the MediaDrm used for the current DRM session.
110119
*

library/src/main/java/com/sonymobile/android/media/internal/drm/MarlinDrmSession.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,20 @@ public synchronized void open() throws DrmLicenseException {
8989
}
9090
}
9191

92-
public synchronized MediaCrypto getMediaCrypto() throws IllegalStateException{
92+
public synchronized MediaCrypto getMediaCrypto(String key) throws IllegalStateException{
9393
if (mState != STATE_OPENED && mState != STATE_OPENED_WITH_KEYS) {
9494
throw new IllegalStateException("Illegal state. Was a DRM session opened?");
9595
}
9696
return mMediaCrypto;
9797
}
9898

99+
public synchronized void releaseMediaCrypto(String key) throws IllegalStateException{
100+
if (mState != STATE_OPENED && mState != STATE_OPENED_WITH_KEYS) {
101+
throw new IllegalStateException("Illegal state. Was a DRM session opened?");
102+
}
103+
mMediaCrypto.release();
104+
}
105+
99106
public synchronized MediaDrm getMediaDrm() throws IllegalStateException{
100107
if (mState != STATE_OPENED && mState != STATE_OPENED_WITH_KEYS) {
101108
throw new IllegalStateException("Illegal state. Was a DRM session opened?");

library/src/main/java/com/sonymobile/android/media/internal/drm/MsDrmSession.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package com.sonymobile.android.media.internal.drm;
1717

18+
import java.util.HashMap;
1819
import java.util.Map;
20+
import java.util.Set;
1921
import java.util.UUID;
2022

2123
import android.media.MediaCrypto;
@@ -35,6 +37,8 @@ public class MsDrmSession extends DrmSession {
3537

3638
private static final String TAG = "MsDrmSession";
3739

40+
private HashMap<String, MediaCrypto> mMediaCryptoMap;
41+
3842
public MsDrmSession(Map<UUID, byte[]> psshInfo) throws UnsupportedSchemeException,
3943
IllegalArgumentException {
4044

@@ -45,6 +49,7 @@ public MsDrmSession(Map<UUID, byte[]> psshInfo) throws UnsupportedSchemeExceptio
4549
mPsshInfo = psshInfo;
4650
mMediaDrm = new MediaDrm(DrmUUID.PLAY_READY);
4751
mOutputController = null;
52+
mMediaCryptoMap = new HashMap<String, MediaCrypto>();
4853
}
4954

5055
@Override
@@ -80,13 +85,27 @@ public synchronized void open() throws DrmLicenseException {
8085
}
8186
}
8287

83-
public synchronized MediaCrypto getMediaCrypto() throws IllegalStateException,
88+
public synchronized MediaCrypto getMediaCrypto(String key) throws IllegalStateException,
8489
MediaCryptoException {
8590
if (mState != STATE_OPENED && mState != STATE_OPENED_WITH_KEYS) {
8691
throw new IllegalStateException("Illegal state. Was a DRM session opened?");
8792
}
8893

89-
return new MediaCrypto(DrmUUID.PLAY_READY, mSessionId);
94+
MediaCrypto mediaCrypto = mMediaCryptoMap.get(key);
95+
if (mediaCrypto == null) {
96+
mediaCrypto = new MediaCrypto(DrmUUID.PLAY_READY, mSessionId);
97+
mMediaCryptoMap.put(key, mediaCrypto);
98+
}
99+
100+
return mediaCrypto;
101+
}
102+
103+
public synchronized void releaseMediaCrypto(String key) throws MediaCryptoException {
104+
MediaCrypto mediaCrypto = mMediaCryptoMap.get(key);
105+
if (mediaCrypto != null) {
106+
mMediaCryptoMap.remove(key);
107+
mediaCrypto.release();
108+
}
90109
}
91110

92111
@Override
@@ -96,6 +115,9 @@ public synchronized void close() {
96115
if (DEBUG_ENABLED) Log.d(TAG, "Close count = " + mOpenCount + " state = " + mState);
97116

98117
if (mOpenCount == 0) {
118+
if(mMediaCryptoMap.size() != 0) {
119+
releaseAllMediaCryptos();
120+
}
99121
if (mSessionId != null) {
100122
mMediaDrm.closeSession(mSessionId);
101123
}
@@ -106,4 +128,15 @@ public synchronized void close() {
106128
}
107129
}
108130
}
131+
132+
private synchronized void releaseAllMediaCryptos() {
133+
if (DEBUG_ENABLED) Log.d(TAG, "Open MediaCryptos: " + mMediaCryptoMap.size());
134+
135+
Set<String> keys = mMediaCryptoMap.keySet();
136+
for (String key : keys) {
137+
MediaCrypto mediaCrypto = mMediaCryptoMap.get(key);
138+
mediaCrypto.release();
139+
}
140+
mMediaCryptoMap.clear();
141+
}
109142
}

0 commit comments

Comments
 (0)