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