Skip to content

Commit

Permalink
Chang handling of player killing to be compatible with the new docker…
Browse files Browse the repository at this point in the history
… setup
  • Loading branch information
hifiberry committed Apr 8, 2024
1 parent 7114f81 commit 4ea46b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
25 changes: 15 additions & 10 deletions ac2/ostools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import glob
import subprocess
import logging
import os


def run_blocking_command(command):
Expand All @@ -19,13 +20,13 @@ def get_hw_params(file_path):
return None


def kill_players():
command = "lsof /dev/snd/pcmC*D*p | grep -v COMMAND | awk '{print $2}' | xargs kill"
def kill_player(processname):
command = "pkill " + processname
run_blocking_command(command)


def kill_kill_players():
command = "lsof /dev/snd/pcmC*D*p | grep -v COMMAND | awk '{print $2}' | xargs kill -KILL"
def kill_kill_player(processname):
command = "pkill -KILL " + processname
run_blocking_command(command)


Expand All @@ -41,8 +42,14 @@ def is_alsa_playing():
return False


def is_process_playing(processname):
command = "lsof /dev/snd/pcmC*D*p | grep -v COMMAND | awk '{print $1}'"
def active_player():
# Check if the "active-alsa-processes" script exists, use lsof otherwise
script_path = "/opt/hifiberry/bin/active-alsa-processes"
if os.path.exists(script_path):
command = f"{script_path}"
else:
command = "lsof /dev/snd/pcmC*D*p | grep -v COMMAND | awk '{print $1}'"

procs = []
try:
output = subprocess.check_output(command, shell=True, text=True)
Expand All @@ -51,8 +58,6 @@ def is_process_playing(processname):
# Handle if the command returns a non-zero exit status
logging.exception(e)

for p in procs:
if p == processname:
return True
procs = [os.path.basename(p) for p in procs]

return False
return procs[0] if procs else None
15 changes: 10 additions & 5 deletions ac2/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from ac2.metadata import Metadata
from ac2.plugins.metadata import MetadataDisplay
from ac2.socketio import sio
from ac2.ostools import is_alsa_playing, kill_kill_players, kill_players, is_process_playing
from ac2.ostools import is_alsa_playing, kill_kill_player, kill_player, active_player
from ac2.processmapper import ProcessMapper


Expand Down Expand Up @@ -210,6 +210,8 @@ def add_updater(self, updater):

def stopall_handler(self, player=None):

activeProcess = None

try:
stopped = not(is_alsa_playing())
if stopped:
Expand All @@ -218,11 +220,14 @@ def stopall_handler(self, player=None):

# Pause except for a specific player
if player is not None and player != "":

processname = self.process_mapper.get_process_name(player, player)
if is_process_playing(processname):
logging.error(processname)
activeProcess = active_player()
if activeProcess == processname:
logging.info("Player %s is playing, not stopping it", processname)
return "ok"
else:
logging.info("active player: %s", activeProcess)

sleeptimes = [0, 1, 1, 1, 1, 1, 1, 1, 1, 1];

Expand All @@ -238,15 +243,15 @@ def stopall_handler(self, player=None):
logging.info("Player still running after sending pause command")

# now try a kill
kill_players()
kill_player(activeProcess)
for t in sleeptimes:
time.sleep(t)
if not(is_alsa_playing()):
return "ok"

logging.info("Player still running after sending kill")
# now do it the hard way using kill -KILL
kill_kill_players()
kill_kill_players(activeProcess)
for t in sleeptimes:
time.sleep(t)
if not(is_alsa_playing()):
Expand Down

0 comments on commit 4ea46b0

Please sign in to comment.