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

refactor: also respect codec preference in audio mode #6579

Merged
merged 1 commit into from
Oct 2, 2024
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
26 changes: 26 additions & 0 deletions app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -869,4 +869,30 @@ object PlayerHelper {
else -> false
}
}

@OptIn(UnstableApi::class)
fun setPreferredCodecs(trackSelector: DefaultTrackSelector) {
trackSelector.updateParameters {
val enabledVideoCodecs = PlayerHelper.enabledVideoCodecs
if (enabledVideoCodecs != "all") {
// map the codecs to their corresponding mimetypes
val mimeType = when (enabledVideoCodecs) {
"vp9" -> arrayOf("video/webm", "video/x-vnd.on2.vp9")
"avc" -> arrayOf("video/mp4", "video/avc")
else -> throw IllegalArgumentException()
}
this.setPreferredVideoMimeTypes(*mimeType)
}
val enabledAudioCodecs = PlayerHelper.enabledAudioCodecs
if (enabledAudioCodecs != "all") {
// map the codecs to their corresponding mimetypes
val mimeType = when (enabledAudioCodecs) {
"opus" -> arrayOf("audio/opus")
"mp4" -> arrayOf("audio/mp4a-latm")
else -> throw IllegalArgumentException()
}
this.setPreferredAudioMimeTypes(*mimeType)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ class OnlinePlayerService : LifecycleService() {
// prevent android from putting LibreTube to sleep when locked
player!!.setWakeMode(WAKE_MODE_NETWORK)

/**
* Listens for changed playbackStates (e.g. pause, end)
* Plays the next video when the current one ended
*/
// Listens for changed playbackStates (e.g. pause, end)
// Plays the next video when the current one ended
player?.addListener(playerListener)

PlayerHelper.setPreferredCodecs(trackSelector!!)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1348,28 +1348,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
viewModel.player.addListener(playerListener)

// control for the track sources like subtitles and audio source
viewModel.trackSelector.updateParameters {
val enabledVideoCodecs = PlayerHelper.enabledVideoCodecs
if (enabledVideoCodecs != "all") {
// map the codecs to their corresponding mimetypes
val mimeType = when (enabledVideoCodecs) {
"vp9" -> arrayOf("video/webm", "video/x-vnd.on2.vp9")
"avc" -> arrayOf("video/mp4", "video/avc")
else -> throw IllegalArgumentException()
}
this.setPreferredVideoMimeTypes(*mimeType)
}
val enabledAudioCodecs = PlayerHelper.enabledAudioCodecs
if (enabledAudioCodecs != "all") {
// map the codecs to their corresponding mimetypes
val mimeType = when (enabledAudioCodecs) {
"opus" -> arrayOf("audio/opus")
"mp4" -> arrayOf("audio/mp4a-latm")
else -> throw IllegalArgumentException()
}
this.setPreferredAudioMimeTypes(*mimeType)
}
}
PlayerHelper.setPreferredCodecs(viewModel.trackSelector)
}

/**
Expand Down
Loading