commit | author | age
|
60719c
|
1 |
/* |
U |
2 |
AV-Direktor - Control OMXPlayer on Raspberry Pi via HTTP |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
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/>. |
|
17 |
*/ |
|
18 |
|
229976
|
19 |
package de.uhilger.calypso.handler; |
8e2038
|
20 |
|
b16b54
|
21 |
import com.sun.net.httpserver.HttpExchange; |
229976
|
22 |
import de.uhilger.calypso.App; |
U |
23 |
import de.uhilger.calypso.MeldeThread; |
|
24 |
import de.uhilger.calypso.ProzessLauscher; |
8e2038
|
25 |
import java.io.BufferedWriter; |
2dd7a5
|
26 |
import java.io.File; |
8e2038
|
27 |
import java.io.IOException; |
U |
28 |
import java.io.OutputStream; |
|
29 |
import java.io.OutputStreamWriter; |
|
30 |
import java.io.Writer; |
2dd7a5
|
31 |
import java.util.ArrayList; |
U |
32 |
import java.util.Arrays; |
|
33 |
import java.util.List; |
b16b54
|
34 |
import java.util.Map; |
8e2038
|
35 |
import java.util.logging.Level; |
U |
36 |
import java.util.logging.Logger; |
|
37 |
|
|
38 |
/** |
cc2a32
|
39 |
* Methoden zur Ausfuehrung des Programmes omxplayer des Raspberry Pi |
U |
40 |
* sowie zum Senden von Kommandos an eine laufende Instanz des |
|
41 |
* omxplayer. |
|
42 |
* |
|
43 |
* Die Klasse OMXPlayer stellt als abstrakte Basisklasse ihre Methoden |
|
44 |
* den Handler-Klassen zur Verfuegung. |
8e2038
|
45 |
* |
U |
46 |
* @author ulrich |
|
47 |
*/ |
82594d
|
48 |
public class OMXPlayer extends BasePlayer implements Player , ProzessLauscher { |
8e2038
|
49 |
|
U |
50 |
private static final Logger logger = Logger.getLogger(OMXPlayer.class.getName()); |
0c8d27
|
51 |
|
6ff352
|
52 |
public static final String NAME = "omxplayer"; |
U |
53 |
|
cfe367
|
54 |
public static final String BLANK = " "; |
37f9ab
|
55 |
public static final String CMD_TOGGLE_INFO = "z"; |
0af362
|
56 |
public static final String CMD_DEC_SPEED = "1"; |
U |
57 |
public static final String CMD_DEC_VOL = "-"; |
cfe367
|
58 |
public static final String CMD_INC_SPEED = "2"; |
37f9ab
|
59 |
public static final String CMD_INC_VOL = "="; // oder "+"; |
cfe367
|
60 |
public static final String CMD_NEXT_AUDIO = "k"; |
U |
61 |
public static final String CMD_NEXT_CHAPTER = "o"; |
|
62 |
public static final String CMD_NEXT_SUB = "m"; |
|
63 |
public static final String CMD_PAUSE_RESUME = "p"; |
|
64 |
public static final String CMD_PREV_AUDIO = "j"; |
|
65 |
public static final String CMD_PREV_CHAPTER = "i"; |
|
66 |
public static final String CMD_PREV_SUB = "n"; |
|
67 |
public static final String CMD_STOP = "q"; |
|
68 |
public static final String CMD_TOGGLE_SUB = "s"; |
2dd7a5
|
69 |
public static final String F_PLAY_ON = "playon"; |
0af362
|
70 |
public static final String F_SEEK = "seek"; |
cfe367
|
71 |
public static final String OPT_HDMI_AUDIO = "-o%20hdmi"; |
U |
72 |
public static final String OPT_LOCAL_AUDIO = "-o%20local"; |
|
73 |
public static final String PFEIL_HERAUF = "5b41"; |
|
74 |
public static final String PFEIL_HERUNTER = "5b42"; |
|
75 |
public static final String PFEIL_LINKS = "5b44"; |
|
76 |
public static final String PFEIL_RECHTS = "5b43"; |
|
77 |
public static final String SP_RUECK_30 = "rueck30"; |
|
78 |
public static final String SP_RUECK_600 = "rueck600"; |
|
79 |
public static final String SP_VOR_30 = "rueck30"; |
|
80 |
public static final String SP_VOR_600 = "vor600"; |
0af362
|
81 |
|
cfe367
|
82 |
@Override |
b6a8f0
|
83 |
public String abspielen(String urlStr, String parameter, String meldeUrlStr, String token) { |
8e2038
|
84 |
String antwort;// = null; |
U |
85 |
try { |
a7f0a1
|
86 |
Process o = App.getPlayerProcess(); |
8e2038
|
87 |
if(o != null) { |
a7f0a1
|
88 |
tilgen(); |
8e2038
|
89 |
} |
2dd7a5
|
90 |
List<String> kommando = new ArrayList(); |
6ff352
|
91 |
kommando.add(NAME); |
2dd7a5
|
92 |
kommando.addAll(Arrays.asList(parameter.split(BLANK))); |
8e2038
|
93 |
if(urlStr.startsWith("http")) { |
2dd7a5
|
94 |
kommando.add(urlStr.replace(" ", "%20")); |
8e2038
|
95 |
} else { |
2dd7a5
|
96 |
kommando.add(App.getInitParameter("nfs-prefix") + urlStr); |
U |
97 |
} |
|
98 |
logger.log(Level.FINE, "parameter: {0}", parameter); |
8e2038
|
99 |
logger.log(Level.FINE, "kommando: {0}", kommando.toString()); |
2dd7a5
|
100 |
ProcessBuilder pb = new ProcessBuilder(kommando); |
U |
101 |
pb.directory(new File(System.getProperty("omx.wd"))); |
|
102 |
Process player_process = pb.start(); |
8e2038
|
103 |
if(meldeUrlStr != null) { |
U |
104 |
MeldeThread mt = new MeldeThread(); |
|
105 |
mt.setProcess(player_process); |
|
106 |
mt.lauscherHinzufuegen(this); |
|
107 |
mt.setMeldeUrl(meldeUrlStr); |
|
108 |
mt.start(); |
|
109 |
} |
a7f0a1
|
110 |
App.setPlayerProcess(player_process); |
8e2038
|
111 |
antwort = "Abspielen gestartet, url: " + urlStr; |
U |
112 |
} |
|
113 |
catch(IOException ex) { |
|
114 |
antwort = "Fehler: " + ex.getMessage(); |
|
115 |
} |
|
116 |
return antwort; |
|
117 |
} |
b16b54
|
118 |
|
U |
119 |
@Override |
|
120 |
public StringBuilder buildParams(HttpExchange t, Map map) { |
|
121 |
StringBuilder params = new StringBuilder(); |
|
122 |
params.append("-o "); |
|
123 |
params.append(getParam(map, "o")); |
|
124 |
params.append(" --threshold "); |
|
125 |
params.append(getParam(map, "th")); |
|
126 |
params.append(" --timeout "); |
|
127 |
params.append(getParam(map, "ti")); |
|
128 |
String log = getParam(map, "log"); |
|
129 |
if (log != null && log.equalsIgnoreCase("true")) { |
|
130 |
params.append(" --genlog"); |
|
131 |
} |
|
132 |
return params; |
|
133 |
} |
|
134 |
|
8e2038
|
135 |
|
U |
136 |
/** |
|
137 |
* Einen eventuell laufenden Abspielprozess beenden und den |
|
138 |
* Servlet-Kontext bereinigen.Diese Methode kann auch verwendet werden, wenn es beim normalen |
|
139 |
Abspielen zu Fehlern kommt und womoeglich der Servlet-Kontext noch |
|
140 |
eine Referenz zu einem Abspielprozess enthaelt, die nicht mehr |
|
141 |
aktuell ist. |
|
142 |
* |
|
143 |
* Mit der Methode tilgen kann man eine solche Referenz |
|
144 |
entfernen und gibt so das Objekt wieder frei fuer die Ausfuehrung |
|
145 |
weiterer Kommandos. |
|
146 |
* |
|
147 |
* @return die Antwort des Servers |
|
148 |
*/ |
82594d
|
149 |
/* |
cfe367
|
150 |
@Override |
a7f0a1
|
151 |
public String tilgen() { |
8e2038
|
152 |
String antwort; // = null; |
U |
153 |
try { |
a7f0a1
|
154 |
Process o = App.getPlayerProcess(); |
8e2038
|
155 |
if(o == null) { |
c18e1d
|
156 |
antwort = "Es ist kein Player zum Beenden vorhanden."; |
U |
157 |
//App.setPlayerProcess(null); |
8e2038
|
158 |
} else { |
2dd7a5
|
159 |
kommando(CMD_STOP); // setzt den Prozess der App auf null |
c18e1d
|
160 |
antwort = "Player gestoppt."; |
8e2038
|
161 |
} |
U |
162 |
} |
|
163 |
catch(Exception ex) { |
|
164 |
antwort = "Fehler: " + ex.getMessage(); |
|
165 |
} |
|
166 |
return antwort; |
|
167 |
} |
82594d
|
168 |
*/ |
8e2038
|
169 |
|
U |
170 |
/* ------ Implementierung ProzessLauscher ----------------- */ |
|
171 |
|
82594d
|
172 |
/* |
8e2038
|
173 |
@Override |
U |
174 |
public void prozessBeendet(String meldeUrlStr) { |
|
175 |
try { |
|
176 |
HttpURLConnection conn = (HttpURLConnection) new URL(meldeUrlStr).openConnection(); |
|
177 |
conn.setRequestMethod("GET"); |
|
178 |
conn.connect(); |
|
179 |
int status = conn.getResponseCode(); |
2dd7a5
|
180 |
logger.log(Level.INFO, |
U |
181 |
"Abspielen beendet, Meldung an {0} mit Statuscode {1} gesendet.", |
|
182 |
new Object[]{meldeUrlStr, status}); |
c18e1d
|
183 |
/* |
U |
184 |
fuer den Fall, dass ein Stopp-Signal den Player nicht erreicht |
|
185 |
oder dort nicht funktioniert, gibt es keine Moeglichkeit festzustellen, |
|
186 |
dass der Player noch spielt. Damit in einem solchen Fall der Zeiger |
|
187 |
auf den Abspielprozess nicht verloren geht, wird der Zeiger nicht |
|
188 |
auf null gesetzt. |
|
189 |
*/ |
|
190 |
//App.setPlayerProcess(null); |
82594d
|
191 |
/* |
8e2038
|
192 |
} catch(IOException ex) { |
U |
193 |
logger.log(Level.INFO, ex.getMessage(), ex); |
|
194 |
} |
|
195 |
} |
82594d
|
196 |
*/ |
8e2038
|
197 |
} |