Commit f83867bb authored by Olivier Aubert's avatar Olivier Aubert

New python bindings, using ctypes, automatically generated from include files.

parent c3f18518
MODULE_NAME=vlc.py
all: $(MODULE_NAME)
$(MODULE_NAME): generate.py header.py override.py ../../include/vlc/*.h
./generate.py ../../include/vlc/*.h > $@
doc: $(MODULE_NAME)
-epydoc -v -o doc $<
clean:
-$(RM) $(MODULE_NAME)
* Python ctypes-based bindings
The bindings use ctypes to directly call the libvlc dynamic lib, and
the code is generated from the include files defining the public API.
** Building
To generate the vlc.py module and its documentation, use
make
Documentation building needs epydoc.
** Layout
The module offers two ways of accessing the API - a raw access to all
exported methods, and more convenient wrapper classes :
- Raw access: methods are available as attributes of the vlc
module. Use their docstring (introspective shells like ipython are
your friends) to explore them.
- Wrapper classes: most major structures of the libvlc API (Instance,
Media, MediaPlayer, etc) are wrapped as classes, with shorter method
names.
** Using the module
On win32, the simplest way is to put the vlc.py file in the same
directory as the libvlc.dll file (standard location:
c:\Program Files\VideoLAN\VLC ).
- Using raw access:
>>> import vlc
>>> vlc.libvlc_get_version()
'1.0.0 Goldeneye'
>>> e=vlc.VLCException()
>>> i=vlc.libvlc_new(0, [], e)
>>> i
<vlc.Instance object at 0x8384a4c>
>>> vlc.libvlc_audio_get_volume(i,e)
50
- Using wrapper classes:
>>> i=vlc.Instance.new()
>>> i.audio_get_volume()
50
>>> m=i.media_new('/tmp/foo.avi')
>>> m.get_mrl()
'/tmp/foo.avi'
* Check cross-platform (win32, MacOSX) support
* Investigate memory management
* Find how to properly define enums
* Autogenerate enums from include files
* Implement event callbacks
* Write a test suite
This diff is collapsed.
#! /usr/bin/python
#
# Python ctypes bindings for VLC
# Copyright (C) 2009 the VideoLAN team
# $Id: $
#
# Authors: Olivier Aubert <olivier.aubert at liris.cnrs.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#
import ctypes
import sys
build_date="This will be replaced by the build date"
if sys.platform == 'linux2':
dll=ctypes.CDLL('libvlc.so')
elif sys.platform == 'win32':
dll=ctypes.CDLL('libvlc.dll')
elif sys.platform == 'darwin':
# FIXME: should find a means to configure path
dll=ctypes.CDLL('/Applications/VLC.app/Contents/MacOS/lib/libvlc.2.dylib')
class ListPOINTER(object):
'''Just like a POINTER but accept a list of ctype as an argument.
'''
def __init__(self, etype):
self.etype = etype
def from_param(self, param):
if isinstance(param, (list,tuple)):
return (self.etype * len(param))(*param)
# From libvlc_structures.h
class VLCException(ctypes.Structure):
_fields_= [
('raised', ctypes.c_int),
('code', ctypes.c_int),
('message', ctypes.c_char_p),
]
def init(self):
libvlc_exception_init(self)
def clear(self):
libvlc_exception_clear(self)
class PlaylistItem(ctypes.Structure):
_fields_= [
('id', ctypes.c_int),
('uri', ctypes.c_char_p),
('name', ctypes.c_char_p),
]
class LogMessage(ctypes.Structure):
_fields_= [
('size', ctypes.c_uint),
('severity', ctypes.c_int),
('type', ctypes.c_char_p),
('name', ctypes.c_char_p),
('header', ctypes.c_char_p),
('message', ctypes.c_char_p),
]
class MediaControlPosition(ctypes.Structure):
_fields_= [
('origin', ctypes.c_ushort),
('key', ctypes.c_ushort),
('value', ctypes.c_longlong),
]
@staticmethod
def from_param(arg):
if isinstance(arg, (int, long)):
p=MediaControlPosition()
p.value=arg
p.key=2
return p
else:
return arg
class MediaControlPositionOrigin(ctypes.c_uint):
enum=(
'AbsolutePosition',
'RelativePosition',
'ModuloPosition',
)
def __repr__(self):
return self.enum[self.value]
class State(ctypes.c_uint):
# FIXME: should be improved (State.NothingSpecial should hold the value)
# and maybe auto-generated from typedefs
enum=(
'NothingSpecial',
'Opening',
'Buffering',
'Playing',
'Paused',
'Stopped',
'Ended',
'Error',
)
def __repr__(self):
return self.enum[self.value]
class MediaControlException(ctypes.Structure):
_fields_= [
('code', ctypes.c_int),
('message', ctypes.c_char_p),
]
def init(self):
mediacontrol_exception_init(self)
def clear(self):
mediacontrol_exception_free(self)
class MediaControlStreamInformation(ctypes.Structure):
_fields_= [
('code', ctypes.c_int),
('message', ctypes.c_char_p),
]
class RGBPicture(ctypes.Structure):
_fields_= [
('width', ctypes.c_int),
('height', ctypes.c_int),
('type', ctypes.c_uint32),
('date', ctypes.c_longlong),
('size', ctypes.c_int),
('data', ctypes.c_char_p),
]
def free(self):
mediacontrol_RGBPicture__free(self)
# Decorator for callback methods
callbackmethod=ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p)
def check_vlc_exception(result, func, args):
"""Error checking method for functions using an exception in/out parameter.
"""
ex=args[-1]
# Take into account both VLCException and MediacontrolException
c=getattr(ex, 'raised', getattr(ex, 'code', 0))
if c:
raise Exception(args[-1].message)
return result
class Instance:
@staticmethod
def new(*p):
"""Create a new Instance.
"""
e=VLCException()
return libvlc_new(len(p), p, e)
class MediaControl:
@staticmethod
def new(*p):
"""Create a new MediaControl
"""
e=MediaControlException()
return mediacontrol_new(len(p), p, e)
@staticmethod
def new_from_instance(i):
"""Create a new MediaControl from an existing Instance.
"""
e=MediaControlException()
return mediacontrol_new_from_instance(i, e)
class MediaList:
def __len__(self):
e=VLCException()
return libvlc_media_list_count(self, e)
def __getitem__(self, i):
e=VLCException()
return libvlc_media_list_item_at_index(self, i, e)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment