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