commit | author | age
|
60719c
|
1 |
/* |
aaed2d
|
2 |
Calypso - Media Player Remote Control via HTTP for Raspberry Pi |
U |
3 |
Copyright (C) 2021-2023 Ulrich Hilger |
60719c
|
4 |
|
U |
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
aaed2d
|
17 |
*/ |
229976
|
18 |
package de.uhilger.calypso; |
8e2038
|
19 |
|
U |
20 |
import com.sun.net.httpserver.HttpServer; |
b16b54
|
21 |
import de.uhilger.calypso.handler.BasePlayer; |
229976
|
22 |
import de.uhilger.calypso.handler.CmdHandler; |
b16b54
|
23 |
import de.uhilger.calypso.handler.DBusHandler; |
229976
|
24 |
import de.uhilger.calypso.handler.FileHandler; |
U |
25 |
import de.uhilger.calypso.handler.LogHandler; |
|
26 |
import de.uhilger.calypso.handler.OMXPlayer; |
|
27 |
import de.uhilger.calypso.handler.PingHandler; |
|
28 |
import de.uhilger.calypso.handler.PlayHandler; |
|
29 |
import de.uhilger.calypso.handler.PlayOnHandler; |
|
30 |
import de.uhilger.calypso.handler.SeekHandler; |
|
31 |
import de.uhilger.calypso.handler.StopServerHandler; |
b16b54
|
32 |
import de.uhilger.calypso.handler.VLCKillHandler; |
U |
33 |
import de.uhilger.calypso.handler.VLCPlayer; |
ac496f
|
34 |
import de.uhilger.calypso.handler.VLCSeekHandler; |
8e2038
|
35 |
import java.io.IOException; |
U |
36 |
import java.util.logging.Logger; |
|
37 |
import java.net.InetSocketAddress; |
cd1557
|
38 |
import java.util.concurrent.Executors; |
b16b54
|
39 |
import java.util.logging.Level; |
8e2038
|
40 |
|
U |
41 |
/** |
aaed2d
|
42 |
* Die Klasse Server implementiert die HTTP-Schnittstelle zum |
U |
43 |
* Mediaplayer. Es wird ein Player-Prozesss fuer das Abspielen |
|
44 |
* eines Titels gestartet. Mit Stopp oder Ende des Titels endet |
|
45 |
* auch der Abspielprozess.<br> |
|
46 |
* <br> |
|
47 |
* zum Abspielen:<br> |
|
48 |
* /calypso/play?title=http://ein.titel.mp3&r=http://rueckmelde.url<br> |
|
49 |
* <br> |
|
50 |
* waehrend des Abspielens:<br> |
|
51 |
* /calypso/pause<br> |
|
52 |
* /calypso/seek?pos=[sekunden]<br> |
|
53 |
* /calypso/stop<br> |
|
54 |
* <br> |
|
55 |
* sonstige Funktionen:<br> |
|
56 |
* /calypso/ping<br> |
|
57 |
* /calypso/server/stop<br> |
|
58 |
* <br> |
|
59 |
* verworfene Funktionen<br> |
|
60 |
* /calypso/vol-inc<br> |
|
61 |
* /calypso/vol-dec<br> |
|
62 |
* Die Lautstaerke wird in aller Regel am Geraet geregelt, an das der |
|
63 |
* Zuspieler (der Raspi) angeschlossen ist. Eine Regelung ueber den |
|
64 |
* Zuspieler ist daher eher selten erforderlich. |
|
65 |
* |
|
66 |
* @author Ulrich Hilger |
8e2038
|
67 |
*/ |
U |
68 |
public class Server { |
b16b54
|
69 |
|
8e2038
|
70 |
private static final Logger logger = Logger.getLogger(Server.class.getName()); |
U |
71 |
|
|
72 |
private int port; |
b16b54
|
73 |
|
c2c1fd
|
74 |
private String contextName; |
b16b54
|
75 |
|
8e2038
|
76 |
public Server(int port) { |
U |
77 |
this.port = port; |
|
78 |
} |
b16b54
|
79 |
|
8e2038
|
80 |
public void setPort(int port) { |
U |
81 |
this.port = port; |
|
82 |
} |
b16b54
|
83 |
|
c2c1fd
|
84 |
/** |
b16b54
|
85 |
* |
c2c1fd
|
86 |
* @param contextName e.g. '/calypso' or '/cal' |
U |
87 |
*/ |
|
88 |
public void setContextName(String contextName) { |
|
89 |
this.contextName = contextName; |
|
90 |
} |
b16b54
|
91 |
|
U |
92 |
public void start(String playerType) throws IOException { |
|
93 |
logger.log(Level.INFO, "Server starting on port {0}", port); |
0c8d27
|
94 |
|
8e2038
|
95 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
b16b54
|
96 |
server.createContext(contextName + "/play", new PlayHandler(BasePlayer.F_PLAY)); |
U |
97 |
if (playerType.equals(App.OMX_PLAYER)) { |
|
98 |
server.createContext(contextName + "/seek", new SeekHandler(OMXPlayer.F_SEEK)); |
|
99 |
server.createContext(contextName + "/stop", new CmdHandler(OMXPlayer.CMD_STOP)); |
|
100 |
server.createContext(contextName + "/pause", new CmdHandler(OMXPlayer.CMD_PAUSE_RESUME)); |
|
101 |
server.createContext(contextName + "/vol-inc", new CmdHandler(OMXPlayer.CMD_INC_VOL)); |
|
102 |
server.createContext(contextName + "/vol-dec", new CmdHandler(OMXPlayer.CMD_DEC_VOL)); |
|
103 |
server.createContext(contextName + "/info", new CmdHandler(OMXPlayer.CMD_TOGGLE_INFO)); |
|
104 |
server.createContext(contextName + "/log", new LogHandler()); |
|
105 |
server.createContext(contextName + "/playon", new PlayOnHandler(OMXPlayer.F_PLAY_ON)); |
|
106 |
} else if (playerType.equals(App.VLC_PLAYER)) { |
|
107 |
server.createContext(contextName + "/pause", new DBusHandler(VLCPlayer.CMD_PAUSE_RESUME)); |
ac496f
|
108 |
//server.createContext(contextName + "/seek", new DBusHandler(VLCPlayer.CMD_SEEK)); |
U |
109 |
server.createContext(contextName + "/seek", new VLCSeekHandler(VLCPlayer.CMD_SEEK)); |
b16b54
|
110 |
server.createContext(contextName + "/stop", new VLCKillHandler()); |
U |
111 |
} |
c2c1fd
|
112 |
server.createContext(contextName + "/ui", new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); |
b16b54
|
113 |
server.createContext(contextName + "/ping", new PingHandler(BasePlayer.F_PING)); |
U |
114 |
server.createContext(contextName + "/server/stop", new StopServerHandler()); |
cd1557
|
115 |
//server.setExecutor(null); // creates a default executor |
U |
116 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
8e2038
|
117 |
server.start(); |
b16b54
|
118 |
} |
8e2038
|
119 |
|
U |
120 |
} |