]> git.sesse.net Git - vlc/blob - lib/vlm.c
mux: avi: handle failed reallocs
[vlc] / lib / vlm.c
1 /*****************************************************************************
2  * vlm.c: libvlc new API VLM handling functions
3  *****************************************************************************
4  * Copyright (C) 2005 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 {
124     if( !p_instance->libvlc_vlm.p_event_manager )
125     {
126         p_instance->libvlc_vlm.p_event_manager =
127             libvlc_event_manager_new( p_instance->libvlc_vlm.p_vlm, p_instance );
128         if( unlikely(p_instance->libvlc_vlm.p_event_manager == NULL) )
129             return VLC_ENOMEM;
130         libvlc_event_manager_register_event_type(
131             p_instance->libvlc_vlm.p_event_manager,
132             libvlc_VlmMediaAdded );
133         libvlc_event_manager_register_event_type(
134             p_instance->libvlc_vlm.p_event_manager,
135             libvlc_VlmMediaRemoved );
136         libvlc_event_manager_register_event_type(
137             p_instance->libvlc_vlm.p_event_manager,
138             libvlc_VlmMediaChanged );
139         libvlc_event_manager_register_event_type(
140             p_instance->libvlc_vlm.p_event_manager,
141             libvlc_VlmMediaInstanceStarted );
142         libvlc_event_manager_register_event_type(
143             p_instance->libvlc_vlm.p_event_manager,
144             libvlc_VlmMediaInstanceStopped );
145         libvlc_event_manager_register_event_type(
146             p_instance->libvlc_vlm.p_event_manager,
147             libvlc_VlmMediaInstanceStatusInit );
148         libvlc_event_manager_register_event_type(
149             p_instance->libvlc_vlm.p_event_manager,
150             libvlc_VlmMediaInstanceStatusOpening );
151         libvlc_event_manager_register_event_type(
152             p_instance->libvlc_vlm.p_event_manager,
153             libvlc_VlmMediaInstanceStatusPlaying );
154         libvlc_event_manager_register_event_type(
155             p_instance->libvlc_vlm.p_event_manager,
156             libvlc_VlmMediaInstanceStatusPause );
157         libvlc_event_manager_register_event_type(
158             p_instance->libvlc_vlm.p_event_manager,
159             libvlc_VlmMediaInstanceStatusEnd );
160         libvlc_event_manager_register_event_type(
161             p_instance->libvlc_vlm.p_event_manager,
162             libvlc_VlmMediaInstanceStatusError );
163     }
164
165     if( !p_instance->libvlc_vlm.p_vlm )
166     {
167         p_instance->libvlc_vlm.p_vlm = vlm_New( p_instance->p_libvlc_int );
168         if( !p_instance->libvlc_vlm.p_vlm )
169         {
170             libvlc_printerr( "VLM not supported or out of memory" );
171             return VLC_EGENERIC;
172         }
173         var_AddCallback( (vlc_object_t *)p_instance->libvlc_vlm.p_vlm,
174                          "intf-event", VlmEvent,
175                          p_instance->libvlc_vlm.p_event_manager );
176         p_instance->libvlc_vlm.pf_release = libvlc_vlm_release_internal;
177     }
178
179     return VLC_SUCCESS;
180 }
181
182 void libvlc_vlm_release( libvlc_instance_t *p_instance )
183 {
184     libvlc_vlm_release_internal( p_instance );
185 }
186
187 #define VLM_RET(p,ret) do { \
188     if( libvlc_vlm_init( p_instance ) ) \
189         return (ret); \
190     (p) = p_instance->libvlc_vlm.p_vlm; \
191   } while(0)
192
193 static vlm_media_instance_t *
194 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
195                                const char *psz_name, int i_minstance_idx )
196 {
197     vlm_t *p_vlm;
198     vlm_media_instance_t **pp_minstance;
199     vlm_media_instance_t *p_minstance;
200     int i_minstance;
201     int64_t id;
202
203     VLM_RET(p_vlm, NULL);
204
205     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
206         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
207                      &i_minstance ) )
208     {
209         libvlc_printerr( "%s: media instances not found", psz_name );
210         return NULL;
211     }
212     p_minstance = NULL;
213     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
214     {
215         p_minstance = pp_minstance[i_minstance_idx];
216         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
217     }
218     while( i_minstance > 0 )
219         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
220     TAB_CLEAN( i_minstance, pp_minstance );
221     return p_minstance;
222 }
223
224 /* local function to be used in libvlc_vlm_show_media only */
225 static char* recurse_answer( vlm_message_t *p_answer, const char* psz_delim,
226                              const int i_list ) {
227     char* psz_childdelim = NULL;
228     char* psz_nametag = NULL;
229     char* psz_response = strdup( "" );
230     char *psz_tmp;
231     int i_success = 0;
232     int i;
233     vlm_message_t *aw_child, **paw_child;
234
235     i_success = asprintf( &psz_childdelim, "%s\t", psz_delim);
236     if( i_success == -1 )
237         return psz_response;
238
239     paw_child = p_answer->child;
240     aw_child = *( paw_child );
241     /* Iterate over children */
242     for( i = 0; i < p_answer->i_child; i++ )
243     {
244         /* Spare comma if it is the last element */
245         char c_comma = ',';
246         if( i == (p_answer->i_child - 1) )
247             c_comma = ' ';
248
249         /* Append name of child node, if not in a list */
250         if( !i_list )
251         {
252             i_success = asprintf( &psz_tmp, "%s\"%s\": ",
253                           psz_response, aw_child->psz_name );
254             if( i_success == -1 ) break;
255             free( psz_response );
256             psz_response = psz_tmp;
257         }
258
259         /* If child node has children, */
260         if( aw_child->i_child )
261         {
262             /* If the parent node is a list (hence the child node is
263              * inside a list), create a property of its name as if it
264              * had a name value node
265              */
266             free( psz_nametag );
267             if( i_list )
268             {
269                 i_success = asprintf( &psz_nametag, "\"name\": \"%s\",%s",
270                               aw_child->psz_name, psz_childdelim );
271                 if( i_success == -1 )
272                 {
273                     psz_nametag = NULL;
274                     break;
275                 }
276             }
277             else
278             {
279                 psz_nametag = strdup( "" );
280             }
281             /* If the child is a list itself, format it accordingly and
282              * recurse through the child's children, telling them that
283              * they are inside a list.
284              */
285             if( strcmp( aw_child->psz_name, "media" ) == 0 ||
286                 strcmp( aw_child->psz_name, "inputs" ) == 0 ||
287                 strcmp( aw_child->psz_name, "options" ) == 0 )
288             {
289                 char *psz_recurse = recurse_answer( aw_child, psz_childdelim, 1 );
290                 i_success = asprintf( &psz_tmp, "%s[%s%s%s]%c%s",
291                                       psz_response, psz_childdelim, psz_recurse,
292                                       psz_delim, c_comma, psz_delim );
293                 free( psz_recurse );
294                 if( i_success == -1 ) break;
295                 free( psz_response );
296                 psz_response = psz_tmp;
297             }
298             /* Not a list, so format the child as a JSON object and
299              * recurse through the child's children
300              */
301             else
302             {
303                 char *psz_recurse = recurse_answer( aw_child, psz_childdelim, 0 );
304                 i_success = asprintf( &psz_tmp, "%s{%s%s%s%s}%c%s",
305                                       psz_response, psz_childdelim, psz_nametag,
306                                       psz_recurse, psz_delim, c_comma, psz_delim );
307                 free( psz_recurse );
308                 if( i_success == -1 ) break;
309                 free( psz_response );
310                 psz_response = psz_tmp;
311             }
312         }
313         /* Otherwise - when no children are present - the node is a
314          * value node. So print the value string
315          */
316         else
317         {
318             /* If value is equivalent to NULL, print it as null */
319             if( aw_child->psz_value == NULL
320                 || strcmp( aw_child->psz_value, "(null)" ) == 0 )
321             {
322                 i_success = asprintf( &psz_tmp, "%snull%c%s",
323                                       psz_response, c_comma, psz_delim );
324                 if( i_success == -1 ) break;
325                 free( psz_response );
326                 psz_response = psz_tmp;
327             }
328             /* Otherwise print the value in quotation marks */
329             else
330             {
331                 i_success = asprintf( &psz_tmp, "%s\"%s\"%c%s",
332                                       psz_response, aw_child->psz_value,
333                                       c_comma, psz_delim );
334                 if( i_success == -1 ) break;
335                 free( psz_response );
336                 psz_response = psz_tmp;
337             }
338         }
339         /* getting next child */
340         paw_child++;
341         aw_child = *( paw_child );
342     }
343     free( psz_nametag );
344     free( psz_childdelim );
345     if( i_success == -1 )
346     {
347         free( psz_response );
348         psz_response = strdup( "" );
349     }
350     return psz_response;
351 }
352
353 const char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
354                                    const char *psz_name )
355 {
356     char *psz_message = NULL;
357     vlm_message_t *answer = NULL;
358     char *psz_response = NULL;
359     const char *psz_fmt = NULL;
360     const char *psz_delimiter = NULL;
361     int i_list;
362     vlm_t *p_vlm = NULL;
363
364     VLM_RET(p_vlm, NULL);
365
366     assert( psz_name );
367
368     if( asprintf( &psz_message, "show %s", psz_name ) == -1 )
369         return NULL;
370
371     vlm_ExecuteCommand( p_vlm, psz_message, &answer );
372     if( answer->psz_value )
373     {
374         libvlc_printerr( "Unable to call show %s: %s",
375                          psz_name, answer->psz_value );
376     }
377     else if ( answer->child )
378     {   /* in case everything was requested  */
379         if ( strcmp( psz_name, "" ) == 0 )
380         {
381             psz_fmt = "{\n\t%s\n}\n";
382             psz_delimiter = "\n\t";
383             i_list = 0;
384         }
385         else
386         {
387             psz_fmt = "%s\n";
388             psz_delimiter = "\n";
389             i_list = 1;
390         }
391         char *psz_tmp = recurse_answer( answer, psz_delimiter, i_list );
392         if( asprintf( &psz_response, psz_fmt, psz_tmp ) == -1 )
393         {
394             libvlc_printerr( "Out of memory" );
395             psz_response = NULL;
396         }
397         free( psz_tmp );
398     }
399     vlm_MessageDelete( answer );
400     free( psz_message );
401     return( psz_response );
402 }
403
404
405 int libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
406                               const char *psz_name,
407                               const char *psz_input,
408                               const char *psz_output, int i_options,
409                               const char * const *ppsz_options,
410                               int b_enabled, int b_loop )
411 {
412     vlm_t *p_vlm;
413     vlm_media_t m;
414     int n;
415
416     VLM_RET(p_vlm, -1);
417
418     vlm_media_Init( &m );
419     m.psz_name = strdup( psz_name );
420     m.b_enabled = b_enabled;
421     m.b_vod = false;
422     m.broadcast.b_loop = b_loop;
423     if( psz_input )
424         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
425     if( psz_output )
426         m.psz_output = strdup( psz_output );
427     for( n = 0; n < i_options; n++ )
428         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
429
430     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
431     vlm_media_Clean( &m );
432     if( n )
433     {
434         libvlc_printerr( "Media %s creation failed", psz_name );
435         return -1;
436     }
437     return 0;
438 }
439
440 int libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
441                         const char *psz_input, int i_options,
442                         const char * const *ppsz_options, int b_enabled,
443                         const char *psz_mux )
444 {
445     vlm_t *p_vlm;
446     vlm_media_t m;
447     int n;
448
449     VLM_RET(p_vlm, -1);
450
451     vlm_media_Init( &m );
452     m.psz_name = strdup( psz_name );
453     m.b_enabled = b_enabled;
454     m.b_vod = true;
455     m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
456     if( psz_input )
457         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
458     for( n = 0; n < i_options; n++ )
459         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
460
461     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
462     vlm_media_Clean( &m );
463     if( n )
464     {
465         libvlc_printerr( "Media %s creation failed", psz_name );
466         return -1;
467     }
468     return 0;
469 }
470
471 int libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name )
472 {
473     vlm_t *p_vlm;
474     int64_t id;
475
476     VLM_RET(p_vlm, -1);
477
478     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
479         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
480     {
481         libvlc_printerr( "Unable to delete %s", psz_name );
482         return -1;
483     }
484     return 0;
485 }
486
487 static vlm_media_t *get_media( libvlc_instance_t *p_instance,
488                                vlm_t **restrict pp_vlm, const char *name )
489 {
490     vlm_media_t *p_media;
491     vlm_t *p_vlm;
492     int64_t id;
493
494     VLM_RET(p_vlm, NULL);
495     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, name, &id ) ||
496         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) )
497         return NULL;
498     *pp_vlm = p_vlm;
499     return p_media;
500 }
501
502 #define VLM_CHANGE(psz_error, code ) do {   \
503     vlm_t *p_vlm;           \
504     vlm_media_t *p_media = get_media( p_instance, &p_vlm, psz_name ); \
505     if( p_media != NULL ) { \
506         code;               \
507         if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) )       \
508             p_vlm = NULL;                                           \
509         vlm_media_Delete( p_media );                                \
510         if( p_vlm != NULL ) \
511             return 0;       \
512     }                       \
513     libvlc_printerr( psz_error, psz_name );                         \
514     return -1;              \
515   } while(0)
516
517 int libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
518                             const char *psz_name, int b_enabled )
519 {
520 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
521     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
522 #undef VLM_CHANGE_CODE
523 }
524
525 int libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
526                          int b_loop )
527 {
528 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
529     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
530 #undef VLM_CHANGE_CODE
531 }
532
533 int libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
534                         const char *psz_mux )
535 {
536 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
537                             free( p_media->vod.psz_mux ); \
538                             p_media->vod.psz_mux = psz_mux \
539                                  ? strdup( psz_mux ) : NULL; \
540                           } }
541     VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
542 #undef VLM_CHANGE_CODE
543 }
544
545 int libvlc_vlm_set_output( libvlc_instance_t *p_instance,
546                            const char *psz_name, const char *psz_output )
547 {
548 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
549                           p_media->psz_output = strdup( psz_output ); }
550     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
551 #undef VLM_CHANGE_CODE
552 }
553
554 int libvlc_vlm_set_input( libvlc_instance_t *p_instance,
555                           const char *psz_name, const char *psz_input )
556 {
557 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
558                             free( p_media->ppsz_input[--p_media->i_input] );\
559                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
560                                       strdup(psz_input) ); }
561     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
562 #undef VLM_CHANGE_CODE
563 }
564
565 int libvlc_vlm_add_input( libvlc_instance_t *p_instance,
566                           const char *psz_name, const char *psz_input )
567 {
568 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
569                           strdup(psz_input) ); }
570     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
571 #undef VLM_CHANGE_CODE
572 }
573
574 int libvlc_vlm_change_media( libvlc_instance_t *p_instance,
575                              const char *psz_name, const char *psz_input,
576                              const char *psz_output, int i_options,
577                              const char * const *ppsz_options, int b_enabled,
578                              int b_loop )
579 {
580 #define VLM_CHANGE_CODE { int n;        \
581     p_media->b_enabled = b_enabled;     \
582     p_media->broadcast.b_loop = b_loop; \
583     while( p_media->i_input > 0 )       \
584         free( p_media->ppsz_input[--p_media->i_input] );    \
585     if( psz_input )                     \
586         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
587     free( p_media->psz_output );        \
588     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
589     while( p_media->i_option > 0 )     \
590         free( p_media->ppsz_option[--p_media->i_option] );        \
591     for( n = 0; n < i_options; n++ )    \
592         TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
593                     strdup(ppsz_options[n]) );   \
594   }
595     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
596 #undef VLM_CHANGE_CODE
597 }
598
599 int libvlc_vlm_play_media( libvlc_instance_t *p_instance,
600                            const char *psz_name )
601 {
602     vlm_t *p_vlm;
603     int64_t id;
604
605     VLM_RET(p_vlm, -1);
606
607     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
608         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
609     {
610         libvlc_printerr( "Unable to play %s", psz_name );
611         return -1;
612     }
613     return 0;
614 }
615
616 int libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
617                            const char *psz_name )
618 {
619     vlm_t *p_vlm;
620     int64_t id;
621
622     VLM_RET(p_vlm, -1);
623
624     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
625         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
626     {
627         libvlc_printerr( "Unable to stop %s", psz_name );
628         return -1;
629     }
630     return 0;
631 }
632
633 int libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
634                             const char *psz_name )
635 {
636     vlm_t *p_vlm;
637     int64_t id;
638
639     VLM_RET(p_vlm, -1);
640
641     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
642         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
643     {
644         libvlc_printerr( "Unable to pause %s", psz_name );
645         return -1;
646     }
647     return 0;
648 }
649
650 int libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
651                            const char *psz_name, float f_percentage )
652 {
653     vlm_t *p_vlm;
654     int64_t id;
655
656     VLM_RET(p_vlm, -1);
657
658     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
659         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
660                      f_percentage ) )
661     {
662         libvlc_printerr( "Unable to seek %s to %f%%", psz_name, f_percentage );
663         return -1;
664     }
665     return 0;
666 }
667
668 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
669                                               const char *psz_name,
670                                               int i_instance )
671 {
672     vlm_media_instance_t *p_mi;
673     float result = -1.;
674
675     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance );
676     if( p_mi )
677     {
678         result = p_mi->d_position;
679         vlm_media_instance_Delete( p_mi );
680     }
681     return result;
682 }
683
684 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
685                                         const char *psz_name, int i_instance )
686 {
687     vlm_media_instance_t *p_mi;
688     int result = -1;
689
690     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance );
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 {
703     vlm_media_instance_t *p_mi;
704     int result = -1;
705
706     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance );
707     if( p_mi )
708     {
709         result = p_mi->i_length;
710         vlm_media_instance_Delete( p_mi );
711     }
712     return result;
713 }
714
715 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
716                                         const char *psz_name, int i_instance )
717 {
718     vlm_media_instance_t *p_mi;
719     int result = -1;
720
721     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance );
722     if( p_mi )
723     {
724         result = p_mi->i_rate;
725         vlm_media_instance_Delete( p_mi );
726     }
727     return result;
728 }
729
730 #if 0
731 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
732                                          const char *psz_name, int i_instance )
733 {
734     vlm_media_instance_t *p_mi;
735
736     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance );
737     if( p_mi )
738         vlm_media_instance_Delete( p_mi );
739     return p_mi ? 0 : -1;
740 }
741
742 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
743                                            const char *psz_name,
744                                            int i_instance )
745 {
746     vlm_media_instance_t *p_mi;
747
748     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
749                                           i_instance );
750     if( p_mi )
751         vlm_media_instance_Delete( p_mi );
752     return p_mi ? 0 : -1;
753 }
754
755 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
756                                             const char *psz_name,
757                                             int i_instance )
758 {
759     vlm_media_instance_t *p_mi;
760
761     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance );
762     if( p_mi )
763         vlm_media_instance_Delete( p_mi );
764     return p_mi ? 0 : -1;
765 }
766 #endif
767
768 libvlc_event_manager_t *
769 libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance )
770 {
771     if( libvlc_vlm_init( p_instance ) )
772         return NULL;
773     return p_instance->libvlc_vlm.p_event_manager;
774 }