+gi.require_version("GLib", "2.0")
+
+from gi.repository import Gtk # nopep8 pylint: disable=wrong-import-position
+from gi.repository import GLib # nopep8 pylint: disable=wrong-import-position
+
+
+@dataclass
+class Stream:
+ process: subprocess.Popen[bytes]
+
+ @staticmethod
+ def start(command: list[str]) -> Stream:
+ # pylint: disable=consider-using-with
+ return Stream(process=subprocess.Popen(command, stdin=subprocess.PIPE))
+
+ def stop(self) -> None:
+ stdin = self.process.stdin
+ assert stdin is not None
+ try:
+ stdin.write(b'q')
+ stdin.flush()
+ except BrokenPipeError:
+ print("Stream already stopped?", file=sys.stderr)
+ self.process.wait()
+
+
+recording: Stream | None = None
+sharing: Stream | None = None
+
+
+def make_filename() -> str:
+ directory = os.environ.get(
+ 'XDG_VIDEOS_DIR',
+ os.path.expanduser('~/Videos/SRec'))
+ os.makedirs(directory, exist_ok=True)
+ timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+ return os.path.join(directory, f'srec {timestamp}.mkv')
+
+
+def video_source(stack: Gtk.Stack) -> list[str]:
+ if stack.get_child_by_name('not_recording').get_first_child().get_active():
+ return ['-f', 'x11grab', '-i', ':0.0+0,0']
+ if is_sharing_enabled(stack):
+ return ['-f', 'v4l2', '-i', '/dev/video9']
+ return ['-f', 'v4l2', '-i', '/dev/video0']
+