Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #49 from carlocastoldi/master
Browse files Browse the repository at this point in the history
Use sink's & source's attributes to detect any pulseaudio activity
  • Loading branch information
Hugo Barrera committed Jan 5, 2021
2 parents ac736cd + 7ae9d15 commit a3e63f3
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions caffeine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from gi.repository import GObject
from gi.repository import Notify
from pulsectl import Pulse
from pulsectl import PulseStateEnum

from . import utils
from .icons import empty_cup_icon
Expand Down Expand Up @@ -139,39 +138,49 @@ def __attempt_autoactivation(self):
# Number of all audio streams including videos. We keep the screen on
# here:
screen_relevant_procs = 0
# Applications currently playing audio.
active_applications = []

if not process_running and not fullscreen:
# Get all audio playback streams
# Music players seem to use the music role. We can turn the screen
# off there. Keep the screen on for audio without music role,
# as they might be videos
with Pulse() as pulseaudio:
for sink in pulseaudio.sink_input_list():
sink_state = pulseaudio.sink_info(sink.sink).state
for application_output in pulseaudio.sink_input_list():
if (
sink_state is PulseStateEnum.running
and sink.proplist.get("media.role") == "music"
not application_output.mute # application audio is not muted
and not application_output.corked # application audio is not paused
and not pulseaudio.sink_info(application_output.sink).mute # system audio is not muted
):
# seems to be audio only
self.music_procs += 1
elif sink_state is PulseStateEnum.running:
# Video or other audio source
screen_relevant_procs += 1
if application_output.proplist.get("media.role") == "music":
# seems to be audio only
self.music_procs += 1
else:
# Video or other audio source
screen_relevant_procs += 1
# Save the application's process name
application_name = application_output.proplist["application.process.binary"]
active_applications.append(application_name)

# Get all audio recording streams
for source in pulseaudio.source_output_list():
source_state = pulseaudio.source_info(source.source).state

if source_state is PulseStateEnum.running:
for application_input in pulseaudio.source_output_list():
if (
not application_input.mute # application input is not muted
and not pulseaudio.source_info(application_input.source).mute # system input is not muted
):
# Treat recordings as video because likely you don't
# want to turn the screen of while recording
screen_relevant_procs += 1
# Save the application's process name
application_name = application_input.proplist["application.process.binary"]
active_applications.append(application_name)

if self.music_procs > 0 or screen_relevant_procs > 0:
if self.__auto_activated:
logger.debug("Audio playback detected. No change.")
logger.debug(f"Audio playback detected ({', '.join(active_applications)}). No change.")
elif not self.get_activated():
logger.info("Audio playback detected. Inhibiting.")
logger.info(f"Audio playback detected ({', '.join(active_applications)}). Inhibiting.")

if (
process_running
Expand Down

0 comments on commit a3e63f3

Please sign in to comment.