play-daemon-threaded/pipeline/handlers/audio.py
2024-03-27 10:40:31 +01:00

52 lines
1.8 KiB
Python

from .handler import Handler
from ..exceptions import ValidationException
@Handler.register
class AudioHandler(Handler):
"""
This class handles audio settings for streams.
"""
@classmethod
def wants(cls, jobspec, existing_package):
"""
Return True if this handler wants to process this jobspec.
Raises an exception if the job is wanted but doesn't pass validation.
In order for a job to be wanted, the field 'sources' must exist
and at least one of the source items in 'sources' must contain a
'playAudio' field.
"""
if 'sources' in jobspec and any('playAudio' in source
for source
in jobspec['sources'].values()):
return cls._validate(jobspec, existing_package)
return False
@classmethod
def _validate(cls, jobspec, existing_package):
"""
Return True if the job is valid for this handler.
A job is valid if exactly one of the items in this job's 'sources'
object has its 'playAudio' field set to True.
"""
super()._validate(jobspec, existing_package)
true_count = 0
for name, source in jobspec['sources'].items():
if source.get('playAudio', False) == True:
true_count += 1
if true_count > 1:
raise ValidationException(
"more than one stream set to play audio")
return True
def _handle(self, jobspec, existing_package, tempdir):
def apply_func(package):
sources = package['sources']
for name, source in jobspec['sources'].items():
if name not in sources:
sources[name] = {}
sources[name]['playAudio'] = source.get('playAudio', False)
return apply_func