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