From 0d77e3c66c179b49c9029bf74eec2d094188066d Mon Sep 17 00:00:00 2001 From: OlegRyz Date: Fri, 6 Sep 2024 16:15:25 +0200 Subject: [PATCH] Enable ping scheduler only if ping feature is requested. --- .../uplynk/internal/PingScheduler.kt | 4 +-- .../uplynk/internal/UplynkAdIntegration.kt | 23 +++++++++------ .../uplynk/internal/UplynkPingFeatures.kt | 28 +++++++++++++++++++ .../UplynkSsaiDescriptionConverter.kt | 16 ++--------- 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkPingFeatures.kt diff --git a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/PingScheduler.kt b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/PingScheduler.kt index 1470ac46..adfccaac 100644 --- a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/PingScheduler.kt +++ b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/PingScheduler.kt @@ -31,8 +31,8 @@ internal class PingScheduler( } } - fun onStart() = - performPing(uplynkDescriptionConverter.buildStartPingUrl(prefix, sessionId, Duration.ZERO)) + fun onStart(time: Duration) = + performPing(uplynkDescriptionConverter.buildStartPingUrl(prefix, sessionId, time)) fun onSeeking(time: Duration) { diff --git a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkAdIntegration.kt b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkAdIntegration.kt index 2d775a36..697f1938 100644 --- a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkAdIntegration.kt +++ b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkAdIntegration.kt @@ -43,6 +43,10 @@ internal class UplynkAdIntegration( player.addEventListener(PlayerEventTypes.SEEKED) { pingScheduler?.onSeeked(it.currentTime.toDuration(DurationUnit.SECONDS)) } + + player.addEventListener(PlayerEventTypes.PLAY) { + pingScheduler?.onStart(it.currentTime.toDuration(DurationUnit.SECONDS)) + } } override suspend fun resetSource() { @@ -80,15 +84,16 @@ internal class UplynkAdIntegration( add(0, newUplynkSource) }) - pingScheduler = PingScheduler( - uplynkApi, - uplynkDescriptionConverter, - minimalResponse.prefix, - minimalResponse.sid, - eventDispatcher, - adScheduler!! - ) - pingScheduler?.onStart() + if (UplynkPingFeatures.from(ssaiDescription) != UplynkPingFeatures.NO_PING) { + pingScheduler = PingScheduler( + uplynkApi, + uplynkDescriptionConverter, + minimalResponse.prefix, + minimalResponse.sid, + eventDispatcher, + adScheduler!! + ) + } if (ssaiDescription.assetInfo) { uplynkDescriptionConverter diff --git a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkPingFeatures.kt b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkPingFeatures.kt new file mode 100644 index 00000000..734f43d4 --- /dev/null +++ b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkPingFeatures.kt @@ -0,0 +1,28 @@ +package com.theoplayer.android.connector.uplynk.internal + +import com.theoplayer.android.connector.uplynk.UplynkAssetType +import com.theoplayer.android.connector.uplynk.UplynkSsaiDescription + +enum class UplynkPingFeatures(val pingfValue: Int) { + NO_PING(0), + AD_IMPRESSIONS(1), + FW_VIDEO_VIEWS(2), + AD_IMPRESSIONS_AND_FW_VIDEO_VIEWS(3), + LINEAR_AD_DATA(4); + + companion object { + fun from(ssaiDescription: UplynkSsaiDescription): UplynkPingFeatures { + val isVod = ssaiDescription.assetType == UplynkAssetType.ASSET + with(ssaiDescription.pingConfiguration) { + return when { + isVod && adImpressions && freeWheelVideoViews -> AD_IMPRESSIONS_AND_FW_VIDEO_VIEWS + isVod && adImpressions -> AD_IMPRESSIONS + isVod && freeWheelVideoViews -> FW_VIDEO_VIEWS + !isVod && linearAdData -> LINEAR_AD_DATA + else -> NO_PING + } + } + } + } + +} \ No newline at end of file diff --git a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkSsaiDescriptionConverter.kt b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkSsaiDescriptionConverter.kt index cc401f97..a5903f1e 100644 --- a/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkSsaiDescriptionConverter.kt +++ b/connectors/uplynk/src/main/java/com/theoplayer/android/connector/uplynk/internal/UplynkSsaiDescriptionConverter.kt @@ -4,10 +4,6 @@ import com.theoplayer.android.connector.uplynk.UplynkAssetType import com.theoplayer.android.connector.uplynk.UplynkSsaiDescription import kotlin.time.Duration -private const val AD_IMPRESSIONS = 1 -private const val FW_VIDEO_VIEWS = 2 -private const val LINEAR_AD_DATA = 4 - internal class UplynkSsaiDescriptionConverter { private val DEFAULT_PREFIX = "https://content.uplynk.com" @@ -44,17 +40,11 @@ internal class UplynkSsaiDescriptionConverter { private val UplynkSsaiDescription.pingParameters: String get() { - val isLive = assetType == UplynkAssetType.ASSET - - val features = with(pingConfiguration) { - (AD_IMPRESSIONS.takeIf { !isLive && adImpressions } ?: 0) + - (FW_VIDEO_VIEWS.takeIf { !isLive && freeWheelVideoViews } ?: 0) + - (LINEAR_AD_DATA.takeIf { isLive && linearAdData } ?: 0) - } - return if (features == 0) { + val feature = UplynkPingFeatures.from(this) + return if (feature == UplynkPingFeatures.NO_PING) { "ad.pingc=0" } else { - "ad.pingc=1&ad.pingf=$features" + "ad.pingc=1&ad.pingf=${feature.pingfValue}" } }