From a26ef1bfc4ba152b24d6179cf46e5d4c21799799 Mon Sep 17 00:00:00 2001 From: miro Date: Mon, 30 Dec 2024 17:48:59 +0000 Subject: [PATCH] refactor!:drop OCP completely remove OCP integration, leave only the legacy audio system around --- ovos_audio/audio.py | 82 ++++++++--------------------------- ovos_audio/playback.py | 13 +++++- ovos_audio/service.py | 11 ++--- requirements/extras.txt | 1 - requirements/requirements.txt | 2 +- 5 files changed, 33 insertions(+), 76 deletions(-) diff --git a/ovos_audio/audio.py b/ovos_audio/audio.py index dd659fe..f0dacfa 100644 --- a/ovos_audio/audio.py +++ b/ovos_audio/audio.py @@ -14,7 +14,6 @@ from threading import Lock from typing import List, Tuple, Union, Optional -from ovos_audio.utils import require_native_source from ovos_bus_client.message import Message from ovos_bus_client.message import dig_for_message from ovos_config.config import Configuration @@ -23,39 +22,10 @@ from ovos_plugin_manager.ocp import load_stream_extractors from ovos_plugin_manager.templates.audio import RemoteAudioBackend from ovos_utils.log import LOG +from ovos_utils.ocp import MediaState from ovos_utils.process_utils import MonotonicEvent -try: - from ovos_utils.ocp import MediaState -except ImportError: - LOG.warning("Please update to ovos-utils~=0.1.") - from enum import IntEnum - - - class MediaState(IntEnum): - # https://doc.qt.io/qt-5/qmediaplayer.html#MediaStatus-enum - # The status of the media cannot be determined. - UNKNOWN = 0 - # There is no current media. PlayerState == STOPPED - NO_MEDIA = 1 - # The current media is being loaded. The player may be in any state. - LOADING_MEDIA = 2 - # The current media has been loaded. PlayerState== STOPPED - LOADED_MEDIA = 3 - # Playback of the current media has stalled due to - # insufficient buffering or some other temporary interruption. - # PlayerState != STOPPED - STALLED_MEDIA = 4 - # The player is buffering data but has enough data buffered - # for playback to continue for the immediate future. - # PlayerState != STOPPED - BUFFERING_MEDIA = 5 - # The player has fully buffered the current media. PlayerState != STOPPED - BUFFERED_MEDIA = 6 - # Playback has reached the end of the current media. PlayerState == STOPPED - END_OF_MEDIA = 7 - # The current media cannot be played. PlayerState == STOPPED - INVALID_MEDIA = 8 +from ovos_audio.utils import require_native_source MINUTES = 60 # Seconds in a minute @@ -66,8 +36,7 @@ class AudioService: to be played. """ - def __init__(self, bus, autoload=True, disable_ocp=False, - validate_source=True, native_sources=None): + def __init__(self, bus, autoload=True, validate_source=True, native_sources=None): """ Args: bus: Mycroft messagebus @@ -77,13 +46,13 @@ def __init__(self, bus, autoload=True, disable_ocp=False, self.service_lock = Lock() self.default = None - self.ocp = None + self.service = [] self.current = None self.play_start_time = 0 self.volume_is_low = False self.volume_is_speaking = False - self.disable_ocp = disable_ocp + self.validate_source = validate_source self.native_sources = native_sources or self.config.get("native_sources", ["debug_cli", "audio"]) @@ -92,27 +61,6 @@ def __init__(self, bus, autoload=True, disable_ocp=False, if autoload: self.load_services() - def find_ocp(self): - if self.disable_ocp: - LOG.info("classic OCP is disabled in config, OCP bus api not available!") - # NOTE: ovos-core should detect this and use the classic audio service api automatically - return - - try: - from ovos_plugin_common_play import OCPAudioBackend - except ImportError: - LOG.debug("classic OCP not installed") - return False - # config from legacy location in default mycroft.conf - ocp_config = Configuration().get("Audio", {}).get("backends", {}).get("OCP", {}) - self.ocp = OCPAudioBackend(ocp_config, bus=self.bus) - try: - self.ocp.player.validate_source = self.validate_source - self.ocp.player.native_sources = self.native_sources - except Exception as e: - # handle older OCP plugin versions - LOG.warning("old OCP version detected! please update 'ovos_plugin_common_play'") - def find_default(self): if not self.service: LOG.error("No audio player plugins found!") @@ -159,11 +107,6 @@ def load_services(self): for s in self.service: s.set_track_start_callback(self.track_start) - # load OCP - # NOTE: this will be replace by ovos-media in a future release - # and can be disabled in config - self.find_ocp() - # load audio playback plugins (vlc, mpv, spotify ...) self.find_default() @@ -276,7 +219,12 @@ def _perform_stop(self, message=None): """Stop audioservice if active.""" if self.current: name = self.current.name - if self.current.stop(): + try: + stopped = self.current.stop() + except Exception as e: + LOG.error(f"Failed to stop {name}: {e}") + stopped = False + if stopped: self.current.ocp_stop() if message: msg = message.reply("mycroft.stop.handled", @@ -287,7 +235,11 @@ def _perform_stop(self, message=None): self.bus.emit(msg) # ensure we don't leave the volume ducked - self.current.restore_volume() + try: + self.current.restore_volume() + except Exception as e: + LOG.error(f"Failed to restore_volume {name}: {e}") + self.volume_is_low = False self.current = None @@ -398,7 +350,7 @@ def _extract(self, tracks: Union[List[str], List[Tuple[str, str]]]) -> List[str] return xtracted def play(self, tracks: Union[List[str], List[Tuple[str, str]]], - prefered_service: Optional[str], repeat: bool =False): + prefered_service: Optional[str], repeat: bool = False): """ play starts playing the audio on the prefered service if it supports the uri. If not the next best backend is found. diff --git a/ovos_audio/playback.py b/ovos_audio/playback.py index ab0e07c..56284c6 100644 --- a/ovos_audio/playback.py +++ b/ovos_audio/playback.py @@ -1,7 +1,7 @@ import random from queue import Empty from threading import Thread, Event -from typing import Optional +from typing import Optional, List from ovos_audio.transformers import TTSTransformersService from ovos_bus_client.message import Message @@ -120,6 +120,17 @@ def on_end(self, listen=False, message=None): LOG.warning("failed to curate TTS cache. please update ovos-plugin-manager") self.blink(0.2) + def put(self, wav: str, + visemes: Optional[List[str]]=None, + listen:bool = False, + tts_id: Optional[str] = None, + message: Optional[Message] = None): + message = message or Message("") + # queue audio for playback + self.queue.put( + (wav, visemes, listen, tts_id, message) + ) + def _play(self): try: data, visemes, listen, tts_id, message = self._now_playing diff --git a/ovos_audio/service.py b/ovos_audio/service.py index c94878b..18e5f81 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -54,7 +54,7 @@ class PlaybackService(Thread): def __init__(self, ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping, alive_hook=on_alive, started_hook=on_started, watchdog=lambda: None, - bus=None, disable_ocp=None, validate_source=True, + bus=None, validate_source=True, tts: Optional[TTS] = None, disable_fallback: bool = False): super(PlaybackService, self).__init__() @@ -103,13 +103,11 @@ def __init__(self, ready_hook=on_ready, error_hook=on_error, self.audio = None self.audio_enabled = self.config.get("enable_old_audioservice", True) # TODO default to False soon - if disable_ocp is None: - disable_ocp = self.config.get("disable_ocp", False) # TODO default to True soon - self.disable_ocp = disable_ocp + LOG.debug(f"legacy audio service enabled: {self.audio_enabled}") if self.audio_enabled: try: - self.audio = AudioService(self.bus, disable_ocp=disable_ocp, + self.audio = AudioService(self.bus, validate_source=validate_source, native_sources=self.native_sources) except Exception as e: @@ -256,9 +254,6 @@ def run(self): self.status.set_alive() if self.audio_enabled: LOG.info("Legacy AudioService enabled") - if not self.disable_ocp: - LOG.warning("OCP has moved to ovos-media, if you already migrated to ovos-media " - 'set "disable_ocp": true in mycroft.conf') if self.audio.wait_for_load(): if len(self.audio.service) == 0: LOG.warning('No audio backends loaded! ' diff --git a/requirements/extras.txt b/requirements/extras.txt index ca9dabc..e8ffbe7 100644 --- a/requirements/extras.txt +++ b/requirements/extras.txt @@ -6,4 +6,3 @@ ovos_audio_plugin_simple>=0.1.0, <1.0.0 ovos-audio-plugin-mpv>=0.2.0, <1.0.0 ovos-media-plugin-spotify>=0.2.3, <1.0.0 ovos-media-plugin-chromecast>=0.1.0, <1.0.0 -ovos_plugin_common_play[extractors]>=1.1.4, <2.0.0 diff --git a/requirements/requirements.txt b/requirements/requirements.txt index da29854..e2a545d 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,4 +1,4 @@ -ovos-utils>=0.0.38,<1.0.0 +ovos-utils>=0.1.0,<1.0.0 ovos_bus_client>=0.0.8,<2.0.0 ovos-config>=0.0.12,<2.0.0 ovos-plugin-manager>=0.0.26,<1.0.0 \ No newline at end of file