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