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