static SessionId absent() { return new SessionId(new byte[0]); }
@Override public SessionId getSessionId() { return SessionId.absent(); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } LocalDrmSession that = (LocalDrmSession) o; if (mediaCrypto != null ? !mediaCrypto.equals(that.mediaCrypto) : that.mediaCrypto != null) { return false; } if (keySetIdToRestore != null ? !keySetIdToRestore.equals(that.keySetIdToRestore) : that.keySetIdToRestore != null) { return false; } return sessionId != null ? sessionId.equals(that.sessionId) : that.sessionId == null; }
@Override public void releaseSession(DrmSession<FrameworkMediaCrypto> drmSession) { FrameworkDrmSession frameworkDrmSession = (FrameworkDrmSession) drmSession; SessionId sessionId = frameworkDrmSession.getSessionId(); mediaDrm.closeSession(sessionId.asBytes()); } }
@SuppressWarnings("PMD.AvoidCatchingGenericException") // We are forced to catch Exception as ResourceBusyException is minSdk 19 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Override public DrmSession<FrameworkMediaCrypto> acquireSession(Looper playbackLooper, DrmInitData drmInitData) { DrmSession<FrameworkMediaCrypto> drmSession; try { SessionId sessionId = SessionId.of(mediaDrm.openSession()); FrameworkMediaCrypto mediaCrypto = mediaDrm.createMediaCrypto(sessionId.asBytes()); mediaDrm.restoreKeys(sessionId.asBytes(), keySetIdToRestore.asBytes()); drmSession = new LocalDrmSession(mediaCrypto, keySetIdToRestore, sessionId); } catch (Exception exception) { drmSession = new InvalidDrmSession(new DrmSession.DrmSessionException(exception)); notifyErrorListener(drmSession); } return drmSession; }
@Override public int hashCode() { int result = mediaCrypto != null ? mediaCrypto.hashCode() : 0; result = 31 * result + (keySetIdToRestore != null ? keySetIdToRestore.hashCode() : 0); result = 31 * result + (sessionId != null ? sessionId.hashCode() : 0); return result; } }
@Test public void givenAcquiredSession_whenReleasingSession_thenClosesCurrentSession() { DrmSession<FrameworkMediaCrypto> drmSession = new LocalDrmSession(frameworkMediaCrypto, KEY_SET_ID_TO_RESTORE, SESSION_ID); localDrmSessionManager.releaseSession(drmSession); verify(mediaDrm).closeSession(SESSION_ID.asBytes()); } }
@Test public void givenValidMediaDrm_whenAcquiringSession_thenRestoresKeys() throws MediaCryptoException { given(mediaDrm.createMediaCrypto(SESSION_ID.asBytes())).willReturn(frameworkMediaCrypto); localDrmSessionManager.acquireSession(IGNORED_LOOPER, IGNORED_DRM_DATA); verify(mediaDrm).restoreKeys(SESSION_ID.asBytes(), KEY_SET_ID_TO_RESTORE.asBytes()); }
static SessionId of(byte[] sessionId) { return new SessionId(Arrays.copyOf(sessionId, sessionId.length)); }
@Before public void setUp() throws MediaDrmException, MediaCryptoException { frameworkMediaCrypto = FrameworkMediaCryptoFixture.aFrameworkMediaCrypto().build(); given(mediaDrm.openSession()).willReturn(SESSION_ID.asBytes()); localDrmSessionManager = new LocalDrmSessionManager( KEY_SET_ID_TO_RESTORE, mediaDrm, DRM_SCHEME, handler, eventListener ); }
@Test public void givenValidMediaDrm_whenAcquiringSession_thenReturnsLocalDrmSession() throws MediaCryptoException { given(mediaDrm.createMediaCrypto(SESSION_ID.asBytes())).willReturn(frameworkMediaCrypto); DrmSession<FrameworkMediaCrypto> drmSession = localDrmSessionManager.acquireSession(IGNORED_LOOPER, IGNORED_DRM_DATA); LocalDrmSession localDrmSession = new LocalDrmSession(frameworkMediaCrypto, KEY_SET_ID_TO_RESTORE, SESSION_ID); assertThat(drmSession).isEqualTo(localDrmSession); }