c21dd000bdda404d2cd1aa4b6be1f4da657597a2
[vlc.git] / src / control / vlm.c
1 /*****************************************************************************
2  * vlm.c: libvlc new API VLM handling functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id: playlist.c 14265 2006-02-12 17:31:39Z zorglub $
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_es.h>
26 #include <vlc_input.h>
27 #include <vlc_vlm.h>
28
29 #include "libvlc_internal.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 #ifdef ENABLE_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 #endif
95     return NULL;
96 }
97 #endif
98
99 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
100                             libvlc_exception_t *p_exception )
101 {
102 #ifdef ENABLE_VLM
103     if( !p_instance->p_vlm )
104         p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
105 #endif
106
107     if( !p_instance->p_vlm )
108     {
109         libvlc_exception_raise( p_exception,
110                                 "Unable to create VLM. It might be disabled." );
111         return VLC_EGENERIC;
112     }
113     return VLC_SUCCESS;
114 }
115 #define VLM_RET(p,ret) do {                                     \
116     if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
117     (p) = p_instance->p_vlm;                                    \
118   } while(0)
119 #define VLM(p) VLM_RET(p,)
120
121 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance, char *psz_name,
122                                char *psz_input, char *psz_output,
123                                int i_options, char **ppsz_options,
124                                int b_enabled, int b_loop,
125                                libvlc_exception_t *p_exception )
126 {
127     vlm_t *p_vlm;
128     vlm_media_t m;
129     int n;
130
131     VLM(p_vlm);
132
133     vlm_media_Init( &m );
134     m.psz_name = strdup( psz_name );
135     m.b_enabled = b_enabled;
136     m.b_vod = VLC_FALSE;
137     m.broadcast.b_loop = b_loop;
138     if( psz_input )
139         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
140     if( psz_output )
141         m.psz_output = strdup( psz_output );
142     for( n = 0; n < i_options; n++ )
143         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
144
145     if( vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL ) )
146     {
147         vlm_media_Clean( &m );
148         libvlc_exception_raise( p_exception, "Media %s creation failed", psz_name );
149     }
150     vlm_media_Clean( &m );
151 }
152
153 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, char *psz_name,
154                            libvlc_exception_t *p_exception )
155 {
156     vlm_t *p_vlm;
157     int64_t id;
158
159     VLM(p_vlm);
160
161     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
162         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
163     {
164         libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
165     }
166 }
167
168 #define VLM_CHANGE(psz_error, code ) do {   \
169     vlm_media_t *p_media;   \
170     vlm_t *p_vlm;           \
171     int64_t id;             \
172     VLM(p_vlm);             \
173     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||    \
174         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) {       \
175         libvlc_exception_raise( p_exception, psz_error, psz_name ); \
176         return;             \
177     }                       \
178     if( !p_media ) goto error;                                      \
179                             \
180     code;                   \
181                             \
182     if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) {         \
183         vlm_media_Delete( p_media );                                \
184         goto error;         \
185     }                       \
186     vlm_media_Delete( p_media );                                    \
187     return;                 \
188   error:                    \
189     libvlc_exception_raise( p_exception, psz_error, psz_name );\
190   } while(0)
191
192 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance, char *psz_name,
193                              int b_enabled, libvlc_exception_t *p_exception )
194 {
195 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
196     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
197 #undef VLM_CHANGE_CODE
198 }
199
200 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, char *psz_name,
201                           int b_loop, libvlc_exception_t *p_exception )
202 {
203 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
204     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
205 #undef VLM_CHANGE_CODE
206 }
207
208 void libvlc_vlm_set_output( libvlc_instance_t *p_instance, char *psz_name,
209                             char *psz_output,  libvlc_exception_t *p_exception )
210 {
211 #define VLM_CHANGE_CODE { if( p_media->psz_output ) free( p_media->psz_output ); \
212                           p_media->psz_output = strdup( psz_output ); }
213     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
214 #undef VLM_CHANGE_CODE
215 }
216
217 void libvlc_vlm_set_input( libvlc_instance_t *p_instance, char *psz_name,
218                            char *psz_input,  libvlc_exception_t *p_exception )
219 {
220 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
221                             free( p_media->ppsz_input[--p_media->i_input] );\
222                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
223     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
224 #undef VLM_CHANGE_CODE
225 }
226
227 void libvlc_vlm_add_input( libvlc_instance_t *p_instance, char *psz_name,
228                            char *psz_input,  libvlc_exception_t *p_exception )
229 {
230 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
231     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
232 #undef VLM_CHANGE_CODE
233 }
234
235
236 void libvlc_vlm_change_media( libvlc_instance_t *p_instance, char *psz_name,
237                               char *psz_input, char *psz_output, int i_options,
238                               char **ppsz_options, int b_enabled, int b_loop,
239                               libvlc_exception_t *p_exception )
240 {
241 #define VLM_CHANGE_CODE { int n;        \
242     p_media->b_enabled = b_enabled;     \
243     p_media->broadcast.b_loop = b_loop; \
244     while( p_media->i_input > 0 )       \
245         free( p_media->ppsz_input[--p_media->i_input] );    \
246     if( psz_input )                     \
247         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
248     if( p_media->psz_output )           \
249         free( p_media->psz_output );    \
250     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
251     while( p_media->i_option > 0 )     \
252         free( p_media->ppsz_option[--p_media->i_option] );        \
253     for( n = 0; n < i_options; n++ )    \
254         TAB_APPEND( p_media->i_option, p_media->ppsz_option, strdup(ppsz_options[n]) );   \
255   }
256     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
257 #undef VLM_CHANGE_CODE
258 }
259
260 void libvlc_vlm_play_media( libvlc_instance_t *p_instance, char *psz_name,
261                             libvlc_exception_t *p_exception )
262  
263 {
264     vlm_t *p_vlm;
265     int64_t id;
266
267     VLM(p_vlm);
268
269     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
270         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
271     {
272         libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
273     }
274 }
275
276 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance, char *psz_name,
277                             libvlc_exception_t *p_exception )
278  
279 {
280     vlm_t *p_vlm;
281     int64_t id;
282
283     VLM(p_vlm);
284
285     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
286         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
287     {
288         libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
289     }
290 }
291
292 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance, char *psz_name,
293                             libvlc_exception_t *p_exception )
294  
295 {
296     vlm_t *p_vlm;
297     int64_t id;
298
299     VLM(p_vlm);
300
301     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
302         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
303     {
304         libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
305     }
306 }
307
308 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance, char *psz_name,
309                             float f_percentage, libvlc_exception_t *p_exception )
310  
311 {
312     vlm_t *p_vlm;
313     int64_t id;
314
315     VLM(p_vlm);
316
317     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
318         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL, f_percentage ) )
319     {
320         libvlc_exception_raise( p_exception, "Unable to seek %s to %f", psz_name, f_percentage );
321     }
322 }
323
324 static vlm_media_instance_t *libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
325                                                             char *psz_name, int i_minstance_idx,
326                                                             libvlc_exception_t *p_exception )
327 {
328     vlm_t *p_vlm;
329     vlm_media_instance_t **pp_minstance;
330     vlm_media_instance_t *p_minstance;
331     int i_minstance;
332     int64_t id;
333
334     VLM_RET(p_vlm, NULL);
335
336     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
337         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance, &i_minstance ) )
338     {
339         libvlc_exception_raise( p_exception, "Unable to get %s instances", psz_name );
340         return NULL;
341     }
342     p_minstance = NULL;
343     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
344     {
345         p_minstance = pp_minstance[i_minstance_idx];
346         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
347     }
348     while( i_minstance > 0 )
349         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
350     TAB_CLEAN( i_minstance, pp_minstance );
351     return p_minstance;
352 }
353
354 #define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, ret, code )\
355 returnType libvlc_vlm_get_media_## attr( libvlc_instance_t *p_instance, \
356                         char *psz_name, int i_instance, libvlc_exception_t *p_exception ) \
357 { \
358     vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance, \
359                                                                 p_exception );  \
360     if( p_mi ) {                            \
361         returnType ret_value;               \
362         code;                               \
363         vlm_media_instance_Delete( p_mi );  \
364         return ret_value;                   \
365     }                                       \
366     libvlc_exception_raise( p_exception, "Unable to get %s "#attr "attribute" ); \
367     return ret; \
368 }
369
370 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( position, float, Float, -1, ret_value = p_mi->d_position; );
371 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( time,     int, Integer, -1, ret_value = p_mi->i_time );
372 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( length,   int, Integer, -1, ret_value = p_mi->i_length );
373 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( rate,     int, Integer, -1, ret_value = p_mi->i_rate );
374 /* FIXME extend vlm_media_instance_t to be able to implement them */
375 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( title,    int, Integer,  0, ret_value = 0 );
376 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( chapter,  int, Integer,  0, ret_value = 0 );
377 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( seekable, int, Bool,     0, ret_value = VLC_FALSE );
378
379 #undef LIBVLC_VLM_GET_MEDIA_ATTRIBUTE
380
381 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
382                              libvlc_exception_t *p_exception )
383 {
384     /* FIXME is it needed ? */
385     libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );
386     return NULL;
387 }
388