initial version
[enigma2-plugins.git] / vlcplayer / src / VlcControlTelnet.py
1 from Components.config import config\r
2 import socket\r
3 \r
4 def vlctime2sec(t):\r
5         t = float(t)\r
6         if t < 0:\r
7                 t += 2**32\r
8         return t/1000000\r
9 \r
10 class VlcControlTelnet:\r
11         defaultStreamName = "dreambox"\r
12         \r
13         def __init__(self, servernum):\r
14                 cfg = config.plugins.vlcplayer.servers[servernum]\r
15                 host = cfg.host.value\r
16                 port = cfg.adminport.value\r
17                 self.address = (host, int(port));\r
18                 self.passwd = cfg.adminpwd.value\r
19                 try:\r
20                         self.defaultStreamName = "dream" + socket.gethostbyname(socket.gethostname()).split('.')[3]\r
21                 except Exception, e:\r
22                         pass\r
23 \r
24         def __connect(self):\r
25                 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
26                 self.s.settimeout(10)\r
27                 self.s.connect(self.address)\r
28                 resp = self.s.recv(1024)\r
29                 if resp.find("Password:") == -1:\r
30                         raise IOError, "expected: 'Password:', got: %s" % resp\r
31                 resp = self.__submitCmd(self.passwd)\r
32                 if resp.find("Welcome") == -1:\r
33                         raise IOError, "expected: 'Welcome', got: %s" % resp\r
34                 self.s.settimeout(3)\r
35 \r
36         def __close(self):\r
37                 self.s.close()\r
38 \r
39         def __submitCmd(self, command):\r
40                 print "[VLC] SEND:", command\r
41                 self.s.send(command + "\n")\r
42                 msg = self.s.recv(4096)\r
43                 if msg is None: \r
44                         msg = ""\r
45                 else:\r
46                         msg = msg.strip()\r
47                 if msg[-1:] == ">": msg = msg[:-1].strip()\r
48                 #print "[VLC] RECV:", msg\r
49                 return msg\r
50                 \r
51         def __submitCmdExpectNone(self, command):\r
52                 resp = self.__submitCmd(command)\r
53                 if len(resp) > 0:\r
54                         raise IOError, "Error with VLC command:\n%s\nResponse: %s" % (command,resp)\r
55 \r
56         def __setupStream(self, inputfile, sout):\r
57                 self.__submitCmd('del ' + self.defaultStreamName)\r
58                 self.__submitCmdExpectNone(\r
59                         'new %s broadcast enabled input "%s" output %s' % (self.defaultStreamName, inputfile, sout)\r
60                 )\r
61 \r
62         def __status(self):\r
63                 stats = {}\r
64                 resp = self.__submitCmd("show %s \n" % self.defaultStreamName)\r
65                 if resp is None or len(resp) == 0:\r
66                         return stats\r
67                 for line in resp.split("\n"):\r
68                         if len(line) == 0 or line[0] == '>':\r
69                                 break\r
70                         args = line.strip().split(" : ", 2)\r
71                         if len(args) == 2:\r
72                                 key = args[0].strip()\r
73                                 val = args[1].strip()\r
74                                 if key == "time":\r
75                                         stats[key] = vlctime2sec(val)\r
76                                 elif key == "length":\r
77                                         stats[key] = vlctime2sec(val)\r
78                                 elif key == "state":\r
79                                         stats[key] = val\r
80                                 elif key == "position":\r
81                                         stats[key] = float(val)\r
82                 return stats\r
83 \r
84         def playfile(self, filename, output):\r
85                 self.__connect()\r
86                 self.__setupStream(filename, output)\r
87                 self.play()\r
88                 self.__close()\r
89 \r
90         def play(self, listid=None):\r
91                 if listid is None: listid = self.defaultStreamName\r
92                 self.__connect()\r
93                 self.__submitCmdExpectNone(\r
94                         'control %s play' % listid\r
95                 )\r
96                 self.__close()\r
97 \r
98         def pause(self):\r
99                 self.__connect()\r
100                 self.__submitCmdExpectNone(\r
101                         'control %s pause' % self.defaultStreamName\r
102                 )\r
103                 self.__close()\r
104 \r
105         def stop(self):\r
106                 self.__connect()\r
107                 self.__submitCmdExpectNone(\r
108                         'control %s stop' % self.defaultStreamName\r
109                 )\r
110                 self.__close()\r
111 \r
112         def seek(self, value):\r
113                 self.__connect()\r
114                 stats = self.__status()\r
115                 time = stats["time"]\r
116                 length = stats["length"]\r
117                 if time is not None and length is not None:\r
118                         time += float(value)\r
119                         if time < 0: time = 0\r
120                         elif time > length: time = length\r
121                         value = str(time/length*100) + "%"\r
122                 self.__submitCmdExpectNone(\r
123                         'control %s seek %s' % (self.defaultStreamName, value)\r
124                 )\r
125                 self.__close()\r
126 \r
127         def delete(self, listid=None):\r
128                 if listid is None:\r
129                         listid = self.defaultStreamName\r
130                 self.__connect()\r
131                 self.__submitCmd('del ' + listid)\r
132                 self.__close()\r
133                 \r
134         def status(self):\r
135                 self.__connect()\r
136                 stats = self.__status()\r
137                 self.__close()\r
138                 return stats\r
139 \r