Skip to content

Commit

Permalink
Move media session implementation to exoplayer module
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Jun 12, 2024
1 parent 5e7b76b commit 6bbf315
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 50 deletions.
15 changes: 6 additions & 9 deletions app/src/main/java/org/jellyfin/androidtv/di/PlaybackModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import org.jellyfin.androidtv.ui.playback.MediaManager
import org.jellyfin.androidtv.ui.playback.RewritePlaybackLauncher
import org.jellyfin.androidtv.ui.playback.VideoQueueManager
import org.jellyfin.androidtv.ui.playback.rewrite.RewriteMediaManager
import org.jellyfin.playback.core.mediasession.MediaSessionOptions
import org.jellyfin.playback.core.mediasession.mediaSessionPlugin
import org.jellyfin.playback.core.playbackManager
import org.jellyfin.playback.exoplayer.exoPlayerPlugin
import org.jellyfin.playback.exoplayer.session.MediaSessionOptions
import org.jellyfin.playback.jellyfin.jellyfinPlugin
import org.koin.android.ext.koin.androidContext
import org.koin.core.scope.Scope
Expand All @@ -44,13 +43,10 @@ val playbackModule = module {
}

fun Scope.createPlaybackManager() = playbackManager(androidContext()) {
install(exoPlayerPlugin(get()))
install(jellyfinPlugin(get()))

val activityIntent = Intent(get(), MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(get(), 0, activityIntent, PendingIntent.FLAG_IMMUTABLE)

val notificationChannelId = "mediasession"
val notificationChannelId = "session"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
notificationChannelId,
Expand All @@ -60,12 +56,13 @@ fun Scope.createPlaybackManager() = playbackManager(androidContext()) {
NotificationManagerCompat.from(get()).createNotificationChannel(channel)
}

install(mediaSessionPlugin(get(), MediaSessionOptions(
val mediaSessionOptions = MediaSessionOptions(
channelId = notificationChannelId,
notificationId = 1,
iconSmall = R.drawable.app_icon_foreground,
openIntent = pendingIntent,
)))
openIntent = pendingIntent,)
install(exoPlayerPlugin(get(), mediaSessionOptions))
install(jellyfinPlugin(get()))

// Options
val userSettingPreferences = get<UserSettingPreferences>()
Expand Down
7 changes: 0 additions & 7 deletions playback/core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id("com.android.library")
kotlin("android")
alias(libs.plugins.kotlin.serialization)
}

android {
Expand Down Expand Up @@ -29,15 +28,9 @@ android {
dependencies {
// Kotlin
implementation(libs.kotlinx.coroutines)
implementation(libs.kotlinx.coroutines.guava)
implementation(libs.kotlinx.serialization.json)

// Android(x)
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintlayout)
implementation(libs.bundles.androidx.lifecycle)
implementation(libs.androidx.media3.session)

// Dependency Injection
implementation(libs.bundles.koin)
Expand Down
14 changes: 1 addition & 13 deletions playback/core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<service
android:name="org.jellyfin.playback.core.mediasession.AndroidMediaService"
android:exported="true"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
11 changes: 0 additions & 11 deletions playback/core/src/main/kotlin/mediasession/MediaSessionPlugin.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ internal class MediaStreamState(
}
}
}.launchIn(coroutineScope + Dispatchers.Main)

// TODO Register some kind of event when $current item is at -30 seconds to setNext()
}

Expand Down
6 changes: 5 additions & 1 deletion playback/exoplayer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ dependencies {
// AndroidX
implementation(libs.androidx.core)

// ExoPlayer
// media3
implementation(libs.androidx.media3.exoplayer)
implementation(libs.androidx.media3.exoplayer.hls)
implementation(libs.jellyfin.androidx.media3.ffmpeg.decoder)
implementation(libs.androidx.media3.ui)
implementation(libs.androidx.media3.session)

// Logging
implementation(libs.timber)

// Compatibility (desugaring)
coreLibraryDesugaring(libs.android.desugar)

// Testing
testImplementation(libs.kotest.runner.junit5)
testImplementation(libs.kotest.assertions)
Expand Down
16 changes: 15 additions & 1 deletion playback/exoplayer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application>
<service
android:name="org.jellyfin.playback.exoplayer.session.AndroidMediaService"
android:exported="true"
android:foregroundServiceType="mediaPlayback"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
</application>
</manifest>
8 changes: 7 additions & 1 deletion playback/exoplayer/src/main/kotlin/ExoPlayerPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package org.jellyfin.playback.exoplayer

import android.content.Context
import org.jellyfin.playback.core.plugin.playbackPlugin
import org.jellyfin.playback.exoplayer.session.MediaSessionOptions
import org.jellyfin.playback.exoplayer.session.MediaSessionService

fun exoPlayerPlugin(androidContext: Context) = playbackPlugin {
fun exoPlayerPlugin(
androidContext: Context,
mediaSessionOptions: MediaSessionOptions,
) = playbackPlugin {
provide(ExoPlayerBackend(androidContext))
provide(MediaSessionService(androidContext, mediaSessionOptions))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session

import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session

import android.app.PendingIntent
import androidx.annotation.DrawableRes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session

import android.os.Looper
import androidx.annotation.OptIn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session

import android.content.Context
import android.os.Looper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jellyfin.playback.core.mediasession
package org.jellyfin.playback.exoplayer.session

import androidx.core.net.toUri
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import org.jellyfin.playback.core.queue.QueueEntryMetadata

fun QueueEntryMetadata.toMediaItem() = MediaItem.Builder().apply {
if (mediaId != null) setMediaId(mediaId)
mediaId?.let { setMediaId(it) }

setMediaMetadata(MediaMetadata.Builder().apply {
setTitle(title)
Expand Down

0 comments on commit 6bbf315

Please sign in to comment.