Initial commit of streamserverseek
[enigma2-plugins.git] / streamserverseek / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 from enigma import eStreamServer, eEnv
4 from Components.StreamServerControl import streamServerControl
5
6 import os.path
7 import urllib
8
9 from Tools.Log import Log
10 from urlparse import parse_qs
11
12 from Plugins.Extensions.WebInterface.WebChilds.Toplevel import addExternalChild
13 from Plugins.Extensions.WebInterface.WebChilds.Screenpage import ScreenPage
14
15 from twisted.web import static
16 from twisted.python import util
17
18 def StreamServerControl__onUriParametersChanged(parameters):
19         self = streamServerControl
20
21         Log.i("%s" %(parameters))
22         params = parse_qs(parameters)
23
24         print "[StreamserverSeek] Old params: %s" % params
25         self._seekToMin = 0
26         if int(self.config.streamserver.source.value) == eStreamServer.INPUT_MODE_BACKGROUND:
27                 ts = False
28                 if "ts" in params:
29                         ts = params.get("ts", [False])[0]
30                         del params["ts"]
31                 self._seekToMin = 0
32                 min = 0
33                 if "min" in params:
34                         tmp = params.get("min", [False])[0]
35                         if "min" in params:
36                                 del params["min"]
37                         try:
38                                 min = int(tmp)
39                         except Exception, e:
40                                 pass
41                 if ts and os.path.isfile(ts):
42                         params["ref"] = ["1:0:0:0:0:0:0:0:0:0:" + urllib.quote(ts)]
43                         if min and min > 0:
44                                 self._seekToMin = min
45                                 print "[StreamserverSeek] Will seek to %d minutes" % self._seekToMin
46                 print "[StreamserverSeek] New params: %s" % params
47
48         self._applyUriParameters(params)
49         for fnc in self.onUriParametersChanged:
50                 fnc(params)
51
52 streamServerControl._onUriParametersChanged_conn = streamServerControl._streamServer.uriParametersChanged.connect(StreamServerControl__onUriParametersChanged)
53
54 Orig_StreamServerControl__startEncoderService = streamServerControl._startEncoderService
55
56 def StreamServerControl__startEncoderService(service):
57         self = streamServerControl
58         result = Orig_StreamServerControl__startEncoderService(service)
59
60         if result == self.ENCODER_SERVICE_SET or result == self.ENCODER_SERVICE_ALREADY_ACTIVE:
61                 if self._seekToMin and self.sourceState == True and self._encoderService and int(self.config.streamserver.source.value) == eStreamServer.INPUT_MODE_BACKGROUND:
62                         seek = self._encoderService.seek()
63                         if seek:
64                                 print "[StreamserverSeek] Seeking to %d minutes" % self._seekToMin
65                                 seek.seekTo(self._seekToMin * 60 * 90000)
66                         self._seekToMin = 0
67
68 streamServerControl._startEncoderService = StreamServerControl__startEncoderService
69
70 File = static.File
71
72 def autostart(reason,**kwargs):
73         if "session" in kwargs:
74                 print "[StreamserverSeek] init"
75                 session = kwargs["session"]
76                 root = File(eEnv.resolve("${libdir}/enigma2/python/Plugins/Extensions/StreamServerSeek/web-data"))
77                 root.putChild("web", ScreenPage(session, util.sibpath(__file__, "web"), True) )
78                 addExternalChild( ("streamserverseek", root) )
79
80 def Plugins(**kwargs): 
81         return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart)]