diff --git a/process.py b/process.py index 30279506..cd49929f 100755 --- a/process.py +++ b/process.py @@ -3,14 +3,29 @@ import threading import logging import json +import time + +from omxplayer.player import OMXPlayer + logger = logging.getLogger("RaspberryCast") volume = 0 +player = None + +def playeraction(action): + global player + try: + player.action(action) + except: + pass def launchvideo(url, config, sub=False): setState("2") - os.system("echo -n q > /tmp/cmd &") # Kill previous instance of OMX + try: + player.quit() #Kill previous instance of OMX + except: + pass if config["new_log"]: os.system("sudo fbi -T 1 -a --noverbose images/processing.jpg") @@ -25,8 +40,6 @@ def launchvideo(url, config, sub=False): new_log=config["new_log"])) thread.start() - os.system("echo . > /tmp/cmd &") # Start signal for OMXplayer - def queuevideo(url, config, onlyqueue=False): logger.info('Extracting source video URL, before adding to queue...') @@ -42,7 +55,6 @@ def queuevideo(url, config, onlyqueue=False): kwargs=dict(width=config["width"], height=config["height"], new_log=config["new_log"])) thread.start() - os.system("echo . > /tmp/cmd &") # Start signal for OMXplayer else: if out is not None: with open('video.queue', 'a') as f: @@ -140,6 +152,7 @@ def playlistToQueue(url, config): def playWithOMX(url, sub, width="", height="", new_log=False): + global player logger.info("Starting OMXPlayer now.") logger.info("Attempting to read resolution from configuration file.") @@ -150,19 +163,19 @@ def playWithOMX(url, sub, width="", height="", new_log=False): resolution = " --win '0 0 {0} {1}'".format(width, height) setState("1") + args = "-b" + resolution + " --vol " + str(volume) if sub: - os.system( - "omxplayer -b -r -o both '" + url + "'" + resolution + - " --vol " + str(volume) + - " --subtitles subtitle.srt < /tmp/cmd" - ) + player = OMXPlayer(url, args + " --subtitles subtitle.srt") elif url is None: pass else: - os.system( - "omxplayer -b -r -o both '" + url + "' " + resolution + " --vol " + - str(volume) + " < /tmp/cmd" - ) + player = OMXPlayer(url, args) + + try: + while not player.playback_status() == "Stopped": # Wait until video finished or stopped + time.sleep(0.5) + except: + pass if getState() != "2": # In case we are again in the launchvideo function setState("0") @@ -181,7 +194,6 @@ def playWithOMX(url, sub, width="", height="", new_log=False): new_log=new_log), ) thread.start() - os.system("echo . > /tmp/cmd &") # Start signal for OMXplayer else: logger.info("Playlist empty, skipping.") if new_log: diff --git a/server.py b/server.py index aab75f26..ee981d51 100755 --- a/server.py +++ b/server.py @@ -12,8 +12,11 @@ from urllib import urlretrieve from bottle import Bottle, SimpleTemplate, request, response, \ template, run, static_file + from process import launchvideo, queuevideo, playlist, \ - setState, getState, setVolume + setState, getState, setVolume, playeraction + +from omxplayer.keys import * if len(sys.argv) > 1: config_file = sys.argv[1] @@ -41,13 +44,6 @@ ch.setFormatter(formatter) root.addHandler(ch) -try: - os.mkfifo("/tmp/cmd") -except OSError as e: - # 17 means the file already exists. - if e.errno != 17: - raise - if config["new_log"]: os.system("sudo fbi -T 1 --noverbose -a images/ready.jpg") @@ -171,27 +167,27 @@ def video(): control = request.query['control'] if control == "pause": logger.info('Command : pause') - os.system("echo -n p > /tmp/cmd &") + playeraction(PAUSE) return "1" elif control in ["stop", "next"]: logger.info('Command : stop video') - os.system("echo -n q > /tmp/cmd &") + playeraction(EXIT) return "1" elif control == "right": logger.info('Command : forward') - os.system("echo -n $'\x1b\x5b\x43' > /tmp/cmd &") + playeraction(SEEK_FORWARD_SMALL) return "1" elif control == "left": logger.info('Command : backward') - os.system("echo -n $'\x1b\x5b\x44' > /tmp/cmd &") + playeraction(SEEK_BACK_SMALL) return "1" elif control == "longright": logger.info('Command : long forward') - os.system("echo -n $'\x1b\x5b\x41' > /tmp/cmd &") + playeraction(SEEK_FORWARD_LARGE) return "1" elif control == "longleft": logger.info('Command : long backward') - os.system("echo -n $'\x1b\x5b\x42' > /tmp/cmd &") + playeraction(SEEK_BACK_LARGE) return "1" @@ -200,10 +196,10 @@ def sound(): vol = request.query['vol'] if vol == "more": logger.info('REMOTE: Command : Sound ++') - os.system("echo -n + > /tmp/cmd &") + playeraction(INCREASE_VOLUME) elif vol == "less": logger.info('REMOTE: Command : Sound --') - os.system("echo -n - > /tmp/cmd &") + playeraction(DECREASE_VOLUME) setVolume(vol) return "1"