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