]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/header.py
cfe047b0e9375c5e2ff58402c02b1855ade12a18
[vlc] / bindings / python-ctypes / header.py
1 #! /usr/bin/python
2
3 #
4 # Python ctypes bindings for VLC
5 # Copyright (C) 2009 the VideoLAN team
6 # $Id: $
7 #
8 # Authors: Olivier Aubert <olivier.aubert at liris.cnrs.fr>
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 #
24
25 """This module provides bindings for the
26 U{libvlc<http://wiki.videolan.org/ExternalAPI>} and
27 U{MediaControl<http://wiki.videolan.org/MediaControlAPI>} APIs.
28
29 You can find documentation at U{http://www.advene.org/download/python-ctypes/}.
30
31 Basically, the most important class is L{Instance}, which is used to
32 create a libvlc Instance. From this instance, you can then create
33 L{MediaPlayer} and L{MediaListPlayer} instances.
34 """
35
36 import logging
37 import ctypes
38 import sys
39
40 build_date="This will be replaced by the build date"
41
42 if sys.platform == 'linux2':
43     dll=ctypes.CDLL('libvlc.so')
44 elif sys.platform == 'win32':
45     import ctypes.util
46     import os
47     plugin_path=None
48     path=ctypes.util.find_library('libvlc.dll')
49     if path is None:
50         # Try to use registry settings
51         import _winreg
52         plugin_path_found = None
53         subkey, name = 'Software\\VideoLAN\\VLC','InstallDir'
54         for hkey in _winreg.HKEY_LOCAL_MACHINE, _winreg.HKEY_CURRENT_USER:
55             try:
56                 reg = _winreg.OpenKey(hkey, subkey)
57                 plugin_path_found, type_id = _winreg.QueryValueEx(reg, name)
58                 _winreg.CloseKey(reg)
59                 break
60             except _winreg.error:
61                 pass
62         if plugin_path_found:
63             plugin_path = plugin_path_found
64         else:
65             # Try a standard location.
66             p='c:\\Program Files\\VideoLAN\\VLC\\libvlc.dll'
67             if os.path.exists(p):
68                 plugin_path=os.path.dirname(p)
69         os.chdir(plugin_path)
70         # If chdir failed, this will not work and raise an exception
71         path='libvlc.dll'
72     else:
73         plugin_path=os.path.dirname(path)
74     dll=ctypes.CDLL(path)
75 elif sys.platform == 'darwin':
76     # FIXME: should find a means to configure path
77     dll=ctypes.CDLL('/Applications/VLC.app/Contents/MacOS/lib/libvlc.2.dylib')
78
79 class ListPOINTER(object):
80     '''Just like a POINTER but accept a list of ctype as an argument.
81     '''
82     def __init__(self, etype):
83         self.etype = etype
84
85     def from_param(self, param):
86         if isinstance(param, (list,tuple)):
87             return (self.etype * len(param))(*param)
88
89 class LibVLCException(Exception):
90     """Python exception raised by libvlc methods.
91     """
92     pass
93
94 # From libvlc_structures.h
95
96 # This is version-dependent, depending on the presence of libvlc_exception_get_message.
97
98 if hasattr(dll, 'libvlc_exception_get_message'):
99     # New-style message passing
100     class VLCException(ctypes.Structure):
101         """libvlc exception.
102         """
103         _fields_= [
104                     ('raised', ctypes.c_int),
105                     ]
106
107         @property
108         def message(self):
109             return dll.libvlc_exception_get_message()
110
111         def init(self):
112             libvlc_exception_init(self)
113
114         def clear(self):
115             libvlc_exception_clear(self)
116 else:
117     # Old-style exceptions
118     class VLCException(ctypes.Structure):
119         """libvlc exception.
120         """
121         _fields_= [
122                     ('raised', ctypes.c_int),
123                     ('code', ctypes.c_int),
124                     ('message', ctypes.c_char_p),
125                     ]
126         def init(self):
127             libvlc_exception_init(self)
128
129         def clear(self):
130             libvlc_exception_clear(self)
131
132 class PlaylistItem(ctypes.Structure):
133     _fields_= [
134                 ('id', ctypes.c_int),
135                 ('uri', ctypes.c_char_p),
136                 ('name', ctypes.c_char_p),
137                 ]
138
139     def __str__(self):
140         return "PlaylistItem #%d %s (%uri)" % (self.id, self.name, self.uri)
141
142 class LogMessage(ctypes.Structure):
143     _fields_= [
144                 ('size', ctypes.c_uint),
145                 ('severity', ctypes.c_int),
146                 ('type', ctypes.c_char_p),
147                 ('name', ctypes.c_char_p),
148                 ('header', ctypes.c_char_p),
149                 ('message', ctypes.c_char_p),
150                 ]
151
152     def __str__(self):
153         return "vlc.LogMessage(%d:%s): %s" % (self.severity, self.type, self.message)
154
155 class MediaControlPosition(ctypes.Structure):
156     _fields_= [
157                 ('origin', ctypes.c_int),
158                 ('key', ctypes.c_int),
159                 ('value', ctypes.c_longlong),
160                 ]
161
162     def __str__(self):
163         return "MediaControlPosition %ld (%s, %s)" % (
164             self.value,
165             str(PositionOrigin(self.origin)),
166             str(PositionKey(self.key))
167             )
168
169     @staticmethod
170     def from_param(arg):
171         if isinstance(arg, (int, long)):
172             p=MediaControlPosition()
173             p.value=arg
174             p.key=2
175             return p
176         else:
177             return arg
178
179 class MediaControlException(ctypes.Structure):
180     _fields_= [
181                 ('code', ctypes.c_int),
182                 ('message', ctypes.c_char_p),
183                 ]
184     def init(self):
185         mediacontrol_exception_init(self)
186
187     def clear(self):
188         mediacontrol_exception_free(self)
189
190 class MediaControlStreamInformation(ctypes.Structure):
191     _fields_= [
192                 ('status', ctypes.c_int),
193                 ('url', ctypes.c_char_p),
194                 ('position', ctypes.c_longlong),
195                 ('length', ctypes.c_longlong),
196                 ]
197
198     def __str__(self):
199         return "%s (%s) : %ld / %ld" % (self.url,
200                                         str(PlayerStatus(self.status)),
201                                         self.position,
202                                         self.length)
203
204 class RGBPicture(ctypes.Structure):
205     _fields_= [
206                 ('width', ctypes.c_int),
207                 ('height', ctypes.c_int),
208                 ('type', ctypes.c_uint32),
209                 ('date', ctypes.c_ulonglong),
210                 ('size', ctypes.c_int),
211                 ('data_pointer', ctypes.c_void_p),
212                 ]
213
214     @property
215     def data(self):
216         return ctypes.string_at(self.data_pointer, self.size)
217
218     def __str__(self):
219         return "RGBPicture (%d, %d) - %ld ms - %d bytes" % (self.width, self.height, self.date, self.size)
220
221     def free(self):
222         mediacontrol_RGBPicture__free(self)
223
224 def check_vlc_exception(result, func, args):
225     """Error checking method for functions using an exception in/out parameter.
226     """
227     ex=args[-1]
228     if not isinstance(ex, (VLCException, MediaControlException)):
229         logging.warn("python-vlc: error when processing function %s. Please report this as a bug to vlc-devel@videolan.org" % str(func))
230         return result
231     # Take into account both VLCException and MediacontrolException:
232     c=getattr(ex, 'raised', getattr(ex, 'code', 0))
233     if c:
234         raise LibVLCException(ex.message)
235     return result
236
237 ### End of header.py ###