diff --git a/Armadillo/src/main/java/com/scribd/armadillo/playback/PlaybackEngine.kt b/Armadillo/src/main/java/com/scribd/armadillo/playback/PlaybackEngine.kt index 5935668..51fc52f 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/playback/PlaybackEngine.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/playback/PlaybackEngine.kt @@ -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 @@ -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 @@ -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) @@ -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() diff --git a/Armadillo/src/main/java/com/scribd/armadillo/playback/mediasource/DashMediaSourceGenerator.kt b/Armadillo/src/main/java/com/scribd/armadillo/playback/mediasource/DashMediaSourceGenerator.kt index eeb593b..7224644 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/playback/mediasource/DashMediaSourceGenerator.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/playback/mediasource/DashMediaSourceGenerator.kt @@ -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 @@ -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( @@ -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))