Skip to content

Commit

Permalink
[APT-9577] Add DRM for downloads for offline usage : when remove a do…
Browse files Browse the repository at this point in the history
…wnloaded audio, release and remove the downloaded license and its download info
  • Loading branch information
rubeus90 committed Feb 12, 2024
1 parent 60fe940 commit 114ee3b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ internal class ExoplayerDownloadEngine @Inject constructor(
})
}

override fun removeDownload(audiobook: AudioPlayable) = downloadManager.removeDownload(audiobook.request.url)
override fun removeDownload(audiobook: AudioPlayable) {
downloadManager.removeDownload(audiobook.request.url)
offlineDrmManager.removeDownloadedDrmLicense(audiobook)
}

override fun removeAllDownloads() = downloadManager.removeAllDownloads()
override fun removeAllDownloads() {
downloadManager.removeAllDownloads()
offlineDrmManager.removeAllDownloadedDrmLicenses()
}

override fun updateProgress() = downloadTracker.updateProgress()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ internal class DashDrmLicenseDownloader @Inject constructor(context: Context) :
}
}

override suspend fun releaseDrmLicense(drmDownload: DrmDownload) {
val offlineHelper = when (drmDownload.drmType) {
DrmType.WIDEVINE -> OfflineLicenseHelper.newWidevineInstance(drmDownload.licenseServer, drmDataSourceFactory, drmEventDispatcher)
}
try {
offlineHelper.releaseLicense(drmDownload.drmKeyId)
} catch (e: Exception) {
Log.e(DrmLicenseDownloader.TAG, "Failure to release downloaded DRM license", e)
throw DrmDownloadException(e)
}
}

private fun DefaultHttpDataSource.Factory.addCustomHeaders(customHeaders: Map<String, String>) {
customHeaders.takeIf { it.isNotEmpty() }?.let { headers ->
setDefaultRequestProperties(headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ internal interface DrmLicenseDownloader {
customRequestHeaders: Map<String, String>,
drmInfo: DrmInfo,
): DrmDownload

/**
* Release a downloaded DRM license so it's no longer valid for usage.
*/
suspend fun releaseDrmLicense(drmDownload: DrmDownload)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.scribd.armadillo.models.AudioPlayable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
Expand Down Expand Up @@ -48,4 +49,41 @@ internal class OfflineDrmManager @Inject constructor(
}
}
}

fun removeDownloadedDrmLicense(audiobook: AudioPlayable) {
globalScope.launch(Dispatchers.IO) {
audiobook.request.drmInfo?.let { drmInfo ->
secureStorage.getDrmDownload(context, audiobook.request.url, drmInfo.drmType)?.let { drmDownload ->
// Remove the persisted download info immediately so audio playback would stop using the offline license
secureStorage.removeDrmDownload(context, audiobook.request.url, drmInfo.drmType)

// Release the DRM license
when (val type = drmDownload.audioType) {
C.TYPE_DASH -> dashDrmLicenseDownloader
else -> throw DrmContentTypeUnsupportedException(type)
}.releaseDrmLicense(drmDownload)
}
}
}
}

fun removeAllDownloadedDrmLicenses() {
globalScope.launch(Dispatchers.IO) {
// Make sure that a removal fails, it won't affect the removal of other licenses
supervisorScope {
secureStorage.getAllDrmDownloads(context).forEach { drmDownloadPair ->
launch {
// Remove the persisted download info immediately so audio playback would stop using the offline license
secureStorage.removeDrmDownload(context, drmDownloadPair.key)

// Release the DRM license
when (val type = drmDownloadPair.value.audioType) {
C.TYPE_DASH -> dashDrmLicenseDownloader
else -> throw DrmContentTypeUnsupportedException(type)
}.releaseDrmLicense(drmDownloadPair.value)
}
}
}
}
}
}

0 comments on commit 114ee3b

Please sign in to comment.