servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / python / Screens / ServiceInfo.py
1 from Components.HTMLComponent import HTMLComponent
2 from Components.GUIComponent import GUIComponent
3 from Screen import Screen
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from ServiceReference import ServiceReference
7 from enigma import eListboxPythonMultiContent, eListbox, gFont, iServiceInformation, eServiceCenter
8 from Tools.Transponder import ConvertToHumanReadable
9
10 RT_HALIGN_LEFT = 0
11
12 TYPE_TEXT = 0
13 TYPE_VALUE_HEX = 1
14 TYPE_VALUE_DEC = 2
15 TYPE_VALUE_HEX_DEC = 3
16 TYPE_SLIDER = 4
17
18 def to_unsigned(x):
19         return x & 0xFFFFFFFF
20
21 def ServiceInfoListEntry(a, b, valueType=TYPE_TEXT, param=4):
22         print "b:", b
23         if not isinstance(b, str):
24                 if valueType == TYPE_VALUE_HEX:
25                         b = ("0x%0" + str(param) + "x") % to_unsigned(b)
26                 elif valueType == TYPE_VALUE_DEC:
27                         b = str(b)
28                 elif valueType == TYPE_VALUE_HEX_DEC:
29                         b = ("0x%0" + str(param) + "x (%dd)") % (to_unsigned(b), b)
30                 else:
31                         b = str(b)
32
33         return [
34                 #PyObject *type, *px, *py, *pwidth, *pheight, *pfnt, *pstring, *pflags;
35                 (eListboxPythonMultiContent.TYPE_TEXT, 0, 0, 200, 30, 0, RT_HALIGN_LEFT, ""),
36                 (eListboxPythonMultiContent.TYPE_TEXT, 0, 0, 200, 25, 0, RT_HALIGN_LEFT, a),
37                 (eListboxPythonMultiContent.TYPE_TEXT, 220, 0, 350, 25, 0, RT_HALIGN_LEFT, b)
38         ]
39
40 class ServiceInfoList(HTMLComponent, GUIComponent):
41         def __init__(self, source):
42                 GUIComponent.__init__(self)
43                 self.l = eListboxPythonMultiContent()
44                 self.list = source
45                 self.l.setList(self.list)
46                 self.l.setFont(0, gFont("Regular", 23))
47                 self.l.setItemHeight(25)
48
49         GUI_WIDGET = eListbox
50
51         def postWidgetCreate(self, instance):
52                 self.instance.setContent(self.l)
53
54 TYPE_SERVICE_INFO = 1
55 TYPE_TRANSPONDER_INFO = 2
56
57 class ServiceInfo(Screen):
58         def __init__(self, session, serviceref=None):
59                 Screen.__init__(self, session)
60                 
61                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
62                 {
63                         "ok": self.close,
64                         "cancel": self.close,
65                         "red": self.information,
66                         "green": self.pids,
67                         "yellow": self.transponder,
68                         "blue": self.tuner
69                 }, -1)
70
71                 if serviceref:
72                         self.type = TYPE_TRANSPONDER_INFO
73                         self["red"] = Label()
74                         self["green"] = Label()
75                         self["yellow"] = Label()
76                         self["blue"] = Label()
77                         info = eServiceCenter.getInstance().info(serviceref)
78                         self.transponder_info = info.getInfoObject(serviceref, iServiceInformation.sTransponderData)
79                         # info is a iStaticServiceInformation, not a iServiceInformation
80                         self.info = None
81                         self.feinfo = None
82                 else:
83                         self.type = TYPE_SERVICE_INFO
84                         self["red"] = Label(_("Service"))
85                         self["green"] = Label(_("PIDs"))
86                         self["yellow"] = Label(_("Multiplex"))
87                         self["blue"] = Label(_("Tuner status"))
88                         service = session.nav.getCurrentService()
89                         if service is not None:
90                                 self.info = service.info()
91                                 self.feinfo = service.frontendInfo()
92                                 print self.info.getInfoObject(iServiceInformation.sCAIDs);
93                         else:
94                                 self.info = None
95                                 self.feinfo = None
96
97                 tlist = [ ]
98
99                 self["infolist"] = ServiceInfoList(tlist)
100                 self.onShown.append(self.information)
101
102         def information(self):
103                 if self.type == TYPE_SERVICE_INFO:
104                         if self.session.nav.getCurrentlyPlayingServiceReference():
105                                 name = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference()).getServiceName()
106                                 refstr = self.session.nav.getCurrentlyPlayingServiceReference().toString()
107                         else:
108                                 name = _("N/A")
109                                 refstr = _("N/A")
110                         aspect = self.getServiceInfoValue(iServiceInformation.sAspect)
111                         if aspect in ( 1, 2, 5, 6, 9, 0xA, 0xD, 0xE ):
112                                 aspect = _("4:3")
113                         else:
114                                 aspect = _("16:9")
115                         width = self.info and self.info.getInfo(iServiceInformation.sVideoWidth) or -1
116                         height = self.info and self.info.getInfo(iServiceInformation.sVideoHeight) or -1
117                         if width != -1 and height != -1:
118                                 Labels = ( (_("Name"), name, TYPE_TEXT),
119                                                    (_("Provider"), self.getServiceInfoValue(iServiceInformation.sProvider), TYPE_TEXT),
120                                                    (_("Aspect ratio"), aspect, TYPE_TEXT),
121                                                    (_("Resolution"), "%dx%d" %(width, height), TYPE_TEXT),
122                                                    (_("Namespace"), self.getServiceInfoValue(iServiceInformation.sNamespace), TYPE_VALUE_HEX, 8),
123                                                    (_("Service reference"), refstr, TYPE_TEXT))
124                         else:
125                                 Labels = ( (_("Name"), name, TYPE_TEXT),
126                                                    (_("Provider"), self.getServiceInfoValue(iServiceInformation.sProvider), TYPE_TEXT),
127                                                    (_("Aspect ratio"), aspect, TYPE_TEXT),
128                                                    (_("Namespace"), self.getServiceInfoValue(iServiceInformation.sNamespace), TYPE_VALUE_HEX, 8),
129                                                    (_("Service reference"), refstr, TYPE_TEXT))
130                         self.fillList(Labels)
131                 else:
132                         if self.transponder_info:
133                                 tp_info = ConvertToHumanReadable(self.transponder_info)
134                                 conv = { "tuner_type"                   : _("Type"),
135                                          "system"                       : _("System"),
136                                          "modulation"                   : _("Modulation"),
137                                          "orbital_position"             : _("Orbital position"),
138                                          "frequency"                    : _("Frequency"),
139                                          "symbol_rate"                  : _("Symbol rate"),
140                                          "bandwidth"                    : _("Bandwidth"),
141                                          "polarization"                 : _("Polarization"),
142                                          "inversion"                    : _("Inversion"),
143                                          "pilot"                        : _("Pilot"),
144                                          "rolloff"                      : _("Roll-off"),
145                                          "fec_inner"                    : _("FEC"),
146                                          "code_rate_lp"                 : _("Code rate LP"),
147                                          "code_rate_hp"                 : _("Code rate HP"),
148                                          "constellation"                : _("Constellation"),
149                                          "transmission_mode"            : _("Transmission mode"),
150                                          "guard_interval"               : _("Guard interval"),
151                                          "hierarchy_information"        : _("Hierarchy info") }
152                                 Labels = [(conv[i], tp_info[i], TYPE_VALUE_DEC) for i in tp_info.keys()]
153                                 self.fillList(Labels)
154
155         def pids(self):
156                 if self.type == TYPE_SERVICE_INFO:
157                         Labels = ( (_("Video PID"), self.getServiceInfoValue(iServiceInformation.sVideoPID), TYPE_VALUE_HEX_DEC, 4),
158                                            (_("Audio PID"), self.getServiceInfoValue(iServiceInformation.sAudioPID), TYPE_VALUE_HEX_DEC, 4),
159                                            (_("PCR PID"), self.getServiceInfoValue(iServiceInformation.sPCRPID), TYPE_VALUE_HEX_DEC, 4),
160                                            (_("PMT PID"), self.getServiceInfoValue(iServiceInformation.sPMTPID), TYPE_VALUE_HEX_DEC, 4),
161                                            (_("TXT PID"), self.getServiceInfoValue(iServiceInformation.sTXTPID), TYPE_VALUE_HEX_DEC, 4),
162                                            (_("TSID"), self.getServiceInfoValue(iServiceInformation.sTSID), TYPE_VALUE_HEX_DEC, 4),
163                                            (_("ONID"), self.getServiceInfoValue(iServiceInformation.sONID), TYPE_VALUE_HEX_DEC, 4),
164                                            (_("SID"), self.getServiceInfoValue(iServiceInformation.sSID), TYPE_VALUE_HEX_DEC, 4))
165                         self.fillList(Labels)
166         
167         def showFrontendData(self, real):
168                 if self.type == TYPE_SERVICE_INFO:
169                         frontendData = self.feinfo and self.feinfo.getAll(real)
170                         Labels = self.getFEData(frontendData)
171                         self.fillList(Labels)
172         
173         def transponder(self):
174                 if self.type == TYPE_SERVICE_INFO:
175                         self.showFrontendData(True)
176                 
177         def tuner(self):
178                 if self.type == TYPE_SERVICE_INFO:
179                         self.showFrontendData(False)
180
181         def getFEData(self, frontendDataOrg):
182                 if frontendDataOrg and len(frontendDataOrg):
183                         frontendData = ConvertToHumanReadable(frontendDataOrg)
184                         if frontendDataOrg["tuner_type"] == "DVB-S":
185                                 return ((_("NIM"), ('A', 'B', 'C', 'D')[frontendData["tuner_number"]], TYPE_TEXT),
186                                                 (_("Type"), frontendData["tuner_type"], TYPE_TEXT),
187                                                 (_("System"), frontendData["system"], TYPE_TEXT),
188                                                 (_("Modulation"), frontendData["modulation"], TYPE_TEXT),
189                                                 (_("Orbital position"), frontendData["orbital_position"], TYPE_VALUE_DEC),
190                                                 (_("Frequency"), frontendData["frequency"], TYPE_VALUE_DEC),
191                                                 (_("Symbol rate"), frontendData["symbol_rate"], TYPE_VALUE_DEC),
192                                                 (_("Polarization"), frontendData["polarization"], TYPE_TEXT),
193                                                 (_("Inversion"), frontendData["inversion"], TYPE_TEXT),
194                                                 (_("FEC"), frontendData["fec_inner"], TYPE_TEXT),
195                                                 (_("Pilot"), frontendData.get("pilot", None), TYPE_TEXT),
196                                                 (_("Roll-off"), frontendData.get("rolloff", None), TYPE_TEXT))
197                         elif frontendDataOrg["tuner_type"] == "DVB-C":
198                                 return ((_("NIM"), ('A', 'B', 'C', 'D')[frontendData["tuner_number"]], TYPE_TEXT),
199                                                 (_("Type"), frontendData["tuner_type"], TYPE_TEXT),
200                                                 (_("Modulation"), frontendData["modulation"], TYPE_TEXT),
201                                                 (_("Frequency"), frontendData["frequency"], TYPE_VALUE_DEC),
202                                                 (_("Symbol rate"), frontendData["symbol_rate"], TYPE_VALUE_DEC),
203                                                 (_("Inversion"), frontendData["inversion"], TYPE_TEXT),
204                                                 (_("FEC"), frontendData["fec_inner"], TYPE_TEXT))
205                         elif frontendDataOrg["tuner_type"] == "DVB-T":
206                                 return ((_("NIM"), ('A', 'B', 'C', 'D')[frontendData["tuner_number"]], TYPE_TEXT),
207                                                 (_("Type"), frontendData["tuner_type"], TYPE_TEXT),
208                                                 (_("Frequency"), frontendData["frequency"], TYPE_VALUE_DEC),
209                                                 (_("Inversion"), frontendData["inversion"], TYPE_TEXT),
210                                                 (_("Bandwidth"), frontendData["bandwidth"], TYPE_VALUE_DEC),
211                                                 (_("Code rate LP"), frontendData["code_rate_lp"], TYPE_TEXT),
212                                                 (_("Code rate HP"), frontendData["code_rate_hp"], TYPE_TEXT),
213                                                 (_("Constellation"), frontendData["constellation"], TYPE_TEXT),
214                                                 (_("Transmission mode"), frontendData["transmission_mode"], TYPE_TEXT),
215                                                 (_("Guard interval"), frontendData["guard_interval"], TYPE_TEXT),
216                                                 (_("Hierarchy info"), frontendData["hierarchy_information"], TYPE_TEXT))
217                 return [ ]
218
219         def fillList(self, Labels):
220                 tlist = [ ]
221
222                 for item in Labels:
223                         if item[1] is None:
224                                 continue;
225                         value = item[1]
226                         if len(item) < 4:
227                                 tlist.append(ServiceInfoListEntry(item[0]+":", value, item[2]))
228                         else:
229                                 tlist.append(ServiceInfoListEntry(item[0]+":", value, item[2], item[3]))
230
231                 self["infolist"].l.setList(tlist)
232
233         def getServiceInfoValue(self, what):
234                 if self.info is None:
235                         return ""
236                 
237                 v = self.info.getInfo(what)
238                 if v == -2:
239                         v = self.info.getInfoString(what)
240                 elif v == -1:
241                         v = _("N/A")
242
243                 return v