From f8a7087779eddcb096438a5c22bb5c623437278d Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Mon, 8 Aug 2016 12:10:18 +0300 Subject: [PATCH] video recording and thread --- setup.py | 13 +++++++--- toxygen/callbacks.py | 2 +- toxygen/calls.py | 59 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 86141f5..7155c37 100644 --- a/setup.py +++ b/setup.py @@ -8,15 +8,21 @@ import sys version = program_version + '.0' -MODULES = [] +MODULES = ['numpy'] if system() in ('Windows', 'Darwin'): - MODULES = ['PyAudio', 'PySide'] + MODULES.extend(['PyAudio', 'PySide']) else: try: import pyaudio except ImportError: - MODULES = ['PyAudio'] + MODULES.append('PyAudio') + + +if system() == 'Windows': + DEPS_LINKS = [] # TODO: add opencv.whl +else: + DEPS_LINKS = [] class InstallScript(install): @@ -55,6 +61,7 @@ setup(name='Toxygen', license='GPL3', packages=['toxygen', 'toxygen.plugins', 'toxygen.styles'], install_requires=MODULES, + dependency_links=DEP_LINKS, include_package_data=True, classifiers=[ 'Programming Language :: Python :: 3 :: Only', diff --git a/toxygen/callbacks.py b/toxygen/callbacks.py index 5151637..cc4d216 100644 --- a/toxygen/callbacks.py +++ b/toxygen/callbacks.py @@ -360,7 +360,7 @@ def init_callbacks(tox, window, tray): toxav.callback_call_state(call_state, 0) toxav.callback_call(call, 0) toxav.callback_audio_receive_frame(callback_audio, 0) - tox.callback_video_receive_frame(video_receive_frame, 0) + toxav.callback_video_receive_frame(video_receive_frame, 0) tox.callback_friend_lossless_packet(lossless_packet, 0) tox.callback_friend_lossy_packet(lossy_packet, 0) diff --git a/toxygen/calls.py b/toxygen/calls.py index 7dc6b67..02272be 100644 --- a/toxygen/calls.py +++ b/toxygen/calls.py @@ -3,6 +3,7 @@ import time import threading import settings from toxav_enums import * +import cv2 # TODO: play sound until outgoing call will be started or cancelled and add timeout @@ -33,9 +34,14 @@ class AV: self._audio_duration = 60 self._audio_sample_count = self._audio_rate * self._audio_channels * self._audio_duration // 1000 + self._video = None + self._video_thread = None + self._video_running = False + def stop(self): self._running = False self.stop_audio_thread() + self.stop_video_thread() def __contains__(self, friend_number): return friend_number in self._calls @@ -120,6 +126,29 @@ class AV: self._out_stream.close() self._out_stream = None + def start_video_thread(self): + if self._video_thread is not None: + return + + self._video_running = True + + self._video = cv2.VideoCapture(0) + self._video.set(cv2.CV_CAP_PROP_FPS, 25) + self._video.set(cv2.CV_CAP_PROP_FRAME_WIDTH, 640) + self._video.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480) + + self._video_thread = threading.Thread(target=self.send_video) + self._video_thread.start() + + def stop_video_thread(self): + if self._video_thread is None: + return + + self._video_running = False + self._video_thread.join() + self._video_thread = None + self._video = None + # ----------------------------------------------------------------------------------------------------------------- # Incoming chunks # ----------------------------------------------------------------------------------------------------------------- @@ -153,10 +182,10 @@ class AV: try: pcm = self._audio_stream.read(self._audio_sample_count) if pcm: - for friend in self._calls: - if self._calls[friend].audio: + for friend_num in self._calls: + if self._calls[friend_num].audio: try: - self._toxav.audio_send_frame(friend, pcm, self._audio_sample_count, + self._toxav.audio_send_frame(friend_num, pcm, self._audio_sample_count, self._audio_channels, self._audio_rate) except: pass @@ -164,3 +193,27 @@ class AV: pass time.sleep(0.01) + + def send_video(self): + while self._video_running: + try: + result, frame = self._video.read() + if result: + height, width, channels = frame.shape + for friend_num in self._calls: + if self._calls[friend_num].video: + try: # TODO: bgr => yuv + y, u, v = convert_bgr_to_yuv(frame) + self._toxav.video_send_frame(friend_num, width, height, y, u, v) + except Exception as e: + print(e) + except Exception as e: + print(e) + + time.sleep(0.01) + + +def convert_bgr_to_yuv(frame): + print(frame.tostring()) + print(dir(frame)) + return 0, 0, 0