[eUrotic TV] handle correct connection error event
[enigma2-plugins.git] / eurotictv / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Screens.InfoBarGenerics import InfoBarSeek, InfoBarCueSheetSupport
5 from Screens.HelpMenu import HelpableScreen
6 from Components.Sources.StaticText import StaticText
7 from Components.Sources.Boolean import Boolean
8 from Components.ActionMap import HelpableActionMap, ActionMap
9 from Components.ServiceEventTracker import ServiceEventTracker, InfoBarBase
10 from Components.VideoWindow import VideoWindow
11 from Components.Label import Label, MultiColorLabel
12 from Components.Pixmap import Pixmap
13 from Components.GUIComponent import GUIComponent
14 from Components.ConfigList import ConfigListScreen
15 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigPassword, ConfigYesNo, ConfigText
16 from enigma import eListboxPythonMultiContent, eServiceReference, getDesktop, iPlayableService, eSize, ePicLoad, iServiceInformation
17 from ServiceReference import ServiceReference
18 from Tools.Directories import SCOPE_PLUGINS, resolveFilename
19 from __init__ import __
20
21 class EuroticTVPlayer(Screen, InfoBarBase, InfoBarSeek, HelpableScreen):
22         STATE_DISCONNECTED, STATE_CONNECTING, STATE_PLAYING, STATE_PAUSED, STATE_FAILURE = range(5)
23         STATE_NAMES = [__("disconnected"), __("connecting..."), __("LIVE"), __("pause"), __("No Connection")]
24         STREAM_DIM = (768, 576)
25         PIP_DIM = (432, 324)
26         STREAM_URI = "http://174.121.228.234/hls-live/livepkgr/_definst_/liveevent/etv-livestream_2.m3u86"
27         CENTER_POS = ((560-PIP_DIM[0])/2)
28
29         skin = """
30         <screen position="center,center" size="560,470" title="eUroticTV" flags="wfNoBorder">
31                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
32                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
33                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
34                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
35                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
36                 <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
37                 <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
38                 <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
39
40                 <widget source="pig_mode" render="Pig" position="%i,60" size="%i,%i" zPosition="0" backgroundColor="transparent">
41                         <convert type="ConditionalShowHide" />
42                 </widget>
43                 <widget name="poster" position="%i,60" size="%i,%i" alphatest="on" zPosition="2" />
44                 <widget name="connection_label" position="85,422" size="384,48" font="Regular;24" valign="center" halign="center" foregroundColors="#000000,#FFFF00,#00FF00,#AAAAAA,#FF0000" />
45                 <widget source="do_blink" render="FixedLabel" text=" " position="85,434" zPosition="1" size="384,24">
46                         <convert type="ConditionalShowHide">Blink</convert>
47                 </widget>
48         </screen>""" % (CENTER_POS, PIP_DIM[0], PIP_DIM[1], CENTER_POS, PIP_DIM[0], PIP_DIM[1])
49
50         def __init__(self, session):
51                 Screen.__init__(self, session)
52                 InfoBarBase.__init__(self, steal_current_service = True)
53                 InfoBarSeek.__init__(self, actionmap = "CutlistSeekActions")
54                 HelpableScreen.__init__(self)
55                 self.old_service = session.nav.getCurrentlyPlayingServiceReference()
56                 self.session.nav.stopService()
57
58                 self["key_red"] = StaticText(__("Exit"))
59                 self["key_green"] = StaticText(__("Play"))
60                 self["key_yellow"] = StaticText((""))
61                 self["key_blue"] = StaticText(__("Fullscreen"))
62                 self["connection_label"] = MultiColorLabel()
63                 self["do_blink"] = Boolean(False)
64                 self["pig_mode"] = Boolean(True)
65
66                 self["poster"] = Pixmap()
67                 self.picload = ePicLoad()
68                 self.picload.PictureData.get().append(self.paintPosterPixmapCB)
69
70                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions", "InfobarSeekActions", "MediaPlayerActions"],
71                 {
72                                 "ok": self.keyOK,
73                                 "cancel": self.exit,
74                                 "stop": self.disconnect,
75                                 "playpauseService": self.playpauseService,
76                                 "red": self.exit,
77                                 "green": self.keyOK,
78                                 "blue": self.keyBlue,
79                                 "yellow": self.keyYellow,
80                                 "seekFwd": self.keyPass
81                         }, -4)
82
83                 self.__event_tracker = ServiceEventTracker(screen = self, eventmap =
84                         {
85                                 iPlayableService.evStart: self.__serviceStarted,
86                                 iPlayableService.evUpdatedEventInfo: self.__streamStarted,
87                                 iPlayableService.evTuneFailed: self.__streamFailed,
88                                 iPlayableService.evEOF: self.__evEOF,
89                                 iPlayableService.evUser+15: self.__streamFailed
90                         })      
91
92                 self.onClose.append(self.__onClose)
93                 self.onExecBegin.append(self.__onExecBegin)
94                 
95                 self.setState(self.STATE_DISCONNECTED)
96
97         def __onExecBegin(self):
98                 from Components.AVSwitch import AVSwitch
99                 sc = AVSwitch().getFramebufferScale()
100                 self.picload.setPara((self["poster"].instance.size().width(), self["poster"].instance.size().height(), self.PIP_DIM[0], self.PIP_DIM[1], False, 1, "#00000000"))
101                 self.picload.startDecode(resolveFilename(SCOPE_PLUGINS)+"Extensions/eUroticTV/"+"eurotic.jpg")
102
103         def setState(self, state):
104                 if state <= self.STATE_FAILURE:
105                         self.state = state
106
107                 self["connection_label"].setForegroundColorNum(self.state)
108                 self["connection_label"].setText(self.STATE_NAMES[self.state])
109                 
110                 if self.state in (self.STATE_CONNECTING, self.STATE_PAUSED):
111                         self["do_blink"].setBoolean(True)
112                 else:
113                         self["do_blink"].setBoolean(False)
114
115                 if self.state in (self.STATE_DISCONNECTED, self.STATE_CONNECTING, self.STATE_FAILURE):
116                         self.togglePIG(fullscreen=False)
117                 
118                 if self.state in (self.STATE_PLAYING, self.STATE_PAUSED):
119                         self["poster"].hide()
120                         self["pig_mode"].setBoolean(True)
121                 else: 
122                         self["poster"].show()
123                         self["pig_mode"].setBoolean(False)
124
125         def paintPosterPixmapCB(self, picInfo=None):
126                 ptr = self.picload.getData()
127                 if ptr != None:
128                         self["poster"].instance.setPixmap(ptr.__deref__())
129                         self["poster"].show()
130
131         def __streamStarted(self):
132                 print "__streamStarted"
133                 if self.state != self.STATE_PAUSED:
134                         self.setState(self.STATE_PLAYING)
135
136         def __serviceStarted(self):
137                 print "__streamStarted"
138                 self.setState(self.STATE_CONNECTING)
139
140         def __streamFailed(self):
141                 print "__streamFailed"
142                 currPlay = self.session.nav.getCurrentService()
143                 message = currPlay.info().getInfoString(iServiceInformation.sUser+12)
144                 self.setState(self.STATE_FAILURE)
145                 self["connection_label"].setText(_("Streaming error: %s") % message)
146
147         def __evEOF(self):
148                 print "__evEOF"
149                 if self.state != self.STATE_FAILURE:
150                         self.setState(self.STATE_DISCONNECTED)
151
152         def keyPass(self):
153                 print "keyPass"
154
155         def keyOK(self):
156                 if self.state in (self.STATE_DISCONNECTED, self.STATE_FAILURE):
157                         self.start()
158                 elif self.state == self.STATE_PAUSED:
159                         self.playpauseService()
160
161         def keyYellow(self):
162                 if self.state in (self.STATE_PLAYING, self.STATE_PAUSED):
163                         self.playpauseService()
164
165         def togglePIG(self, fullscreen=False):
166                 if fullscreen == True:
167                         self.hide()
168                 else:
169                         self.show()
170                 self["pig_mode"].setBoolean(not fullscreen)
171
172         def keyBlue(self):
173                 if self["pig_mode"].getBoolean() == True:
174                         self.togglePIG(True)
175                 else:
176                         self.togglePIG(fullscreen=False)
177
178         def playpauseService(self):
179                 if self.seekstate == self.SEEK_STATE_PLAY:
180                         self.setState(self.STATE_PAUSED)
181                         self.pauseService()
182                 elif self.seekstate == self.SEEK_STATE_PAUSE:
183                         self.setState(self.STATE_PLAYING)
184                         self.unPauseService()
185
186         def checkSkipShowHideLock(self):
187                 pass
188
189         def start(self):
190                 sref = eServiceReference(4097,0,self.STREAM_URI)
191                 sref.setName("eUroticTV Live Stream")
192                 self.session.nav.playService(sref)
193
194         def disconnect(self):
195                 self.session.nav.stopService()
196                 self.setState(self.STATE_DISCONNECTED)
197
198         def __onClose(self):
199                 self.session.nav.playService(self.old_service, forceRestart=True)
200
201         def exit(self):
202                 self.close()
203
204 def main(session, **kwargs):
205         session.open(EuroticTVPlayer)
206
207 def Plugins(**kwargs):
208         return PluginDescriptor(name="eUroticTV", description=__("Watch eUroticTV via HTTP Live Streaming"), where = PluginDescriptor.WHERE_PLUGINMENU, icon="plugin.png", needsRestart = False, fnc=main)