Skip to content

Commit

Permalink
remove video quality selection from settings
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Nov 19, 2024
1 parent 0a16beb commit fd90c9f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/dev/brahmkshatriya/echo/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PlayerService : MediaLibraryService() {
.build()

val factory = StreamableMediaSource.Factory(
this, scope, currentSources, extListFlow, cache, settings
this, scope, settings, currentSources, extListFlow, cache
)

ExoPlayer.Builder(this, factory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ object MediaItemUtils {
val MediaMetadata.clientId get() = requireNotNull(extras?.getString("clientId"))
val MediaMetadata.context get() = extras?.getSerialized<EchoMediaItem?>("context")
val MediaMetadata.sourcesIndex get() = extras?.getInt("sourcesIndex") ?: -1
val MediaMetadata.sourceIndex get() = extras?.getInt("sourceIndex") ?: 0
val MediaMetadata.sourceIndex get() = extras?.getInt("sourceIndex") ?: -1
val MediaMetadata.backgroundIndex get() = extras?.getInt("backgroundIndex") ?: -1
val MediaMetadata.subtitleIndex get() = extras?.getInt("subtitleIndex") ?: -1
val MediaMetadata.isLiked get() = (userRating as? ThumbRating)?.isThumbsUp == true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import dev.brahmkshatriya.echo.playback.MediaItemUtils.sourceIndex
import dev.brahmkshatriya.echo.playback.MediaItemUtils.sourcesIndex
import dev.brahmkshatriya.echo.playback.MediaItemUtils.subtitleIndex
import dev.brahmkshatriya.echo.playback.MediaItemUtils.track
import dev.brahmkshatriya.echo.ui.settings.AudioFragment.AudioPreference.Companion.select
import dev.brahmkshatriya.echo.utils.saveToCache
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -43,6 +44,7 @@ import java.io.IOException
class StreamableMediaSource(
private val context: Context,
private val scope: CoroutineScope,
private val settings: SharedPreferences,
private val current: MutableStateFlow<Map<String, Streamable.Media.Sources>>,
private val loader: StreamableLoader,
private val dash: Lazy<MediaSource.Factory>,
Expand Down Expand Up @@ -102,7 +104,7 @@ class StreamableMediaSource(
}.toTypedArray()
) else {
val index = mediaItem.sourceIndex
val source = sources[index]
val source = sources.getOrNull(index) ?: sources.select(settings)
create(new, index, source)
}
}
Expand Down Expand Up @@ -145,10 +147,10 @@ class StreamableMediaSource(
class Factory(
val context: Context,
val scope: CoroutineScope,
val settings: SharedPreferences,
val current: MutableStateFlow<Map<String, Streamable.Media.Sources>>,
extListFlow: MutableStateFlow<List<MusicExtension>?>,
cache: SimpleCache,
settings: SharedPreferences,
) : MediaSource.Factory {

private val dataSource = ResolvingDataSource.Factory(
Expand Down Expand Up @@ -195,7 +197,7 @@ class StreamableMediaSource(
private val loader = StreamableLoader(context, settings, extListFlow)

override fun createMediaSource(mediaItem: MediaItem) = StreamableMediaSource(
context, scope, current, loader, dash, hls, default, mediaItem
context, scope, settings, current, loader, dash, hls, default, mediaItem
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class ShelfClickListener(
private val afterOpening: (() -> Unit)? = null,
) : ShelfAdapter.Listener {

val fragment by lazy { fragmentManager.findFragmentById(fragmentId)!! }
val fragment get() = fragmentManager.findFragmentById(fragmentId)!!

private fun snack(block: Context.() -> SnackBar.Message) =
fragment.createSnack(fragment.requireContext().block())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class TrackDetailsFragment : Fragment() {

@OptIn(UnstableApi::class)
private fun Format.getBitrate() =
(bitrate / 1024).takeIf { it > 0 }?.let { "$it kbps" } ?: ""
(bitrate / 1000).takeIf { it > 0 }?.let { "$it kbps" } ?: ""

private fun Format.getFrameRate() =
frameRate.toInt().takeIf { it > 0 }?.let { "$it fps" } ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AudioFragment : BaseSettingsFragment() {
}

MaterialListPreference(context).apply {
key = AUDIO_STREAM_QUALITY
key = STREAM_QUALITY
title = getString(R.string.stream_quality)
summary = getString(R.string.stream_quality_summary)
entries = context.resources.getStringArray(R.array.stream_qualities)
Expand All @@ -92,22 +92,6 @@ class AudioFragment : BaseSettingsFragment() {
addPreference(this)
}

MaterialListPreference(context).apply {
key = VIDEO_STREAM_QUALITY
title = getString(R.string.video_quality)
summary = getString(R.string.video_quality_summary)
entries = context.resources.getStringArray(R.array.stream_qualities)
.toMutableList().let {
it.add(getString(R.string.video_quality_none))
it.toTypedArray()
}
entryValues = videoQualities
layoutResource = R.layout.preference
isIconSpaceReserved = false
setDefaultValue(streamQualities[1])
addPreference(this)
}

MaterialSliderPreference(context, 200, 1000, allowOverride = true).apply {
key = CACHE_SIZE
title = getString(R.string.cache_size)
Expand Down Expand Up @@ -148,22 +132,29 @@ class AudioFragment : BaseSettingsFragment() {
const val AUTO_START_RADIO = "auto_start_radio"
const val EQUALIZER = "equalizer"

const val AUDIO_STREAM_QUALITY = "stream_quality"
const val VIDEO_STREAM_QUALITY = "video_stream_quality"
const val STREAM_QUALITY = "stream_quality"
const val CACHE_SIZE = "cache_size"
val streamQualities = arrayOf("highest", "medium", "lowest")
val videoQualities = arrayOf("highest", "medium", "lowest", "none")

fun selectSourceIndex(settings: SharedPreferences?, streamables: List<Streamable>) =
streamables.indexOf(selectAudioStream(settings, streamables))
fun selectSourceIndex(
settings: SharedPreferences?, streamables: List<Streamable>
) = if (streamables.isNotEmpty()) streamables.indexOf(streamables.select(settings))
else -1


fun selectAudioStream(settings: SharedPreferences?, streamables: List<Streamable>) =
when (settings?.getString(AUDIO_STREAM_QUALITY, "medium")) {
"highest" -> streamables.maxByOrNull { it.quality }
"medium" -> streamables.sortedBy { it.quality }.getOrNull(streamables.size / 2)
"lowest" -> streamables.minByOrNull { it.quality }
else -> streamables.firstOrNull()
fun <E> List<E>.select(settings: SharedPreferences?, quality: (E) -> Int) =
when (settings?.getString(STREAM_QUALITY, "medium")) {
"highest" -> maxBy { quality(it) }
"medium" -> sortedBy { quality(it) }[size / 2]
"lowest" -> minBy { quality(it) }
else -> first()
}

fun List<Streamable>.select(settings: SharedPreferences?) =
select(settings) { it.quality }

fun List<Streamable.Source>.select(settings: SharedPreferences?) =
select(settings) { it.quality }
}
}
}

0 comments on commit fd90c9f

Please sign in to comment.