Skip to content

Commit 9be0925

Browse files
committed
feat: already set auto-fullscreen on shorts before rendering starts
1 parent 310b49f commit 9be0925

5 files changed

Lines changed: 18 additions & 36 deletions

File tree

app/src/main/java/com/github/libretube/api/NewPipeMediaServiceRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.github.libretube.api.obj.StreamItem.Companion.TYPE_STREAM
2222
import com.github.libretube.api.obj.Streams
2323
import com.github.libretube.api.obj.Subtitle
2424
import com.github.libretube.api.poToken.PoTokenGenerator
25-
import com.github.libretube.extensions.parallelMap
2625
import com.github.libretube.extensions.sha256Sum
2726
import com.github.libretube.extensions.toID
2827
import com.github.libretube.helpers.NewPipeExtractorInstance
@@ -48,9 +47,9 @@ import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
4847
import org.schabi.newpipe.extractor.search.SearchInfo
4948
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor
5049
import org.schabi.newpipe.extractor.stream.AudioStream
50+
import org.schabi.newpipe.extractor.stream.ContentAvailability
5151
import org.schabi.newpipe.extractor.stream.StreamInfo
5252
import org.schabi.newpipe.extractor.stream.StreamInfoItem
53-
import org.schabi.newpipe.extractor.stream.ContentAvailability
5453
import org.schabi.newpipe.extractor.stream.VideoStream
5554
import kotlin.time.toKotlinInstant
5655

@@ -348,7 +347,8 @@ class NewPipeMediaServiceRepository : MediaServiceRepository {
348347
it.languageTag,
349348
it.isAutoGenerated
350349
)
351-
}
350+
},
351+
isShort = resp.isShortFormContent
352352
)
353353
}
354354

app/src/main/java/com/github/libretube/api/PipedMediaServiceRepository.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.github.libretube.api.obj.StreamItem
1313
import com.github.libretube.api.obj.Streams
1414
import com.github.libretube.constants.PreferenceKeys
1515
import com.github.libretube.helpers.PreferenceHelper
16-
import kotlinx.serialization.encodeToString
1716
import retrofit2.HttpException
1817

1918
open class PipedMediaServiceRepository : MediaServiceRepository {
@@ -24,7 +23,11 @@ open class PipedMediaServiceRepository : MediaServiceRepository {
2423

2524
override suspend fun getStreams(videoId: String): Streams {
2625
return try {
27-
api.getStreams(videoId)
26+
api.getStreams(videoId).also {
27+
it.isShort = it.videoStreams.firstOrNull()?.let { stream ->
28+
(stream.height ?: 0) > (stream.width ?: 0)
29+
} ?: false
30+
}
2831
} catch (e: HttpException) {
2932
val errorMessage = e.response()?.errorBody()?.string()?.runCatching {
3033
JsonHelper.json.decodeFromString<Message>(this).message

app/src/main/java/com/github/libretube/api/obj/Streams.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ data class Streams(
4848
val proxyUrl: String? = null,
4949
val chapters: List<ChapterSegment> = emptyList(),
5050
val uploaderSubscriberCount: Long = 0,
51-
val previewFrames: List<PreviewFrames> = emptyList()
51+
val previewFrames: List<PreviewFrames> = emptyList(),
52+
var isShort: Boolean = false
5253
): Parcelable {
5354
@IgnoredOnParcel
5455
val isLive = livestream || duration <= 0

app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ object PlayerHelper {
125125
}
126126
}
127127

128-
fun getOrientation(videoWidth: Int, videoHeight: Int): Int {
128+
fun getFullscreenOrientation(isVerticalVideo: Boolean): Int {
129129
val fullscreenOrientationPref = PreferenceHelper.getString(
130130
PreferenceKeys.FULLSCREEN_ORIENTATION,
131131
"ratio"
@@ -134,7 +134,7 @@ object PlayerHelper {
134134
return when (fullscreenOrientationPref) {
135135
"ratio" -> {
136136
// probably a youtube shorts video
137-
if (videoHeight > videoWidth) {
137+
if (isVerticalVideo) {
138138
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
139139
} // a video with normal aspect ratio
140140
else {

app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ import com.github.libretube.extensions.updateIfChanged
7373
import com.github.libretube.helpers.BackgroundHelper
7474
import com.github.libretube.helpers.DownloadHelper
7575
import com.github.libretube.helpers.ImageHelper
76-
import com.github.libretube.helpers.NavBarHelper
7776
import com.github.libretube.helpers.NavigationHelper
7877
import com.github.libretube.helpers.PlayerHelper
7978
import com.github.libretube.helpers.PlayerHelper.getCurrentSegment
@@ -115,7 +114,6 @@ import kotlinx.coroutines.launch
115114
import kotlinx.coroutines.runBlocking
116115
import kotlinx.coroutines.withContext
117116
import kotlin.io.path.exists
118-
import kotlin.math.abs
119117
import kotlin.math.absoluteValue
120118

121119

@@ -142,15 +140,6 @@ class PlayerFragment : Fragment(R.layout.fragment_player), CustomPlayerCallback
142140

143141
// data and objects stored for the player
144142
private lateinit var streams: Streams
145-
private val isShort: Boolean
146-
get() {
147-
if (PlayingQueue.getCurrent()?.isShort == true) return true
148-
if (!::playerController.isInitialized) return false
149-
150-
val currentVideoFormat = PlayerHelper.getCurrentVideoFormat(playerController)
151-
?: return false
152-
return currentVideoFormat.height > currentVideoFormat.width
153-
}
154143

155144
private val handler = Handler(Looper.getMainLooper())
156145

@@ -259,7 +248,6 @@ class PlayerFragment : Fragment(R.layout.fragment_player), CustomPlayerCallback
259248
}
260249
}
261250

262-
var fullscreenOnShortsAlreadyDone = false
263251
override fun onPlaybackStateChanged(playbackState: Int) {
264252
// set the playback speed to one if having reached the end of a livestream
265253
if (playbackState == Player.STATE_BUFFERING && binding.player.isLive &&
@@ -302,15 +290,6 @@ class PlayerFragment : Fragment(R.layout.fragment_player), CustomPlayerCallback
302290
bufferingTimeoutTask?.let { handler.removeCallbacks(it) }
303291
}
304292

305-
if (playbackState == Player.STATE_READY && binding.playerMotionLayout.progress == 0f) {
306-
if (PlayerHelper.autoFullscreenShortsEnabled && isShort && !fullscreenOnShortsAlreadyDone) {
307-
setFullscreen()
308-
fullscreenOnShortsAlreadyDone = true
309-
}
310-
}
311-
312-
if (playbackState == Player.STATE_ENDED) fullscreenOnShortsAlreadyDone = false
313-
314293
super.onPlaybackStateChanged(playbackState)
315294
}
316295

@@ -835,12 +814,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), CustomPlayerCallback
835814
private fun updateFullscreenOrientation() {
836815
if (PlayerHelper.autoFullscreenEnabled || !this::streams.isInitialized) return
837816

838-
val height = streams.videoStreams.firstOrNull()?.height
839-
?: playerController.videoSize.height
840-
val width =
841-
streams.videoStreams.firstOrNull()?.width ?: playerController.videoSize.width
842-
843-
baseActivity.requestedOrientation = PlayerHelper.getOrientation(width, height)
817+
baseActivity.requestedOrientation = PlayerHelper.getFullscreenOrientation(streams.isShort)
844818
}
845819

846820
private fun setFullscreen() {
@@ -1221,6 +1195,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), CustomPlayerCallback
12211195
seekBarPreviewListener = listener
12221196
playerControlsBinding.exoProgress.addSeekBarListener(listener)
12231197
}
1198+
1199+
if (binding.playerMotionLayout.progress == 0f && PlayerHelper.autoFullscreenShortsEnabled && streams.isShort) {
1200+
setFullscreen()
1201+
}
12241202
}
12251203

12261204
private suspend fun showRelatedStreams() {
@@ -1472,6 +1450,6 @@ class PlayerFragment : Fragment(R.layout.fragment_player), CustomPlayerCallback
14721450
}
14731451

14741452
override fun isVideoShort(): Boolean {
1475-
return isShort
1453+
return streams.isShort
14761454
}
14771455
}

0 commit comments

Comments
 (0)