1 /*****************************************************************************
2 * vlm.c: libvlc new API VLM handling functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
28 #include <vlc_input.h>
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="";
38 vlm_message_t *aw_child, **paw_child;
40 asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
42 if ( p_answer->i_child )
44 paw_child = p_answer->child;
45 aw_child = *( paw_child );
46 for( i = 0; i < p_answer->i_child; i++ )
48 asprintf( &response_tmp, "%s%s%s:%s\n",
49 psz_response, psz_prefix, aw_child->psz_name,
50 aw_child->psz_value );
52 psz_response = response_tmp;
53 if ( aw_child->i_child )
55 asprintf(&response_tmp, "%s%s", psz_response,
56 recurse_answer(psz_childprefix, aw_child));
58 psz_response = response_tmp;
61 aw_child = *( paw_child );
64 free( psz_childprefix );
68 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
69 libvlc_exception_t *p_exception )
72 vlm_message_t *answer;
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 )
82 libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
83 psz_name, answer->psz_value );
89 psz_response = recurse_answer( "", answer );
93 return(psz_response );
95 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
101 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
102 libvlc_exception_t *p_exception )
105 if( !p_instance->p_vlm )
106 p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
108 libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
112 if( !p_instance->p_vlm )
114 libvlc_exception_raise( p_exception,
115 "Unable to create VLM." );
120 #define VLM_RET(p,ret) do { \
121 if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
122 (p) = p_instance->p_vlm; \
124 #define VLM(p) VLM_RET(p,)
126 static vlm_media_instance_t *libvlc_vlm_get_media_instance( libvlc_instance_t *p_instance,
127 char *psz_name, int i_minstance_idx,
128 libvlc_exception_t *p_exception )
131 vlm_media_instance_t **pp_minstance;
132 vlm_media_instance_t *p_minstance;
136 VLM_RET(p_vlm, NULL);
138 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
139 vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance, &i_minstance ) )
141 libvlc_exception_raise( p_exception, "Unable to get %s instances", psz_name );
145 if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
147 p_minstance = pp_minstance[i_minstance_idx];
148 TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
150 while( i_minstance > 0 )
151 vlm_media_instance_Delete( pp_minstance[--i_minstance] );
152 TAB_CLEAN( i_minstance, pp_minstance );
157 void libvlc_vlm_release( libvlc_instance_t *p_instance, libvlc_exception_t *p_exception)
167 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance, char *psz_name,
168 char *psz_input, char *psz_output,
169 int i_options, char **ppsz_options,
170 int b_enabled, int b_loop,
171 libvlc_exception_t *p_exception )
179 vlm_media_Init( &m );
180 m.psz_name = strdup( psz_name );
181 m.b_enabled = b_enabled;
183 m.broadcast.b_loop = b_loop;
185 TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
187 m.psz_output = strdup( psz_output );
188 for( n = 0; n < i_options; n++ )
189 TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
191 if( vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL ) )
193 vlm_media_Clean( &m );
194 libvlc_exception_raise( p_exception, "Media %s creation failed", psz_name );
196 vlm_media_Clean( &m );
199 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, char *psz_name,
200 libvlc_exception_t *p_exception )
207 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
208 vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
210 libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
214 #define VLM_CHANGE(psz_error, code ) do { \
215 vlm_media_t *p_media; \
219 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) || \
220 vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) { \
221 libvlc_exception_raise( p_exception, psz_error, psz_name ); \
224 if( !p_media ) goto error; \
228 if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) { \
229 vlm_media_Delete( p_media ); \
232 vlm_media_Delete( p_media ); \
235 libvlc_exception_raise( p_exception, psz_error, psz_name );\
238 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance, char *psz_name,
239 int b_enabled, libvlc_exception_t *p_exception )
241 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
242 VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
243 #undef VLM_CHANGE_CODE
246 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, char *psz_name,
247 int b_loop, libvlc_exception_t *p_exception )
249 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
250 VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
251 #undef VLM_CHANGE_CODE
254 void libvlc_vlm_set_output( libvlc_instance_t *p_instance, char *psz_name,
255 char *psz_output, libvlc_exception_t *p_exception )
257 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
258 p_media->psz_output = strdup( psz_output ); }
259 VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
260 #undef VLM_CHANGE_CODE
263 void libvlc_vlm_set_input( libvlc_instance_t *p_instance, char *psz_name,
264 char *psz_input, libvlc_exception_t *p_exception )
266 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
267 free( p_media->ppsz_input[--p_media->i_input] );\
268 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
269 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
270 #undef VLM_CHANGE_CODE
273 void libvlc_vlm_add_input( libvlc_instance_t *p_instance, char *psz_name,
274 char *psz_input, libvlc_exception_t *p_exception )
276 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
277 VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
278 #undef VLM_CHANGE_CODE
281 void libvlc_vlm_change_media( libvlc_instance_t *p_instance, char *psz_name,
282 char *psz_input, char *psz_output, int i_options,
283 char **ppsz_options, int b_enabled, int b_loop,
284 libvlc_exception_t *p_exception )
286 #define VLM_CHANGE_CODE { int n; \
287 p_media->b_enabled = b_enabled; \
288 p_media->broadcast.b_loop = b_loop; \
289 while( p_media->i_input > 0 ) \
290 free( p_media->ppsz_input[--p_media->i_input] ); \
292 TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
293 free( p_media->psz_output ); \
294 p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
295 while( p_media->i_option > 0 ) \
296 free( p_media->ppsz_option[--p_media->i_option] ); \
297 for( n = 0; n < i_options; n++ ) \
298 TAB_APPEND( p_media->i_option, p_media->ppsz_option, strdup(ppsz_options[n]) ); \
300 VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
301 #undef VLM_CHANGE_CODE
304 void libvlc_vlm_play_media( libvlc_instance_t *p_instance, char *psz_name,
305 libvlc_exception_t *p_exception )
312 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
313 vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
315 libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
319 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance, char *psz_name,
320 libvlc_exception_t *p_exception )
327 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
328 vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
330 libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
334 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance, char *psz_name,
335 libvlc_exception_t *p_exception )
342 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
343 vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
345 libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
349 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance, char *psz_name,
350 float f_percentage, libvlc_exception_t *p_exception )
357 if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
358 vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL, f_percentage ) )
360 libvlc_exception_raise( p_exception, "Unable to seek %s to %f", psz_name, f_percentage );
364 #define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, ret, code )\
365 returnType libvlc_vlm_get_media_## attr( libvlc_instance_t *p_instance, \
366 char *psz_name, int i_instance, libvlc_exception_t *p_exception ) \
368 vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance, \
371 returnType ret_value; \
373 vlm_media_instance_Delete( p_mi ); \
376 libvlc_exception_raise( p_exception, "Unable to get %s "#attr "attribute" ); \
380 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( position, float, Float, -1, ret_value = p_mi->d_position; );
381 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( time, int, Integer, -1, ret_value = p_mi->i_time );
382 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( length, int, Integer, -1, ret_value = p_mi->i_length );
383 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( rate, int, Integer, -1, ret_value = p_mi->i_rate );
384 /* FIXME extend vlm_media_instance_t to be able to implement them */
385 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( title, int, Integer, 0, ret_value = 0 );
386 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( chapter, int, Integer, 0, ret_value = 0 );
387 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( seekable, int, Bool, 0, ret_value = VLC_FALSE );
389 #undef LIBVLC_VLM_GET_MEDIA_ATTRIBUTE
391 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
392 libvlc_exception_t *p_exception )
394 /* FIXME is it needed ? */
395 libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );