Skip to content

Commit

Permalink
refactor: reimplement stats for nerds
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 17, 2024
1 parent f0af2c9 commit ddefae9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
44 changes: 32 additions & 12 deletions app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -815,18 +815,38 @@ object PlayerHelper {
}

@OptIn(androidx.media3.common.util.UnstableApi::class)
fun getVideoStats(player: ExoPlayer, videoId: String): VideoStats {
val videoInfo = "${player.videoFormat?.codecs.orEmpty()} ${
TextUtils.formatBitrate(
player.videoFormat?.bitrate
)
}"
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"
return VideoStats(videoId, videoInfo, videoQuality, audioInfo)
fun getVideoStats(tracks: Tracks, videoId: String): VideoStats {
val videoStats = VideoStats(videoId, "", "", "")

for (group in tracks.groups) {
if (!group.isSelected || group.length == 0) continue

when (group.type) {
C.TRACK_TYPE_AUDIO -> {
val audioFormat = (0..group.length).firstOrNull { index ->
group.isTrackSelected(index)
}?.let { index -> group.getTrackFormat(index) } ?: continue

videoStats.audioInfo = "${audioFormat.codecs.orEmpty()} ${
TextUtils.formatBitrate(audioFormat.bitrate)
}"
}

C.TRACK_TYPE_VIDEO -> {
val videoFormat = (0..group.length).firstOrNull { index ->
group.isTrackSelected(index)
}?.let { index -> group.getTrackFormat(index) } ?: continue

videoStats.videoInfo = "${videoFormat.codecs.orEmpty()} ${
TextUtils.formatBitrate(videoFormat.bitrate)
}"
videoStats.videoQuality =
"${videoFormat.width}x${videoFormat.height} ${videoFormat.frameRate.toInt()}fps"
}
}
}

return videoStats
}

fun getPlayPauseActionIcon(player: Player) = when {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/github/libretube/obj/VideoStats.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class VideoStats(
val videoId: String,
val videoInfo: String,
val videoQuality: String,
val audioInfo: String
var videoInfo: String,
var videoQuality: String,
var audioInfo: String
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import com.github.libretube.ui.models.CommonPlayerViewModel
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.ui.sheets.CommentsSheet
import com.github.libretube.ui.sheets.StatsSheet
import com.github.libretube.util.OnlineTimeFrameReceiver
import com.github.libretube.util.PauseableTimer
import com.github.libretube.util.PlayingQueue
Expand Down Expand Up @@ -153,7 +154,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// if null, use same quality as fullscreen
private var noFullscreenResolution: Int? = null

private var selectedAudioLanguageAndRoleFlags: Pair<String?, @C. RoleFlags Int>? = null
private var selectedAudioLanguageAndRoleFlags: Pair<String?, @C.RoleFlags Int>? = null

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

Expand Down Expand Up @@ -1421,11 +1422,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {

override fun onStatsClicked() {
if (!this::streams.isInitialized) return
// TODO: reimplement video stats
// val videoStats = getVideoStats(playerController, videoId)
// StatsSheet()
// .apply { arguments = bundleOf(IntentData.videoStats to videoStats) }
// .show(childFragmentManager)

val videoStats = PlayerHelper.getVideoStats(playerController.currentTracks, videoId)
StatsSheet()
.apply { arguments = bundleOf(IntentData.videoStats to videoStats) }
.show(childFragmentManager)
}

override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) {
Expand Down

0 comments on commit ddefae9

Please sign in to comment.