]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/override.py
phonon: Allow building against kdesupport libphonon.
[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 Media:
172     def add_options(self, *list_of_options):
173         """Add a list of options to the media.
174
175         Options must be written without the double-dash, e.g.:
176         m.add_options('sub-filter=marq@test{marquee=Hello}', 'video-filter=invert')
177
178         Note that you also can directly pass these options in the Instance.media_new method:
179         m=instance.media_new( 'foo.avi', 'sub-filter=marq@test{marquee=Hello}', 'video-filter=invert')
180         """
181         for o in list_of_options:
182             self.add_option(o)
183
184 class MediaPlayer:
185     """Create a new MediaPlayer instance.
186
187     It may take as parameter either:
188       - a string (media URI). In this case, a vlc.Instance will be created.
189       - a vlc.Instance
190     """
191     def __new__(cls, *p):
192         if p and p[0] == 0:
193             return None
194         elif p and isinstance(p[0], (int, long)):
195             # instance creation from ctypes
196             o=object.__new__(cls)
197             o._as_parameter_=ctypes.c_void_p(p[0])
198             return o
199
200         if p and isinstance(p[0], Instance):
201             return p[0].media_player_new()
202         else:
203             i=Instance()
204             o=i.media_player_new()
205             if p:
206                 o.set_media(i.media_new(p[0]))
207             return o
208
209     def get_instance(self):
210         """Return the associated vlc.Instance.
211         """
212         return self._instance
213
214 class MediaListPlayer:
215     """Create a new MediaPlayer instance.
216
217     It may take as parameter either:
218       - a vlc.Instance
219       - nothing
220     """
221     def __new__(cls, *p):
222         if p and p[0] == 0:
223             return None
224         elif p and isinstance(p[0], (int, long)):
225             # instance creation from ctypes
226             o=object.__new__(cls)
227             o._as_parameter_=ctypes.c_void_p(p[0])
228             return o
229         elif len(p) == 1 and isinstance(p[0], (tuple, list)):
230             p=p[0]
231
232         if p and isinstance(p[0], Instance):
233             return p[0].media_list_player_new()
234         else:
235             i=Instance()
236             o=i.media_list_player_new()
237             return o
238
239     def get_instance(self):
240         """Return the associated vlc.Instance.
241         """
242         return self._instance
243
244 class LogIterator:
245     def __iter__(self):
246         return self
247
248     def next(self):
249         if not self.has_next():
250             raise StopIteration
251         buf=LogMessage()
252         e=VLCException()
253         ret=libvlc_log_iterator_next(self, buf, e)
254         return ret.contents
255
256 class Log:
257     def __iter__(self):
258         return self.get_iterator()
259
260     def dump(self):
261         return [ str(m) for m in self ]