Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video: add screenshot function #1612

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions labgrid/driver/httpvideodriver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pathlib
import subprocess
import sys
from urllib.parse import urlsplit
Expand All @@ -20,15 +21,21 @@ class HTTPVideoDriver(Driver, VideoProtocol):
def get_qualities(self):
return ("high", [("high", None)])

@Driver.check_active
def stream(self, quality_hint=None):
def _get_default_port(self):
default_port = None
s = urlsplit(self.video.url)
if s.scheme == "http":
default_port = 80
elif s.scheme == "https":
default_port = 443
else:
print(f"Unknown scheme: {s.scheme}", file=sys.stderr)
return default_port

@Driver.check_active
def stream(self, quality_hint=None):
default_port = self._get_default_port()
if default_port is None:
return

url = proxymanager.get_url(self.video.url, default_port=default_port)
Expand All @@ -47,3 +54,28 @@ def stream(self, quality_hint=None):

sub = subprocess.run(pipeline)
return sub.returncode

@Driver.check_active
def screenshot(self, filename):
default_port = self._get_default_port()
if default_port is None:
return

filepath = pathlib.Path(filename)
url = proxymanager.get_url(self.video.url, default_port=default_port)
pipeline = [
"gst-launch-1.0",
"souphttpsrc",
"num-buffers=75",
f"location={url}",
"!",
"decodebin",
"!",
"jpegenc",
"!",
"filesink",
f"location={filepath.absolute()}",
]

sub = subprocess.run(pipeline)
return sub.returncode
43 changes: 43 additions & 0 deletions labgrid/driver/usbvideodriver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pathlib
import subprocess

import attr
Expand Down Expand Up @@ -170,3 +171,45 @@ def stream(self, caps_hint=None, controls=None):

rx.communicate()
tx.communicate()

@Driver.check_active
def screenshot(self, filename, caps_hint=None, controls=None):
assert isinstance(filename, str)

filepath = pathlib.Path(filename)
caps = self.select_caps(caps_hint)
pipeline = self.get_pipeline(self.video.path, caps, controls)

tx_cmd = self.video.command_prefix + ["gst-launch-1.0", "-q"]
tx_cmd += pipeline.split()
rx_cmd = ["gst-launch-1.0", "fdsrc", " num-buffers=75", "!", "jpegdec", "!", "jpegenc", "!", "filesink", f"location={filepath.absolute()}"]

tx = subprocess.Popen(
tx_cmd,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
)
rx = subprocess.Popen(
rx_cmd,
stdin=tx.stdout,
stdout=subprocess.DEVNULL,
)

# wait until one subprocess has terminated
while True:
try:
tx.wait(timeout=0.1)
break
except subprocess.TimeoutExpired:
pass
try:
rx.wait(timeout=0.1)
break
except subprocess.TimeoutExpired:
pass

rx.terminate()
tx.terminate()

rx.communicate()
tx.communicate()
4 changes: 4 additions & 0 deletions labgrid/protocol/videoprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ def get_qualities(self):
@abc.abstractmethod
def stream(self, quality_hint=None):
raise NotImplementedError

@abc.abstractmethod
def screenshot(self, filename):
raise NotImplementedError