Skip to content

Commit

Permalink
Merge pull request #191 from dmunozv04/add-mutex-command
Browse files Browse the repository at this point in the history
Implements mutex when sending commands to YouTube
  • Loading branch information
dmunozv04 authored Sep 18, 2024
2 parents c56cbfe + 8cc3f8a commit e999a93
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/iSponsorBlockTV/ytlounge.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
self.mute_ads = config.mute_ads
self.skip_ads = config.skip_ads
self.auto_play = config.auto_play
self._command_mutex = asyncio.Lock()

# Ensures that we still are subscribed to the lounge
async def _watchdog(self):
Expand Down Expand Up @@ -145,9 +146,12 @@ def _process_event(self, event_id: int, event_type: str, args):
self.shorts_disconnected = False
create_task(self.play_video(video_id_saved))
elif event_type == "loungeScreenDisconnected":
data = args[0]
if data["reason"] == "disconnectedByUserScreenInitiated": # Short playing?
self.shorts_disconnected = True
if args: # Sometimes it's empty
data = args[0]
if (
data["reason"] == "disconnectedByUserScreenInitiated"
): # Short playing?
self.shorts_disconnected = True
elif event_type == "onAutoplayModeChanged":
create_task(self.set_auto_play_mode(self.auto_play))

Expand Down Expand Up @@ -181,3 +185,9 @@ async def set_auto_play_mode(self, enabled: bool):

async def play_video(self, video_id: str) -> bool:
return await self._command("setPlaylist", {"videoId": video_id})

# Test to wrap the command function in a mutex to avoid race conditions with
# the _command_offset (TODO: move to upstream if it works)
async def _command(self, command: str, command_parameters: dict = None) -> bool:
async with self._command_mutex:
return await super()._command(command, command_parameters)

0 comments on commit e999a93

Please sign in to comment.