* don't use nested functions please. --- fixes compilation on OSX, which was broken...
[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 <libvlc_internal.h>
25 #include <vlc/libvlc.h>
26
27 #include <vlc/vlc.h>
28 #include <vlc_es.h>
29 #include <vlc_input.h>
30 #include <vlc_vlm.h>
31
32 void InitVLM( libvlc_instance_t *p_instance )
33 {
34 #ifdef ENABLE_VLM
35     if( p_instance->p_vlm ) return;
36     p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
37 #else
38     p_instance->p_vlm = NULL;
39 #endif
40 }
41
42 #define CHECK_VLM { if( !p_instance->p_vlm ) InitVLM( p_instance ); \
43                     if( !p_instance->p_vlm ) {\
44                   libvlc_exception_raise( p_exception, \
45                   "Unable to create VLM. It might be disabled." ); return; } }
46
47 #define GET_MEDIA { p_media = vlm_MediaSearch( p_instance->p_vlm, psz_name );\
48                    if( !p_media ) \
49                    { \
50                         libvlc_exception_raise( p_exception, \
51                                                 "Media %s does not exist", \
52                                                 psz_name ); return; } }
53
54 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance, char *psz_name,
55                                char *psz_input, char *psz_output,
56                                int i_options, char **ppsz_options,
57                                int b_enabled, int b_loop,
58                                libvlc_exception_t *p_exception )
59 {
60     vlm_media_t *p_media;
61     CHECK_VLM;
62
63     p_media = vlm_MediaNew( p_instance->p_vlm, psz_name, BROADCAST_TYPE );
64     if( !p_media )
65     {
66         libvlc_exception_raise( p_exception, "Media %s creation failed",
67                                 psz_name );
68         return;
69     }
70     libvlc_vlm_change_media( p_instance, psz_name, psz_input, psz_output,
71                              i_options, ppsz_options, b_enabled, b_loop,
72                              p_exception );
73
74 }
75
76 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, char *psz_name,
77                            libvlc_exception_t *p_exception )
78 {
79     char *psz_message;
80     vlm_message_t *answer;
81     CHECK_VLM;
82 #ifdef ENABLE_VLM
83     asprintf( &psz_message, "del %s", psz_name );
84     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
85     if( answer->psz_value )
86     {
87         libvlc_exception_raise( p_exception, "Unable to delete %s",
88                                 psz_name );
89     }
90     free( psz_message);
91 #endif
92 }
93
94 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance, char *psz_name,
95                              int b_enabled, libvlc_exception_t *p_exception )
96 {
97     vlm_media_t *p_media;
98     CHECK_VLM;
99 #ifdef ENABLE_VLM
100     GET_MEDIA;
101     if( b_enabled != 0 ) b_enabled = 1;
102     p_media->b_enabled = b_enabled;
103 #endif
104 }
105
106 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, char *psz_name,
107                           int b_loop, libvlc_exception_t *p_exception )
108 {
109     vlm_media_t *p_media;
110     CHECK_VLM;
111 #ifdef ENABLE_VLM
112     GET_MEDIA;
113     if( b_loop != 0 ) b_loop = 1;
114     p_media->b_loop = b_loop;
115 #endif
116 }
117
118 void libvlc_vlm_set_output( libvlc_instance_t *p_instance, char *psz_name,
119                             char *psz_output,  libvlc_exception_t *p_exception )
120 {
121     vlm_media_t *p_media;
122     int i_ret;
123     CHECK_VLM;
124 #ifdef ENABLE_VLM
125     GET_MEDIA;
126
127     vlc_mutex_lock( &p_instance->p_vlm->lock );
128     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
129     if( i_ret )
130     {
131         libvlc_exception_raise( p_exception, "Unable to set output" );
132         vlc_mutex_unlock( &p_instance->p_vlm->lock );
133         return;
134     }
135     vlc_mutex_unlock( &p_instance->p_vlm->lock );
136 #endif
137 }
138
139 void libvlc_vlm_set_input( libvlc_instance_t *p_instance, char *psz_name,
140                            char *psz_input,  libvlc_exception_t *p_exception )
141 {
142     vlm_media_t *p_media;
143     int i_ret;
144     CHECK_VLM;
145 #ifdef ENABLE_VLM
146     vlc_mutex_lock( &p_instance->p_vlm->lock );
147     GET_MEDIA;
148
149     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "inputdel", "all" );
150     if( i_ret )
151     {
152         libvlc_exception_raise( p_exception, "Unable to change input" );
153         vlc_mutex_unlock( &p_instance->p_vlm->lock );
154         return;
155     }
156     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
157     if( i_ret )
158     {
159         libvlc_exception_raise( p_exception, "Unable to change input" );
160         vlc_mutex_unlock( &p_instance->p_vlm->lock );
161         return;
162     }
163     vlc_mutex_unlock( &p_instance->p_vlm->lock );
164 #endif
165 }
166
167 void libvlc_vlm_add_input( libvlc_instance_t *p_instance, char *psz_name,
168                            char *psz_input,  libvlc_exception_t *p_exception )
169 {
170     vlm_media_t *p_media;
171     int i_ret;
172     CHECK_VLM;
173 #ifdef ENABLE_VLM
174     vlc_mutex_lock( &p_instance->p_vlm->lock );
175     GET_MEDIA;
176
177     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
178     if( i_ret )
179     {
180         libvlc_exception_raise( p_exception, "Unable to change input" );
181         vlc_mutex_unlock( &p_instance->p_vlm->lock );
182         return;
183     }
184
185     vlc_mutex_unlock( &p_instance->p_vlm->lock );
186 #endif
187 }
188
189
190 void libvlc_vlm_change_media( libvlc_instance_t *p_instance, char *psz_name,
191                               char *psz_input, char *psz_output, int i_options,
192                               char **ppsz_options, int b_enabled, int b_loop,
193                               libvlc_exception_t *p_exception )
194 {
195     vlm_media_t *p_media;
196     int i_ret;
197     CHECK_VLM;
198 #ifdef ENABLE_VLM
199     vlc_mutex_lock( &p_instance->p_vlm->lock );
200     GET_MEDIA;
201     if( b_enabled != 0 ) b_enabled = 1;
202     if( b_loop != 0 ) b_loop = 1;
203
204     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
205     if( i_ret )
206     {
207         libvlc_exception_raise( p_exception, "Unable to set output" );
208         vlc_mutex_unlock( &p_instance->p_vlm->lock );
209         return;
210     }
211     p_media->b_enabled = b_enabled;
212     p_media->b_loop = b_loop;
213
214     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
215     if( i_ret )
216     {
217         libvlc_exception_raise( p_exception, "Unable to set output" );
218         vlc_mutex_unlock( &p_instance->p_vlm->lock );
219         return;
220     }
221     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "inputdel", "all" );
222     if( i_ret )
223     {
224         libvlc_exception_raise( p_exception, "Unable to change input" );
225         vlc_mutex_unlock( &p_instance->p_vlm->lock );
226         return;
227     }
228     i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
229     if( i_ret )
230     {
231         libvlc_exception_raise( p_exception, "Unable to change input" );
232         vlc_mutex_unlock( &p_instance->p_vlm->lock );
233         return;
234     }
235
236     vlc_mutex_unlock( &p_instance->p_vlm->lock );
237 #endif
238 }
239
240 void libvlc_vlm_play_media( libvlc_instance_t *p_instance, char *psz_name,
241                             libvlc_exception_t *p_exception )
242     
243 {
244     char *psz_message;
245     vlm_message_t *answer;
246     CHECK_VLM;
247 #ifdef ENABLE_VLM
248     asprintf( &psz_message, "control %s play", psz_name );
249     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
250     if( answer->psz_value )
251     {
252         libvlc_exception_raise( p_exception, "Unable to play %s",
253                                 psz_name );
254     }
255     free( psz_message);
256 #endif
257 }
258
259 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance, char *psz_name,
260                             libvlc_exception_t *p_exception )
261     
262 {
263     char *psz_message;
264     vlm_message_t *answer;
265     CHECK_VLM;
266 #ifdef ENABLE_VLM
267     asprintf( &psz_message, "control %s stop", psz_name );
268     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
269     if( answer->psz_value )
270     {
271         libvlc_exception_raise( p_exception, "Unable to stop %s",
272                                 psz_name );
273     }
274     free( psz_message);
275 #endif
276 }
277
278 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance, char *psz_name,
279                             libvlc_exception_t *p_exception )
280     
281 {
282     char *psz_message;
283     vlm_message_t *answer;
284     CHECK_VLM;
285 #ifdef ENABLE_VLM
286     asprintf( &psz_message, "control %s pause", psz_name );
287     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
288     if( answer->psz_value )
289     {
290         libvlc_exception_raise( p_exception, "Unable to pause %s",
291                                 psz_name );
292     }
293     free( psz_message );
294 #endif
295 }
296
297 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance, char *psz_name,
298                             float f_percentage, libvlc_exception_t *p_exception )
299     
300 {
301     char *psz_message;
302     vlm_message_t *answer;
303     CHECK_VLM;
304 #ifdef ENABLE_VLM
305     asprintf( &psz_message, "control %s seek %f", psz_name, f_percentage );
306     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
307     if( answer->psz_value )
308     {
309         libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
310                                 psz_name, f_percentage );
311     }
312     free( psz_message );
313 #endif
314 }
315
316 #ifdef ENABLE_VLM
317 #define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, default)\
318 returnType libvlc_vlm_get_media_## attr( libvlc_instance_t *p_instance, \
319                         char *psz_name, int i_instance, \
320                         libvlc_exception_t *p_exception ) \
321 { \
322     vlm_media_instance_t *p_media_instance; \
323     CHECK_VLM; \
324     vlm_media_t *p_media; \
325     p_media = vlm_MediaSearch( p_instance->p_vlm, psz_name ); \
326     if ( p_media == NULL ) \
327     { \
328         libvlc_exception_raise( p_exception, "Unable to find media %s", \
329                                 psz_name); \
330     } \
331     else \
332     { \
333         if ( i_instance < p_media->i_instance ) \
334         { \
335             p_media_instance = p_media->instance[ i_instance ]; \
336             return var_Get ## getType( p_media_instance->p_input, #attr );\
337         } \
338         else \
339         { \
340             libvlc_exception_raise( p_exception, "Media index %i out of range",\
341                                     i_instance); \
342         } \
343     } \
344     return default; \
345 }
346 #else
347 #define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, default)\
348 returnType libvlc_vlm_get_media_## attr( libvlc_instance_t *p_instance, \
349                         char *psz_name, int i_instance, libvlc_exception_t *p_exception ) \
350 { \
351     char *psz_message; \
352     vlm_message_t *answer; \
353     CHECK_VLM; \
354     return default; \
355 }
356 #endif
357
358 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( position, float, Float, -1);
359 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( time, int, Integer, -1);
360 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( length, int, Integer, -1);
361 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( rate, int, Integer, -1);
362 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( title, int, Integer, 0);
363 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( chapter, int, Integer, 0);
364 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( seekable, int, Bool, 0);
365
366 #undef LIBVLC_VLM_GET_MEDIA_ATTRIBUTE
367
368 /* local function to be used in libvlc_vlm_show_media only */
369 char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
370     char* psz_childprefix;
371     char* psz_response="";
372     char* response_tmp;
373     int i;
374     vlm_message_t *aw_child, **paw_child;
375     
376     asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
377     
378     if ( p_answer->i_child )
379     {
380         paw_child = p_answer->child;
381         aw_child = *( paw_child );
382         for( i = 0; i < p_answer->i_child; i++ )
383         {
384             asprintf( &response_tmp, "%s%s%s:%s\n",
385                       psz_response, psz_prefix, aw_child->psz_name,
386                       aw_child->psz_value );
387             free( psz_response );
388             psz_response = response_tmp;
389             if ( aw_child->i_child )
390             {
391                 asprintf(&response_tmp, "%s%s", psz_response,
392                          recurse_answer(psz_childprefix, aw_child));
393                 free( psz_response );
394                 psz_response = response_tmp;
395             }
396             paw_child++;
397             aw_child = *( paw_child );
398         }
399     }
400     free( psz_childprefix );
401     return psz_response;
402 }
403
404 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
405                              libvlc_exception_t *p_exception )
406 {
407     char *psz_message;
408     vlm_message_t *answer;
409     char *psz_response;
410
411     CHECK_VLM;
412 #ifdef ENABLE_VLM
413     asprintf( &psz_message, "show %s", psz_name );
414     asprintf( &psz_response, "", psz_name );
415     vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
416     if( answer->psz_value )
417     {
418         libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
419                                 psz_name, answer->psz_value );
420     }
421     else
422     {
423         if ( answer->child )
424         {
425             psz_response = recurse_answer( "", answer );
426         }
427     }
428     free( psz_message );
429     return(psz_response );
430 #endif
431 }