|
| 1 | +# Copyright (C) 2025 Frederik Pasch |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions |
| 13 | +# and limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +from enum import Enum |
| 18 | +import py_trees |
| 19 | +import os |
| 20 | +from scenario_execution.actions.base_action import ActionError |
| 21 | +from scenario_execution.actions.run_process import RunProcess |
| 22 | +from Xlib.display import Display |
| 23 | +from Xlib import error |
| 24 | + |
| 25 | + |
| 26 | +class CaptureWindowState(Enum): |
| 27 | + IDLE = 1 |
| 28 | + WAITING_FOR_WINDOW = 2 |
| 29 | + CAPTURING = 3 |
| 30 | + DONE = 11 |
| 31 | + FAILURE = 12 |
| 32 | + |
| 33 | + |
| 34 | +class CaptureWindow(RunProcess): |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + super().__init__() |
| 38 | + self.current_state = None |
| 39 | + self.output_dir = "." |
| 40 | + self.video_size = None |
| 41 | + self.window_name = None |
| 42 | + self.root_display = None |
| 43 | + |
| 44 | + def setup(self, **kwargs): |
| 45 | + if "DISPLAY" not in os.environ: |
| 46 | + raise ActionError("capture_window() requires environment variable 'DISPLAY' to be set.", action=self) |
| 47 | + |
| 48 | + if kwargs['output_dir']: |
| 49 | + if not os.path.exists(kwargs['output_dir']): |
| 50 | + raise ActionError( |
| 51 | + f"Specified destination dir '{kwargs['output_dir']}' does not exist", action=self) |
| 52 | + self.output_dir = kwargs['output_dir'] |
| 53 | + |
| 54 | + self.root_display = Display().screen().root |
| 55 | + |
| 56 | + def execute(self, window_name: str, output_filename: str, frame_rate: float): # pylint: disable=arguments-differ |
| 57 | + super().execute(None, wait_for_shutdown=True) |
| 58 | + self.window_name = window_name |
| 59 | + self.frame_rate = frame_rate |
| 60 | + self.output_filename = output_filename |
| 61 | + self.current_state = CaptureWindowState.WAITING_FOR_WINDOW |
| 62 | + |
| 63 | + def update(self) -> py_trees.common.Status: |
| 64 | + if self.current_state == CaptureWindowState.WAITING_FOR_WINDOW: |
| 65 | + self.feedback_message = f"Waiting for window {self.window_name}..." # pylint: disable= attribute-defined-outside-init |
| 66 | + |
| 67 | + video_size = self.get_window(self.root_display, self.window_name, "") |
| 68 | + if video_size is not None: |
| 69 | + self.video_size = video_size |
| 70 | + self.feedback_message = f"Found window (size {self.video_size})..." # pylint: disable= attribute-defined-outside-init |
| 71 | + self.current_state = CaptureWindowState.IDLE |
| 72 | + |
| 73 | + cmd = ["ffmpeg", |
| 74 | + "-video_size", f"{self.video_size[2]}x{self.video_size[3]}", |
| 75 | + "-f", "x11grab", |
| 76 | + "-draw_mouse", "0", |
| 77 | + "-framerate", str(self.frame_rate), |
| 78 | + "-i", f"{os.environ["DISPLAY"]}+{self.video_size[0]},{self.video_size[1]}", |
| 79 | + "-c:v", "libx264", |
| 80 | + "-preset", "veryfast", |
| 81 | + "-f", "mp4", |
| 82 | + "-nostdin", |
| 83 | + "-y", os.path.join(self.output_dir, self.output_filename)] |
| 84 | + self.set_command(cmd) |
| 85 | + return py_trees.common.Status.RUNNING |
| 86 | + else: |
| 87 | + return super().update() |
| 88 | + |
| 89 | + def get_logger_stdout(self): |
| 90 | + return self.logger.debug |
| 91 | + |
| 92 | + def get_logger_stderr(self): |
| 93 | + return self.logger.debug |
| 94 | + |
| 95 | + def on_executed(self): |
| 96 | + self.current_state = CaptureWindowState.CAPTURING |
| 97 | + self.feedback_message = f"Capturing window..." # pylint: disable= attribute-defined-outside-init |
| 98 | + |
| 99 | + def on_process_finished(self, ret): |
| 100 | + if self.current_state == CaptureWindowState.CAPTURING: |
| 101 | + self.feedback_message = f"Capturing window failed. {self.output[-1]}" # pylint: disable= attribute-defined-outside-init |
| 102 | + return py_trees.common.Status.FAILURE |
| 103 | + return py_trees.common.Status.SUCCESS |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + def get_window(self, window, window_name, indent): |
| 108 | + children = window.query_tree().children |
| 109 | + splits = window_name.split('/') |
| 110 | + if splits: |
| 111 | + for w in children: |
| 112 | + try: |
| 113 | + if len(splits) == 1: |
| 114 | + name = w.get_wm_name() |
| 115 | + wm_class = w.get_wm_class() |
| 116 | + if (name is not None and splits[0] in str(name)) or (wm_class is not None and splits[0] in wm_class): |
| 117 | + geom = w.get_geometry() |
| 118 | + window_pos = (geom.x, geom.y, geom.width, geom.height) |
| 119 | + return window_pos |
| 120 | + else: |
| 121 | + continue |
| 122 | + else: |
| 123 | + name = w.get_wm_name() |
| 124 | + wm_class = w.get_wm_class() |
| 125 | + if (name is not None and splits[0] in str(name)) or (wm_class is not None and splits[0] in wm_class): |
| 126 | + return self.get_window(w, "/".join(splits[1:]), indent + " ") |
| 127 | + else: |
| 128 | + found = self.get_window(w, window_name, indent + " ") |
| 129 | + if found: |
| 130 | + geom = w.get_geometry() |
| 131 | + current_window_pos = (geom.x, geom.y, geom.width, geom.height) |
| 132 | + return (current_window_pos[0] + found[0], |
| 133 | + current_window_pos[1] + found[1], |
| 134 | + found[2], |
| 135 | + found[3]) |
| 136 | + except (error.BadWindow, error.XError): |
| 137 | + continue |
| 138 | + return None |
0 commit comments