Skip to content

Commit

Permalink
* test for webcam source mixin - already found one bug:
Browse files Browse the repository at this point in the history
* fix stop call: needs device_id now
* switch to csc_libyuv

git-svn-id: https://xpra.org/svn/Xpra/trunk@18593 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Feb 25, 2018
1 parent d576049 commit ea239db
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
43 changes: 42 additions & 1 deletion src/unittests/unit/server/source_mixins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

import unittest

from xpra.util import typedict, AdHocStruct
from xpra.util import typedict
from xpra.os_util import POSIX, OSX, BytesIOClass


class SourceMixinsTest(unittest.TestCase):

def test_clientinfo(self):
from xpra.server.source.clientinfo_mixin import ClientInfoMixin
x = ClientInfoMixin()
x.init_state()
assert x.get_connect_info()
assert x.get_info()
c = typedict()
Expand All @@ -27,12 +29,51 @@ def test_clientinfo(self):
def test_clientdisplay(self):
from xpra.server.source.clientdisplay_mixin import ClientDisplayMixin
x = ClientDisplayMixin()
x.init_state()
assert x.get_info()
c = typedict()
x.parse_client_caps(c)
assert x.get_info()
x.cleanup()
assert x.get_info()

def test_webcam(self):
if not POSIX or OSX:
return
from xpra.platform.xposix.webcam import get_virtual_video_devices, check_virtual_dir
if not check_virtual_dir():
return
devices = get_virtual_video_devices()
if not devices:
return
from xpra.server.source.webcam_mixin import WebcamMixin
wm = WebcamMixin(True, None, ["png", "jpeg"])
wm.hello_sent = True
packets = []
def send(*args):
packets.append(args)
#wm.send = send
wm.send_async = send
try:
assert wm.get_info()
device_id = 0
w, h = 640, 480
assert wm.start_virtual_webcam(device_id, w, h)
assert wm.get_info().get("webcam", {}).get("active-devices", 0)==1
assert len(packets)==1 #ack sent
frame_no = 0
encoding = "png"
buf = BytesIOClass()
from PIL import Image
image = Image.new('RGB', size=(w, h), color=(155, 0, 0))
image.save(buf, 'jpeg')
data = buf.getvalue()
buf.close()
wm.process_webcam_frame(device_id, frame_no, encoding, w, h, data)
assert len(packets)==2 #ack sent
wm.stop_virtual_webcam(device_id)
finally:
wm.cleanup()


def main():
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/server/source/idle_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def get_info(self):

def parse_client_caps(self, _c):
#start the timer
log.warn("parse_client_caps idle")
self.schedule_idle_grace_timeout()
self.schedule_idle_timeout()

Expand Down Expand Up @@ -81,6 +80,7 @@ def schedule_idle_grace_timeout(self):
if self.idle_timeout>0 and not self.is_closed():
grace = self.idle_timeout - self.idle_grace_duration
self.idle_grace_timer = self.timeout_add(max(0, int(grace*1000)), self.idle_grace_timedout)
log("schedule_idle_grace_timeout() timer=%s due in %i seconds", self.idle_grace_timer, grace)

def idle_grace_timedout(self):
self.idle_grace_timer = None
Expand Down
18 changes: 9 additions & 9 deletions src/xpra/server/source/webcam_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def cleanup(self):
self.stop_all_virtual_webcams()


def get_info(self, _proto):
def get_info(self, _proto=None):
return {
"webcam" : {
"encodings" : self.webcam_encodings,
Expand Down Expand Up @@ -78,14 +78,14 @@ def fail(msg):
self.send_webcam_stop(device_id, msg)
if not self.webcam_enabled:
fail("webcam forwarding is disabled")
return
return False
devices = self.get_device_options(device_id)
if len(devices)==0:
fail("no virtual devices found")
return
return False
if len(self.webcam_forwarding_devices)>MAX_WEBCAM_DEVICES:
fail("too many virtual devices are already in use: %i" % len(self.webcam_forwarding_devices))
return
return False
errs = {}
for vid, device_info in devices.items():
log("trying device %s: %s", vid, device_info)
Expand All @@ -100,7 +100,7 @@ def fail(msg):
log.info("webcam forwarding using %s", device_str)
#this tell the client to start sending, and the size to use - which may have changed:
self.send_webcam_ack(device_id, 0, p.get_width(), p.get_height())
return
return True
except Exception as e:
errs[device_str] = str(e)
del e
Expand All @@ -109,7 +109,7 @@ def fail(msg):
log.error(" tried %i devices:", len(errs))
for device_str, err in errs.items():
log.error(" %s : %s", device_str, err)

return False

def stop_all_virtual_webcams(self):
log("stop_all_virtual_webcams() stopping: %s", self.webcam_forwarding_devices)
Expand Down Expand Up @@ -156,8 +156,8 @@ def process_webcam_frame(self, device_id, frame_no, encoding, w, h, data):
return
#one of those two should be present
try:
csc_mod = "csc_swscale"
from xpra.codecs.csc_swscale.colorspace_converter import get_input_colorspaces, get_output_colorspaces, ColorspaceConverter #@UnresolvedImport
csc_mod = "csc_libyuv"
from xpra.codecs.csc_libyuv.colorspace_converter import get_input_colorspaces, get_output_colorspaces, ColorspaceConverter #@UnresolvedImport
except ImportError:
self.send_webcam_stop(device_id, "no csc module")
return
Expand Down Expand Up @@ -187,4 +187,4 @@ def process_webcam_frame(self, device_id, frame_no, encoding, w, h, data):
log.error(" %s error" % webcam, exc_info=True)
log.error(" %s", msg)
self.send_webcam_stop(device_id, msg)
self.stop_virtual_webcam()
self.stop_virtual_webcam(device_id)

0 comments on commit ea239db

Please sign in to comment.