Skip to content

Commit

Permalink
Simplify MetricsOverlay to be updated every 500ms
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Jul 25, 2024
1 parent a5fb9e4 commit 5bc9222
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.zIndex
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.media3.common.Player
import ch.srgssr.pillarbox.demo.shared.ui.settings.MetricsOverlayOptions
import ch.srgssr.pillarbox.demo.ui.player.controls.PlayerControls
import ch.srgssr.pillarbox.demo.ui.player.controls.PlayerError
import ch.srgssr.pillarbox.demo.ui.player.controls.PlayerNoContent
import ch.srgssr.pillarbox.demo.ui.player.controls.SkipButton
import ch.srgssr.pillarbox.demo.ui.player.controls.rememberProgressTrackerState
import ch.srgssr.pillarbox.demo.ui.player.metrics.MetricsDebugView
import ch.srgssr.pillarbox.demo.ui.player.metrics.MetricsOverlay
import ch.srgssr.pillarbox.demo.ui.theme.paddings
import ch.srgssr.pillarbox.player.PillarboxExoPlayer
import ch.srgssr.pillarbox.player.currentPositionAsFlow
import ch.srgssr.pillarbox.ui.ProgressTrackerState
import ch.srgssr.pillarbox.ui.ScaleMode
import ch.srgssr.pillarbox.ui.exoplayer.ExoPlayerSubtitleView
Expand All @@ -41,6 +43,8 @@ import ch.srgssr.pillarbox.ui.widget.ToggleableBox
import ch.srgssr.pillarbox.ui.widget.keepScreenOn
import ch.srgssr.pillarbox.ui.widget.player.PlayerSurface
import ch.srgssr.pillarbox.ui.widget.rememberDelayedVisibilityState
import kotlinx.coroutines.flow.map
import kotlin.time.Duration.Companion.milliseconds

/**
* Simple player view
Expand Down Expand Up @@ -124,13 +128,20 @@ fun PlayerView(
}
ExoPlayerSubtitleView(player = player)
if (overlayEnabled && player is PillarboxExoPlayer) {
MetricsDebugView(
modifier = Modifier
.fillMaxSize()
.align(Alignment.TopStart),
overlayOptions = overlayOptions,
player = player,
)
val currentMetricsFlow = remember(player) {
player.currentPositionAsFlow(updateInterval = 500.milliseconds).map {
player.getCurrentMetrics()
}
}
val currentMetrics by currentMetricsFlow.collectAsStateWithLifecycle(player.getCurrentMetrics())
currentMetrics?.let {
MetricsOverlay(
modifier = Modifier
.fillMaxSize()
.align(Alignment.TopStart),
playbackMetrics = it, overlayOptions = overlayOptions
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,25 @@ package ch.srgssr.pillarbox.demo.ui.player.metrics
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.media3.common.Format
import ch.srgssr.pillarbox.demo.shared.ui.settings.MetricsOverlayOptions
import ch.srgssr.pillarbox.player.PillarboxExoPlayer
import ch.srgssr.pillarbox.player.analytics.metrics.PlaybackMetrics
import ch.srgssr.pillarbox.player.utils.BitrateUtil.toByteRate

/**
* Metrics debug view
* Display [playbackMetrics] as overlay.
*
* @param player the [PillarboxExoPlayer] to debug.
* @param modifier The Modifier.
* @param overlayOptions [MetricsOverlayOptions].
* @param playbackMetrics The [PlaybackMetrics] to display.
* @param overlayOptions The [MetricsOverlayOptions] the options.
* @param modifier The modifier to be applied to the layout.
*/
@Composable
fun MetricsDebugView(
player: PillarboxExoPlayer,
modifier: Modifier = Modifier,
overlayOptions: MetricsOverlayOptions = MetricsOverlayOptions()
) {
val viewmodel = rememberMetricsViewModel(player)
val currentMetrics by viewmodel.metricsFlow.collectAsStateWithLifecycle()
currentMetrics?.let {
OverlayMetrics(modifier = Modifier, playbackMetrics = it, overlayOptions = overlayOptions)
}
}

@Composable
internal fun OverlayMetrics(
fun MetricsOverlay(
playbackMetrics: PlaybackMetrics,
overlayOptions: MetricsOverlayOptions,
modifier: Modifier = Modifier,
Expand Down

This file was deleted.

0 comments on commit 5bc9222

Please sign in to comment.