Skip to content

Commit

Permalink
Aspect ratio control (mpv)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroyami committed Jan 6, 2024
1 parent 5f5f879 commit f8bd9da
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class ExoPlayer : BasePlayer() {
override val canChangeAspectRatio: Boolean
get() = true

override val supportsChapters: Boolean
get() = false

override fun initialize() {
val context = exoView.context

Expand Down Expand Up @@ -273,17 +276,9 @@ class ExoPlayer : BasePlayer() {
}
}

override fun analyzeChapters(mediafile: MediaFile) {
//TODO("Not yet implemented")
}

override fun jumpToChapter(chapter: Chapter) {
//TODO("Not yet implemented")
}

override fun skipChapter() {
//TODO("Not yet implemented")
}
override fun analyzeChapters(mediafile: MediaFile) {}
override fun jumpToChapter(chapter: Chapter) {}
override fun skipChapter() {}

override fun reapplyTrackChoices() {
/* We need to cast MediaController to ExoPlayer since they're roughly the same */
Expand Down Expand Up @@ -432,8 +427,6 @@ class ExoPlayer : BasePlayer() {
override fun switchAspectRatio(): String {
val resolutions = mutableMapOf<Int, String>()

val lyricist = Lyricist("en", Stringies)

resolutions[AspectRatioFrameLayout.RESIZE_MODE_FIT] = lyricist.strings.roomScalingFitScreen
resolutions[AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH] = lyricist.strings.roomScalingFixedWidth
resolutions[AspectRatioFrameLayout.RESIZE_MODE_FIXED_HEIGHT] = lyricist.strings.roomScalingFixedHeight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class MpvPlayer : BasePlayer() {
private lateinit var ctx: Context

override val canChangeAspectRatio: Boolean
get() = false
get() = true

override val supportsChapters: Boolean
get() = true

override fun initialize() {
ctx = mpvView.context.applicationContext
Expand Down Expand Up @@ -319,7 +322,41 @@ class MpvPlayer : BasePlayer() {
return mpvPos
}

override fun switchAspectRatio() = ""
override fun switchAspectRatio(): String {
val currentAspect = MPVLib.getPropertyString("video-aspect-override")
val currentPanscan = MPVLib.getPropertyDouble("panscan")

loggy("currentAspect: $currentAspect and currentPanscan: $currentPanscan", 0)

val aspectRatios = listOf(
"-1.000000" to "Original" ,
"1.777778" to "16:9",
"1.600000" to "16:10",
"1.333333" to "4:3",
"2.350000" to "2.35:1",
"panscan" to "Pan/Scan"
)

var enablePanscan = false
val nextAspect = if (currentPanscan == 1.0) {
aspectRatios[0]
} else if (currentAspect == "2.350000") {
enablePanscan = true
aspectRatios[5]
} else {
aspectRatios[aspectRatios.indexOfFirst { it.first == currentAspect } + 1]
}

if (enablePanscan) {
MPVLib.setPropertyString("video-aspect-override", "-1")
MPVLib.setPropertyDouble("panscan", 1.0)
} else {
MPVLib.setPropertyString("video-aspect-override", nextAspect.first)
MPVLib.setPropertyDouble("panscan", 0.0)
}

return nextAspect.second
}

override fun collectInfoLocal(mediafile: MediaFile) {
collectInfoLocalAndroid(mediafile, ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class BasePlayer {
val playerScopeIO = CoroutineScope(Dispatchers.IO)

abstract val canChangeAspectRatio: Boolean
abstract val supportsChapters: Boolean

/** Called when the player is to be initialized */
abstract fun initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ fun RoomUI() {
if (unlockButtonVisibility.value && !pipModeObserver) {
Card(
modifier = Modifier.width(48.dp).alpha(0.5f).aspectRatio(1f).clickable(
interactionSource = remember { MutableInteractionSource() }, indication = rememberRipple(color = Paletting.SP_ORANGE)
) {
locked = false
hudVisibility = true
},
interactionSource = remember { MutableInteractionSource() }, indication = rememberRipple(color = Paletting.SP_ORANGE)
) {
locked = false
hudVisibility = true
},
shape = RoundedCornerShape(6.dp),
border = BorderStroke(width = 1.dp, brush = Brush.linearGradient(colors = Paletting.SP_GRADIENT)),
colors = CardDefaults.cardColors(containerColor = Color.DarkGray),
Expand Down Expand Up @@ -357,7 +357,7 @@ fun RoomUI() {
brush = Brush.verticalGradient(
listOf(Color.Transparent, blacky, blacky, blacky)
)
).clickable(false){}
).clickable(false) {}
)
}
}
Expand Down Expand Up @@ -570,7 +570,8 @@ fun RoomUI() {
Box {
/** User-info card (toggled on and off) */
if (!isSoloMode) {
FreeAnimatedVisibility(modifier = Modifier.fillMaxWidth(cardWidth).fillMaxHeight(cardHeight),
FreeAnimatedVisibility(
modifier = Modifier.fillMaxWidth(cardWidth).fillMaxHeight(cardHeight),
enter = slideInHorizontally(initialOffsetX = { (dimensions.wPX * 1.3).toInt() }),
exit = slideOutHorizontally(targetOffsetX = { (dimensions.wPX * 1.3).toInt() }),
visible = !inroomprefsVisibility.value && userinfoVisibility.value && !sharedplaylistVisibility.value
Expand All @@ -581,7 +582,8 @@ fun RoomUI() {

/** Shared Playlist card (toggled on and off) */
if (false /* TODO !isSoloMode */) {
FreeAnimatedVisibility(modifier = Modifier.fillMaxWidth(cardWidth).fillMaxHeight(cardHeight),
FreeAnimatedVisibility(
modifier = Modifier.fillMaxWidth(cardWidth).fillMaxHeight(cardHeight),
enter = slideInHorizontally(initialOffsetX = { (dimensions.wPX * 1.3).toInt() }),
exit = slideOutHorizontally(targetOffsetX = { (dimensions.wPX * 1.3).toInt() }),
visible = !inroomprefsVisibility.value && !userinfoVisibility.value && sharedplaylistVisibility.value
Expand All @@ -591,7 +593,8 @@ fun RoomUI() {
}

/** In-room card (toggled on and off) */
FreeAnimatedVisibility(modifier = Modifier.fillMaxWidth(cardWidth).fillMaxHeight(cardHeight),
FreeAnimatedVisibility(
modifier = Modifier.fillMaxWidth(cardWidth).fillMaxHeight(cardHeight),
enter = slideInHorizontally(initialOffsetX = { (dimensions.wPX * 1.3).toInt() }),
exit = slideOutHorizontally(targetOffsetX = { (dimensions.wPX * 1.3).toInt() }),
visible = inroomprefsVisibility.value && !userinfoVisibility.value && !sharedplaylistVisibility.value
Expand All @@ -617,12 +620,10 @@ fun RoomUI() {
modifier = Modifier.padding(6.dp).fillMaxHeight(), verticalArrangement = Arrangement.SpaceEvenly, horizontalArrangement = Arrangement.Center
) {
/* Aspect Ratio */
if (viewmodel?.player?.canChangeAspectRatio == true) {
FancyIcon2(icon = Icons.Filled.AspectRatio, size = ROOM_ICON_SIZE, shadowColor = Color.Black) {
val newAspectRatio = viewmodel?.player?.switchAspectRatio()
if (newAspectRatio != null) {
composeScope.dispatchOSD(newAspectRatio)
}
FancyIcon2(icon = Icons.Filled.AspectRatio, size = ROOM_ICON_SIZE, shadowColor = Color.Black) {
val newAspectRatio = viewmodel?.player?.switchAspectRatio()
if (newAspectRatio != null) {
composeScope.dispatchOSD(newAspectRatio)
}
}

Expand Down Expand Up @@ -779,49 +780,51 @@ fun RoomUI() {


/* Chapters */
Box {
var chaptersPopup by remember { mutableStateOf(false) }
if (viewmodel?.player?.supportsChapters == true) {
Box {
var chaptersPopup by remember { mutableStateOf(false) }

FancyIcon2(icon = Icons.Filled.Theaters, size = ROOM_ICON_SIZE, shadowColor = Color.Black) {
viewmodel?.player?.analyzeChapters(viewmodel?.media ?: return@FancyIcon2)
chaptersPopup = !chaptersPopup
}

DropdownMenu(modifier = Modifier.background(color = MaterialTheme.colorScheme.tertiaryContainer),
expanded = chaptersPopup,
properties = PopupProperties(
dismissOnBackPress = true, focusable = true, dismissOnClickOutside = true
),
onDismissRequest = { chaptersPopup = false }
) {
FancyIcon2(icon = Icons.Filled.Theaters, size = ROOM_ICON_SIZE, shadowColor = Color.Black) {
viewmodel?.player?.analyzeChapters(viewmodel?.media ?: return@FancyIcon2)
chaptersPopup = !chaptersPopup
}

ComposeUtils.FancyText2(
modifier = Modifier.align(Alignment.CenterHorizontally),
string = "Chapters", solid = Color.Black, size = 14f, font = directive
)
DropdownMenu(modifier = Modifier.background(color = MaterialTheme.colorScheme.tertiaryContainer),
expanded = chaptersPopup,
properties = PopupProperties(
dismissOnBackPress = true, focusable = true, dismissOnClickOutside = true
),
onDismissRequest = { chaptersPopup = false }
) {

DropdownMenuItem(text = {
Row(verticalAlignment = CenterVertically) {
Text(
color = Color.LightGray, text = "Skip chapter"
)
}
}, onClick = {
viewmodel?.player?.skipChapter()
chaptersPopup = false
})
ComposeUtils.FancyText2(
modifier = Modifier.align(Alignment.CenterHorizontally),
string = "Chapters", solid = Color.Black, size = 14f, font = directive
)

for (chapter in (viewmodel?.media?.chapters ?: listOf())) {
DropdownMenuItem(text = {
Row(verticalAlignment = CenterVertically) {
Text(
color = Color.LightGray, text = chapter.name
color = Color.LightGray, text = "Skip chapter"
)
}
}, onClick = {
viewmodel?.player?.jumpToChapter(chapter)
viewmodel?.player?.skipChapter()
chaptersPopup = false
})

for (chapter in (viewmodel?.media?.chapters ?: listOf())) {
DropdownMenuItem(text = {
Row(verticalAlignment = CenterVertically) {
Text(
color = Color.LightGray, text = chapter.name
)
}
}, onClick = {
viewmodel?.player?.jumpToChapter(chapter)
chaptersPopup = false
})
}
}
}
}
Expand Down Expand Up @@ -950,8 +953,8 @@ fun RoomUI() {
modifier = Modifier
.background(color = MaterialTheme.colorScheme.tertiaryContainer),
expanded = addmediacardvisible, properties = PopupProperties(
dismissOnBackPress = true, focusable = true, dismissOnClickOutside = true
), onDismissRequest = { addmediacardvisible = false }) {
dismissOnBackPress = true, focusable = true, dismissOnClickOutside = true
), onDismissRequest = { addmediacardvisible = false }) {

ComposeUtils.FancyText2(
modifier = Modifier.align(Alignment.CenterHorizontally).padding(horizontal = 2.dp), string = "Add media", solid = Color.Black, size = 14f, font = directive
Expand Down

0 comments on commit f8bd9da

Please sign in to comment.