* libvlc binding for python
[vlc.git] / python / vlrs / session.py
1 #!/usr/bin/python -O
2 #
3 # VideoLAN RTSP Server
4 #
5 # Author: Cyril Deguet <asmax@via.ecp.fr>
6
7
8 import cfg, random, time
9
10 from streamer import VlcError, VlcStreamer
11
12
13 class Session:
14     "RTSP Session"
15     
16     def __init__(self, id, uri, dest):
17         self.id = id
18         self.uri = uri
19         self.dest = dest
20         self.state = 'ready'
21         media = cfg.playlist.getMedia(self.uri)
22         self.fileName = media['file']
23         address = "rtp/ts://" + dest
24         self.streamer = VlcStreamer(self.fileName, address)
25         
26     def play(self):
27         "Play this session"
28         if self.state == 'playing':
29             print "Session " + self.id + " (" + self.fileName + "): already playing"
30             return 0
31         self.state = 'playing'
32         print "Session " + self.id + " (" + self.fileName + "): play"
33         try:
34             self.streamer.play()
35         except VlcError:
36             print "Streamer: play failed"
37             return -1
38         return 0
39
40     def pause(self):
41         "Pause this session"
42         print "Session " + self.id + " (" + self.fileName + "): pause"
43         self.state = 'ready'
44         try:
45             self.streamer.pause()
46         except VlcError:
47             print "Streamer: pause failed"
48             return -1
49         return 0
50
51     def stop(self):
52         "Stop this session"
53         print "Session " + self.id + " (" + self.fileName + "): stop"
54         try:
55             self.streamer.stop()
56         except VlcError:
57             print "Streamer: stop failed"
58             return -1
59         return 0
60
61
62
63 class SessionList:
64     "Manages RTSP sessions"
65
66     list = {}
67     chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
68
69     def __init__(self):
70         self.rand = random.Random(time.time())
71     
72     def newSessionId(self):
73         "Build a random session id"
74         id = ""
75         for x in range(12):
76             id += self.chars[self.rand.randrange(0, len(self.chars), 1)]
77         return id
78
79     def newSession(self, uri, dest):
80         "Create a new RTSP session"
81         id = self.newSessionId()
82         while self.list.has_key(id):
83             id = self.newSessionId()
84         try:
85             session = Session(id, uri, dest)
86         except VlcError:
87             print "Streamer: creation failed"
88             return None
89         self.list[id] = session
90         print "New session: " + id
91         return id
92         
93     def getSession(self, id):
94         "Get a session from its session id"
95         if self.list.has_key(id):
96             return self.list[id]
97         else:
98             return None
99
100     def delSession(self, id):
101         "Delete a session"
102         if self.list.has_key(id):
103             del self.list[id]
104             return 0
105         else:
106             return -1
107
108