diff --git a/human/human_render.py b/human/human_render.py index 186ddbc..8e08171 100644 --- a/human/human_render.py +++ b/human/human_render.py @@ -48,7 +48,7 @@ class HumanRender(AudioHandler): logging.info('human render run') while self._exit_event.is_set() and self._is_running: self._run_step() - delay = 0.04 + delay = 0.075 time.sleep(delay) logging.info('human render exit') diff --git a/ipc/ipc.dll b/ipc/ipc.dll index d9bb918..a12ee07 100644 Binary files a/ipc/ipc.dll and b/ipc/ipc.dll differ diff --git a/ipc/ipc.exp b/ipc/ipc.exp index 7c7dc1e..433ba89 100644 Binary files a/ipc/ipc.exp and b/ipc/ipc.exp differ diff --git a/ipc/ipc.lib b/ipc/ipc.lib index 13f3748..0816970 100644 Binary files a/ipc/ipc.lib and b/ipc/ipc.lib differ diff --git a/ipc/ipc.pdb b/ipc/ipc.pdb index 9d7c96e..83c7e03 100644 Binary files a/ipc/ipc.pdb and b/ipc/ipc.pdb differ diff --git a/ipc/ipc_util.py b/ipc/ipc_util.py index ac45d20..798536a 100644 --- a/ipc/ipc_util.py +++ b/ipc/ipc_util.py @@ -38,12 +38,13 @@ class IPCUtil: self.__ipc_obj.send.restype = c_bool send_data = data.encode('utf-8') send_len = len(send_data) + 1 - return self.__ipc_obj.send(send_data, send_len) + if not self.__ipc_obj.send(send_data, send_len): + self.__ipc_obj.reConnect() + return True def send_binary(self, data, size): if not self.__init: return False - print('send_binary', size) self.__ipc_obj.send.argtypes = [c_char_p, c_uint] self.__ipc_obj.send.restype = c_bool data_ptr = cast(data, c_char_p) @@ -58,9 +59,9 @@ class IPCUtil: self.__ipc_obj.setReaderCallback.restype = c_bool return self.__ipc_obj.setReaderCallback(self.c_callback) -# + # def ipc_log_callback(log, size): -# print(f'log={log}, len={size}') +# print(f'log={log.decode("utf-8")}, len={size}') # # # util = IPCUtil('ipc_sender', 'ipc_sender') diff --git a/main.py b/main.py index ac45dd5..258f3da 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import logging import os +from ui import IpcRender from utils import config_logging logger = logging.getLogger(__name__) @@ -12,6 +13,6 @@ if __name__ == '__main__': config_logging('./logs/info.log', logging.INFO, logging.INFO) logger.info('------------start------------') - ui = PyGameUI() - ui.run() + render = IpcRender() + render.run() logger.info('------------finish------------') \ No newline at end of file diff --git a/ui/__init__.py b/ui/__init__.py index 865208a..d16e6ce 100644 --- a/ui/__init__.py +++ b/ui/__init__.py @@ -1,3 +1,3 @@ #encoding = utf8 - +from .ipc_render import IpcRender diff --git a/ui/ipc_render.py b/ui/ipc_render.py index bbd1c68..ac0c90a 100644 --- a/ui/ipc_render.py +++ b/ui/ipc_render.py @@ -2,10 +2,12 @@ import os import logging +import time from queue import Queue from human import HumanContext from ipc import IPCUtil +from utils import render_image logger = logging.getLogger(__name__) current_file_path = os.path.dirname(os.path.abspath(__file__)) @@ -15,35 +17,55 @@ class IpcRender: def __init__(self): self._human_context = None self._queue = None - self._ipc = IPCUtil('ipc_sender', 'ipc_sender') + self._exit = False + self._ipc = None + + def _send_image(self, identifier, image): + height, width, channels = image.shape + + width_bytes = width.to_bytes(4, byteorder='little') + height_bytes = height.to_bytes(4, byteorder='little') + bit_depth_bytes = channels.to_bytes(4, byteorder='little') + + img_bytes = image.tobytes() + data = identifier + width_bytes + height_bytes + bit_depth_bytes + img_bytes + self._ipc.send_binary(data, len(data)) + + def _on_reader_callback(self, data_str, size): + data_str = data_str.decode('utf-8') + print(f'on_reader_callback: {data_str}, size:{size}') + if 'quit' == data_str: + self._exit = True + elif 'heartbeat' == data_str: + pass def run(self): self._queue = Queue() self._human_context = HumanContext() self._human_context.build() + self._ipc = IPCUtil('human_product', 'human_render') + self._ipc.set_reader_callback(self._on_reader_callback) + logger.info(f'ipc listen:{self._ipc.listen()}') + render = self._human_context.render_handler render.set_image_render(self) + while not self._exit: + if not self._queue.empty(): + while self._queue.qsize() > 5: + self._queue.get() + print('render queue is slower') + + image = self._queue.get() + image = render_image(self._human_context, image) + self._send_image(b'\x01', image) + else: + time.sleep(0.02) + logger.info('ipc render exit') + def stop(self): pass - def get_image(self): - pass + def on_render(self, image): + self._queue.put(image) - def get_audio(self): - pass - - def send_audio(self, audio): - pass - - def send_image(self, image): - pass - - def send_text(self, text): - pass - - def send_command(self, command): - pass - - def send_binary(self, data, length): - pass