import cleanup
[enigma2-plugins.git] / netcaster / src / bin / StreamPlayer.py
1 from enigma import eServiceReference
2 from os import system
3
4 class StreamPlayer:
5     is_playing = False
6     
7     def __init__(self,session, args = 0):
8         print " init StreamPlayer"
9         self.session = session
10         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
11         self.session.nav.event.append(self.__event)
12     
13     def __event(self, ev):
14         print "EVENT ==>",ev
15
16     def play(self,stream):
17         print " start streaming %s" %stream.getURL()
18         if self.is_playing is True:
19             self.stop()
20             self.play(stream)
21         else:
22             if stream.getURL().startswith("/") is not True:
23                 print "playing remote stream",stream.getURL()
24                 self.session.nav.stopService()
25 #                sref = eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%stream.getURL().replace(":",":"))
26 #                self.session.nav.playService(sref)
27                 self.targetfile = "/tmp/streamtarget."+stream.getType().lower() 
28                 system("mknod %s p" %self.targetfile)
29                 system("wget %s -O- > %s&" %(stream.getURL(),self.targetfile))
30                 self.session.nav.playService(eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%self.targetfile))
31             else:
32                 print "playing local stream",stream.getURL()
33                 esref = eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%stream.getURL())
34                 self.session.nav.playService(esref)
35             self.is_playing = True
36             
37     def stop(self,text=""):
38         if self.is_playing is True:
39             print " stop streaming",text
40             try:
41                 
42                 self.session.nav.stopService()
43                 system("killall -9 wget")
44                 system("rm %s" %self.targetfile)
45                 self.session.nav.playService(self.oldService)
46                 self.is_playing = False
47             except TypeError,e:
48                 print " ERROR ",e
49                 self.exit()
50         else:
51             pass
52     def exit(self):
53         self.stop()
54