Skip to content

Commit

Permalink
Rewrite logic of reapplying track choices
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroyami committed Apr 4, 2024
1 parent ebe514b commit 445372b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,27 +251,29 @@ class ExoPlayer : BasePlayer() {
}
}

override fun selectTrack(type: TRACKTYPE, index: Int) {
override fun selectTrack(track: Track?, type: TRACKTYPE) {
val exoTrack = track as? ExoTrack

val builder = exoplayer?.trackSelector?.parameters?.buildUpon() ?: return

/* First, clearing our subtitle track selection (This helps troubleshoot many issues */
exoplayer?.trackSelector?.parameters = builder.clearOverridesOfType(type.getExoType()).build()
viewmodel?.currentTrackChoices?.lastSubtitleOverride = null

/* Now, selecting our subtitle track should one be selected */
if (index >= 0) {
if (exoTrack != null) {
when (type) {
TRACKTYPE.SUBTITLE -> {
viewmodel?.currentTrackChoices?.lastSubtitleOverride = TrackSelectionOverride(
(viewmodel?.media!!.subtitleTracks[index] as ExoTrack).trackGroup,
viewmodel?.media!!.subtitleTracks[index].index
exoTrack.trackGroup,
exoTrack.index
)
}

TRACKTYPE.AUDIO -> {
viewmodel?.currentTrackChoices?.lastSubtitleOverride = TrackSelectionOverride(
(viewmodel?.media!!.audioTracks[index] as ExoTrack).trackGroup,
viewmodel?.media!!.audioTracks[index].index
exoTrack.trackGroup,
exoTrack.index
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.yuroyami.syncplay.models.Track
import com.yuroyami.syncplay.player.BasePlayer
import com.yuroyami.syncplay.player.PlayerUtils.trackProgress
import com.yuroyami.syncplay.player.exo.ExoPlayer
import com.yuroyami.syncplay.player.vlc.VlcPlayer
import com.yuroyami.syncplay.protocol.JsonSender
import com.yuroyami.syncplay.utils.RoomUtils.checkFileMismatches
import com.yuroyami.syncplay.utils.RoomUtils.sendPlayback
Expand Down Expand Up @@ -140,26 +141,26 @@ class MpvPlayer : BasePlayer() {
}
}

override fun selectTrack(type: TRACKTYPE, index: Int) {
override fun selectTrack(track: Track?, type: TRACKTYPE) {
when (type) {
TRACKTYPE.SUBTITLE -> {
if (index >= 0) {
MPVLib.setPropertyInt("sid", index)
} else if (index == -1) {
if (track != null) {
MPVLib.setPropertyInt("sid", track.index)
} else {
MPVLib.setPropertyString("sid", "no")
}

viewmodel?.currentTrackChoices?.subtitleSelectionIndexMpv = index
viewmodel?.currentTrackChoices?.subtitleSelectionIndexMpv = track?.index ?: -1
}

TRACKTYPE.AUDIO -> {
if (index >= 0) {
MPVLib.setPropertyInt("aid", index)
} else if (index == -1) {
if (track != null) {
MPVLib.setPropertyInt("aid", track.index)
} else {
MPVLib.setPropertyString("aid", "no")
}

viewmodel?.currentTrackChoices?.audioSelectionIndexMpv = index
viewmodel?.currentTrackChoices?.audioSelectionIndexMpv = track?.index ?: -1
}
}
}
Expand Down Expand Up @@ -194,9 +195,24 @@ class MpvPlayer : BasePlayer() {
val subIndex = viewmodel?.currentTrackChoices?.subtitleSelectionIndexMpv
val audioIndex = viewmodel?.currentTrackChoices?.audioSelectionIndexMpv

val ccMap = viewmodel?.media?.subtitleTracks
val audioMap = viewmodel?.media?.subtitleTracks

val ccGet = ccMap?.firstOrNull { it.index == subIndex }
val audioGet = audioMap?.firstOrNull { it.index == audioIndex }

with(viewmodel?.player ?: return) {
if (subIndex != null) selectTrack(TRACKTYPE.SUBTITLE, subIndex)
if (audioIndex != null) selectTrack(TRACKTYPE.AUDIO, audioIndex)
if (subIndex == -1) {
selectTrack(null, TRACKTYPE.SUBTITLE)
} else if (ccGet != null) {
selectTrack(ccGet, TRACKTYPE.SUBTITLE)
}

if (audioIndex == -1) {
selectTrack(null, TRACKTYPE.AUDIO)
} else if (audioGet != null) {
selectTrack(audioGet, TRACKTYPE.AUDIO)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,28 @@ class VlcPlayer : BasePlayer() {
}
}

override fun selectTrack(type: TRACKTYPE, index: Int) {
override fun selectTrack(track: com.yuroyami.syncplay.models.Track?, type: TRACKTYPE) {
val vlcTrack = track as? VlcTrack

when (type) {
TRACKTYPE.SUBTITLE -> {
if (index >= 0) {
vlcPlayer?.selectTrack((viewmodel?.media?.subtitleTracks?.get(index) as VlcTrack).id)
} else if (index == -1) {
if (vlcTrack != null) {
vlcPlayer?.selectTrack(vlcTrack.id)
} else {
vlcPlayer?.unselectTrackType(Track.Type.Text)
}

viewmodel?.currentTrackChoices?.subtitleSelectionIndexVlc = index
viewmodel?.currentTrackChoices?.subtitleSelectionIdVlc = vlcTrack?.id ?: "-1"
}

TRACKTYPE.AUDIO -> {
if (index >= 0) {
vlcPlayer?.selectTrack((viewmodel?.media?.subtitleTracks?.get(index) as VlcTrack).id)
} else if (index == -1) {
if (vlcTrack != null) {
vlcPlayer?.selectTrack(vlcTrack.id)
} else {
vlcPlayer?.unselectTrackType(Track.Type.Audio)
}

viewmodel?.currentTrackChoices?.audioSelectionIndexVlc = index
viewmodel?.currentTrackChoices?.audioSelectionIdVlc = vlcTrack?.id ?: "-1"
}
}
}
Expand Down Expand Up @@ -180,13 +182,27 @@ class VlcPlayer : BasePlayer() {
}

override fun reapplyTrackChoices() {
val subId = viewmodel?.currentTrackChoices?.subtitleSelectionIdVlc
val audioId = viewmodel?.currentTrackChoices?.audioSelectionIdVlc

val ccMap = viewmodel?.media?.subtitleTracks?.map { it as VlcTrack }
val audioMap = viewmodel?.media?.subtitleTracks?.map { it as VlcTrack }

val subIndex = viewmodel?.currentTrackChoices?.subtitleSelectionIndexMpv
val audioIndex = viewmodel?.currentTrackChoices?.audioSelectionIndexMpv
val ccGet = ccMap?.firstOrNull { it.id == subId }
val audioGet = audioMap?.firstOrNull { it.id == audioId }

with(viewmodel?.player ?: return) {
if (subIndex != null) selectTrack(TRACKTYPE.SUBTITLE, subIndex)
if (audioIndex != null) selectTrack(TRACKTYPE.AUDIO, audioIndex)
if (subId == "-1") {
selectTrack(null, TRACKTYPE.SUBTITLE)
} else if (ccGet != null) {
selectTrack(ccGet, TRACKTYPE.SUBTITLE)
}

if (audioId == "-1") {
selectTrack(null, TRACKTYPE.AUDIO)
} else if (audioGet != null) {
selectTrack(audioGet, TRACKTYPE.AUDIO)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class TrackChoices {
var subtitleSelectionIndexMpv: Int? = null

/* vlc */
var audioSelectionIndexVlc: Int? = null
var subtitleSelectionIndexVlc: Int? = null
var audioSelectionIdVlc: String? = null
var subtitleSelectionIdVlc: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.ui.Modifier
import com.eygraber.uri.Uri
import com.yuroyami.syncplay.models.Chapter
import com.yuroyami.syncplay.models.MediaFile
import com.yuroyami.syncplay.models.Track
import com.yuroyami.syncplay.utils.sha256
import com.yuroyami.syncplay.utils.toHex
import com.yuroyami.syncplay.watchroom.viewmodel
Expand Down Expand Up @@ -72,7 +73,7 @@ abstract class BasePlayer {
/** Called when the player ought to analyze the tracks of the currently loaded media */
abstract fun analyzeTracks(mediafile: MediaFile)

abstract fun selectTrack(type: TRACKTYPE, index: Int)
abstract fun selectTrack(track: Track?, type: TRACKTYPE)

abstract fun analyzeChapters(mediafile: MediaFile)
abstract fun jumpToChapter(chapter: Chapter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.yuroyami.syncplay.settings.valueBlockingly
import com.yuroyami.syncplay.watchroom.GestureCallback
import com.yuroyami.syncplay.watchroom.RoomCallback
import com.yuroyami.syncplay.watchroom.RoomUI
import com.yuroyami.syncplay.watchroom.gestureCallback
import com.yuroyami.syncplay.watchroom.homeCallback
import com.yuroyami.syncplay.watchroom.prepareProtocol
import com.yuroyami.syncplay.watchroom.viewmodel
Expand Down Expand Up @@ -58,6 +59,7 @@ val delegato = AppleDelegate().also {

var pipcontroller: AVPictureInPictureController? = null

@Suppress("CONFLICTING_OVERLOADS")
class AppleDelegate : NSObject(), UIApplicationDelegateProtocol {

var myOrientationMask: UIInterfaceOrientationMask = UIInterfaceOrientationMaskPortrait
Expand All @@ -74,14 +76,12 @@ class AppleDelegate : NSObject(), UIApplicationDelegateProtocol {
return myOrientationMask
}

@Suppress("CONFLICTING_OVERLOADS")
override fun application(application: UIApplication, didFinishLaunchingWithOptions: Map<Any?, *>?): Boolean {
(didFinishLaunchingWithOptions?.get(UIApplicationLaunchOptionsShortcutItemKey) as? UIApplicationShortcutItem)
?.let { handleShortcut(it) }
return false
}

@Suppress("CONFLICTING_OVERLOADS")
override fun application(application: UIApplication, willFinishLaunchingWithOptions: Map<Any?, *>?): Boolean {
(willFinishLaunchingWithOptions?.get(UIApplicationLaunchOptionsShortcutItemKey) as? UIApplicationShortcutItem)
?.let { handleShortcut(it) }
Expand All @@ -107,7 +107,8 @@ fun SyncplayController() = ComposeUIViewController(configure = {

when (room) {
true -> {
viewmodel?.roomCallback = Room
viewmodel?.roomCallback = remember { Room }
gestureCallback = remember { AppleGesture }

LaunchedEffect(null) {
delegato.myOrientationMask = UIInterfaceOrientationMaskLandscape
Expand Down Expand Up @@ -295,37 +296,35 @@ object AppleLifecycleWatchdog: ComposeUIViewControllerDelegate {
}
}

object AppleGesture: GestureCallback {
override fun getMaxVolume() = 100
object AppleGesture : GestureCallback {
private const val MAX_VOLUME = 100
private const val MAX_BRIGHTNESS = 1.0f

override fun getMaxVolume() = MAX_VOLUME

override fun getCurrentVolume(): Int {
//Volume in iOS is only relative, and max is 100% (1f)
return (((viewmodel?.player as? AvPlayer)?.avPlayer?.volume)?.times(100f))?.roundToInt()
?: (((viewmodel?.player as? VlcPlayer)?.vlcPlayer?.pitch)?.times(100f))?.roundToInt()
?: 0
}
override fun changeCurrentVolume(v: Int) {
val epsilon = 1e-2
if (v.toFloat() >= 0.0f - epsilon && v.toFloat() <= 1.0f + epsilon) {
// Volume is within the range [0.0, 1.0] with precision up to two decimal places
val clampedVolume = (v.toFloat()).coerceIn(0.0f, 1.0f)
(viewmodel?.player as? AvPlayer)?.avPlayer?.setVolume(clampedVolume)
(viewmodel?.player as? VlcPlayer)?.vlcPlayer?.setPitch(clampedVolume)

} else {
// Volume is outside the range [0.0, 1.0] with precision up to two decimal places
val clampedVolume = (v.toFloat() / 100f).coerceIn(0.0f, 1.0f)
(viewmodel?.player as? AvPlayer)?.avPlayer?.setVolume(clampedVolume)
(viewmodel?.player as? VlcPlayer)?.vlcPlayer?.setPitch(clampedVolume)
val avPlayer = (viewmodel?.player as? AvPlayer)?.avPlayer
val vlcPlayer = (viewmodel?.player as? VlcPlayer)?.vlcPlayer

return when {
avPlayer != null -> (avPlayer.volume * MAX_VOLUME).roundToInt()
vlcPlayer != null -> (vlcPlayer.pitch * MAX_VOLUME).roundToInt()
else -> 0
}
}

override fun getMaxBrightness(): Float = 1.0f
override fun changeCurrentVolume(v: Int) {
val clampedVolume = v.toFloat().coerceIn(0.0f, MAX_VOLUME.toFloat()) / MAX_VOLUME

override fun getCurrentBrightness(): Float {
return UIScreen.mainScreen.brightness.toFloat()
(viewmodel?.player as? AvPlayer)?.avPlayer?.setVolume(clampedVolume)
(viewmodel?.player as? VlcPlayer)?.vlcPlayer?.setPitch(clampedVolume)
}

override fun getMaxBrightness() = MAX_BRIGHTNESS

override fun getCurrentBrightness(): Float = UIScreen.mainScreen.brightness.toFloat()

override fun changeCurrentBrightness(v: Float) {
UIScreen.mainScreen.brightness = v.coerceIn(0.0f, 1.0f).toDouble()
UIScreen.mainScreen.brightness = v.coerceIn(0.0f, MAX_BRIGHTNESS).toDouble()
}
}

0 comments on commit 445372b

Please sign in to comment.