playback: stop gap that occurs between load/playback

This commit is contained in:
Alexander Capehart 2024-07-27 19:14:38 -06:00
parent 8bc7418887
commit 8094ff05d5
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
9 changed files with 88 additions and 194 deletions

View file

@ -136,32 +136,11 @@
</receiver> </receiver>
<!-- Tasker integration -->
<activity <activity
android:name=".tasker.StartConfigBasicAction" android:name=".tasker.StartConfigBasicAction"
android:exported="true" android:exported="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="Start Auxio"> android:label="Start">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
<activity
android:name=".tasker.ShuffleAllConfigBasicAction"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="Shuffle All Songs">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
<activity
android:name=".tasker.RestoreStateConfigBasicAction"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="Restore State">
<intent-filter> <intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" /> <action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter> </intent-filter>

View file

@ -57,6 +57,7 @@ object IntegerTable {
const val PLAYBACK_NOTIFICATION_CODE = 0xA0A0 const val PLAYBACK_NOTIFICATION_CODE = 0xA0A0
/** "Music loading" notification code */ /** "Music loading" notification code */
const val INDEXER_NOTIFICATION_CODE = 0xA0A1 const val INDEXER_NOTIFICATION_CODE = 0xA0A1
const val TASKER_ERROR_NOT_RESTORED = 0xA0A2
/** MainActivity Intent request code */ /** MainActivity Intent request code */
const val REQUEST_CODE = 0xA0C0 const val REQUEST_CODE = 0xA0C0
/** RepeatMode.NONE */ /** RepeatMode.NONE */

View file

@ -96,7 +96,9 @@ constructor(
// Not observing and done loading, exit foreground. // Not observing and done loading, exit foreground.
logD("Exiting foreground") logD("Exiting foreground")
post(observingNotification) post(observingNotification)
} else { } else if (!playbackManager.awaitingDeferredPlayback) {
// Very possible we are done loading music and now need to avoid downtime
// as the player begins to load the playback state.
post(null) post(null)
} }
} }

View file

@ -42,6 +42,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield import kotlinx.coroutines.yield
import org.oxycblt.auxio.ForegroundListener
import org.oxycblt.auxio.image.ImageSettings import org.oxycblt.auxio.image.ImageSettings
import org.oxycblt.auxio.music.MusicParent import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.MusicRepository import org.oxycblt.auxio.music.MusicRepository
@ -84,11 +85,13 @@ class ExoPlaybackStateHolder(
private val restoreScope = CoroutineScope(Dispatchers.IO + saveJob) private val restoreScope = CoroutineScope(Dispatchers.IO + saveJob)
private var currentSaveJob: Job? = null private var currentSaveJob: Job? = null
private var openAudioEffectSession = false private var openAudioEffectSession = false
private var foregroundListener: ForegroundListener? = null
override var sessionOngoing = false override var sessionOngoing = false
private set private set
fun attach() { fun attach(foregroundListener: ForegroundListener) {
this.foregroundListener = foregroundListener
imageSettings.registerListener(this) imageSettings.registerListener(this)
player.addListener(this) player.addListener(this)
replayGainProcessor.attach() replayGainProcessor.attach()
@ -105,6 +108,7 @@ class ExoPlaybackStateHolder(
replayGainProcessor.release() replayGainProcessor.release()
imageSettings.unregisterListener(this) imageSettings.unregisterListener(this)
player.release() player.release()
foregroundListener = null
} }
override var parent: MusicParent? = null override var parent: MusicParent? = null
@ -168,13 +172,15 @@ class ExoPlaybackStateHolder(
// Apply the saved state on the main thread to prevent code expecting // Apply the saved state on the main thread to prevent code expecting
// state updates on the main thread from crashing. // state updates on the main thread from crashing.
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
playbackManager.applySavedState(state, false)
if (action.sessionRequired) { if (action.sessionRequired) {
sessionOngoing = true sessionOngoing = true
foregroundListener?.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
playbackManager.applySavedState(state, false)
} }
} else if (action.sessionRequired) { } else {
error("No playback state to restore, but need to start session") logD("No saved state to restore")
foregroundListener?.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
} }
} }
@ -189,11 +195,15 @@ class ExoPlaybackStateHolder(
// Open -> Try to find the Song for the given file and then play it from all songs // Open -> Try to find the Song for the given file and then play it from all songs
is DeferredPlayback.Open -> { is DeferredPlayback.Open -> {
logD("Opening specified file") logD("Opening specified file")
deviceLibrary.findSongForUri(context, action.uri)?.let { song -> val song = deviceLibrary.findSongForUri(context, action.uri)
if (song != null) {
playbackManager.play( playbackManager.play(
requireNotNull(commandFactory.song(song, ShuffleMode.IMPLICIT)) { requireNotNull(commandFactory.song(song, ShuffleMode.IMPLICIT)) {
"Invalid playback parameters" "Invalid playback parameters"
}) })
} else {
logD("No song found for uri")
foregroundListener?.updateForeground(ForegroundListener.Change.MEDIA_SESSION)
} }
} }
} }
@ -373,13 +383,36 @@ class ExoPlaybackStateHolder(
rawQueue: RawQueue, rawQueue: RawQueue,
ack: StateAck.NewPlayback? ack: StateAck.NewPlayback?
) { ) {
this.parent = parent var sendNewPlaybackEvent = false
player.setMediaItems(rawQueue.heap.map { it.toMediaItem(context, null) }) var shouldSeek = false
if (rawQueue.isShuffled) { if (this.parent != parent) {
player.shuffleModeEnabled = true this.parent = parent
player.setShuffleOrder(BetterShuffleOrder(rawQueue.shuffledMapping.toIntArray())) sendNewPlaybackEvent = true
} else { }
player.shuffleModeEnabled = false if (rawQueue != resolveQueue()) {
player.setMediaItems(rawQueue.heap.map { it.toMediaItem(context, null) })
if (rawQueue.isShuffled) {
player.shuffleModeEnabled = true
player.setShuffleOrder(BetterShuffleOrder(rawQueue.shuffledMapping.toIntArray()))
} else {
player.shuffleModeEnabled = false
}
player.seekTo(rawQueue.heapIndex, C.TIME_UNSET)
player.prepare()
player.pause()
sendNewPlaybackEvent = true
shouldSeek = true
}
repeatMode(repeatMode)
// Positions in milliseconds will drift during tight restores (i.e what the music loader
// does to sanitize the state), compare by seconds instead.
// if (positionMs.msToSecs() != player.currentPosition.msToSecs() || shouldSeek) {
// player.seekTo(positionMs)
// }
if (sendNewPlaybackEvent) {
ack?.let { playbackManager.ack(this, it) }
} }
player.seekTo(rawQueue.heapIndex, C.TIME_UNSET) player.seekTo(rawQueue.heapIndex, C.TIME_UNSET)
player.prepare() player.prepare()

View file

@ -69,6 +69,10 @@ interface PlaybackStateManager {
/** Whether there is an ongoing playback session or not. */ /** Whether there is an ongoing playback session or not. */
val sessionOngoing: Boolean val sessionOngoing: Boolean
/* Whether the player is awaiting a [DeferredPlayback] to be consumed. */
val awaitingDeferredPlayback: Boolean
/** The audio session ID of the internal player. Null if no internal player exists. */ /** The audio session ID of the internal player. Null if no internal player exists. */
val currentAudioSessionId: Int? val currentAudioSessionId: Int?
@ -395,6 +399,10 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
override val sessionOngoing override val sessionOngoing
get() = stateHolder?.sessionOngoing ?: false get() = stateHolder?.sessionOngoing ?: false
override val awaitingDeferredPlayback: Boolean
get() = pendingDeferredPlayback != null
override val currentAudioSessionId: Int? override val currentAudioSessionId: Int?
get() = stateHolder?.audioSessionId get() = stateHolder?.audioSessionId

View file

@ -1,75 +0,0 @@
/*
* Copyright (c) 2024 Auxio Project
* RestoreState.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.tasker
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.joaomgcd.taskerpluginlibrary.action.TaskerPluginRunnerActionNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfig
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigNoInput
import com.joaomgcd.taskerpluginlibrary.input.TaskerInput
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import dagger.hilt.EntryPoints
import kotlinx.coroutines.runBlocking
import org.oxycblt.auxio.AuxioService
import org.oxycblt.auxio.playback.state.DeferredPlayback
class RestoreStateHelper(config: TaskerPluginConfig<Unit>) :
TaskerPluginConfigHelperNoOutputOrInput<RestoreStateRunner>(config) {
override val runnerClass: Class<RestoreStateRunner>
get() = RestoreStateRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append("Shuffles All Songs Once the Service is Available")
}
}
class RestoreStateConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
override val context: Context
get() = applicationContext
private val taskerHelper by lazy { RestoreStateHelper(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
taskerHelper.finishForTasker()
}
}
class RestoreStateRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
ContextCompat.startForegroundService(
context,
Intent(context, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true))
val entryPoint = EntryPoints.get(context.applicationContext, TaskerEntryPoint::class.java)
val playbackManager = entryPoint.playbackManager()
runBlocking {
playbackManager.playDeferred(DeferredPlayback.RestoreState(sessionRequired = true))
}
while (!playbackManager.sessionOngoing) {}
Thread.sleep(100)
return TaskerPluginResultSucess()
}
}

View file

@ -1,74 +0,0 @@
/*
* Copyright (c) 2024 Auxio Project
* ShuffleAll.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.tasker
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.joaomgcd.taskerpluginlibrary.action.TaskerPluginRunnerActionNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfig
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigNoInput
import com.joaomgcd.taskerpluginlibrary.input.TaskerInput
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import dagger.hilt.EntryPoints
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.oxycblt.auxio.AuxioService
import org.oxycblt.auxio.playback.state.DeferredPlayback
class ShuffleAllHelper(config: TaskerPluginConfig<Unit>) :
TaskerPluginConfigHelperNoOutputOrInput<ShuffleAllRunner>(config) {
override val runnerClass: Class<ShuffleAllRunner>
get() = ShuffleAllRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append("Shuffles All Songs Once the Service is Available")
}
}
class ShuffleAllConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
override val context: Context
get() = applicationContext
private val taskerHelper by lazy { ShuffleAllHelper(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
taskerHelper.finishForTasker()
}
}
class ShuffleAllRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
ContextCompat.startForegroundService(
context,
Intent(context, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true))
val entryPoint = EntryPoints.get(context.applicationContext, TaskerEntryPoint::class.java)
val playbackManager = entryPoint.playbackManager()
runBlocking(Dispatchers.Main) { playbackManager.playDeferred(DeferredPlayback.ShuffleAll) }
while (!playbackManager.sessionOngoing) {}
Thread.sleep(100)
return TaskerPluginResultSucess()
}
}

View file

@ -29,16 +29,21 @@ import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputO
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigNoInput import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigNoInput
import com.joaomgcd.taskerpluginlibrary.input.TaskerInput import com.joaomgcd.taskerpluginlibrary.input.TaskerInput
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultError
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import dagger.hilt.EntryPoints
import kotlinx.coroutines.runBlocking
import org.oxycblt.auxio.AuxioService import org.oxycblt.auxio.AuxioService
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.playback.state.DeferredPlayback
class StartActionHelper(config: TaskerPluginConfig<Unit>) : class StartHelper(config: TaskerPluginConfig<Unit>) :
TaskerPluginConfigHelperNoOutputOrInput<StartActionRunner>(config) { TaskerPluginConfigHelperNoOutputOrInput<StartStateRunner>(config) {
override val runnerClass: Class<StartActionRunner> override val runnerClass: Class<StartStateRunner>
get() = StartActionRunner::class.java get() = StartStateRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) { override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append("Starts the Auxio Service. You MUST apply an action after this.") blurbBuilder.append("Shuffles All Songs Once the Service is Available")
} }
} }
@ -46,7 +51,7 @@ class StartConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
override val context: Context override val context: Context
get() = applicationContext get() = applicationContext
private val taskerHelper by lazy { StartActionHelper(this) } private val taskerHelper by lazy { StartHelper(this) }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -54,12 +59,27 @@ class StartConfigBasicAction : Activity(), TaskerPluginConfigNoInput {
} }
} }
class StartActionRunner : TaskerPluginRunnerActionNoOutputOrInput() { class StartStateRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> { override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
ContextCompat.startForegroundService( ContextCompat.startForegroundService(
context, context,
Intent(context, AuxioService::class.java) Intent(context, AuxioService::class.java)
.putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true)) .putExtra(AuxioService.INTENT_KEY_INTERNAL_START, true)
)
val entryPoint = EntryPoints.get(context.applicationContext, TaskerEntryPoint::class.java)
val playbackManager = entryPoint.playbackManager()
runBlocking {
playbackManager.playDeferred(DeferredPlayback.RestoreState(sessionRequired = true))
}
while (!playbackManager.sessionOngoing) {
if (!playbackManager.awaitingDeferredPlayback) {
return TaskerPluginResultError(
IntegerTable.TASKER_ERROR_NOT_RESTORED,
"No state to restore, did not restart playback."
)
}
}
Thread.sleep(100)
return TaskerPluginResultSucess() return TaskerPluginResultSucess()
} }
} }

2
media

@ -1 +1 @@
Subproject commit 00124cbac493c06a24e19b01893946bdaf8faf58 Subproject commit 9fc2401b8fdc2b23905402462e775c6db4e1527f