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 $
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"
25 #include <vlc/libvlc.h>
27 #include <vlc_input.h>
30 static void InitVLM( libvlc_instance_t *p_instance )
33 if( p_instance->p_vlm ) return;
34 p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
36 p_instance->p_vlm = NULL;
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; } }
45 #define GET_MEDIA { p_media = vlm_MediaSearch( p_instance->p_vlm, psz_name );\
48 libvlc_exception_raise( p_exception, \
49 "Media %s does not exist", \
50 psz_name ); return; } }
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 )
61 p_media = vlm_MediaNew( p_instance->p_vlm, psz_name, BROADCAST_TYPE );
64 libvlc_exception_raise( p_exception, "Media %s creation failed",
68 libvlc_vlm_change_media( p_instance, psz_name, psz_input, psz_output,
69 i_options, ppsz_options, b_enabled, b_loop,
74 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, char *psz_name,
75 libvlc_exception_t *p_exception )
78 vlm_message_t *answer;
81 asprintf( &psz_message, "del %s", psz_name );
82 vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
83 if( answer->psz_value )
85 libvlc_exception_raise( p_exception, "Unable to delete %s",
92 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance, char *psz_name,
93 int b_enabled, libvlc_exception_t *p_exception )
99 if( b_enabled != 0 ) b_enabled = 1;
100 p_media->b_enabled = b_enabled;
104 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, char *psz_name,
105 int b_loop, libvlc_exception_t *p_exception )
107 vlm_media_t *p_media;
111 if( b_loop != 0 ) b_loop = 1;
112 p_media->b_loop = b_loop;
116 void libvlc_vlm_set_output( libvlc_instance_t *p_instance, char *psz_name,
117 char *psz_output, libvlc_exception_t *p_exception )
119 vlm_media_t *p_media;
125 vlc_mutex_lock( &p_instance->p_vlm->lock );
126 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
129 libvlc_exception_raise( p_exception, "Unable to set output" );
130 vlc_mutex_unlock( &p_instance->p_vlm->lock );
133 vlc_mutex_unlock( &p_instance->p_vlm->lock );
137 void libvlc_vlm_set_input( libvlc_instance_t *p_instance, char *psz_name,
138 char *psz_input, libvlc_exception_t *p_exception )
140 vlm_media_t *p_media;
144 vlc_mutex_lock( &p_instance->p_vlm->lock );
147 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "inputdel", "all" );
150 libvlc_exception_raise( p_exception, "Unable to change input" );
151 vlc_mutex_unlock( &p_instance->p_vlm->lock );
154 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
157 libvlc_exception_raise( p_exception, "Unable to change input" );
158 vlc_mutex_unlock( &p_instance->p_vlm->lock );
161 vlc_mutex_unlock( &p_instance->p_vlm->lock );
165 void libvlc_vlm_add_input( libvlc_instance_t *p_instance, char *psz_name,
166 char *psz_input, libvlc_exception_t *p_exception )
168 vlm_media_t *p_media;
172 vlc_mutex_lock( &p_instance->p_vlm->lock );
175 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
178 libvlc_exception_raise( p_exception, "Unable to change input" );
179 vlc_mutex_unlock( &p_instance->p_vlm->lock );
183 vlc_mutex_unlock( &p_instance->p_vlm->lock );
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 )
193 vlm_media_t *p_media;
197 vlc_mutex_lock( &p_instance->p_vlm->lock );
199 if( b_enabled != 0 ) b_enabled = 1;
200 if( b_loop != 0 ) b_loop = 1;
202 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
205 libvlc_exception_raise( p_exception, "Unable to set output" );
206 vlc_mutex_unlock( &p_instance->p_vlm->lock );
209 p_media->b_enabled = b_enabled;
210 p_media->b_loop = b_loop;
212 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
215 libvlc_exception_raise( p_exception, "Unable to set output" );
216 vlc_mutex_unlock( &p_instance->p_vlm->lock );
219 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "inputdel", "all" );
222 libvlc_exception_raise( p_exception, "Unable to change input" );
223 vlc_mutex_unlock( &p_instance->p_vlm->lock );
226 i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
229 libvlc_exception_raise( p_exception, "Unable to change input" );
230 vlc_mutex_unlock( &p_instance->p_vlm->lock );
234 vlc_mutex_unlock( &p_instance->p_vlm->lock );
238 void libvlc_vlm_play_media( libvlc_instance_t *p_instance, char *psz_name,
239 libvlc_exception_t *p_exception )
243 vlm_message_t *answer;
246 asprintf( &psz_message, "control %s play", psz_name );
247 vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
248 if( answer->psz_value )
250 libvlc_exception_raise( p_exception, "Unable to play %s",
257 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance, char *psz_name,
258 libvlc_exception_t *p_exception )
262 vlm_message_t *answer;
265 asprintf( &psz_message, "control %s stop", psz_name );
266 vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
267 if( answer->psz_value )
269 libvlc_exception_raise( p_exception, "Unable to stop %s",
276 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance, char *psz_name,
277 libvlc_exception_t *p_exception )
281 vlm_message_t *answer;
284 asprintf( &psz_message, "control %s pause", psz_name );
285 vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
286 if( answer->psz_value )
288 libvlc_exception_raise( p_exception, "Unable to pause %s",
295 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance, char *psz_name,
296 float f_percentage, libvlc_exception_t *p_exception )
300 vlm_message_t *answer;
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 )
307 libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
308 psz_name, f_percentage );
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 ) \
320 vlm_media_instance_t *p_media_instance; \
322 vlm_media_t *p_media; \
323 p_media = vlm_MediaSearch( p_instance->p_vlm, psz_name ); \
324 if ( p_media == NULL ) \
326 libvlc_exception_raise( p_exception, "Unable to find media %s", \
331 if ( i_instance < p_media->i_instance ) \
333 p_media_instance = p_media->instance[ i_instance ]; \
334 return var_Get ## getType( p_media_instance->p_input, #attr );\
338 libvlc_exception_raise( p_exception, "Media index %i out of range",\
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 ) \
350 vlm_message_t *answer; \
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);
364 #undef LIBVLC_VLM_GET_MEDIA_ATTRIBUTE
366 /* local function to be used in libvlc_vlm_show_media only */
367 static char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
368 char* psz_childprefix;
369 char* psz_response="";
372 vlm_message_t *aw_child, **paw_child;
374 asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
376 if ( p_answer->i_child )
378 paw_child = p_answer->child;
379 aw_child = *( paw_child );
380 for( i = 0; i < p_answer->i_child; i++ )
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 )
389 asprintf(&response_tmp, "%s%s", psz_response,
390 recurse_answer(psz_childprefix, aw_child));
391 free( psz_response );
392 psz_response = response_tmp;
395 aw_child = *( paw_child );
398 free( psz_childprefix );
402 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
403 libvlc_exception_t *p_exception )
406 vlm_message_t *answer;
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 )
416 libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
417 psz_name, answer->psz_value );
423 psz_response = recurse_answer( "", answer );
427 return(psz_response );