TransCordingSetup : add new plugin.
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / TransCordingSetup / plugin.py
1 from Screens.Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection
4 from Components.ActionMap import ActionMap
5 from Screens.MessageBox import MessageBox
6 from Components.Sources.StaticText import StaticText
7 from Plugins.Plugin import PluginDescriptor
8 from Tools.Directories import fileExists
9 from enigma import eTimer
10 from os import system as os_system
11 from __init__ import _
12
13 config.plugins.transcordingsetup = ConfigSubsection()
14 config.plugins.transcordingsetup.transcording = ConfigSelection(default = "disabled", choices = [ ("enabled", _("enabled")), ("disabled", _("disabled"))] )
15 config.plugins.transcordingsetup.port = ConfigSelection(default = "8002", choices = [ ("8001", "8001"), ("8002", "8002")] )
16
17 error_msg ={
18         -1 : "File not exist - /proc/stb/encoder/enable.",
19         -2 : "File not exist - /etc/inetd.conf.",
20         -3 : "File open error - /proc/stb/encoder/enable.",
21         -4 : "File open error - /etc/inetd.conf.",
22         -5 : "Set encoder error.",
23         -6 : "Set port error.",
24         -7 : "Setting value is incorrect."
25 }
26 class TranscordingSetupInit:
27         def __init__(self):
28                 self.transcording_value = config.plugins.transcordingsetup.transcording.value
29                 if self.transcording_value == "disabled":
30                         self.port_value = "8002"
31                 else:
32                         self.port_value = config.plugins.transcordingsetup.port.value
33                 self.transcording_old = config.plugins.transcordingsetup.transcording.value
34                 ret = self.setTranscording(self.transcording_value, self.port_value)
35                 if ret is not None and ret < 0:
36                         print "[TranscordingSetup] setup failed!(%s, %s)"%(self.transcording_value, self.port_value)
37
38         def setTranscording(self, transcording, port):
39                 if not self.getModel():
40                         print "This plugin is only supported for solo2/duo2."
41                         return -8
42                 if transcording not in ["enabled","disabled"] or port not in ["8001","8002"]:
43                         print "Input arg error.."
44                         return -7
45                 if not fileExists("/proc/stb/encoder/enable"):
46                         return -1
47                 elif not fileExists("/etc/inetd.conf"):
48                         return -2
49                 if self.setEncoder(transcording) < 0:
50                         return -5
51                 res = self.setPort(port)
52                 if res < 0:
53                         self.setEncoder(self.transcording_old)
54                         return res
55                 else:
56                         self.inetdRestart()
57                 return res
58
59         def setEncoder(self,mode = "disabled"):
60                 print "<TranscordingSetup> set encoder : %s" % mode
61                 mode = mode.strip(' ').strip('\n')
62                 try:
63                         fd = open("/proc/stb/encoder/enable",'r')
64                         self.transcording_old = fd.read()
65                         fd.close()
66                         fd = open("/proc/stb/encoder/enable",'w')
67                         fd.write(mode)
68                         fd.close()
69                         print "-->",mode
70                         fd = open("/proc/stb/encoder/enable",'r')
71                         encoder_enable = fd.read().strip(' ').strip('\n')
72                         fd.close()
73                         print "<--",encoder_enable
74                         if encoder_enable == mode:
75                                 return 0
76                         else:
77                                 print "can not setting.."
78                                 return -1
79                 except:
80                         print "setEncoder exception error"
81                         return -1
82
83         def setPort(self, port = "8001"):
84                 print "<TranscordingSetup> set port : %s" % port
85                 try:
86                         fp = file('/etc/inetd.conf', 'r')
87                         datas = fp.readlines()
88                         fp.close()
89                 except:
90                         print "file open error, inetd.conf!"
91                         return -4
92                 try:
93                         newdatas=""
94                         s_port = ""
95                         if port == "8001":
96                                 s_port = "8002"
97                         else:
98                                 s_port = "8001"
99                         for line in datas:
100                                 if line.find("transtreamproxy") != -1:
101                                         p=line.replace('\t',' ').find(' ')
102                                         line = port+line[p:]
103                                 elif line.find("streamproxy") != -1:
104                                         p=line.replace('\t',' ').find(' ')
105                                         line = s_port+line[p:]
106                                 newdatas+=line
107
108                         if newdatas.find("transtreamproxy") == -1:
109                                 newdatas+=port+'\t'+'stream'+'\t'+'tcp'+'\t'+'nowait'+'\t'+'root'+'\t'+'/usr/bin/transtreamproxy'+'\t'+'transtreamproxy\n'
110                         fd = file("/etc/inetd.conf",'w')
111                         fd.write(newdatas)
112                         fd.close()
113                 except:
114                         return -6
115                 return 0
116
117         def inetdRestart(self):
118                 if fileExists("/etc/init.d/inetd"):
119                         os_system("/etc/init.d/inetd restart")
120
121         def getModel(self):
122                 if fileExists("/proc/stb/info/vumodel"):
123                         vumodel = open("/proc/stb/info/vumodel")
124                         info=vumodel.read().strip()
125                         vumodel.close()
126                         if info in ["solo2", "duo2"]:
127                                 return True
128                         else:
129                                 return False
130                 else:
131                         return False
132
133 class TranscordingSetup(Screen,ConfigListScreen, TranscordingSetupInit):
134         skin =  """
135                 <screen position="center,center" size="560,270" title="Transcording Setup" >
136                         <ePixmap pixmap="skin_default/buttons/red.png" position="110,10" size="140,40" alphatest="on" />
137                         <ePixmap pixmap="skin_default/buttons/green.png" position="310,10" size="140,40" alphatest="on" />
138                         <widget source="key_red" render="Label" position="110,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
139                         <widget source="key_green" render="Label" position="310,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
140                         <widget name="config" zPosition="2" position="5,70" size="550,70" scrollbarMode="showOnDemand" transparent="1" />
141                         <widget source="text" render="Label" position="20,140" size="520,130" font="Regular;18" halign="center" valign="center" />
142                 </screen>
143                 """
144
145         def __init__(self,session):
146                 Screen.__init__(self,session)
147                 self.session = session
148                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
149                 {
150                         "ok": self.keySave,
151                         "cancel": self.keyCancel,
152                         "red": self.keyCancel,
153                         "green": self.keySave,
154                 }, -2)
155                 self.list = []
156                 ConfigListScreen.__init__(self, self.list,session = self.session)
157                 TEXT = "Tanscoding can be started when there is no corresponding channel recordings."
158                 TEXT += "\nWhen transcoding, both PIP and analog video outputs are disabled."
159                 self["key_red"] = StaticText(_("Cancel"))
160                 self["key_green"] = StaticText(_("Ok"))
161                 self["text"] = StaticText(_("%s")%TEXT)
162                 self.createSetup()
163                 self.onLayoutFinish.append(self.checkModel)
164                 self.checkModelTimer = eTimer()
165                 self.checkModelTimer.callback.append(self.invalidmodel)
166
167         def checkModel(self):
168                 if not self.getModel():
169                         self.checkModelTimer.start(1000,True)
170
171         def invalidmodel(self):
172                         self.session.openWithCallback(self.close, MessageBox, _("This Plugin is available on SOLO2/DUO2"), MessageBox.TYPE_ERROR)
173
174         def createSetup(self):
175                 self.list = []
176                 self.transcording = getConfigListEntry(_("Transcording"), config.plugins.transcordingsetup.transcording)
177                 self.port = getConfigListEntry(_("Port"), config.plugins.transcordingsetup.port)
178                 self.list.append( self.transcording )
179                 if config.plugins.transcordingsetup.transcording.value == "enabled":
180                         self.list.append( self.port )
181                 self["config"].list = self.list
182                 self["config"].l.setList(self.list)
183
184         def keySave(self):
185                 transcording = config.plugins.transcordingsetup.transcording.value
186                 port = config.plugins.transcordingsetup.port.value
187                 print "<ZappingModeSelection> Transcording %s(port : %s)"%(transcording, port)
188                 ret = self.setupTranscording(transcording, port)
189                 if ret is not None and ret <0 :
190                         self.resetConfig()
191                         global error_msg
192                         self.session.openWithCallback(self.close, MessageBox, _("Failed, Encoder %s\n(%s).")%(transcording, error_msg[ret]), MessageBox.TYPE_ERROR)
193                 else:
194                         self.saveAll()
195                         if transcording == "enabled" and port == "8001" :
196                                 text = "PC Streaming is replaced with mobile streaming."
197                                 self.session.openWithCallback(self.close, MessageBox, _("OK. Encoder %s.\n%s")%(transcording,text), MessageBox.TYPE_INFO)
198                         else:
199                                 self.session.openWithCallback(self.close, MessageBox, _("OK. Encoder %s.")%transcording, MessageBox.TYPE_INFO)
200                         self.close()
201
202         def resetConfig(self):
203                 for x in self["config"].list:
204                         x[1].cancel()
205
206         def setupTranscording(self, transcording = None, port = None):
207                 if transcording == "disabled":
208                         config.plugins.transcordingsetup.port.value = "8002"
209                         port = "8002"
210                 return self.setTranscording(transcording, port)
211
212         def keyLeft(self):
213                 ConfigListScreen.keyLeft(self)
214                 if self["config"].getCurrent() == self.transcording:
215                         self.createSetup()
216
217         def keyRight(self):
218                 ConfigListScreen.keyRight(self)
219                 if self["config"].getCurrent() == self.transcording:
220                         self.createSetup()
221
222 def main(session, **kwargs):
223         session.open(TranscordingSetup)
224
225 def Plugins(**kwargs):
226         return [PluginDescriptor(name=_("TranscordingSetup"), description="Transcording Setup", where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)]
227
228 transcordingsetupinit = TranscordingSetupInit()
229