]> git.sesse.net Git - vlc/blob - bindings/python/vlc_media.c
libvlcpp: fix the compilation and throw an exception if the constructor fail.
[vlc] / bindings / python / vlc_media.c
1 /*****************************************************************************
2  * vlc_media.c: vlc.Media binding
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <olivier.aubert at liris.cnrs.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include "vlcglue.h"
24
25 /***********************************************************************
26  * vlc.Media
27  ***********************************************************************/
28
29 static PyObject *
30 vlcMedia_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
31 {
32     fprintf(stderr, "vlcMedia_new called\n");
33     PyErr_SetString( PyExc_TypeError, "vlc.Media can be instantiated by itself. You should use vlc.Instance().media_new(mrl)." );
34     return NULL;
35 }
36
37 static void
38 vlcMedia_dealloc( PyObject *self )
39 {
40     libvlc_media_release( LIBVLC_MEDIA(self) );
41     PyObject_DEL( self );
42 }
43
44 static PyObject *
45 vlcMedia_add_option( PyObject *self, PyObject *args )
46 {
47     libvlc_exception_t ex;
48     char* psz_options = NULL;
49
50     if( !PyArg_ParseTuple( args, "s", &psz_options ) )
51         return NULL;
52
53     LIBVLC_TRY;
54     libvlc_media_add_option( LIBVLC_MEDIA(self), psz_options, &ex);
55     LIBVLC_EXCEPT;
56     Py_INCREF( Py_None );
57     return Py_None;
58 }
59
60 static PyObject *
61 vlcMedia_get_mrl( PyObject *self, PyObject *args )
62 {
63     libvlc_exception_t ex;
64     char * psz_mrl;
65     PyObject * o_ret;
66
67     LIBVLC_TRY;
68     psz_mrl = libvlc_media_get_mrl( LIBVLC_MEDIA(self), &ex);
69     LIBVLC_EXCEPT;
70
71     o_ret = Py_BuildValue( "s", psz_mrl );
72     free( psz_mrl );
73     return o_ret;
74 }
75
76 static PyObject *
77 vlcMedia_get_state( PyObject *self, PyObject *args )
78 {
79     libvlc_exception_t ex;
80     libvlc_state_t i_state;
81
82     LIBVLC_TRY;
83     i_state = libvlc_media_get_state( LIBVLC_MEDIA(self), &ex);
84     LIBVLC_EXCEPT;
85     /* FIXME: return the defined state constant */
86     return Py_BuildValue( "i", i_state );
87 }
88
89 static PyObject *
90 vlcMedia_get_duration( PyObject *self, PyObject *args )
91 {
92     libvlc_exception_t ex;
93     libvlc_time_t i_ret;
94     LIBVLC_TRY;
95     i_ret = libvlc_media_get_duration( LIBVLC_MEDIA(self), &ex);
96     LIBVLC_EXCEPT;
97     return Py_BuildValue( "L", i_ret );
98 }
99
100 static PyObject *
101 vlcMedia_media_player_new( PyObject *self, PyObject *args )
102 {
103     libvlc_exception_t ex;
104     libvlc_media_player_t *p_mp;
105     vlcMediaPlayer *p_ret;
106
107     LIBVLC_TRY;
108     p_mp = libvlc_media_player_new_from_media( LIBVLC_MEDIA(self), &ex);
109     LIBVLC_EXCEPT;
110
111     p_ret = PyObject_New( vlcMediaPlayer, &vlcMediaPlayer_Type );
112     p_ret->p_mp = p_mp;
113     Py_INCREF( p_ret ); /* Ah bon ? */
114     return ( PyObject * )p_ret;
115 }
116
117 static PyObject *
118 vlcMedia_is_preparsed( PyObject *self, PyObject *args )
119 {
120     libvlc_exception_t ex;
121     int i_ret;
122     LIBVLC_TRY;
123     i_ret = libvlc_media_is_preparsed( LIBVLC_MEDIA(self), &ex);
124     LIBVLC_EXCEPT;
125     return Py_BuildValue( "L", i_ret );
126 }
127
128 static PyObject *
129 vlcMedia_get_meta( PyObject *self, PyObject *args )
130 {
131     libvlc_exception_t ex;
132     char * psz_meta = NULL;
133     char * psz_ret = NULL;
134     PyObject* o_ret;
135     int i_index = -1;
136     int i_loop = 0;
137     static const char * meta_names[] = { "Title", "Artist", "Genre", "Copyright", "Album", "TrackNumber", "Description", "Rating", "Date", "Setting", "URL", "Language", "NowPlaying", "Publisher", "EncodedBy", "ArtworkURL", "TrackID", NULL };
138
139     if( !PyArg_ParseTuple( args, "s", &psz_meta ) )
140         return NULL;
141     while( meta_names[i_loop] )
142     {
143         if( !strncmp(meta_names[i_loop], psz_meta, strlen(meta_names[i_loop])) )
144         {
145             i_index = i_loop;
146             break;
147         }
148         i_loop++;
149     }
150     if( i_index < 0 )
151     {
152         PyObject *py_exc = vlc_Exception;
153         PyErr_SetString( py_exc, "Unknown meta attribute" );
154         return NULL;
155     }
156
157     LIBVLC_TRY;
158     psz_ret = libvlc_media_get_meta( LIBVLC_MEDIA(self), i_index, &ex);
159     LIBVLC_EXCEPT;
160
161     o_ret = Py_BuildValue( "s", psz_ret );
162     free( psz_ret );
163     return o_ret;
164 }
165
166 static PyMethodDef vlcMedia_methods[] =
167 {
168     { "add_option", vlcMedia_add_option, METH_VARARGS,
169       "add_option(str) Add an option to the media." },
170     { "get_mrl", vlcMedia_get_mrl, METH_VARARGS,
171       "get_mrl() -> str" },
172     { "get_state", vlcMedia_get_state, METH_VARARGS,
173       "get_state() -> int" },
174     { "get_duration", vlcMedia_get_duration, METH_VARARGS,
175       "get_duration() -> int" },
176     { "mediaplayer_new", vlcMedia_media_player_new, METH_VARARGS,
177       "mediaplayer_new() -> vlc.MediaPlayer   Create a MediaPlayer object from a Media" },
178     { "is_preparsed", vlcMedia_is_preparsed, METH_VARARGS,
179       "is_preparsed() -> int" },
180     { "get_meta", vlcMedia_get_meta, METH_VARARGS,
181       "get_meta(str) -> str   Read the meta of the media." },
182
183     { NULL }  /* Sentinel */
184 };
185
186 static PyTypeObject vlcMedia_Type =
187 {
188     PyObject_HEAD_INIT( NULL )
189     0,                         /*ob_size*/
190     "vlc.Media",            /*tp_name*/
191     sizeof( vlcMedia_Type ),   /*tp_basicsize*/
192     0,                         /*tp_itemsize*/
193     vlcMedia_dealloc, /*tp_dealloc*/
194     0,                         /*tp_print*/
195     0,                         /*tp_getattr*/
196     0,                         /*tp_setattr*/
197     0,                         /*tp_compare*/
198     0,                         /*tp_repr*/
199     0,                         /*tp_as_number*/
200     0,                         /*tp_as_sequence*/
201     0,                         /*tp_as_mapping*/
202     0,                         /*tp_hash */
203     0,                         /*tp_call*/
204     0,                         /*tp_str*/
205     0,                         /*tp_getattro*/
206     0,                         /*tp_setattro*/
207     0,                         /*tp_as_buffer*/
208     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
209     "vlc.Media object.",  /* tp_doc */
210     0,                        /* tp_traverse */
211     0,                        /* tp_clear */
212     0,                         /* tp_richcompare */
213     0,                         /* tp_weaklistoffset */
214     0,                         /* tp_iter */
215     0,                          /* tp_iternext */
216     vlcMedia_methods,          /* tp_methods */
217     0,                         /* tp_members */
218     0,                         /* tp_getset */
219     0,                         /* tp_base */
220     0,                         /* tp_dict */
221     0,                         /* tp_descr_get */
222     0,                         /* tp_descr_set */
223     0,                         /* tp_dictoffset */
224     0,                         /* tp_init */
225     0,                         /* tp_alloc */
226     vlcMedia_new,              /* tp_new */
227 };
228