Refactor play_* functions to use a common base

Adds comments for PULSE ENVIRONMENT
pull/2496/head
Åke Forslund 2020-03-28 09:47:45 +01:00
parent 60c0af69d6
commit cf40c2e36c
1 changed files with 27 additions and 21 deletions

View File

@ -56,6 +56,10 @@ def play_audio_file(uri: str, environment=None):
return None return None
# Create a custom environment to use that can be ducked by a phone role.
# This is kept separate from the normal os.environ to ensure that the TTS
# role isn't affected and that any thirdparty software launched through
# a mycroft process can select if they wish to honor this.
_ENVIRONMENT = deepcopy(os.environ) _ENVIRONMENT = deepcopy(os.environ)
_ENVIRONMENT['PULSE_PROP'] = 'media.role=music' _ENVIRONMENT['PULSE_PROP'] = 'media.role=music'
@ -69,6 +73,23 @@ def _get_pulse_environment(config):
return os.environ return os.environ
def _play_cmd(cmd, uri, config, environment):
"""Generic function for starting playback from a commandline and uri.
Arguments:
cmd (str): commandline to execute %1 in the command line will be
replaced with the provided uri.
uri (str): uri to play
config (dict): config to use
environment: environment to execute in, can be used to supply specific
pulseaudio settings.
"""
environment = environment or _get_pulse_environment(config)
cmd_elements = str(cmd).split(" ")
cmdline = [e if e != '%1' else get_http(uri) for e in cmd_elements]
return subprocess.Popen(cmdline, env=environment)
def play_wav(uri, environment=None): def play_wav(uri, environment=None):
"""Play a wav-file. """Play a wav-file.
@ -83,14 +104,9 @@ def play_wav(uri, environment=None):
Returns: subprocess.Popen object Returns: subprocess.Popen object
""" """
config = mycroft.configuration.Configuration.get() config = mycroft.configuration.Configuration.get()
environment = environment or _get_pulse_environment(config) play_wav_cmd = config['play_wav_cmdline']
play_cmd = config.get("play_wav_cmdline")
play_wav_cmd = str(play_cmd).split(" ")
for index, cmd in enumerate(play_wav_cmd):
if cmd == "%1":
play_wav_cmd[index] = (get_http(uri))
try: try:
return subprocess.Popen(play_wav_cmd, env=environment) return _play_cmd(play_wav_cmd, uri, config, environment)
except Exception as e: except Exception as e:
LOG.error("Failed to launch WAV: {}".format(play_wav_cmd)) LOG.error("Failed to launch WAV: {}".format(play_wav_cmd))
LOG.debug("Error: {}".format(repr(e)), exc_info=True) LOG.debug("Error: {}".format(repr(e)), exc_info=True)
@ -111,14 +127,9 @@ def play_mp3(uri, environment=None):
Returns: subprocess.Popen object Returns: subprocess.Popen object
""" """
config = mycroft.configuration.Configuration.get() config = mycroft.configuration.Configuration.get()
environment = environment or _get_pulse_environment(config) play_mp3_cmd = config.get("play_mp3_cmdline")
play_cmd = config.get("play_mp3_cmdline")
play_mp3_cmd = str(play_cmd).split(" ")
for index, cmd in enumerate(play_mp3_cmd):
if cmd == "%1":
play_mp3_cmd[index] = (get_http(uri))
try: try:
return subprocess.Popen(play_mp3_cmd, env=environment) return _play_cmd(play_mp3_cmd, uri, config, environment)
except Exception as e: except Exception as e:
LOG.error("Failed to launch MP3: {}".format(play_mp3_cmd)) LOG.error("Failed to launch MP3: {}".format(play_mp3_cmd))
LOG.debug("Error: {}".format(repr(e)), exc_info=True) LOG.debug("Error: {}".format(repr(e)), exc_info=True)
@ -139,14 +150,9 @@ def play_ogg(uri, environment=None):
Returns: subprocess.Popen object Returns: subprocess.Popen object
""" """
config = mycroft.configuration.Configuration.get() config = mycroft.configuration.Configuration.get()
environment = environment or _get_pulse_environment(config) play_ogg_cmd = config.get("play_ogg_cmdline")
play_cmd = config.get("play_ogg_cmdline")
play_ogg_cmd = str(play_cmd).split(" ")
for index, cmd in enumerate(play_ogg_cmd):
if cmd == "%1":
play_ogg_cmd[index] = (get_http(uri))
try: try:
return subprocess.Popen(play_ogg_cmd, env=environment) return _play_cmd(play_ogg_cmd, uri, config, environment)
except Exception as e: except Exception as e:
LOG.error("Failed to launch OGG: {}".format(play_ogg_cmd)) LOG.error("Failed to launch OGG: {}".format(play_ogg_cmd))
LOG.debug("Error: {}".format(repr(e)), exc_info=True) LOG.debug("Error: {}".format(repr(e)), exc_info=True)