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