Skip to content

Commit

Permalink
Merge pull request #6761 from Bnyro/master
Browse files Browse the repository at this point in the history
refactor: merge VideoOnlinePlayerService with OnlinePlayerService
  • Loading branch information
Bnyro authored Nov 18, 2024
2 parents 9acb486 + e244ded commit 5bae646
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 576 deletions.
7 changes: 6 additions & 1 deletion app/src/main/java/com/github/libretube/api/obj/Segment.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.libretube.api.obj

import android.os.Parcelable
import androidx.collection.FloatFloatPair
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient

@Serializable
@Parcelize
data class Segment(
@SerialName("UUID") val uuid: String? = null,
val actionType: String? = null,
Expand All @@ -17,7 +21,8 @@ data class Segment(
val videoDuration: Double? = null,
val votes: Int? = null,
var skipped: Boolean = false
) {
): Parcelable {
@Transient
@IgnoredOnParcel
val segmentStartAndEnd = FloatFloatPair(segment[0], segment[1])
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ object IntentData {
const val maxAudioQuality = "maxAudioQuality"
const val audioLanguage = "audioLanguage"
const val captionLanguage = "captionLanguage"
const val wasIntentStopped = "wasIntentStopped"
const val tabData = "tabData"
const val videoList = "videoList"
const val nextPage = "nextPage"
Expand All @@ -57,4 +56,5 @@ object IntentData {
const val downloadInfo = "downloadInfo"
const val streams = "streams"
const val chapters = "chapters"
const val segments = "segments"
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/github/libretube/enums/PlayerCommand.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.github.libretube.enums

enum class PlayerCommand {
START_PLAYBACK,
SKIP_SILENCE,
SET_VIDEO_TRACK_TYPE_DISABLED,
SET_AUDIO_ROLE_FLAGS,
SET_RESOLUTION,
SET_AUDIO_LANGUAGE,
SET_SUBTITLE
SET_SUBTITLE,
SET_SB_AUTO_SKIP_ENABLED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import com.github.libretube.db.obj.DownloadChapter
import com.github.libretube.db.obj.DownloadWithItems

@OptIn(UnstableApi::class)
fun MediaItem.Builder.setMetadata(streams: Streams) = apply {
fun MediaItem.Builder.setMetadata(streams: Streams, videoId: String) = apply {
val extras = bundleOf(
MediaMetadataCompat.METADATA_KEY_TITLE to streams.title,
MediaMetadataCompat.METADATA_KEY_ARTIST to streams.uploader,
IntentData.videoId to videoId,
IntentData.streams to streams,
IntentData.chapters to streams.chapters
)
setMediaMetadata(
Expand All @@ -27,6 +29,8 @@ fun MediaItem.Builder.setMetadata(streams: Streams) = apply {
.setArtworkUri(streams.thumbnailUrl.toUri())
.setComposer(streams.uploaderUrl.toID())
.setExtras(extras)
// send a unique timestamp to notify that the metadata changed, even if playing the same video twice
.setTrackNumber(System.currentTimeMillis().mod(Int.MAX_VALUE))
.build()
)
}
Expand All @@ -38,6 +42,7 @@ fun MediaItem.Builder.setMetadata(downloadWithItems: DownloadWithItems) = apply
val extras = bundleOf(
MediaMetadataCompat.METADATA_KEY_TITLE to download.title,
MediaMetadataCompat.METADATA_KEY_ARTIST to download.uploader,
IntentData.videoId to download.videoId,
IntentData.chapters to chapters.map(DownloadChapter::toChapterSegment)
)
setMediaMetadata(
Expand All @@ -47,6 +52,8 @@ fun MediaItem.Builder.setMetadata(downloadWithItems: DownloadWithItems) = apply
.setDurationMs(download.duration?.times(1000))
.setArtworkUri(download.thumbnailPath?.toAndroidUri())
.setExtras(extras)
// send a unique timestamp to notify that the metadata changed, even if playing the same video twice
.setTrackNumber(System.currentTimeMillis().mod(Int.MAX_VALUE))
.build()
)
}
44 changes: 23 additions & 21 deletions app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.pm.ActivityInfo
import android.net.Uri
import android.util.Base64
import android.view.accessibility.CaptioningManager
import android.widget.Toast
import androidx.annotation.OptIn
import androidx.annotation.StringRes
import androidx.core.app.PendingIntentCompat
Expand Down Expand Up @@ -42,6 +41,7 @@ import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.PlayerEvent
import com.github.libretube.enums.SbSkipOptions
import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.extensions.togglePlayPauseState
import com.github.libretube.extensions.updateParameters
import com.github.libretube.obj.VideoStats
Expand Down Expand Up @@ -333,13 +333,13 @@ object PlayerHelper {
false
)

val enabledVideoCodecs: String
private val enabledVideoCodecs: String
get() = PreferenceHelper.getString(
PreferenceKeys.ENABLED_VIDEO_CODECS,
"all"
)

val enabledAudioCodecs: String
private val enabledAudioCodecs: String
get() = PreferenceHelper.getString(
PreferenceKeys.ENABLED_AUDIO_CODECS,
"all"
Expand Down Expand Up @@ -601,33 +601,35 @@ object PlayerHelper {
fun Player.checkForSegments(
context: Context,
segments: List<Segment>,
sponsorBlockConfig: MutableMap<String, SbSkipOptions>
sponsorBlockConfig: MutableMap<String, SbSkipOptions>,
skipAutomaticallyIfEnabled: Boolean
): Segment? {
for (segment in segments.filter { it.category != SPONSOR_HIGHLIGHT_CATEGORY }) {
val (start, end) = segment.segmentStartAndEnd
val (segmentStart, segmentEnd) = (start * 1000f).toLong() to (end * 1000f).toLong()

// avoid seeking to the same segment multiple times, e.g. when the SB segment is at the end of the video
if ((duration - currentPosition).absoluteValue < 500) continue

if (currentPosition in segmentStart until segmentEnd) {
val key = sponsorBlockConfig[segment.category]
if (key == SbSkipOptions.AUTOMATIC ||
(key == SbSkipOptions.AUTOMATIC_ONCE && !segment.skipped)
) {
if (sponsorBlockNotifications) {
runCatching {
Toast.makeText(context, R.string.segment_skipped, Toast.LENGTH_SHORT)
.show()
}
if (currentPosition !in segmentStart until segmentEnd) continue

val key = sponsorBlockConfig[segment.category]

if (!skipAutomaticallyIfEnabled || key == SbSkipOptions.MANUAL ||
(key == SbSkipOptions.AUTOMATIC_ONCE && segment.skipped)
) {
return segment
} else if (key == SbSkipOptions.AUTOMATIC ||
(key == SbSkipOptions.AUTOMATIC_ONCE && !segment.skipped)
) {
if (sponsorBlockNotifications) {
runCatching {
context.toastFromMainThread(R.string.segment_skipped)
}
seekTo(segmentEnd)
segment.skipped = true
} else if (key == SbSkipOptions.MANUAL ||
(key == SbSkipOptions.AUTOMATIC_ONCE && segment.skipped)
) {
return segment
}
seekTo(segmentEnd)
segment.skipped = true
} else {
return null
}
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import androidx.media3.session.MediaSession
import androidx.media3.session.SessionCommand
import androidx.media3.session.SessionResult
import com.github.libretube.R
import com.github.libretube.api.obj.Subtitle
import com.github.libretube.enums.PlayerCommand
import com.github.libretube.enums.PlayerEvent
import com.github.libretube.extensions.parcelable
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.PlayerHelper
Expand Down Expand Up @@ -110,8 +112,53 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio

open fun runPlayerCommand(args: Bundle) {
when {
args.containsKey(PlayerCommand.SKIP_SILENCE.name) ->
exoPlayer?.skipSilenceEnabled = args.getBoolean(PlayerCommand.SKIP_SILENCE.name)
args.containsKey(PlayerCommand.SKIP_SILENCE.name) -> exoPlayer?.skipSilenceEnabled =
args.getBoolean(PlayerCommand.SKIP_SILENCE.name)

args.containsKey(PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name) -> trackSelector?.updateParameters {
setTrackTypeDisabled(
C.TRACK_TYPE_VIDEO,
args.getBoolean(PlayerCommand.SET_VIDEO_TRACK_TYPE_DISABLED.name)
)
}

args.containsKey(PlayerCommand.SET_AUDIO_ROLE_FLAGS.name) -> {
trackSelector?.updateParameters {
setPreferredAudioRoleFlags(args.getInt(PlayerCommand.SET_AUDIO_ROLE_FLAGS.name))
}
}

args.containsKey(PlayerCommand.SET_AUDIO_LANGUAGE.name) -> {
trackSelector?.updateParameters {
setPreferredAudioLanguage(args.getString(PlayerCommand.SET_AUDIO_LANGUAGE.name))
}
}

args.containsKey(PlayerCommand.SET_RESOLUTION.name) -> {
trackSelector?.updateParameters {
val resolution = args.getInt(PlayerCommand.SET_RESOLUTION.name)
setMinVideoSize(Int.MIN_VALUE, resolution)
setMaxVideoSize(Int.MAX_VALUE, resolution)
}
}

args.containsKey(PlayerCommand.SET_SUBTITLE.name) -> {
val subtitle: Subtitle? = args.parcelable(PlayerCommand.SET_SUBTITLE.name)

trackSelector?.updateParameters {
val roleFlags = if (subtitle?.code != null) getSubtitleRoleFlags(subtitle) else 0
setPreferredTextRoleFlags(roleFlags)
setPreferredTextLanguage(subtitle?.code)
}
}
}
}

fun getSubtitleRoleFlags(subtitle: Subtitle?): Int {
return if (subtitle?.autoGenerated != true) {
C.ROLE_FLAG_CAPTION
} else {
PlayerHelper.ROLE_FLAG_AUTO_GEN_SUBTITLE
}
}

Expand Down
Loading

0 comments on commit 5bae646

Please sign in to comment.