Skip to content

Commit

Permalink
Make the connection manager a normal class instead of a module
Browse files Browse the repository at this point in the history
As modules represent something native that can be re-used from the
JS side, but this is not the case here
  • Loading branch information
123mpozzi committed Oct 18, 2024
1 parent ec76873 commit 9a39c49
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.util.Log
import com.bitmovin.player.api.Player
import com.bitmovin.player.api.source.Source
import com.bitmovin.player.reactnative.extensions.drmModule
import com.bitmovin.player.reactnative.extensions.mediaSessionModule
import com.bitmovin.player.reactnative.extensions.networkModule
import com.bitmovin.player.reactnative.extensions.offlineModule
import com.bitmovin.player.reactnative.extensions.playerModule
Expand Down Expand Up @@ -59,9 +58,6 @@ abstract class BitmovinBaseModule(
protected val RejectPromiseOnExceptionBlock.networkModule: NetworkModule get() = context.networkModule
?: throw IllegalStateException("NetworkModule not found")

protected val RejectPromiseOnExceptionBlock.mediaSessionModule: MediaSessionModule
get() = context.mediaSessionModule ?: throw IllegalStateException("MediaSessionModule not found")

fun RejectPromiseOnExceptionBlock.getPlayer(
nativeId: NativeId,
playerModule: PlayerModule = this.playerModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ import com.bitmovin.player.api.Player
import com.bitmovin.player.reactnative.extensions.playerModule
import com.bitmovin.player.reactnative.services.MediaSessionPlaybackService
import com.facebook.react.bridge.*
import com.facebook.react.module.annotations.ReactModule
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

private const val MODULE_NAME = "MediaSessionModule"
@ReactModule(name = MODULE_NAME)
class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(context) {
override fun getName() = MODULE_NAME

class MediaSessionConnectionManager(val context: ReactApplicationContext) {
private var isServiceStarted = false
private lateinit var playerId: NativeId

Expand All @@ -39,7 +34,7 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(
}

fun setupMediaSession(playerId: NativeId) {
this@MediaSessionModule.playerId = playerId
this@MediaSessionConnectionManager.playerId = playerId
val intent = Intent(context, MediaSessionPlaybackService::class.java)
intent.action = Intent.ACTION_MEDIA_BUTTON

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
*/
private val players: Registry<Player> = mutableMapOf()

var mediaSessionConnectionManager: MediaSessionConnectionManager? = null

/**
* JS exported module name.
*/
Expand Down Expand Up @@ -93,9 +95,9 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
}

if (playerConfig.lockScreenControlConfig.isEnabled) {
mediaSessionConnectionManager = MediaSessionConnectionManager(context)
promise.unit.resolveOnUiThread {
mediaSessionModule
.setupMediaSession(nativeId)
mediaSessionConnectionManager?.setupMediaSession(nativeId)
}
}
}
Expand Down Expand Up @@ -222,6 +224,7 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
promise.unit.resolveOnUiThreadWithPlayer(nativeId) {
destroy()
players.remove(nativeId)
mediaSessionConnectionManager = null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.bitmovin.player.api.ui.PlayerViewConfig
import com.bitmovin.player.api.ui.StyleConfig
import com.bitmovin.player.reactnative.converter.lockScreenControlConfig
import com.bitmovin.player.reactnative.converter.toJson
import com.bitmovin.player.reactnative.extensions.mediaSessionModule
import com.bitmovin.player.reactnative.extensions.playerModule
import com.facebook.react.ReactActivity
import com.facebook.react.bridge.*
import com.facebook.react.uimanager.events.RCTEventEmitter
Expand Down Expand Up @@ -111,9 +111,9 @@ class RNPlayerView(
private var playerEventRelay: EventRelay<Player, Event>

private var mediaSessionServicePlayer: Player?
get() = context.mediaSessionModule?.serviceBinder?.value?.player
get() = context.playerModule?.mediaSessionConnectionManager?.serviceBinder?.value?.player
set(value) {
context.mediaSessionModule?.serviceBinder?.value?.player = value
context.playerModule?.mediaSessionConnectionManager?.serviceBinder?.value?.player = value
}

private val activityLifecycleObserver = object : DefaultLifecycleObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class RNPlayerViewPackage : ReactPackage {
BitmovinCastManagerModule(reactContext),
BufferModule(reactContext),
NetworkModule(reactContext),
MediaSessionModule(reactContext),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bitmovin.player.reactnative.extensions

import com.bitmovin.player.reactnative.DrmModule
import com.bitmovin.player.reactnative.MediaSessionModule
import com.bitmovin.player.reactnative.NetworkModule
import com.bitmovin.player.reactnative.OfflineModule
import com.bitmovin.player.reactnative.PlayerModule
Expand All @@ -21,4 +20,3 @@ val ReactApplicationContext.uiManagerModule get() = getModule<UIManagerModule>()
val ReactApplicationContext.drmModule get() = getModule<DrmModule>()
val ReactApplicationContext.customMessageHandlerModule get() = getModule<CustomMessageHandlerModule>()
val ReactApplicationContext.networkModule get() = getModule<NetworkModule>()
val ReactApplicationContext.mediaSessionModule get() = getModule<MediaSessionModule>()

0 comments on commit 9a39c49

Please sign in to comment.