]> git.sesse.net Git - vlc/blob - src/input/vlm.c
57fa336932d71832a2a267f4d8e426966f56c5dc
[vlc] / src / input / vlm.c
1 /*****************************************************************************
2  * vlm.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <stdio.h>
36 #include <ctype.h>                                              /* tolower() */
37 #include <assert.h>
38
39 #include <vlc_vlm.h>
40
41 #ifndef WIN32
42 #   include <sys/time.h>                                   /* gettimeofday() */
43 #endif
44
45 #ifdef HAVE_TIME_H
46 #   include <time.h>                                              /* ctime() */
47 #   include <sys/timeb.h>                                         /* ftime() */
48 #endif
49
50 #include <vlc_input.h>
51 #include "input_internal.h"
52 #include <vlc_stream.h>
53 #include "vlm_internal.h"
54 #include <vlc_vod.h>
55 #include <vlc_charset.h>
56 #include <vlc_sout.h>
57 #include "../stream_output/stream_output.h"
58 #include "../libvlc.h"
59
60 /*****************************************************************************
61  * Local prototypes.
62  *****************************************************************************/
63
64 static void vlm_Destructor( vlm_t *p_vlm );
65 static int Manage( vlc_object_t * );
66 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
67
68 /*****************************************************************************
69  * vlm_New:
70  *****************************************************************************/
71 vlm_t *__vlm_New ( vlc_object_t *p_this )
72 {
73     vlc_value_t lockval;
74     vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
75     char *psz_vlmconf;
76     static const char vlm_object_name[] = "vlm daemon";
77
78     /* Avoid multiple creation */
79     if( var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ) ||
80         var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ) )
81         return NULL;
82
83     vlc_mutex_lock( lockval.p_address );
84
85     p_vlm = *pp_vlm;
86     if( p_vlm )
87     {   /* VLM already exists */
88         vlc_object_yield( p_vlm );
89         vlc_mutex_unlock( lockval.p_address );
90         return p_vlm;
91     }
92
93     msg_Dbg( p_this, "creating VLM" );
94
95     p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
96                                vlm_object_name );
97     if( !p_vlm )
98     {
99         vlc_mutex_unlock( lockval.p_address );
100         return NULL;
101     }
102
103     vlc_mutex_init( &p_vlm->lock );
104     p_vlm->i_id = 1;
105     TAB_INIT( p_vlm->i_media, p_vlm->media );
106     TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
107     p_vlm->i_vod = 0;
108     p_vlm->p_vod = NULL;
109     vlc_object_attach( p_vlm, p_this->p_libvlc );
110
111     if( vlc_thread_create( p_vlm, "vlm thread",
112                            Manage, VLC_THREAD_PRIORITY_LOW, false ) )
113     {
114         vlc_mutex_destroy( &p_vlm->lock );
115         vlc_object_release( p_vlm );
116         return NULL;
117     }
118
119     /* Load our configuration file */
120     psz_vlmconf = var_CreateGetString( p_vlm, "vlm-conf" );
121     if( psz_vlmconf && *psz_vlmconf )
122     {
123         vlm_message_t *p_message = NULL;
124         char *psz_buffer = NULL;
125
126         msg_Dbg( p_this, "loading VLM configuration" );
127         if( asprintf(&psz_buffer, "load %s", psz_vlmconf ) == -1 )
128             psz_buffer = NULL;
129         if( psz_buffer )
130         {
131             msg_Dbg( p_this, psz_buffer );
132             if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) )
133                 msg_Warn( p_this, "error while loading the configuration file" );
134
135             vlm_MessageDelete(p_message);
136             free(psz_buffer);
137         }
138     }
139     free(psz_vlmconf);
140
141     vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
142     *pp_vlm = p_vlm; /* for future reference */
143     vlc_mutex_unlock( lockval.p_address );
144
145     return p_vlm;
146 }
147
148 /*****************************************************************************
149  * vlm_Delete:
150  *****************************************************************************/
151 void vlm_Delete( vlm_t *p_vlm )
152 {
153     vlc_value_t lockval;
154
155     /* vlm_Delete() is serialized against itself, and against vlm_New().
156      * This way, vlm_Destructor () (called from vlc_objet_release() above)
157      * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
158     var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
159     vlc_mutex_lock( lockval.p_address );
160     vlc_object_release( p_vlm );
161     vlc_mutex_unlock( lockval.p_address );
162 }
163
164 /*****************************************************************************
165  * vlm_Destructor:
166  *****************************************************************************/
167 static void vlm_Destructor( vlm_t *p_vlm )
168 {
169     libvlc_priv (p_vlm->p_libvlc)->p_vlm = NULL;
170
171     vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
172     TAB_CLEAN( p_vlm->i_media, p_vlm->media );
173
174     vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
175     TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
176
177     vlc_mutex_destroy( &p_vlm->lock );
178 }
179
180 /*****************************************************************************
181  * vlm_ExecuteCommand:
182  *****************************************************************************/
183 int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
184                         vlm_message_t **pp_message)
185 {
186     int i_result;
187
188     vlc_mutex_lock( &p_vlm->lock );
189     i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
190     vlc_mutex_unlock( &p_vlm->lock );
191
192     return i_result;
193 }
194
195
196 int64_t vlm_Date(void)
197 {
198 #ifdef WIN32
199     struct timeb tm;
200     ftime( &tm );
201     return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
202 #else
203     struct timeval tv_date;
204
205     /* gettimeofday() cannot fail given &tv_date is a valid address */
206     (void)gettimeofday( &tv_date, NULL );
207     return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
208 #endif
209 }
210
211
212 /*****************************************************************************
213  *
214  *****************************************************************************/
215 static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
216                                 const char *psz_id, int i_query, va_list args )
217 {
218     vlm_t *vlm = (vlm_t *)p_private;
219     int i, i_ret;
220     const char *psz;
221     int64_t id;
222
223     if( !p_private || !p_vod_media )
224         return VLC_EGENERIC;
225
226     vlc_mutex_lock( &vlm->lock );
227
228     /* Find media id */
229     for( i = 0, id = -1; i < vlm->i_media; i++ )
230     {
231         if( p_vod_media == vlm->media[i]->vod.p_media )
232         {
233             id = vlm->media[i]->cfg.id;
234             break;
235         }
236     }
237     if( id == -1 )
238     {
239         vlc_mutex_unlock( &vlm->lock );
240         return VLC_EGENERIC;
241     }
242
243     switch( i_query )
244     {
245     case VOD_MEDIA_PLAY:
246         psz = (const char *)va_arg( args, const char * );
247         i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
248         break;
249
250     case VOD_MEDIA_PAUSE:
251         i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
252         break;
253
254     case VOD_MEDIA_STOP:
255         i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
256         break;
257
258     case VOD_MEDIA_SEEK:
259     {
260         double d_position = (double)va_arg( args, double );
261         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position/100.0 );
262         break;
263     }
264
265     case VOD_MEDIA_REWIND:
266     {
267         double d_scale = (double)va_arg( args, double );
268         double d_position;
269
270         vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
271         d_position -= (d_scale / 1000.0);
272         if( d_position < 0.0 )
273             d_position = 0.0;
274         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
275         break;
276     }
277
278     case VOD_MEDIA_FORWARD:
279     {
280         double d_scale = (double)va_arg( args, double );
281         double d_position;
282
283         vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_POSITION, id, psz_id, &d_position );
284         d_position += (d_scale / 1000.0);
285         if( d_position > 1.0 )
286             d_position = 1.0;
287         i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_POSITION, id, psz_id, d_position );
288         break;
289     }
290
291     default:
292         i_ret = VLC_EGENERIC;
293         break;
294     }
295
296     vlc_mutex_unlock( &vlm->lock );
297
298     return i_ret;
299 }
300
301
302 /*****************************************************************************
303  * Manage:
304  *****************************************************************************/
305 static int Manage( vlc_object_t* p_object )
306 {
307     vlm_t *vlm = (vlm_t*)p_object;
308     int i, j;
309     mtime_t i_lastcheck;
310     mtime_t i_time;
311
312     i_lastcheck = vlm_Date();
313
314     while( !vlm->b_die )
315     {
316         char **ppsz_scheduled_commands = NULL;
317         int    i_scheduled_commands = 0;
318
319         vlc_mutex_lock( &vlm->lock );
320
321         /* destroy the inputs that wants to die, and launch the next input */
322         for( i = 0; i < vlm->i_media; i++ )
323         {
324             vlm_media_sys_t *p_media = vlm->media[i];
325
326             for( j = 0; j < p_media->i_instance; )
327             {
328                 vlm_media_instance_sys_t *p_instance = p_media->instance[j];
329
330                 if( p_instance->p_input && ( p_instance->p_input->b_eof || p_instance->p_input->b_error ) )
331                 {
332                     int i_new_input_index;
333
334                     /* */
335                     i_new_input_index = p_instance->i_index + 1;
336                     if( !p_media->cfg.b_vod && p_media->cfg.broadcast.b_loop && i_new_input_index >= p_media->cfg.i_input )
337                         i_new_input_index = 0;
338
339                     /* FIXME implement multiple input with VOD */
340                     if( p_media->cfg.b_vod || i_new_input_index >= p_media->cfg.i_input )
341                         vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, p_media->cfg.id, p_instance->psz_name );
342                     else
343                         vlm_ControlInternal( vlm, VLM_START_MEDIA_BROADCAST_INSTANCE, p_media->cfg.id, p_instance->psz_name, i_new_input_index );
344
345                     j = 0;
346                 }
347                 else
348                 {
349                     j++;
350                 }
351             }
352         }
353
354         /* scheduling */
355         i_time = vlm_Date();
356
357         for( i = 0; i < vlm->i_schedule; i++ )
358         {
359             mtime_t i_real_date = vlm->schedule[i]->i_date;
360
361             if( vlm->schedule[i]->b_enabled == true )
362             {
363                 if( vlm->schedule[i]->i_date == 0 ) // now !
364                 {
365                     vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
366                     i_real_date = i_time;
367                 }
368                 else if( vlm->schedule[i]->i_period != 0 )
369                 {
370                     int j = 0;
371                     while( vlm->schedule[i]->i_date + j *
372                            vlm->schedule[i]->i_period <= i_lastcheck &&
373                            ( vlm->schedule[i]->i_repeat > j ||
374                              vlm->schedule[i]->i_repeat == -1 ) )
375                     {
376                         j++;
377                     }
378
379                     i_real_date = vlm->schedule[i]->i_date + j *
380                         vlm->schedule[i]->i_period;
381                 }
382
383                 if( i_real_date <= i_time && i_real_date > i_lastcheck )
384                 {
385                     for( j = 0; j < vlm->schedule[i]->i_command; j++ )
386                     {
387                         TAB_APPEND( i_scheduled_commands,
388                                     ppsz_scheduled_commands,
389                                     strdup(vlm->schedule[i]->command[j] ) );
390                     }
391                 }
392             }
393         }
394         while( i_scheduled_commands )
395         {
396             vlm_message_t *message = NULL;
397             char *psz_command = ppsz_scheduled_commands[0];
398             ExecuteCommand( vlm, psz_command,&message );
399
400             /* for now, drop the message */
401             vlm_MessageDelete( message );
402             TAB_REMOVE( i_scheduled_commands,
403                         ppsz_scheduled_commands,
404                         psz_command );
405             free( psz_command );
406         }
407
408         i_lastcheck = i_time;
409
410         vlc_mutex_unlock( &vlm->lock );
411
412         msleep( 100000 );
413     }
414
415     return VLC_SUCCESS;
416 }
417
418 /* New API
419  */
420 /*
421 typedef struct
422 {
423     struct
424     {
425         int i_connection_count;
426         int i_connection_active;
427     } vod;
428     struct
429     {
430         int        i_count;
431         bool b_playing;
432         int        i_playing_index;
433     } broadcast;
434
435 } vlm_media_status_t;
436 */
437
438 /* */
439 static vlm_media_sys_t *vlm_ControlMediaGetById( vlm_t *p_vlm, int64_t id )
440 {
441     int i;
442
443     for( i = 0; i < p_vlm->i_media; i++ )
444     {
445         if( p_vlm->media[i]->cfg.id == id )
446             return p_vlm->media[i];
447     }
448     return NULL;
449 }
450 static vlm_media_sys_t *vlm_ControlMediaGetByName( vlm_t *p_vlm, const char *psz_name )
451 {
452     int i;
453
454     for( i = 0; i < p_vlm->i_media; i++ )
455     {
456         if( !strcmp( p_vlm->media[i]->cfg.psz_name, psz_name ) )
457             return p_vlm->media[i];
458     }
459     return NULL;
460 }
461 static int vlm_MediaDescriptionCheck( vlm_t *p_vlm, vlm_media_t *p_cfg )
462 {
463     int i;
464
465     if( !p_cfg || !p_cfg->psz_name ||
466         !strcmp( p_cfg->psz_name, "all" ) || !strcmp( p_cfg->psz_name, "media" ) || !strcmp( p_cfg->psz_name, "schedule" ) )
467         return VLC_EGENERIC;
468
469     for( i = 0; i < p_vlm->i_media; i++ )
470     {
471         if( p_vlm->media[i]->cfg.id == p_cfg->id )
472             continue;
473         if( !strcmp( p_vlm->media[i]->cfg.psz_name, p_cfg->psz_name ) )
474             return VLC_EGENERIC;
475     }
476     return VLC_SUCCESS;
477 }
478
479
480 /* Called after a media description is changed/added */
481 static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
482 {
483     vlm_media_t *p_cfg = &p_media->cfg;
484     /* Check if we need to create/delete a vod media */
485     if( p_cfg->b_vod )
486     {
487         if( !p_cfg->b_enabled && p_media->vod.p_media )
488         {
489             p_vlm->p_vod->pf_media_del( p_vlm->p_vod, p_media->vod.p_media );
490             p_media->vod.p_media = NULL;
491         }
492         else if( p_cfg->b_enabled && !p_media->vod.p_media && p_cfg->i_input )
493         {
494             /* Pre-parse the input */
495             input_thread_t *p_input;
496             char *psz_output;
497             char *psz_header;
498             char *psz_dup;
499             int i;
500
501             vlc_gc_decref( p_media->vod.p_item );
502             p_media->vod.p_item = input_ItemNew( p_vlm, p_cfg->ppsz_input[0],
503                 p_cfg->psz_name );
504
505             if( p_cfg->psz_output )
506                 asprintf( &psz_output, "%s:description", p_cfg->psz_output );
507             else
508                 asprintf( &psz_output, "#description" );
509
510             asprintf( &psz_dup, "sout=%s", psz_output);
511             input_ItemAddOption( p_media->vod.p_item, psz_dup );
512             free( psz_dup );
513             for( i = 0; i < p_cfg->i_option; i++ )
514                 input_ItemAddOption( p_media->vod.p_item,
515                                      p_cfg->ppsz_option[i] );
516
517             asprintf( &psz_header, _("Media: %s"), p_cfg->psz_name );
518
519             if( (p_input = input_CreateThreadExtended( p_vlm, p_media->vod.p_item, psz_header, NULL ) ) )
520             {
521                 while( !p_input->b_eof && !p_input->b_error )
522                     msleep( 100000 );
523
524                 input_StopThread( p_input );
525                 vlc_object_release( p_input );
526             }
527             free( psz_output );
528             free( psz_header );
529
530             if( p_cfg->vod.psz_mux )
531             {
532                 input_item_t item;
533                 es_format_t es, *p_es = &es;
534                 char fourcc[5];
535
536                 sprintf( fourcc, "%4.4s", p_cfg->vod.psz_mux );
537                 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
538                 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
539
540                 /* XXX: Don't do it that way, but properly use a new input item ref. */
541                 item = *p_media->vod.p_item;
542                 item.i_es = 1;
543                 item.es = &p_es;
544                 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
545
546                 p_media->vod.p_media =
547                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
548             }
549             else
550             {
551                 p_media->vod.p_media =
552                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
553             }
554         }
555     }
556     else
557     {
558         /* TODO start media if needed */
559     }
560
561     /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
562
563     return VLC_SUCCESS;
564 }
565 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
566 {
567     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
568
569     /* */
570     if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
571         return VLC_EGENERIC;
572     if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
573         return VLC_EGENERIC;
574
575     if( 0 )
576     {
577         /* TODO check what are the changes being done (stop instance if needed) */
578     }
579
580     vlm_media_Clean( &p_media->cfg );
581     vlm_media_Copy( &p_media->cfg, p_cfg );
582
583     return vlm_OnMediaUpdate( p_vlm, p_media );
584 }
585
586 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
587 {
588     vlm_media_sys_t *p_media;
589
590     if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
591     {
592         msg_Err( p_vlm, "invalid media description" );
593         return VLC_EGENERIC;
594     }
595     /* Check if we need to load the VOD server */
596     if( p_cfg->b_vod && !p_vlm->i_vod )
597     {
598         p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
599                                           VLC_OBJECT_GENERIC, "vod server" );
600         vlc_object_attach( p_vlm->p_vod, p_vlm );
601         p_vlm->p_vod->p_module = module_Need( p_vlm->p_vod, "vod server", 0, 0 );
602         if( !p_vlm->p_vod->p_module )
603         {
604             msg_Err( p_vlm, "cannot find vod server" );
605             vlc_object_detach( p_vlm->p_vod );
606             vlc_object_release( p_vlm->p_vod );
607             p_vlm->p_vod = 0;
608             return VLC_EGENERIC;
609         }
610
611         p_vlm->p_vod->p_data = p_vlm;
612         p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
613     }
614
615     p_media = malloc( sizeof( vlm_media_sys_t ) );
616     if( !p_media )
617         return VLC_ENOMEM;
618     memset( p_media, 0, sizeof(vlm_media_sys_t) );
619
620     if( p_cfg->b_vod )
621         p_vlm->i_vod++;
622
623     vlm_media_Copy( &p_media->cfg, p_cfg );
624     p_media->cfg.id = p_vlm->i_id++;
625     /* FIXME do we do something here if enabled is true ? */
626
627     p_media->vod.p_item = input_ItemNew( p_vlm, NULL, NULL );
628
629     p_media->vod.p_media = NULL;
630     TAB_INIT( p_media->i_instance, p_media->instance );
631
632     /* */
633     TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
634
635     if( p_id )
636         *p_id = p_media->cfg.id;
637
638     return vlm_OnMediaUpdate( p_vlm, p_media );
639 }
640
641 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
642 {
643     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
644
645     if( !p_media )
646         return VLC_EGENERIC;
647
648     while( p_media->i_instance > 0 )
649         vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
650
651     if( p_media->cfg.b_vod )
652     {
653         p_media->cfg.b_enabled = false;
654         vlm_OnMediaUpdate( p_vlm, p_media );
655         p_vlm->i_vod--;
656     }
657
658     vlm_media_Clean( &p_media->cfg );
659
660     vlc_gc_decref( p_media->vod.p_item );
661
662     TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
663
664     free( p_media );
665
666     /* Check if we need to unload the VOD server */
667     if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
668     {
669         module_Unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
670         vlc_object_detach( p_vlm->p_vod );
671         vlc_object_release( p_vlm->p_vod );
672         p_vlm->p_vod = NULL;
673     }
674     return VLC_SUCCESS;
675 }
676
677 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
678 {
679     vlm_media_t **pp_dsc;
680     int                     i_dsc;
681     int i;
682
683     TAB_INIT( i_dsc, pp_dsc );
684     for( i = 0; i < p_vlm->i_media; i++ )
685     {
686         vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
687         TAB_APPEND( i_dsc, pp_dsc, p_dsc );
688     }
689
690     *ppp_dsc = pp_dsc;
691     *pi_dsc = i_dsc;
692
693     return VLC_SUCCESS;
694 }
695 static int vlm_ControlMediaClear( vlm_t *p_vlm )
696 {
697     while( p_vlm->i_media > 0 )
698         vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
699
700     return VLC_SUCCESS;
701 }
702 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
703 {
704     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
705     if( !p_media )
706         return VLC_EGENERIC;
707
708     *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
709     return VLC_SUCCESS;
710 }
711 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
712 {
713     vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
714     if( !p_media )
715         return VLC_EGENERIC;
716
717     *p_id = p_media->cfg.id;
718     return VLC_SUCCESS;
719 }
720
721 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
722 {
723     int i;
724
725     for( i = 0; i < p_media->i_instance; i++ )
726     {
727         const char *psz = p_media->instance[i]->psz_name;
728         if( ( psz == NULL && psz_id == NULL ) ||
729             ( psz && psz_id && !strcmp( psz, psz_id ) ) )
730             return p_media->instance[i];
731     }
732     return NULL;
733 }
734 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
735 {
736     vlm_media_instance_sys_t *p_instance = malloc( sizeof(vlm_media_instance_sys_t) );
737     if( !p_instance )
738         return NULL;
739
740     memset( p_instance, 0, sizeof(vlm_media_instance_sys_t) );
741
742     p_instance->psz_name = NULL;
743     if( psz_name )
744         p_instance->psz_name = strdup( psz_name );
745
746     p_instance->p_item = input_ItemNew( p_vlm, NULL, NULL );
747
748     p_instance->i_index = 0;
749     p_instance->b_sout_keep = false;
750     p_instance->p_input = NULL;
751     p_instance->p_sout = NULL;
752
753     return p_instance;
754 }
755 static void vlm_MediaInstanceDelete( vlm_media_instance_sys_t *p_instance )
756 {
757     if( p_instance->p_input )
758     {
759         input_StopThread( p_instance->p_input );
760         p_instance->p_sout = input_DetachSout( p_instance->p_input );
761         vlc_object_release( p_instance->p_input );
762     }
763     if( p_instance->p_sout )
764         sout_DeleteInstance( p_instance->p_sout );
765
766     vlc_gc_decref( p_instance->p_item );
767     free( p_instance->psz_name );
768     free( p_instance );
769 }
770
771
772 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
773 {
774     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
775     vlm_media_instance_sys_t *p_instance;
776     char *psz_log;
777
778     if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
779         return VLC_EGENERIC;
780
781     /* TODO support multiple input for VOD with sout-keep ? */
782
783     if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
784         return VLC_EGENERIC;
785
786     if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
787         return VLC_EGENERIC;
788
789     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
790     if( !p_instance )
791     {
792         vlm_media_t *p_cfg = &p_media->cfg;
793         int i;
794
795         p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
796         if( !p_instance )
797             return VLC_ENOMEM;
798
799         if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
800         {
801             char *psz_buffer;
802             asprintf( &psz_buffer, "sout=%s%s%s",
803                       p_cfg->psz_output ? p_cfg->psz_output : "",
804                       (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
805                       psz_vod_output ? psz_vod_output : "" );
806             input_ItemAddOption( p_instance->p_item, psz_buffer );
807             free( psz_buffer );
808         }
809
810         for( i = 0; i < p_cfg->i_option; i++ )
811         {
812             if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
813                 p_instance->b_sout_keep = true;
814             else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
815                 p_instance->b_sout_keep = false;
816             else
817                 input_ItemAddOption( p_instance->p_item, p_cfg->ppsz_option[i] );
818         }
819         TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
820     }
821
822     /* Stop old instance */
823     if( p_instance->p_input )
824     {
825         if( p_instance->i_index == i_input_index &&
826             !p_instance->p_input->b_eof && !p_instance->p_input->b_error )
827         {
828             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
829                 var_SetInteger( p_instance->p_input, "state",  PLAYING_S );
830             return VLC_SUCCESS;
831         }
832
833         input_StopThread( p_instance->p_input );
834         p_instance->p_sout = input_DetachSout( p_instance->p_input );
835         vlc_object_release( p_instance->p_input );
836         if( !p_instance->b_sout_keep && p_instance->p_sout )
837         {
838             sout_DeleteInstance( p_instance->p_sout );
839             p_instance->p_sout = NULL;
840         }
841     }
842
843     /* Start new one */
844     p_instance->i_index = i_input_index;
845     input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
846
847     asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name );
848     p_instance->p_input = input_CreateThreadExtended( p_vlm, p_instance->p_item, psz_log, p_instance->p_sout );
849     if( !p_instance->p_input )
850     {
851         TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
852         vlm_MediaInstanceDelete( p_instance );
853     }
854     free( psz_log );
855
856     return VLC_SUCCESS;
857 }
858
859 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
860 {
861     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
862     vlm_media_instance_sys_t *p_instance;
863
864     if( !p_media )
865         return VLC_EGENERIC;
866
867     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
868     if( !p_instance )
869         return VLC_EGENERIC;
870
871     TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
872
873     vlm_MediaInstanceDelete( p_instance );
874
875     return VLC_SUCCESS;
876 }
877 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
878 {
879     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
880     vlm_media_instance_sys_t *p_instance;
881     int i_state;
882
883     if( !p_media )
884         return VLC_EGENERIC;
885
886     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
887     if( !p_instance || !p_instance->p_input )
888         return VLC_EGENERIC;
889
890     /* Toggle pause state */
891     i_state = var_GetInteger( p_instance->p_input, "state" );
892     if( i_state == PAUSE_S )
893         var_SetInteger( p_instance->p_input, "state", PLAYING_S );
894     else if( i_state == PLAYING_S )
895         var_SetInteger( p_instance->p_input, "state", PAUSE_S );
896     return VLC_SUCCESS;
897 }
898 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
899 {
900     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
901     vlm_media_instance_sys_t *p_instance;
902
903     if( !p_media )
904         return VLC_EGENERIC;
905
906     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
907     if( !p_instance || !p_instance->p_input )
908         return VLC_EGENERIC;
909
910     if( pi_time )
911         *pi_time = var_GetTime( p_instance->p_input, "time" );
912     if( pd_position )
913         *pd_position = var_GetFloat( p_instance->p_input, "position" );
914     return VLC_SUCCESS;
915 }
916 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
917 {
918     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
919     vlm_media_instance_sys_t *p_instance;
920
921     if( !p_media )
922         return VLC_EGENERIC;
923
924     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
925     if( !p_instance || !p_instance->p_input )
926         return VLC_EGENERIC;
927
928     if( i_time >= 0 )
929         return var_SetTime( p_instance->p_input, "time", i_time );
930     else if( d_position >= 0 && d_position <= 100 )
931         return var_SetFloat( p_instance->p_input, "position", d_position );
932     return VLC_EGENERIC;
933 }
934
935 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
936 {
937     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
938     vlm_media_instance_t **pp_idsc;
939     int                              i_idsc;
940     int i;
941
942     if( !p_media )
943         return VLC_EGENERIC;
944
945     TAB_INIT( i_idsc, pp_idsc );
946     for( i = 0; i < p_media->i_instance; i++ )
947     {
948         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
949         vlm_media_instance_t *p_idsc = vlm_media_instance_New();
950
951         if( p_instance->psz_name )
952             p_idsc->psz_name = strdup( p_instance->psz_name );
953         if( p_instance->p_input )
954         {
955             p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
956             p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
957             p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
958             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
959                 p_idsc->b_paused = true;
960             p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
961         }
962
963         TAB_APPEND( i_idsc, pp_idsc, p_idsc );
964     }
965     *ppp_idsc = pp_idsc;
966     *pi_instance = i_idsc;
967     return VLC_SUCCESS;
968 }
969
970 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
971 {
972     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
973
974     if( !p_media )
975         return VLC_EGENERIC;
976
977     while( p_media->i_instance > 0 )
978         vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
979
980     return VLC_SUCCESS;
981 }
982
983 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
984 {
985     while( p_vlm->i_schedule > 0 )
986         vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
987
988     return VLC_SUCCESS;
989 }
990
991 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
992 {
993     vlm_media_t *p_dsc;
994     vlm_media_t **pp_dsc;
995     vlm_media_t ***ppp_dsc;
996     vlm_media_instance_t ***ppp_idsc;
997     const char *psz_id;
998     const char *psz_vod;
999     int64_t *p_id;
1000     int64_t id;
1001     int i_int;
1002     int *pi_int;
1003
1004     int64_t *pi_i64;
1005     int64_t i_i64;
1006     double *pd_double;
1007     double d_double;
1008
1009     switch( i_query )
1010     {
1011     /* Media control */
1012     case VLM_GET_MEDIAS:
1013         ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1014         pi_int = (int *)va_arg( args, int * );
1015         return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1016
1017     case VLM_CLEAR_MEDIAS:
1018         return vlm_ControlMediaClear( p_vlm );
1019
1020     case VLM_CHANGE_MEDIA:
1021         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1022         return vlm_ControlMediaChange( p_vlm, p_dsc );
1023
1024     case VLM_ADD_MEDIA:
1025         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1026         p_id = (int64_t*)va_arg( args, int64_t * );
1027         return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1028
1029     case VLM_DEL_MEDIA:
1030         id = (int64_t)va_arg( args, int64_t );
1031         return vlm_ControlMediaDel( p_vlm, id );
1032
1033     case VLM_GET_MEDIA:
1034         id = (int64_t)va_arg( args, int64_t );
1035         pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1036         return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1037
1038     case VLM_GET_MEDIA_ID:
1039         psz_id = (const char*)va_arg( args, const char * );
1040         p_id = (int64_t*)va_arg( args, int64_t * );
1041         return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1042
1043
1044     /* Media instance control */
1045     case VLM_GET_MEDIA_INSTANCES:
1046         id = (int64_t)va_arg( args, int64_t );
1047         ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1048         pi_int = (int *)va_arg( args, int *);
1049         return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1050
1051     case VLM_CLEAR_MEDIA_INSTANCES:
1052         id = (int64_t)va_arg( args, int64_t );
1053         return vlm_ControlMediaInstanceClear( p_vlm, id );
1054
1055
1056     case VLM_START_MEDIA_BROADCAST_INSTANCE:
1057         id = (int64_t)va_arg( args, int64_t );
1058         psz_id = (const char*)va_arg( args, const char* );
1059         i_int = (int)va_arg( args, int );
1060         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1061
1062     case VLM_START_MEDIA_VOD_INSTANCE:
1063         id = (int64_t)va_arg( args, int64_t );
1064         psz_id = (const char*)va_arg( args, const char* );
1065         i_int = (int)va_arg( args, int );
1066         psz_vod = (const char*)va_arg( args, const char* );
1067         if( !psz_vod )
1068             return VLC_EGENERIC;
1069         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1070
1071     case VLM_STOP_MEDIA_INSTANCE:
1072         id = (int64_t)va_arg( args, int64_t );
1073         psz_id = (const char*)va_arg( args, const char* );
1074         return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1075
1076     case VLM_PAUSE_MEDIA_INSTANCE:
1077         id = (int64_t)va_arg( args, int64_t );
1078         psz_id = (const char*)va_arg( args, const char* );
1079         return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1080
1081     case VLM_GET_MEDIA_INSTANCE_TIME:
1082         id = (int64_t)va_arg( args, int64_t );
1083         psz_id = (const char*)va_arg( args, const char* );
1084         pi_i64 = (int64_t*)va_arg( args, int64_t * );
1085         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1086     case VLM_GET_MEDIA_INSTANCE_POSITION:
1087         id = (int64_t)va_arg( args, int64_t );
1088         psz_id = (const char*)va_arg( args, const char* );
1089         pd_double = (double*)va_arg( args, double* );
1090         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1091
1092     case VLM_SET_MEDIA_INSTANCE_TIME:
1093         id = (int64_t)va_arg( args, int64_t );
1094         psz_id = (const char*)va_arg( args, const char* );
1095         i_i64 = (int64_t)va_arg( args, int64_t);
1096         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1097     case VLM_SET_MEDIA_INSTANCE_POSITION:
1098         id = (int64_t)va_arg( args, int64_t );
1099         psz_id = (const char*)va_arg( args, const char* );
1100         d_double = (double)va_arg( args, double );
1101         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1102
1103     case VLM_CLEAR_SCHEDULES:
1104         return vlm_ControlScheduleClear( p_vlm );
1105
1106     default:
1107         msg_Err( p_vlm, "unknown VLM query" );
1108         return VLC_EGENERIC;
1109     }
1110 }
1111
1112 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1113 {
1114     va_list args;
1115     int     i_result;
1116
1117     va_start( args, i_query );
1118     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1119     va_end( args );
1120
1121     return i_result;
1122 }
1123
1124 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1125 {
1126     va_list args;
1127     int     i_result;
1128
1129     va_start( args, i_query );
1130
1131     vlc_mutex_lock( &p_vlm->lock );
1132     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1133     vlc_mutex_unlock( &p_vlm->lock );
1134
1135     va_end( args );
1136
1137     return i_result;
1138 }