From 6bffa9985ff3fbc67d22432b641c1a2970abcfab Mon Sep 17 00:00:00 2001 From: mrkeuz Date: Sat, 9 Sep 2023 00:24:54 +0300 Subject: [PATCH] Minor refactoring - Renames - Add comments --- vlcsync/vlc.py | 11 ++++++----- vlcsync/vlc_state.py | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/vlcsync/vlc.py b/vlcsync/vlc.py index f2e8db2..552f29a 100644 --- a/vlcsync/vlc.py +++ b/vlcsync/vlc.py @@ -31,7 +31,7 @@ def play_state(self) -> PlayState: status = self.vlc_conn.cmd("status") return self._extract_state(status) - def get_time(self) -> int | None: + def get_seek(self) -> int | None: seek = self.vlc_conn.cmd("get_time") if seek != '': return int(seek) @@ -67,12 +67,13 @@ def play(self): self.vlc_conn.cmd("play") def cur_state(self) -> State: - get_time = self.get_time() + cur_seek = self.get_seek() return State(self.play_state(), - get_time, + cur_seek, self.playlist().active_order_index(), - time.time() - (get_time or 0) + # Abs time of video start + time.time() - (cur_seek or 0) ) def is_state_change(self) -> (bool, State): @@ -218,7 +219,7 @@ def sync_all(self, state: State, source: Vlc): logger.debug(f"Detect change to {state} from {source.vlc_id}") logger.debug(f" old --> {source.prev_state} ") logger.debug(f" new --> {state} ") - logger.debug(f" Time diff abs(old - new) {abs(state.start_at_abs_time - source.prev_state.start_at_abs_time)}") + logger.debug(f" Time diff abs(old - new) {abs(source.prev_state.vid_start_at - state.vid_start_at)}") logger.debug("<" * 60) logger.debug("") print(">>> Sync players...") diff --git a/vlcsync/vlc_state.py b/vlcsync/vlc_state.py index a9ad89c..669b02f 100644 --- a/vlcsync/vlc_state.py +++ b/vlcsync/vlc_state.py @@ -2,7 +2,7 @@ from dataclasses import dataclass, field from enum import Enum -from typing import Optional, List +from typing import Optional from loguru import logger @@ -39,8 +39,16 @@ class State: play_state: PlayState seek: int playlist_order_idx: int - # Real clock time of video start - start_at_abs_time: float = field(repr=False) + """ + vid_start_at - is abs time of video start in given vlc + + If this time changed it indicates time seek + + See for details: + - play_in_same_pos() + - Vlc.cur_state() + """ + vid_start_at: float = field(repr=False) def same(self, other: State): return (self.same_play_state(other) and @@ -60,14 +68,14 @@ def pause_is_same_pos(self: State, other: State): def play_in_same_pos(self: State, other: State): """ Check time_diff only when play """ - desync_secs = abs(self.start_at_abs_time - other.start_at_abs_time) + desync_secs = abs(self.vid_start_at - other.vid_start_at) if 2 < desync_secs < MAX_DESYNC_SECONDS: logger.debug(f"Asynchronous anomaly between probes: {desync_secs} secs") return ( self.play_state == other.play_state == PlayState.PLAYING and - # self.start_at and other.start_at and + self.vid_start_at and other.vid_start_at and desync_secs < MAX_DESYNC_SECONDS )