Skip to content

Commit

Permalink
Test commit add DRM support
Browse files Browse the repository at this point in the history
  • Loading branch information
kschults committed Nov 29, 2023
1 parent f5b1bcd commit ff69189
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.scribd.armadillo.playback

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.annotation.VisibleForTesting
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.PlaybackParameters
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.drm.DrmSessionEventListener
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.upstream.cache.Cache
import com.scribd.armadillo.ArmadilloPlayerChoreographer
import com.scribd.armadillo.Constants
Expand All @@ -28,6 +33,7 @@ import com.scribd.armadillo.models.Chapter
import com.scribd.armadillo.models.PlaybackState
import com.scribd.armadillo.playback.mediasource.MediaSourceRetriever
import com.scribd.armadillo.time.milliseconds
import java.lang.Exception
import javax.inject.Inject
import javax.inject.Named

Expand Down Expand Up @@ -74,6 +80,9 @@ internal interface AudioPlaybackEngine {
* @property downloadCache
*/
internal class ExoplayerPlaybackEngine(private var audioPlayable: AudioPlayable) : AudioPlaybackEngine {
private companion object {
const val TAG = "ExoplayerPlaybackEngine"
}

init {
Injector.mainComponent.inject(this)
Expand Down Expand Up @@ -124,6 +133,37 @@ internal class ExoplayerPlaybackEngine(private var audioPlayable: AudioPlayable)
exoPlayer = createExoplayerInstance(context, audioAttributes.exoPlayerAttrs)

val mediaSource = mediaSourceRetriever.generateMediaSource(audioPlayable.request, context)
mediaSource.addDrmEventListener(Handler(Looper.getMainLooper()), object : DrmSessionEventListener {
override fun onDrmSessionAcquired(windowIndex: Int, mediaPeriodId: MediaSource.MediaPeriodId?, state: Int) {
super.onDrmSessionAcquired(windowIndex, mediaPeriodId, state)
Log.d(TAG, "Drm session acquired")
}

override fun onDrmKeysLoaded(windowIndex: Int, mediaPeriodId: MediaSource.MediaPeriodId?) {
super.onDrmKeysLoaded(windowIndex, mediaPeriodId)
Log.d(TAG, "Drm keys loaded")
}

override fun onDrmSessionManagerError(windowIndex: Int, mediaPeriodId: MediaSource.MediaPeriodId?, error: Exception) {
Log.d(TAG, "Drm session manager error: $error")
super.onDrmSessionManagerError(windowIndex, mediaPeriodId, error)
}

override fun onDrmKeysRestored(windowIndex: Int, mediaPeriodId: MediaSource.MediaPeriodId?) {
super.onDrmKeysRestored(windowIndex, mediaPeriodId)
Log.d(TAG, "Drm keys restored")
}

override fun onDrmKeysRemoved(windowIndex: Int, mediaPeriodId: MediaSource.MediaPeriodId?) {
super.onDrmKeysRemoved(windowIndex, mediaPeriodId)
Log.d(TAG, "Drm keys removed")
}

override fun onDrmSessionReleased(windowIndex: Int, mediaPeriodId: MediaSource.MediaPeriodId?) {
super.onDrmSessionReleased(windowIndex, mediaPeriodId)
Log.d(TAG, "Drm session released")
}
})
exoPlayer.setMediaSource(mediaSource)
exoPlayer.prepare()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.scribd.armadillo.playback.mediasource

import android.content.Context
import android.util.Base64
import android.util.Log
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.offline.Download
import com.google.android.exoplayer2.offline.DownloadHelper
Expand All @@ -15,6 +18,8 @@ import com.scribd.armadillo.download.CacheManager
import com.scribd.armadillo.download.DownloadTracker
import com.scribd.armadillo.extensions.toUri
import com.scribd.armadillo.models.AudioPlayable
import org.json.JSONObject
import java.nio.charset.Charset
import javax.inject.Inject

internal class DashMediaSourceGenerator @Inject constructor(
Expand All @@ -31,9 +36,35 @@ internal class DashMediaSourceGenerator @Inject constructor(
}
}

val licenseHeader = mapOf(
"userId" to "296700362",
"sessionId" to "296700362-469909861",
"merchant" to "scribd"
)

val toString = JSONObject(licenseHeader).toString()
Log.d("Dash", "Has JSON: $toString")

val base64String = Base64.encode(
toString.toByteArray(),
Base64.DEFAULT
).toString(Charset.defaultCharset()).replace("\n", "")
Log.d("Dash", "Has base64: $base64String")

val headersMap = mapOf(
"x-dt-custom-data" to base64String
)
Log.d("Dash", "Headers map: $headersMap")
val mediaItem = MediaItem.Builder()
.setUri(request.url)

.setDrmConfiguration(MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID)
.setLicenseUri("https://lic.staging.drmtoday.com/license-proxy-widevine/cenc/?specConform=true")
.setLicenseRequestHeaders(
headersMap
)
.build()
)
.build()

return DashMediaSource.Factory(buildDataSourceFactory(context, request))
Expand Down

0 comments on commit ff69189

Please sign in to comment.