-
Notifications
You must be signed in to change notification settings - Fork 1
/
PillarboxMediaSession.kt
283 lines (254 loc) · 10.2 KB
/
PillarboxMediaSession.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.session
import android.app.PendingIntent
import android.content.Context
import android.os.Bundle
import android.support.v4.media.session.MediaSessionCompat
import androidx.media3.common.MediaItem
import androidx.media3.common.util.Util
import androidx.media3.session.MediaLibraryService
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSession.MediaItemsWithStartPosition
import androidx.media3.session.SessionCommand
import androidx.media3.session.SessionCommands
import androidx.media3.session.SessionResult
import ch.srgssr.pillarbox.player.PillarboxPlayer
import ch.srgssr.pillarbox.player.utils.DebugLogger
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
/**
* [PillarboxMediaSession] link together a [MediaSession] to a [PillarboxPlayer].
*/
open class PillarboxMediaSession internal constructor() {
/**
* Callback
*/
interface Callback {
/**
* @see MediaSession.Callback.onSetMediaItems
*/
fun onSetMediaItems(
mediaSession: PillarboxMediaSession,
controller: MediaSession.ControllerInfo,
mediaItems: MutableList<MediaItem>,
startIndex: Int,
startPositionMs: Long
): ListenableFuture<MediaItemsWithStartPosition> {
return Util.transformFutureAsync(
onAddMediaItems(mediaSession, controller, mediaItems)
) { input -> Futures.immediateFuture(MediaItemsWithStartPosition(input!!, startIndex, startPositionMs)) }
}
/**
* @see MediaSession.Callback.onAddMediaItems
*/
fun onAddMediaItems(
mediaSession: PillarboxMediaSession,
controller: MediaSession.ControllerInfo,
mediaItems: MutableList<MediaItem>
): ListenableFuture<MutableList<MediaItem>> {
for (mediaItem in mediaItems) {
if (mediaItem.localConfiguration == null) {
return Futures.immediateFailedFuture(UnsupportedOperationException())
}
}
return Futures.immediateFuture(mediaItems)
}
/**
* Default implementation
*/
object Default : Callback
}
/**
* Builder
*
* @param context
* @param player
*/
class Builder(context: Context, player: PillarboxPlayer) {
private val mediaSessionBuilder = MediaSession.Builder(context, player)
private var callback: Callback = Callback.Default
/**
* Sets a [PendingIntent] to launch an [Activity][android.app.Activity] for the [MediaSession].
* This can be used as a quick link to an ongoing media screen.
*
* @param pendingIntent The [PendingIntent].
* @return this builder for convenience.
* @see MediaSession.Builder.setSessionActivity
*/
fun setSessionActivity(pendingIntent: PendingIntent): Builder {
mediaSessionBuilder.setSessionActivity(pendingIntent)
return this
}
/**
* Sets an ID of the [PillarboxMediaSession]. If not set, an empty string will be used.
* Use this if and only if your app supports multiple playback at the same time and also wants to provide external apps to have
* finer-grained controls.
*
* @param id The ID. Must be unique among all sessions per package.
* @return this builder for convenience.
* @see MediaSession.Builder.setId
*/
fun setId(id: String): Builder {
mediaSessionBuilder.setId(id)
return this
}
/**
* Set callback
*
* @param callback
*/
fun setCallback(callback: Callback) {
this.callback = callback
}
/**
* Build
*
* @return create a [PillarboxMediaSession].
*/
fun build(): PillarboxMediaSession {
val pillarboxMediaSession = PillarboxMediaSession()
val media3SessionCallback = MediaSessionCallbackImpl(callback, pillarboxMediaSession)
val mediaSession = mediaSessionBuilder
.setCallback(media3SessionCallback)
.build()
pillarboxMediaSession.setMediaSession(mediaSession)
return pillarboxMediaSession
}
}
private lateinit var _mediaSession: MediaSession
private val listener = ComponentListener()
/**
* The underlying [androidx.media3.session.MediaSession].
*/
open val mediaSession: MediaSession
get() {
return _mediaSession
}
/**
* @see MediaSession.getSessionCompatToken
*/
val token: MediaSessionCompat.Token
get() {
return _mediaSession.sessionCompatToken
}
/**
* Player
*/
var player: PillarboxPlayer
get() {
return _mediaSession.player as PillarboxPlayer
}
set(value) {
if (value != this.player) {
this.player.removeListener(listener)
_mediaSession.player = value
value.addListener(listener)
listener.updateMediaSessionExtras()
}
}
private val playerSessionState: PlayerSessionState
get() {
return PlayerSessionState(player)
}
internal fun setMediaSession(mediaSession: MediaSession) {
this._mediaSession = mediaSession
player.addListener(listener)
}
/**
* Release the underlying [MediaSession]
*/
fun release() {
player.removeListener(listener)
_mediaSession.release()
}
private inner class ComponentListener : PillarboxPlayer.Listener {
internal fun updateMediaSessionExtras() {
for (controllerInfo in _mediaSession.connectedControllers) {
_mediaSession.setSessionExtras(
controllerInfo,
playerSessionState.toBundle(_mediaSession.sessionExtras)
)
}
}
override fun onSmoothSeekingEnabledChanged(smoothSeekingEnabled: Boolean) {
updateMediaSessionExtras()
}
override fun onTrackingEnabledChanged(trackingEnabled: Boolean) {
updateMediaSessionExtras()
}
}
internal open class MediaSessionCallbackImpl(
val callback: Callback,
val mediaSession: PillarboxMediaSession
) : MediaSession.Callback {
override fun onConnect(session: MediaSession, controller: MediaSession.ControllerInfo): MediaSession.ConnectionResult {
val availableSessionCommands = SessionCommands.Builder().apply {
if (session is MediaLibraryService.MediaLibrarySession) {
addSessionCommands(MediaSession.ConnectionResult.DEFAULT_SESSION_AND_LIBRARY_COMMANDS.commands)
} else {
addSessionCommands(MediaSession.ConnectionResult.DEFAULT_SESSION_COMMANDS.commands)
}
// TODO maybe add a way integrators can add custom commands
add(PillarboxSessionCommands.COMMAND_SMOOTH_SEEKING_ENABLED)
add(PillarboxSessionCommands.COMMAND_TRACKER_ENABLED)
}.build()
val pillarboxPlayer = session.player as PillarboxPlayer
val playerSessionState = PlayerSessionState(pillarboxPlayer)
DebugLogger.debug(TAG, "onConnect with state = $playerSessionState")
val sessionExtras = playerSessionState.toBundle()
return MediaSession.ConnectionResult.AcceptedResultBuilder(session)
.setAvailableSessionCommands(availableSessionCommands)
.setSessionExtras(sessionExtras)
.build()
}
@Suppress("ReturnCount")
override fun onCustomCommand(
session: MediaSession,
controller: MediaSession.ControllerInfo,
customCommand: SessionCommand,
args: Bundle
): ListenableFuture<SessionResult> {
// TODO maybe add a way integrators can add custom commands
DebugLogger.debug(TAG, "onCustomCommand ${customCommand.customAction} ${customCommand.customExtras}")
val player = session.player
when (customCommand.customAction) {
PillarboxSessionCommands.SMOOTH_SEEKING_ENABLED -> {
if (player is PillarboxPlayer) {
player.smoothSeekingEnabled = customCommand.customExtras.getBoolean(PillarboxSessionCommands.SMOOTH_SEEKING_ARG)
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}
}
PillarboxSessionCommands.TRACKER_ENABLED -> {
if (player is PillarboxPlayer) {
player.trackingEnabled = customCommand.customExtras.getBoolean(PillarboxSessionCommands.TRACKER_ENABLED_ARG)
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}
}
}
DebugLogger.warning(TAG, "Unsupported session command ${customCommand.customAction}")
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_ERROR_NOT_SUPPORTED))
}
override fun onSetMediaItems(
mediaSession: MediaSession,
controller: MediaSession.ControllerInfo,
mediaItems: MutableList<MediaItem>,
startIndex: Int,
startPositionMs: Long
): ListenableFuture<MediaItemsWithStartPosition> {
return callback.onSetMediaItems(this.mediaSession, controller, mediaItems, startIndex, startPositionMs)
}
override fun onAddMediaItems(
mediaSession: MediaSession,
controller: MediaSession.ControllerInfo,
mediaItems: MutableList<MediaItem>
): ListenableFuture<MutableList<MediaItem>> {
return callback.onAddMediaItems(this.mediaSession, controller, mediaItems)
}
}
companion object {
internal const val TAG = "PillarboxMediaSession"
}
}