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