You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.0 KiB

import asyncio
import os
import errno
import shutil
import signal
import subprocess
import socket
import logging
shutil.copy(os.path.dirname(__file__) + "/replay-sorcery.conf", "/home/deck/.config/replay-sorcery.conf")
os.chown("/home/deck/.config/replay-sorcery.conf", 1000, 1000)
try:
os.remove("/tmp/replay-sorcery/device.sock")
except FileNotFoundError:
pass
try:
os.remove("/tmp/replay-sorcery/control.sock")
except FileNotFoundError:
pass
with open('/tmp/recorder-daemon.log', "w") as out:
kmsgrabber = subprocess.Popen([os.path.dirname(__file__) + "/out/replay-sorcery", "kms-service"], stdin=subprocess.PIPE, stdout=out, stderr=out)
os.setuid(1000)
os.environ['HOME'] = "/home/deck"
os.environ['XDG_RUNTIME_DIR'] = "/run/user/1000"
dirpath = "/home/deck/Videos/Replays"
logging.basicConfig(filename="/tmp/recorder.log",
format='[Recorder] %(asctime)s %(levelname)s %(message)s',
filemode='w',
force=True)
logger=logging.getLogger()
logger.setLevel(logging.DEBUG)
# if "replay-sorcery.conf" not in os.listdir("/home/deck/.config"):
class Plugin:
def __del__():
kmsgrabber.terminate()
kmsgrabber.join()
async def _start_recorder(self):
self.process = await asyncio.create_subprocess_exec(*[os.path.dirname(__file__) + "/out/replay-sorcery"], stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
while True:
line = await self.process.stdout.readline()
if line:
logger.info("OUT: " + line.decode("UTF-8"))
#if line == b"KMS service failed to get frame: Input/output error\n":
# Deck has switched window.
# if line.endswith(b"Failed to send frame to filter graph: Invalid argument\n"):
# await self.suspend_pause(self)
# await asyncio.sleep(0.2)
# await self.suspend_resume(self)
# break
#logger.info("RS: " + str(line))
if line.startswith(b"Saving video to '"):
self.saving.set_result(line.replace(b"Saving video to '", b"").replace(b"'...", b"").decode("UTF-8"))
else:
if self.process.returncode != 0 and self.recording and not self.paused:
await asyncio.sleep(1)
self.process = await asyncio.create_subprocess_exec(*[os.path.dirname(__file__) + "/out/replay-sorcery"], stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
continue
break
async def start_recorder(self):
if not self.recording:
self.recording = True
logger.info("Starting")
asyncio.ensure_future(self._start_recorder(self))
async def stop_recorder(self):
if self.recording:
self.recording = False
logger.info("Stopping")
os.kill(self.process.pid, signal.SIGINT)
async def toggle_recorder(self):
if self.recording:
return await self.stop_recorder(self)
else:
return await self.start_recorder(self)
async def save(self):
if self.recording and not self.paused:
self.saving = asyncio.get_event_loop().create_future()
logger.info("Saving")
with open('/tmp/recorder.log', "a") as out:
subprocess.Popen([os.path.dirname(__file__) + "/out/replay-sorcery", "save"], stdin=subprocess.PIPE, stdout=out, stderr=out)
logger.info("Done saving")
return await self.saving
return False
async def suspend_pause(self):
if self.recording:
logger.info("Suspending")
self.paused = True
os.kill(self.process.pid, signal.SIGINT)
async def suspend_resume(self):
if self.recording and self.paused:
logger.info("Resuming")
self.paused = False
asyncio.ensure_future(self._start_recorder(self))
# await self.start_recorder(self)
async def get_status(self):
return self.recording
# client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
# client.connect("/tmp/replay-sorcery/control.sock")
# client.close()
async def get_videos_len(self):
return len(os.listdir(dirpath))
async def get_videos(self):
arr = sorted(filter(lambda s: os.path.isfile(os.path.join(dirpath, s)), os.listdir(dirpath)), key=lambda s: os.path.getmtime(os.path.join(dirpath, s)))
arr.reverse()
return arr
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
self.recording = True
self.paused = True
path = os.path.dirname(os.path.abspath(__file__))
try:
os.symlink(dirpath, path + "/dist/assets", target_is_directory=True)
except OSError as e:
if e.errno == errno.EEXIST:
pass
# await self.start_recorder(self)
pass