From 1e1772e3067f8e61c176c03857355ad984e6202d Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 12 Jul 2017 21:18:21 +0300 Subject: [PATCH] screen sharing initial commit --- toxygen/calls.py | 13 +++++++++---- toxygen/menu.py | 8 ++++++-- toxygen/screen_sharing.py | 22 ++++++++++++++++++++++ toxygen/settings.py | 2 +- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 toxygen/screen_sharing.py diff --git a/toxygen/calls.py b/toxygen/calls.py index 6d6bbef..a16a79d 100644 --- a/toxygen/calls.py +++ b/toxygen/calls.py @@ -6,6 +6,7 @@ from toxav_enums import * import cv2 import itertools import numpy as np +import screen_sharing # TODO: play sound until outgoing call will be started or cancelled @@ -203,10 +204,14 @@ class AV: self._video_width = s.video['width'] self._video_height = s.video['height'] - self._video = cv2.VideoCapture(s.video['device']) - self._video.set(cv2.CAP_PROP_FPS, 25) - self._video.set(cv2.CAP_PROP_FRAME_WIDTH, self._video_width) - self._video.set(cv2.CAP_PROP_FRAME_HEIGHT, self._video_height) + if s.video['device'] == -1: + self._video = screen_sharing.DesktopGrabber(s.video['x'], s.video['y'], + s.video['width'], s.video['height']) + else: + self._video = cv2.VideoCapture(s.video['device']) + self._video.set(cv2.CAP_PROP_FPS, 25) + self._video.set(cv2.CAP_PROP_FRAME_WIDTH, self._video_width) + self._video.set(cv2.CAP_PROP_FRAME_HEIGHT, self._video_height) self._video_thread = threading.Thread(target=self.send_video) self._video_thread.start() diff --git a/toxygen/menu.py b/toxygen/menu.py index 4b2419a..547d721 100644 --- a/toxygen/menu.py +++ b/toxygen/menu.py @@ -832,8 +832,12 @@ class VideoSettings(CenteredWidget): self.input.setGeometry(QtCore.QRect(25, 30, 350, 30)) self.input.currentIndexChanged.connect(self.selectionChanged) import cv2 - self.devices = [] - self.frame_max_sizes = [] + self.devices = [-1] + screen = QtWidgets.QApplication.primaryScreen() + size = screen.size() + self.frame_max_sizes = [(size.width(), size.height())] + desktop = QtWidgets.QApplication.translate("videoSettingsForm", "Desktop") + self.input.addItem(desktop) for i in range(10): v = cv2.VideoCapture(i) if v.isOpened(): diff --git a/toxygen/screen_sharing.py b/toxygen/screen_sharing.py new file mode 100644 index 0000000..265658c --- /dev/null +++ b/toxygen/screen_sharing.py @@ -0,0 +1,22 @@ +import numpy as np +from PyQt5 import QtWidgets + + +class DesktopGrabber: + + def __init__(self, x, y, width, height): + self._x = x + self._y = y + self._width = width + self._height = height + self._width -= width % 4 + self._height -= height % 4 + self._screen = QtWidgets.QApplication.primaryScreen() + + def read(self): + pixmap = self._screen.grabWindow(0, self._x, self._y, self._width, self._height) + image = pixmap.toImage() + s = image.bits().asstring(self._width * self._height * 4) + arr = np.fromstring(s, dtype=np.uint8).reshape((self._height, self._width, 4)) + + return True, arr diff --git a/toxygen/settings.py b/toxygen/settings.py index 0718b17..5457ba6 100644 --- a/toxygen/settings.py +++ b/toxygen/settings.py @@ -47,7 +47,7 @@ class Settings(dict, Singleton): self.audio = {'input': p.get_default_input_device_info()['index'] if input_devices else -1, 'output': p.get_default_output_device_info()['index'] if output_devices else -1, 'enabled': input_devices and output_devices} - self.video = {'device': 0, 'width': 640, 'height': 480} + self.video = {'device': -1, 'width': 640, 'height': 480, 'x': 0, 'y': 0} @staticmethod def get_auto_profile():