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; |
098eef
|
26 |
import de.uhilger.calypso.handler.MPVKillHandler; |
U |
27 |
import de.uhilger.calypso.handler.MPVPlayHandler; |
|
28 |
import de.uhilger.calypso.handler.MPVPlayer; |
|
29 |
import de.uhilger.calypso.handler.MPVSeekHandler; |
7a79ba
|
30 |
import de.uhilger.calypso.handler.MPlayHandler; |
U |
31 |
import de.uhilger.calypso.handler.MPlayer; |
229976
|
32 |
import de.uhilger.calypso.handler.OMXPlayer; |
U |
33 |
import de.uhilger.calypso.handler.PingHandler; |
|
34 |
import de.uhilger.calypso.handler.PlayHandler; |
|
35 |
import de.uhilger.calypso.handler.PlayOnHandler; |
|
36 |
import de.uhilger.calypso.handler.SeekHandler; |
098eef
|
37 |
import de.uhilger.calypso.handler.SocketHandler; |
229976
|
38 |
import de.uhilger.calypso.handler.StopServerHandler; |
b16b54
|
39 |
import de.uhilger.calypso.handler.VLCKillHandler; |
U |
40 |
import de.uhilger.calypso.handler.VLCPlayer; |
ac496f
|
41 |
import de.uhilger.calypso.handler.VLCSeekHandler; |
098eef
|
42 |
import java.io.File; |
8e2038
|
43 |
import java.io.IOException; |
U |
44 |
import java.util.logging.Logger; |
|
45 |
import java.net.InetSocketAddress; |
cd1557
|
46 |
import java.util.concurrent.Executors; |
b16b54
|
47 |
import java.util.logging.Level; |
8e2038
|
48 |
|
U |
49 |
/** |
aaed2d
|
50 |
* Die Klasse Server implementiert die HTTP-Schnittstelle zum |
U |
51 |
* Mediaplayer. Es wird ein Player-Prozesss fuer das Abspielen |
|
52 |
* eines Titels gestartet. Mit Stopp oder Ende des Titels endet |
|
53 |
* auch der Abspielprozess.<br> |
|
54 |
* <br> |
|
55 |
* zum Abspielen:<br> |
|
56 |
* /calypso/play?title=http://ein.titel.mp3&r=http://rueckmelde.url<br> |
|
57 |
* <br> |
|
58 |
* waehrend des Abspielens:<br> |
|
59 |
* /calypso/pause<br> |
|
60 |
* /calypso/seek?pos=[sekunden]<br> |
|
61 |
* /calypso/stop<br> |
|
62 |
* <br> |
|
63 |
* sonstige Funktionen:<br> |
|
64 |
* /calypso/ping<br> |
|
65 |
* /calypso/server/stop<br> |
|
66 |
* <br> |
|
67 |
* verworfene Funktionen<br> |
|
68 |
* /calypso/vol-inc<br> |
|
69 |
* /calypso/vol-dec<br> |
|
70 |
* Die Lautstaerke wird in aller Regel am Geraet geregelt, an das der |
|
71 |
* Zuspieler (der Raspi) angeschlossen ist. Eine Regelung ueber den |
|
72 |
* Zuspieler ist daher eher selten erforderlich. |
|
73 |
* |
|
74 |
* @author Ulrich Hilger |
8e2038
|
75 |
*/ |
U |
76 |
public class Server { |
b16b54
|
77 |
|
8e2038
|
78 |
private static final Logger logger = Logger.getLogger(Server.class.getName()); |
U |
79 |
|
|
80 |
private int port; |
b16b54
|
81 |
|
c2c1fd
|
82 |
private String contextName; |
b16b54
|
83 |
|
098eef
|
84 |
private String path; |
U |
85 |
|
8e2038
|
86 |
public Server(int port) { |
U |
87 |
this.port = port; |
|
88 |
} |
b16b54
|
89 |
|
8e2038
|
90 |
public void setPort(int port) { |
U |
91 |
this.port = port; |
098eef
|
92 |
} |
U |
93 |
|
|
94 |
public void setPath(String path) { |
|
95 |
this.path = path; |
8e2038
|
96 |
} |
b16b54
|
97 |
|
c2c1fd
|
98 |
/** |
b16b54
|
99 |
* |
c2c1fd
|
100 |
* @param contextName e.g. '/calypso' or '/cal' |
U |
101 |
*/ |
|
102 |
public void setContextName(String contextName) { |
|
103 |
this.contextName = contextName; |
|
104 |
} |
b16b54
|
105 |
|
U |
106 |
public void start(String playerType) throws IOException { |
|
107 |
logger.log(Level.INFO, "Server starting on port {0}", port); |
0c8d27
|
108 |
|
8e2038
|
109 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
7a79ba
|
110 |
|
b16b54
|
111 |
if (playerType.equals(App.OMX_PLAYER)) { |
7a79ba
|
112 |
server.createContext(contextName + "/play", new PlayHandler(BasePlayer.F_PLAY)); |
b16b54
|
113 |
server.createContext(contextName + "/seek", new SeekHandler(OMXPlayer.F_SEEK)); |
U |
114 |
server.createContext(contextName + "/stop", new CmdHandler(OMXPlayer.CMD_STOP)); |
|
115 |
server.createContext(contextName + "/pause", new CmdHandler(OMXPlayer.CMD_PAUSE_RESUME)); |
|
116 |
server.createContext(contextName + "/vol-inc", new CmdHandler(OMXPlayer.CMD_INC_VOL)); |
|
117 |
server.createContext(contextName + "/vol-dec", new CmdHandler(OMXPlayer.CMD_DEC_VOL)); |
|
118 |
server.createContext(contextName + "/info", new CmdHandler(OMXPlayer.CMD_TOGGLE_INFO)); |
|
119 |
server.createContext(contextName + "/log", new LogHandler()); |
|
120 |
server.createContext(contextName + "/playon", new PlayOnHandler(OMXPlayer.F_PLAY_ON)); |
|
121 |
} else if (playerType.equals(App.VLC_PLAYER)) { |
7a79ba
|
122 |
server.createContext(contextName + "/play", new PlayHandler(BasePlayer.F_PLAY)); |
b16b54
|
123 |
server.createContext(contextName + "/pause", new DBusHandler(VLCPlayer.CMD_PAUSE_RESUME)); |
7a79ba
|
124 |
//server.createContext(contextName + "/pause", new CmdHandler("pause")); |
ac496f
|
125 |
//server.createContext(contextName + "/seek", new DBusHandler(VLCPlayer.CMD_SEEK)); |
U |
126 |
server.createContext(contextName + "/seek", new VLCSeekHandler(VLCPlayer.CMD_SEEK)); |
b16b54
|
127 |
server.createContext(contextName + "/stop", new VLCKillHandler()); |
7a79ba
|
128 |
} else if (playerType.equals(App.M_PLAYER)) { |
U |
129 |
server.createContext(contextName + "/play", new MPlayHandler()); |
|
130 |
server.createContext(contextName + "/pause", new CmdHandler(MPlayer.CMD_PAUSE_RESUME)); |
|
131 |
server.createContext(contextName + "/stop", new CmdHandler(MPlayer.CMD_STOP)); |
098eef
|
132 |
} else if (playerType.equals(App.MPV_PLAYER)) { |
U |
133 |
server.createContext(contextName + "/play", new MPVPlayHandler()); |
|
134 |
server.createContext(contextName + "/pause", new SocketHandler(path + MPVPlayer.CMD_PAUSE_RESUME)); |
|
135 |
server.createContext(contextName + "/seek", new MPVSeekHandler(path + MPVPlayer.CMD_SEEK)); |
|
136 |
server.createContext(contextName + "/stop", new MPVKillHandler()); |
b16b54
|
137 |
} |
c2c1fd
|
138 |
server.createContext(contextName + "/ui", new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); |
b16b54
|
139 |
server.createContext(contextName + "/ping", new PingHandler(BasePlayer.F_PING)); |
U |
140 |
server.createContext(contextName + "/server/stop", new StopServerHandler()); |
cd1557
|
141 |
//server.setExecutor(null); // creates a default executor |
U |
142 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
8e2038
|
143 |
server.start(); |
b16b54
|
144 |
} |
8e2038
|
145 |
|
U |
146 |
} |