Skip to content

Commit

Permalink
Send pause when no surface is connected to player (#267)
Browse files Browse the repository at this point in the history
Co-authored-by: Samuel Défago <[email protected]>
  • Loading branch information
StaehliJ and defagos authored Oct 20, 2023
1 parent beda01c commit 9cbab6d
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
}
}
}

/**
Expand Down

0 comments on commit 9cbab6d

Please sign in to comment.