]> git.sesse.net Git - vlc/blob - src/control/vlm.c
libvlc_event_manager: remove exceptions
[vlc] / src / control / vlm.c
1 /*****************************************************************************
2  * vlm.c: libvlc new API VLM handling functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
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
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <vlc/libvlc.h>
29 #include <vlc/libvlc_vlm.h>
30 #include <vlc_es.h>
31 #include <vlc_input.h>
32 #include <vlc_vlm.h>
33 #include <assert.h>
34
35 #include "libvlc_internal.h"
36
37 /* VLM events callback. Transmit to libvlc */
38 static int VlmEvent( vlc_object_t *p_this, const char * name,
39                      vlc_value_t old_val, vlc_value_t newval, void *param )
40 {
41     VLC_UNUSED(p_this);
42     VLC_UNUSED(name);
43     VLC_UNUSED(old_val);    
44     vlm_event_t *event = (vlm_event_t*)newval.p_address;
45     libvlc_event_manager_t *p_event_manager = (libvlc_event_manager_t *) param;
46     libvlc_event_t libvlc_event;
47
48     libvlc_event.u.vlm_media_event.psz_instance_name = NULL;
49     libvlc_event.u.vlm_media_event.psz_media_name = event->psz_name;
50
51     switch( event->i_type )
52     {
53     case VLM_EVENT_MEDIA_ADDED:
54         libvlc_event.type = libvlc_VlmMediaAdded;
55         break;
56     case VLM_EVENT_MEDIA_REMOVED:
57         libvlc_event.type = libvlc_VlmMediaRemoved;
58         break;
59     case VLM_EVENT_MEDIA_CHANGED:
60         libvlc_event.type = libvlc_VlmMediaChanged;
61         break;
62     case VLM_EVENT_MEDIA_INSTANCE_STARTED:
63         libvlc_event.type = libvlc_VlmMediaInstanceStarted;
64         break;
65     case VLM_EVENT_MEDIA_INSTANCE_STOPPED:
66         libvlc_event.type = libvlc_VlmMediaInstanceStopped;
67         break;
68     case VLM_EVENT_MEDIA_INSTANCE_STATE:
69         libvlc_event.u.vlm_media_event.psz_instance_name =
70             event->psz_instance_name;
71         switch( event->input_state )
72         {
73         case INIT_S:
74             libvlc_event.type = libvlc_VlmMediaInstanceStatusInit;
75             break;
76         case OPENING_S:
77             libvlc_event.type =
78                 libvlc_VlmMediaInstanceStatusOpening;
79             break;
80         case PLAYING_S:
81             libvlc_event.type =
82                 libvlc_VlmMediaInstanceStatusPlaying;
83             break;
84         case PAUSE_S:
85             libvlc_event.type = libvlc_VlmMediaInstanceStatusPause;
86             break;
87         case END_S:
88             libvlc_event.type = libvlc_VlmMediaInstanceStatusEnd;
89             break;
90         case ERROR_S:
91             libvlc_event.type = libvlc_VlmMediaInstanceStatusError;
92             break;
93         default:
94             return 0;
95         }
96         break;
97     default:
98         return 0;
99     }
100     libvlc_event_send( p_event_manager, &libvlc_event );
101     return 0;
102 }
103
104 static void libvlc_vlm_release_internal( libvlc_instance_t *p_instance )
105 {
106     vlm_t *p_vlm = p_instance->libvlc_vlm.p_vlm;
107     if( !p_instance->libvlc_vlm.p_vlm )
108         return;
109     /* We need to remove medias in order to receive events */
110     vlm_Control( p_vlm, VLM_CLEAR_MEDIAS );
111     vlm_Control( p_vlm, VLM_CLEAR_SCHEDULES );
112
113     var_DelCallback( (vlc_object_t *)p_vlm, "intf-event", VlmEvent,
114                      p_instance->libvlc_vlm.p_event_manager );
115     p_instance->libvlc_vlm.pf_release = NULL;
116     libvlc_event_manager_release( p_instance->libvlc_vlm.p_event_manager );
117     p_instance->libvlc_vlm.p_event_manager = NULL;
118     vlm_Delete( p_vlm );
119     p_instance->libvlc_vlm.p_vlm = NULL;
120 }
121
122 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
123                             libvlc_exception_t *p_exception )
124 {
125     if( !p_instance->libvlc_vlm.p_event_manager )
126     {
127         p_instance->libvlc_vlm.p_event_manager =
128             libvlc_event_manager_new( p_instance->libvlc_vlm.p_vlm, p_instance );
129         if( unlikely(p_instance->libvlc_vlm.p_event_manager == NULL) )
130             return VLC_ENOMEM;
131         libvlc_event_manager_register_event_type(
132             p_instance->libvlc_vlm.p_event_manager,
133             libvlc_VlmMediaAdded );
134         libvlc_event_manager_register_event_type(
135             p_instance->libvlc_vlm.p_event_manager,
136             libvlc_VlmMediaRemoved );
137         libvlc_event_manager_register_event_type(
138             p_instance->libvlc_vlm.p_event_manager,
139             libvlc_VlmMediaChanged );
140         libvlc_event_manager_register_event_type(
141             p_instance->libvlc_vlm.p_event_manager,
142             libvlc_VlmMediaInstanceStarted );
143         libvlc_event_manager_register_event_type(
144             p_instance->libvlc_vlm.p_event_manager,
145             libvlc_VlmMediaInstanceStopped );
146         libvlc_event_manager_register_event_type(
147             p_instance->libvlc_vlm.p_event_manager,
148             libvlc_VlmMediaInstanceStatusInit );
149         libvlc_event_manager_register_event_type(
150             p_instance->libvlc_vlm.p_event_manager,
151             libvlc_VlmMediaInstanceStatusOpening );
152         libvlc_event_manager_register_event_type(
153             p_instance->libvlc_vlm.p_event_manager,
154             libvlc_VlmMediaInstanceStatusPlaying );
155         libvlc_event_manager_register_event_type(
156             p_instance->libvlc_vlm.p_event_manager,
157             libvlc_VlmMediaInstanceStatusPause );
158         libvlc_event_manager_register_event_type(
159             p_instance->libvlc_vlm.p_event_manager,
160             libvlc_VlmMediaInstanceStatusEnd );
161         libvlc_event_manager_register_event_type(
162             p_instance->libvlc_vlm.p_event_manager,
163             libvlc_VlmMediaInstanceStatusError );
164     }
165
166     if( !p_instance->libvlc_vlm.p_vlm )
167     {
168         p_instance->libvlc_vlm.p_vlm = vlm_New( p_instance->p_libvlc_int );
169         if( !p_instance->libvlc_vlm.p_vlm )
170         {
171             libvlc_exception_raise( p_exception );
172             libvlc_printerr( "VLM not supported or out of memory" );
173             return VLC_EGENERIC;
174         }
175         var_AddCallback( (vlc_object_t *)p_instance->libvlc_vlm.p_vlm,
176                          "intf-event", VlmEvent,
177                          p_instance->libvlc_vlm.p_event_manager );
178         p_instance->libvlc_vlm.pf_release = libvlc_vlm_release_internal;
179     }
180
181     return VLC_SUCCESS;
182 }
183
184 void libvlc_vlm_release( libvlc_instance_t *p_instance,
185                          libvlc_exception_t *p_exception)
186 {
187     VLC_UNUSED(p_exception);
188     libvlc_vlm_release_internal( p_instance );
189 }
190
191 #define VLM_RET(p,ret) do {                                     \
192     if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
193     (p) = p_instance->libvlc_vlm.p_vlm;                                    \
194   } while(0)
195 #define VLM(p) VLM_RET(p,)
196
197 static vlm_media_instance_t *
198 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
199                                const char *psz_name, int i_minstance_idx,
200                                libvlc_exception_t *p_exception )
201 {
202     vlm_t *p_vlm;
203     vlm_media_instance_t **pp_minstance;
204     vlm_media_instance_t *p_minstance;
205     int i_minstance;
206     int64_t id;
207
208     VLM_RET(p_vlm, NULL);
209
210     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
211         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
212                      &i_minstance ) )
213     {
214         libvlc_exception_raise( p_exception );
215         libvlc_printerr( "%s: media instances not found", psz_name );
216         return NULL;
217     }
218     p_minstance = NULL;
219     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
220     {
221         p_minstance = pp_minstance[i_minstance_idx];
222         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
223     }
224     while( i_minstance > 0 )
225         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
226     TAB_CLEAN( i_minstance, pp_minstance );
227     return p_minstance;
228 }
229
230 /* local function to be used in libvlc_vlm_show_media only */
231 static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
232                              const int i_list ) {
233     char* psz_childdelim = NULL;
234     char* psz_nametag = NULL;
235     char* psz_response = strdup( "" );
236     int i_success = 0;
237     int i;
238     vlm_message_t *aw_child, **paw_child;
239
240     i_success = asprintf( &psz_childdelim, "%s\t", psz_delim);
241
242     /* starting with the children of root node */
243     if( i_success != -1 && p_answer->i_child )
244     {
245         paw_child = p_answer->child;
246         aw_child = *( paw_child );
247         /* Iterate over children */
248         for( i = 0; i < p_answer->i_child; i++ )
249         {
250             /* Spare comma if it is the last element */
251             char c_comma = ',';
252             if( i == (p_answer->i_child - 1) )
253                 c_comma = ' ';
254
255             /* Append name of child node, if not in a list */
256             if( !i_list )
257             {
258                 i_success = asprintf( &psz_response, "%s\"%s\": ",
259                               psz_response, aw_child->psz_name );
260                 if( i_success == -1 ) break;
261             }
262
263             /* If child node has children, */
264             if( aw_child->i_child )
265             {
266                 /* If the parent node is a list (hence the child node is
267                  * inside a list), create a property of its name as if it
268                  * had a name value node
269                  */
270                 if( i_list )
271                 {
272                     i_success = asprintf( &psz_nametag, "\"name\": \"%s\",%s",
273                                   aw_child->psz_name, psz_childdelim );
274                     if( i_success == -1 ) break;
275                 }
276                 else
277                 {
278                     psz_nametag = strdup( "" );
279                 }
280                 /* If the child is a list itself, format it accordingly and
281                  * recurse through the child's children, telling them that
282                  * they are inside a list.
283                  */
284                 if( strcmp( aw_child->psz_name, "media" ) == 0 ||
285                     strcmp( aw_child->psz_name, "inputs" ) == 0 ||
286                     strcmp( aw_child->psz_name, "options" ) == 0 )
287                 {
288                     i_success = asprintf( &psz_response, "%s[%s%s%s]%c%s",
289                                           psz_response, psz_childdelim,
290                                           recurse_answer( aw_child,
291                                                           psz_childdelim, 1 ),
292                                           psz_delim, c_comma, psz_delim );
293                     if( i_success == -1 ) break;
294                 }
295                 /* Not a list, so format the child as a JSON object and
296                  * recurse through the child's children
297                  */
298                 else
299                 {
300                     i_success = asprintf( &psz_response, "%s{%s%s%s%s}%c%s",
301                                           psz_response, psz_childdelim, psz_nametag,
302                                           recurse_answer( aw_child,
303                                                           psz_childdelim, 0 ),
304                                           psz_delim, c_comma, psz_delim );
305                     if( i_success == -1 ) break;
306                 }
307             }
308             /* Otherwise - when no children are present - the node is a
309              * value node. So print the value string
310              */
311             else
312             {
313                 /* If value is equivalent to NULL, print it as null */
314                 if( aw_child->psz_value == NULL
315                     || strcmp( aw_child->psz_value, "(null)" ) == 0 )
316                 {
317                     i_success = asprintf( &psz_response, "%snull%c%s",
318                                           psz_response, c_comma, psz_delim );
319                     if( i_success == -1 )
320                         break;
321                 }
322                 /* Otherwise print the value in quotation marks */
323                 else
324                 {
325                     i_success = asprintf( &psz_response, "%s\"%s\"%c%s",
326                                           psz_response, aw_child->psz_value,
327                                           c_comma, psz_delim );
328                     if( i_success == -1 ) break;
329                 }
330             }
331             /* getting next child */
332             paw_child++;
333             aw_child = *( paw_child );
334         }
335     }
336     free( psz_nametag );
337     free( psz_childdelim );
338     if( i_success == -1 )
339     {
340         free( psz_response );
341         psz_response = strdup( "" );
342     }
343     return psz_response;
344 }
345
346 const char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
347                                    const char *psz_name,
348                                    libvlc_exception_t *p_exception )
349 {
350     char *psz_message = NULL;
351     vlm_message_t *answer = NULL;
352     char *psz_response = NULL;
353     const char *psz_fmt = NULL;
354     const char *psz_delimiter = NULL;
355     int i_list;
356     vlm_t *p_vlm = NULL;
357
358     VLM_RET(p_vlm, NULL);
359
360     assert( psz_name );
361
362     if( asprintf( &psz_message, "show %s", psz_name ) == -1 )
363     {
364         libvlc_exception_raise( p_exception );
365         libvlc_printerr( "Not enough memory" );
366         return NULL;
367     }
368
369     vlm_ExecuteCommand( p_vlm, psz_message, &answer );
370     if( answer->psz_value )
371     {
372         libvlc_exception_raise( p_exception );
373         libvlc_printerr( "Unable to call show %s: %s",
374                          psz_name, answer->psz_value );
375     }
376     else if ( answer->child )
377     {   /* in case everything was requested  */
378         if ( strcmp( psz_name, "" ) == 0 )
379         {
380             psz_fmt = "{\n\t%s\n}\n";
381             psz_delimiter = "\n\t";
382             i_list = 0;
383         }
384         else
385         {
386             psz_fmt = "%s\n";
387             psz_delimiter = "\n";
388             i_list = 1;
389         }
390         if( asprintf( &psz_response, psz_fmt,
391                       recurse_answer( answer, psz_delimiter, i_list ) ) == -1 )
392         {
393             libvlc_exception_raise( p_exception );
394             libvlc_printerr( "Out of memory" );
395         }
396     }
397     free( psz_message );
398     return( psz_response );
399 }
400
401
402 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
403                                const char *psz_name,
404                                const char *psz_input,
405                                const char *psz_output, int i_options,
406                                const char * const *ppsz_options,
407                                int b_enabled, int b_loop,
408                                libvlc_exception_t *p_exception )
409 {
410     vlm_t *p_vlm;
411     vlm_media_t m;
412     int n;
413
414     VLM(p_vlm);
415
416     vlm_media_Init( &m );
417     m.psz_name = strdup( psz_name );
418     m.b_enabled = b_enabled;
419     m.b_vod = false;
420     m.broadcast.b_loop = b_loop;
421     if( psz_input )
422         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
423     if( psz_output )
424         m.psz_output = strdup( psz_output );
425     for( n = 0; n < i_options; n++ )
426         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
427
428     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
429     vlm_media_Clean( &m );
430     if( n )
431     {
432         libvlc_exception_raise( p_exception );
433         libvlc_printerr( "Media %s creation failed", psz_name );
434     }
435 }
436
437 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
438                          const char *psz_input, int i_options,
439                          const char * const *ppsz_options, int b_enabled,
440                          const char *psz_mux, libvlc_exception_t *p_exception )
441 {
442     vlm_t *p_vlm;
443     vlm_media_t m;
444     int n;
445
446     VLM(p_vlm);
447
448     vlm_media_Init( &m );
449     m.psz_name = strdup( psz_name );
450     m.b_enabled = b_enabled;
451     m.b_vod = true;
452     m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
453     if( psz_input )
454         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
455     for( n = 0; n < i_options; n++ )
456         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
457
458     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
459     vlm_media_Clean( &m );
460     if( n )
461     {
462         libvlc_exception_raise( p_exception );
463         libvlc_printerr( "Media %s creation failed", psz_name );
464     }
465 }
466
467 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
468                            libvlc_exception_t *p_exception )
469 {
470     vlm_t *p_vlm;
471     int64_t id;
472
473     VLM(p_vlm);
474
475     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
476         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
477     {
478         libvlc_exception_raise( p_exception );
479         libvlc_printerr( "Unable to delete %s", psz_name );
480     }
481 }
482
483 #define VLM_CHANGE(psz_error, code ) do {   \
484     vlm_media_t *p_media;   \
485     vlm_t *p_vlm;           \
486     int64_t id;             \
487     VLM(p_vlm);             \
488     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||    \
489         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) {       \
490         libvlc_exception_raise( p_exception );                      \
491         libvlc_printerr( psz_error, psz_name );                     \
492         return;             \
493     }                       \
494     if( !p_media ) goto error;                                      \
495                             \
496     code;                   \
497                             \
498     if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) {         \
499         vlm_media_Delete( p_media );                                \
500         goto error;         \
501     }                       \
502     vlm_media_Delete( p_media );                                    \
503     return;                 \
504   error:                    \
505     libvlc_exception_raise( p_exception );                          \
506     libvlc_printerr( psz_error, psz_name );                         \
507   } while(0)
508
509 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
510                              const char *psz_name, int b_enabled,
511                              libvlc_exception_t *p_exception )
512 {
513 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
514     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
515 #undef VLM_CHANGE_CODE
516 }
517
518 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
519                           int b_loop, libvlc_exception_t *p_exception )
520 {
521 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
522     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
523 #undef VLM_CHANGE_CODE
524 }
525
526 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
527                          const char *psz_mux, libvlc_exception_t *p_exception )
528 {
529 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
530                             free( p_media->vod.psz_mux ); \
531                             p_media->vod.psz_mux = psz_mux \
532                                  ? strdup( psz_mux ) : NULL; \
533                           } }
534     VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
535 #undef VLM_CHANGE_CODE
536 }
537
538 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
539                             const char *psz_name, const char *psz_output,
540                             libvlc_exception_t *p_exception )
541 {
542 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
543                           p_media->psz_output = strdup( psz_output ); }
544     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
545 #undef VLM_CHANGE_CODE
546 }
547
548 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
549                            const char *psz_name, const char *psz_input,
550                            libvlc_exception_t *p_exception )
551 {
552 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
553                             free( p_media->ppsz_input[--p_media->i_input] );\
554                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
555                                       strdup(psz_input) ); }
556     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
557 #undef VLM_CHANGE_CODE
558 }
559
560 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
561                            const char *psz_name, const char *psz_input,
562                            libvlc_exception_t *p_exception )
563 {
564 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
565                           strdup(psz_input) ); }
566     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
567 #undef VLM_CHANGE_CODE
568 }
569
570 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
571                               const char *psz_name, const char *psz_input,
572                               const char *psz_output, int i_options,
573                               const char * const *ppsz_options, int b_enabled,
574                               int b_loop, libvlc_exception_t *p_exception )
575 {
576 #define VLM_CHANGE_CODE { int n;        \
577     p_media->b_enabled = b_enabled;     \
578     p_media->broadcast.b_loop = b_loop; \
579     while( p_media->i_input > 0 )       \
580         free( p_media->ppsz_input[--p_media->i_input] );    \
581     if( psz_input )                     \
582         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
583     free( p_media->psz_output );        \
584     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
585     while( p_media->i_option > 0 )     \
586         free( p_media->ppsz_option[--p_media->i_option] );        \
587     for( n = 0; n < i_options; n++ )    \
588         TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
589                     strdup(ppsz_options[n]) );   \
590   }
591     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
592 #undef VLM_CHANGE_CODE
593 }
594
595 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
596                             const char *psz_name,
597                             libvlc_exception_t *p_exception )
598 {
599     vlm_t *p_vlm;
600     int64_t id;
601
602     VLM(p_vlm);
603
604     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
605         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
606     {
607         libvlc_exception_raise( p_exception );
608         libvlc_printerr( "Unable to play %s", psz_name );
609     }
610 }
611
612 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
613                             const char *psz_name,
614                             libvlc_exception_t *p_exception )
615 {
616     vlm_t *p_vlm;
617     int64_t id;
618
619     VLM(p_vlm);
620
621     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
622         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
623     {
624         libvlc_exception_raise( p_exception );
625         libvlc_printerr( "Unable to stop %s", psz_name );
626     }
627 }
628
629 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
630                              const char *psz_name,
631                              libvlc_exception_t *p_exception )
632 {
633     vlm_t *p_vlm;
634     int64_t id;
635
636     VLM(p_vlm);
637
638     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
639         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
640     {
641         libvlc_exception_raise( p_exception );
642         libvlc_printerr( "Unable to pause %s", psz_name );
643     }
644 }
645
646 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
647                             const char *psz_name, float f_percentage,
648                             libvlc_exception_t *p_exception )
649 {
650     vlm_t *p_vlm;
651     int64_t id;
652
653     VLM(p_vlm);
654
655     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
656         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
657                      f_percentage ) )
658     {
659         libvlc_exception_raise( p_exception );
660         libvlc_printerr( "Unable to seek %s to %f%%", psz_name, f_percentage );
661     }
662 }
663
664 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
665                                               const char *psz_name,
666                                               int i_instance,
667                                               libvlc_exception_t *p_exception )
668 {
669     vlm_media_instance_t *p_mi;
670     float result = -1.;
671
672     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
673                                           i_instance, p_exception );
674     if( p_mi )
675     {
676         result = p_mi->d_position;
677         vlm_media_instance_Delete( p_mi );
678     }
679     return result;
680 }
681
682 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
683                                         const char *psz_name, int i_instance,
684                                         libvlc_exception_t *p_exception )
685 {
686     vlm_media_instance_t *p_mi;
687     int result = -1;
688
689     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
690                                         i_instance, p_exception );
691     if( p_mi )
692     {
693         result = p_mi->i_time;
694         vlm_media_instance_Delete( p_mi );
695     }
696     return result;
697 }
698
699 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
700                                           const char *psz_name,
701                                           int i_instance,
702                                           libvlc_exception_t *p_exception )
703 {
704     vlm_media_instance_t *p_mi;
705     int result = -1;
706
707     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
708                                           i_instance, p_exception );
709     if( p_mi )
710     {
711         result = p_mi->i_length;
712         vlm_media_instance_Delete( p_mi );
713     }
714     return result;
715 }
716
717 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
718                                         const char *psz_name, int i_instance,
719                                         libvlc_exception_t *p_exception )
720 {
721     vlm_media_instance_t *p_mi;
722     int result = -1;
723
724     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
725                                           i_instance, p_exception );
726     if( p_mi )
727     {
728         result = p_mi->i_rate;
729         vlm_media_instance_Delete( p_mi );
730     }
731     return result;
732 }
733
734 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
735                                          const char *psz_name, int i_instance,
736                                          libvlc_exception_t *p_exception )
737 {
738     vlm_media_instance_t *p_mi;
739
740     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
741                                           i_instance, p_exception );
742     if( p_mi )
743         vlm_media_instance_Delete( p_mi );
744     return p_mi ? 0 : -1;
745 }
746
747 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
748                                            const char *psz_name,
749                                            int i_instance,
750                                            libvlc_exception_t *p_exception )
751 {
752     vlm_media_instance_t *p_mi;
753
754     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
755                                           i_instance, p_exception );
756     if( p_mi )
757         vlm_media_instance_Delete( p_mi );
758     return p_mi ? 0 : -1;
759 }
760
761 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
762                                             const char *psz_name,
763                                             int i_instance,
764                                             libvlc_exception_t *p_exception )
765 {
766     vlm_media_instance_t *p_mi;
767
768     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
769                                           i_instance, p_exception );
770     if( p_mi )
771         vlm_media_instance_Delete( p_mi );
772     return p_mi ? 0 : -1;
773 }
774
775 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
776                                                        libvlc_exception_t *p_exception )
777 {
778     vlm_t *p_vlm;
779     VLM_RET( p_vlm, NULL);
780     return p_instance->libvlc_vlm.p_event_manager;
781 }