/
Player.java
581 lines (503 loc) · 19.7 KB
/
Player.java
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Copyright 2015 Synced Synapse. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.jsonrpc.method;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.xbmc.kore.jsonrpc.ApiException;
import org.xbmc.kore.jsonrpc.ApiMethod;
import org.xbmc.kore.jsonrpc.type.ListType;
import org.xbmc.kore.jsonrpc.type.PlayerType;
import org.xbmc.kore.jsonrpc.type.PlayerType.GetActivePlayersReturnType;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
import org.xbmc.kore.utils.JsonUtils;
import java.util.ArrayList;
/**
* All JSON RPC methods in Playyer.*
*/
public class Player {
/**
* Returns all active players.
*/
public static final class GetActivePlayers extends ApiMethod<ArrayList<GetActivePlayersReturnType>> {
public final static String METHOD_NAME = "Player.GetActivePlayers";
/**
* Returns all active players.
*/
public GetActivePlayers() {
super();
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public ArrayList<GetActivePlayersReturnType> resultFromJson(ObjectNode jsonObject) throws ApiException {
ArrayNode resultNode = (ArrayNode)jsonObject.get(RESULT_NODE);
ArrayList<GetActivePlayersReturnType> res = new ArrayList<>();
if (resultNode != null) {
for (JsonNode node : resultNode) {
res.add(new GetActivePlayersReturnType(node));
}
}
return res;
}
}
/**
* Retrieves the currently played item
*/
public static final class GetItem extends ApiMethod<ListType.ItemsAll> {
public final static String METHOD_NAME = "Player.GetItem";
/**
* Retrieves the currently played item
* @param playerId Player id for which to retrieve the item
* @param properties Properties to retrieve.
* See {@link ListType.FieldsAll} for a list of accepted values
*/
public GetItem(int playerId, String... properties) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("properties", properties);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public ListType.ItemsAll resultFromJson(ObjectNode jsonObject) throws ApiException {
return new ListType.ItemsAll(jsonObject.get(RESULT_NODE).get("item"));
}
}
/**
* Retrieves the values of the given properties
*/
public static final class GetProperties extends ApiMethod<PlayerType.PropertyValue> {
public final static String METHOD_NAME = "Player.GetProperties";
/**
* Retrieves the values of the given properties
* @param playerId Player id for which to retrieve the item
* @param properties Properties to retrieve.
* See {@link PlayerType.PropertyName} constants for a list of accepted values
*/
public GetProperties(int playerId, String... properties) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("properties", properties);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public PlayerType.PropertyValue resultFromJson(ObjectNode jsonObject) throws ApiException {
return new PlayerType.PropertyValue(jsonObject.get(RESULT_NODE));
}
}
/**
* Pauses or unpause playback and returns the new state
*/
public static final class PlayPause extends ApiMethod<Integer> {
public final static String METHOD_NAME = "Player.PlayPause";
/**
* Pauses or unpause playback and returns the new state
* @param playerId Player id for which to toggle the state
*/
public PlayPause(int playerId) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("play", "toggle");
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public Integer resultFromJson(ObjectNode jsonObject) throws ApiException {
return JsonUtils.intFromJsonNode(jsonObject.get(RESULT_NODE), "speed");
}
}
/**
* Set the speed of the current playback
*/
public static final class SetSpeed extends ApiMethod<Integer> {
public final static String METHOD_NAME = "Player.SetSpeed";
/**
* Set the speed of the current playback
* @param playerId Player id for which to toggle the state
* @param speed String enum in {@link org.xbmc.kore.jsonrpc.type.GlobalType.IncrementDecrement}
*/
public SetSpeed(int playerId, String speed) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("speed", speed);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public Integer resultFromJson(ObjectNode jsonObject) throws ApiException {
return JsonUtils.intFromJsonNode(jsonObject.get(RESULT_NODE), "speed");
}
}
/**
* Stops playback
*/
public static final class Stop extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.Stop";
/**
* Stops playback
* @param playerId Player id for which to stop playback
*/
public Stop(int playerId) {
super();
addParameterToRequest("playerid", playerId);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Go to previous/next/specific item in the playlist.
*/
public static final class GoTo extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.GoTo";
/**
* Go to constants
*/
public static final String PREVIOUS = "previous";
public static final String NEXT = "next";
/**
* Go to previous/next/specific item in the playlist.
* @param playerId Player id for which to stop playback
* @param to Where to go. See this class constants for values
*/
public GoTo(int playerId, String to) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("to", to);
}
/**
* Go to previous/next/specific item in the playlist.
* @param playerId Player id for which to stop playback
* @param to position in playlist
*/
public GoTo(int playerId, int to) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("to", to);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Seek through the playing item
*/
public static final class Seek extends ApiMethod<PlayerType.SeekReturnType> {
public final static String METHOD_NAME = "Player.Seek";
/**
* Seek constants
*/
public static final String BACKWARD = "smallbackward";
public static final String FORWARD = "smallforward";
/**
* Seek through the playing item (by time)
* @param playerId Player id for which to stop playback
* @param value Where to seek
*/
public Seek(int playerId, PlayerType.PositionTime value) {
super();
addParameterToRequest("playerid", playerId);
ObjectNode valueObject = objectMapper.createObjectNode();
if (value != null)
valueObject.set("time", value.toJsonNode());
addParameterToRequest("value", valueObject);
}
/**
* Seek through the playing item (by percentage)
* @param playerId Player id for which to stop playback
* @param value Percentage
*/
public Seek(int playerId, int value) {
super();
addParameterToRequest("playerid", playerId);
ObjectNode valueObject = objectMapper.createObjectNode();
valueObject.put("percentage", value);
addParameterToRequest("value", valueObject);
}
/**
* Seek through the playing item (by step)
* @param playerId Player id for which to stop playback
* @param value step (smallbackward/smallforward)
*/
public Seek(int playerId, String value) {
super();
addParameterToRequest("playerid", playerId);
ObjectNode valueObject = objectMapper.createObjectNode();
valueObject.put("step", value);
addParameterToRequest("value", valueObject);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public PlayerType.SeekReturnType resultFromJson(ObjectNode jsonObject) throws ApiException {
return new PlayerType.SeekReturnType(jsonObject.get(RESULT_NODE));
}
}
/**
* Set the subtitle displayed by the player
*/
public static final class SetSubtitle extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.SetSubtitle";
/**
* SetSubtitle constants
*/
public static final String PREVIOUS = "previous";
public static final String NEXT = "next";
public static final String OFF = "off";
public static final String ON = "on";
/**
* Set the subtitle displayed by the player
* @param playerId Player id for which to stop playback
* @param subtitle One of the constanstants of this class
* @param enable Whether to enable subtitles to be displayed after setting the new subtitle
*/
public SetSubtitle(int playerId, String subtitle, boolean enable) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("subtitle", subtitle);
addParameterToRequest("enable", enable);
}
/**
* Set the subtitle displayed by the player
* @param playerId Player id for which to stop playback
* @param subtitle Index of the subtitle to display
* @param enable Whether to enable subtitles to be displayed after setting the new subtitle
*/
public SetSubtitle(int playerId, int subtitle, boolean enable) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("subtitle", subtitle);
addParameterToRequest("enable", enable);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Set the audio stream played by the player
*/
public static final class SetAudioStream extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.SetAudioStream";
/**
* SetAudioStream constants
*/
public static final String PREVIOUS = "previous";
public static final String NEXT = "next";
/**
* Set the audio stream played by the player
* @param playerId Player id for which to stop playback
* @param stream One of the constanstants of this class
*/
public SetAudioStream(int playerId, String stream) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("stream", stream);
}
/**
* Set the audio stream played by the player
* @param playerId Player id for which to stop playback
* @param stream Index of the audio stream to play
*/
public SetAudioStream(int playerId, int stream) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("stream", stream);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Set the repeat mode of the player
*/
public static final class SetRepeat extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.SetRepeat";
/**
* Set the repeat mode of the player
* @param playerId Player id for which to stop playback
* @param repeat Repeat mode, see {@link org.xbmc.kore.jsonrpc.type.PlayerType.Repeat}
*/
public SetRepeat(int playerId, String repeat) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("repeat", repeat);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Shuffle/Unshuffle items in the player
*/
public static final class SetShuffle extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.SetShuffle";
/**
* Shuffle/Unshuffle items in the player
* @param playerId Player id for which to shuffle
* @param shuffle True/false
*/
public SetShuffle(int playerId, boolean shuffle) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("shuffle", shuffle);
}
/**
* Shuffle/Unshuffle items in the player
* @param playerId Player id for which to shuffle
*/
public SetShuffle(int playerId) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("shuffle", "toggle");
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Set/Unset Party Mode on the player
*/
public static final class SetPartymode extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.SetPartymode";
/**
* Set/Unset Party Mode on the player
* @param playerId Player id for which to shuffle
* @param partymode True/false
*/
public SetPartymode(int playerId, boolean partymode) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("partymode", partymode);
}
/**
* Set/Unset Party Mode on the player
* @param playerId Player id for which to shuffle
*/
public SetPartymode(int playerId) {
super();
addParameterToRequest("playerid", playerId);
addParameterToRequest("partymode", "toggle");
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Start playback of either the playlist with the given ID, a slideshow with the pictures
* from the given directory or a single file or an item from the database.
*/
public static final class Open extends ApiMethod<String> {
public final static String METHOD_NAME = "Player.Open";
public final static String TYPE_PLAYLIST = "playlist",
TYPE_CHANNEL = "channel",
TYPE_RECORDING = "recording";
/**
* Start playback of either the playlist with the given ID, a slideshow with the pictures
* from the given directory or a single file or an item from the database.
* @param itemType This should always be TYPE_PLAYLIST
* @param playlistId Id
* @param position Position to start
*/
public Open(String itemType, int playlistId, int position) {
super();
final ObjectNode item = objectMapper.createObjectNode();
item.put("playlistid", playlistId);
item.put("position", position);
addParameterToRequest("item", item);
}
/**
* Start playback of either the playlist with the given ID, a slideshow with the pictures
* from the given directory or a single file or an item from the database.
* @param playlistItem Item to play
*/
public Open(PlaylistType.Item playlistItem) {
super();
addParameterToRequest("item", playlistItem.toJsonNode());
}
/**
* Starts playing a playlist or channel
* @param itemType TYPE_PLAYLIST or TYPE_CHANNEL
* @param itemId Corresponding ID to open
*/
public Open(String itemType, int itemId) {
super();
final ObjectNode item = objectMapper.createObjectNode();
switch (itemType) {
case TYPE_PLAYLIST:
item.put("playlistid", itemId);
break;
case TYPE_CHANNEL:
item.put("channelid", itemId);
break;
case TYPE_RECORDING:
item.put("recordingid", itemId);
break;
}
addParameterToRequest("item", item);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
/**
* Send notification message to XBMC/Kodi
*/
public static final class Notification extends ApiMethod<String> {
public final static String METHOD_NAME = "GUI.ShowNotification";
/**
* Sends a text notification message to XBMC/Kodi
* @param title The title of the notification
* @param message The text message of the notification
*/
public Notification(String title, String message) {
super(false);
addParameterToRequest("title", title);
addParameterToRequest("message", message);
}
@Override
public String getMethodName() { return METHOD_NAME; }
@Override
public String resultFromJson(ObjectNode jsonObject) throws ApiException {
return jsonObject.get(RESULT_NODE).textValue();
}
}
}