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