Skip to content

Commit

Permalink
add x11 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
fred-labs committed Aug 5, 2024
1 parent 35a9808 commit cc42911
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/test_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,43 @@ jobs:
with:
name: test-scenario-execution-pybullet
path: test_scenario_execution_pybullet/test.xml
test-scenario-execution-x11:
needs: [build]
runs-on: intellabs-01
timeout-minutes: 3
container:
image: ghcr.io/intellabs/scenario-execution:${{ github.event.pull_request.base.ref }}
credentials:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Restore cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
key: ${{ runner.os }}-build-${{ github.run_number }}
path: .
- name: Test Scenario Execution X11
shell: bash
run: |
source /opt/ros/${{ github.event.pull_request.base.ref == 'main' && 'humble' || github.event.pull_request.base.ref }}/setup.bash
source install/setup.bash
sudo apt -y install mesa-utils
Xvfb :1 -screen 0 800x600x16 &
export DISPLAY=:1.0
ros2 run scenario_execution_ros scenario_execution_ros libs/scenario_execution_x11/scenarios/example_capture.osc -t -o test_scenario_execution_x11
find test_scenario_execution_x11
- name: Upload result
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
if: always()
with:
name: test-scenario-execution-x11
path: test_scenario_execution_x11/test.xml
- name: Upload video
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
if: always()
with:
name: test-scenario-execution-x11
path: test_scenario_execution_x11/capture.mp4
tests:
needs:
- test-scenario-execution
Expand All @@ -482,6 +519,7 @@ jobs:
- test-scenario-execution-gazebo
- test-scenario-execution-nav2
- test-scenario-execution-pybullet
- test-scenario-execution-x11
runs-on: intellabs-01
if: ${{ always() }}
permissions:
Expand Down
1 change: 1 addition & 0 deletions libs/scenario_execution_x11/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include scenario_execution_x11/lib_osc/*.osc
16 changes: 16 additions & 0 deletions libs/scenario_execution_x11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Scenario Execution Library for X11

The `scenario_execution_x11` package provides actions to interact with the X window system.

It provides the following scenario execution library:

- `x11.osc`: Actions specific to x11 window system


# Run Example

``` bash
colcon build
sudo apt install mesa-utils
ros2 run scenario_execution_ros scenario_execution_ros libs/scenario_execution_x11/scenarios/example_capture.osc -t
```
18 changes: 18 additions & 0 deletions libs/scenario_execution_x11/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>scenario_execution_x11</name>
<version>1.2.0</version>
<description>Scenario Execution library for X11</description>
<author email="[email protected]">Intel Labs</author>
<maintainer email="[email protected]">Intel Labs</maintainer>
<license file="../../LICENSE">Apache-2.0</license>

<depend>scenario_execution</depend>

<exec_depend>ffmpeg</exec_depend>

<export>
<build_type>ament_python</build_type>
</export>
</package>
Empty file.
21 changes: 21 additions & 0 deletions libs/scenario_execution_x11/scenario_execution_x11/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

from . import actions

__all__ = [
'actions'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

from enum import Enum


from geometry_msgs.msg import PoseWithCovarianceStamped
from lifecycle_msgs.srv import GetState
from rclpy.node import Node
from rclpy.qos import QoSDurabilityPolicy, QoSHistoryPolicy
from rclpy.qos import QoSProfile, QoSReliabilityPolicy
from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.time import Time
from rclpy.duration import Duration
from tf2_ros import Buffer
from datetime import datetime, timedelta
import py_trees
import os

from scenario_execution_ros.actions.common import get_pose_stamped, NamespacedTransformListener
from scenario_execution.actions.run_process import RunProcess
import signal

class CaptureScreenState(Enum):
IDLE = 1
CAPTURING = 2
DONE = 11
FAILURE = 12

class CaptureScreen(RunProcess):

def __init__(self, output_filename: str, frame_rate: float):
super().__init__("", wait_for_shutdown=True)
self.current_state = CaptureScreenState.IDLE
self.output_dir = "."

def setup(self, **kwargs):
if "DISPLAY" not in os.environ:
raise ValueError("capture_screen() requires environment variable 'DISPLAY' to be set.")

if kwargs['output_dir']:
if not os.path.exists(kwargs['output_dir']):
raise ValueError(
f"Specified destination dir '{kwargs['output_dir']}' does not exist")
self.output_dir = kwargs['output_dir']

def execute(self, output_filename: str, frame_rate: float): # pylint: disable=arguments-differ
cmd = ["ffmpeg",
"-f", "x11grab",
"-draw_mouse", "0",
"-framerate", str(frame_rate),
"-i", os.environ["DISPLAY"],
"-c:v", "libx264",
"-preset", "veryfast",
"-f", "mp4",
"-nostdin",
"-y", os.path.join(self.output_dir, output_filename)]
self.set_command(cmd)

def get_logger_stdout(self):
return self.logger.debug

def get_logger_stderr(self):
return self.logger.debug

def on_executed(self):
self.current_state = CaptureScreenState.CAPTURING
self.feedback_message = f"Capturing screen..." # pylint: disable= attribute-defined-outside-init

def on_process_finished(self, ret):
if self.current_state == CaptureScreenState.CAPTURING:
self.feedback_message = f"Capturing screen failed. {self.output[-1]}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0


def get_osc_library():
return 'scenario_execution_x11', 'x11.osc'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import osc.standard.base

action capture_screen:
# create video from screen capture
output_filename: string = "capture.mp4" # the initial pose to set during initialization
frame_rate: float = 25.0 # framerate of the resulting video
10 changes: 10 additions & 0 deletions libs/scenario_execution_x11/scenarios/example_capture.osc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import osc.helpers
import osc.x11

scenario example_x11:
do parallel:
capture_screen()
run_process("glxgears")
serial:
wait elapsed(10s)
emit end
4 changes: 4 additions & 0 deletions libs/scenario_execution_x11/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/scenario_execution_x11
[install]
install_scripts=$base/lib/scenario_execution_x11
47 changes: 47 additions & 0 deletions libs/scenario_execution_x11/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

from setuptools import find_namespace_packages, setup

PACKAGE_NAME = 'scenario_execution_x11'

setup(
name=PACKAGE_NAME,
version='1.2.0',
packages=find_namespace_packages(),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + PACKAGE_NAME]),
('share/' + PACKAGE_NAME, ['package.xml'])
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='Intel Labs',
maintainer_email='[email protected]',
description='Scenario Execution library for X11',
license='Apache License 2.0',
tests_require=['pytest'],
include_package_data=True,
entry_points={
'scenario_execution.actions': [
'capture_screen = scenario_execution_x11.actions.capture_screen:CaptureScreen',
],
'scenario_execution.osc_libraries': [
'x11 = '
'scenario_execution_x11.get_osc_library:get_osc_library',
]
},
)

0 comments on commit cc42911

Please sign in to comment.