-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from scribd/ngoc/APT-9577-addDrmForDownloads
Add DRM support for download content
- Loading branch information
Showing
15 changed files
with
478 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Armadillo/src/main/java/com/scribd/armadillo/download/drm/DashDrmLicenseDownloader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.scribd.armadillo.download.drm | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import com.google.android.exoplayer2.C | ||
import com.google.android.exoplayer2.drm.DrmSessionEventListener | ||
import com.google.android.exoplayer2.drm.OfflineLicenseHelper | ||
import com.google.android.exoplayer2.source.dash.DashUtil | ||
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource | ||
import com.google.android.exoplayer2.util.Log | ||
import com.scribd.armadillo.Constants | ||
import com.scribd.armadillo.error.DrmDownloadException | ||
import com.scribd.armadillo.models.DrmDownload | ||
import com.scribd.armadillo.models.DrmInfo | ||
import com.scribd.armadillo.models.DrmType | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
internal class DashDrmLicenseDownloader @Inject constructor(context: Context) : DrmLicenseDownloader { | ||
|
||
private val drmDataSourceFactory = DefaultHttpDataSource.Factory().setUserAgent(Constants.getUserAgent(context)) | ||
private val audioDataSourceFactory = DefaultHttpDataSource.Factory().setUserAgent(Constants.getUserAgent(context)) | ||
private val drmEventDispatcher = DrmSessionEventListener.EventDispatcher() | ||
|
||
override suspend fun downloadDrmLicense( | ||
requestUrl: String, | ||
customRequestHeaders: Map<String, String>, | ||
drmInfo: DrmInfo, | ||
): DrmDownload { | ||
// Update data source for DRM license to add any DRM-specific request headers | ||
drmDataSourceFactory.setDefaultRequestProperties(drmInfo.drmHeaders) | ||
// Update data source for audio to add custom headers | ||
audioDataSourceFactory.setDefaultRequestProperties(customRequestHeaders) | ||
|
||
// Create helper to download DRM license | ||
val offlineHelper = when (drmInfo.drmType) { | ||
DrmType.WIDEVINE -> OfflineLicenseHelper.newWidevineInstance(drmInfo.licenseServer, drmDataSourceFactory, drmEventDispatcher) | ||
} | ||
return try { | ||
val audioDataSource = audioDataSourceFactory.createDataSource() | ||
val manifest = DashUtil.loadManifest(audioDataSource, Uri.parse(requestUrl)) | ||
val format = DashUtil.loadFormatWithDrmInitData(audioDataSource, manifest.getPeriod(0)) | ||
format?.let { | ||
DrmDownload( | ||
drmKeyId = offlineHelper.downloadLicense(format), | ||
drmType = drmInfo.drmType, | ||
licenseServer = drmInfo.licenseServer, | ||
audioType = C.TYPE_DASH, | ||
) | ||
} ?: throw IllegalStateException("No media format retrieved for audio request") | ||
} catch (e: Exception) { | ||
Log.e(DrmLicenseDownloader.TAG, "Failure to download DRM license for offline usage", e) | ||
throw DrmDownloadException(e) | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Armadillo/src/main/java/com/scribd/armadillo/download/drm/DrmLicenseDownloader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.scribd.armadillo.download.drm | ||
|
||
import com.scribd.armadillo.models.DrmDownload | ||
import com.scribd.armadillo.models.DrmInfo | ||
|
||
/** | ||
* This is a helper class responsible for downloading the DRM license to local storage for a DRM-protected content. | ||
* This downloaded license can then be retrieved for offline usage using its key ID. | ||
*/ | ||
internal interface DrmLicenseDownloader { | ||
companion object { | ||
const val TAG = "DrmLicenseDownloader" | ||
} | ||
|
||
/** | ||
* Download and persist the DRM license | ||
* @return object containing information about the downloaded DRM license | ||
*/ | ||
suspend fun downloadDrmLicense( | ||
requestUrl: String, | ||
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) | ||
} |
Oops, something went wrong.