Skip to content

Commit

Permalink
Remove quality selector and add video selector
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Apr 22, 2024
1 parent ef20330 commit af047c2
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 847 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.media3.common.C
import androidx.media3.common.Player
import ch.srgssr.pillarbox.demo.shared.R
import ch.srgssr.pillarbox.player.extension.displayName
Expand All @@ -25,18 +26,17 @@ import ch.srgssr.pillarbox.player.getCurrentTracksAsFlow
import ch.srgssr.pillarbox.player.getPlaybackSpeedAsFlow
import ch.srgssr.pillarbox.player.getTrackSelectionParametersAsFlow
import ch.srgssr.pillarbox.player.tracks.Track
import ch.srgssr.pillarbox.player.tracks.VideoQuality
import ch.srgssr.pillarbox.player.tracks.VideoTrack
import ch.srgssr.pillarbox.player.tracks.audioTracks
import ch.srgssr.pillarbox.player.tracks.disableAudioTrack
import ch.srgssr.pillarbox.player.tracks.disableTextTrack
import ch.srgssr.pillarbox.player.tracks.disableVideoTrack
import ch.srgssr.pillarbox.player.tracks.selectMaxVideoQuality
import ch.srgssr.pillarbox.player.tracks.selectTrack
import ch.srgssr.pillarbox.player.tracks.setAutoAudioTrack
import ch.srgssr.pillarbox.player.tracks.setAutoTextTrack
import ch.srgssr.pillarbox.player.tracks.setAutoVideoTrack
import ch.srgssr.pillarbox.player.tracks.textTracks
import ch.srgssr.pillarbox.player.tracks.videoQualities
import ch.srgssr.pillarbox.player.tracks.videoTracks
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -67,9 +67,9 @@ class PlayerSettingsViewModel(
tracks,
trackSelectionParameters
) { tracks, trackSelectionParameters ->
SettingItemOptions(
TracksSettingItem(
title = application.getString(R.string.subtitles),
items = tracks.textTracks,
tracks = tracks.textTracks,
disabled = trackSelectionParameters.isTextTrackDisabled
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)
Expand All @@ -81,23 +81,27 @@ class PlayerSettingsViewModel(
tracks,
trackSelectionParameters
) { tracks, trackSelectionParameters ->
SettingItemOptions(
TracksSettingItem(
title = application.getString(R.string.audio_track),
items = tracks.audioTracks,
tracks = tracks.audioTracks,
disabled = trackSelectionParameters.isAudioTrackDisabled
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)

/**
* All the available video qualities for the current [player].
* All the available video tracks for the current [player].
*/
val videoQualities = combine(
val videoTracks = combine(
tracks,
trackSelectionParameters,
) { _, trackSelectionParameters ->
SettingItemOptions(
title = application.getString(R.string.quality),
items = player.videoQualities,
) { tracks, trackSelectionParameters ->
TracksSettingItem(
title = application.getString(R.string.video_tracks),
tracks = tracks.videoTracks
.sortedWith(
compareByDescending<VideoTrack> { it.format.height }
.thenByDescending { it.format.bitrate }
),
disabled = trackSelectionParameters.isVideoTrackDisabled,
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)
Expand All @@ -121,10 +125,10 @@ class PlayerSettingsViewModel(
val settings = combine(
subtitles,
audioTracks,
videoQualities,
videoTracks,
trackSelectionParameters,
playbackSpeed,
) { subtitles, audioTracks, videoQualities, trackSelectionParameters, playbackSpeed ->
) { subtitles, audioTracks, videoTracks, trackSelectionParameters, playbackSpeed ->
buildList {
add(
SettingItem(
Expand All @@ -135,12 +139,12 @@ class PlayerSettingsViewModel(
)
)

if (subtitles != null && subtitles.items.isNotEmpty()) {
if (subtitles != null && subtitles.tracks.isNotEmpty()) {
add(
SettingItem(
title = application.getString(R.string.subtitles),
subtitle = getTracksSubtitle(
tracks = subtitles.items,
tracks = subtitles.tracks,
disabled = trackSelectionParameters.isTextTrackDisabled,
),
icon = Icons.Default.ClosedCaption,
Expand All @@ -149,12 +153,12 @@ class PlayerSettingsViewModel(
)
}

if (audioTracks != null && audioTracks.items.isNotEmpty()) {
if (audioTracks != null && audioTracks.tracks.isNotEmpty()) {
add(
SettingItem(
title = application.getString(R.string.audio_track),
subtitle = getTracksSubtitle(
tracks = audioTracks.items,
tracks = audioTracks.tracks,
disabled = trackSelectionParameters.isAudioTrackDisabled,
),
icon = Icons.Default.RecordVoiceOver,
Expand All @@ -163,12 +167,12 @@ class PlayerSettingsViewModel(
)
}

if (videoQualities != null && videoQualities.items.isNotEmpty()) {
if (videoTracks != null && videoTracks.tracks.isNotEmpty()) {
add(
SettingItem(
title = application.getString(R.string.quality),
subtitle = getVideoQualitiesSubtitle(
videoQualities = videoQualities.items,
title = application.getString(R.string.video_tracks),
subtitle = getTracksSubtitle(
tracks = videoTracks.tracks,
disabled = trackSelectionParameters.isVideoTrackDisabled,
),
icon = Icons.Default.Tune,
Expand All @@ -188,15 +192,6 @@ class PlayerSettingsViewModel(
player.selectTrack(track)
}

/**
* Select the max video quality.
*
* @param videoQuality The max video quality.
*/
fun selectMaxVideoQuality(videoQuality: VideoQuality) {
player.selectMaxVideoQuality(videoQuality)
}

/**
* Reset the subtitles.
*/
Expand Down Expand Up @@ -257,19 +252,7 @@ class PlayerSettingsViewModel(
} else {
tracks.filter { it.isSelected }
.map { it.format.displayName }
.firstOrNull()
}
}

private fun getVideoQualitiesSubtitle(
videoQualities: List<VideoQuality>,
disabled: Boolean,
): String? {
return if (disabled) {
application.getString(R.string.disabled)
} else {
videoQualities.filter { it.isSelected }
.map { "${it.height}p" }
.filter { it != C.LANGUAGE_UNDETERMINED }
.firstOrNull()
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.shared.ui.player.settings

import ch.srgssr.pillarbox.player.tracks.Track

/**
* The setting for a specific kind a track (audio/text/video).
*/
data class TracksSettingItem(
/**
* The title of the setting.
*/
val title: String,

/**
* The list of possible tracks.
*/
val tracks: List<Track>,

/**
* `true` if this kind of tracks is disabled, `false` otherwise.
*/
val disabled: Boolean
)
2 changes: 1 addition & 1 deletion pillarbox-demo-shared/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<string name="duration"><xliff:g example="15" id="duration">%1$d</xliff:g> min</string>
<string name="settings">Settings</string>
<string name="audio_track">Audio track</string>
<string name="quality">Quality</string>
<string name="video_tracks">Video tacks</string>
<string name="subtitles">Subtitles</string>
<string name="speed">Speed</string>
<string name="speed_normal">Normal</string>
Expand Down
Loading

0 comments on commit af047c2

Please sign in to comment.