4 # Python ctypes bindings for VLC
5 # Copyright (C) 2009 the VideoLAN team
8 # Authors: Olivier Aubert <olivier.aubert at liris.cnrs.fr>
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.
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.
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.
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.
29 You can find documentation at U{http://www.advene.org/download/python-ctypes/}.
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.
40 build_date="This will be replaced by the build date"
42 if sys.platform == 'linux2':
43 dll=ctypes.CDLL('libvlc.so')
44 elif sys.platform == 'win32':
48 path=ctypes.util.find_library('libvlc.dll')
50 # Try to use registry settings
52 plugin_path_found = None
53 subkey, name = 'Software\\VideoLAN\\VLC','InstallDir'
54 for hkey in _winreg.HKEY_LOCAL_MACHINE, _winreg.HKEY_CURRENT_USER:
56 reg = _winreg.OpenKey(hkey, subkey)
57 plugin_path_found, type_id = _winreg.QueryValueEx(reg, name)
63 plugin_path = plugin_path_found
65 # Try a standard location.
66 p='c:\\Program Files\\VideoLAN\\VLC\\libvlc.dll'
68 plugin_path=os.path.dirname(p)
70 # If chdir failed, this will not work and raise an exception
73 plugin_path=os.path.dirname(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')
80 # Generated enum types.
86 # End of generated enum types.
89 class ListPOINTER(object):
90 '''Just like a POINTER but accept a list of ctype as an argument.
92 def __init__(self, etype):
95 def from_param(self, param):
96 if isinstance(param, (list,tuple)):
97 return (self.etype * len(param))(*param)
99 class LibVLCException(Exception):
100 """Python exception raised by libvlc methods.
104 # From libvlc_structures.h
106 # This is version-dependent, depending on the presence of libvlc_errmsg
108 if hasattr(dll, 'libvlc_errmsg'):
109 # New-style message passing
110 class VLCException(ctypes.Structure):
114 ('raised', ctypes.c_int),
119 return dll.libvlc_errmsg()
122 libvlc_exception_init(self)
125 libvlc_exception_clear(self)
127 # Old-style exceptions
128 class VLCException(ctypes.Structure):
132 ('raised', ctypes.c_int),
133 ('code', ctypes.c_int),
134 ('message', ctypes.c_char_p),
137 libvlc_exception_init(self)
140 libvlc_exception_clear(self)
142 class PlaylistItem(ctypes.Structure):
144 ('id', ctypes.c_int),
145 ('uri', ctypes.c_char_p),
146 ('name', ctypes.c_char_p),
150 return "PlaylistItem #%d %s (%uri)" % (self.id, self.name, self.uri)
152 class LogMessage(ctypes.Structure):
154 ('size', ctypes.c_uint),
155 ('severity', ctypes.c_int),
156 ('type', ctypes.c_char_p),
157 ('name', ctypes.c_char_p),
158 ('header', ctypes.c_char_p),
159 ('message', ctypes.c_char_p),
163 self.size=ctypes.sizeof(self)
166 return "vlc.LogMessage(%d:%s): %s" % (self.severity, self.type, self.message)
168 class MediaControlPosition(ctypes.Structure):
170 ('origin', PositionOrigin),
171 ('key', PositionKey),
172 ('value', ctypes.c_longlong),
175 def __init__(self, value=0, origin=None, key=None):
176 # We override the __init__ method so that instanciating the
177 # class with an int as parameter will create the appropriate
178 # default position (absolute position, media time, with the
182 origin=PositionOrigin.AbsolutePosition
184 key=PositionKey.MediaTime
189 return "MediaControlPosition %ld (%s, %s)" % (
197 if isinstance(arg, (int, long)):
198 p=MediaControlPosition(arg)
203 class MediaControlException(ctypes.Structure):
205 ('code', ctypes.c_int),
206 ('message', ctypes.c_char_p),
209 mediacontrol_exception_init(self)
212 mediacontrol_exception_free(self)
214 class MediaControlStreamInformation(ctypes.Structure):
216 ('status', PlayerStatus),
217 ('url', ctypes.c_char_p),
218 ('position', ctypes.c_longlong),
219 ('length', ctypes.c_longlong),
223 return "%s (%s) : %ld / %ld" % (self.url or "<No defined URL>",
228 class RGBPicture(ctypes.Structure):
230 ('width', ctypes.c_int),
231 ('height', ctypes.c_int),
232 ('type', ctypes.c_uint32),
233 ('date', ctypes.c_ulonglong),
234 ('size', ctypes.c_int),
235 ('data_pointer', ctypes.c_void_p),
240 return ctypes.string_at(self.data_pointer, self.size)
243 return "RGBPicture (%d, %d) - %ld ms - %d bytes" % (self.width, self.height, self.date, self.size)
246 mediacontrol_RGBPicture__free(self)
248 def check_vlc_exception(result, func, args):
249 """Error checking method for functions using an exception in/out parameter.
252 if not isinstance(ex, (VLCException, MediaControlException)):
253 logging.warn("python-vlc: error when processing function %s. Please report this as a bug to vlc-devel@videolan.org" % str(func))
255 # Take into account both VLCException and MediacontrolException:
256 c=getattr(ex, 'raised', getattr(ex, 'code', 0))
258 raise LibVLCException(ex.message)
261 ### End of header.py ###