From 541072f8e0c44a46c3f5dcab980a841782978210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Tue, 6 Aug 2024 17:20:41 -0700 Subject: [PATCH] transports(base_output): improve render sleep computation --- src/pipecat/transports/base_output.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/pipecat/transports/base_output.py b/src/pipecat/transports/base_output.py index e4b5c6007..171d967a5 100644 --- a/src/pipecat/transports/base_output.py +++ b/src/pipecat/transports/base_output.py @@ -282,6 +282,7 @@ async def _camera_out_task_handler(self): self._camera_out_start_time = None self._camera_out_frame_index = 0 self._camera_out_frame_duration = 1 / self._params.camera_out_framerate + self._camera_out_frame_reset = self._camera_out_frame_duration * 5 while True: try: if self._params.camera_out_is_live: @@ -303,14 +304,19 @@ async def _camera_out_is_live_handler(self): # We get the start time as soon as we get the first image. if not self._camera_out_start_time: self._camera_out_start_time = time.time() + self._camera_out_frame_index = 0 # Calculate how much time we need to wait before rendering next image. - render_time = self._camera_out_start_time + \ - self._camera_out_frame_index * self._camera_out_frame_duration - time_until_render = render_time - time.time() - if time_until_render > 0: - await asyncio.sleep(time_until_render) - self._camera_out_frame_index += 1 + real_elapsed_time = time.time() - self._camera_out_start_time + real_render_time = self._camera_out_frame_index * self._camera_out_frame_duration + delay_time = self._camera_out_frame_duration + real_render_time - real_elapsed_time + + if abs(delay_time) > self._camera_out_frame_reset: + self._camera_out_start_time = time.time() + self._camera_out_frame_index = 0 + elif delay_time > 0: + await asyncio.sleep(delay_time) + self._camera_out_frame_index += 1 # Render image await self._draw_image(image)