Skip to content

Commit

Permalink
Merge branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed May 12, 2024
2 parents 2cee2f6 + 751b937 commit 1f14691
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 43 deletions.
39 changes: 16 additions & 23 deletions app/src/main/java/is/xyz/mpv/MPVActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package `is`.xyz.mpv
import `is`.xyz.mpv.databinding.PlayerBinding
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.SuppressLint
import androidx.appcompat.app.AlertDialog
import android.app.PictureInPictureParams
import android.app.RemoteAction
Expand Down Expand Up @@ -207,8 +206,8 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
unlockBtn.setOnClickListener { unlockUI() }
playbackDurationTxt.setOnClickListener {
useTimeRemaining = !useTimeRemaining
updatePlaybackPos(psc.position_s)
updatePlaybackDuration(psc.duration_s)
updatePlaybackPos(psc.positionSec)
updatePlaybackDuration(psc.durationSec)
}

cycleAudioBtn.setOnLongClickListener { pickAudio(); true }
Expand Down Expand Up @@ -1402,8 +1401,8 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
private fun refreshUi() {
// forces update of entire UI, used when resuming the activity
updatePlaybackStatus(psc.pause)
updatePlaybackPos(psc.position_s)
updatePlaybackDuration(psc.duration_s)
updatePlaybackPos(psc.positionSec)
updatePlaybackDuration(psc.durationSec)
updateAudioUI()
updateOrientation()
updateMetadataDisplay()
Expand Down Expand Up @@ -1477,7 +1476,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
fun updatePlaybackPos(position: Int) {
binding.playbackPositionTxt.text = Utils.prettyTime(position)
if (useTimeRemaining) {
val diff = psc.duration_s - position
val diff = psc.durationSec - position
binding.playbackDurationTxt.text = if (diff <= 0)
"-00:00"
else
Expand Down Expand Up @@ -1520,7 +1519,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
}

private fun updateSpeedButton() {
binding.cycleSpeedBtn.text = getString(R.string.ui_speed, player.playbackSpeed)
binding.cycleSpeedBtn.text = getString(R.string.ui_speed, psc.speed)
}

private fun updatePlaylistButtons() {
Expand Down Expand Up @@ -1559,13 +1558,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
if (initial || player.vid == -1)
return

var ratio = player.videoAspect?.toFloat() ?: 0f
if (ratio != 0f) {
if ((player.videoOutRotation ?: 0) % 180 == 90)
ratio = 1f / ratio
}
Log.v(TAG, "auto rotation: aspect ratio = $ratio")

val ratio = player.getVideoOutAspect()?.toFloat() ?: 0f
if (ratio == 0f || ratio in (1f / ASPECT_RATIO_MIN) .. ASPECT_RATIO_MIN) {
// video is square, let Android do what it wants
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
Expand All @@ -1592,7 +1585,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
}

val params = with(PictureInPictureParams.Builder()) {
val aspect = player.videoAspect ?: 1.0
val aspect = player.getVideoOutAspect() ?: 0.0
setAspectRatio(Rational(aspect.times(10000).toInt(), 10000))
setActions(listOf(action1))
}
Expand Down Expand Up @@ -1652,8 +1645,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
if (!activityIsForeground) return
when (property) {
"track-list" -> player.loadTracks()
"speed" -> updateSpeedButton()
"video-params/aspect" -> {
"video-out-params/aspect", "video-out-params/rotate" -> {
updateOrientation()
updatePiPParams()
}
Expand All @@ -1680,15 +1672,16 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse

private fun eventPropertyUi(property: String, value: String, triggerMetaUpdate: Boolean) {
if (!activityIsForeground) return
when (property) {
"speed" -> updateSpeedButton()
}
if (triggerMetaUpdate)
updateMetadataDisplay()
}

private fun eventUi(eventId: Int) {
if (!activityIsForeground) return
when (eventId) {
MPVLib.mpvEventId.MPV_EVENT_PLAYBACK_RESTART -> updatePlaybackStatus(player.paused!!)
}
// empty
}

override fun eventProperty(property: String) {
Expand Down Expand Up @@ -1778,7 +1771,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
PropertyChange.Init -> {
mightWantToToggleControls = false

initialSeek = psc.position_s
initialSeek = psc.positionSec
initialBright = Utils.getScreenBrightness(this) ?: 0.5f
with (audioManager!!) {
initialVolume = getStreamVolume(AudioManager.STREAM_MUSIC)
Expand All @@ -1795,7 +1788,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
}
PropertyChange.Seek -> {
// disable seeking when duration is unknown
val duration = psc.duration_s
val duration = psc.durationSec
if (duration == 0 || initialSeek < 0)
return
if (smoothSeekGesture && pausedForSeek == 0) {
Expand Down Expand Up @@ -1843,7 +1836,7 @@ class MPVActivity : AppCompatActivity(), MPVLib.EventObserver, TouchGesturesObse
/* Tap gestures */
PropertyChange.SeekFixed -> {
val seekTime = diff * 10f
val newPos = psc.position_s + seekTime.toInt() // only for display
val newPos = psc.positionSec + seekTime.toInt() // only for display
MPVLib.command(arrayOf("seek", seekTime.toString(), "relative"))

val diffText = Utils.prettyTime(seekTime.toInt(), true)
Expand Down
21 changes: 19 additions & 2 deletions app/src/main/java/is/xyz/mpv/MPVView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ class MPVView(context: Context, attrs: AttributeSet) : SurfaceView(context, attr
Property("pause", MPV_FORMAT_FLAG),
Property("eof-reached", MPV_FORMAT_FLAG),
Property("paused-for-cache", MPV_FORMAT_FLAG),
Property("speed"),
Property("speed", MPV_FORMAT_STRING),
Property("track-list"),
// observing double properties is not hooked up in the JNI code, but doing this
// will restrict updates to when it actually changes
Property("video-params/aspect", MPV_FORMAT_DOUBLE),
Property("video-out-params/aspect", MPV_FORMAT_DOUBLE),
Property("video-out-params/rotate", MPV_FORMAT_DOUBLE),
//
Property("playlist-pos", MPV_FORMAT_INT64),
Property("playlist-count", MPV_FORMAT_INT64),
Expand Down Expand Up @@ -352,6 +353,22 @@ class MPVView(context: Context, attrs: AttributeSet) : SurfaceView(context, attr
val videoOutRotation: Int?
get() = MPVLib.getPropertyInt("video-out-params/rotate")

/**
* Returns the video aspect ratio after video filters (before VO).
* Rotation is taken into account.
*/
fun getVideoOutAspect(): Double? {
return videoAspect?.let {
if (it < 0.001)
return 0.0
val rot = videoOutRotation ?: 0
if (rot % 180 == 90)
1.0 / it
else
it
}
}

class TrackDelegate(private val name: String) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
val v = MPVLib.getPropertyString(name)
Expand Down
41 changes: 27 additions & 14 deletions app/src/main/java/is/xyz/mpv/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,36 +226,48 @@ object Utils {
}
}

// does about 200% more than AudioMetadata

/**
* Helper class that keeps much more state than <code>AudioMetadata</code>, in order to facilitate
* updating a media session.
* @see MediaSessionCompat
*/
class PlaybackStateCache {
val meta = AudioMetadata()
var cachePause = false
private set
var pause = false
private set
var position = -1L // in ms
/** playback position in ms */
var position = -1L
private set
var duration = 0L // in ms
/** duration in ms */
var duration = 0L
private set
var playlistPos = 0
private set
var playlistCount = 0
private set
var speed = 1f
private set

val position_s get() = (position / 1000).toInt()
val duration_s get() = (duration / 1000).toInt()

fun reset() {
position = -1
duration = 0
}
/** playback position in seconds */
val positionSec get() = (position / 1000).toInt()
/** duration in seconds */
val durationSec get() = (duration / 1000).toInt()

/** callback for properties of type <code>MPV_FORMAT_STRING</code> */
fun update(property: String, value: String): Boolean {
if (meta.update(property, value))
return true
return false
when (property) {
"speed" -> speed = value.toFloat()
else -> return false
}
return true
}

/** callback for properties of type <code>MPV_FORMAT_FLAG</code> */
fun update(property: String, value: Boolean): Boolean {
when (property) {
"pause" -> pause = value
Expand All @@ -265,6 +277,7 @@ object Utils {
return true
}

/** callback for properties of type <code>MPV_FORMAT_INT64</code> */
fun update(property: String, value: Long): Boolean {
when (property) {
"time-pos" -> position = value * 1000
Expand All @@ -276,8 +289,8 @@ object Utils {
return true
}

private var mediaMetadataBuilder = MediaMetadataCompat.Builder()
private var playbackStateBuilder = PlaybackStateCompat.Builder()
private val mediaMetadataBuilder = MediaMetadataCompat.Builder()
private val playbackStateBuilder = PlaybackStateCompat.Builder()

private fun buildMediaMetadata(includeThumb: Boolean): MediaMetadataCompat {
// TODO could provide: genre, num_tracks, track_number, year
Expand Down Expand Up @@ -316,7 +329,7 @@ object Utils {
PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
}
return with (playbackStateBuilder) {
setState(stateInt, position, 1.0f)
setState(stateInt, position, speed)
setActions(actions)
//setActiveQueueItemId(0) TODO
build()
Expand Down
8 changes: 6 additions & 2 deletions buildscripts/buildall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ loadarch () {
cc_triple=$ndk_triple$apilvl
prefix_name=x86_64
else
echo "Invalid architecture"
echo "Invalid architecture" >&2
exit 1
fi
export prefix_dir="$PWD/prefix/$prefix_name"
Expand Down Expand Up @@ -83,7 +83,7 @@ cpp = '$CXX'
ar = 'llvm-ar'
nm = 'llvm-nm'
strip = 'llvm-strip'
pkg-config = 'pkg-config'
pkgconfig = 'pkg-config'
[host_machine]
system = 'android'
cpu_family = '$cpu_family'
Expand Down Expand Up @@ -153,6 +153,10 @@ while [ $# -gt 0 ]; do
-h|--help)
usage
;;
-*)
echo "Unknown flag $1" >&2
exit 1
;;
*)
target=$1
;;
Expand Down
3 changes: 1 addition & 2 deletions buildscripts/include/depinfo.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/bash -e

## Dependency versions
# Make sure to keep v_ndk and v_ndk_n in sync, the numeric version can be found in source.properties
# also remember to update path.sh
# Make sure to keep v_ndk and v_ndk_n in sync, both are listed on the NDK download page

v_sdk=11076708_latest
v_ndk=r26d
Expand Down

0 comments on commit 1f14691

Please sign in to comment.