Skip to content

Commit

Permalink
Merge pull request #36 from THEOplayer/maintenance/uplynk-refactoring
Browse files Browse the repository at this point in the history
Maintenance/uplynk refactoring
  • Loading branch information
hovig-theo authored Sep 27, 2024
2 parents e5bbec2 + ddd5ab5 commit 38904d9
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ import java.util.WeakHashMap
import kotlin.time.Duration
import kotlin.time.DurationUnit

private val Duration.secToMs: Int
get() = this.toInt(DurationUnit.MILLISECONDS)

@Suppress("UnstableApiUsage")
internal class AdHandler(private val controller: ServerSideAdIntegrationController) {
private val scheduledAds = WeakHashMap<UplynkAd, Ad>()

fun createAdBreak(adBreak: UplynkAdBreak) {
val adBreakInit = AdBreakInit(
timeOffset = adBreak.timeOffset.secToMs,
maxDuration = adBreak.duration.secToMs,
timeOffset = adBreak.timeOffset.inWholeSeconds.toInt(),
maxDuration = adBreak.duration.inWholeSeconds.toInt(),
customData = adBreak
)
val currentAdBreak = controller.createAdBreak(adBreakInit)
adBreak.ads.forEach {
val adInit = AdInit(
type = adBreak.type,
duration = it.duration.secToMs,
duration = it.duration.inWholeSeconds.toInt(),
customData = it
)
scheduledAds[it] = controller.createAd(adInit, currentAdBreak)
Expand Down Expand Up @@ -61,5 +58,4 @@ internal class AdHandler(private val controller: ServerSideAdIntegrationControll

controller.updateAdProgress(ad, progress)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.theoplayer.android.connector.uplynk.internal

import com.theoplayer.android.connector.uplynk.UplynkAssetType
import com.theoplayer.android.connector.uplynk.UplynkSsaiDescription
import kotlin.time.Duration

Expand All @@ -10,59 +9,15 @@ internal class UplynkSsaiDescriptionConverter {
fun buildPreplayVodUrl(ssaiDescription: UplynkSsaiDescription): String = with(ssaiDescription) {
val prefix = prefix ?: DEFAULT_PREFIX

var url = "$prefix/preplay/$urlAssetId?v=2"
if (ssaiDescription.contentProtected) {
url += "&manifest=mpd"
url += "&rmt=wv"
}

url += "&$pingParameters&$urlParameters"

return url
return "$prefix/preplay/$urlAssetId?v=2$drmParameters$pingParameters$urlParameters"
}

fun buildPreplayLiveUrl(ssaiDescription: UplynkSsaiDescription): String = with(ssaiDescription) {
val prefix = prefix ?: DEFAULT_PREFIX

var url = "$prefix/preplay/$urlAssetType/$urlAssetId?v=2"
if (ssaiDescription.contentProtected) {
url += "&manifest=mpd"
url += "&rmt=wv"
}

url += "&$pingParameters&$urlParameters"

return url
return "$prefix/preplay/$urlAssetType/$urlAssetId?v=2$drmParameters$pingParameters$urlParameters"
}

private val UplynkSsaiDescription.urlParameters
get() = preplayParameters.map { "${it.key}=${it.value}" }.joinToString("&")

private val UplynkSsaiDescription.pingParameters: String
get() {
val feature = UplynkPingFeatures.from(this)
return if (feature == UplynkPingFeatures.NO_PING) {
"ad.pingc=0"
} else {
"ad.pingc=1&ad.pingf=${feature.pingfValue}"
}
}

private val UplynkSsaiDescription.urlAssetType
get() = when (assetType) {
UplynkAssetType.ASSET -> ""
UplynkAssetType.CHANNEL -> "channel"
UplynkAssetType.EVENT -> "event"
}

private val UplynkSsaiDescription.urlAssetId
get() = when {
assetIds.isEmpty() && externalIds.size == 1 -> "$userId/${externalIds.first()}.json"
assetIds.isEmpty() && externalIds.size > 1 -> "$userId/${externalIds.joinToString(",")}/multiple.json"
assetIds.size == 1 -> "${assetIds.first()}.json"
else -> assetIds.joinToString(separator = ",") + "/multiple.json"
}

fun buildAssetInfoUrls(
ssaiDescription: UplynkSsaiDescription,
sessionId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.theoplayer.android.connector.uplynk.internal

import com.theoplayer.android.connector.uplynk.UplynkAssetType
import com.theoplayer.android.connector.uplynk.UplynkSsaiDescription

internal val UplynkSsaiDescription.drmParameters: String
get() = if (contentProtected) {
"&manifest=mpd&rmt=wv"
} else {
""
}

internal val UplynkSsaiDescription.urlParameters
get() = if (preplayParameters.isNotEmpty()) {
preplayParameters.map { "${it.key}=${it.value}" }.joinToString("&", prefix = "&")
} else {
""
}

internal val UplynkSsaiDescription.pingParameters: String
get() {
val feature = UplynkPingFeatures.from(this)
return if (feature == UplynkPingFeatures.NO_PING) {
"&ad.pingc=0"
} else {
"&ad.pingc=1&ad.pingf=${feature.pingfValue}"
}
}

internal val UplynkSsaiDescription.urlAssetType
get() = when (assetType) {
UplynkAssetType.ASSET -> ""
UplynkAssetType.CHANNEL -> "channel"
UplynkAssetType.EVENT -> "event"
}

internal val UplynkSsaiDescription.urlAssetId
get() = when {
assetIds.isEmpty() && externalIds.size == 1 -> "$userId/${externalIds.first()}.json"
assetIds.isEmpty() && externalIds.size > 1 -> "$userId/${externalIds.joinToString(",")}/multiple.json"
assetIds.size == 1 -> "${assetIds.first()}.json"
else -> assetIds.joinToString(separator = ",") + "/multiple.json"
}
Loading

0 comments on commit 38904d9

Please sign in to comment.