Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/legacy_audio ocp state events #64

Merged
merged 9 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig
sudo apt install python3-dev swig sox mpg123
python -m pip install build wheel
- name: Install repo
run: |
Expand Down
59 changes: 53 additions & 6 deletions ovos_audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@
except ImportError:
OCPAudioBackend = None

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


MINUTES = 60 # Seconds in a minute


Expand Down Expand Up @@ -92,16 +125,16 @@ def load_services(self):
local = []
remote = []
for plugin_name, plugin_module in found_plugins.items():
LOG.info(f'Loading audio service plugin: {plugin_name}')
LOG.info(f'Found audio service plugin: {plugin_name}')
s = setup_audio_service(plugin_module, config=self.config, bus=self.bus)
if not s:
LOG.debug(f"{plugin_name} not loaded! config: {self.config}")
continue
if isinstance(s, RemoteAudioBackend):
remote += s
else:
local += s


# Sort services so local services are checked first
self.service = local + remote

Expand Down Expand Up @@ -163,11 +196,13 @@ def track_start(self, track):
LOG.debug('New track coming up!')
self.bus.emit(Message('mycroft.audio.playing_track',
data={'track': track}))
self.current.ocp_start()
else:
# If no track is about to start last track of the queue has been
# played.
LOG.debug('End of playlist!')
self.bus.emit(Message('mycroft.audio.queue_end'))
self.current.ocp_stop()

def _pause(self, message=None):
"""
Expand All @@ -181,6 +216,7 @@ def _pause(self, message=None):
return
if self.current:
self.current.pause()
self.current.ocp_pause()

def _resume(self, message=None):
"""
Expand All @@ -193,6 +229,7 @@ def _resume(self, message=None):
return
if self.current:
self.current.resume()
self.current.ocp_resume()

def _next(self, message=None):
"""
Expand Down Expand Up @@ -227,6 +264,7 @@ def _perform_stop(self, message=None):
if self.current:
name = self.current.name
if self.current.stop():
self.current.ocp_stop()
if message:
msg = message.reply("mycroft.stop.handled",
{"by": "audio:" + name})
Expand Down Expand Up @@ -342,13 +380,22 @@ def play(self, tracks, prefered_service, repeat=False):
break
else:
LOG.info('No service found for uri_type: ' + uri_type)
self.bus.emit(Message("ovos.common_play.media.state",
{"state": MediaState.INVALID_MEDIA}))
return
if not selected_service.supports_mime_hints:
tracks = [t[0] if isinstance(t, list) else t for t in tracks]
selected_service.clear_list()
selected_service.add_list(tracks)
selected_service.play(repeat)

self.current = selected_service
self.current.clear_list()
self.current.add_list(tracks)

try:
self.current.play(repeat)
self.current.ocp_start()
except Exception as e:
LOG.exception(f"failed to play with {self.current}")
self.current.ocp_error()
self.play_start_time = time.monotonic()

def _is_message_for_service(self, message):
Expand Down Expand Up @@ -390,7 +437,7 @@ def _play(self, message):
if ('utterance' in message.data and
s.name in message.data['utterance']):
prefered_service = s
LOG.debug(s.name + ' would be prefered')
LOG.debug(s.name + ' would be preferred')
break
except Exception as e:
LOG.error(f"failed to parse audio service name: {s}")
Expand Down
3 changes: 2 additions & 1 deletion requirements/extras.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ovos-tts-plugin-server
ovos-tts-plugin-server
ovos_audio_plugin_simple>=0.0.2a7
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, >=0.0.38
ovos-bus-client~=0.0, >=0.0.8
ovos-config~=0.0,>=0.0.13a7
ovos-plugin-manager~=0.0, >=0.0.26a16
ovos-plugin-manager~=0.0, >=0.0.26a22
6 changes: 2 additions & 4 deletions test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ flake8==3.7.9
pytest==5.2.4
pytest-cov==2.8.1
cov-core==1.15.0
sphinx==2.2.1
sphinx-rtd-theme==0.4.3
ovos-audio-plugin-simple~=0.0.1
ovos-plugin-vlc~=0.0.1
ovos_audio_plugin_simple>=0.0.2a7
ovos-utils>=0.1.0a16
Loading
Loading