]> git.sesse.net Git - vlc/blob - python/vlrs/session.py
mediacontrol_audio_video.c: release vout in mediacontrol_snapshot()
[vlc] / 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         self.name = media['name']
24         address = "rtp/ts://" + dest
25         self.streamer = VlcStreamer(self.fileName, address)
26         
27     def play(self):
28         "Play this session"
29         if self.state == 'playing':
30             print "Session " + self.id + " (" + self.fileName + "): already playing"
31             return 0
32         self.state = 'playing'
33         print "Session " + self.id + " (" + self.fileName + "): play"
34         try:
35             self.streamer.play()
36         except VlcError:
37             print "Streamer: play failed"
38             return -1
39         cfg.announceList.addMulticastSession(self)
40         return 0
41
42     def pause(self):
43         "Pause this session"
44         print "Session " + self.id + " (" + self.fileName + "): pause"
45         self.state = 'ready'
46         try:
47             self.streamer.pause()
48         except VlcError:
49             print "Streamer: pause failed"
50             return -1
51         return 0
52
53     def stop(self):
54         "Stop this session"
55         print "Session " + self.id + " (" + self.fileName + "): stop"
56         try:
57             self.streamer.stop()
58         except VlcError:
59             print "Streamer: stop failed"
60             return -1
61         return 0
62
63
64
65 class SessionList:
66     "Manages RTSP sessions"
67
68     list = {}
69     chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
70
71     def __init__(self):
72         self.rand = random.Random(time.time())
73     
74     def newSessionId(self):
75         "Build a random session id"
76         id = ""
77         for x in range(12):
78             id += self.chars[self.rand.randrange(0, len(self.chars), 1)]
79         return id
80
81     def newSession(self, uri, dest):
82         "Create a new RTSP session"
83         id = self.newSessionId()
84         while self.list.has_key(id):
85             id = self.newSessionId()
86         try:
87             session = Session(id, uri, dest)
88         except VlcError:
89             print "Streamer: creation failed"
90             return None
91         self.list[id] = session
92         print "New session: " + id
93         return id
94         
95     def getSession(self, id):
96         "Get a session from its session id"
97         if self.list.has_key(id):
98             return self.list[id]
99         else:
100             return None
101
102     def delSession(self, id):
103         "Delete a session"
104         if self.list.has_key(id):
105             del self.list[id]
106             return 0
107         else:
108             return -1
109
110