servicemp3.cpp: more simple/flexible streaming detection
[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 class ServiceInfo(Converter, object):
6         HAS_TELETEXT = 0
7         IS_MULTICHANNEL = 1
8         IS_CRYPTED = 2
9         IS_WIDESCREEN = 3
10         SUBSERVICES_AVAILABLE = 4
11         XRES = 5
12         YRES = 6
13         APID = 7
14         VPID = 8
15         PCRPID = 9
16         PMTPID = 10
17         TXTPID = 11
18         TSID = 12
19         ONID = 13
20         SID = 14
21         FRAMERATE = 15
22         TRANSFERBPS = 16
23         
24
25         def __init__(self, type):
26                 Converter.__init__(self, type)
27                 self.type, self.interesting_events = {
28                                 "HasTelext": (self.HAS_TELETEXT, (iPlayableService.evUpdatedInfo,)),
29                                 "IsMultichannel": (self.IS_MULTICHANNEL, (iPlayableService.evUpdatedInfo,)),
30                                 "IsCrypted": (self.IS_CRYPTED, (iPlayableService.evUpdatedInfo,)),
31                                 "IsWidescreen": (self.IS_WIDESCREEN, (iPlayableService.evVideoSizeChanged,)),
32                                 "SubservicesAvailable": (self.SUBSERVICES_AVAILABLE, (iPlayableService.evUpdatedEventInfo,)),
33                                 "VideoWidth": (self.XRES, (iPlayableService.evVideoSizeChanged,)),
34                                 "VideoHeight": (self.YRES, (iPlayableService.evVideoSizeChanged,)),
35                                 "AudioPid": (self.APID, (iPlayableService.evUpdatedInfo,)),
36                                 "VideoPid": (self.VPID, (iPlayableService.evUpdatedInfo,)),
37                                 "PcrPid": (self.PCRPID, (iPlayableService.evUpdatedInfo,)),
38                                 "PmtPid": (self.PMTPID, (iPlayableService.evUpdatedInfo,)),
39                                 "TxtPid": (self.TXTPID, (iPlayableService.evUpdatedInfo,)),
40                                 "TsId": (self.TSID, (iPlayableService.evUpdatedInfo,)),
41                                 "OnId": (self.ONID, (iPlayableService.evUpdatedInfo,)),
42                                 "Sid": (self.SID, (iPlayableService.evUpdatedInfo,)),
43                                 "Framerate": (self.FRAMERATE, (iPlayableService.evVideoSizeChanged,iPlayableService.evUpdatedInfo,)),
44                                 "TransferBPS": (self.TRANSFERBPS, (iPlayableService.evUpdatedInfo,)),
45                         }[type]
46
47         def getServiceInfoString(self, info, what, convert = lambda x: "%d" % x):
48                 v = info.getInfo(what)
49                 if v == -1:
50                         return "N/A"
51                 if v == -2:
52                         return info.getInfoString(what)
53                 return convert(v)
54
55         @cached
56         def getBoolean(self):
57                 service = self.source.service
58                 info = service and service.info()
59                 if not info:
60                         return False
61                 
62                 if self.type == self.HAS_TELETEXT:
63                         tpid = info.getInfo(iServiceInformation.sTXTPID)
64                         return tpid != -1
65                 elif self.type == self.IS_MULTICHANNEL:
66                         # FIXME. but currently iAudioTrackInfo doesn't provide more information.
67                         audio = service.audioTracks()
68                         if audio:
69                                 n = audio.getNumberOfTracks()
70                                 idx = 0
71                                 while idx < n:
72                                         i = audio.getTrackInfo(idx)
73                                         description = i.getDescription();
74                                         if "AC3" in description or "DTS" in description:
75                                                 return True
76                                         idx += 1
77                         return False
78                 elif self.type == self.IS_CRYPTED:
79                         return info.getInfo(iServiceInformation.sIsCrypted) == 1
80                 elif self.type == self.IS_WIDESCREEN:
81                         return info.getInfo(iServiceInformation.sAspect) in (3, 4, 7, 8, 0xB, 0xC, 0xF, 0x10)
82                 elif self.type == self.SUBSERVICES_AVAILABLE:
83                         subservices = service.subServices()
84                         return subservices and subservices.getNumberOfSubservices() > 0
85
86         boolean = property(getBoolean)
87         
88         @cached
89         def getText(self):
90                 service = self.source.service
91                 info = service and service.info()
92                 if not info:
93                         return ""
94
95                 if self.type == self.XRES:
96                         return self.getServiceInfoString(info, iServiceInformation.sVideoWidth)
97                 elif self.type == self.YRES:
98                         return self.getServiceInfoString(info, iServiceInformation.sVideoHeight)
99                 elif self.type == self.APID:
100                         return self.getServiceInfoString(info, iServiceInformation.sAudioPID)
101                 elif self.type == self.VPID:
102                         return self.getServiceInfoString(info, iServiceInformation.sVideoPID)
103                 elif self.type == self.PCRPID:
104                         return self.getServiceInfoString(info, iServiceInformation.sPCRPID)
105                 elif self.type == self.PMTPID:
106                         return self.getServiceInfoString(info, iServiceInformation.sPMTPID)
107                 elif self.type == self.TXTPID:
108                         return self.getServiceInfoString(info, iServiceInformation.sTXTPID)
109                 elif self.type == self.TSID:
110                         return self.getServiceInfoString(info, iServiceInformation.sTSID)
111                 elif self.type == self.ONID:
112                         return self.getServiceInfoString(info, iServiceInformation.sONID)
113                 elif self.type == self.SID:
114                         return self.getServiceInfoString(info, iServiceInformation.sSID)
115                 elif self.type == self.FRAMERATE:
116                         return self.getServiceInfoString(info, iServiceInformation.sFrameRate, lambda x: "%d fps" % ((x+500)/1000))
117                 elif self.type == self.TRANSFERBPS:
118                         return self.getServiceInfoString(info, iServiceInformation.sTransferBPS, lambda x: "%d kB/s" % (x/1024))
119                 return ""
120
121         text = property(getText)
122
123         @cached
124         def getValue(self):
125                 service = self.source.service
126                 info = service and service.info()
127                 if not info:
128                         return -1
129
130                 if self.type == self.XRES:
131                         return info.getInfo(iServiceInformation.sVideoWidth)
132                 if self.type == self.YRES:
133                         return info.getInfo(iServiceInformation.sVideoHeight)
134                 if self.type == self.FRAMERATE:
135                         return info.getInfo(iServiceInformation.sFrameRate)
136
137                 return -1
138
139         value = property(getValue)
140
141         def changed(self, what):
142                 if what[0] != self.CHANGED_SPECIFIC or what[1] in self.interesting_events:
143                         Converter.changed(self, what)