netcaster: load defaults from plugin directory, simplify directory structure
[enigma2-plugins.git] / netcaster / src / StreamPlayer.py
1 from enigma import eServiceReference, iServiceInformation
2
3 class StreamPlayer:
4         is_playing = False
5
6         def __init__(self, session, args=0):
7             print "[NETcaster.StreamPlayer] init StreamPlayer"
8             self.is_playing = False
9             self.session = session
10             self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
11             self.session.nav.event.append(self.__event)
12             self.metadatachangelisteners = []
13
14         def __event(self, ev):
15             print "[NETcaster.StreamPlayer] EVENT ==>", ev
16             if ev == 5: # can we use a constant here instead of just 5?
17                         currentServiceRef = self.session.nav.getCurrentService()
18                         if currentServiceRef is not None:
19                                 #it seems, that only Title is avaible for now
20                                 sTagTitle = currentServiceRef.info().getInfoString(iServiceInformation.sTagTitle)
21                                 self._onMetadataChanged(sTagTitle)
22
23         def _onMetadataChanged(self,title):
24                 for i in self.metadatachangelisteners:
25                         i(title)
26
27         def play(self, stream):
28             if self.is_playing:
29                 self.stop()
30             stream.getURL(self._playURL)
31
32         def _playURL(self, url=None):
33                 if not url:
34                         print "no URL provided for play"
35                         return
36                 print "[NETcaster.StreamPlayer] playing stream", url
37
38                 esref = eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s" % url.replace(':', '%3a'))
39
40                 try:
41                         self.session.nav.playService(esref)
42                         self.is_playing = True
43                 except:
44                         print "[NETcaster.StreamPlayer] Could not play %s" % esref
45
46         def stop(self, text=""):
47             if self.is_playing:
48                 print "[NETcaster.StreamPlayer] stop streaming", text
49                 try:
50                     self.is_playing = False
51                     self.session.nav.stopService()
52                     self.session.nav.playService(self.oldService)
53                 except TypeError, e:
54                     print " ERROR Z", e
55                     self.exit()
56
57         def exit(self):
58             self.stop()
59