From 7feec9e1e81d5ad8069c0d2cc7b21e7836ecf04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Fri, 11 Oct 2024 14:44:33 +0200 Subject: [PATCH] Small formating, doc, and code cleanup --- .../ui/showcases/layouts/OptimizedStory.kt | 9 +-- .../ui/showcases/layouts/StoryViewModel.kt | 68 +++++++++---------- .../player/PillarboxPreloadManager.kt | 6 +- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/OptimizedStory.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/OptimizedStory.kt index 871deeec0..f031845f4 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/OptimizedStory.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/OptimizedStory.kt @@ -70,10 +70,8 @@ fun OptimizedStory(storyViewModel: StoryViewModel = viewModel()) { val movablePlayerView = remember { (0 until storyViewModel.playerCount).map { index -> movableContentOf { - Box { - val player = remember { storyViewModel.getPlayer(index) } - PlayerView(player, modifier = Modifier.fillMaxSize()) - } + val player = remember { storyViewModel.getPlayer(index) } + PlayerView(player, modifier = Modifier.fillMaxSize()) } } } @@ -118,8 +116,7 @@ private fun PlayerView(player: Player, modifier: Modifier = Modifier) { modifier = modifier, ) { PlayerSurface( - modifier = Modifier - .fillMaxHeight(), + modifier = Modifier.fillMaxHeight(), scaleMode = ScaleMode.Crop, surfaceType = if (Build.VERSION.SDK_INT == Build.VERSION_CODES.UPSIDE_DOWN_CAKE) SurfaceType.Texture else SurfaceType.Surface, player = player, diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/StoryViewModel.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/StoryViewModel.kt index 79c5c42f0..f2a3f1b60 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/StoryViewModel.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/layouts/StoryViewModel.kt @@ -35,17 +35,16 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) { private val mediaSourceFactory = PillarboxMediaSourceFactory(application).apply { addAssetLoader(SRGAssetLoader(application)) } - private val preloadManager = - PillarboxPreloadManager( - context = application, - targetPreloadStatusControl = StoryPreloadStatusControl(), - mediaSourceFactory = mediaSourceFactory, - trackSelector = PillarboxTrackSelector(application).apply { - parameters = parameters.buildUpon() - .setForceLowestBitrate(true) - .build() - } - ) + private val preloadManager = PillarboxPreloadManager( + context = application, + targetPreloadStatusControl = StoryPreloadStatusControl(), + mediaSourceFactory = mediaSourceFactory, + trackSelector = PillarboxTrackSelector(application).apply { + parameters = parameters.buildUpon() + .setForceLowestBitrate(true) + .build() + } + ) private val loadControl = PillarboxLoadControl( bufferDurations = PillarboxLoadControl.BufferDurations( @@ -54,25 +53,24 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) { bufferForPlayback = 500.milliseconds, bufferForPlaybackAfterRebuffer = 1_000.milliseconds, ), - preloadManager.allocator + allocator = preloadManager.allocator, ) private var currentPage = C.INDEX_UNSET - private val players = SparseArray(3).apply { - for (i in 0 until 3) { - put( - i, - PillarboxExoPlayer( - context = application, - playbackLooper = preloadManager.playbackLooper, - loadControl = loadControl - ).apply { - repeatMode = Player.REPEAT_MODE_ONE - videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING - prepare() - } - ) + private val players = SparseArray(PLAYERS_COUNT).apply { + for (i in 0 until PLAYERS_COUNT) { + val player = PillarboxExoPlayer( + context = application, + playbackLooper = preloadManager.playbackLooper, + loadControl = loadControl + ).apply { + repeatMode = Player.REPEAT_MODE_ONE + videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING + prepare() + } + + put(i, player) } } @@ -90,8 +88,7 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) { mediaItems.forEachIndexed { index, mediaItem -> preloadManager.add(mediaItem, index) } - preloadManager.currentPlayingIndex = 0 - preloadManager.invalidate() + setCurrentPage(0) players.forEach { key, _ -> setupPlayerForPage(key) } } @@ -136,16 +133,13 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) { /** * Play * - * @param player to play all others are paused. + * @param player The player to play, all others are paused. */ fun play(player: PillarboxExoPlayer) { if (player.playWhenReady) return players.forEach { _, value -> - value.pause() value.seekToDefaultPosition() - if (value == player) { - player.play() - } + value.playWhenReady = value == player } } @@ -157,11 +151,11 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) { } /** - * Default implementation of [TargetPreloadStatusControl] that will preload the first second of the `n ± 1` item, and the first half-second of + * Custom implementation of [TargetPreloadStatusControl] that will preload the first second of the `n ± 1` item, and the first millisecond of * the `n ± 2,3,4` item, where `n` is the index of the current item. */ @Suppress("MagicNumber") - inner class StoryPreloadStatusControl : TargetPreloadStatusControl { + private inner class StoryPreloadStatusControl : TargetPreloadStatusControl { override fun getTargetPreloadStatus(rankingData: Int): TargetPreloadStatusControl.PreloadStatus? { val offset = abs(rankingData - currentPage) @@ -172,4 +166,8 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) { } } } + + private companion object { + private const val PLAYERS_COUNT = 3 + } } diff --git a/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/PillarboxPreloadManager.kt b/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/PillarboxPreloadManager.kt index 6c9839e92..7af9a547d 100644 --- a/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/PillarboxPreloadManager.kt +++ b/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/PillarboxPreloadManager.kt @@ -36,14 +36,14 @@ import kotlin.time.Duration.Companion.seconds * @param bandwidthMeter The [BandwidthMeter] for this preload manager. * @param rendererCapabilitiesListFactory The [RendererCapabilitiesList.Factory] for this preload manager. * @property allocator The [Allocator] for this preload manager. Have to be the same as the one used by the Player. - * @param playbackThread The [Thread] on which the players run. + * @param playbackThread The [Thread] on which the players run. Its lifecycle is handled internally by [PillarboxPreloadManager]. * * @see DefaultPreloadManager */ class PillarboxPreloadManager( context: Context, targetPreloadStatusControl: TargetPreloadStatusControl? = null, - mediaSourceFactory: PillarboxMediaSourceFactory = PillarboxMediaSourceFactory(context), + mediaSourceFactory: MediaSource.Factory = PillarboxMediaSourceFactory(context), trackSelector: TrackSelector = PillarboxTrackSelector(context), bandwidthMeter: BandwidthMeter = PillarboxBandwidthMeter(context), rendererCapabilitiesListFactory: RendererCapabilitiesList.Factory = DefaultRendererCapabilitiesList.Factory( @@ -136,7 +136,7 @@ class PillarboxPreloadManager( } /** - * Release the preload manager and the underlying [PlayerPool]. + * Release the preload manager. * The preload manager must not be used after calling this method. * * @see DefaultPreloadManager.release