ServiceInfo: Add is 4K
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / Converter / ServiceInfo.py
1 from Components.Converter.Converter import Converter
2 from enigma import iServiceInformation, iPlayableService
3 from Components.Element import cached
4
5 WIDESCREEN = [3, 4, 7, 8, 0xB, 0xC, 0xF, 0x10]
6
7 class ServiceInfo(Converter, object):
8         HAS_TELETEXT = 0
9         IS_MULTICHANNEL = 1
10         IS_CRYPTED = 2
11         IS_WIDESCREEN = 3
12         SUBSERVICES_AVAILABLE = 4
13         XRES = 5
14         YRES = 6
15         APID = 7
16         VPID = 8
17         PCRPID = 9
18         PMTPID = 10
19         TXTPID = 11
20         TSID = 12
21         ONID = 13
22         SID = 14
23         FRAMERATE = 15
24         TRANSFERBPS = 16
25         HAS_HBBTV = 17
26         AUDIOTRACKS_AVAILABLE = 18
27         SUBTITLES_AVAILABLE = 19
28         EDITMODE = 20
29         IS_STREAM = 21
30         IS_SD = 22
31         IS_HD = 23
32         IS_SD_AND_WIDESCREEN = 24
33         IS_SD_AND_NOT_WIDESCREEN = 25
34         IS_4K = 26
35
36         def __init__(self, type):
37                 Converter.__init__(self, type)
38                 self.type, self.interesting_events = {
39                                 "HasTelext": (self.HAS_TELETEXT, (iPlayableService.evUpdatedInfo,)),
40                                 "IsMultichannel": (self.IS_MULTICHANNEL, (iPlayableService.evUpdatedInfo,)),
41                                 "IsCrypted": (self.IS_CRYPTED, (iPlayableService.evUpdatedInfo,)),
42                                 "IsWidescreen": (self.IS_WIDESCREEN, (iPlayableService.evVideoSizeChanged,)),
43                                 "SubservicesAvailable": (self.SUBSERVICES_AVAILABLE, (iPlayableService.evUpdatedEventInfo,)),
44                                 "VideoWidth": (self.XRES, (iPlayableService.evVideoSizeChanged,)),
45                                 "VideoHeight": (self.YRES, (iPlayableService.evVideoSizeChanged,)),
46                                 "AudioPid": (self.APID, (iPlayableService.evUpdatedInfo,)),
47                                 "VideoPid": (self.VPID, (iPlayableService.evUpdatedInfo,)),
48                                 "PcrPid": (self.PCRPID, (iPlayableService.evUpdatedInfo,)),
49                                 "PmtPid": (self.PMTPID, (iPlayableService.evUpdatedInfo,)),
50                                 "TxtPid": (self.TXTPID, (iPlayableService.evUpdatedInfo,)),
51                                 "TsId": (self.TSID, (iPlayableService.evUpdatedInfo,)),
52                                 "OnId": (self.ONID, (iPlayableService.evUpdatedInfo,)),
53                                 "Sid": (self.SID, (iPlayableService.evUpdatedInfo,)),
54                                 "Framerate": (self.FRAMERATE, (iPlayableService.evVideoSizeChanged,iPlayableService.evUpdatedInfo,)),
55                                 "TransferBPS": (self.TRANSFERBPS, (iPlayableService.evUpdatedInfo,)),
56                                 "HasHBBTV": (self.HAS_HBBTV, (iPlayableService.evUpdatedInfo,iPlayableService.evHBBTVInfo,)),
57                                 "AudioTracksAvailable": (self.AUDIOTRACKS_AVAILABLE, (iPlayableService.evUpdatedInfo,)),
58                                 "SubtitlesAvailable": (self.SUBTITLES_AVAILABLE, (iPlayableService.evUpdatedInfo,)),
59                                 "Editmode": (self.EDITMODE, (iPlayableService.evUpdatedInfo,)),
60                                 "IsStream": (self.IS_STREAM, (iPlayableService.evUpdatedInfo,)),
61                                 "IsSD": (self.IS_SD, (iPlayableService.evVideoSizeChanged,)),
62                                 "IsHD": (self.IS_HD, (iPlayableService.evVideoSizeChanged,)),
63                                 "IsSDAndWidescreen": (self.IS_SD_AND_WIDESCREEN, (iPlayableService.evVideoSizeChanged,)),
64                                 "IsSDAndNotWidescreen": (self.IS_SD_AND_NOT_WIDESCREEN, (iPlayableService.evVideoSizeChanged,)),
65                                 "Is4K": (self.IS_4K, (iPlayableService.evVideoSizeChanged,)),
66                         }[type]
67
68         def getServiceInfoString(self, info, what, convert = lambda x: "%d" % x):
69                 v = info.getInfo(what)
70                 if v == -1:
71                         return "N/A"
72                 if v == -2:
73                         return info.getInfoString(what)
74                 return convert(v)
75
76         @cached
77         def getBoolean(self):
78                 service = self.source.service
79                 info = service and service.info()
80                 if not info:
81                         return False
82
83                 if self.type == self.HAS_TELETEXT:
84                         tpid = info.getInfo(iServiceInformation.sTXTPID)
85                         return tpid != -1
86                 elif self.type == self.IS_MULTICHANNEL:
87                         # FIXME. but currently iAudioTrackInfo doesn't provide more information.
88                         audio = service.audioTracks()
89                         if audio:
90                                 n = audio.getNumberOfTracks()
91                                 idx = 0
92                                 while idx < n:
93                                         i = audio.getTrackInfo(idx)
94                                         description = i.getDescription();
95                                         if "AC3" in description or "AC-3" in description or "DTS" in description:
96                                                 return True
97                                         idx += 1
98                         return False
99                 elif self.type == self.IS_CRYPTED:
100                         return info.getInfo(iServiceInformation.sIsCrypted) == 1
101                 elif self.type == self.IS_WIDESCREEN:
102                         return info.getInfo(iServiceInformation.sAspect) in WIDESCREEN
103                 elif self.type == self.SUBSERVICES_AVAILABLE:
104                         subservices = service.subServices()
105                         return subservices and subservices.getNumberOfSubservices() > 0
106                 elif self.type == self.HAS_HBBTV:
107                         return info.getInfoString(iServiceInformation.sHBBTVUrl) != ""
108                 elif self.type == self.AUDIOTRACKS_AVAILABLE:
109                         audio = service.audioTracks()
110                         return audio and audio.getNumberOfTracks() > 1
111                 elif self.type == self.SUBTITLES_AVAILABLE:
112                         subtitle = service and service.subtitle()
113                         subtitlelist = subtitle and subtitle.getSubtitleList()
114                         if subtitlelist:
115                                 return len(subtitlelist) > 0
116                         return False
117                 elif self.type == self.EDITMODE:
118                         return hasattr(self.source, "editmode") and not not self.source.editmode
119                 elif self.type == self.IS_STREAM:
120                         return service.streamed() is not None
121                 elif self.type == self.IS_SD:
122                         return info.getInfo(iServiceInformation.sVideoHeight) < 720
123                 elif self.type == self.IS_HD:
124                         return info.getInfo(iServiceInformation.sVideoHeight) >= 720 and info.getInfo(iServiceInformation.sVideoHeight) < 2160
125                 elif self.type == self.IS_SD_AND_WIDESCREEN:
126                         return info.getInfo(iServiceInformation.sVideoHeight) < 720 and info.getInfo(iServiceInformation.sAspect) in WIDESCREEN
127                 elif self.type == self.IS_SD_AND_NOT_WIDESCREEN:
128                         return info.getInfo(iServiceInformation.sVideoHeight) < 720 and info.getInfo(iServiceInformation.sAspect) not in WIDESCREEN
129                 elif self.type == self.IS_4K:
130                         return info.getInfo(iServiceInformation.sVideoHeight) >= 2160
131                 return False
132
133         boolean = property(getBoolean)
134
135         @cached
136         def getText(self):
137                 service = self.source.service
138                 info = service and service.info()
139                 if not info:
140                         return ""
141
142                 if self.type == self.XRES:
143                         return self.getServiceInfoString(info, iServiceInformation.sVideoWidth)
144                 elif self.type == self.YRES:
145                         return self.getServiceInfoString(info, iServiceInformation.sVideoHeight)
146                 elif self.type == self.APID:
147                         return self.getServiceInfoString(info, iServiceInformation.sAudioPID)
148                 elif self.type == self.VPID:
149                         return self.getServiceInfoString(info, iServiceInformation.sVideoPID)
150                 elif self.type == self.PCRPID:
151                         return self.getServiceInfoString(info, iServiceInformation.sPCRPID)
152                 elif self.type == self.PMTPID:
153                         return self.getServiceInfoString(info, iServiceInformation.sPMTPID)
154                 elif self.type == self.TXTPID:
155                         return self.getServiceInfoString(info, iServiceInformation.sTXTPID)
156                 elif self.type == self.TSID:
157                         return self.getServiceInfoString(info, iServiceInformation.sTSID)
158                 elif self.type == self.ONID:
159                         return self.getServiceInfoString(info, iServiceInformation.sONID)
160                 elif self.type == self.SID:
161                         return self.getServiceInfoString(info, iServiceInformation.sSID)
162                 elif self.type == self.FRAMERATE:
163                         return self.getServiceInfoString(info, iServiceInformation.sFrameRate, lambda x: "%d fps" % ((x+500)/1000))
164                 elif self.type == self.TRANSFERBPS:
165                         return self.getServiceInfoString(info, iServiceInformation.sTransferBPS, lambda x: "%d kB/s" % (x/1024))
166                 elif self.type == self.HAS_HBBTV:
167                         return info.getInfoString(iServiceInformation.sHBBTVUrl)
168                 return ""
169
170         text = property(getText)
171
172         @cached
173         def getValue(self):
174                 service = self.source.service
175                 info = service and service.info()
176                 if not info:
177                         return -1
178
179                 if self.type == self.XRES:
180                         return info.getInfo(iServiceInformation.sVideoWidth)
181                 if self.type == self.YRES:
182                         return info.getInfo(iServiceInformation.sVideoHeight)
183                 if self.type == self.FRAMERATE:
184                         return info.getInfo(iServiceInformation.sFrameRate)
185
186                 return -1
187
188         value = property(getValue)
189
190         def changed(self, what):
191                 if what[0] != self.CHANGED_SPECIFIC or what[1] in self.interesting_events:
192                         Converter.changed(self, what)