]> git.sesse.net Git - vlc/blob - src/control/vlm.c
Fix VLM double free
[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$
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
26 #include <vlc/libvlc.h>
27 #include <vlc_es.h>
28 #include <vlc_input.h>
29 #include <vlc_vlm.h>
30
31 #if 0
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="";
36     char* response_tmp;
37     int i;
38     vlm_message_t *aw_child, **paw_child;
39
40     asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
41
42     if ( p_answer->i_child )
43     {
44         paw_child = p_answer->child;
45         aw_child = *( paw_child );
46         for( i = 0; i < p_answer->i_child; i++ )
47         {
48             asprintf( &response_tmp, "%s%s%s:%s\n",
49                       psz_response, psz_prefix, aw_child->psz_name,
50                       aw_child->psz_value );
51             free( psz_response );
52             psz_response = response_tmp;
53             if ( aw_child->i_child )
54             {
55                 asprintf(&response_tmp, "%s%s", psz_response,
56                          recurse_answer(psz_childprefix, aw_child));
57                 free( psz_response );
58                 psz_response = response_tmp;
59             }
60             paw_child++;
61             aw_child = *( paw_child );
62         }
63     }
64     free( psz_childprefix );
65     return psz_response;
66 }
67
68 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
69                              libvlc_exception_t *p_exception )
70 {
71     char *psz_message;
72     vlm_message_t *answer;
73     char *psz_response;
74
75     CHECK_VLM;
76 #ifdef ENABLE_VLM
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 )
81     {
82         libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
83                                 psz_name, answer->psz_value );
84     }
85     else
86     {
87         if ( answer->child )
88         {
89             psz_response = recurse_answer( "", answer );
90         }
91     }
92     free( psz_message );
93     return(psz_response );
94 #else
95     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
96     return NULL;
97 #endif
98 }
99 #endif /* 0 */
100
101 static int libvlc_vlm_init( libvlc_instance_t *p_instance,
102                             libvlc_exception_t *p_exception )
103 {
104 #ifdef ENABLE_VLM
105     if( !p_instance->p_vlm )
106         p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
107 #else
108     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
109     return VLC_EGENERIC;
110 #endif
111
112     if( !p_instance->p_vlm )
113     {
114         libvlc_exception_raise( p_exception,
115                                 "Unable to create VLM." );
116         return VLC_EGENERIC;
117     }
118     return VLC_SUCCESS;
119 }
120 #define VLM_RET(p,ret) do {                                     \
121     if( libvlc_vlm_init( p_instance, p_exception ) ) return ret;\
122     (p) = p_instance->p_vlm;                                    \
123   } while(0)
124 #define VLM(p) VLM_RET(p,)
125
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 )
129 {
130 #ifdef ENABLE_VLM
131     vlm_t *p_vlm;
132     vlm_media_instance_t **pp_minstance;
133     vlm_media_instance_t *p_minstance;
134     int i_minstance;
135     int64_t id;
136
137     VLM_RET(p_vlm, NULL);
138
139     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
140         vlm_Control( p_vlm, VLM_GET_MEDIA_INSTANCES, id, &pp_minstance, &i_minstance ) )
141     {
142         libvlc_exception_raise( p_exception, "Unable to get %s instances", psz_name );
143         return NULL;
144     }
145     p_minstance = NULL;
146     if( i_minstance_idx >= 0 && i_minstance_idx < i_minstance )
147     {
148         p_minstance = pp_minstance[i_minstance_idx];
149         TAB_REMOVE( i_minstance, pp_minstance, p_minstance );
150     }
151     while( i_minstance > 0 )
152         vlm_media_instance_Delete( pp_minstance[--i_minstance] );
153     TAB_CLEAN( i_minstance, pp_minstance );
154     return p_minstance;
155 #else
156     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
157     return VLC_EGENERIC;
158 #endif
159 }
160
161
162 void libvlc_vlm_release( libvlc_instance_t *p_instance, libvlc_exception_t *p_exception)
163 {
164 #ifdef ENABLE_VLM
165     vlm_t *p_vlm;
166
167     VLM(p_vlm);
168
169     vlm_Delete( p_vlm );
170 #else
171     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
172     return VLC_EGENERIC;
173 #endif
174 }
175
176
177 void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance, char *psz_name,
178                                char *psz_input, char *psz_output,
179                                int i_options, char **ppsz_options,
180                                int b_enabled, int b_loop,
181                                libvlc_exception_t *p_exception )
182 {
183 #ifdef ENABLE_VLM
184     vlm_t *p_vlm;
185     vlm_media_t m;
186     int n;
187
188     VLM(p_vlm);
189
190     vlm_media_Init( &m );
191     m.psz_name = strdup( psz_name );
192     m.b_enabled = b_enabled;
193     m.b_vod = false;
194     m.broadcast.b_loop = b_loop;
195     if( psz_input )
196         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
197     if( psz_output )
198         m.psz_output = strdup( psz_output );
199     for( n = 0; n < i_options; n++ )
200         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
201
202     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
203     vlm_media_Clean( &m );
204     if( n )
205         libvlc_exception_raise( p_exception, "Media %s creation failed", psz_name );
206 #else
207     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
208     return VLC_EGENERIC;
209 #endif
210 }
211
212 void libvlc_vlm_add_vod( libvlc_instance_t *p_instance, char *psz_name,
213                          char *psz_input, int i_options,
214                          char **ppsz_options, int b_enabled,
215                          char *psz_mux, libvlc_exception_t *p_exception )
216 {
217 #ifdef ENABLE_VLM
218     vlm_t *p_vlm;
219     vlm_media_t m;
220     int n;
221
222     VLM(p_vlm);
223
224     vlm_media_Init( &m );
225     m.psz_name = strdup( psz_name );
226     m.b_enabled = b_enabled;
227     m.b_vod = true;
228     m.vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL;
229     if( psz_input )
230         TAB_APPEND( m.i_input, m.ppsz_input, strdup(psz_input) );
231     for( n = 0; n < i_options; n++ )
232         TAB_APPEND( m.i_option, m.ppsz_option, strdup(ppsz_options[n]) );
233
234     n = vlm_Control( p_vlm, VLM_ADD_MEDIA, &m, NULL );
235     vlm_media_Clean( &m );
236     if( n )
237         libvlc_exception_raise( p_exception, "Media %s creation failed", psz_name );
238 #else
239     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
240     return VLC_EGENERIC;
241 #endif
242 }
243
244 void libvlc_vlm_del_media( libvlc_instance_t *p_instance, char *psz_name,
245                            libvlc_exception_t *p_exception )
246 {
247 #ifdef ENABLE_VLM
248     vlm_t *p_vlm;
249     int64_t id;
250
251     VLM(p_vlm);
252
253     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
254         vlm_Control( p_vlm, VLM_DEL_MEDIA, id ) )
255     {
256         libvlc_exception_raise( p_exception, "Unable to delete %s", psz_name );
257     }
258 #else
259     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
260     return VLC_EGENERIC;
261 #endif
262 }
263
264 #define VLM_CHANGE(psz_error, code ) do {   \
265     vlm_media_t *p_media;   \
266     vlm_t *p_vlm;           \
267     int64_t id;             \
268     VLM(p_vlm);             \
269     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||    \
270         vlm_Control( p_vlm, VLM_GET_MEDIA, id, &p_media ) ) {       \
271         libvlc_exception_raise( p_exception, psz_error, psz_name ); \
272         return;             \
273     }                       \
274     if( !p_media ) goto error;                                      \
275                             \
276     code;                   \
277                             \
278     if( vlm_Control( p_vlm, VLM_CHANGE_MEDIA, p_media ) ) {         \
279         vlm_media_Delete( p_media );                                \
280         goto error;         \
281     }                       \
282     vlm_media_Delete( p_media );                                    \
283     return;                 \
284   error:                    \
285     libvlc_exception_raise( p_exception, psz_error, psz_name );\
286   } while(0)
287
288 void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance, char *psz_name,
289                              int b_enabled, libvlc_exception_t *p_exception )
290 {
291 #ifdef ENABLE_VLM
292 #define VLM_CHANGE_CODE { p_media->b_enabled = b_enabled; }
293     VLM_CHANGE( "Unable to delete %s", VLM_CHANGE_CODE );
294 #undef VLM_CHANGE_CODE
295 #else
296     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
297     return VLC_EGENERIC;
298 #endif
299 }
300
301 void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, char *psz_name,
302                           int b_loop, libvlc_exception_t *p_exception )
303 {
304 #ifdef ENABLE_VLM
305 #define VLM_CHANGE_CODE { p_media->broadcast.b_loop = b_loop; }
306     VLM_CHANGE( "Unable to change %s loop property", VLM_CHANGE_CODE );
307 #undef VLM_CHANGE_CODE
308 #else
309     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
310     return VLC_EGENERIC;
311 #endif
312 }
313
314 void libvlc_vlm_set_mux( libvlc_instance_t *p_instance, char *psz_name,
315                          char *psz_mux, libvlc_exception_t *p_exception )
316 {
317 #ifdef ENABLE_VLM
318 #define VLM_CHANGE_CODE { if( p_media->b_vod ) { \
319                             free( p_media->vod.psz_mux ); \
320                             p_media->vod.psz_mux = psz_mux ? strdup( psz_mux ) : NULL; \
321                           } }
322     VLM_CHANGE( "Unable to change %s mux property", VLM_CHANGE_CODE );
323 #undef VLM_CHANGE_CODE
324 #else
325     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
326     return VLC_EGENERIC;
327 #endif
328 }
329
330 void libvlc_vlm_set_output( libvlc_instance_t *p_instance, char *psz_name,
331                             char *psz_output,  libvlc_exception_t *p_exception )
332 {
333 #ifdef ENABLE_VLM
334 #define VLM_CHANGE_CODE { free( p_media->psz_output ); \
335                           p_media->psz_output = strdup( psz_output ); }
336     VLM_CHANGE( "Unable to change %s output property", VLM_CHANGE_CODE );
337 #undef VLM_CHANGE_CODE
338 #else
339     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
340     return VLC_EGENERIC;
341 #endif
342 }
343
344 void libvlc_vlm_set_input( libvlc_instance_t *p_instance, char *psz_name,
345                            char *psz_input,  libvlc_exception_t *p_exception )
346 {
347 #ifdef ENABLE_VLM
348 #define VLM_CHANGE_CODE { while( p_media->i_input > 0 ) \
349                             free( p_media->ppsz_input[--p_media->i_input] );\
350                           TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
351     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
352 #undef VLM_CHANGE_CODE
353 #else
354     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
355     return VLC_EGENERIC;
356 #endif
357 }
358
359 void libvlc_vlm_add_input( libvlc_instance_t *p_instance, char *psz_name,
360                            char *psz_input,  libvlc_exception_t *p_exception )
361 {
362 #ifdef ENABLE_VLM
363 #define VLM_CHANGE_CODE { TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); }
364     VLM_CHANGE( "Unable to change %s input property", VLM_CHANGE_CODE );
365 #undef VLM_CHANGE_CODE
366 #else
367     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
368     return VLC_EGENERIC;
369 #endif
370 }
371
372 void libvlc_vlm_change_media( libvlc_instance_t *p_instance, char *psz_name,
373                               char *psz_input, char *psz_output, int i_options,
374                               char **ppsz_options, int b_enabled, int b_loop,
375                               libvlc_exception_t *p_exception )
376 {
377 #ifdef ENABLE_VLM
378 #define VLM_CHANGE_CODE { int n;        \
379     p_media->b_enabled = b_enabled;     \
380     p_media->broadcast.b_loop = b_loop; \
381     while( p_media->i_input > 0 )       \
382         free( p_media->ppsz_input[--p_media->i_input] );    \
383     if( psz_input )                     \
384         TAB_APPEND( p_media->i_input, p_media->ppsz_input, strdup(psz_input) ); \
385     free( p_media->psz_output );        \
386     p_media->psz_output = psz_output ? strdup( psz_output ) : NULL; \
387     while( p_media->i_option > 0 )     \
388         free( p_media->ppsz_option[--p_media->i_option] );        \
389     for( n = 0; n < i_options; n++ )    \
390         TAB_APPEND( p_media->i_option, p_media->ppsz_option, strdup(ppsz_options[n]) );   \
391   }
392     VLM_CHANGE( "Unable to change %s properties", VLM_CHANGE_CODE );
393 #undef VLM_CHANGE_CODE
394 #else
395     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
396     return VLC_EGENERIC;
397 #endif
398 }
399
400 void libvlc_vlm_play_media( libvlc_instance_t *p_instance, char *psz_name,
401                             libvlc_exception_t *p_exception )
402 {
403 #ifdef ENABLE_VLM
404     vlm_t *p_vlm;
405     int64_t id;
406
407     VLM(p_vlm);
408
409     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
410         vlm_Control( p_vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, id, NULL, 0 ) )
411     {
412         libvlc_exception_raise( p_exception, "Unable to play %s", psz_name );
413     }
414 #else
415     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
416     return VLC_EGENERIC;
417 #endif
418 }
419
420 void libvlc_vlm_stop_media( libvlc_instance_t *p_instance, char *psz_name,
421                             libvlc_exception_t *p_exception )
422 {
423 #ifdef ENABLE_VLM
424     vlm_t *p_vlm;
425     int64_t id;
426
427     VLM(p_vlm);
428
429     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
430         vlm_Control( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, NULL ) )
431     {
432         libvlc_exception_raise( p_exception, "Unable to stop %s", psz_name );
433     }
434 #else
435     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
436     return VLC_EGENERIC;
437 #endif
438 }
439
440 void libvlc_vlm_pause_media( libvlc_instance_t *p_instance, char *psz_name,
441                             libvlc_exception_t *p_exception )
442 {
443 #ifdef ENABLE_VLM
444     vlm_t *p_vlm;
445     int64_t id;
446
447     VLM(p_vlm);
448
449     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
450         vlm_Control( p_vlm, VLM_PAUSE_MEDIA_INSTANCE, id, NULL ) )
451     {
452         libvlc_exception_raise( p_exception, "Unable to pause %s", psz_name );
453     }
454 #else
455     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
456     return VLC_EGENERIC;
457 #endif
458 }
459
460 void libvlc_vlm_seek_media( libvlc_instance_t *p_instance, char *psz_name,
461                             float f_percentage, libvlc_exception_t *p_exception )
462 {
463 #ifdef ENABLE_VLM
464     vlm_t *p_vlm;
465     int64_t id;
466
467     VLM(p_vlm);
468
469     if( vlm_Control( p_vlm, VLM_GET_MEDIA_ID, psz_name, &id ) ||
470         vlm_Control( p_vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, NULL, f_percentage ) )
471     {
472         libvlc_exception_raise( p_exception, "Unable to seek %s to %f", psz_name, f_percentage );
473     }
474 #else
475     libvlc_exception_raise( p_exception, "VLM has been disabled in this libvlc." );
476     return VLC_EGENERIC;
477 #endif
478 }
479
480 #define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, ret, code )\
481 returnType libvlc_vlm_get_media_instance_## attr( libvlc_instance_t *p_instance, \
482                         char *psz_name, int i_instance, libvlc_exception_t *p_exception ) \
483 { \
484     vlm_media_instance_t *p_mi = libvlc_vlm_get_media_instance( p_instance, psz_name, i_instance, \
485                                                                 p_exception );  \
486     if( p_mi ) {                            \
487         returnType ret_value;               \
488         code;                               \
489         vlm_media_instance_Delete( p_mi );  \
490         return ret_value;                   \
491     }                                       \
492     libvlc_exception_raise( p_exception, "Unable to get %s "#attr "attribute" ); \
493     return ret; \
494 }
495
496 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( position, float, Float, -1, ret_value = p_mi->d_position; );
497 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( time,     int, Integer, -1, ret_value = p_mi->i_time );
498 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( length,   int, Integer, -1, ret_value = p_mi->i_length );
499 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( rate,     int, Integer, -1, ret_value = p_mi->i_rate );
500 /* FIXME extend vlm_media_instance_t to be able to implement them */
501 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( title,    int, Integer,  0, ret_value = 0 );
502 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( chapter,  int, Integer,  0, ret_value = 0 );
503 LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( seekable, int, Bool,     0, ret_value = false );
504
505 #undef LIBVLC_VLM_GET_MEDIA_ATTRIBUTE
506
507 char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
508                              libvlc_exception_t *p_exception )
509 {
510     (void)p_instance;
511     /* FIXME is it needed ? */
512     libvlc_exception_raise( p_exception, "Unable to call show %s", psz_name );
513     return NULL;
514 }