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