Skip to content

Commit

Permalink
Remove time intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Apr 30, 2024
1 parent 6e0a794 commit e22ad91
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ class SRGAssetLoader(
)
}.build(),
chapters = ChapterAdapter.getChapters(result),
blockedIntervals = SegmentAdapter.getBlockedIntervals(chapter.listSegment),
timeIntervals = TimeIntervalAdapter.getTimeIntervals(result.mainChapter.timeIntervalList),
blockedTimeRanges = SegmentAdapter.getBlockedTimeRanges(chapter.listSegment),
timeRanges = TimeIntervalAdapter.getTimeIntervals(result.mainChapter.timeIntervalList),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import ch.srgssr.pillarbox.player.asset.BlockedTimeRange

internal object SegmentAdapter {

fun getBlockedInterval(segment: Segment): BlockedTimeRange {
fun getBlockedTimeRange(segment: Segment): BlockedTimeRange {
requireNotNull(segment.blockReason)
return BlockedTimeRange(segment.urn, segment.markIn, segment.markOut, segment.blockReason.toString())
}

fun getBlockedIntervals(listSegment: List<Segment>?): List<BlockedTimeRange> {
fun getBlockedTimeRanges(listSegment: List<Segment>?): List<BlockedTimeRange> {
return listSegment?.filter { it.blockReason != null }?.map {
getBlockedInterval(it)
getBlockedTimeRange(it)
} ?: emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ class SRGAssetLoaderTest {
val asset = assetLoader.loadAsset(
SRGMediaItemBuilder(DummyMediaCompositionProvider.URN_SEGMENT_BLOCK_REASON).build()
)
val expectedBlockIntervals = listOf(SegmentAdapter.getBlockedInterval(DummyMediaCompositionProvider.BLOCKED_SEGMENT))
assertEquals(expectedBlockIntervals, asset.blockedIntervals)
val expectedBlockIntervals = listOf(SegmentAdapter.getBlockedTimeRange(DummyMediaCompositionProvider.BLOCKED_SEGMENT))
assertEquals(expectedBlockIntervals, asset.blockedTimeRanges)
}

@Test
Expand All @@ -160,7 +160,7 @@ class SRGAssetLoaderTest {
val expectedTimeIntervals = TimeIntervalAdapter.getTimeIntervals(
listOf(DummyMediaCompositionProvider.TIME_INTERVAL_1, DummyMediaCompositionProvider.TIME_INTERVAL_2)
)
assertEquals(expectedTimeIntervals, asset.timeIntervals)
assertEquals(expectedTimeIntervals, asset.timeRanges)
}

internal class DummyMediaCompositionProvider : MediaCompositionService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ class SegmentAdapterTest {
@Test(expected = IllegalArgumentException::class)
fun `getBlockedInterval of a non blocked segment`() {
val segmentIl = Segment(urn = "urn1", title = "title 1", markIn = 1, markOut = 2, blockReason = null)
SegmentAdapter.getBlockedInterval(segmentIl)
SegmentAdapter.getBlockedTimeRange(segmentIl)
}

@Test
fun `getBlockedInterval of a blocked segment`() {
val segmentIl = Segment(urn = "urn1", title = "title 1", markIn = 1, markOut = 2, blockReason = BlockReason.UNKNOWN)
val expected = BlockedTimeRange(id = "urn1", start = 1, end = 2, reason = "UNKNOWN")
assertEquals(expected, SegmentAdapter.getBlockedInterval(segmentIl))
assertEquals(expected, SegmentAdapter.getBlockedTimeRange(segmentIl))
}

@Test
fun `empty segment list return empty blocked interval`() {
assertEquals(emptyList(), SegmentAdapter.getBlockedIntervals(null))
assertEquals(emptyList(), SegmentAdapter.getBlockedIntervals(emptyList()))
assertEquals(emptyList(), SegmentAdapter.getBlockedTimeRanges(null))
assertEquals(emptyList(), SegmentAdapter.getBlockedTimeRanges(emptyList()))
}

@Test
Expand All @@ -37,7 +37,7 @@ class SegmentAdapterTest {
Segment(urn = "urn1", title = "title 1", markIn = 1, markOut = 2),
Segment(urn = "urn2", title = "title 2", markIn = 3, markOut = 4),
)
assertEquals(emptyList(), SegmentAdapter.getBlockedIntervals(listSegments))
assertEquals(emptyList(), SegmentAdapter.getBlockedTimeRanges(listSegments))
}

@Test
Expand All @@ -52,6 +52,6 @@ class SegmentAdapterTest {
BlockedTimeRange(id = "urn1_blocked", start = 1, end = 4, reason = "LEGAL"),
BlockedTimeRange(id = "urn3", start = 5, end = 56, reason = "UNKNOWN"),
)
assertEquals(expected, SegmentAdapter.getBlockedIntervals(listSegments))
assertEquals(expected, SegmentAdapter.getBlockedTimeRanges(listSegments))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ class PillarboxExoPlayer internal constructor(
private val blockedTimeRangeTracker = BlockedTimeRangeTracker(this)
private val chapterTracker = TimeRangeTracker(
player = this,
getTimeIntervalAtPosition = Player::getChapterAtPosition,
getAllTimeIntervals = PillarboxData::chapters,
notifyTimeIntervalChanged = { notifyCurrentChapterChanged(it) },
getTimeRangeAt = Player::getChapterAtPosition,
getAllTimeRanges = PillarboxData::chapters,
notifyTimeRangeChanged = { notifyCurrentChapterChanged(it) },
)
private val timeRangeTracker = TimeRangeTracker(
player = this,
getTimeIntervalAtPosition = Player::getSkipableTimeRangeAtPosition,
getAllTimeIntervals = PillarboxData::timeIntervals,
notifyTimeIntervalChanged = { notifyTimeIntervalChanged(it) },
getTimeRangeAt = Player::getSkipableTimeRangeAtPosition,
getAllTimeRanges = PillarboxData::timeRanges,
notifyTimeRangeChanged = { notifyTimeRangeChanged(it) },
)

init {
Expand Down Expand Up @@ -178,15 +178,15 @@ class PillarboxExoPlayer internal constructor(
}
}

internal fun notifyBlockedIntervalReached(blockedInterval: BlockedTimeRange) {
internal fun notifyBlockedTimeRangeReached(blockedTimeRange: BlockedTimeRange) {
HashSet(listeners).forEach {
it.onBlockedTimeRangeReached(blockedInterval)
it.onBlockedTimeRangeReached(blockedTimeRange)
}
}

internal fun notifyTimeIntervalChanged(timeInterval: SkipableTimeRange?) {
internal fun notifyTimeRangeChanged(timeRange: SkipableTimeRange?) {
HashSet(listeners).forEach {
it.onSkipableTimeRangeChanged(timeInterval)
it.onSkipableTimeRangeChanged(timeRange)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ interface PillarboxPlayer : Player {
fun onCurrentChapterChanged(chapter: Chapter?) {}

/**
* On block interval reached
* On blocked time range reached
*
* @param blockedTimeRange The [BlockedTimeRange] reached by the player.
*/
fun onBlockedTimeRangeReached(blockedTimeRange: BlockedTimeRange) {}

/**
* `onTimeIntervalChanged` is called when either:
* `onSkipableTimeRangeChanged` is called when either:
* - The player position changes while playing automatically.
* - The use seeks to a new position.
* - The playlist changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerData
* @property mediaSource The [MediaSource] used by the player to play something.
* @property trackersData The [MediaItemTrackerData] to set to the [PillarboxData].
* @property mediaMetadata The [MediaMetadata] to set to the player media item.
* @property blockedIntervals The [BlockedTimeRange] list to set to the [PillarboxData].
* @property blockedTimeRanges The [BlockedTimeRange] list to set to the [PillarboxData].
* @property chapters The [Chapter] list to set to the [PillarboxData].
* @property timeIntervals The [SkipableTimeRange] list to set to the [PillarboxData].
* @property timeRanges The [SkipableTimeRange] list to set to the [PillarboxData].
*/
data class Asset(
val mediaSource: MediaSource,
val trackersData: MediaItemTrackerData = MediaItemTrackerData.EMPTY,
val mediaMetadata: MediaMetadata = MediaMetadata.EMPTY,
val blockedIntervals: List<BlockedTimeRange> = emptyList(),
val blockedTimeRanges: List<BlockedTimeRange> = emptyList(),
val chapters: List<Chapter> = emptyList(),
val timeIntervals: List<SkipableTimeRange> = emptyList(),
val timeRanges: List<SkipableTimeRange> = emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerData
* Pillarbox data
*
* @property trackersData The [MediaItemTrackerData].
* @property blockedIntervals The [BlockedTimeRange] list.
* @property blockedTimeRanges The [BlockedTimeRange] list.
* @property chapters The [Chapter] list.
* @property timeIntervals The [SkipableTimeRange] list.
* @property timeRanges The [SkipableTimeRange] list.
*/
data class PillarboxData(
val trackersData: MediaItemTrackerData = MediaItemTrackerData.EMPTY,
val blockedIntervals: List<BlockedTimeRange> = emptyList(),
val blockedTimeRanges: List<BlockedTimeRange> = emptyList(),
val chapters: List<Chapter> = emptyList(),
val timeIntervals: List<SkipableTimeRange> = emptyList(),
val timeRanges: List<SkipableTimeRange> = emptyList(),
) {
companion object {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package ch.srgssr.pillarbox.player.asset
import kotlin.math.abs

/**
* Time interval
* Time range
*/
interface TimeRange {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fun Player.getCurrentChapters(): List<Chapter> {
* @return The current media item time intervals or an empty list.
*/
fun Player.getSkipableTimeRange(): List<SkipableTimeRange> {
return currentMediaItem?.pillarboxData?.timeIntervals.orEmpty()
return currentMediaItem?.pillarboxData?.timeRanges.orEmpty()
}

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ fun Player.isAtLiveEdge(positionMs: Long = currentPosition, window: Window = Win
* @return The current media item blocked intervals or an empty list.
*/
fun Player.getCurrentBlockedIntervals(): List<BlockedTimeRange> {
return currentMediaItem?.pillarboxData?.blockedIntervals ?: emptyList()
return currentMediaItem?.pillarboxData?.blockedTimeRanges ?: emptyList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class PillarboxMediaSource internal constructor(
.setTag(
PillarboxData(
trackersData = asset.trackersData,
blockedIntervals = asset.blockedIntervals,
blockedTimeRanges = asset.blockedTimeRanges,
chapters = asset.chapters,
timeIntervals = asset.timeIntervals,
timeRanges = asset.timeRanges,
)
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ internal class BlockedTimeRangeTracker(
clearPlayerMessage()
pillarboxExoPlayer.removeListener(listener)
if (data == null || mediaItem == null) return
listBlockedIntervals = mediaItem.pillarboxData.blockedIntervals
listBlockedIntervals = mediaItem.pillarboxData.blockedTimeRanges
pillarboxExoPlayer.addListener(listener)
createMessages()
}

private fun notifyBlockedSegment(blockedSection: BlockedTimeRange) {
Log.i(TAG, "Blocked segment reached $blockedSection")
pillarboxExoPlayer.notifyBlockedIntervalReached(blockedSection)
pillarboxExoPlayer.notifyBlockedTimeRangeReached(blockedSection)
pillarboxExoPlayer.seekToWithoutSmoothSeeking(blockedSection.end + 1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ import ch.srgssr.pillarbox.player.extension.pillarboxData

internal class TimeRangeTracker<T : TimeRange>(
private val player: PillarboxExoPlayer,
private val getTimeIntervalAtPosition: PillarboxExoPlayer.(position: Long) -> T?,
private val getAllTimeIntervals: PillarboxData.() -> List<T>,
private val notifyTimeIntervalChanged: PillarboxExoPlayer.(timeInterval: T?) -> Unit,
private val getTimeRangeAt: PillarboxExoPlayer.(position: Long) -> T?,
private val getAllTimeRanges: PillarboxData.() -> List<T>,
private val notifyTimeRangeChanged: PillarboxExoPlayer.(timeInterval: T?) -> Unit,
) : CurrentMediaItemPillarboxDataTracker.Callback {
private val playerMessages = mutableListOf<PlayerMessage>()
private var timeRanges = emptyList<T>()
private val listener = Listener()

private var lastTimeInterval: T? = player.getTimeIntervalAtPosition(player.currentPosition)
private var lastTimeRange: T? = player.getTimeRangeAt(player.currentPosition)
set(value) {
if (field != value) {
field = value
player.notifyTimeIntervalChanged(field)
player.notifyTimeRangeChanged(field)
}
}

override fun onPillarboxDataChanged(mediaItem: MediaItem?, data: PillarboxData?) {
clearPlayerMessages()
lastTimeInterval = player.getTimeIntervalAtPosition(player.currentPosition)
lastTimeRange = player.getTimeRangeAt(player.currentPosition)
player.removeListener(listener)
if (data == null || mediaItem == null) {
return
}

timeRanges = mediaItem.pillarboxData.getAllTimeIntervals()
timeRanges = mediaItem.pillarboxData.getAllTimeRanges()
player.addListener(listener)
createPlayerMessages()
}
Expand All @@ -51,12 +51,12 @@ internal class TimeRangeTracker<T : TimeRange>(

when (messageType) {
TYPE_ENTER -> {
if (timeInterval != lastTimeInterval) {
lastTimeInterval = timeInterval
if (timeInterval != lastTimeRange) {
lastTimeRange = timeInterval
}
}

TYPE_EXIT -> lastTimeInterval = null
TYPE_EXIT -> lastTimeRange = null
}
}

Expand Down Expand Up @@ -102,12 +102,12 @@ internal class TimeRangeTracker<T : TimeRange>(
oldPosition.mediaItemIndex == newPosition.mediaItemIndex
) {
val currentPosition = player.currentPosition
val currentTimeInterval = lastTimeInterval
val currentTimeInterval = lastTimeRange
?.takeIf { timeInterval -> currentPosition in timeInterval }
?: player.getTimeIntervalAtPosition(currentPosition)
?: player.getTimeRangeAt(currentPosition)

if (currentTimeInterval != lastTimeInterval) {
lastTimeInterval = currentTimeInterval
if (currentTimeInterval != lastTimeRange) {
lastTimeRange = currentTimeInterval
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class PlayerTest {
val player = mockk<Player> {
every { currentMediaItem } returns MediaItem.Builder()
.setUri("https://example.com/")
.setTag(PillarboxData(timeIntervals = timeIntervals))
.setTag(PillarboxData(timeRanges = timeIntervals))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private class BlockedAssetLoader(context: Context) : AssetLoader(DefaultMediaSou
return Asset(
mediaSource = mediaSourceFactory.createMediaSource(itemBuilder.build()),
mediaMetadata = mediaItem.mediaMetadata,
blockedIntervals = blockedIntervals,
blockedTimeRanges = blockedIntervals,
)
}

Expand Down

0 comments on commit e22ad91

Please sign in to comment.