Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to set a different default resolution when not in fullscreen #4849

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import com.github.libretube.extensions.updateParameters
import com.github.libretube.obj.VideoStats
import com.github.libretube.util.TextUtils
import kotlinx.coroutines.runBlocking
import java.util.Locale
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
import kotlinx.coroutines.runBlocking

object PlayerHelper {
private const val ACTION_MEDIA_CONTROL = "media_control"
Expand Down Expand Up @@ -324,13 +324,17 @@
true
)

fun getDefaultResolution(context: Context): String {
val prefKey = if (NetworkHelper.isNetworkMetered(context)) {
fun getDefaultResolution(context: Context, isFullscreen: Boolean): Int? {
var prefKey = if (NetworkHelper.isNetworkMetered(context)) {
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE
} else {
PreferenceKeys.DEFAULT_RESOLUTION
}
if (!isFullscreen) prefKey += "_no_fullscreen"

return PreferenceHelper.getString(prefKey, "")
.replace("p", "")
.toIntOrNull()
}

/**
Expand Down Expand Up @@ -502,9 +506,9 @@
if (currentPosition in segmentStart until segmentEnd) {
if (sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC ||
(
sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE &&
!segment.skipped
)
sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE &&

Check failure on line 509 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (28) (should be 24) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:509:1: error: Unexpected indentation (28) (should be 24) (standard:indent)
!segment.skipped

Check failure on line 510 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (36) (should be 28) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:510:1: error: Unexpected indentation (36) (should be 28) (standard:indent)
)

Check failure on line 511 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (28) (should be 24) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:511:1: error: Unexpected indentation (28) (should be 24) (standard:indent)
) {
if (sponsorBlockNotifications) {
runCatching {
Expand All @@ -516,9 +520,9 @@
segment.skipped = true
} else if (sponsorBlockConfig[segment.category] == SbSkipOptions.MANUAL ||
(
sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE &&
segment.skipped
)
sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE &&

Check failure on line 523 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (28) (should be 24) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:523:1: error: Unexpected indentation (28) (should be 24) (standard:indent)
segment.skipped

Check failure on line 524 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (36) (should be 28) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:524:1: error: Unexpected indentation (36) (should be 28) (standard:indent)
)

Check failure on line 525 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (28) (should be 24) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:525:1: error: Unexpected indentation (28) (should be 24) (standard:indent)
) {
return segment
}
Expand All @@ -543,9 +547,9 @@
return chapters
.filter {
it.highlightDrawable == null ||
// remove the video highlight if it's already longer ago than [ChapterSegment.HIGHLIGHT_LENGTH],
// otherwise the SponsorBlock highlight would be shown from its starting point to the end
(currentPositionSeconds - it.start) < ChapterSegment.HIGHLIGHT_LENGTH
// remove the video highlight if it's already longer ago than [ChapterSegment.HIGHLIGHT_LENGTH],

Check failure on line 550 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (24) (should be 20) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:550:1: error: Unexpected indentation (24) (should be 20) (standard:indent)
// otherwise the SponsorBlock highlight would be shown from its starting point to the end

Check failure on line 551 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (24) (should be 20) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:551:1: error: Unexpected indentation (24) (should be 20) (standard:indent)
(currentPositionSeconds - it.start) < ChapterSegment.HIGHLIGHT_LENGTH

Check failure on line 552 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unexpected indentation (24) (should be 20) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:552:1: error: Unexpected indentation (24) (should be 20) (standard:indent)
}
.sortedBy { it.start }
.indexOfLast { currentPositionSeconds >= it.start }
Expand Down Expand Up @@ -701,9 +705,9 @@
*/
fun haveAudioTrackRoleFlagSet(@C.RoleFlags roleFlags: Int): Boolean {
return isFlagSet(roleFlags, C.ROLE_FLAG_DESCRIBES_VIDEO) ||
isFlagSet(roleFlags, C.ROLE_FLAG_DUB) ||
isFlagSet(roleFlags, C.ROLE_FLAG_MAIN) ||
isFlagSet(roleFlags, C.ROLE_FLAG_ALTERNATE)
isFlagSet(roleFlags, C.ROLE_FLAG_DUB) ||
isFlagSet(roleFlags, C.ROLE_FLAG_MAIN) ||
isFlagSet(roleFlags, C.ROLE_FLAG_ALTERNATE)
}

@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
Expand All @@ -716,7 +720,8 @@
val audioInfo = "${player.audioFormat?.codecs.orEmpty()} ${
TextUtils.formatBitrate(player.audioFormat?.bitrate)
}"
val videoQuality = "${player.videoFormat?.width}x${player.videoFormat?.height} ${player.videoFormat?.frameRate?.toInt()}fps"
val videoQuality =
"${player.videoFormat?.width}x${player.videoFormat?.height} ${player.videoFormat?.frameRate?.toInt()}fps"
return VideoStats(videoId, videoInfo, videoQuality, audioInfo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,32 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
*/
private lateinit var streams: Streams

/**
* for the transition
*/
// progress state of the motion layout transition
private var transitionStartId = 0
private var transitionEndId = 0
private var isTransitioning = true

/**
* for the player
*/
// data and objects stored for the player
private lateinit var exoPlayer: ExoPlayer
private lateinit var trackSelector: DefaultTrackSelector
private var currentSubtitle = Subtitle(code = PlayerHelper.defaultSubtitleCode)

// if null, it's been set to automatic
private var fullscreenResolution: Int? = null

// the resolution to use when the video is not played in fullscreen
// if null, use same quality as fullscreen
private var noFullscreenResolution: Int? = null

private val cronetDataSourceFactory = CronetDataSource.Factory(
CronetHelper.cronetEngine,
Executors.newCachedThreadPool()
)

/**
* for the player notification
*/
// for the player notification
private lateinit var nowPlayingNotification: NowPlayingNotification

/**
* SponsorBlock
*/
// SponsorBlock
private var segments = listOf<Segment>()
private var sponsorBlockEnabled = PlayerHelper.sponsorBlockEnabled
private var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories()
Expand Down Expand Up @@ -263,6 +262,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
1000,
1000
)

fullscreenResolution = PlayerHelper.getDefaultResolution(requireContext(), true)
noFullscreenResolution = PlayerHelper.getDefaultResolution(requireContext(), false)
}

override fun onCreateView(
Expand Down Expand Up @@ -541,6 +543,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {

updateFullscreenOrientation()
viewModel.isFullscreen.value = true

updateResolutionOnFullscreenChange(true)
}

@SuppressLint("SourceLockedOrientationActivity")
Expand Down Expand Up @@ -570,6 +574,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}

viewModel.isFullscreen.value = false

updateResolutionOnFullscreenChange(false)
}

private fun toggleDescription() {
Expand Down Expand Up @@ -1324,9 +1330,16 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
}

private fun updateResolutionOnFullscreenChange(isFullscreen: Boolean) {
if (!isFullscreen && noFullscreenResolution != null) {
setPlayerResolution(noFullscreenResolution!!)
} else {
setPlayerResolution(fullscreenResolution ?: Int.MAX_VALUE)
}
}

private suspend fun setStreamSource() {
val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "")
if (defaultResolution.isNotEmpty()) setPlayerResolution(defaultResolution.toInt())
updateResolutionOnFullscreenChange(viewModel.isFullscreen.value == true)

val (uri, mimeType) = when {
// LBRY HLS
Expand Down Expand Up @@ -1503,7 +1516,15 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (currentQuality == it.resolution) "${it.name} ✓" else it.name
}
) { which ->
setPlayerResolution(resolutions[which].resolution)
val newResolution = resolutions[which].resolution
setPlayerResolution(newResolution)

// save the selected resolution to update on fullscreen change
if (noFullscreenResolution != null && viewModel.isFullscreen.value != true) {
noFullscreenResolution = newResolution
} else {
fullscreenResolution = newResolution
}
}
.show(childFragmentManager)
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@
<item>240p</item>
<item>144p</item>
</string-array>
<string-array name="defresNoPortrait">
<item>@string/same_as_fullscreen</item>
<item>2160p</item>
<item>1440p</item>
<item>1080p</item>
<item>720p</item>
<item>480p</item>
<item>360p</item>
<item>240p</item>
<item>144p</item>
</string-array>
<string-array name="defresValue">
<item />
<item>2160p</item>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@
<string name="uploader_name">Uploader name</string>
<string name="duration_span">Duration: %1$s</string>
<string name="remove_watched_videos">Remove watched videos</string>
<string name="no_fullscreen_resolution">No-Fullscreen resolution</string>
<string name="same_as_fullscreen">Same as fullscreen</string>

<!-- Backup & Restore Settings -->
<string name="import_subscriptions_from">Import subscriptions from</string>
<string name="export_subscriptions_to">Export subscriptions to</string>
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/xml/audio_video_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
app:title="@string/defres"
app:useSimpleSummaryProvider="true" />

<ListPreference
android:icon="@drawable/ic_fullscreen"
app:defaultValue=""
app:entries="@array/defresNoPortrait"
app:entryValues="@array/defresValue"
app:key="default_res_no_fullscreen"
app:title="@string/no_fullscreen_resolution"
app:useSimpleSummaryProvider="true" />

<ListPreference
android:icon="@drawable/ic_headphones"
app:defaultValue="auto"
Expand All @@ -35,6 +44,15 @@
app:title="@string/defres"
app:useSimpleSummaryProvider="true" />

<ListPreference
android:icon="@drawable/ic_fullscreen"
app:defaultValue=""
app:entries="@array/defresNoPortrait"
app:entryValues="@array/defresValue"
app:key="default_res_mobile_no_fullscreen"
app:title="@string/no_fullscreen_resolution"
app:useSimpleSummaryProvider="true" />

<ListPreference
android:icon="@drawable/ic_headphones"
app:defaultValue="auto"
Expand Down
Loading