Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
- Renames
- Add comments
  • Loading branch information
mrkeuz committed Sep 8, 2023
1 parent 7483458 commit 6bffa99
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 6 additions & 5 deletions vlcsync/vlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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...")
Expand Down
18 changes: 13 additions & 5 deletions vlcsync/vlc_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
)

Expand Down

0 comments on commit 6bffa99

Please # to comment.