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

MPEG Video - made restarting of video stream more robust #888

Merged
merged 3 commits into from
Apr 3, 2024
Merged
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
31 changes: 19 additions & 12 deletions mxcubecore/HardwareObjects/TangoLimaMpegVideo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Class for streaming MPEG1 video with cameras connected to
Class for streaming MPEG1 video with cameras connected to
Lima Tango Device Servers

Example configuration:
Expand All @@ -12,6 +12,7 @@
<video_mode>RGB24</video_mode>
</device>
"""

import os
import subprocess
import uuid
Expand All @@ -29,6 +30,7 @@ def __init__(self, name):
self.stream_hash = str(uuid.uuid1())
self._quality_str = "High"
self._QUALITY_STR_TO_INT = {"High": 4, "Medium": 10, "Low": 20, "Adaptive": -1}
self._port = 8000

def init(self):
super().init()
Expand Down Expand Up @@ -73,12 +75,12 @@ def start_video_stream_process(self, port):
self._video_stream_process = subprocess.Popen(
[
"video-streamer",
"-tu",
"-uri",
self.get_property("tangoname").strip(),
"-hs",
"localhost",
"-p",
port,
str(self._port),
"-q",
str(self._quality),
"-s",
Expand All @@ -96,27 +98,32 @@ def start_video_stream_process(self, port):

def stop_streaming(self):
if self._video_stream_process:
ps = psutil.Process(self._video_stream_process.pid).children() + [
self._video_stream_process
]

for p in ps:
p.kill()
try:
ps = [self._video_stream_process] + psutil.Process(
self._video_stream_process.pid
).children()
for p in ps:
p.kill()
except psutil.NoSuchProcess:
pass

self._video_stream_process = None

def start_streaming(self, _format=None, size=(0, 0), port="4042"):
def start_streaming(self, _format=None, size=(0, 0), port=None):
if _format:
self._format = _format

if port:
self._port = port

if not size[0]:
_s = self.get_width(), self.get_height()
else:
_s = size

self.set_stream_size(_s[0], _s[1])
self.start_video_stream_process(port)
self.start_video_stream_process(self._port)

def restart_streaming(self, size):
self.stop_streaming()
self.start_streaming(self._format)
self.start_streaming(self._format, size=size)
42 changes: 26 additions & 16 deletions mxcubecore/HardwareObjects/mockup/MDCameraMockup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Class for cameras connected to framegrabbers run by Taco Device Servers
"""
"""Class for cameras connected to framegrabbers run by Taco Device Servers"""

import psutil
import subprocess
import logging
Expand Down Expand Up @@ -28,6 +28,7 @@ def _init(self):
self.image = HWR.get_hardware_repository().find_in_repository(self.image_name)
self.set_is_ready(True)
self._video_stream_process = None
self._current_stream_size = "0, 0"

def init(self):
logging.getLogger("HWR").info("initializing camera object")
Expand Down Expand Up @@ -104,29 +105,34 @@ def get_available_stream_sizes(self):

return video_sizes

def set_stream_size(self, w, h):
self._current_stream_size = "%s,%s" % (int(w), int(h))

def get_stream_size(self):
return self.get_width(), self.get_height(), 1
current_size = self._current_stream_size.split(",")
scale = float(current_size[0]) / self.get_width()
return current_size + list((scale,))

def start_video_stream_process(self, size):
def start_video_stream_process(self, port):
if (
not self._video_stream_process
or self._video_stream_process.poll() is not None
):
self._video_stream_process = subprocess.Popen(
[
"video-streamer",
"-tu",
"-uri",
"test",
"-hs",
"localhost",
"-p",
self._port,
str(self._port),
"-of",
self._format,
"-q",
"4",
"-s",
"%s,%s" % size,
self._current_stream_size,
"-id",
self.stream_hash,
],
Expand All @@ -135,25 +141,29 @@ def start_video_stream_process(self, size):

def stop_streaming(self):
if self._video_stream_process:
ps = [self._video_stream_process] + psutil.Process(
self._video_stream_process.pid
).children()
for p in ps:
p.kill()
try:
ps = [self._video_stream_process] + psutil.Process(
self._video_stream_process.pid
).children()
for p in ps:
p.kill()
except psutil.NoSuchProcess:
pass

self._video_stream_process = None

def start_streaming(self, _format="MPEG1", size=(0, 0), port="4042"):
def start_streaming(self, _format="MPEG1", size=(0, 0), port="8000"):
self._format = _format
self._port = port

if not size[0]:
_s = int(self.get_width()), int(self.get_height())
else:
_s = size
_s = int(size[0]), int(size[1])

self.start_video_stream_process(_s)
self.set_stream_size(_s[0], _s[1])
self.start_video_stream_process(port)

def restart_streaming(self, size):
self.stop_streaming()
self.start_streaming(size)
self.start_streaming(self._format, size=size)
Loading