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