]> git.sesse.net Git - vlc/blob - src/control/vlm.c
libvlc vlm init fix Don't test p_vlm twice if it wasn't NULL the first time Don't...
[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 #include <vlc/libvlc.h>
25 #include <vlc/libvlc_vlm.h>
26 #include <vlc_es.h>
27 #include <vlc_input.h>
28 #include <vlc_vlm.h>
29
30 #include "libvlc_internal.h"
31
32 #if 0
33 /* local function to be used in libvlc_vlm_show_media only */
34 static char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
35     char* psz_childprefix;
36     char* psz_response="";
37     char* response_tmp;
38     int i;
39     vlm_message_t *aw_child, **paw_child;
40
41     asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
42
43     if ( p_answer->i_child )
44     {
45         paw_child = p_answer->child;
46         aw_child = *( paw_child );
47         for( i = 0; i < p_answer->i_child; i++ )
48         {
49             asprintf( &response_tmp, "%s%s%s:%s\n",
50                       psz_response, psz_prefix, aw_child->psz_name,
51                       aw_child->psz_value );
52             free( psz_response );
53             psz_response = response_tmp;
54             if ( aw_child->i_child )
55             {
56                 asprintf(&response_tmp, "%s%s", psz_response,
57                          recurse_answer(psz_childprefix, aw_child));
58                 free( psz_response );
59                 psz_response = response_tmp;
60             }
61             paw_child++;
62             aw_child = *( paw_child );
63         }
64     }
65     free( psz_childprefix );
66     return psz_response;
67 }
68
69 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
70                              libvlc_exception_t *p_exception )
71 {
72     char *psz_message;
73     vlm_message_t *answer;
74     char *psz_response;
75
76     CHECK_VLM;
77     asprintf( &psz_message, "show %s", psz_name );
78     asprintf( &psz_response, "", psz_name );
79     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
80     if( answer->psz_value )
81     {
82         libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
83                                 psz_name, answer->psz_value );
84     }
85     else
86     {
87         if ( answer->child )
88         {
89             psz_response = recurse_answer( "", answer );
90         }
91     }
92     free( psz_message );
93     return(psz_response );
94 }
95 #else
96
97 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance,
98                              const char *psz_name,
99                              libvlc_exception_t *p_exception )
100 {
101     (void)p_instance;
102     /* FIXME is it needed ? */
103     libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );
104     return NULL;
105 }
106
107 #endif /* 0 */
108 /* VLM events callback. Transmit to libvlc */
109 static int VlmEvent( vlc_object_t *p_this, const char * name,
110                      vlc_value_t old_val, vlc_value_t newval, void *param )
111 {
112     vlm_event_t *event = (vlm_event_t*)newval.p_address;
113     libvlc_event_manager_t *p_event_manager = (libvlc_event_manager_t *) param;
114     libvlc_event_t libvlc_event;
115
116     libvlc_event.u.vlm_media_event.psz_media_name = event->psz_name;
117
118     switch( event->i_type )
119     {
120     case VLM_EVENT_MEDIA_ADDED:
121         libvlc_event.type = libvlc_VlmMediaAdded;
122         break;
123     case VLM_EVENT_MEDIA_REMOVED:
124         libvlc_event.type = libvlc_VlmMediaRemoved;
125         break;
126     case VLM_EVENT_MEDIA_CHANGED:
127         libvlc_event.type = libvlc_VlmMediaChanged;
128         break;
129     case VLM_EVENT_MEDIA_INSTANCE_STARTED:
130         libvlc_event.type = libvlc_VlmMediaInstanceStarted;
131         break;
132     case VLM_EVENT_MEDIA_INSTANCE_STOPPED:
133         libvlc_event.type = libvlc_VlmMediaInstanceStopped;
134         break;
135     }
136     libvlc_event_send( p_event_manager, &libvlc_event );
137     return 0;
138 }
139
140 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
141                             libvlc_exception_t *p_exception )
142 {
143     if( !p_instance->p_event_manager )
144     {
145         p_instance->p_event_manager = libvlc_event_manager_new( p_instance->p_vlm,
146                                                                 p_instance, p_exception );
147         libvlc_event_manager_register_event_type( p_instance->p_event_manager,
148                                                   libvlc_VlmMediaAdded, NULL );
149         libvlc_event_manager_register_event_type( p_instance->p_event_manager,
150                                                   libvlc_VlmMediaRemoved, NULL );
151         libvlc_event_manager_register_event_type( p_instance->p_event_manager,
152                                                   libvlc_VlmMediaChanged, NULL );
153         libvlc_event_manager_register_event_type( p_instance->p_event_manager,
154                                                   libvlc_VlmMediaInstanceStarted, NULL );
155         libvlc_event_manager_register_event_type( p_instance->p_event_manager,
156                                                   libvlc_VlmMediaInstanceStopped, NULL );
157     }
158
159     if( !p_instance->p_vlm )
160     {
161         p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
162         if( !p_instance->p_vlm )
163         {
164             libvlc_exception_raise( p_exception,
165                                     "Unable to create VLM." );
166             return VLC_EGENERIC;
167         }
168         var_AddCallback( (vlc_object_t *)p_instance->p_vlm, "intf-event", VlmEvent,
169                          p_instance->p_event_manager );
170     }
171
172     return VLC_SUCCESS;
173 }
174 #define VLM_RET(p,ret) do {                                     \
175     if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
176     (p) = p_instance->p_vlm;                                    \
177   } while(0)
178 #define VLM(p) VLM_RET(p,)
179
180 static vlm_media_instance_t *
181 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
182                                const char *psz_name, int i_minstance_idx,
183                                libvlc_exception_t *p_exception )
184 {
185     vlm_t *p_vlm;
186     vlm_media_instance_t **pp_minstance;
187     vlm_media_instance_t *p_minstance;
188     int i_minstance;
189     int64_t id;
190
191     VLM_RET(p_vlm, NULL);
192
193     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
194         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
195                      &i_minstance ) )
196     {
197         libvlc_exception_raise( p_exception, "Unable to get %s instances",
198                                 psz_name );
199         return NULL;
200     }
201     p_minstance = NULL;
202     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
203     {
204         p_minstance = pp_minstance[i_minstance_idx];
205         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
206     }
207     while( i_minstance > 0 )
208         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
209     TAB_CLEAN( i_minstance, pp_minstance );
210     return p_minstance;
211 }
212
213
214 void libvlc_vlm_release( libvlc_instance_t *p_instance,
215                          libvlc_exception_t *p_exception)
216 {
217     vlm_t *p_vlm;
218
219     VLM(p_vlm);
220
221     vlm_Delete( p_vlm );
222     p_instance->p_vlm = NULL;
223 }
224
225 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
226                                const char *psz_name,
227                                const char *psz_input,
228                                const char *psz_output, int i_options,
229                                const char * const *ppsz_options,
230                                int b_enabled, int b_loop,
231                                libvlc_exception_t *p_exception )
232 {
233     vlm_t *p_vlm;
234     vlm_media_t m;
235     int n;
236
237     VLM(p_vlm);
238
239     vlm_media_Init( &m );
240     m.psz_name = strdup( psz_name );
241     m.b_enabled = b_enabled;
242     m.b_vod = false;
243     m.broadcast.b_loop = b_loop;
244     if( psz_input )
245         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
246     if( psz_output )
247         m.psz_output = strdup( psz_output );
248     for( n = 0; n < i_options; n++ )
249         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
250
251     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
252     vlm_media_Clean( &m );
253     if( n )
254         libvlc_exception_raise( p_exception, "Media %s creation failed",
255                                 psz_name );
256 }
257
258 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
259                          const char *psz_input, int i_options,
260                          const char * const *ppsz_options, int b_enabled,
261                          const char *psz_mux, libvlc_exception_t *p_exception )
262 {
263     vlm_t *p_vlm;
264     vlm_media_t m;
265     int n;
266
267     VLM(p_vlm);
268
269     vlm_media_Init( &m );
270     m.psz_name = strdup( psz_name );
271     m.b_enabled = b_enabled;
272     m.b_vod = true;
273     m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
274     if( psz_input )
275         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
276     for( n = 0; n < i_options; n++ )
277         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
278
279     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
280     vlm_media_Clean( &m );
281     if( n )
282         libvlc_exception_raise( p_exception, "Media %s creation failed",
283                                 psz_name );
284 }
285
286 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
287                            libvlc_exception_t *p_exception )
288 {
289     vlm_t *p_vlm;
290     int64_t id;
291
292     VLM(p_vlm);
293
294     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
295         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
296     {
297         libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
298     }
299 }
300
301 #define VLM_CHANGE(psz_error, code ) do {   \
302     vlm_media_t *p_media;   \
303     vlm_t *p_vlm;           \
304     int64_t id;             \
305     VLM(p_vlm);             \
306     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||    \
307         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) {       \
308         libvlc_exception_raise( p_exception, psz_error, psz_name ); \
309         return;             \
310     }                       \
311     if( !p_media ) goto error;                                      \
312                             \
313     code;                   \
314                             \
315     if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) {         \
316         vlm_media_Delete( p_media );                                \
317         goto error;         \
318     }                       \
319     vlm_media_Delete( p_media );                                    \
320     return;                 \
321   error:                    \
322     libvlc_exception_raise( p_exception, psz_error, psz_name );\
323   } while(0)
324
325 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
326                              const char *psz_name, int b_enabled,
327                              libvlc_exception_t *p_exception )
328 {
329 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
330     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
331 #undef VLM_CHANGE_CODE
332 }
333
334 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
335                           int b_loop, libvlc_exception_t *p_exception )
336 {
337 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
338     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
339 #undef VLM_CHANGE_CODE
340 }
341
342 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
343                          const char *psz_mux, libvlc_exception_t *p_exception )
344 {
345 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
346                             free( p_media->vod.psz_mux ); \
347                             p_media->vod.psz_mux = psz_mux \
348                                  ? strdup( psz_mux ) : NULL; \
349                           } }
350     VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
351 #undef VLM_CHANGE_CODE
352 }
353
354 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
355                             const char *psz_name, const char *psz_output,
356                             libvlc_exception_t *p_exception )
357 {
358 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
359                           p_media->psz_output = strdup( psz_output ); }
360     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
361 #undef VLM_CHANGE_CODE
362 }
363
364 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
365                            const char *psz_name, const char *psz_input,
366                            libvlc_exception_t *p_exception )
367 {
368 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
369                             free( p_media->ppsz_input[--p_media->i_input] );\
370                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
371                                       strdup(psz_input) ); }
372     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
373 #undef VLM_CHANGE_CODE
374 }
375
376 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
377                            const char *psz_name, const char *psz_input,
378                            libvlc_exception_t *p_exception )
379 {
380 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
381                           strdup(psz_input) ); }
382     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
383 #undef VLM_CHANGE_CODE
384 }
385
386 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
387                               const char *psz_name, const char *psz_input,
388                               const char *psz_output, int i_options,
389                               const char * const *ppsz_options, int b_enabled,
390                               int b_loop, libvlc_exception_t *p_exception )
391 {
392 #define VLM_CHANGE_CODE { int n;        \
393     p_media->b_enabled = b_enabled;     \
394     p_media->broadcast.b_loop = b_loop; \
395     while( p_media->i_input > 0 )       \
396         free( p_media->ppsz_input[--p_media->i_input] );    \
397     if( psz_input )                     \
398         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
399     free( p_media->psz_output );        \
400     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
401     while( p_media->i_option > 0 )     \
402         free( p_media->ppsz_option[--p_media->i_option] );        \
403     for( n = 0; n < i_options; n++ )    \
404         TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
405                     strdup(ppsz_options[n]) );   \
406   }
407     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
408 #undef VLM_CHANGE_CODE
409 }
410
411 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
412                             const char *psz_name,
413                             libvlc_exception_t *p_exception )
414 {
415     vlm_t *p_vlm;
416     int64_t id;
417
418     VLM(p_vlm);
419
420     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
421         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
422     {
423         libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
424     }
425 }
426
427 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
428                             const char *psz_name,
429                             libvlc_exception_t *p_exception )
430 {
431     vlm_t *p_vlm;
432     int64_t id;
433
434     VLM(p_vlm);
435
436     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
437         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
438     {
439         libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
440     }
441 }
442
443 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
444                              const char *psz_name,
445                              libvlc_exception_t *p_exception )
446 {
447     vlm_t *p_vlm;
448     int64_t id;
449
450     VLM(p_vlm);
451
452     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
453         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
454     {
455         libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
456     }
457 }
458
459 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
460                             const char *psz_name, float f_percentage,
461                             libvlc_exception_t *p_exception )
462 {
463     vlm_t *p_vlm;
464     int64_t id;
465
466     VLM(p_vlm);
467
468     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
469         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
470                      f_percentage ) )
471         libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
472                                 psz_name, f_percentage );
473 }
474
475 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
476                                               const char *psz_name,
477                                               int i_instance,
478                                               libvlc_exception_t *p_exception )
479 {
480     vlm_media_instance_t *p_mi;
481     float result = -1.;
482
483     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
484                                           i_instance, p_exception );
485     if( p_mi )
486     {
487         result = p_mi->d_position;
488         vlm_media_instance_Delete( p_mi );
489     }
490     return result;
491 }
492
493 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
494                                         const char *psz_name, int i_instance,
495                                         libvlc_exception_t *p_exception )
496 {
497     vlm_media_instance_t *p_mi;
498     int result = -1;
499
500     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
501                                         i_instance, p_exception );
502     if( p_mi )
503     {
504         result = p_mi->i_time;
505         vlm_media_instance_Delete( p_mi );
506     }
507     return result;
508 }
509
510 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
511                                           const char *psz_name,
512                                           int i_instance,
513                                           libvlc_exception_t *p_exception )
514 {
515     vlm_media_instance_t *p_mi;
516     int result = -1;
517
518     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
519                                           i_instance, p_exception );
520     if( p_mi )
521     {
522         result = p_mi->i_length;
523         vlm_media_instance_Delete( p_mi );
524     }
525     return result;
526 }
527
528 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
529                                         const char *psz_name, int i_instance,
530                                         libvlc_exception_t *p_exception )
531 {
532     vlm_media_instance_t *p_mi;
533     int result = -1;
534
535     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
536                                           i_instance, p_exception );
537     if( p_mi )
538     {
539         result = p_mi->i_rate;
540         vlm_media_instance_Delete( p_mi );
541     }
542     return result;
543 }
544
545 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
546                                          const char *psz_name, int i_instance,
547                                          libvlc_exception_t *p_exception )
548 {
549     vlm_media_instance_t *p_mi;
550
551     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
552                                           i_instance, p_exception );
553     if( p_mi )
554         vlm_media_instance_Delete( p_mi );
555     return p_mi ? 0 : -1;
556 }
557
558 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
559                                            const char *psz_name,
560                                            int i_instance,
561                                            libvlc_exception_t *p_exception )
562 {
563     vlm_media_instance_t *p_mi;
564
565     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
566                                           i_instance, p_exception );
567     if( p_mi )
568         vlm_media_instance_Delete( p_mi );
569     return p_mi ? 0 : -1;
570 }
571
572 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
573                                             const char *psz_name,
574                                             int i_instance,
575                                             libvlc_exception_t *p_exception )
576 {
577     vlm_media_instance_t *p_mi;
578
579     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
580                                           i_instance, p_exception );
581     if( p_mi )
582         vlm_media_instance_Delete( p_mi );
583     return p_mi ? 0 : -1;
584 }
585
586 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
587                                                        libvlc_exception_t *p_exception )
588 {
589     vlm_t *p_vlm;
590     VLM_RET( p_vlm, NULL);
591     return p_instance->p_event_manager;
592 }