]> git.sesse.net Git - vlc/blob - src/control/vlm.c
Handle VLM events in libvlc
[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         var_AddCallback( (vlc_object_t *)p_instance->p_vlm, "intf-event", VlmEvent,
163                          p_instance->p_event_manager );
164     }
165     if( !p_instance->p_vlm )
166     {
167         libvlc_exception_raise( p_exception,
168                                 "Unable to create VLM." );
169         return VLC_EGENERIC;
170     }
171     return VLC_SUCCESS;
172 }
173 #define VLM_RET(p,ret) do {                                     \
174     if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
175     (p) = p_instance->p_vlm;                                    \
176   } while(0)
177 #define VLM(p) VLM_RET(p,)
178
179 static vlm_media_instance_t *
180 libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
181                                const char *psz_name, int i_minstance_idx,
182                                libvlc_exception_t *p_exception )
183 {
184     vlm_t *p_vlm;
185     vlm_media_instance_t **pp_minstance;
186     vlm_media_instance_t *p_minstance;
187     int i_minstance;
188     int64_t id;
189
190     VLM_RET(p_vlm, NULL);
191
192     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
193         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance,
194                      &i_minstance ) )
195     {
196         libvlc_exception_raise( p_exception, "Unable to get %s instances",
197                                 psz_name );
198         return NULL;
199     }
200     p_minstance = NULL;
201     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
202     {
203         p_minstance = pp_minstance[i_minstance_idx];
204         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
205     }
206     while( i_minstance > 0 )
207         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
208     TAB_CLEAN( i_minstance, pp_minstance );
209     return p_minstance;
210 }
211
212
213 void libvlc_vlm_release( libvlc_instance_t *p_instance,
214                          libvlc_exception_t *p_exception)
215 {
216     vlm_t *p_vlm;
217
218     VLM(p_vlm);
219
220     vlm_Delete( p_vlm );
221 }
222
223 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance,
224                                const char *psz_name,
225                                const char *psz_input,
226                                const char *psz_output, int i_options,
227                                const char * const *ppsz_options,
228                                int b_enabled, int b_loop,
229                                libvlc_exception_t *p_exception )
230 {
231     vlm_t *p_vlm;
232     vlm_media_t m;
233     int n;
234
235     VLM(p_vlm);
236
237     vlm_media_Init( &m );
238     m.psz_name = strdup( psz_name );
239     m.b_enabled = b_enabled;
240     m.b_vod = false;
241     m.broadcast.b_loop = b_loop;
242     if( psz_input )
243         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
244     if( psz_output )
245         m.psz_output = strdup( psz_output );
246     for( n = 0; n < i_options; n++ )
247         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
248
249     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
250     vlm_media_Clean( &m );
251     if( n )
252         libvlc_exception_raise( p_exception, "Media %s creation failed",
253                                 psz_name );
254 }
255
256 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, const char *psz_name,
257                          const char *psz_input, int i_options,
258                          const char * const *ppsz_options, int b_enabled,
259                          const char *psz_mux, libvlc_exception_t *p_exception )
260 {
261     vlm_t *p_vlm;
262     vlm_media_t m;
263     int n;
264
265     VLM(p_vlm);
266
267     vlm_media_Init( &m );
268     m.psz_name = strdup( psz_name );
269     m.b_enabled = b_enabled;
270     m.b_vod = true;
271     m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
272     if( psz_input )
273         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
274     for( n = 0; n < i_options; n++ )
275         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
276
277     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
278     vlm_media_Clean( &m );
279     if( n )
280         libvlc_exception_raise( p_exception, "Media %s creation failed",
281                                 psz_name );
282 }
283
284 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, const char *psz_name,
285                            libvlc_exception_t *p_exception )
286 {
287     vlm_t *p_vlm;
288     int64_t id;
289
290     VLM(p_vlm);
291
292     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
293         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
294     {
295         libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
296     }
297 }
298
299 #define VLM_CHANGE(psz_error, code ) do {   \
300     vlm_media_t *p_media;   \
301     vlm_t *p_vlm;           \
302     int64_t id;             \
303     VLM(p_vlm);             \
304     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||    \
305         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) {       \
306         libvlc_exception_raise( p_exception, psz_error, psz_name ); \
307         return;             \
308     }                       \
309     if( !p_media ) goto error;                                      \
310                             \
311     code;                   \
312                             \
313     if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) {         \
314         vlm_media_Delete( p_media );                                \
315         goto error;         \
316     }                       \
317     vlm_media_Delete( p_media );                                    \
318     return;                 \
319   error:                    \
320     libvlc_exception_raise( p_exception, psz_error, psz_name );\
321   } while(0)
322
323 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance,
324                              const char *psz_name, int b_enabled,
325                              libvlc_exception_t *p_exception )
326 {
327 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
328     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
329 #undef VLM_CHANGE_CODE
330 }
331
332 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, const char *psz_name,
333                           int b_loop, libvlc_exception_t *p_exception )
334 {
335 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
336     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
337 #undef VLM_CHANGE_CODE
338 }
339
340 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, const char *psz_name,
341                          const char *psz_mux, libvlc_exception_t *p_exception )
342 {
343 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
344                             free( p_media->vod.psz_mux ); \
345                             p_media->vod.psz_mux = psz_mux \
346                                  ? strdup( psz_mux ) : NULL; \
347                           } }
348     VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
349 #undef VLM_CHANGE_CODE
350 }
351
352 void libvlc_vlm_set_output( libvlc_instance_t *p_instance,
353                             const char *psz_name, const char *psz_output,
354                             libvlc_exception_t *p_exception )
355 {
356 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
357                           p_media->psz_output = strdup( psz_output ); }
358     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
359 #undef VLM_CHANGE_CODE
360 }
361
362 void libvlc_vlm_set_input( libvlc_instance_t *p_instance,
363                            const char *psz_name, const char *psz_input,
364                            libvlc_exception_t *p_exception )
365 {
366 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
367                             free( p_media->ppsz_input[--p_media->i_input] );\
368                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
369                                       strdup(psz_input) ); }
370     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
371 #undef VLM_CHANGE_CODE
372 }
373
374 void libvlc_vlm_add_input( libvlc_instance_t *p_instance,
375                            const char *psz_name, const char *psz_input,
376                            libvlc_exception_t *p_exception )
377 {
378 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, \
379                           strdup(psz_input) ); }
380     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
381 #undef VLM_CHANGE_CODE
382 }
383
384 void libvlc_vlm_change_media( libvlc_instance_t *p_instance,
385                               const char *psz_name, const char *psz_input,
386                               const char *psz_output, int i_options,
387                               const char * const *ppsz_options, int b_enabled,
388                               int b_loop, libvlc_exception_t *p_exception )
389 {
390 #define VLM_CHANGE_CODE { int n;        \
391     p_media->b_enabled = b_enabled;     \
392     p_media->broadcast.b_loop = b_loop; \
393     while( p_media->i_input > 0 )       \
394         free( p_media->ppsz_input[--p_media->i_input] );    \
395     if( psz_input )                     \
396         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
397     free( p_media->psz_output );        \
398     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
399     while( p_media->i_option > 0 )     \
400         free( p_media->ppsz_option[--p_media->i_option] );        \
401     for( n = 0; n < i_options; n++ )    \
402         TAB_APPEND( p_media->i_option, p_media->ppsz_option, \
403                     strdup(ppsz_options[n]) );   \
404   }
405     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
406 #undef VLM_CHANGE_CODE
407 }
408
409 void libvlc_vlm_play_media( libvlc_instance_t *p_instance,
410                             const char *psz_name,
411                             libvlc_exception_t *p_exception )
412 {
413     vlm_t *p_vlm;
414     int64_t id;
415
416     VLM(p_vlm);
417
418     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
419         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
420     {
421         libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
422     }
423 }
424
425 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance,
426                             const char *psz_name,
427                             libvlc_exception_t *p_exception )
428 {
429     vlm_t *p_vlm;
430     int64_t id;
431
432     VLM(p_vlm);
433
434     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
435         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
436     {
437         libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
438     }
439 }
440
441 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance,
442                              const char *psz_name,
443                              libvlc_exception_t *p_exception )
444 {
445     vlm_t *p_vlm;
446     int64_t id;
447
448     VLM(p_vlm);
449
450     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
451         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
452     {
453         libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
454     }
455 }
456
457 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance,
458                             const char *psz_name, float f_percentage,
459                             libvlc_exception_t *p_exception )
460 {
461     vlm_t *p_vlm;
462     int64_t id;
463
464     VLM(p_vlm);
465
466     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
467         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL,
468                      f_percentage ) )
469         libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
470                                 psz_name, f_percentage );
471 }
472
473 float libvlc_vlm_get_media_instance_position( libvlc_instance_t *p_instance,
474                                               const char *psz_name,
475                                               int i_instance,
476                                               libvlc_exception_t *p_exception )
477 {
478     vlm_media_instance_t *p_mi;
479     float result = -1.;
480
481     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
482                                           i_instance, p_exception );
483     if( p_mi )
484     {
485         result = p_mi->d_position;
486         vlm_media_instance_Delete( p_mi );
487     }
488     return result;
489 }
490
491 int libvlc_vlm_get_media_instance_time( libvlc_instance_t *p_instance,
492                                         const char *psz_name, int i_instance,
493                                         libvlc_exception_t *p_exception )
494 {
495     vlm_media_instance_t *p_mi;
496     int result = -1;
497
498     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
499                                         i_instance, p_exception );
500     if( p_mi )
501     {
502         result = p_mi->i_time;
503         vlm_media_instance_Delete( p_mi );
504     }
505     return result;
506 }
507
508 int libvlc_vlm_get_media_instance_length( libvlc_instance_t *p_instance,
509                                           const char *psz_name,
510                                           int i_instance,
511                                           libvlc_exception_t *p_exception )
512 {
513     vlm_media_instance_t *p_mi;
514     int result = -1;
515
516     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
517                                           i_instance, p_exception );
518     if( p_mi )
519     {
520         result = p_mi->i_length;
521         vlm_media_instance_Delete( p_mi );
522     }
523     return result;
524 }
525
526 int libvlc_vlm_get_media_instance_rate( libvlc_instance_t *p_instance,
527                                         const char *psz_name, int i_instance,
528                                         libvlc_exception_t *p_exception )
529 {
530     vlm_media_instance_t *p_mi;
531     int result = -1;
532
533     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
534                                           i_instance, p_exception );
535     if( p_mi )
536     {
537         result = p_mi->i_rate;
538         vlm_media_instance_Delete( p_mi );
539     }
540     return result;
541 }
542
543 int libvlc_vlm_get_media_instance_title( libvlc_instance_t *p_instance,
544                                          const char *psz_name, int i_instance,
545                                          libvlc_exception_t *p_exception )
546 {
547     vlm_media_instance_t *p_mi;
548
549     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
550                                           i_instance, p_exception );
551     if( p_mi )
552         vlm_media_instance_Delete( p_mi );
553     return p_mi ? 0 : -1;
554 }
555
556 int libvlc_vlm_get_media_instance_chapter( libvlc_instance_t *p_instance,
557                                            const char *psz_name,
558                                            int i_instance,
559                                            libvlc_exception_t *p_exception )
560 {
561     vlm_media_instance_t *p_mi;
562
563     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
564                                           i_instance, p_exception );
565     if( p_mi )
566         vlm_media_instance_Delete( p_mi );
567     return p_mi ? 0 : -1;
568 }
569
570 int libvlc_vlm_get_media_instance_seekable( libvlc_instance_t *p_instance,
571                                             const char *psz_name,
572                                             int i_instance,
573                                             libvlc_exception_t *p_exception )
574 {
575     vlm_media_instance_t *p_mi;
576
577     p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name,
578                                           i_instance, p_exception );
579     if( p_mi )
580         vlm_media_instance_Delete( p_mi );
581     return p_mi ? 0 : -1;
582 }
583
584 libvlc_event_manager_t * libvlc_vlm_get_event_manager( libvlc_instance_t *p_instance,
585                                                        libvlc_exception_t *p_exception )
586 {
587     vlm_t *p_vlm;
588     VLM_RET( p_vlm, NULL);
589     return p_instance->p_event_manager;
590 }