Skip to content

Commit

Permalink
Add argument to set Player.getMaxSeekToPreviousPosition when creati…
Browse files Browse the repository at this point in the history
…ng a `PillarboxExoPlayer`
  • Loading branch information
MGaetan89 authored and StaehliJ committed Jul 30, 2024
1 parent 415a79d commit 48fd019
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ch.srgssr.pillarbox.core.business

import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.media3.common.Player
import androidx.media3.common.util.Clock
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.LoadControl
Expand All @@ -14,10 +15,12 @@ import ch.srgssr.pillarbox.core.business.integrationlayer.service.MediaCompositi
import ch.srgssr.pillarbox.core.business.source.SRGAssetLoader
import ch.srgssr.pillarbox.core.business.tracker.DefaultMediaItemTrackerRepository
import ch.srgssr.pillarbox.player.PillarboxExoPlayer
import ch.srgssr.pillarbox.player.PillarboxExoPlayer.Companion.DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION
import ch.srgssr.pillarbox.player.PillarboxLoadControl
import ch.srgssr.pillarbox.player.SeekIncrement
import ch.srgssr.pillarbox.player.source.PillarboxMediaSourceFactory
import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerProvider
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

/**
Expand All @@ -31,6 +34,7 @@ object DefaultPillarbox {
*
* @param context The context.
* @param seekIncrement The seek increment.
* @param maxSeekToPreviousPosition The [Player.getMaxSeekToPreviousPosition] value.
* @param mediaItemTrackerRepository The provider of MediaItemTracker, by default [DefaultMediaItemTrackerRepository].
* @param mediaCompositionService The [MediaCompositionService] to use, by default [HttpMediaCompositionService].
* @param loadControl The load control, by default [PillarboxLoadControl].
Expand All @@ -39,13 +43,15 @@ object DefaultPillarbox {
operator fun invoke(
context: Context,
seekIncrement: SeekIncrement = defaultSeekIncrement,
maxSeekToPreviousPosition: Duration = DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION,
mediaItemTrackerRepository: MediaItemTrackerProvider = DefaultMediaItemTrackerRepository(),
mediaCompositionService: MediaCompositionService = HttpMediaCompositionService(),
loadControl: LoadControl = PillarboxLoadControl(),
): PillarboxExoPlayer {
return DefaultPillarbox(
context = context,
seekIncrement = seekIncrement,
maxSeekToPreviousPosition = maxSeekToPreviousPosition,
mediaItemTrackerRepository = mediaItemTrackerRepository,
mediaCompositionService = mediaCompositionService,
loadControl = loadControl,
Expand All @@ -58,6 +64,7 @@ object DefaultPillarbox {
*
* @param context The context.
* @param seekIncrement The seek increment.
* @param maxSeekToPreviousPosition The [Player.getMaxSeekToPreviousPosition] value.
* @param mediaItemTrackerRepository The provider of MediaItemTracker, by default [DefaultMediaItemTrackerRepository].
* @param loadControl The load control, by default [DefaultLoadControl].
* @param mediaCompositionService The [MediaCompositionService] to use, by default [HttpMediaCompositionService].
Expand All @@ -68,6 +75,7 @@ object DefaultPillarbox {
operator fun invoke(
context: Context,
seekIncrement: SeekIncrement = defaultSeekIncrement,
maxSeekToPreviousPosition: Duration = DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION,
mediaItemTrackerRepository: MediaItemTrackerProvider = DefaultMediaItemTrackerRepository(),
loadControl: LoadControl = DefaultLoadControl(),
mediaCompositionService: MediaCompositionService = HttpMediaCompositionService(),
Expand All @@ -76,6 +84,7 @@ object DefaultPillarbox {
return PillarboxExoPlayer(
context = context,
seekIncrement = seekIncrement,
maxSeekToPreviousPosition = maxSeekToPreviousPosition,
mediaSourceFactory = PillarboxMediaSourceFactory(context).apply {
addAssetLoader(SRGAssetLoader(context, mediaCompositionService))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ch.srgssr.pillarbox.player

import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.PlaybackParameters
Expand Down Expand Up @@ -33,6 +34,8 @@ import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerProvider
import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerRepository
import ch.srgssr.pillarbox.player.tracker.TimeRangeTracker
import ch.srgssr.pillarbox.player.utils.PillarboxEventLogger
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

/**
* Pillarbox player
Expand Down Expand Up @@ -123,13 +126,15 @@ class PillarboxExoPlayer internal constructor(
mediaSourceFactory: PillarboxMediaSourceFactory = PillarboxMediaSourceFactory(context),
loadControl: LoadControl = PillarboxLoadControl(),
mediaItemTrackerProvider: MediaItemTrackerProvider = MediaItemTrackerRepository(),
seekIncrement: SeekIncrement = SeekIncrement()
seekIncrement: SeekIncrement = SeekIncrement(),
maxSeekToPreviousPosition: Duration = DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION,
) : this(
context = context,
mediaSourceFactory = mediaSourceFactory,
loadControl = loadControl,
mediaItemTrackerProvider = mediaItemTrackerProvider,
seekIncrement = seekIncrement,
maxSeekToPreviousPosition = maxSeekToPreviousPosition,
clock = Clock.DEFAULT,
)

Expand All @@ -140,13 +145,15 @@ class PillarboxExoPlayer internal constructor(
loadControl: LoadControl = PillarboxLoadControl(),
mediaItemTrackerProvider: MediaItemTrackerProvider = MediaItemTrackerRepository(),
seekIncrement: SeekIncrement = SeekIncrement(),
maxSeekToPreviousPosition: Duration = DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION,
clock: Clock,
analyticsCollector: PillarboxAnalyticsCollector = PillarboxAnalyticsCollector(clock),
) : this(
ExoPlayer.Builder(context)
.setClock(clock)
.setUsePlatformDiagnostics(false)
.setSeekIncrements(seekIncrement)
.setMaxSeekToPreviousPositionMs(maxSeekToPreviousPosition.inWholeMilliseconds)
.setRenderersFactory(
DefaultRenderersFactory(context)
.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF)
Expand Down Expand Up @@ -405,6 +412,13 @@ class PillarboxExoPlayer internal constructor(
}
}
}

companion object {
/**
* A default maximum position for which a seek to previous will seek to the previous window.
*/
val DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION = C.DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION_MS.milliseconds
}
}

/**
Expand All @@ -426,7 +440,7 @@ fun Player.isPlaybackSpeedPossibleAtPosition(position: Long, speed: Float, windo

internal fun Window.isPlaybackSpeedPossibleAtPosition(positionMs: Long, playbackSpeed: Float): Boolean {
return when {
!isLive() || playbackSpeed == NormalSpeed -> true
!isLive || playbackSpeed == NormalSpeed -> true
!isSeekable -> false
isAtDefaultPosition(positionMs) && playbackSpeed > NormalSpeed -> false
else -> true
Expand Down

0 comments on commit 48fd019

Please sign in to comment.