Add android graceful fallback to transcode & onPlaybackFailed event

This commit is contained in:
advplyr
2022-04-16 13:36:30 -05:00
parent 6cea0ba03d
commit 760d05cf1b
7 changed files with 51 additions and 13 deletions
@@ -48,6 +48,8 @@ class PlaybackSession(
@get:JsonIgnore
val isHLS get() = playMethod == PLAYMETHOD_TRANSCODE
@get:JsonIgnore
val isDirectPlay get() = playMethod == PLAYMETHOD_DIRECTPLAY
@get:JsonIgnore
val isLocal get() = playMethod == PLAYMETHOD_LOCAL
@get:JsonIgnore
val currentTimeMs get() = (currentTime * 1000L).toLong()
@@ -15,8 +15,9 @@ class PlayerListener(var playerNotificationService:PlayerNotificationService) :
private var onSeekBack: Boolean = false
override fun onPlayerError(error: PlaybackException) {
error.message?.let { Log.e(tag, it) }
error.localizedMessage?.let { Log.e(tag, it) }
var errorMessage = error.message ?: "Unknown Error"
Log.e(tag, "onPlayerError $errorMessage")
playerNotificationService.handlePlayerPlaybackError(errorMessage) // If was direct playing session, fallback to transcode
}
override fun onEvents(player: Player, events: Player.Events) {
@@ -55,6 +55,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
fun onSleepTimerEnded(currentPosition: Long)
fun onSleepTimerSet(sleepTimeRemaining: Int)
fun onLocalMediaProgressUpdate(localMediaProgress: LocalMediaProgress)
fun onPlaybackFailed(errorMessage:String)
}
private val tag = "PlayerService"
@@ -289,7 +290,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
mediaSession.setMetadata(metadata)
var mediaItems = playbackSession.getMediaItems()
if (mPlayer == currentPlayer) {
var mediaSource:MediaSource
if (playbackSession.isLocal) {
@@ -333,6 +333,26 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
currentPlayer.prepare()
}
fun handlePlayerPlaybackError(errorMessage:String) {
// On error and was attempting to direct play - fallback to transcode
currentPlaybackSession?.let { playbackSession ->
if (playbackSession.isDirectPlay) {
Log.d(tag, "Fallback to transcode")
var libraryItemId = playbackSession.libraryItemId ?: "" // Must be true since direct play
var episodeId = playbackSession.episodeId
apiHandler.playLibraryItem(libraryItemId, episodeId, true) {
Handler(Looper.getMainLooper()).post() {
preparePlayer(it, true)
}
}
} else {
clientEventEmitter?.onPlaybackFailed(errorMessage)
closePlayback()
}
}
}
fun switchToPlayer(useCastPlayer: Boolean) {
currentPlayer = if (useCastPlayer) {
Log.d(tag, "switchToPlayer: Using Cast Player " + castPlayer?.deviceInfo)
@@ -68,6 +68,10 @@ class AbsAudioPlayer : Plugin() {
override fun onLocalMediaProgressUpdate(localMediaProgress: LocalMediaProgress) {
notifyListeners("onLocalMediaProgressUpdate", JSObject(jacksonMapper.writeValueAsString(localMediaProgress)))
}
override fun onPlaybackFailed(errorMessage: String) {
emit("onPlaybackFailed", errorMessage)
}
})
}
mainActivity.pluginCallback = foregroundServiceReady
@@ -151,12 +151,11 @@ class ApiHandler {
}
}
fun playLibraryItem(libraryItemId:String, episodeId:String, forceTranscode:Boolean, cb: (PlaybackSession) -> Unit) {
fun playLibraryItem(libraryItemId:String, episodeId:String?, forceTranscode:Boolean, cb: (PlaybackSession) -> Unit) {
var payload = JSObject()
payload.put("mediaPlayer", "exo-player")
// Only if direct play fails do we force transcode
// TODO: Fallback to transcode
if (!forceTranscode) payload.put("forceDirectPlay", true)
else payload.put("forceTranscode", true)