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