204 lines
7.4 KiB
Java
204 lines
7.4 KiB
Java
package ctbrec.ui;
|
|
|
|
import java.io.File;
|
|
import java.io.OutputStream;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import ctbrec.Config;
|
|
import ctbrec.Hmac;
|
|
import ctbrec.Model;
|
|
import ctbrec.OS;
|
|
import ctbrec.Recording;
|
|
import ctbrec.io.StreamRedirectThread;
|
|
import ctbrec.recorder.download.StreamSource;
|
|
import ctbrec.ui.controls.Dialogs;
|
|
import javafx.scene.Scene;
|
|
|
|
public class Player {
|
|
private static final Logger LOG = LoggerFactory.getLogger(Player.class);
|
|
private static PlayerThread playerThread;
|
|
public static Scene scene;
|
|
|
|
private Player() {
|
|
}
|
|
|
|
public static boolean play(String url) {
|
|
return play(url, true);
|
|
}
|
|
|
|
public static boolean play(String url, boolean async) {
|
|
boolean singlePlayer = Config.getInstance().getSettings().singlePlayer;
|
|
try {
|
|
if (singlePlayer && playerThread != null && playerThread.isRunning()) {
|
|
playerThread.stopThread();
|
|
}
|
|
|
|
playerThread = new PlayerThread(url);
|
|
if (!async) {
|
|
playerThread.join();
|
|
}
|
|
return true;
|
|
} catch (Exception e1) {
|
|
LOG.error("Couldn't start player", e1);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static boolean play(Recording rec) {
|
|
boolean singlePlayer = Config.getInstance().getSettings().singlePlayer;
|
|
try {
|
|
if (singlePlayer && playerThread != null && playerThread.isRunning()) {
|
|
playerThread.stopThread();
|
|
}
|
|
|
|
playerThread = new PlayerThread(rec);
|
|
return true;
|
|
} catch (Exception e1) {
|
|
LOG.error("Couldn't start player", e1);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static boolean play(Model model) {
|
|
return play(model, true);
|
|
}
|
|
|
|
public static boolean play(Model model, boolean async) {
|
|
try {
|
|
if (model.isOnline(true)) {
|
|
boolean singlePlayer = Config.getInstance().getSettings().singlePlayer;
|
|
if (singlePlayer && playerThread != null && playerThread.isRunning()) {
|
|
playerThread.stopThread();
|
|
}
|
|
List<StreamSource> sources = model.getStreamSources();
|
|
Collections.sort(sources);
|
|
StreamSource best = sources.get(sources.size() - 1);
|
|
LOG.debug("Playing {}", best.getMediaPlaylistUrl());
|
|
return Player.play(best.getMediaPlaylistUrl(), async);
|
|
} else {
|
|
Dialogs.showError(scene, "Room not public", "Room is currently not public", null);
|
|
return false;
|
|
}
|
|
} catch (Exception e1) {
|
|
LOG.error("Couldn't get stream information for model {}", model, e1);
|
|
Dialogs.showError(scene, "Couldn't determine stream URL", e1.getLocalizedMessage(), e1);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static void stop() {
|
|
if (playerThread != null) {
|
|
playerThread.stopThread();
|
|
}
|
|
}
|
|
|
|
private static class PlayerThread extends Thread {
|
|
private boolean running = false;
|
|
private Process playerProcess;
|
|
private String url;
|
|
private Recording rec;
|
|
|
|
PlayerThread(String url) {
|
|
this.url = url;
|
|
setName(getClass().getName());
|
|
start();
|
|
}
|
|
|
|
PlayerThread(Recording rec) {
|
|
this.rec = rec;
|
|
setName(getClass().getName());
|
|
start();
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
running = true;
|
|
Runtime rt = Runtime.getRuntime();
|
|
Config cfg = Config.getInstance();
|
|
try {
|
|
if (cfg.getSettings().localRecording && rec != null) {
|
|
File file = new File(cfg.getSettings().recordingsDir, rec.getPath());
|
|
String[] cmdline = createCmdline(file.getAbsolutePath());
|
|
playerProcess = rt.exec(cmdline, OS.getEnvironment(), file.getParentFile());
|
|
} else {
|
|
if (rec != null) {
|
|
url = getRemoteRecordingUrl(rec, cfg);
|
|
}
|
|
LOG.debug("Playing {}", url);
|
|
String[] cmdline = createCmdline(url);
|
|
playerProcess = rt.exec(cmdline);
|
|
}
|
|
|
|
// create threads, which read stdout and stderr of the player process. these are needed,
|
|
// because otherwise the internal buffer for these streams fill up and block the process
|
|
Thread std = new Thread(new StreamRedirectThread(playerProcess.getInputStream(), OutputStream.nullOutputStream()));
|
|
//Thread std = new Thread(new StreamRedirectThread(playerProcess.getInputStream(), System.out));
|
|
std.setName("Player stdout pipe");
|
|
std.setDaemon(true);
|
|
std.start();
|
|
Thread err = new Thread(new StreamRedirectThread(playerProcess.getErrorStream(), OutputStream.nullOutputStream()));
|
|
//Thread err = new Thread(new StreamRedirectThread(playerProcess.getErrorStream(), System.err));
|
|
err.setName("Player stderr pipe");
|
|
err.setDaemon(true);
|
|
err.start();
|
|
|
|
playerProcess.waitFor();
|
|
LOG.debug("Media player finished.");
|
|
} catch (Exception e) {
|
|
LOG.error("Error in player thread", e);
|
|
Dialogs.showError(scene, "Playback failed", "Couldn't start playback", e);
|
|
}
|
|
running = false;
|
|
}
|
|
|
|
private String[] createCmdline(String mediaSource) {
|
|
Config cfg = Config.getInstance();
|
|
String[] playerArgs = cfg.getSettings().mediaPlayerParams.trim().split(" ");
|
|
String[] cmdline = new String[playerArgs.length + 2];
|
|
cmdline[0] = cfg.getSettings().mediaPlayer;
|
|
//cmdline[cmdline.length - 2] = "--";
|
|
cmdline[cmdline.length - 1] = mediaSource;
|
|
System.arraycopy(playerArgs, 0, cmdline, 1, playerArgs.length);
|
|
LOG.debug("Player command line: {}", Arrays.toString(cmdline));
|
|
return cmdline;
|
|
}
|
|
|
|
private String getRemoteRecordingUrl(Recording rec, Config cfg)
|
|
throws MalformedURLException, InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
|
|
String hlsBase = Config.getInstance().getServerUrl() + "/hls";
|
|
String recUrl = hlsBase + rec.getPath() + (rec.isSingleFile() ? "" : "/playlist.m3u8");
|
|
if (cfg.getSettings().requireAuthentication) {
|
|
URL u = new URL(recUrl);
|
|
String path = u.getPath();
|
|
if (!cfg.getContextPath().isEmpty()) {
|
|
path = path.substring(cfg.getContextPath().length());
|
|
}
|
|
byte[] key = cfg.getSettings().key;
|
|
String hmac = Hmac.calculate(path, key);
|
|
recUrl = recUrl + "?hmac=" + hmac;
|
|
}
|
|
return recUrl;
|
|
}
|
|
|
|
public boolean isRunning() {
|
|
return running;
|
|
}
|
|
|
|
public void stopThread() {
|
|
if (playerProcess != null) {
|
|
playerProcess.destroy();
|
|
}
|
|
}
|
|
}
|
|
}
|