Skip to content

Commit

Permalink
clear buffer if out of sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlt8 committed May 9, 2024
1 parent 0775c41 commit fe5d427
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions app/wyzecam/iotc.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def sleep_interval(self) -> float:
if self._sleep_buffer:
self._sleep_buffer = max(self._sleep_buffer - 0.05, 0)

return max((1 / self.preferred_frame_rate) - delta, 1 / 120)
return max((1 / self.preferred_frame_rate) - delta, 1 / 80)

@property
def pipe_name(self) -> str:
Expand Down Expand Up @@ -506,7 +506,7 @@ def _video_frame_slow(self, frame_info) -> Optional[bool]:

def _handle_frame_error(self, err_no: int) -> None:
"""Handle errors that occur when receiving frame data."""
time.sleep(1 / 30)
time.sleep(1 / self.preferred_frame_rate * 0.8)
if err_no == tutk.AV_ER_DATA_NOREADY or err_no >= 0:
return

Expand Down Expand Up @@ -559,7 +559,7 @@ def clear_buffer(self) -> None:
warnings.warn("clear buffer")
self.flush_pipe("audio")
self.sync_camera_time()
tutk.av_client_clean_local_buf(self.tutk_platform_lib, self.av_chan_id)
tutk.av_client_clean_buf(self.tutk_platform_lib, self.av_chan_id)

def flush_pipe(self, pipe_type: str = "audio"):
if pipe_type == "audio" and not self.audio_pipe_ready:
Expand Down Expand Up @@ -634,6 +634,12 @@ def _audio_frame_slow(self, frame_info) -> Optional[bool]:

gap = self.frame_ts - float(f"{frame_info.timestamp}.{frame_info.timestamp_ms}")

if abs(gap) > 5:
logger.debug(f"[audio] out of sync {gap=}")
self._sleep_buffer += abs(gap)
self.clear_buffer()
return

if gap <= -1:
logger.debug(f"[audio] rushing ahead of video.. {gap=}")
self.flush_pipe("audio")
Expand Down

0 comments on commit fe5d427

Please sign in to comment.