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