Repository URL to install this package:
|
Version:
0.1-r4 ▾
|
enigma2-plugin-extensions-audioswitchonservicechange
/
usr
/
lib
/
enigma2
/
python
/
Plugins
/
Extensions
/
AudioSwitchOnServiceChange
/
plugin.py
|
|---|
from Plugins.Plugin import PluginDescriptor
from enigma import eTimer, eDVBVolumecontrol
from Screens.Screen import Screen
from Components.ConfigList import ConfigListScreen, ConfigList
from Components.config import config, ConfigSubsection, ConfigYesNo, ConfigSelection, ConfigSelectionNumber, getConfigListEntry
from Components.Sources.StaticText import StaticText
from Components.ActionMap import ActionMap
#from Components.HdmiCec import hdmi_cec
#== language-support ====================================
from Components.Language import language
from os import environ as os_environ
import gettext
from Tools.Directories import resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
lang = language.getLanguage()
os_environ["LANGUAGE"] = lang[:2]
gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))
gettext.textdomain("enigma2")
gettext.bindtextdomain("enigma2-plugins", resolveFilename(SCOPE_LANGUAGE))
gettext.bindtextdomain("AudioSwitchOnServiceChange", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/AudioSwitchOnServiceChange/locale"))
try:
from enigma import eDisplayManager
has_eDisplayManager = True
except:
has_eDisplayManager = False
def _(txt):
t = gettext.dgettext("AudioSwitchOnServiceChange", txt)
if t == txt:
t = gettext.gettext(txt)
if t == txt:
t = gettext.dgettext("enigma2-plugins", txt)
return t
#== end language support ================================
config.plugins.AudioSwitchOnServiceChange = ConfigSubsection()
config.plugins.AudioSwitchOnServiceChange.enable = ConfigYesNo(default=True)
config.plugins.AudioSwitchOnServiceChange.event = ConfigSelection(choices=[("servicechange",_("on every service change")), ("boxstart",_("only on box start/start from idle"))], default = "servicechange")
config.plugins.AudioSwitchOnServiceChange.toggleMute = ConfigYesNo(default=True)
config.plugins.AudioSwitchOnServiceChange.toggleAC3downmix = ConfigYesNo(default=True)
config.plugins.AudioSwitchOnServiceChange.onHdmiChanged = ConfigYesNo(default=False)
config.plugins.AudioSwitchOnServiceChange.waitTime = ConfigSelectionNumber(100, 7000, 100, default = 100)
plugin_version = "0.1-r4"
def autostart(reason, **kwargs):
if kwargs.has_key("session") and reason == 0:
session = kwargs["session"]
session.open(EventScreen)
def main(session, **kwargs):
session.open(AudioSwitchOnServiceChangeSetup)
class EventScreen(Screen):
def __init__(self, session):
Screen.__init__(self, session)
print("[AudioSwitchOnServiceChange] open screen")
self.unmuteTimer = eTimer()
self.unmuteTimer_conn = self.unmuteTimer.timeout.connect(self.setUnmute)
from Components.ServiceEventTracker import ServiceEventTracker
from enigma import iPlayableService
self.__event_tracker = ServiceEventTracker(screen = self, eventmap = {
#iPlayableService.evEOF : self.eventStop,
#iPlayableService.evStart : self.eventStart,
#iPlayableService.evEnd : self.eventEnd,
iPlayableService.evTunedIn : self.eventTunedin,
iPlayableService.evTuneFailed : self.eventTuneFailed,
iPlayableService.evServiceChanged : self.evServiceChanged,
})
self.wasMuted = 0
self.muteOnTuning = False
self.actionOnServiceChanged = True
config.misc.standbyCounter.addNotifier(self.onIdle, initial_call=False)
if has_eDisplayManager:
self._displayManager = eDisplayManager.getInstance()
self._hdmiChangedSigConn = self._displayManager.hdmiChanged.connect(self.onHdmiChanged)
def onHdmiChanged(self):
with open("/sys/devices/virtual/amhdmitx/amhdmitx0/hpd_state", "r") as f:
hpd_state = f.read()
if hpd_state == "0":
print("[AudioSwitchOnServiceChange] onHdmiChanged - no action because no active hdmi")
return
if config.plugins.AudioSwitchOnServiceChange.onHdmiChanged.value and config.plugins.AudioSwitchOnServiceChange.event.value == "boxstart":
from Screens.Standby import inStandby
if inStandby is None:
# videoPort = config.av.videoport.value
# videoMode = config.av.videomode[videoPort].value
# print("[AudioSwitchOnServiceChange] onHdmiChanged - video port: %s, mode: %s" %(videoPort, videoMode))
# with open("/sys/devices/virtual/amhdmitx/amhdmitx0/hdmi_audio/state") as f:
# audio_state = f.read()
# print("[AudioSwitchOnServiceChange] onHdmiChanged - hdmi_audio state: %s" % audio_state)
self.actionOnServiceChanged = False
print("[AudioSwitchOnServiceChange] onHdmiChanged - set ac3-downmix")
self.setAC3downmix(True)
print("[AudioSwitchOnServiceChange] onHdmiChanged - start timer to unmute/toggle ac3-downmix (%s ms)" % config.plugins.AudioSwitchOnServiceChange.waitTime.value)
self.unmuteTimer.start(int(config.plugins.AudioSwitchOnServiceChange.waitTime.value),True)
else:
print("[AudioSwitchOnServiceChange] onHdmiChanged - no action on idle mode")
else:
print("[AudioSwitchOnServiceChange] onHdmiChanged - no action because hdmiChanged option not activated")
def onIdle(self, configElement):
print("[AudioSwitchOnServiceChange] reset actionOnServiceChanged for next start from idle")
self.actionOnServiceChanged = True
#hdmi_cec.otpDisable()
#from Screens.Standby import inStandby
#inStandby.onClose.append(self.onLeaveIdle)
#def onLeaveIdle(self):
# hdmi_cec.otpEnable()
# def eventStop(self):
# print("=== eventStop")
# def eventStart(self):
# print("=== eventStart")
# def eventEnd(self):
# print("=== eventEnd")
def eventTunedin(self):
if self.actionOnServiceChanged:
print("[AudioSwitchOnServiceChange] onTunedin - action on service change")
self.leaveMute()
else:
print("[AudioSwitchOnServiceChange] onTunedin - no action - only on boxstart or start from idle")
def eventTuneFailed(self):
print("[AudioSwitchOnServiceChange] onTuneFailed")
self.leaveMute()
def evServiceChanged(self):
if config.plugins.AudioSwitchOnServiceChange.enable.value and self.actionOnServiceChanged:
if self.muteOnTuning:
self.unmuteTimer.stop()
print("[AudioSwitchOnServiceChange] onServiceChanged - don't set mute because servicechange before unmute")
else:
self.wasMuted = 0
if config.plugins.AudioSwitchOnServiceChange.toggleMute.value:
print("[AudioSwitchOnServiceChange] onServiceChanged - set mute")
self.setMute()
if config.plugins.AudioSwitchOnServiceChange.toggleAC3downmix.value:
print("[AudioSwitchOnServiceChange] onServiceChanged - set ac3-downmix")
self.setAC3downmix(True)
self.muteOnTuning = True
def setMute(self):
if (eDVBVolumecontrol.getInstance().isMuted()):
self.wasMuted = 1
print("[AudioSwitchOnServiceChange] mute already active")
else:
self.wasMuted = 0
eDVBVolumecontrol.getInstance().volumeToggleMute()
print("[AudioSwitchOnServiceChange] setMute")
def setAC3downmix(self, value=False):
config.av.downmix_ac3.value = value
config.av.downmix_ac3.save()
print("[AudioSwitchOnServiceChange] set ac3 downmix: %s" % value)
def leaveMute(self):
if config.plugins.AudioSwitchOnServiceChange.enable.value and self.actionOnServiceChanged:
if config.plugins.AudioSwitchOnServiceChange.event.value == "boxstart":
self.actionOnServiceChanged = False
self.unmuteTimer.start(int(config.plugins.AudioSwitchOnServiceChange.waitTime.value), True)
print("[AudioSwitchOnServiceChange] start timer to unmute/toggle ac3-downmix (%s ms)" % config.plugins.AudioSwitchOnServiceChange.waitTime.value)
def setUnmute(self):
self.muteOnTuning = False
if config.plugins.AudioSwitchOnServiceChange.toggleMute.value and self.wasMuted == 0:
if (eDVBVolumecontrol.getInstance().isMuted()):
eDVBVolumecontrol.getInstance().volumeToggleMute()
print("[AudioSwitchOnServiceChange] setUnmute")
if config.plugins.AudioSwitchOnServiceChange.toggleAC3downmix.value:
# if config.plugins.AudioSwitchOnServiceChange.event.value == "boxstart":
# # some tests to reset volume on wakeup from idle - sometimes no audio
# from enigma import eAudioManager
# eAudio = eAudioManager.getInstance()
# print("[AudioSwitchOnServiceChange] eAudioManager load modes")
# eAudio.load()
# ac3plusmodes = eAudio.getAvailableModes(eAudioManager.OUTPUT_HDMI, eAudioManager.FMT_AC3_PLUS)
# print("[AudioSwitchOnServiceChange] eAudioManager modes", len(ac3plusmodes))
# for ac3mode in ac3plusmodes:
# print("[AudioSwitchOnServiceChange] eAudioManager mode", ac3mode)
# if not eAudio.hasMode(eAudioManager.OUTPUT_HDMI,eAudioManager.MODE_AUTO):
# print("[AudioSwitchOnServiceChange] eAudioManager has no auto mode")
# if config.plugins.AudioSwitchOnServiceChange.event.value == "boxstart":
# from enigma import eCec
# from Plugins.SystemPlugins.CEC.Cec import cec
# cec.reportPhysicalAddress()
# cec.givePhysicalAddress(eCec.ADDR_AUDIO_SYSTEM)
# cec.giveSystemAudioModeStatus()
# from enigma import eAlsaOutput
# self.audioHDMI = eAlsaOutput.getInstance(eAlsaOutput.HDMI)
# print("[AudioSwitchOnServiceChange] eAlsaOutput.HDMI stop")
# self.audioHDMI.stop()
# #self.audioHDMI.close()
# if config.plugins.AudioSwitchOnServiceChange.event.value == "boxstart":
# #prevent muting volume after start from idle
# from enigma import eAudioManager
# eAudio = eAudioManager.getInstance()
# eAudio.setMode(eAudioManager.OUTPUT_HDMI, eAudioManager.MODE_DOWNMIX)
self.setAC3downmix(False)
#print("[AudioSwitchOnServiceChange] isMuted()=%s" % eDVBVolumecontrol.getInstance().isMuted())
class AudioSwitchOnServiceChangeSetup(Screen, ConfigListScreen):
def __init__(self, session):
Screen.__init__(self, session)
self.skinName = "Setup"
self.setTitle(_("AudioSwitchOnServiceChange Setup - v%s") % plugin_version)
# Initialize widgets
self["key_red"] = StaticText(_("Close"))
self["key_green"] = StaticText(_("Save"))
# Define Actions
self["actions"] = ActionMap(["SetupActions", "ColorActions"],
{
"cancel": self.keyCancel,
"green": self.keySave,
},-2)
self.list = []
ConfigListScreen.__init__(self, self.list, session = self.session, on_change = self.changedEntry)
self.createSetup()
def changedEntry(self):
cur = self["config"].getCurrent()
if cur and cur[1] in (config.plugins.AudioSwitchOnServiceChange.enable, config.plugins.AudioSwitchOnServiceChange.event):
self.createSetup()
def createSetup(self):
self.list = []
self.list.append(getConfigListEntry(_("enable AudioSwitchOnServiceChange"), config.plugins.AudioSwitchOnServiceChange.enable))
if config.plugins.AudioSwitchOnServiceChange.enable.value:
self.list.append(getConfigListEntry(_("set audio switch action"), config.plugins.AudioSwitchOnServiceChange.event))
if has_eDisplayManager and config.plugins.AudioSwitchOnServiceChange.event.value == "boxstart":
self.list.append(getConfigListEntry(_("audio switch action on hdmi change"), config.plugins.AudioSwitchOnServiceChange.onHdmiChanged))
self.list.append(getConfigListEntry(_("audio switch actions"), ))
self.list.append(getConfigListEntry(_("mute on service change"), config.plugins.AudioSwitchOnServiceChange.toggleMute))
self.list.append(getConfigListEntry(_("toggle AC3 downmix on service change"), config.plugins.AudioSwitchOnServiceChange.toggleAC3downmix))
self.list.append(getConfigListEntry(_("waiting time to unmute/toggle AC3 downmix after tunedin (in ms)"), config.plugins.AudioSwitchOnServiceChange.waitTime))
self["config"].setList(self.list)
def Plugins(**kwargs):
descriptors = []
plugin_name = _("AudioSwitch on ServiceChange")
desc = _("%s Setup (v%s)") % (plugin_name, plugin_version)
descriptors.append( PluginDescriptor(name =_(plugin_name), description=_(desc), where= PluginDescriptor.WHERE_PLUGINMENU, fnc = main, needsRestart = False, icon="plugin.png" ))
descriptors.append( PluginDescriptor(name=_(plugin_name), description=_(plugin_name), where = PluginDescriptor.WHERE_SESSIONSTART, fnc = autostart))
return descriptors