Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit f8794663 authored by Sebastiano Barezzi's avatar Sebastiano Barezzi
Browse files

Twelve: PlaybackService: Optimize resumption item mapping to media item

Change-Id: I61f0b01d791d6e1b40a5ba7eba14af869625f911
parent 3082635c
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2025 The LineageOS Project
 * SPDX-License-Identifier: Apache-2.0
 */

package org.lineageos.twelve.ext

import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope

/**
 * Maps a list of elements asynchronously.
 */
suspend fun <T, R> Iterable<T>.mapAsync(
    transform: suspend (T) -> R,
): List<R> = coroutineScope {
    map {
        async {
            transform(it)
        }
    }.awaitAll()
}
+11 −7
Original line number Diff line number Diff line
@@ -37,14 +37,17 @@ import androidx.media3.session.SessionCommand
import androidx.media3.session.SessionError
import androidx.media3.session.SessionResult
import androidx.preference.PreferenceManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.guava.await
import kotlinx.coroutines.guava.future
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.lineageos.twelve.MainActivity
import org.lineageos.twelve.R
import org.lineageos.twelve.TwelveApplication
import org.lineageos.twelve.ext.enableFloatOutput
import org.lineageos.twelve.ext.enableOffload
import org.lineageos.twelve.ext.mapAsync
import org.lineageos.twelve.ext.next
import org.lineageos.twelve.ext.setOffloadEnabled
import org.lineageos.twelve.ext.skipSilence
@@ -541,8 +544,10 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
        var startIndex = resumptionPlaylist.startIndex
        var startPositionMs = resumptionPlaylist.startPositionMs

        val mediaItems = resumptionPlaylist.mediaItemIds.mapIndexed { index, itemId ->
            when (val mediaItem = mediaRepositoryTree.getItem(itemId)) {
        val mediaItems = resumptionPlaylist.mediaItemIds.mapAsync { itemId ->
            mediaRepositoryTree.getItem(itemId)
        }.withIndex().mapNotNull { (index, mediaItem) ->
            when (mediaItem) {
                null -> {
                    if (index == resumptionPlaylist.startIndex) {
                        // The playback position is now invalid
@@ -561,7 +566,7 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {

                else -> mediaItem
            }
        }.filterNotNull()
        }

        return if (mediaItems.isEmpty()) {
            // No valid media items found, clear the resumption playlist
@@ -569,9 +574,6 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {

            MediaSession.MediaItemsWithStartPosition(emptyList(), 0, 0)
        } else {
            // Shouldn't be needed, but just to be sure
            startIndex = startIndex.coerceIn(mediaItems.indices)

            MediaSession.MediaItemsWithStartPosition(
                mediaItems,
                startIndex,
@@ -589,7 +591,9 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
            return
        }

        val resumptionPlaylist = getResumptionPlaylist()
        val resumptionPlaylist = withContext(Dispatchers.IO) {
            getResumptionPlaylist()
        }
        if (resumptionPlaylist.mediaItems.isEmpty()) {
            Log.e(LOG_TAG, "No resumption playlist items found")
            return