Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PruthiviRaj27 committed Jan 2, 2023
1 parent a897aff commit 259fd0a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/tpstream/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.tpstream.player.OfflineDownloadResolution
import com.tpstream.player.TpInitParams
import com.tpstream.player.TpStreamDownloadManager

Expand Down Expand Up @@ -55,7 +56,7 @@ class MainActivity : AppCompatActivity() {
.setOrgCode("demoveranda")
.build()

TpStreamDownloadManager(this).startDownloads(listOf(drmVideo,aesEncryptedVideo,clearVideo),2)
TpStreamDownloadManager(this).startDownloads(listOf(drmVideo,aesEncryptedVideo,clearVideo),OfflineDownloadResolution.HIGH)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TpStreamDownloadManager(val context: Context) {
return offlineVideoInfo.url
}

fun startDownloads(paramsList: List<TpInitParams>,videoResolution:Int){
fun startDownloads(paramsList: List<TpInitParams>,resolution:OfflineDownloadResolution = OfflineDownloadResolution.VERY_LOW){
CoroutineScope(Dispatchers.IO).launch {
val downloadedList = offlineVideoInfoRepository.getAllDownloads()
val paramsList1 = if (downloadedList != null){
Expand All @@ -48,7 +48,7 @@ class TpStreamDownloadManager(val context: Context) {
paramsList.toMutableList()
}
for (params in paramsList1){
val videoDownloadRequestCreationHandler = VideoDownloadRequestCreationHandler(context,null,params,videoResolution).init()
val videoDownloadRequestCreationHandler = VideoDownloadRequestCreationHandler(context,null,params,resolution.height).init()
videoDownloadRequestCreationHandler.setOnDownloadRequestCreation { downloadRequest, videoInfo ->
DownloadTask(context).start(downloadRequest)
saveOfflineVideoInfo(params,videoInfo)
Expand All @@ -65,4 +65,11 @@ class TpStreamDownloadManager(val context: Context) {
OfflineVideoInfoRepository(context).insert(offlineVideoInfo)
}
}
}

enum class OfflineDownloadResolution(val height:Int){
VERY_LOW(240),
LOW(360),
MEDIUM(480),
HIGH(720),
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class VideoDownloadRequestCreationHandler(
val context: Context,
private val player: TpStreamPlayer? = null,
private var params: TpInitParams? = null,
private val downloadResolution: Int = 0
private val downloadResolutionHeight: Int = 240
) :
DownloadHelper.Callback, DRMLicenseFetchCallback {
private lateinit var downloadHelper: DownloadHelper
Expand Down Expand Up @@ -117,7 +117,7 @@ class VideoDownloadRequestCreationHandler(
}
return
}
createDownloadOverrides(downloadResolution)
createDownloadOverrides(downloadResolutionHeight)
} else {
val videoOrAudioData = VideoPlayerUtil.getAudioOrVideoInfoWithDrmInitData(helper)
val isDRMProtectedVideo = videoOrAudioData != null
Expand Down Expand Up @@ -155,7 +155,7 @@ class VideoDownloadRequestCreationHandler(
val trackGroup = downloadHelper.getTracks(0).groups
val overrides = trackSelectionParameters.overrides.toMutableMap()
val trackInfos = getTrackInfos(trackGroup)
val resolution = trackInfos[int]
val resolution = trackInfos[getResolutionIndex(int,trackGroup)]
val mediaTrackGroup: TrackGroup = resolution.trackGroup.mediaTrackGroup
overrides.clear()
overrides[mediaTrackGroup] =
Expand All @@ -176,6 +176,23 @@ class VideoDownloadRequestCreationHandler(
return trackInfos
}

private fun getResolutionIndex(resolutionHeight:Int,trackGroups: List<Tracks.Group>):Int{
val track = trackGroups[0]
for (index in 0 until track.mediaTrackGroup.length){
if (resolutionHeight == track.mediaTrackGroup.getFormat(index).height){
val format = track.mediaTrackGroup.getFormat(index)
return track.mediaTrackGroup.indexOf(format)
}
}
for (index in 0 until track.mediaTrackGroup.length){
if (240 == track.mediaTrackGroup.getFormat(index).height){
val format = track.mediaTrackGroup.getFormat(index)
return track.mediaTrackGroup.indexOf(format)
}
}
return 0
}

fun buildDownloadRequest(overrides: MutableMap<TrackGroup, TrackSelectionOverride>): DownloadRequest {
setSelectedTracks(overrides)
val name = videoInfo.title!!
Expand All @@ -195,7 +212,7 @@ class VideoDownloadRequestCreationHandler(

override fun onLicenseFetchSuccess(keySetId: ByteArray) {
this.keySetId = keySetId
createDownloadOverrides(downloadResolution)
createDownloadOverrides(downloadResolutionHeight)
CoroutineScope(Dispatchers.Main).launch {
Log.d("TAG", "onLicenseFetchSuccess: Success")
listener?.onDownloadRequestHandlerPrepared(true, downloadHelper)
Expand Down

0 comments on commit 259fd0a

Please sign in to comment.