Skip to content

Commit

Permalink
Store MPV socket in $XDG_RUNTIME_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
notpeelz committed Apr 27, 2024
1 parent 665f24f commit 6c5e3a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
19 changes: 17 additions & 2 deletions syncplay/players/mpv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding:utf8
import os
import random
import re
import sys
import time
Expand All @@ -10,7 +11,7 @@
from syncplay import constants
from syncplay.messages import getMessage
from syncplay.players.basePlayer import BasePlayer
from syncplay.utils import isURL, findResourcePath
from syncplay.utils import getRuntimeDir, isURL, findResourcePath
from syncplay.utils import isMacOS, isWindows, isASCII
from syncplay.vendor.python_mpv_jsonipc.python_mpv_jsonipc import MPV

Expand Down Expand Up @@ -621,7 +622,15 @@ def __init__(self, playerController, playerIPCHandler, playerPath, filePath, arg
env['PATH'] = python_executable + ':' + env['PATH']
env['PYTHONPATH'] = pythonPath
try:
self.mpvpipe = self.playerIPCHandler(mpv_location=self.playerPath, loglevel="info", log_handler=self.__playerController.mpv_log_handler, quit_callback=self.stop_client, env=env, **self.mpv_arguments)
self.mpvpipe = self.playerIPCHandler(
loglevel="info",
ipc_socket=self._get_ipc_socket(),
mpv_location=self.playerPath,
log_handler=self.__playerController.mpv_log_handler,
quit_callback=self.stop_client,
env=env,
**self.mpv_arguments
)
except Exception as e:
self.quitReason = getMessage("media-player-error").format(str(e)) + " " + getMessage("mpv-failed-advice")
self.__playerController.reactor.callFromThread(self.__playerController._client.ui.showErrorMessage, self.quitReason, True)
Expand All @@ -630,6 +639,12 @@ def __init__(self, playerController, playerIPCHandler, playerPath, filePath, arg
#self.mpvpipe.show_text("HELLO WORLD!", 1000)
threading.Thread.__init__(self, name="MPV Listener")

def _get_ipc_socket(self):
if isWindows():
# On Windows, mpv expects a named pipe identifier (not a path)
return "syncplay-mpv-{0}".format(random.randint(0, 2**48))
return getRuntimeDir().joinpath("mpv-socket").as_posix()

def __getCwd(self, filePath, env):
if not filePath:
return None
Expand Down
23 changes: 22 additions & 1 deletion syncplay/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import ast
import atexit
import datetime
import hashlib
import itertools
Expand All @@ -10,11 +10,13 @@
import string
import subprocess
import sys
import tempfile
import time
import traceback
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path

from syncplay import constants
from syncplay.messages import getMessage
Expand All @@ -37,9 +39,28 @@ def isMacOS():
def isBSD():
return constants.OS_BSD in sys.platform or sys.platform.startswith(constants.OS_DRAGONFLY)


def isWindowsConsole():
return os.path.basename(sys.executable) == "SyncplayConsole.exe"


def getRuntimeDir():
cachedPath = getattr(getRuntimeDir, "cachedPath", None)
if cachedPath is not None:
return cachedPath

baseDir = None
if not isWindows() and not isMacOS():
baseDir = os.getenv("XDG_RUNTIME_DIR", None)

tmp = tempfile.TemporaryDirectory(prefix="syncplay-", dir=baseDir)
atexit.register(tmp.cleanup)

o = Path(tmp.name)
setattr(getRuntimeDir, "cachedPath", o)
return o


def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
"""Retry calling the decorated function using an exponential backoff.
Expand Down

0 comments on commit 6c5e3a1

Please sign in to comment.