|
| 1 | +/* |
| 2 | + * Copyright (c) SRG SSR. All rights reserved. |
| 3 | + * License information is available from the LICENSE file. |
| 4 | + */ |
| 5 | +package ch.srgssr.pillarbox.demo.shared.source |
| 6 | + |
| 7 | +import android.content.Context |
| 8 | +import androidx.media3.common.MediaItem |
| 9 | +import androidx.media3.exoplayer.source.DefaultMediaSourceFactory |
| 10 | +import ch.srgssr.pillarbox.demo.shared.data.DemoItem |
| 11 | +import ch.srgssr.pillarbox.player.asset.Asset |
| 12 | +import ch.srgssr.pillarbox.player.asset.AssetLoader |
| 13 | +import ch.srgssr.pillarbox.player.asset.timeRange.BlockedTimeRange |
| 14 | +import kotlin.time.Duration.Companion.minutes |
| 15 | +import kotlin.time.Duration.Companion.seconds |
| 16 | + |
| 17 | +/** |
| 18 | + * An AssetLoader to demonstrate some edge cases with [BlockedTimeRange]. |
| 19 | + */ |
| 20 | +class BlockedTimeRangeAssetLoader(context: Context) : AssetLoader(DefaultMediaSourceFactory(context)) { |
| 21 | + |
| 22 | + override fun canLoadAsset(mediaItem: MediaItem): Boolean { |
| 23 | + return mediaItem.localConfiguration?.uri?.toString()?.startsWith("blocked:") ?: false |
| 24 | + } |
| 25 | + |
| 26 | + override suspend fun loadAsset(mediaItem: MediaItem): Asset { |
| 27 | + return Asset( |
| 28 | + mediaSource = mediaSourceFactory.createMediaSource(MediaItem.fromUri(URL)), |
| 29 | + blockedTimeRanges = createBlockedTimeRangesFromId(mediaItem.mediaId), |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + @Suppress("MagicNumber") |
| 34 | + private fun createBlockedTimeRangesFromId(mediaId: String): List<BlockedTimeRange> { |
| 35 | + return when (mediaId) { |
| 36 | + ID_START_END -> { |
| 37 | + listOf( |
| 38 | + BlockedTimeRange( |
| 39 | + start = 0L, |
| 40 | + end = 10.seconds.inWholeMilliseconds, |
| 41 | + ), |
| 42 | + BlockedTimeRange( |
| 43 | + start = (videoDuration - 5.minutes).inWholeMilliseconds, |
| 44 | + end = videoDuration.inWholeMilliseconds, |
| 45 | + ) |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + ID_OVERLAP -> { |
| 50 | + listOf( |
| 51 | + BlockedTimeRange( |
| 52 | + start = 10.seconds.inWholeMilliseconds, |
| 53 | + end = 50.seconds.inWholeMilliseconds, |
| 54 | + ), |
| 55 | + BlockedTimeRange( |
| 56 | + start = 15.seconds.inWholeMilliseconds, |
| 57 | + end = 5.minutes.inWholeMilliseconds, |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + ID_INCLUDED -> { |
| 63 | + listOf( |
| 64 | + BlockedTimeRange( |
| 65 | + start = 15.seconds.inWholeMilliseconds, |
| 66 | + end = 30.seconds.inWholeMilliseconds, |
| 67 | + reason = "contained", |
| 68 | + ), |
| 69 | + BlockedTimeRange( |
| 70 | + start = 10.seconds.inWholeMilliseconds, |
| 71 | + end = 1.minutes.inWholeMilliseconds, |
| 72 | + reason = "big", |
| 73 | + ), |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + else -> emptyList() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + companion object { |
| 82 | + private val URL = DemoItem.AppleBasic_16_9_TS_HLS.uri |
| 83 | + private val videoDuration = 1800.05.seconds |
| 84 | + |
| 85 | + private const val ID_START_END = "blocked://StartEnd" |
| 86 | + private const val ID_OVERLAP = "blocked://Overlap" |
| 87 | + private const val ID_INCLUDED = "blocked://Included" |
| 88 | + |
| 89 | + /** |
| 90 | + * [DemoItem] to test [BlockedTimeRange] at start and end of the media. |
| 91 | + */ |
| 92 | + val DemoItemBlockedTimeRangeAtStartAndEnd = DemoItem( |
| 93 | + title = "Starts and ends with a blocked time range", |
| 94 | + uri = ID_START_END, |
| 95 | + description = "Blocked times ranges at 00:00 - 00:10 and 25:00 - 30:00", |
| 96 | + ) |
| 97 | + |
| 98 | + /** |
| 99 | + * [DemoItem] to test overlapping [BlockedTimeRange]. |
| 100 | + */ |
| 101 | + val DemoItemBlockedTimeRangeOverlaps = DemoItem( |
| 102 | + title = "Blocked time ranges are overlapping", |
| 103 | + uri = ID_OVERLAP, |
| 104 | + description = "Blocked times ranges at 00:10 to 00:50 and 00:15 to 05:00" |
| 105 | + ) |
| 106 | + |
| 107 | + /** |
| 108 | + * [DemoItem] to test included [BlockedTimeRange]. |
| 109 | + */ |
| 110 | + val DemoItemBlockedTimeRangeIncluded = DemoItem( |
| 111 | + title = "Blocked time range is included in an other one", |
| 112 | + uri = ID_INCLUDED, |
| 113 | + description = "Blocked times ranges at 00:15 - 00:30 and 00:10 - 01:00" |
| 114 | + ) |
| 115 | + } |
| 116 | +} |
0 commit comments