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