]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/override.py
729f5002cbd93458f2d3f5a7e371cf6173317c92
[vlc] / bindings / python-ctypes / override.py
1 class Instance:
2     """Create a new Instance instance.
3
4     It may take as parameter either:
5       - a string
6       - a list of strings as first parameters
7       - the parameters given as the constructor parameters (must be strings)
8       - a MediaControl instance
9     """
10     def __new__(cls, *p):
11         if p and p[0] == 0:
12             return None
13         elif p and isinstance(p[0], (int, long)):
14             # instance creation from ctypes
15             o=object.__new__(cls)
16             o._as_parameter_=ctypes.c_void_p(p[0])
17             return o
18         elif len(p) == 1 and isinstance(p[0], basestring):
19             # Only 1 string parameter: should be a parameter line
20             p=p[0].split(' ')
21         elif len(p) == 1 and isinstance(p[0], (tuple, list)):
22             p=p[0]
23
24         if p and isinstance(p[0], MediaControl):
25             return p[0].get_instance()
26         else:
27             e=VLCException()
28             return libvlc_new(len(p), p, e)
29
30     def media_player_new(self, uri=None):
31         """Create a new Media Player object.
32
33         @param uri: an optional URI to play in the player.
34         """
35         e=VLCException()
36         p=libvlc_media_player_new(self, e)
37         if uri:
38             p.set_media(self.media_new(uri))
39         p._instance=self
40         return p
41
42     def media_list_player_new(self):
43         """Create an empty Media Player object
44         """
45         e=VLCException()
46         p=libvlc_media_list_player_new(self, e)
47         p._instance=self
48         return p
49
50 class MediaControl:
51     """Create a new MediaControl instance
52
53     It may take as parameter either:
54       - a string
55       - a list of strings as first parameters
56       - the parameters given as the constructor parameters (must be strings)
57       - a vlc.Instance
58     """
59     def __new__(cls, *p):
60         if p and p[0] == 0:
61             return None
62         elif p and isinstance(p[0], (int, long)):
63             # instance creation from ctypes
64             o=object.__new__(cls)
65             o._as_parameter_=ctypes.c_void_p(p[0])
66             return o
67         elif len(p) == 1 and isinstance(p[0], basestring):
68             # Only 1 string parameter: should be a parameter line
69             p=p[0].split(' ')
70         elif len(p) == 1 and isinstance(p[0], (tuple, list)):
71             p=p[0]
72
73         if p and isinstance(p[0], Instance):
74             e=MediaControlException()
75             return mediacontrol_new_from_instance(p[0], e)
76         else:
77             e=MediaControlException()
78             return mediacontrol_new(len(p), p, e)
79
80     def get_media_position(self, origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime):
81         e=MediaControlException()
82         p=mediacontrol_get_media_position(self, origin, key, e)
83         if p:
84             return p.contents
85         else:
86             return None
87
88     def set_media_position(self, pos):
89         """Set the media position.
90
91         @param pos: a MediaControlPosition or an integer (in ms)
92         """
93         if not isinstance(pos, MediaControlPosition):
94             pos=MediaControlPosition(long(pos))
95         e=MediaControlException()
96         mediacontrol_set_media_position(self, pos, e)
97
98     def start(self, pos=0):
99         """Start the player at the given position.
100
101         @param pos: a MediaControlPosition or an integer (in ms)
102         """
103         if not isinstance(pos, MediaControlPosition):
104             pos=MediaControlPosition(long(pos))
105         e=MediaControlException()
106         mediacontrol_start(self, pos, e)
107
108     def snapshot(self, pos=0):
109         """Take a snapshot.
110
111         Note: the position parameter is not properly implemented. For
112         the moment, the only valid position is the 0-relative position
113         (i.e. the current position).
114
115         @param pos: a MediaControlPosition or an integer (in ms)
116         """
117         if not isinstance(pos, MediaControlPosition):
118             pos=MediaControlPosition(long(pos))
119         e=MediaControlException()
120         p=mediacontrol_snapshot(self, pos, e)
121         if p:
122             snap=p.contents
123             # FIXME: there is a bug in the current mediacontrol_snapshot
124             # implementation, which sets an incorrect date.
125             # Workaround here:
126             snap.date=self.get_media_position().value
127             return snap
128         else:
129             return None
130
131     def display_text(self, message='', begin=0, end=1000):
132         """Display a caption between begin and end positions.
133
134         @param message: the caption to display
135         @param begin: the begin position
136         @param end: the end position
137         """
138         if not isinstance(begin, MediaControlPosition):
139             begin=self.value2position(pos)
140         if not isinstance(end, MediaControlPosition):
141             end=self.value2position(end)
142         e=MediaControlException()
143         mediacontrol_display_text(self, message, begin, end, e)
144
145     def get_stream_information(self, key=PositionKey.MediaTime):
146         """Return information about the stream.
147         """
148         e=MediaControlException()
149         return mediacontrol_get_stream_information(self, key, e).contents
150
151 class MediaPlayer:
152     """Create a new MediaPlayer instance.
153
154     It may take as parameter either:
155       - a string (media URI). In this case, a vlc.Instance will be created.
156       - a vlc.Instance
157     """
158     def __new__(cls, *p):
159         if p and p[0] == 0:
160             return None
161         elif p and isinstance(p[0], (int, long)):
162             # instance creation from ctypes
163             o=object.__new__(cls)
164             o._as_parameter_=ctypes.c_void_p(p[0])
165             return o
166
167         if p and isinstance(p[0], Instance):
168             return p[0].media_player_new()
169         else:
170             i=Instance()
171             o=i.media_player_new()
172             if p:
173                 o.set_media(i.media_new(p[0]))
174             return o
175
176     def get_instance(self):
177         """Return the associated vlc.Instance.
178         """
179         return self._instance
180
181 class MediaListPlayer:
182     """Create a new MediaPlayer instance.
183
184     It may take as parameter either:
185       - a vlc.Instance
186       - nothing
187     """
188     def __new__(cls, *p):
189         if p and p[0] == 0:
190             return None
191         elif p and isinstance(p[0], (int, long)):
192             # instance creation from ctypes
193             o=object.__new__(cls)
194             o._as_parameter_=ctypes.c_void_p(p[0])
195             return o
196         elif len(p) == 1 and isinstance(p[0], (tuple, list)):
197             p=p[0]
198
199         if p and isinstance(p[0], Instance):
200             return p[0].media_list_player_new()
201         else:
202             i=Instance()
203             o=i.media_list_player_new()
204             return o
205
206     def get_instance(self):
207         """Return the associated vlc.Instance.
208         """
209         return self._instance
210
211 class LogIterator:
212     def __iter__(self):
213         return self
214
215     def next(self):
216         if not self.has_next():
217             raise StopIteration
218         buf=LogMessage()
219         e=VLCException()
220         ret=libvlc_log_iterator_next(self, buf, e)
221         return ret.contents
222
223 class Log:
224     def __iter__(self):
225         return self.get_iterator()
226
227     def dump(self):
228         return [ str(m) for m in self ]