From 351f0018db6bfe2a7b1ecc5414728faf09c8d73a Mon Sep 17 00:00:00 2001 From: tzugen Date: Mon, 24 Jul 2023 20:33:19 +0200 Subject: [PATCH] Rework thread managment --- .../ultrasonic/service/MediaPlayerManager.kt | 47 +++++++++---------- .../org/moire/ultrasonic/service/RxBus.kt | 8 +--- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt index 8ee3792f..2a4cbbdc 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt @@ -181,23 +181,22 @@ class MediaPlayerManager( createMediaController(onCreated) - rxBusSubscription += - RxBus - .activeServerChangingObservable - .subscribeOn(AndroidSchedulers.mainThread()) - .subscribe { oldServer -> - if (oldServer != OFFLINE_DB_ID) { - // When the server changes, the playlist can retain the downloaded songs. - // Incomplete songs should be removed as the new server won't recognise them. - removeIncompleteTracksFromPlaylist() - DownloadService.requestStop() - } - if (controller is JukeboxMediaPlayer) { - // When the server changes, the Jukebox should be released. - // The new server won't understand the jukebox requests of the old one. - switchToLocalPlayer() - } + rxBusSubscription += RxBus.activeServerChangingObservable + // All interaction with the Media3 needs to happen on the main thread + .subscribeOn(RxBus.mainThread()) + .subscribe { oldServer -> + if (oldServer != OFFLINE_DB_ID) { + // When the server changes, the playlist can retain the downloaded songs. + // Incomplete songs should be removed as the new server won't recognise them. + removeIncompleteTracksFromPlaylist() + DownloadService.requestStop() } + if (controller is JukeboxMediaPlayer) { + // When the server changes, the Jukebox should be released. + // The new server won't understand the jukebox requests of the old one. + switchToLocalPlayer() + } + } rxBusSubscription += RxBus.activeServerChangedObservable.subscribe { val jukebox = activeServerProvider.getActiveServer().jukeboxByDefault @@ -208,19 +207,19 @@ class MediaPlayerManager( isJukeboxEnabled = jukebox } - rxBusSubscription += RxBus.throttledPlaylistObservable.subscribe { - // Even though Rx should launch on the main thread it doesn't always :( - mainScope.launch { + rxBusSubscription += RxBus.throttledPlaylistObservable + // All interaction with the Media3 needs to happen on the main thread + .subscribeOn(RxBus.mainThread()) + .subscribe { serializeCurrentSession() } - } - rxBusSubscription += RxBus.throttledPlayerStateObservable.subscribe { - // Even though Rx should launch on the main thread it doesn't always :( - mainScope.launch { + rxBusSubscription += RxBus.throttledPlayerStateObservable + // All interaction with the Media3 needs to happen on the main thread + .subscribeOn(RxBus.mainThread()) + .subscribe { serializeCurrentSession() } - } rxBusSubscription += RxBus.shutdownCommandObservable.subscribe { clear(false) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/RxBus.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/RxBus.kt index 9c87c519..5846f829 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/RxBus.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/RxBus.kt @@ -1,6 +1,5 @@ package org.moire.ultrasonic.service -import android.os.Looper import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.disposables.CompositeDisposable @@ -12,14 +11,9 @@ import org.moire.ultrasonic.domain.Track class RxBus { - /* - * TODO: mainThread() seems to be not equal to the "normal" main Thread, so it causes - * a lot of often unnecessary thread switching. It looks like observeOn can actually - * be removed in many cases - */ companion object { - private fun mainThread() = AndroidSchedulers.from(Looper.getMainLooper()) + fun mainThread() = AndroidSchedulers.mainThread() val shufflePlayPublisher: PublishSubject = PublishSubject.create()