From 9cbab6dc0068a7ac9a7eaa32940f2efb91b966ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Fri, 20 Oct 2023 13:37:17 +0200 Subject: [PATCH] Send pause when no surface is connected to player (#267) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Samuel Défago --- .../tracker/comscore/ComScoreTracker.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTracker.kt b/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTracker.kt index c0e127c0d..7a5615757 100644 --- a/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTracker.kt +++ b/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTracker.kt @@ -4,9 +4,11 @@ */ package ch.srgssr.pillarbox.core.business.tracker.comscore +import android.util.Log import androidx.media3.common.PlaybackParameters import androidx.media3.common.Player import androidx.media3.common.Timeline.Window +import androidx.media3.common.util.Size import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.analytics.AnalyticsListener import ch.srgssr.pillarbox.analytics.BuildConfig @@ -32,6 +34,12 @@ class ComScoreTracker : MediaItemTracker { private val window = Window() private lateinit var latestData: Data + /** + * A surface is connected to the player when its [ExoPlayer.getSurfaceSize] is different from [Size.ZERO]. + * When used with MediaSessionService or MediaBrowser the size is always [Size.UNKNOWN]. When not connected the size is [Size.ZERO]. + */ + private var isSurfaceConnected: Boolean = false + init { streamingAnalytics.setMediaPlayerName(MEDIA_PLAYER_NAME) streamingAnalytics.setMediaPlayerVersion(BuildConfig.VERSION_NAME) @@ -40,6 +48,7 @@ class ComScoreTracker : MediaItemTracker { override fun start(player: ExoPlayer, initialData: Any?) { requireNotNull(initialData) require(initialData is Data) + isSurfaceConnected = player.surfaceSize != Size.ZERO streamingAnalytics.createPlaybackSession() setMetadata(initialData) handleStart(player) @@ -85,6 +94,7 @@ class ComScoreTracker : MediaItemTracker { } private fun notifyPlay(position: Long, window: Window) { + if (!isSurfaceConnected) return DebugLogger.debug(TAG, "notifyPlay: $position") notifyPosition(position, window) streamingAnalytics.notifyPlay() @@ -187,6 +197,21 @@ class ComScoreTracker : MediaItemTracker { notifyPause() } } + + override fun onSurfaceSizeChanged(eventTime: AnalyticsListener.EventTime, width: Int, height: Int) { + val isCurrentSurfaceConnected = Size(width, height) != Size.ZERO + if (isCurrentSurfaceConnected != isSurfaceConnected) { + Log.d(TAG, "Surface connected change $isSurfaceConnected -> $isCurrentSurfaceConnected") + isSurfaceConnected = isCurrentSurfaceConnected + if (isCurrentSurfaceConnected) { + val position = eventTime.eventPlaybackPositionMs + eventTime.timeline.getWindow(eventTime.windowIndex, window) + notifyPlay(position, window) + } else { + notifyPause() + } + } + } } /**