VLM: fix aliasing
[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                 union { char text[5]; uint32_t value; } fourcc;
575
576                 sprintf( fourcc.text, "%4.4s", p_cfg->vod.psz_mux );
577                 fourcc.text[0] = tolower(fourcc.text[0]);
578                 fourcc.text[1] = tolower(fourcc.text[1]);
579                 fourcc.text[2] = tolower(fourcc.text[2]);
580                 fourcc.text[3] = tolower(fourcc.text[3]);
581
582                 /* XXX: Don't do it that way, but properly use a new input item ref. */
583                 item = *p_media->vod.p_item;
584                 item.i_es = 1;
585                 item.es = &p_es;
586                 es_format_Init( &es, VIDEO_ES, fourcc.value );
587
588                 p_media->vod.p_media =
589                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, &item );
590             }
591             else
592             {
593                 p_media->vod.p_media =
594                     p_vlm->p_vod->pf_media_new( p_vlm->p_vod, p_cfg->psz_name, p_media->vod.p_item );
595             }
596         }
597     }
598     else
599     {
600         /* TODO start media if needed */
601     }
602
603     /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
604
605     vlm_SendEventMediaChanged( p_vlm, p_cfg->id, p_cfg->psz_name );
606     return VLC_SUCCESS;
607 }
608 static int vlm_ControlMediaChange( vlm_t *p_vlm, vlm_media_t *p_cfg )
609 {
610     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, p_cfg->id );
611
612     /* */
613     if( !p_media || vlm_MediaDescriptionCheck( p_vlm, p_cfg ) )
614         return VLC_EGENERIC;
615     if( ( p_media->cfg.b_vod && !p_cfg->b_vod ) || ( !p_media->cfg.b_vod && p_cfg->b_vod ) )
616         return VLC_EGENERIC;
617
618     if( 0 )
619     {
620         /* TODO check what are the changes being done (stop instance if needed) */
621     }
622
623     vlm_media_Clean( &p_media->cfg );
624     vlm_media_Copy( &p_media->cfg, p_cfg );
625
626     return vlm_OnMediaUpdate( p_vlm, p_media );
627 }
628
629 static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id )
630 {
631     vlm_media_sys_t *p_media;
632
633     if( vlm_MediaDescriptionCheck( p_vlm, p_cfg ) || vlm_ControlMediaGetByName( p_vlm, p_cfg->psz_name ) )
634     {
635         msg_Err( p_vlm, "invalid media description" );
636         return VLC_EGENERIC;
637     }
638     /* Check if we need to load the VOD server */
639     if( p_cfg->b_vod && !p_vlm->i_vod )
640     {
641         p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
642                                           VLC_OBJECT_GENERIC, "vod server" );
643         vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
644         p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", NULL, false );
645         if( !p_vlm->p_vod->p_module )
646         {
647             msg_Err( p_vlm, "cannot find vod server" );
648             vlc_object_detach( p_vlm->p_vod );
649             vlc_object_release( p_vlm->p_vod );
650             p_vlm->p_vod = NULL;
651             return VLC_EGENERIC;
652         }
653
654         p_vlm->p_vod->p_data = p_vlm;
655         p_vlm->p_vod->pf_media_control = vlm_MediaVodControl;
656     }
657
658     p_media = calloc( 1, sizeof( vlm_media_sys_t ) );
659     if( !p_media )
660         return VLC_ENOMEM;
661
662     if( p_cfg->b_vod )
663         p_vlm->i_vod++;
664
665     vlm_media_Copy( &p_media->cfg, p_cfg );
666     p_media->cfg.id = p_vlm->i_id++;
667     /* FIXME do we do something here if enabled is true ? */
668
669     p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
670
671     p_media->vod.p_media = NULL;
672     TAB_INIT( p_media->i_instance, p_media->instance );
673
674     /* */
675     TAB_APPEND( p_vlm->i_media, p_vlm->media, p_media );
676
677     if( p_id )
678         *p_id = p_media->cfg.id;
679
680     /* */
681     vlm_SendEventMediaAdded( p_vlm, p_media->cfg.id, p_media->cfg.psz_name );
682     return vlm_OnMediaUpdate( p_vlm, p_media );
683 }
684
685 static int vlm_ControlMediaDel( vlm_t *p_vlm, int64_t id )
686 {
687     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
688
689     if( !p_media )
690         return VLC_EGENERIC;
691
692     while( p_media->i_instance > 0 )
693         vlm_ControlInternal( p_vlm, VLM_STOP_MEDIA_INSTANCE, id, p_media->instance[0]->psz_name );
694
695     if( p_media->cfg.b_vod )
696     {
697         p_media->cfg.b_enabled = false;
698         vlm_OnMediaUpdate( p_vlm, p_media );
699         p_vlm->i_vod--;
700     }
701
702     /* */
703     vlm_SendEventMediaRemoved( p_vlm, id, p_media->cfg.psz_name );
704
705     vlm_media_Clean( &p_media->cfg );
706
707     vlc_gc_decref( p_media->vod.p_item );
708
709     TAB_REMOVE( p_vlm->i_media, p_vlm->media, p_media );
710
711     free( p_media );
712
713     /* Check if we need to unload the VOD server */
714     if( p_vlm->p_vod && p_vlm->i_vod <= 0 )
715     {
716         module_unneed( p_vlm->p_vod, p_vlm->p_vod->p_module );
717         vlc_object_detach( p_vlm->p_vod );
718         vlc_object_release( p_vlm->p_vod );
719         p_vlm->p_vod = NULL;
720     }
721
722     return VLC_SUCCESS;
723 }
724
725 static int vlm_ControlMediaGets( vlm_t *p_vlm, vlm_media_t ***ppp_dsc, int *pi_dsc )
726 {
727     vlm_media_t **pp_dsc;
728     int                     i_dsc;
729     int i;
730
731     TAB_INIT( i_dsc, pp_dsc );
732     for( i = 0; i < p_vlm->i_media; i++ )
733     {
734         vlm_media_t *p_dsc = vlm_media_Duplicate( &p_vlm->media[i]->cfg );
735         TAB_APPEND( i_dsc, pp_dsc, p_dsc );
736     }
737
738     *ppp_dsc = pp_dsc;
739     *pi_dsc = i_dsc;
740
741     return VLC_SUCCESS;
742 }
743 static int vlm_ControlMediaClear( vlm_t *p_vlm )
744 {
745     while( p_vlm->i_media > 0 )
746         vlm_ControlMediaDel( p_vlm, p_vlm->media[0]->cfg.id );
747
748     return VLC_SUCCESS;
749 }
750 static int vlm_ControlMediaGet( vlm_t *p_vlm, int64_t id, vlm_media_t **pp_dsc )
751 {
752     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
753     if( !p_media )
754         return VLC_EGENERIC;
755
756     *pp_dsc = vlm_media_Duplicate( &p_media->cfg );
757     return VLC_SUCCESS;
758 }
759 static int vlm_ControlMediaGetId( vlm_t *p_vlm, const char *psz_name, int64_t *p_id )
760 {
761     vlm_media_sys_t *p_media = vlm_ControlMediaGetByName( p_vlm, psz_name );
762     if( !p_media )
763         return VLC_EGENERIC;
764
765     *p_id = p_media->cfg.id;
766     return VLC_SUCCESS;
767 }
768
769 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
770 {
771     int i;
772
773     for( i = 0; i < p_media->i_instance; i++ )
774     {
775         const char *psz = p_media->instance[i]->psz_name;
776         if( ( psz == NULL && psz_id == NULL ) ||
777             ( psz && psz_id && !strcmp( psz, psz_id ) ) )
778             return p_media->instance[i];
779     }
780     return NULL;
781 }
782 static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char *psz_name )
783 {
784     vlm_media_instance_sys_t *p_instance = calloc( 1, sizeof(vlm_media_instance_sys_t) );
785     if( !p_instance )
786         return NULL;
787
788     p_instance->psz_name = NULL;
789     if( psz_name )
790         p_instance->psz_name = strdup( psz_name );
791
792     p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
793
794     p_instance->i_index = 0;
795     p_instance->b_sout_keep = false;
796     p_instance->p_input = NULL;
797     p_instance->p_input_resource = NULL;
798
799     return p_instance;
800 }
801 static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instance_sys_t *p_instance, vlm_media_sys_t *p_media )
802 {
803     input_thread_t *p_input = p_instance->p_input;
804     if( p_input )
805     {
806         input_resource_t *p_resource;
807
808         input_Stop( p_input, true );
809         vlc_thread_join( p_input );
810
811         p_resource = input_DetachResource( p_input );
812         input_resource_Delete( p_resource );
813
814         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
815         vlc_object_release( p_input );
816
817         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
818     }
819     if( p_instance->p_input_resource )
820         input_resource_Delete( p_instance->p_input_resource );
821
822     vlc_gc_decref( p_instance->p_item );
823     free( p_instance->psz_name );
824     free( p_instance );
825 }
826
827
828 static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *psz_id, int i_input_index, const char *psz_vod_output )
829 {
830     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
831     vlm_media_instance_sys_t *p_instance;
832     char *psz_log;
833
834     if( !p_media || !p_media->cfg.b_enabled || p_media->cfg.i_input <= 0 )
835         return VLC_EGENERIC;
836
837     /* TODO support multiple input for VOD with sout-keep ? */
838
839     if( ( p_media->cfg.b_vod && !psz_vod_output ) || ( !p_media->cfg.b_vod && psz_vod_output ) )
840         return VLC_EGENERIC;
841
842     if( i_input_index < 0 || i_input_index >= p_media->cfg.i_input )
843         return VLC_EGENERIC;
844
845     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
846     if( !p_instance )
847     {
848         vlm_media_t *p_cfg = &p_media->cfg;
849         int i;
850
851         p_instance = vlm_MediaInstanceNew( p_vlm, psz_id );
852         if( !p_instance )
853             return VLC_ENOMEM;
854
855         if( p_cfg->psz_output != NULL || psz_vod_output != NULL )
856         {
857             char *psz_buffer;
858             if( asprintf( &psz_buffer, "sout=%s%s%s",
859                       p_cfg->psz_output ? p_cfg->psz_output : "",
860                       (p_cfg->psz_output && psz_vod_output) ? ":" : psz_vod_output ? "#" : "",
861                       psz_vod_output ? psz_vod_output : "" ) != -1 )
862             {
863                 input_item_AddOption( p_instance->p_item, psz_buffer, VLC_INPUT_OPTION_TRUSTED );
864                 free( psz_buffer );
865             }
866         }
867
868         for( i = 0; i < p_cfg->i_option; i++ )
869         {
870             if( !strcmp( p_cfg->ppsz_option[i], "sout-keep" ) )
871                 p_instance->b_sout_keep = true;
872             else if( !strcmp( p_cfg->ppsz_option[i], "nosout-keep" ) || !strcmp( p_cfg->ppsz_option[i], "no-sout-keep" ) )
873                 p_instance->b_sout_keep = false;
874             else
875                 input_item_AddOption( p_instance->p_item, p_cfg->ppsz_option[i], VLC_INPUT_OPTION_TRUSTED );
876         }
877         TAB_APPEND( p_media->i_instance, p_media->instance, p_instance );
878     }
879
880     /* Stop old instance */
881     input_thread_t *p_input = p_instance->p_input;
882     if( p_input )
883     {
884         if( p_instance->i_index == i_input_index &&
885             !p_input->b_eof && !p_input->b_error )
886         {
887             if( var_GetInteger( p_input, "state" ) == PAUSE_S )
888                 var_SetInteger( p_input, "state",  PLAYING_S );
889             return VLC_SUCCESS;
890         }
891
892         input_Stop( p_input, !p_input->b_eof && !p_input->b_error );
893         vlc_thread_join( p_input );
894
895         p_instance->p_input_resource = input_DetachResource( p_input );
896
897         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
898         vlc_object_release( p_input );
899
900         if( !p_instance->b_sout_keep )
901             input_resource_TerminateSout( p_instance->p_input_resource );
902         input_resource_TerminateVout( p_instance->p_input_resource );
903
904         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
905     }
906
907     /* Start new one */
908     p_instance->i_index = i_input_index;
909     input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
910
911     if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
912     {
913         p_instance->p_input = input_Create( p_vlm->p_libvlc, p_instance->p_item,
914                                             psz_log, p_instance->p_input_resource );
915         if( p_instance->p_input )
916         {
917             var_AddCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
918             if( input_Start( p_instance->p_input ) != VLC_SUCCESS )
919             {
920                 var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
921                 vlc_object_release( p_instance->p_input );
922                 p_instance->p_input = NULL;
923             }
924         }
925         p_instance->p_input_resource = NULL;
926
927         if( !p_instance->p_input )
928         {
929             TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
930             vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
931         }
932         else
933         {
934             vlm_SendEventMediaInstanceStarted( p_vlm, id, p_media->cfg.psz_name );
935         }
936         free( psz_log );
937     }
938
939     return VLC_SUCCESS;
940 }
941
942 static int vlm_ControlMediaInstanceStop( vlm_t *p_vlm, int64_t id, const char *psz_id )
943 {
944     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
945     vlm_media_instance_sys_t *p_instance;
946
947     if( !p_media )
948         return VLC_EGENERIC;
949
950     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
951     if( !p_instance )
952         return VLC_EGENERIC;
953
954     TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
955
956     vlm_MediaInstanceDelete( p_vlm, id, p_instance, p_media );
957
958     return VLC_SUCCESS;
959 }
960 static int vlm_ControlMediaInstancePause( vlm_t *p_vlm, int64_t id, const char *psz_id )
961 {
962     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
963     vlm_media_instance_sys_t *p_instance;
964     int i_state;
965
966     if( !p_media )
967         return VLC_EGENERIC;
968
969     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
970     if( !p_instance || !p_instance->p_input )
971         return VLC_EGENERIC;
972
973     /* Toggle pause state */
974     i_state = var_GetInteger( p_instance->p_input, "state" );
975     if( i_state == PAUSE_S )
976         var_SetInteger( p_instance->p_input, "state", PLAYING_S );
977     else if( i_state == PLAYING_S )
978         var_SetInteger( p_instance->p_input, "state", PAUSE_S );
979     return VLC_SUCCESS;
980 }
981 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t *pi_time, double *pd_position )
982 {
983     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
984     vlm_media_instance_sys_t *p_instance;
985
986     if( !p_media )
987         return VLC_EGENERIC;
988
989     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
990     if( !p_instance || !p_instance->p_input )
991         return VLC_EGENERIC;
992
993     if( pi_time )
994         *pi_time = var_GetTime( p_instance->p_input, "time" );
995     if( pd_position )
996         *pd_position = var_GetFloat( p_instance->p_input, "position" );
997     return VLC_SUCCESS;
998 }
999 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t *p_vlm, int64_t id, const char *psz_id, int64_t i_time, double d_position )
1000 {
1001     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1002     vlm_media_instance_sys_t *p_instance;
1003
1004     if( !p_media )
1005         return VLC_EGENERIC;
1006
1007     p_instance = vlm_ControlMediaInstanceGetByName( p_media, psz_id );
1008     if( !p_instance || !p_instance->p_input )
1009         return VLC_EGENERIC;
1010
1011     if( i_time >= 0 )
1012         return var_SetTime( p_instance->p_input, "time", i_time );
1013     else if( d_position >= 0 && d_position <= 100 )
1014         return var_SetFloat( p_instance->p_input, "position", d_position );
1015     return VLC_EGENERIC;
1016 }
1017
1018 static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_instance_t ***ppp_idsc, int *pi_instance )
1019 {
1020     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1021     vlm_media_instance_t **pp_idsc;
1022     int                              i_idsc;
1023     int i;
1024
1025     if( !p_media )
1026         return VLC_EGENERIC;
1027
1028     TAB_INIT( i_idsc, pp_idsc );
1029     for( i = 0; i < p_media->i_instance; i++ )
1030     {
1031         vlm_media_instance_sys_t *p_instance = p_media->instance[i];
1032         vlm_media_instance_t *p_idsc = vlm_media_instance_New();
1033
1034         if( p_instance->psz_name )
1035             p_idsc->psz_name = strdup( p_instance->psz_name );
1036         if( p_instance->p_input )
1037         {
1038             p_idsc->i_time = var_GetTime( p_instance->p_input, "time" );
1039             p_idsc->i_length = var_GetTime( p_instance->p_input, "length" );
1040             p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
1041             if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
1042                 p_idsc->b_paused = true;
1043             p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" );
1044         }
1045
1046         TAB_APPEND( i_idsc, pp_idsc, p_idsc );
1047     }
1048     *ppp_idsc = pp_idsc;
1049     *pi_instance = i_idsc;
1050     return VLC_SUCCESS;
1051 }
1052
1053 static int vlm_ControlMediaInstanceClear( vlm_t *p_vlm, int64_t id )
1054 {
1055     vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
1056
1057     if( !p_media )
1058         return VLC_EGENERIC;
1059
1060     while( p_media->i_instance > 0 )
1061         vlm_ControlMediaInstanceStop( p_vlm, id, p_media->instance[0]->psz_name );
1062
1063     return VLC_SUCCESS;
1064 }
1065
1066 static int vlm_ControlScheduleClear( vlm_t *p_vlm )
1067 {
1068     while( p_vlm->i_schedule > 0 )
1069         vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0] );
1070
1071     return VLC_SUCCESS;
1072 }
1073
1074 static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
1075 {
1076     vlm_media_t *p_dsc;
1077     vlm_media_t **pp_dsc;
1078     vlm_media_t ***ppp_dsc;
1079     vlm_media_instance_t ***ppp_idsc;
1080     const char *psz_id;
1081     const char *psz_vod;
1082     int64_t *p_id;
1083     int64_t id;
1084     int i_int;
1085     int *pi_int;
1086
1087     int64_t *pi_i64;
1088     int64_t i_i64;
1089     double *pd_double;
1090     double d_double;
1091
1092     switch( i_query )
1093     {
1094     /* Media control */
1095     case VLM_GET_MEDIAS:
1096         ppp_dsc = (vlm_media_t ***)va_arg( args, vlm_media_t *** );
1097         pi_int = (int *)va_arg( args, int * );
1098         return vlm_ControlMediaGets( p_vlm, ppp_dsc, pi_int );
1099
1100     case VLM_CLEAR_MEDIAS:
1101         return vlm_ControlMediaClear( p_vlm );
1102
1103     case VLM_CHANGE_MEDIA:
1104         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1105         return vlm_ControlMediaChange( p_vlm, p_dsc );
1106
1107     case VLM_ADD_MEDIA:
1108         p_dsc = (vlm_media_t*)va_arg( args, vlm_media_t * );
1109         p_id = (int64_t*)va_arg( args, int64_t * );
1110         return vlm_ControlMediaAdd( p_vlm, p_dsc, p_id );
1111
1112     case VLM_DEL_MEDIA:
1113         id = (int64_t)va_arg( args, int64_t );
1114         return vlm_ControlMediaDel( p_vlm, id );
1115
1116     case VLM_GET_MEDIA:
1117         id = (int64_t)va_arg( args, int64_t );
1118         pp_dsc = (vlm_media_t **)va_arg( args, vlm_media_t ** );
1119         return vlm_ControlMediaGet( p_vlm, id, pp_dsc );
1120
1121     case VLM_GET_MEDIA_ID:
1122         psz_id = (const char*)va_arg( args, const char * );
1123         p_id = (int64_t*)va_arg( args, int64_t * );
1124         return vlm_ControlMediaGetId( p_vlm, psz_id, p_id );
1125
1126
1127     /* Media instance control */
1128     case VLM_GET_MEDIA_INSTANCES:
1129         id = (int64_t)va_arg( args, int64_t );
1130         ppp_idsc = (vlm_media_instance_t ***)va_arg( args, vlm_media_instance_t *** );
1131         pi_int = (int *)va_arg( args, int *);
1132         return vlm_ControlMediaInstanceGets( p_vlm, id, ppp_idsc, pi_int );
1133
1134     case VLM_CLEAR_MEDIA_INSTANCES:
1135         id = (int64_t)va_arg( args, int64_t );
1136         return vlm_ControlMediaInstanceClear( p_vlm, id );
1137
1138
1139     case VLM_START_MEDIA_BROADCAST_INSTANCE:
1140         id = (int64_t)va_arg( args, int64_t );
1141         psz_id = (const char*)va_arg( args, const char* );
1142         i_int = (int)va_arg( args, int );
1143         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, NULL );
1144
1145     case VLM_START_MEDIA_VOD_INSTANCE:
1146         id = (int64_t)va_arg( args, int64_t );
1147         psz_id = (const char*)va_arg( args, const char* );
1148         i_int = (int)va_arg( args, int );
1149         psz_vod = (const char*)va_arg( args, const char* );
1150         if( !psz_vod )
1151             return VLC_EGENERIC;
1152         return vlm_ControlMediaInstanceStart( p_vlm, id, psz_id, i_int, psz_vod );
1153
1154     case VLM_STOP_MEDIA_INSTANCE:
1155         id = (int64_t)va_arg( args, int64_t );
1156         psz_id = (const char*)va_arg( args, const char* );
1157         return vlm_ControlMediaInstanceStop( p_vlm, id, psz_id );
1158
1159     case VLM_PAUSE_MEDIA_INSTANCE:
1160         id = (int64_t)va_arg( args, int64_t );
1161         psz_id = (const char*)va_arg( args, const char* );
1162         return vlm_ControlMediaInstancePause( p_vlm, id, psz_id );
1163
1164     case VLM_GET_MEDIA_INSTANCE_TIME:
1165         id = (int64_t)va_arg( args, int64_t );
1166         psz_id = (const char*)va_arg( args, const char* );
1167         pi_i64 = (int64_t*)va_arg( args, int64_t * );
1168         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, pi_i64, NULL );
1169     case VLM_GET_MEDIA_INSTANCE_POSITION:
1170         id = (int64_t)va_arg( args, int64_t );
1171         psz_id = (const char*)va_arg( args, const char* );
1172         pd_double = (double*)va_arg( args, double* );
1173         return vlm_ControlMediaInstanceGetTimePosition( p_vlm, id, psz_id, NULL, pd_double );
1174
1175     case VLM_SET_MEDIA_INSTANCE_TIME:
1176         id = (int64_t)va_arg( args, int64_t );
1177         psz_id = (const char*)va_arg( args, const char* );
1178         i_i64 = (int64_t)va_arg( args, int64_t);
1179         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, i_i64, -1 );
1180     case VLM_SET_MEDIA_INSTANCE_POSITION:
1181         id = (int64_t)va_arg( args, int64_t );
1182         psz_id = (const char*)va_arg( args, const char* );
1183         d_double = (double)va_arg( args, double );
1184         return vlm_ControlMediaInstanceSetTimePosition( p_vlm, id, psz_id, -1, d_double );
1185
1186     case VLM_CLEAR_SCHEDULES:
1187         return vlm_ControlScheduleClear( p_vlm );
1188
1189     default:
1190         msg_Err( p_vlm, "unknown VLM query" );
1191         return VLC_EGENERIC;
1192     }
1193 }
1194
1195 int vlm_ControlInternal( vlm_t *p_vlm, int i_query, ... )
1196 {
1197     va_list args;
1198     int     i_result;
1199
1200     va_start( args, i_query );
1201     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1202     va_end( args );
1203
1204     return i_result;
1205 }
1206
1207 int vlm_Control( vlm_t *p_vlm, int i_query, ... )
1208 {
1209     va_list args;
1210     int     i_result;
1211
1212     va_start( args, i_query );
1213
1214     vlc_mutex_lock( &p_vlm->lock );
1215     i_result = vlm_vaControlInternal( p_vlm, i_query, args );
1216     vlc_mutex_unlock( &p_vlm->lock );
1217
1218     va_end( args );
1219
1220     return i_result;
1221 }
1222