@@ -59,8 +59,28 @@ class PlayerRestHandler(
59
59
): ResponseEntity <Player > {
60
60
val context = socketContext(socketServer, sessionId)
61
61
62
- val encodedTrack = playerUpdate.encodedTrack
63
- if (encodedTrack is Omissible .Present && playerUpdate.identifier is Omissible .Present ) {
62
+ if (playerUpdate.track.isPresent() && (playerUpdate.encodedTrack is Omissible .Present || playerUpdate.identifier is Omissible .Present )) {
63
+ throw ResponseStatusException (HttpStatus .BAD_REQUEST , " Cannot specify both track and encodedTrack/identifier" )
64
+ }
65
+
66
+ val track = if (playerUpdate.track.isPresent()) {
67
+ playerUpdate.track
68
+ } else {
69
+ if (playerUpdate.encodedTrack is Omissible .Present || playerUpdate.identifier is Omissible .Present ) {
70
+ PlayerUpdateTrack (
71
+ playerUpdate.encodedTrack,
72
+ playerUpdate.identifier
73
+ ).toOmissible()
74
+ } else {
75
+ Omissible .Omitted ()
76
+ }
77
+ }
78
+
79
+ val encodedTrack = track.ifPresent { it.encoded } ? : Omissible .Omitted ()
80
+ val identifier = track.ifPresent { it.identifier } ? : Omissible .Omitted ()
81
+ val userData = track.ifPresent { it.userData } ? : Omissible .Omitted ()
82
+
83
+ if (encodedTrack is Omissible .Present && identifier is Omissible .Present ) {
64
84
throw ResponseStatusException (HttpStatus .BAD_REQUEST , " Cannot specify both encodedTrack and identifier" )
65
85
}
66
86
@@ -112,25 +132,31 @@ class PlayerRestHandler(
112
132
113
133
// we handle pause differently for playing new tracks
114
134
val paused = playerUpdate.paused
115
- paused.takeIfPresent { encodedTrack is Omissible .Omitted && playerUpdate. identifier is Omissible .Omitted }
135
+ paused.takeIfPresent { encodedTrack is Omissible .Omitted && identifier is Omissible .Omitted }
116
136
?.let {
117
137
player.setPause(it)
118
138
}
119
139
140
+ // we handle userData differently for playing new tracks
141
+ userData.takeIfPresent { encodedTrack is Omissible .Omitted && identifier is Omissible .Omitted }
142
+ ?.let {
143
+ player.track?.userData = it
144
+ }
145
+
120
146
playerUpdate.volume.ifPresent {
121
147
player.setVolume(it)
122
148
}
123
149
124
150
// we handle position differently for playing new tracks
125
- playerUpdate.position.takeIfPresent { encodedTrack is Omissible .Omitted && playerUpdate. identifier is Omissible .Omitted }
151
+ playerUpdate.position.takeIfPresent { encodedTrack is Omissible .Omitted && identifier is Omissible .Omitted }
126
152
?.let {
127
153
if (player.isPlaying) {
128
154
player.seekTo(it)
129
155
SocketServer .sendPlayerUpdate(context, player)
130
156
}
131
157
}
132
158
133
- playerUpdate.endTime.takeIfPresent { encodedTrack is Omissible .Omitted && playerUpdate. identifier is Omissible .Omitted }
159
+ playerUpdate.endTime.takeIfPresent { encodedTrack is Omissible .Omitted && identifier is Omissible .Omitted }
134
160
?.let { endTime ->
135
161
val marker = TrackMarker (endTime, TrackEndMarkerHandler (player))
136
162
player.track?.setMarker(marker)
@@ -141,72 +167,75 @@ class PlayerRestHandler(
141
167
SocketServer .sendPlayerUpdate(context, player)
142
168
}
143
169
144
- if (encodedTrack is Omissible .Present || playerUpdate. identifier is Omissible .Present ) {
170
+ if (encodedTrack is Omissible .Present || identifier is Omissible .Present ) {
145
171
146
172
if (noReplace && player.track != null ) {
147
173
log.info(" Skipping play request because of noReplace" )
148
174
return ResponseEntity .ok(player.toPlayer(context, pluginInfoModifiers))
149
175
}
150
176
player.setPause(if (paused is Omissible .Present ) paused.value else false )
151
177
152
- val track : AudioTrack ? = if (encodedTrack is Omissible .Present ) {
178
+ val newTrack : AudioTrack ? = if (encodedTrack is Omissible .Present ) {
153
179
encodedTrack.value?.let {
154
180
decodeTrack(context.audioPlayerManager, it)
155
181
}
156
182
} else {
157
183
val trackFuture = CompletableFuture <AudioTrack >()
158
- val identifier = playerUpdate.identifier as Omissible .Present
159
- context.audioPlayerManager.loadItem(identifier.value, object : AudioLoadResultHandler {
160
- override fun trackLoaded (track : AudioTrack ) {
161
- trackFuture.complete(track)
162
- }
163
-
164
- override fun playlistLoaded (playlist : AudioPlaylist ) {
165
- trackFuture.completeExceptionally(
166
- ResponseStatusException (
167
- HttpStatus .BAD_REQUEST ,
168
- " Cannot play a playlist or search result"
184
+ context.audioPlayerManager.loadItemSync(
185
+ (identifier as Omissible .Present ).value,
186
+ object : AudioLoadResultHandler {
187
+ override fun trackLoaded (track : AudioTrack ) {
188
+ trackFuture.complete(track)
189
+ }
190
+
191
+ override fun playlistLoaded (playlist : AudioPlaylist ) {
192
+ trackFuture.completeExceptionally(
193
+ ResponseStatusException (
194
+ HttpStatus .BAD_REQUEST ,
195
+ " Cannot play a playlist or search result"
196
+ )
169
197
)
170
- )
171
- }
172
-
173
- override fun noMatches () {
174
- trackFuture.completeExceptionally (
175
- ResponseStatusException (
176
- HttpStatus . BAD_REQUEST ,
177
- " No matches found for identifier "
198
+ }
199
+
200
+ override fun noMatches () {
201
+ trackFuture.completeExceptionally(
202
+ ResponseStatusException (
203
+ HttpStatus . BAD_REQUEST ,
204
+ " No matches found for identifier "
205
+ )
178
206
)
179
- )
180
- }
181
-
182
- override fun loadFailed ( exception : FriendlyException ) {
183
- trackFuture.completeExceptionally (
184
- ResponseStatusException (
185
- HttpStatus . INTERNAL_SERVER_ERROR ,
186
- exception.message,
187
- getRootCause(exception )
207
+ }
208
+
209
+ override fun loadFailed ( exception : FriendlyException ) {
210
+ trackFuture.completeExceptionally(
211
+ ResponseStatusException (
212
+ HttpStatus . INTERNAL_SERVER_ERROR ,
213
+ exception.message ,
214
+ getRootCause( exception)
215
+ )
188
216
)
189
- )
190
- }
191
- })
217
+ }
218
+ })
192
219
193
- trackFuture.exceptionally {
194
- throw it
195
- }.join()
220
+ trackFuture.join()
196
221
}
197
222
198
- track ?.let {
223
+ newTrack ?.let {
199
224
playerUpdate.position.ifPresent { position ->
200
- track.position = position
225
+ newTrack.position = position
226
+ }
227
+
228
+ userData.ifPresent { userData ->
229
+ newTrack.userData = userData
201
230
}
202
231
203
232
playerUpdate.endTime.ifPresent { endTime ->
204
233
if (endTime != null ) {
205
- track .setMarker(TrackMarker (endTime, TrackEndMarkerHandler (player)))
234
+ newTrack .setMarker(TrackMarker (endTime, TrackEndMarkerHandler (player)))
206
235
}
207
236
}
208
237
209
- player.play(track )
238
+ player.play(newTrack )
210
239
player.provideTo(context.getMediaConnection(player))
211
240
} ? : player.stop()
212
241
}
0 commit comments