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

Send pause when no surface is connected to player #267

Merged
merged 2 commits into from
Oct 20, 2023
Merged
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 @@ -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