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