优化帧等待,帧追赶算法,较稳定
This commit is contained in:
@@ -129,34 +129,33 @@ class HLSFrameProcessor(threading.Thread):
|
||||
current_time = time.time()
|
||||
|
||||
# 计算时间差
|
||||
time_diff = current_time - expected_play_time
|
||||
wait_time = expected_play_time - current_time
|
||||
|
||||
# 打印距离上次处理过去的时间
|
||||
current_time = time.time()
|
||||
if self.last_process_time is not None:
|
||||
time_since_last = (current_time - self.last_process_time) * 1000 # 转换为毫秒
|
||||
self.pts_diff = frame_pts - self.last_frame_pts # 与上一帧的PTS差
|
||||
# logger.info(f"[INFO] Time since last frame: {time_since_last:.1f}ms, pts: {frame_pts} ms, pts_diff: {self.pts_diff}ms")
|
||||
self.last_process_time = current_time
|
||||
self.last_frame_pts = frame_pts
|
||||
|
||||
# 时间同步策略
|
||||
if time_diff > 0.06: # 超过60ms,播放落后 (3帧)
|
||||
# 跳过这一帧追赶
|
||||
logger.info(f"[DEBUG] Lagging behind by {time_diff*1000:.1f}ms, skipping frame")
|
||||
return False
|
||||
elif time_diff < -0.06: # 超前60ms,需要等待 (3帧)
|
||||
# 等待到正确的时间点
|
||||
wait_time = -time_diff
|
||||
if wait_time > 5: # 如果等待时间过长,重置时间基准
|
||||
# # 打印距离上次处理过去的时间
|
||||
# current_time = time.time()
|
||||
# if self.last_process_time is not None:
|
||||
# time_since_last = (current_time - self.last_process_time) * 1000 # 转换为毫秒
|
||||
# self.pts_diff = frame_pts - self.last_frame_pts # 与上一帧的PTS差
|
||||
# # logger.info(f"[INFO] Time since last frame: {time_since_last:.1f}ms, pts: {frame_pts} ms, pts_diff: {self.pts_diff}ms")
|
||||
# self.last_process_time = current_time
|
||||
# self.last_frame_pts = frame_pts
|
||||
|
||||
if wait_time > 0:
|
||||
if wait_time > 1: # 如果等待时间过长,重置时间基准
|
||||
logger.info(f"[WARN] Too far ahead, resetting time base")
|
||||
self.start_time = current_time
|
||||
self.base_pts = frame_pts
|
||||
else:
|
||||
time.sleep(wait_time)
|
||||
logger.info(f"[DEBUG] Waiting for {wait_time*1000:.1f}ms")
|
||||
|
||||
# 时间同步良好,放入raw_queue
|
||||
# logger.info(f"[DEBUG] Frame Waiting for {wait_time*1000:.1f}ms")
|
||||
else :
|
||||
if wait_time < -1:
|
||||
logger.info(f"[WARN] Too far behind, resetting time base")
|
||||
self.start_time = current_time
|
||||
self.base_pts = frame_pts
|
||||
# else:
|
||||
# logger.info(f"[WARN] frame ahead, no waiting")
|
||||
|
||||
item = {
|
||||
"camera_id": self.camera_cfg.id,
|
||||
"camera_name": self.camera_cfg.name,
|
||||
@@ -174,8 +173,6 @@ class HLSFrameProcessor(threading.Thread):
|
||||
pass
|
||||
|
||||
self.raw_queue.put(item, timeout=0.5)
|
||||
|
||||
time.sleep(0.02)
|
||||
# if self.pts_diff > 0:
|
||||
# time.sleep(self.pts_diff/1000)
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user