Skip to content

Commit

Permalink
[OAV - Video] - Improved camera mockup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-oscarsson committed Mar 27, 2024
1 parent 1788dc2 commit 4c7d985
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 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,18 +105,23 @@ 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",
Expand All @@ -126,7 +132,7 @@ def start_video_stream_process(self, size):
"-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)

0 comments on commit 4c7d985

Please sign in to comment.