Skip to content
This repository has been archived by the owner on Jun 24, 2023. It is now read-only.

add multi monitor screenshare support #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ A secure confirmation dialog will appear asking where the webcam stream is to be

Simply run the following command in the virtual machine of the screen sharing recipient:

`qubes-video-companion screenshare`
`qubes-video-companion screenshare [screenId]`

A secure confirmation dialog will appear asking where the screen to share is to be sourced from. Select any qube as the target screen, this could be a regular unprivileged qube such as `personal` or a [DisposableVM](https://www.qubes-os.org/doc/disposablevm/), or the ultimately trusted `dom0` (caution is advised to avoid information disclosure. Afterwards, confirm the operation by clicking `OK`.

Note that confirmation isn't required when a VM wants to view the screen of a DisposableVM it launched itself because the parent VM already has full control over the DisposableVM.

The command above will share all active monitors in a multi monitor setup per default. You can pass the screenId as optional parameter to share a specific screen only.

### Preview

At this point, install and open an application such as Cheese (packaged as `cheese`) to preview the webcam or screen shared video stream. You're all set!
Expand Down
8 changes: 6 additions & 2 deletions receiver/qubes-video-companion
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -E # Enable function inheritance of traps
trap exit ERR

usage() {
echo "Usage: qubes-video-companion webcam|screenshare"
echo "Usage: qubes-video-companion webcam|screenshare [screenId]"
} >&2

video_source="$1"
Expand Down Expand Up @@ -53,4 +53,8 @@ fi
/usr/share/qubes-video-companion/receiver/setup.sh
# Disabling buffering should lower latency and it doesn't seem to impact performance
# Filter standard error escape characters for safe printing to the terminal from the video sender
qrexec-client-vm --buffer-size=0 --filter-escape-chars-stderr -- dom0 "$qvc_service" /usr/share/qubes-video-companion/receiver/receiver.py
if [ -z "$2" ]; then
qrexec-client-vm --buffer-size=0 --filter-escape-chars-stderr -- dom0 "$qvc_service" /usr/share/qubes-video-companion/receiver/receiver.py
else
qrexec-client-vm --buffer-size=0 --filter-escape-chars-stderr -- dom0 "$qvc_service+$2" /usr/share/qubes-video-companion/receiver/receiver.py
fi
26 changes: 22 additions & 4 deletions receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
# Copyright (C) 2021 Demi Marie Obenour <[email protected]>
# Licensed under the MIT License. See LICENSE file for details.

import sys
import struct
import os
import struct
import sys

import gi
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk

from typing import NoReturn

def main(argv) -> NoReturn:
Expand Down Expand Up @@ -52,13 +57,26 @@ def read_video_parameters() -> (int, int, int):
raise AssertionError('bug')

untrusted_input = os.read(0, input_size)
if not untrusted_input:
raise RuntimeError('can not read from stream')
if len(untrusted_input) != input_size:
raise RuntimeError('wrong number of bytes read')
untrusted_width, untrusted_height, untrusted_fps = s.unpack(untrusted_input)
del untrusted_input

if untrusted_width > 4096 or untrusted_height > 4096 or untrusted_fps > 4096:
raise RuntimeError('excessive width, height, and/or fps')
screen = Gdk.Display().get_default().get_default_screen()
if untrusted_width > screen.width() or untrusted_height > screen.height():
raise RuntimeError('excessive width, height')

if untrusted_fps > 60:
raise RuntimeError('excessive fps')

if untrusted_width > (3840 * 3):
raise RuntimeError('excessive width')

if untrusted_height > (2160 * 3):
raise RuntimeError('excessive height')

width, height, fps = untrusted_width, untrusted_height, untrusted_fps
del untrusted_width, untrusted_height, untrusted_fps

Expand Down
47 changes: 42 additions & 5 deletions sender/screenshare.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
from service import Service
import sys

class ScreenShare(Service):
"""Screen sharing video souce class"""

_share_specific_screen = None
def __init__(self):
self.main(self)

Expand All @@ -26,10 +27,33 @@ def video_source(self) -> str:
def icon(self) -> str:
return 'video-display'

def get_specific_monitor(self, monitor_id):
amount_of_monitors = Gdk.Display().get_default().get_n_monitors()
if monitor_id >= amount_of_monitors:
return None

return Gdk.Display().get_default().get_monitor(monitor_id)

def parse_args(self):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('screen', type=int, nargs='?', default=None, help='id of the screen to share')

args = parser.parse_args()
self._share_specific_screen = args.screen

def parameters(self):
monitor = Gdk.Display().get_default().get_monitor(0)
scale, geometry = monitor.get_scale_factor(), monitor.get_geometry()
return (scale * geometry.width, scale * geometry.height, 30)
if self._share_specific_screen != None:
monitor = self.get_specific_monitor(self._share_specific_screen)
if not monitor:
raise RuntimeError("requested screen to share does not exist")

scale, geometry = monitor.get_scale_factor(), monitor.get_geometry()
return (scale * geometry.width, scale * geometry.height, 30)

else:
screen = Gdk.Display().get_default().get_default_screen()
return (screen.width(), screen.height(), 30)

def pipeline(self, width: int, height: int, fps: int):
caps = ('width={0},'
Expand All @@ -39,7 +63,7 @@ def pipeline(self, width: int, height: int, fps: int):
'pixel-aspect-ratio=1/1,'
'max-framerate={2}/1,'
'views=1'.format(width, height, fps))
return [
args = [
'ximagesrc',
'use-damage=false',
'!',
Expand All @@ -55,6 +79,19 @@ def pipeline(self, width: int, height: int, fps: int):
'!',
'fdsink',
]
if self._share_specific_screen != None:
monitor = self.get_specific_monitor(self._share_specific_screen)
if not monitor:
raise RuntimeError("requested screen to share does not exist")
geometry = monitor.get_geometry()

args.insert(1, "startx={}".format(geometry.x))
args.insert(2, "starty={}".format(geometry.y))
args.insert(3, "endx={}".format(geometry.x+geometry.width-1))
args.insert(4, "endy={}".format(geometry.y+geometry.height-1))

print(" ".join(args))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(" ".join(args))

This would break the receiver, which isn’t expecting this line.

return args

if __name__ == '__main__':
screenshare = ScreenShare()
8 changes: 6 additions & 2 deletions sender/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def parameters(self):
"""
raise NotImplementedError("Pure virtual method called!")

def parse_args(self):
import argparse
argparse.ArgumentParser().parse_args()

def quit(self) -> None:
"""Close the pipeline"""

Expand Down Expand Up @@ -119,8 +123,8 @@ def start_transmission(self) -> None:
def main(cls, self) -> NoReturn:
"""Program entry point"""

import argparse, qubesdb, os
argparse.ArgumentParser().parse_args()
import qubesdb, os
self.parse_args()

try:
target_domain = qubesdb.QubesDB().read('/name').decode('ascii', 'strict')
Expand Down