Commit f822f7f4 authored by Jonas Jelten's avatar Jonas Jelten

added mpd control and mpv options

parent 1fd0cf63
Videoscreen
===========
Listens on a TCP port for incoming links.
The link is opened with mpv and played.
Another link terminates the previous link.
......@@ -4,43 +4,89 @@ The video screen.
import socket
import subprocess
from threading import Thread
MPV_INVOCATION = ["mpv"]
class Player(Thread):
"""
Player wrapper to detect its termination.
"""
def __init__(self, cmd, hook=None):
super().__init__()
self.cmd = cmd
self.proc = None
self.hook = hook
def run(self):
print("exec: %s" % self.cmd)
self.proc = subprocess.Popen(self.cmd)
self.proc.wait()
if self.hook:
self.hook()
def kill(self):
""" kill the contained process """
if self.proc:
try:
self.proc.wait(timeout=0.1)
return
except subprocess.TimeoutExpired:
self.proc.kill()
try:
self.proc.wait(timeout=0.5)
except subprocess.TimeoutExpired:
raise Exception("process doesn't die :(")
class VideoScreen:
"""
video screen state class
"""
def __init__(self, address, port):
self.address = address
self.port = port
def __init__(self, args):
self.address = args.listen
self.port = args.port
self.mpv = None
self.mpv_options = args.options[1:]
self.control_mpd = args.music
self.sock = None
self.current_mpv = None
def display(self, data):
""" show a new video and kill the old one """
print(" showing '%s'" % data)
if self.control_mpd:
print("pausing mpd...")
subprocess.call(["mpc", "pause"])
# kill the existing mpv:
if self.current_mpv:
try:
self.current_mpv.wait(timeout=0.1)
except subprocess.TimeoutExpired:
self.current_mpv.kill()
if self.mpv is not None:
self.mpv.kill()
try:
self.current_mpv.wait(timeout=0.5)
except subprocess.TimeoutExpired:
raise Exception("process doesn't die :(")
self.mpv = Player(MPV_INVOCATION + self.mpv_options + ["--", data],
self.on_player_terminate)
self.mpv.start()
self.current_mpv = subprocess.Popen(MPV_INVOCATION + [data])
def on_player_terminate(self):
""" what to do when the videoscreen player terminates """
if self.control_mpd:
print("resuming mpd...")
subprocess.call(["mpc", "play"])
def launch(self):
""" run the videoscreen """
print("launching on %s:%d" % (self.address, self.port))
self.sock = socket.socket()
self.sock.bind((self.address, self.port))
self.sock.listen(1)
......
......@@ -16,11 +16,20 @@ def main():
type=int, help="port to listen on")
cli.add_argument("-l", "--listen", default="0.0.0.0",
help="ip to listen on")
cli.add_argument("-m", "--music", action="store_true",
help="control mpd player by pausing and unpausing")
cli.add_argument("options", default="", nargs=argparse.REMAINDER,
help="mpv options")
args = cli.parse_args()
screen = VideoScreen(args.listen, args.port)
screen = VideoScreen(args)
screen.launch()
if __name__ == "__main__":
main()
print("ohai")
try:
main()
except KeyboardInterrupt:
pass
print("\nkthxbai")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment