Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance/uplynk refactoring #36

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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