]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/header.py
python-cytpes: fix MediaControl API support
[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 ctypes
37 import sys
38
39 build_date="This will be replaced by the build date"
40
41 if sys.platform == 'linux2':
42     dll=ctypes.CDLL('libvlc.so')
43 elif sys.platform == 'win32':
44     import ctypes.util
45     import os
46     plugin_path=None
47     path=ctypes.util.find_library('libvlc.dll')
48     if path is None:
49         # Try a standard location.
50         p='c:\\Program Files\\VideoLAN\\VLC\\libvlc.dll'
51         if os.path.exists(p):
52             plugin_path=os.path.dirname(p)
53             os.chdir(plugin_path)
54         # If chdir failed, this will not work and raise an exception
55         path='libvlc.dll'
56     else:
57         plugin_path=os.path.dirname(path)
58     dll=ctypes.CDLL(path)
59 elif sys.platform == 'darwin':
60     # FIXME: should find a means to configure path
61     dll=ctypes.CDLL('/Applications/VLC.app/Contents/MacOS/lib/libvlc.2.dylib')
62
63 class ListPOINTER(object):
64     '''Just like a POINTER but accept a list of ctype as an argument.
65     '''
66     def __init__(self, etype):
67         self.etype = etype
68
69     def from_param(self, param):
70         if isinstance(param, (list,tuple)):
71             return (self.etype * len(param))(*param)
72
73 class LibVLCException(Exception):
74     """Python exception raised by libvlc methods.
75     """
76     pass
77
78 # From libvlc_structures.h
79 class VLCException(ctypes.Structure):
80     """libvlc exception.
81     """
82     _fields_= [
83                 ('raised', ctypes.c_int),
84                 ('code', ctypes.c_int),
85                 ('message', ctypes.c_char_p),
86                 ]
87     def init(self):
88         libvlc_exception_init(self)
89
90     def clear(self):
91         libvlc_exception_clear(self)
92
93 class PlaylistItem(ctypes.Structure):
94     _fields_= [
95                 ('id', ctypes.c_int),
96                 ('uri', ctypes.c_char_p),
97                 ('name', ctypes.c_char_p),
98                 ]
99
100     def __str__(self):
101         return "PlaylistItem #%d %s (%uri)" % (self.id, self.name, self.uri)
102
103 class LogMessage(ctypes.Structure):
104     _fields_= [
105                 ('size', ctypes.c_uint),
106                 ('severity', ctypes.c_int),
107                 ('type', ctypes.c_char_p),
108                 ('name', ctypes.c_char_p),
109                 ('header', ctypes.c_char_p),
110                 ('message', ctypes.c_char_p),
111                 ]
112
113     def __str__(self):
114         return "vlc.LogMessage(%d:%s): %s" % (self.severity, self.type, self.message)
115
116 class MediaControlPosition(ctypes.Structure):
117     _fields_= [
118                 ('origin', ctypes.c_int),
119                 ('key', ctypes.c_int),
120                 ('value', ctypes.c_longlong),
121                 ]
122
123     def __str__(self):
124         return "MediaControlPosition %ld (%s, %s)" % (
125             self.value,
126             str(PositionOrigin(self.origin)),
127             str(PositionKey(self.key))
128             )
129
130     @staticmethod
131     def from_param(arg):
132         if isinstance(arg, (int, long)):
133             p=MediaControlPosition()
134             p.value=arg
135             p.key=2
136             return p
137         else:
138             return arg
139
140 class MediaControlException(ctypes.Structure):
141     _fields_= [
142                 ('code', ctypes.c_int),
143                 ('message', ctypes.c_char_p),
144                 ]
145     def init(self):
146         mediacontrol_exception_init(self)
147
148     def clear(self):
149         mediacontrol_exception_free(self)
150
151 class MediaControlStreamInformation(ctypes.Structure):
152     _fields_= [
153                 ('status', ctypes.c_int),
154                 ('url', ctypes.c_char_p),
155                 ('position', ctypes.c_longlong),
156                 ('length', ctypes.c_longlong),
157                 ]
158
159     def __str__(self):
160         return "%s (%s) : %ld / %ld" % (self.url,
161                                         str(PlayerStatus(self.status)),
162                                         self.position,
163                                         self.length)
164
165 class RGBPicture(ctypes.Structure):
166     _fields_= [
167                 ('width', ctypes.c_int),
168                 ('height', ctypes.c_int),
169                 ('type', ctypes.c_uint32),
170                 ('date', ctypes.c_ulonglong),
171                 ('size', ctypes.c_int),
172                 ('data_pointer', ctypes.c_void_p),
173                 ]
174
175     @property
176     def data(self):
177         return ctypes.string_at(self.data_pointer, self.size)
178
179     def __str__(self):
180         return "RGBPicture (%d, %d) - %ld ms - %d bytes" % (self.width, self.height, self.date, self.size)
181
182     def free(self):
183         mediacontrol_RGBPicture__free(self)
184
185 def check_vlc_exception(result, func, args):
186     """Error checking method for functions using an exception in/out parameter.
187     """
188     ex=args[-1]
189     # Take into account both VLCException and MediacontrolException:
190     c=getattr(ex, 'raised', getattr(ex, 'code', 0))
191     if c:
192         raise LibVLCException(args[-1].message)
193     return result
194
195 ### End of header.py ###