1 /*****************************************************************************
2 * vlm.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VLC authors and VideoLAN
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
36 #include <ctype.h> /* tolower() */
37 #include <time.h> /* ctime() */
40 #include <sys/time.h> /* gettimeofday() */
43 #include <vlc_modules.h>
45 #include <vlc_input.h>
46 #include <vlc_stream.h>
47 #include "vlm_internal.h"
48 #include "vlm_event.h"
52 #include "../stream_output/stream_output.h"
53 #include "../libvlc.h"
55 /*****************************************************************************
57 *****************************************************************************/
59 static void* Manage( void * );
60 static int vlm_MediaVodControl( void *, vod_media_t
*, const char *, int, va_list );
62 typedef struct preparse_data_t
68 static int InputEventPreparse( vlc_object_t
*p_this
, char const *psz_cmd
,
69 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
71 VLC_UNUSED(p_this
); VLC_UNUSED(psz_cmd
); VLC_UNUSED(oldval
);
72 preparse_data_t
*p_pre
= p_data
;
74 if( newval
.i_int
== INPUT_EVENT_DEAD
||
75 ( p_pre
->b_mux
&& newval
.i_int
== INPUT_EVENT_ITEM_META
) )
76 vlc_sem_post( p_pre
->p_sem
);
81 static int InputEvent( vlc_object_t
*p_this
, char const *psz_cmd
,
82 vlc_value_t oldval
, vlc_value_t newval
,
87 input_thread_t
*p_input
= (input_thread_t
*)p_this
;
88 vlm_t
*p_vlm
= libvlc_priv( p_input
->p_libvlc
)->p_vlm
;
90 vlm_media_sys_t
*p_media
= p_data
;
91 const char *psz_instance_name
= NULL
;
93 if( newval
.i_int
== INPUT_EVENT_STATE
)
95 for( int i
= 0; i
< p_media
->i_instance
; i
++ )
97 if( p_media
->instance
[i
]->p_input
== p_input
)
99 psz_instance_name
= p_media
->instance
[i
]->psz_name
;
103 vlm_SendEventMediaInstanceState( p_vlm
, p_media
->cfg
.id
, p_media
->cfg
.psz_name
, psz_instance_name
, var_GetInteger( p_input
, "state" ) );
105 vlc_mutex_lock( &p_vlm
->lock_manage
);
106 p_vlm
->input_state_changed
= true;
107 vlc_cond_signal( &p_vlm
->wait_manage
);
108 vlc_mutex_unlock( &p_vlm
->lock_manage
);
113 static vlc_mutex_t vlm_mutex
= VLC_STATIC_MUTEX
;
116 /*****************************************************************************
118 *****************************************************************************/
119 vlm_t
*vlm_New ( vlc_object_t
*p_this
)
121 vlm_t
*p_vlm
= NULL
, **pp_vlm
= &(libvlc_priv (p_this
->p_libvlc
)->p_vlm
);
124 /* Avoid multiple creation */
125 vlc_mutex_lock( &vlm_mutex
);
129 { /* VLM already exists */
130 if( likely( p_vlm
->users
< UINT_MAX
) )
134 vlc_mutex_unlock( &vlm_mutex
);
138 msg_Dbg( p_this
, "creating VLM" );
140 p_vlm
= vlc_custom_create( p_this
->p_libvlc
, sizeof( *p_vlm
),
144 vlc_mutex_unlock( &vlm_mutex
);
148 vlc_mutex_init( &p_vlm
->lock
);
149 vlc_mutex_init( &p_vlm
->lock_manage
);
150 vlc_cond_init_daytime( &p_vlm
->wait_manage
);
152 p_vlm
->input_state_changed
= false;
154 TAB_INIT( p_vlm
->i_media
, p_vlm
->media
);
155 TAB_INIT( p_vlm
->i_schedule
, p_vlm
->schedule
);
157 var_Create( p_vlm
, "intf-event", VLC_VAR_ADDRESS
);
159 if( vlc_clone( &p_vlm
->thread
, Manage
, p_vlm
, VLC_THREAD_PRIORITY_LOW
) )
161 vlc_cond_destroy( &p_vlm
->wait_manage
);
162 vlc_mutex_destroy( &p_vlm
->lock
);
163 vlc_mutex_destroy( &p_vlm
->lock_manage
);
164 vlc_object_release( p_vlm
);
165 vlc_mutex_unlock( &vlm_mutex
);
169 *pp_vlm
= p_vlm
; /* for future reference */
171 /* Load our configuration file */
172 psz_vlmconf
= var_CreateGetString( p_vlm
, "vlm-conf" );
173 if( psz_vlmconf
&& *psz_vlmconf
)
175 vlm_message_t
*p_message
= NULL
;
176 char *psz_buffer
= NULL
;
178 msg_Dbg( p_this
, "loading VLM configuration" );
179 if( asprintf(&psz_buffer
, "load %s", psz_vlmconf
) != -1 )
181 msg_Dbg( p_this
, "%s", psz_buffer
);
182 if( vlm_ExecuteCommand( p_vlm
, psz_buffer
, &p_message
) )
183 msg_Warn( p_this
, "error while loading the configuration file" );
185 vlm_MessageDelete( p_message
);
191 vlc_mutex_unlock( &vlm_mutex
);
196 /*****************************************************************************
198 *****************************************************************************/
199 void vlm_Delete( vlm_t
*p_vlm
)
201 /* vlm_Delete() is serialized against itself, and against vlm_New().
202 * This mutex protects libvlc_priv->p_vlm and p_vlm->users. */
203 vlc_mutex_lock( &vlm_mutex
);
204 assert( p_vlm
->users
> 0 );
205 if( --p_vlm
->users
== 0 )
206 assert( libvlc_priv(p_vlm
->p_libvlc
)->p_vlm
== p_vlm
);
212 vlc_mutex_unlock( &vlm_mutex
);
216 /* Destroy and release VLM */
217 vlc_mutex_lock( &p_vlm
->lock
);
218 vlm_ControlInternal( p_vlm
, VLM_CLEAR_MEDIAS
);
219 TAB_CLEAN( p_vlm
->i_media
, p_vlm
->media
);
221 vlm_ControlInternal( p_vlm
, VLM_CLEAR_SCHEDULES
);
222 TAB_CLEAN( p_vlm
->i_schedule
, p_vlm
->schedule
);
223 vlc_mutex_unlock( &p_vlm
->lock
);
225 vlc_cancel( p_vlm
->thread
);
229 module_unneed( p_vlm
->p_vod
, p_vlm
->p_vod
->p_module
);
230 vlc_object_release( p_vlm
->p_vod
);
233 libvlc_priv(p_vlm
->p_libvlc
)->p_vlm
= NULL
;
234 vlc_mutex_unlock( &vlm_mutex
);
236 vlc_join( p_vlm
->thread
, NULL
);
238 vlc_cond_destroy( &p_vlm
->wait_manage
);
239 vlc_mutex_destroy( &p_vlm
->lock
);
240 vlc_mutex_destroy( &p_vlm
->lock_manage
);
241 vlc_object_release( p_vlm
);
244 /*****************************************************************************
245 * vlm_ExecuteCommand:
246 *****************************************************************************/
247 int vlm_ExecuteCommand( vlm_t
*p_vlm
, const char *psz_command
,
248 vlm_message_t
**pp_message
)
252 vlc_mutex_lock( &p_vlm
->lock
);
253 i_result
= ExecuteCommand( p_vlm
, psz_command
, pp_message
);
254 vlc_mutex_unlock( &p_vlm
->lock
);
260 int64_t vlm_Date(void)
264 (void)gettimeofday( &tv
, NULL
);
265 return tv
.tv_sec
* INT64_C(1000000) + tv
.tv_usec
;
269 /*****************************************************************************
271 *****************************************************************************/
272 static int vlm_MediaVodControl( void *p_private
, vod_media_t
*p_vod_media
,
273 const char *psz_id
, int i_query
, va_list args
)
275 vlm_t
*vlm
= (vlm_t
*)p_private
;
280 if( !p_private
|| !p_vod_media
)
283 vlc_mutex_lock( &vlm
->lock
);
286 for( i
= 0, id
= -1; i
< vlm
->i_media
; i
++ )
288 if( p_vod_media
== vlm
->media
[i
]->vod
.p_media
)
290 id
= vlm
->media
[i
]->cfg
.id
;
296 vlc_mutex_unlock( &vlm
->lock
);
304 psz
= (const char *)va_arg( args
, const char * );
305 int64_t *i_time
= (int64_t *)va_arg( args
, int64_t *);
306 bool b_retry
= false;
309 /* No start time requested: return the current NPT */
310 i_ret
= vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
311 /* The instance is not running yet, it will start at 0 */
317 /* We want to seek before unpausing, but it won't
318 * work if the instance is not running yet. */
319 b_retry
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, *i_time
);
322 i_ret
= vlm_ControlInternal( vlm
, VLM_START_MEDIA_VOD_INSTANCE
, id
, psz_id
, 0, psz
);
324 if (!i_ret
&& b_retry
)
325 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, *i_time
);
329 case VOD_MEDIA_PAUSE
:
331 int64_t *i_time
= (int64_t *)va_arg( args
, int64_t *);
332 i_ret
= vlm_ControlInternal( vlm
, VLM_PAUSE_MEDIA_INSTANCE
, id
, psz_id
);
334 i_ret
= vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
339 i_ret
= vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, psz_id
);
344 int64_t i_time
= (int64_t)va_arg( args
, int64_t );
345 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_TIME
, id
, psz_id
, i_time
);
349 case VOD_MEDIA_REWIND
:
351 double d_scale
= (double)va_arg( args
, double );
354 vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, &d_position
);
355 d_position
-= (d_scale
/ 1000.0);
356 if( d_position
< 0.0 )
358 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, d_position
);
362 case VOD_MEDIA_FORWARD
:
364 double d_scale
= (double)va_arg( args
, double );
367 vlm_ControlInternal( vlm
, VLM_GET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, &d_position
);
368 d_position
+= (d_scale
/ 1000.0);
369 if( d_position
> 1.0 )
371 i_ret
= vlm_ControlInternal( vlm
, VLM_SET_MEDIA_INSTANCE_POSITION
, id
, psz_id
, d_position
);
376 i_ret
= VLC_EGENERIC
;
380 vlc_mutex_unlock( &vlm
->lock
);
386 /*****************************************************************************
388 *****************************************************************************/
389 static void* Manage( void* p_object
)
391 vlm_t
*vlm
= (vlm_t
*)p_object
;
395 mtime_t i_nextschedule
= 0;
397 i_lastcheck
= vlm_Date();
401 char **ppsz_scheduled_commands
= NULL
;
402 int i_scheduled_commands
= 0;
403 bool scheduled_command
= false;
405 vlc_mutex_lock( &vlm
->lock_manage
);
406 mutex_cleanup_push( &vlm
->lock_manage
);
407 while( !vlm
->input_state_changed
&& !scheduled_command
)
410 scheduled_command
= vlc_cond_timedwait( &vlm
->wait_manage
, &vlm
->lock_manage
, i_nextschedule
) != 0;
412 vlc_cond_wait( &vlm
->wait_manage
, &vlm
->lock_manage
);
414 vlm
->input_state_changed
= false;
416 vlc_mutex_unlock( &vlm
->lock_manage
);
418 int canc
= vlc_savecancel ();
419 /* destroy the inputs that wants to die, and launch the next input */
420 vlc_mutex_lock( &vlm
->lock
);
421 for( i
= 0; i
< vlm
->i_media
; i
++ )
423 vlm_media_sys_t
*p_media
= vlm
->media
[i
];
425 for( j
= 0; j
< p_media
->i_instance
; )
427 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[j
];
430 if( p_instance
->p_input
!= NULL
)
431 state
= var_GetInteger( p_instance
->p_input
, "state" );
432 if( state
== END_S
|| state
== ERROR_S
)
434 int i_new_input_index
;
437 i_new_input_index
= p_instance
->i_index
+ 1;
438 if( !p_media
->cfg
.b_vod
&& p_media
->cfg
.broadcast
.b_loop
&& i_new_input_index
>= p_media
->cfg
.i_input
)
439 i_new_input_index
= 0;
441 /* FIXME implement multiple input with VOD */
442 if( p_media
->cfg
.b_vod
|| i_new_input_index
>= p_media
->cfg
.i_input
)
443 vlm_ControlInternal( vlm
, VLM_STOP_MEDIA_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
);
445 vlm_ControlInternal( vlm
, VLM_START_MEDIA_BROADCAST_INSTANCE
, p_media
->cfg
.id
, p_instance
->psz_name
, i_new_input_index
);
460 for( i
= 0; i
< vlm
->i_schedule
; i
++ )
462 mtime_t i_real_date
= vlm
->schedule
[i
]->i_date
;
464 if( vlm
->schedule
[i
]->b_enabled
)
466 if( vlm
->schedule
[i
]->i_date
== 0 ) // now !
468 vlm
->schedule
[i
]->i_date
= (i_time
/ 1000000) * 1000000 ;
469 i_real_date
= i_time
;
471 else if( vlm
->schedule
[i
]->i_period
!= 0 )
474 while( vlm
->schedule
[i
]->i_date
+ j
*
475 vlm
->schedule
[i
]->i_period
<= i_lastcheck
&&
476 ( vlm
->schedule
[i
]->i_repeat
> j
||
477 vlm
->schedule
[i
]->i_repeat
== -1 ) )
482 i_real_date
= vlm
->schedule
[i
]->i_date
+ j
*
483 vlm
->schedule
[i
]->i_period
;
486 if( i_real_date
<= i_time
)
488 if( i_real_date
> i_lastcheck
)
490 for( j
= 0; j
< vlm
->schedule
[i
]->i_command
; j
++ )
492 TAB_APPEND( i_scheduled_commands
,
493 ppsz_scheduled_commands
,
494 strdup(vlm
->schedule
[i
]->command
[j
] ) );
498 else if( i_nextschedule
== 0 || i_real_date
< i_nextschedule
)
500 i_nextschedule
= i_real_date
;
505 while( i_scheduled_commands
)
507 vlm_message_t
*message
= NULL
;
508 char *psz_command
= ppsz_scheduled_commands
[0];
509 ExecuteCommand( vlm
, psz_command
,&message
);
511 /* for now, drop the message */
512 vlm_MessageDelete( message
);
513 TAB_REMOVE( i_scheduled_commands
,
514 ppsz_scheduled_commands
,
519 i_lastcheck
= i_time
;
520 vlc_mutex_unlock( &vlm
->lock
);
521 vlc_restorecancel (canc
);
534 int i_connection_count;
535 int i_connection_active;
544 } vlm_media_status_t;
548 static vlm_media_sys_t
*vlm_ControlMediaGetById( vlm_t
*p_vlm
, int64_t id
)
552 for( i
= 0; i
< p_vlm
->i_media
; i
++ )
554 if( p_vlm
->media
[i
]->cfg
.id
== id
)
555 return p_vlm
->media
[i
];
559 static vlm_media_sys_t
*vlm_ControlMediaGetByName( vlm_t
*p_vlm
, const char *psz_name
)
563 for( i
= 0; i
< p_vlm
->i_media
; i
++ )
565 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, psz_name
) )
566 return p_vlm
->media
[i
];
570 static int vlm_MediaDescriptionCheck( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
574 if( !p_cfg
|| !p_cfg
->psz_name
||
575 !strcmp( p_cfg
->psz_name
, "all" ) || !strcmp( p_cfg
->psz_name
, "media" ) || !strcmp( p_cfg
->psz_name
, "schedule" ) )
578 for( i
= 0; i
< p_vlm
->i_media
; i
++ )
580 if( p_vlm
->media
[i
]->cfg
.id
== p_cfg
->id
)
582 if( !strcmp( p_vlm
->media
[i
]->cfg
.psz_name
, p_cfg
->psz_name
) )
589 /* Called after a media description is changed/added */
590 static int vlm_OnMediaUpdate( vlm_t
*p_vlm
, vlm_media_sys_t
*p_media
)
592 vlm_media_t
*p_cfg
= &p_media
->cfg
;
593 /* Check if we need to create/delete a vod media */
594 if( p_cfg
->b_vod
&& p_vlm
->p_vod
)
596 if( !p_cfg
->b_enabled
&& p_media
->vod
.p_media
)
598 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
599 p_media
->vod
.p_media
= NULL
;
601 else if( p_cfg
->b_enabled
&& !p_media
->vod
.p_media
&& p_cfg
->i_input
)
603 /* Pre-parse the input */
604 input_thread_t
*p_input
;
610 vlc_gc_decref( p_media
->vod
.p_item
);
612 if( strstr( p_cfg
->ppsz_input
[0], "://" ) == NULL
)
614 char *psz_uri
= vlc_path2uri( p_cfg
->ppsz_input
[0], NULL
);
615 p_media
->vod
.p_item
= input_item_New( psz_uri
,
620 p_media
->vod
.p_item
= input_item_New( p_cfg
->ppsz_input
[0],
623 if( p_cfg
->psz_output
)
625 if( asprintf( &psz_output
, "%s:description", p_cfg
->psz_output
) == -1 )
629 psz_output
= strdup( "#description" );
631 if( psz_output
&& asprintf( &psz_dup
, "sout=%s", psz_output
) != -1 )
633 input_item_AddOption( p_media
->vod
.p_item
, psz_dup
, VLC_INPUT_OPTION_TRUSTED
);
638 for( i
= 0; i
< p_cfg
->i_option
; i
++ )
639 input_item_AddOption( p_media
->vod
.p_item
,
640 p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
642 if( asprintf( &psz_header
, _("Media: %s"), p_cfg
->psz_name
) == -1 )
645 sout_description_data_t data
;
646 TAB_INIT(data
.i_es
, data
.es
);
648 p_input
= input_Create( p_vlm
->p_vod
, p_media
->vod
.p_item
, psz_header
, NULL
);
651 vlc_sem_t sem_preparse
;
652 vlc_sem_init( &sem_preparse
, 0 );
654 preparse_data_t preparse
= { .p_sem
= &sem_preparse
,
655 .b_mux
= (p_cfg
->vod
.psz_mux
!= NULL
) };
656 var_AddCallback( p_input
, "intf-event", InputEventPreparse
,
659 data
.sem
= &sem_preparse
;
660 var_Create( p_input
, "sout-description-data", VLC_VAR_ADDRESS
);
661 var_SetAddress( p_input
, "sout-description-data", &data
);
663 if( !input_Start( p_input
) )
664 vlc_sem_wait( &sem_preparse
);
666 var_DelCallback( p_input
, "intf-event", InputEventPreparse
,
669 input_Stop( p_input
);
670 input_Close( p_input
);
671 vlc_sem_destroy( &sem_preparse
);
675 /* XXX: Don't do it that way, but properly use a new input item ref. */
676 input_item_t item
= *p_media
->vod
.p_item
;;
677 if( p_cfg
->vod
.psz_mux
)
680 if (!strcmp(p_cfg
->vod
.psz_mux
, "ps"))
682 else if (!strcmp(p_cfg
->vod
.psz_mux
, "ts"))
685 psz_mux
= p_cfg
->vod
.psz_mux
;
687 es_format_t es
, *p_es
= &es
;
690 unsigned char utext
[5];
694 sprintf( fourcc
.text
, "%4.4s", psz_mux
);
695 for( int i
= 0; i
< 4; i
++ )
696 fourcc
.utext
[i
] = tolower(fourcc
.utext
[i
]);
700 es_format_Init( &es
, VIDEO_ES
, fourcc
.value
);
704 item
.i_es
= data
.i_es
;
707 p_media
->vod
.p_media
= p_vlm
->p_vod
->pf_media_new( p_vlm
->p_vod
,
708 p_cfg
->psz_name
, &item
);
710 TAB_CLEAN(data
.i_es
, data
.es
);
713 else if ( p_cfg
->b_vod
)
714 msg_Err( p_vlm
, "vod server is not loaded" );
717 /* TODO start media if needed */
720 /* TODO add support of var vlm_media_broadcast/vlm_media_vod */
722 vlm_SendEventMediaChanged( p_vlm
, p_cfg
->id
, p_cfg
->psz_name
);
725 static int vlm_ControlMediaChange( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
)
727 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, p_cfg
->id
);
730 if( !p_media
|| vlm_MediaDescriptionCheck( p_vlm
, p_cfg
) )
732 if( ( p_media
->cfg
.b_vod
&& !p_cfg
->b_vod
) || ( !p_media
->cfg
.b_vod
&& p_cfg
->b_vod
) )
737 /* TODO check what are the changes being done (stop instance if needed) */
740 vlm_media_Clean( &p_media
->cfg
);
741 vlm_media_Copy( &p_media
->cfg
, p_cfg
);
743 return vlm_OnMediaUpdate( p_vlm
, p_media
);
746 static int vlm_ControlMediaAdd( vlm_t
*p_vlm
, vlm_media_t
*p_cfg
, int64_t *p_id
)
748 vlm_media_sys_t
*p_media
;
750 if( vlm_MediaDescriptionCheck( p_vlm
, p_cfg
) || vlm_ControlMediaGetByName( p_vlm
, p_cfg
->psz_name
) )
752 msg_Err( p_vlm
, "invalid media description" );
755 /* Check if we need to load the VOD server */
756 if( p_cfg
->b_vod
&& !p_vlm
->p_vod
)
758 p_vlm
->p_vod
= vlc_custom_create( VLC_OBJECT(p_vlm
), sizeof( vod_t
),
760 p_vlm
->p_vod
->p_module
= module_need( p_vlm
->p_vod
, "vod server", "$vod-server", false );
761 if( !p_vlm
->p_vod
->p_module
)
763 msg_Err( p_vlm
, "cannot find vod server" );
764 vlc_object_release( p_vlm
->p_vod
);
769 p_vlm
->p_vod
->p_data
= p_vlm
;
770 p_vlm
->p_vod
->pf_media_control
= vlm_MediaVodControl
;
773 p_media
= calloc( 1, sizeof( vlm_media_sys_t
) );
777 vlm_media_Copy( &p_media
->cfg
, p_cfg
);
778 p_media
->cfg
.id
= p_vlm
->i_id
++;
779 /* FIXME do we do something here if enabled is true ? */
781 p_media
->vod
.p_item
= input_item_New( NULL
, NULL
);
783 p_media
->vod
.p_media
= NULL
;
784 TAB_INIT( p_media
->i_instance
, p_media
->instance
);
787 TAB_APPEND( p_vlm
->i_media
, p_vlm
->media
, p_media
);
790 *p_id
= p_media
->cfg
.id
;
793 vlm_SendEventMediaAdded( p_vlm
, p_media
->cfg
.id
, p_media
->cfg
.psz_name
);
794 return vlm_OnMediaUpdate( p_vlm
, p_media
);
797 static int vlm_ControlMediaDel( vlm_t
*p_vlm
, int64_t id
)
799 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
804 while( p_media
->i_instance
> 0 )
805 vlm_ControlInternal( p_vlm
, VLM_STOP_MEDIA_INSTANCE
, id
, p_media
->instance
[0]->psz_name
);
807 if( p_media
->cfg
.b_vod
)
809 p_media
->cfg
.b_enabled
= false;
810 vlm_OnMediaUpdate( p_vlm
, p_media
);
814 vlm_SendEventMediaRemoved( p_vlm
, id
, p_media
->cfg
.psz_name
);
816 vlm_media_Clean( &p_media
->cfg
);
818 vlc_gc_decref( p_media
->vod
.p_item
);
820 if( p_media
->vod
.p_media
)
821 p_vlm
->p_vod
->pf_media_del( p_vlm
->p_vod
, p_media
->vod
.p_media
);
823 TAB_REMOVE( p_vlm
->i_media
, p_vlm
->media
, p_media
);
829 static int vlm_ControlMediaGets( vlm_t
*p_vlm
, vlm_media_t
***ppp_dsc
, int *pi_dsc
)
831 vlm_media_t
**pp_dsc
;
835 TAB_INIT( i_dsc
, pp_dsc
);
836 for( i
= 0; i
< p_vlm
->i_media
; i
++ )
838 vlm_media_t
*p_dsc
= vlm_media_Duplicate( &p_vlm
->media
[i
]->cfg
);
839 TAB_APPEND( i_dsc
, pp_dsc
, p_dsc
);
847 static int vlm_ControlMediaClear( vlm_t
*p_vlm
)
849 while( p_vlm
->i_media
> 0 )
850 vlm_ControlMediaDel( p_vlm
, p_vlm
->media
[0]->cfg
.id
);
854 static int vlm_ControlMediaGet( vlm_t
*p_vlm
, int64_t id
, vlm_media_t
**pp_dsc
)
856 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
860 *pp_dsc
= vlm_media_Duplicate( &p_media
->cfg
);
863 static int vlm_ControlMediaGetId( vlm_t
*p_vlm
, const char *psz_name
, int64_t *p_id
)
865 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetByName( p_vlm
, psz_name
);
869 *p_id
= p_media
->cfg
.id
;
873 static vlm_media_instance_sys_t
*vlm_ControlMediaInstanceGetByName( vlm_media_sys_t
*p_media
, const char *psz_id
)
877 for( i
= 0; i
< p_media
->i_instance
; i
++ )
879 const char *psz
= p_media
->instance
[i
]->psz_name
;
880 if( ( psz
== NULL
&& psz_id
== NULL
) ||
881 ( psz
&& psz_id
&& !strcmp( psz
, psz_id
) ) )
882 return p_media
->instance
[i
];
886 static vlm_media_instance_sys_t
*vlm_MediaInstanceNew( vlm_t
*p_vlm
, const char *psz_name
)
888 vlm_media_instance_sys_t
*p_instance
= calloc( 1, sizeof(vlm_media_instance_sys_t
) );
892 p_instance
->psz_name
= NULL
;
894 p_instance
->psz_name
= strdup( psz_name
);
896 p_instance
->p_item
= input_item_New( NULL
, NULL
);
898 p_instance
->i_index
= 0;
899 p_instance
->b_sout_keep
= false;
900 p_instance
->p_parent
= vlc_object_create( p_vlm
, sizeof (vlc_object_t
) );
901 p_instance
->p_input
= NULL
;
902 p_instance
->p_input_resource
= input_resource_New( p_instance
->p_parent
);
906 static void vlm_MediaInstanceDelete( vlm_t
*p_vlm
, int64_t id
, vlm_media_instance_sys_t
*p_instance
, vlm_media_sys_t
*p_media
)
908 input_thread_t
*p_input
= p_instance
->p_input
;
911 input_Stop( p_input
);
912 input_Close( p_input
);
914 vlm_SendEventMediaInstanceStopped( p_vlm
, id
, p_media
->cfg
.psz_name
);
916 input_resource_Terminate( p_instance
->p_input_resource
);
917 input_resource_Release( p_instance
->p_input_resource
);
918 vlc_object_release( p_instance
->p_parent
);
920 TAB_REMOVE( p_media
->i_instance
, p_media
->instance
, p_instance
);
921 vlc_gc_decref( p_instance
->p_item
);
922 free( p_instance
->psz_name
);
927 static int vlm_ControlMediaInstanceStart( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int i_input_index
, const char *psz_vod_output
)
929 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
930 vlm_media_instance_sys_t
*p_instance
;
933 if( !p_media
|| !p_media
->cfg
.b_enabled
|| p_media
->cfg
.i_input
<= 0 )
936 /* TODO support multiple input for VOD with sout-keep ? */
938 if( ( p_media
->cfg
.b_vod
&& !psz_vod_output
) || ( !p_media
->cfg
.b_vod
&& psz_vod_output
) )
941 if( i_input_index
< 0 || i_input_index
>= p_media
->cfg
.i_input
)
944 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
947 vlm_media_t
*p_cfg
= &p_media
->cfg
;
950 p_instance
= vlm_MediaInstanceNew( p_vlm
, psz_id
);
956 var_Create( p_instance
->p_parent
, "vod-media", VLC_VAR_ADDRESS
);
957 var_SetAddress( p_instance
->p_parent
, "vod-media",
958 p_media
->vod
.p_media
);
959 var_Create( p_instance
->p_parent
, "vod-session", VLC_VAR_STRING
);
960 var_SetString( p_instance
->p_parent
, "vod-session", psz_id
);
963 if( p_cfg
->psz_output
!= NULL
|| psz_vod_output
!= NULL
)
966 if( asprintf( &psz_buffer
, "sout=%s%s%s",
967 p_cfg
->psz_output
? p_cfg
->psz_output
: "",
968 (p_cfg
->psz_output
&& psz_vod_output
) ? ":" : psz_vod_output
? "#" : "",
969 psz_vod_output
? psz_vod_output
: "" ) != -1 )
971 input_item_AddOption( p_instance
->p_item
, psz_buffer
, VLC_INPUT_OPTION_TRUSTED
);
976 for( i
= 0; i
< p_cfg
->i_option
; i
++ )
978 if( !strcmp( p_cfg
->ppsz_option
[i
], "sout-keep" ) )
979 p_instance
->b_sout_keep
= true;
980 else if( !strcmp( p_cfg
->ppsz_option
[i
], "nosout-keep" ) || !strcmp( p_cfg
->ppsz_option
[i
], "no-sout-keep" ) )
981 p_instance
->b_sout_keep
= false;
983 input_item_AddOption( p_instance
->p_item
, p_cfg
->ppsz_option
[i
], VLC_INPUT_OPTION_TRUSTED
);
985 TAB_APPEND( p_media
->i_instance
, p_media
->instance
, p_instance
);
988 /* Stop old instance */
989 input_thread_t
*p_input
= p_instance
->p_input
;
992 if( p_instance
->i_index
== i_input_index
)
994 int state
= var_GetInteger( p_input
, "state" );
995 if( state
== PAUSE_S
)
996 var_SetInteger( p_input
, "state", PLAYING_S
);
1000 input_Stop( p_input
);
1001 input_Close( p_input
);
1003 if( !p_instance
->b_sout_keep
)
1004 input_resource_TerminateSout( p_instance
->p_input_resource
);
1005 input_resource_TerminateVout( p_instance
->p_input_resource
);
1007 vlm_SendEventMediaInstanceStopped( p_vlm
, id
, p_media
->cfg
.psz_name
);
1011 p_instance
->i_index
= i_input_index
;
1012 if( strstr( p_media
->cfg
.ppsz_input
[p_instance
->i_index
], "://" ) == NULL
)
1014 char *psz_uri
= vlc_path2uri(
1015 p_media
->cfg
.ppsz_input
[p_instance
->i_index
], NULL
);
1016 input_item_SetURI( p_instance
->p_item
, psz_uri
) ;
1020 input_item_SetURI( p_instance
->p_item
, p_media
->cfg
.ppsz_input
[p_instance
->i_index
] ) ;
1022 if( asprintf( &psz_log
, _("Media: %s"), p_media
->cfg
.psz_name
) != -1 )
1024 p_instance
->p_input
= input_Create( p_instance
->p_parent
,
1025 p_instance
->p_item
, psz_log
,
1026 p_instance
->p_input_resource
);
1027 if( p_instance
->p_input
)
1029 var_AddCallback( p_instance
->p_input
, "intf-event", InputEvent
, p_media
);
1031 if( input_Start( p_instance
->p_input
) != VLC_SUCCESS
)
1033 var_DelCallback( p_instance
->p_input
, "intf-event", InputEvent
, p_media
);
1034 input_Close( p_instance
->p_input
);
1035 p_instance
->p_input
= NULL
;
1039 if( !p_instance
->p_input
)
1041 vlm_MediaInstanceDelete( p_vlm
, id
, p_instance
, p_media
);
1045 vlm_SendEventMediaInstanceStarted( p_vlm
, id
, p_media
->cfg
.psz_name
);
1053 static int vlm_ControlMediaInstanceStop( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
)
1055 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1056 vlm_media_instance_sys_t
*p_instance
;
1059 return VLC_EGENERIC
;
1061 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1063 return VLC_EGENERIC
;
1065 vlm_MediaInstanceDelete( p_vlm
, id
, p_instance
, p_media
);
1069 static int vlm_ControlMediaInstancePause( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
)
1071 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1072 vlm_media_instance_sys_t
*p_instance
;
1076 return VLC_EGENERIC
;
1078 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1079 if( !p_instance
|| !p_instance
->p_input
)
1080 return VLC_EGENERIC
;
1082 /* Toggle pause state */
1083 i_state
= var_GetInteger( p_instance
->p_input
, "state" );
1084 if( i_state
== PAUSE_S
&& !p_media
->cfg
.b_vod
)
1085 var_SetInteger( p_instance
->p_input
, "state", PLAYING_S
);
1086 else if( i_state
== PLAYING_S
)
1087 var_SetInteger( p_instance
->p_input
, "state", PAUSE_S
);
1090 static int vlm_ControlMediaInstanceGetTimePosition( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int64_t *pi_time
, double *pd_position
)
1092 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1093 vlm_media_instance_sys_t
*p_instance
;
1096 return VLC_EGENERIC
;
1098 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1099 if( !p_instance
|| !p_instance
->p_input
)
1100 return VLC_EGENERIC
;
1103 *pi_time
= var_GetInteger( p_instance
->p_input
, "time" );
1105 *pd_position
= var_GetFloat( p_instance
->p_input
, "position" );
1108 static int vlm_ControlMediaInstanceSetTimePosition( vlm_t
*p_vlm
, int64_t id
, const char *psz_id
, int64_t i_time
, double d_position
)
1110 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1111 vlm_media_instance_sys_t
*p_instance
;
1114 return VLC_EGENERIC
;
1116 p_instance
= vlm_ControlMediaInstanceGetByName( p_media
, psz_id
);
1117 if( !p_instance
|| !p_instance
->p_input
)
1118 return VLC_EGENERIC
;
1121 return var_SetInteger( p_instance
->p_input
, "time", i_time
);
1122 else if( d_position
>= 0 && d_position
<= 100 )
1123 return var_SetFloat( p_instance
->p_input
, "position", d_position
);
1124 return VLC_EGENERIC
;
1127 static int vlm_ControlMediaInstanceGets( vlm_t
*p_vlm
, int64_t id
, vlm_media_instance_t
***ppp_idsc
, int *pi_instance
)
1129 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1130 vlm_media_instance_t
**pp_idsc
;
1135 return VLC_EGENERIC
;
1137 TAB_INIT( i_idsc
, pp_idsc
);
1138 for( i
= 0; i
< p_media
->i_instance
; i
++ )
1140 vlm_media_instance_sys_t
*p_instance
= p_media
->instance
[i
];
1141 vlm_media_instance_t
*p_idsc
= vlm_media_instance_New();
1143 if( p_instance
->psz_name
)
1144 p_idsc
->psz_name
= strdup( p_instance
->psz_name
);
1145 if( p_instance
->p_input
)
1147 p_idsc
->i_time
= var_GetInteger( p_instance
->p_input
, "time" );
1148 p_idsc
->i_length
= var_GetInteger( p_instance
->p_input
, "length" );
1149 p_idsc
->d_position
= var_GetFloat( p_instance
->p_input
, "position" );
1150 if( var_GetInteger( p_instance
->p_input
, "state" ) == PAUSE_S
)
1151 p_idsc
->b_paused
= true;
1152 p_idsc
->i_rate
= INPUT_RATE_DEFAULT
1153 / var_GetFloat( p_instance
->p_input
, "rate" );
1156 TAB_APPEND( i_idsc
, pp_idsc
, p_idsc
);
1158 *ppp_idsc
= pp_idsc
;
1159 *pi_instance
= i_idsc
;
1163 static int vlm_ControlMediaInstanceClear( vlm_t
*p_vlm
, int64_t id
)
1165 vlm_media_sys_t
*p_media
= vlm_ControlMediaGetById( p_vlm
, id
);
1168 return VLC_EGENERIC
;
1170 while( p_media
->i_instance
> 0 )
1171 vlm_ControlMediaInstanceStop( p_vlm
, id
, p_media
->instance
[0]->psz_name
);
1176 static int vlm_ControlScheduleClear( vlm_t
*p_vlm
)
1178 while( p_vlm
->i_schedule
> 0 )
1179 vlm_ScheduleDelete( p_vlm
, p_vlm
->schedule
[0] );
1184 static int vlm_vaControlInternal( vlm_t
*p_vlm
, int i_query
, va_list args
)
1187 vlm_media_t
**pp_dsc
;
1188 vlm_media_t
***ppp_dsc
;
1189 vlm_media_instance_t
***ppp_idsc
;
1191 const char *psz_vod
;
1205 case VLM_GET_MEDIAS
:
1206 ppp_dsc
= (vlm_media_t
***)va_arg( args
, vlm_media_t
*** );
1207 pi_int
= (int *)va_arg( args
, int * );
1208 return vlm_ControlMediaGets( p_vlm
, ppp_dsc
, pi_int
);
1210 case VLM_CLEAR_MEDIAS
:
1211 return vlm_ControlMediaClear( p_vlm
);
1213 case VLM_CHANGE_MEDIA
:
1214 p_dsc
= (vlm_media_t
*)va_arg( args
, vlm_media_t
* );
1215 return vlm_ControlMediaChange( p_vlm
, p_dsc
);
1218 p_dsc
= (vlm_media_t
*)va_arg( args
, vlm_media_t
* );
1219 p_id
= (int64_t*)va_arg( args
, int64_t * );
1220 return vlm_ControlMediaAdd( p_vlm
, p_dsc
, p_id
);
1223 id
= (int64_t)va_arg( args
, int64_t );
1224 return vlm_ControlMediaDel( p_vlm
, id
);
1227 id
= (int64_t)va_arg( args
, int64_t );
1228 pp_dsc
= (vlm_media_t
**)va_arg( args
, vlm_media_t
** );
1229 return vlm_ControlMediaGet( p_vlm
, id
, pp_dsc
);
1231 case VLM_GET_MEDIA_ID
:
1232 psz_id
= (const char*)va_arg( args
, const char * );
1233 p_id
= (int64_t*)va_arg( args
, int64_t * );
1234 return vlm_ControlMediaGetId( p_vlm
, psz_id
, p_id
);
1237 /* Media instance control */
1238 case VLM_GET_MEDIA_INSTANCES
:
1239 id
= (int64_t)va_arg( args
, int64_t );
1240 ppp_idsc
= (vlm_media_instance_t
***)va_arg( args
, vlm_media_instance_t
*** );
1241 pi_int
= (int *)va_arg( args
, int *);
1242 return vlm_ControlMediaInstanceGets( p_vlm
, id
, ppp_idsc
, pi_int
);
1244 case VLM_CLEAR_MEDIA_INSTANCES
:
1245 id
= (int64_t)va_arg( args
, int64_t );
1246 return vlm_ControlMediaInstanceClear( p_vlm
, id
);
1249 case VLM_START_MEDIA_BROADCAST_INSTANCE
:
1250 id
= (int64_t)va_arg( args
, int64_t );
1251 psz_id
= (const char*)va_arg( args
, const char* );
1252 i_int
= (int)va_arg( args
, int );
1253 return vlm_ControlMediaInstanceStart( p_vlm
, id
, psz_id
, i_int
, NULL
);
1255 case VLM_START_MEDIA_VOD_INSTANCE
:
1256 id
= (int64_t)va_arg( args
, int64_t );
1257 psz_id
= (const char*)va_arg( args
, const char* );
1258 i_int
= (int)va_arg( args
, int );
1259 psz_vod
= (const char*)va_arg( args
, const char* );
1261 return VLC_EGENERIC
;
1262 return vlm_ControlMediaInstanceStart( p_vlm
, id
, psz_id
, i_int
, psz_vod
);
1264 case VLM_STOP_MEDIA_INSTANCE
:
1265 id
= (int64_t)va_arg( args
, int64_t );
1266 psz_id
= (const char*)va_arg( args
, const char* );
1267 return vlm_ControlMediaInstanceStop( p_vlm
, id
, psz_id
);
1269 case VLM_PAUSE_MEDIA_INSTANCE
:
1270 id
= (int64_t)va_arg( args
, int64_t );
1271 psz_id
= (const char*)va_arg( args
, const char* );
1272 return vlm_ControlMediaInstancePause( p_vlm
, id
, psz_id
);
1274 case VLM_GET_MEDIA_INSTANCE_TIME
:
1275 id
= (int64_t)va_arg( args
, int64_t );
1276 psz_id
= (const char*)va_arg( args
, const char* );
1277 pi_i64
= (int64_t*)va_arg( args
, int64_t * );
1278 return vlm_ControlMediaInstanceGetTimePosition( p_vlm
, id
, psz_id
, pi_i64
, NULL
);
1279 case VLM_GET_MEDIA_INSTANCE_POSITION
:
1280 id
= (int64_t)va_arg( args
, int64_t );
1281 psz_id
= (const char*)va_arg( args
, const char* );
1282 pd_double
= (double*)va_arg( args
, double* );
1283 return vlm_ControlMediaInstanceGetTimePosition( p_vlm
, id
, psz_id
, NULL
, pd_double
);
1285 case VLM_SET_MEDIA_INSTANCE_TIME
:
1286 id
= (int64_t)va_arg( args
, int64_t );
1287 psz_id
= (const char*)va_arg( args
, const char* );
1288 i_i64
= (int64_t)va_arg( args
, int64_t);
1289 return vlm_ControlMediaInstanceSetTimePosition( p_vlm
, id
, psz_id
, i_i64
, -1 );
1290 case VLM_SET_MEDIA_INSTANCE_POSITION
:
1291 id
= (int64_t)va_arg( args
, int64_t );
1292 psz_id
= (const char*)va_arg( args
, const char* );
1293 d_double
= (double)va_arg( args
, double );
1294 return vlm_ControlMediaInstanceSetTimePosition( p_vlm
, id
, psz_id
, -1, d_double
);
1296 case VLM_CLEAR_SCHEDULES
:
1297 return vlm_ControlScheduleClear( p_vlm
);
1300 msg_Err( p_vlm
, "unknown VLM query" );
1301 return VLC_EGENERIC
;
1305 int vlm_ControlInternal( vlm_t
*p_vlm
, int i_query
, ... )
1310 va_start( args
, i_query
);
1311 i_result
= vlm_vaControlInternal( p_vlm
, i_query
, args
);
1317 int vlm_Control( vlm_t
*p_vlm
, int i_query
, ... )
1322 va_start( args
, i_query
);
1324 vlc_mutex_lock( &p_vlm
->lock
);
1325 i_result
= vlm_vaControlInternal( p_vlm
, i_query
, args
);
1326 vlc_mutex_unlock( &p_vlm
->lock
);