Skip to content

Commit

Permalink
refactor!:drop OCP
Browse files Browse the repository at this point in the history
completely remove OCP integration, leave only the legacy audio system around
  • Loading branch information
JarbasAl committed Dec 30, 2024
1 parent 09c1de9 commit a26ef1b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 76 deletions.
82 changes: 17 additions & 65 deletions ovos_audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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"])
Expand All @@ -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!")
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion ovos_audio/playback.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions ovos_audio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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! '
Expand Down
1 change: 0 additions & 1 deletion requirements/extras.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a26ef1b

Please sign in to comment.