Skip to content

Commit

Permalink
Small formating, doc, and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Oct 11, 2024
1 parent 8d030b0 commit 7feec9e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<PillarboxExoPlayer>(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<PillarboxExoPlayer>(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)
}
}

Expand All @@ -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) }
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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<Int> {
private inner class StoryPreloadStatusControl : TargetPreloadStatusControl<Int> {
override fun getTargetPreloadStatus(rankingData: Int): TargetPreloadStatusControl.PreloadStatus? {
val offset = abs(rankingData - currentPage)

Expand All @@ -172,4 +166,8 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) {
}
}
}

private companion object {
private const val PLAYERS_COUNT = 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>? = null,
mediaSourceFactory: PillarboxMediaSourceFactory = PillarboxMediaSourceFactory(context),
mediaSourceFactory: MediaSource.Factory = PillarboxMediaSourceFactory(context),
trackSelector: TrackSelector = PillarboxTrackSelector(context),
bandwidthMeter: BandwidthMeter = PillarboxBandwidthMeter(context),
rendererCapabilitiesListFactory: RendererCapabilitiesList.Factory = DefaultRendererCapabilitiesList.Factory(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7feec9e

Please sign in to comment.