2021-02-25 00:07:17 +01:00
|
|
|
from __future__ import annotations
|
2021-04-10 01:16:20 +02:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import sched
|
|
|
|
import time
|
2021-04-24 12:17:02 +02:00
|
|
|
import typing
|
2021-04-10 01:16:20 +02:00
|
|
|
|
2021-02-25 00:07:17 +01:00
|
|
|
from librespot.core.Session import Session
|
2021-04-10 01:16:25 +02:00
|
|
|
from librespot.player import PlayerConfiguration
|
|
|
|
from librespot.player import StateWrapper
|
2021-02-25 00:07:17 +01:00
|
|
|
from librespot.player.metrics import PlaybackMetrics
|
|
|
|
from librespot.player.mixing import AudioSink
|
|
|
|
from librespot.player.playback.PlayerSession import PlayerSession
|
|
|
|
from librespot.player.state.DeviceStateHandler import DeviceStateHandler
|
|
|
|
from librespot.standard.Closeable import Closeable
|
|
|
|
|
|
|
|
|
|
|
|
class Player(Closeable, PlayerSession.Listener, AudioSink.Listener):
|
|
|
|
VOLUME_MAX: int = 65536
|
2021-02-25 10:22:28 +01:00
|
|
|
_LOGGER: logging = logging.getLogger(__name__)
|
2021-02-25 00:07:17 +01:00
|
|
|
_scheduler: sched.scheduler = sched.scheduler(time.time)
|
2021-07-31 13:42:13 +02:00
|
|
|
_session: Session
|
|
|
|
_conf: PlayerConfiguration
|
|
|
|
_events: Player.EventsDispatcher
|
|
|
|
_sink: AudioSink
|
2021-04-24 12:17:02 +02:00
|
|
|
_metrics: typing.Dict[str, PlaybackMetrics] = {}
|
2021-07-31 13:42:13 +02:00
|
|
|
_state: StateWrapper
|
|
|
|
_playerSession: PlayerSession
|
2021-02-25 00:07:17 +01:00
|
|
|
_releaseLineFuture = None
|
2021-07-31 13:42:13 +02:00
|
|
|
_deviceStateListener: DeviceStateHandler.Listener
|
2021-02-25 00:07:17 +01:00
|
|
|
|
|
|
|
def __init__(self, conf: PlayerConfiguration, session: Session):
|
|
|
|
self._conf = conf
|
|
|
|
self._session = session
|
|
|
|
self._events = Player.EventsDispatcher(conf)
|
|
|
|
self._sink = AudioSink(conf, self)
|
|
|
|
|
2021-05-19 13:34:24 +02:00
|
|
|
self.__init_state()
|
2021-02-25 10:22:28 +01:00
|
|
|
|
2021-05-19 13:34:24 +02:00
|
|
|
def __init_state(self):
|
2021-04-10 01:16:32 +02:00
|
|
|
self._state = StateWrapper.StateWrapper(self._session, self,
|
|
|
|
self._conf)
|
2021-02-25 00:07:17 +01:00
|
|
|
|
|
|
|
class Anonymous(DeviceStateHandler.Listener):
|
2021-02-25 10:22:28 +01:00
|
|
|
_player: Player = None
|
|
|
|
|
|
|
|
def __init__(self, player: Player):
|
|
|
|
self._player = player
|
|
|
|
|
2021-02-25 00:07:17 +01:00
|
|
|
def ready(self) -> None:
|
|
|
|
pass
|
|
|
|
|
2021-04-10 01:16:18 +02:00
|
|
|
def command(
|
|
|
|
self,
|
|
|
|
endpoint: DeviceStateHandler.Endpoint,
|
|
|
|
data: DeviceStateHandler.CommandBody,
|
|
|
|
) -> None:
|
2021-04-10 01:16:32 +02:00
|
|
|
self._player._LOGGER.debug(
|
|
|
|
"Received command: {}".format(endpoint))
|
2021-02-25 00:07:17 +01:00
|
|
|
|
2021-02-25 10:22:28 +01:00
|
|
|
self._deviceStateListener = Anonymous(self)
|
|
|
|
self._state.add_listener(self._deviceStateListener)
|
2021-02-25 00:07:17 +01:00
|
|
|
|
|
|
|
def volume_up(self, steps: int = 1):
|
2021-02-25 10:22:28 +01:00
|
|
|
if self._state is None:
|
2021-02-25 00:07:17 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
class EventsDispatcher:
|
|
|
|
def __init__(self, conf: PlayerConfiguration):
|
|
|
|
pass
|