vlm.c 13.7 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*****************************************************************************
 * vlm.c: libvlc new API VLM handling functions
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $Id: playlist.c 14265 2006-02-12 17:31:39Z zorglub $
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

zorglub's avatar
zorglub committed
24
#include "libvlc_internal.h"
zorglub's avatar
zorglub committed
25
#include <vlc/libvlc.h>
26
#include <vlc_es.h>
zorglub's avatar
zorglub committed
27
#include <vlc_input.h>
28
#include "../input/vlm_internal.h"
zorglub's avatar
zorglub committed
29

zorglub's avatar
zorglub committed
30
static void InitVLM( libvlc_instance_t *p_instance )
zorglub's avatar
zorglub committed
31
{
zorglub's avatar
zorglub committed
32
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
33
    if( p_instance->p_vlm ) return;
34
    p_instance->p_vlm = vlm_New( p_instance->p_libvlc_int );
zorglub's avatar
zorglub committed
35
36
37
#else
    p_instance->p_vlm = NULL;
#endif
zorglub's avatar
zorglub committed
38
39
}

40
41
42
43
44
45
46
/* XXX this code has to be rewritten, disable it for now */
#define vlm_MediaNew( a, b, c) (NULL)
#define vlm_MediaDelete(a,b,c)
#define vlm_MediaSetup(a,b,c,d) (-1)
#define vlm_MediaControl(a,b,c,d,e) (-1)
#define vlm_MediaSearch(a,b) (NULL)

zorglub's avatar
zorglub committed
47
48
#define CHECK_VLM { if( !p_instance->p_vlm ) InitVLM( p_instance ); \
                    if( !p_instance->p_vlm ) {\
zorglub's avatar
zorglub committed
49
50
                  libvlc_exception_raise( p_exception, \
                  "Unable to create VLM. It might be disabled." ); return; } }
zorglub's avatar
zorglub committed
51

zorglub's avatar
zorglub committed
52
53
54
55
56
57
58
#define GET_MEDIA { p_media = vlm_MediaSearch( p_instance->p_vlm, psz_name );\
                   if( !p_media ) \
                   { \
                        libvlc_exception_raise( p_exception, \
                                                "Media %s does not exist", \
                                                psz_name ); return; } }

zorglub's avatar
zorglub committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
void libvlc_vlm_add_broadcast( libvlc_instance_t *p_instance, char *psz_name,
                               char *psz_input, char *psz_output,
                               int i_options, char **ppsz_options,
                               int b_enabled, int b_loop,
                               libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
    CHECK_VLM;

    p_media = vlm_MediaNew( p_instance->p_vlm, psz_name, BROADCAST_TYPE );
    if( !p_media )
    {
        libvlc_exception_raise( p_exception, "Media %s creation failed",
                                psz_name );
        return;
    }
    libvlc_vlm_change_media( p_instance, psz_name, psz_input, psz_output,
                             i_options, ppsz_options, b_enabled, b_loop,
                             p_exception );

}

void libvlc_vlm_del_media( libvlc_instance_t *p_instance, char *psz_name,
                           libvlc_exception_t *p_exception )
{
    char *psz_message;
    vlm_message_t *answer;
    CHECK_VLM;
zorglub's avatar
zorglub committed
87
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
88
89
90
91
92
93
94
95
    asprintf( &psz_message, "del %s", psz_name );
    vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
    if( answer->psz_value )
    {
        libvlc_exception_raise( p_exception, "Unable to delete %s",
                                psz_name );
    }
    free( psz_message);
zorglub's avatar
zorglub committed
96
#endif
zorglub's avatar
zorglub committed
97
98
99
100
101
102
103
}

void libvlc_vlm_set_enabled( libvlc_instance_t *p_instance, char *psz_name,
                             int b_enabled, libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
    CHECK_VLM;
zorglub's avatar
zorglub committed
104
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
105
    GET_MEDIA;
zorglub's avatar
zorglub committed
106
107
    if( b_enabled != 0 ) b_enabled = 1;
    p_media->b_enabled = b_enabled;
zorglub's avatar
zorglub committed
108
#endif
zorglub's avatar
zorglub committed
109
110
}

zorglub's avatar
zorglub committed
111
112
113
114
115
void libvlc_vlm_set_loop( libvlc_instance_t *p_instance, char *psz_name,
                          int b_loop, libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
    CHECK_VLM;
zorglub's avatar
zorglub committed
116
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
117
118
119
    GET_MEDIA;
    if( b_loop != 0 ) b_loop = 1;
    p_media->b_loop = b_loop;
zorglub's avatar
zorglub committed
120
#endif
zorglub's avatar
zorglub committed
121
122
123
124
125
126
127
128
}

void libvlc_vlm_set_output( libvlc_instance_t *p_instance, char *psz_name,
                            char *psz_output,  libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
    int i_ret;
    CHECK_VLM;
zorglub's avatar
zorglub committed
129
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
130
131
132
133
134
    GET_MEDIA;

    vlc_mutex_lock( &p_instance->p_vlm->lock );
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
    if( i_ret )
zorglub's avatar
zorglub committed
135
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
136
137
138
        libvlc_exception_raise( p_exception, "Unable to set output" );
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
139
    }
zorglub's avatar
zorglub committed
140
    vlc_mutex_unlock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
141
#endif
zorglub's avatar
zorglub committed
142
143
144
145
146
147
148
149
}

void libvlc_vlm_set_input( libvlc_instance_t *p_instance, char *psz_name,
                           char *psz_input,  libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
    int i_ret;
    CHECK_VLM;
zorglub's avatar
zorglub committed
150
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
151
    vlc_mutex_lock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
152
    GET_MEDIA;
zorglub's avatar
zorglub committed
153

zorglub's avatar
zorglub committed
154
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "inputdel", "all" );
zorglub's avatar
zorglub committed
155
    if( i_ret )
zorglub's avatar
zorglub committed
156
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
157
158
159
        libvlc_exception_raise( p_exception, "Unable to change input" );
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
160
    }
zorglub's avatar
zorglub committed
161
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
zorglub's avatar
zorglub committed
162
    if( i_ret )
zorglub's avatar
zorglub committed
163
164
    {
        libvlc_exception_raise( p_exception, "Unable to change input" );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
165
166
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
167
    }
zorglub's avatar
zorglub committed
168
    vlc_mutex_unlock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
169
#endif
zorglub's avatar
zorglub committed
170
171
172
173
174
175
176
177
}

void libvlc_vlm_add_input( libvlc_instance_t *p_instance, char *psz_name,
                           char *psz_input,  libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
    int i_ret;
    CHECK_VLM;
zorglub's avatar
zorglub committed
178
#ifdef ENABLE_VLM
zorglub's avatar
zorglub committed
179
    vlc_mutex_lock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
180
    GET_MEDIA;
zorglub's avatar
zorglub committed
181

zorglub's avatar
zorglub committed
182
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
zorglub's avatar
zorglub committed
183
    if( i_ret )
zorglub's avatar
zorglub committed
184
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
185
186
187
        libvlc_exception_raise( p_exception, "Unable to change input" );
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
188
    }
zorglub's avatar
zorglub committed
189
190

    vlc_mutex_unlock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
191
#endif
zorglub's avatar
zorglub committed
192
193
194
}


zorglub's avatar
zorglub committed
195
196
197
198
199
200
void libvlc_vlm_change_media( libvlc_instance_t *p_instance, char *psz_name,
                              char *psz_input, char *psz_output, int i_options,
                              char **ppsz_options, int b_enabled, int b_loop,
                              libvlc_exception_t *p_exception )
{
    vlm_media_t *p_media;
zorglub's avatar
zorglub committed
201
    int i_ret;
zorglub's avatar
zorglub committed
202
    CHECK_VLM;
zorglub's avatar
zorglub committed
203
204
#ifdef ENABLE_VLM
    vlc_mutex_lock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
205
    GET_MEDIA;
zorglub's avatar
zorglub committed
206
207
208
    if( b_enabled != 0 ) b_enabled = 1;
    if( b_loop != 0 ) b_loop = 1;

zorglub's avatar
zorglub committed
209
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
zorglub's avatar
zorglub committed
210
211
212
213
214
215
    if( i_ret )
    {
        libvlc_exception_raise( p_exception, "Unable to set output" );
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
    }
zorglub's avatar
zorglub committed
216
217
    p_media->b_enabled = b_enabled;
    p_media->b_loop = b_loop;
zorglub's avatar
zorglub committed
218
219
220

    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "output", psz_output );
    if( i_ret )
zorglub's avatar
zorglub committed
221
222
    {
        libvlc_exception_raise( p_exception, "Unable to set output" );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
223
224
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
225
    }
zorglub's avatar
zorglub committed
226
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "inputdel", "all" );
zorglub's avatar
zorglub committed
227
    if( i_ret )
zorglub's avatar
zorglub committed
228
229
    {
        libvlc_exception_raise( p_exception, "Unable to change input" );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
230
231
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
232
    }
zorglub's avatar
zorglub committed
233
    i_ret = vlm_MediaSetup( p_instance->p_vlm, p_media, "input", psz_input );
zorglub's avatar
zorglub committed
234
    if( i_ret )
zorglub's avatar
zorglub committed
235
236
    {
        libvlc_exception_raise( p_exception, "Unable to change input" );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
237
238
        vlc_mutex_unlock( &p_instance->p_vlm->lock );
        return;
zorglub's avatar
zorglub committed
239
    }
zorglub's avatar
zorglub committed
240
241

    vlc_mutex_unlock( &p_instance->p_vlm->lock );
zorglub's avatar
zorglub committed
242
#endif
zorglub's avatar
zorglub committed
243
}
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

void libvlc_vlm_play_media( libvlc_instance_t *p_instance, char *psz_name,
                            libvlc_exception_t *p_exception )
    
{
    char *psz_message;
    vlm_message_t *answer;
    CHECK_VLM;
#ifdef ENABLE_VLM
    asprintf( &psz_message, "control %s play", psz_name );
    vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
    if( answer->psz_value )
    {
        libvlc_exception_raise( p_exception, "Unable to play %s",
                                psz_name );
    }
    free( psz_message);
#endif
}

void libvlc_vlm_stop_media( libvlc_instance_t *p_instance, char *psz_name,
                            libvlc_exception_t *p_exception )
    
{
    char *psz_message;
    vlm_message_t *answer;
    CHECK_VLM;
#ifdef ENABLE_VLM
    asprintf( &psz_message, "control %s stop", psz_name );
    vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
    if( answer->psz_value )
    {
        libvlc_exception_raise( p_exception, "Unable to stop %s",
                                psz_name );
    }
    free( psz_message);
#endif
}

void libvlc_vlm_pause_media( libvlc_instance_t *p_instance, char *psz_name,
                            libvlc_exception_t *p_exception )
    
{
    char *psz_message;
    vlm_message_t *answer;
    CHECK_VLM;
#ifdef ENABLE_VLM
    asprintf( &psz_message, "control %s pause", psz_name );
    vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
    if( answer->psz_value )
    {
        libvlc_exception_raise( p_exception, "Unable to pause %s",
                                psz_name );
    }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    free( psz_message );
#endif
}

void libvlc_vlm_seek_media( libvlc_instance_t *p_instance, char *psz_name,
                            float f_percentage, libvlc_exception_t *p_exception )
    
{
    char *psz_message;
    vlm_message_t *answer;
    CHECK_VLM;
#ifdef ENABLE_VLM
    asprintf( &psz_message, "control %s seek %f", psz_name, f_percentage );
    vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
    if( answer->psz_value )
    {
        libvlc_exception_raise( p_exception, "Unable to seek %s to %f",
                                psz_name, f_percentage );
    }
    free( psz_message );
#endif
}

#ifdef ENABLE_VLM
#define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, default)\
returnType libvlc_vlm_get_media_## attr( libvlc_instance_t *p_instance, \
                        char *psz_name, int i_instance, \
                        libvlc_exception_t *p_exception ) \
{ \
    vlm_media_instance_t *p_media_instance; \
    CHECK_VLM; \
    vlm_media_t *p_media; \
    p_media = vlm_MediaSearch( p_instance->p_vlm, psz_name ); \
    if ( p_media == NULL ) \
    { \
        libvlc_exception_raise( p_exception, "Unable to find media %s", \
                                psz_name); \
    } \
    else \
    { \
        if ( i_instance < p_media->i_instance ) \
        { \
            p_media_instance = p_media->instance[ i_instance ]; \
            return var_Get ## getType( p_media_instance->p_input, #attr );\
        } \
        else \
        { \
            libvlc_exception_raise( p_exception, "Media index %i out of range",\
                                    i_instance); \
        } \
    } \
    return default; \
}
#else
#define LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( attr, returnType, getType, default)\
returnType libvlc_vlm_get_media_## attr( libvlc_instance_t *p_instance, \
                        char *psz_name, int i_instance, libvlc_exception_t *p_exception ) \
{ \
    char *psz_message; \
    vlm_message_t *answer; \
    CHECK_VLM; \
    return default; \
}
#endif

LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( position, float, Float, -1);
LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( time, int, Integer, -1);
LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( length, int, Integer, -1);
LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( rate, int, Integer, -1);
LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( title, int, Integer, 0);
LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( chapter, int, Integer, 0);
LIBVLC_VLM_GET_MEDIA_ATTRIBUTE( seekable, int, Bool, 0);

#undef LIBVLC_VLM_GET_MEDIA_ATTRIBUTE

373
/* local function to be used in libvlc_vlm_show_media only */
374
static char* recurse_answer( char* psz_prefix, vlm_message_t *p_answer ) {
375
376
377
378
379
    char* psz_childprefix;
    char* psz_response="";
    char* response_tmp;
    int i;
    vlm_message_t *aw_child, **paw_child;
380

381
    asprintf( &psz_childprefix, "%s%s.", psz_prefix, p_answer->psz_name );
382

383
384
385
386
387
    if ( p_answer->i_child )
    {
        paw_child = p_answer->child;
        aw_child = *( paw_child );
        for( i = 0; i < p_answer->i_child; i++ )
388
        {
389
390
391
392
393
394
            asprintf( &response_tmp, "%s%s%s:%s\n",
                      psz_response, psz_prefix, aw_child->psz_name,
                      aw_child->psz_value );
            free( psz_response );
            psz_response = response_tmp;
            if ( aw_child->i_child )
395
            {
396
397
                asprintf(&response_tmp, "%s%s", psz_response,
                         recurse_answer(psz_childprefix, aw_child));
398
399
400
                free( psz_response );
                psz_response = response_tmp;
            }
401
402
            paw_child++;
            aw_child = *( paw_child );
403
404
        }
    }
405
406
407
408
409
410
411
    free( psz_childprefix );
    return psz_response;
}

char* libvlc_vlm_show_media( libvlc_instance_t *p_instance, char *psz_name,
                             libvlc_exception_t *p_exception )
{
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    char *psz_message;
    vlm_message_t *answer;
    char *psz_response;

    CHECK_VLM;
#ifdef ENABLE_VLM
    asprintf( &psz_message, "show %s", psz_name );
    asprintf( &psz_response, "", psz_name );
    vlm_ExecuteCommand( p_instance->p_vlm, psz_message, &answer );
    if( answer->psz_value )
    {
        libvlc_exception_raise( p_exception, "Unable to call show %s: %s",
                                psz_name, answer->psz_value );
    }
    else
    {
        if ( answer->child )
        {
            psz_response = recurse_answer( "", answer );
        }
    }
    free( psz_message );
    return(psz_response );
435
#endif
436
    return NULL;
437
}