]> git.sesse.net Git - vlc/blobdiff - src/input/vlm.c
win32: remove non-standard empty struct
[vlc] / src / input / vlm.c
index 290100aad791c66a2bb3f29de98e353c9b1548d7..0f577687f56cd1533e5c164a7298c1c624737bb0 100644 (file)
@@ -1,26 +1,26 @@
 /*****************************************************************************
  * vlm.c: VLM interface plugin
  *****************************************************************************
- * Copyright (C) 2000-2005 the VideoLAN team
+ * Copyright (C) 2000-2005 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Simon Latapie <garf@videolan.org>
  *          Laurent Aimar <fenrir@videolan.org>
  *          Gildas Bazin <gbazin@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 
 #include <stdio.h>
 #include <ctype.h>                                              /* tolower() */
+#include <time.h>                                                 /* ctime() */
+#include <limits.h>
 #include <assert.h>
+#include <sys/time.h>                                      /* gettimeofday() */
 
 #include <vlc_vlm.h>
 #include <vlc_modules.h>
 
-#ifndef WIN32
-#   include <sys/time.h>                                   /* gettimeofday() */
-#endif
-
-#ifdef UNDER_CE
-#include <sys/time.h>                                      /* gettimeofday() */
-#endif
-
-#include <time.h>                                                 /* ctime() */
-#if defined (WIN32) && !defined (UNDER_CE)
-#include <sys/timeb.h>                                            /* ftime() */
-#endif
-
 #include <vlc_input.h>
 #include <vlc_stream.h>
 #include "vlm_internal.h"
@@ -66,7 +56,6 @@
  * Local prototypes.
  *****************************************************************************/
 
-static void vlm_Destructor( vlm_t *p_vlm );
 static void* Manage( void * );
 static int vlm_MediaVodControl( void *, vod_media_t *, const char *, int, va_list );
 
@@ -131,7 +120,6 @@ vlm_t *vlm_New ( vlc_object_t *p_this )
 {
     vlm_t *p_vlm = NULL, **pp_vlm = &(libvlc_priv (p_this->p_libvlc)->p_vlm);
     char *psz_vlmconf;
-    static const char vlm_object_name[] = "vlm daemon";
 
     /* Avoid multiple creation */
     vlc_mutex_lock( &vlm_mutex );
@@ -139,15 +127,18 @@ vlm_t *vlm_New ( vlc_object_t *p_this )
     p_vlm = *pp_vlm;
     if( p_vlm )
     {   /* VLM already exists */
-        vlc_object_hold( p_vlm );
+        if( likely( p_vlm->users < UINT_MAX ) )
+            p_vlm->users++;
+        else
+            p_vlm = NULL;
         vlc_mutex_unlock( &vlm_mutex );
         return p_vlm;
     }
 
     msg_Dbg( p_this, "creating VLM" );
 
-    p_vlm = vlc_custom_create( p_this, sizeof( *p_vlm ), VLC_OBJECT_GENERIC,
-                               vlm_object_name );
+    p_vlm = vlc_custom_create( p_this->p_libvlc, sizeof( *p_vlm ),
+                               "vlm daemon" );
     if( !p_vlm )
     {
         vlc_mutex_unlock( &vlm_mutex );
@@ -157,13 +148,13 @@ vlm_t *vlm_New ( vlc_object_t *p_this )
     vlc_mutex_init( &p_vlm->lock );
     vlc_mutex_init( &p_vlm->lock_manage );
     vlc_cond_init_daytime( &p_vlm->wait_manage );
+    p_vlm->users = 1;
     p_vlm->input_state_changed = false;
     p_vlm->i_id = 1;
     TAB_INIT( p_vlm->i_media, p_vlm->media );
     TAB_INIT( p_vlm->i_schedule, p_vlm->schedule );
     p_vlm->p_vod = NULL;
     var_Create( p_vlm, "intf-event", VLC_VAR_ADDRESS );
-    vlc_object_attach( p_vlm, p_this->p_libvlc );
 
     if( vlc_clone( &p_vlm->thread, Manage, p_vlm, VLC_THREAD_PRIORITY_LOW ) )
     {
@@ -197,7 +188,6 @@ vlm_t *vlm_New ( vlc_object_t *p_this )
     }
     free( psz_vlmconf );
 
-    vlc_object_set_destructor( p_vlm, (vlc_destructor_t)vlm_Destructor );
     vlc_mutex_unlock( &vlm_mutex );
 
     return p_vlm;
@@ -209,28 +199,30 @@ vlm_t *vlm_New ( vlc_object_t *p_this )
 void vlm_Delete( vlm_t *p_vlm )
 {
     /* vlm_Delete() is serialized against itself, and against vlm_New().
-     * This way, vlm_Destructor () (called from vlc_objet_release() above)
-     * is serialized against setting libvlc_priv->p_vlm from vlm_New(). */
+     * This mutex protects libvlc_priv->p_vlm and p_vlm->users. */
     vlc_mutex_lock( &vlm_mutex );
-    vlc_object_release( p_vlm );
-    vlc_mutex_unlock( &vlm_mutex );
-}
+    assert( p_vlm->users > 0 );
+    if( --p_vlm->users == 0 )
+        assert( libvlc_priv(p_vlm->p_libvlc)->p_vlm == p_vlm );
+    else
+        p_vlm = NULL;
 
-/*****************************************************************************
- * vlm_Destructor:
- *****************************************************************************/
-static void vlm_Destructor( vlm_t *p_vlm )
-{
+    if( p_vlm == NULL )
+    {
+        vlc_mutex_unlock( &vlm_mutex );
+        return;
+    }
+
+    /* Destroy and release VLM */
     vlc_mutex_lock( &p_vlm->lock );
     vlm_ControlInternal( p_vlm, VLM_CLEAR_MEDIAS );
     TAB_CLEAN( p_vlm->i_media, p_vlm->media );
 
     vlm_ControlInternal( p_vlm, VLM_CLEAR_SCHEDULES );
-    TAB_CLEAN( p_vlm->schedule, p_vlm->schedule );
+    TAB_CLEAN( p_vlm->i_schedule, p_vlm->schedule );
     vlc_mutex_unlock( &p_vlm->lock );
 
-    libvlc_priv(p_vlm->p_libvlc)->p_vlm = NULL;
-    vlc_object_kill( p_vlm );
+    vlc_cancel( p_vlm->thread );
 
     if( p_vlm->p_vod )
     {
@@ -238,17 +230,15 @@ static void vlm_Destructor( vlm_t *p_vlm )
         vlc_object_release( p_vlm->p_vod );
     }
 
-    vlc_mutex_lock( &p_vlm->lock_manage );
-    p_vlm->input_state_changed = true;
-    vlc_cond_signal( &p_vlm->wait_manage );
-    vlc_mutex_unlock( &p_vlm->lock_manage );
+    libvlc_priv(p_vlm->p_libvlc)->p_vlm = NULL;
+    vlc_mutex_unlock( &vlm_mutex );
 
-    /*vlc_cancel( p_vlm->thread ); */
     vlc_join( p_vlm->thread, NULL );
 
     vlc_cond_destroy( &p_vlm->wait_manage );
     vlc_mutex_destroy( &p_vlm->lock );
     vlc_mutex_destroy( &p_vlm->lock_manage );
+    vlc_object_release( p_vlm );
 }
 
 /*****************************************************************************
@@ -269,17 +259,10 @@ int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
 
 int64_t vlm_Date(void)
 {
-#if defined (WIN32) && !defined (UNDER_CE)
-    struct timeb tm;
-    ftime( &tm );
-    return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
-#else
-    struct timeval tv_date;
-
-    /* gettimeofday() cannot fail given &tv_date is a valid address */
-    (void)gettimeofday( &tv_date, NULL );
-    return (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
-#endif
+    struct timeval tv;
+
+    (void)gettimeofday( &tv, NULL );
+    return tv.tv_sec * INT64_C(1000000) + tv.tv_usec;
 }
 
 
@@ -317,13 +300,40 @@ static int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
     switch( i_query )
     {
     case VOD_MEDIA_PLAY:
+    {
         psz = (const char *)va_arg( args, const char * );
+        int64_t *i_time = (int64_t *)va_arg( args, int64_t *);
+        bool b_retry = false;
+        if (*i_time < 0)
+        {
+            /* No start time requested: return the current NPT */
+            i_ret = vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_TIME, id, psz_id, i_time );
+            /* The instance is not running yet, it will start at 0 */
+            if (i_ret)
+                *i_time = 0;
+        }
+        else
+        {
+            /* We want to seek before unpausing, but it won't
+             * work if the instance is not running yet. */
+            b_retry = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_TIME, id, psz_id, *i_time );
+        }
+
         i_ret = vlm_ControlInternal( vlm, VLM_START_MEDIA_VOD_INSTANCE, id, psz_id, 0, psz );
+
+        if (!i_ret && b_retry)
+            i_ret = vlm_ControlInternal( vlm, VLM_SET_MEDIA_INSTANCE_TIME, id, psz_id, *i_time );
         break;
+    }
 
     case VOD_MEDIA_PAUSE:
+    {
+        int64_t *i_time = (int64_t *)va_arg( args, int64_t *);
         i_ret = vlm_ControlInternal( vlm, VLM_PAUSE_MEDIA_INSTANCE, id, psz_id );
+        if (!i_ret)
+            i_ret = vlm_ControlInternal( vlm, VLM_GET_MEDIA_INSTANCE_TIME, id, psz_id, i_time );
         break;
+    }
 
     case VOD_MEDIA_STOP:
         i_ret = vlm_ControlInternal( vlm, VLM_STOP_MEDIA_INSTANCE, id, psz_id );
@@ -384,16 +394,16 @@ static void* Manage( void* p_object )
     mtime_t i_time;
     mtime_t i_nextschedule = 0;
 
-    int canc = vlc_savecancel ();
     i_lastcheck = vlm_Date();
 
-    while( !vlm->b_die )
+    for( ;; )
     {
         char **ppsz_scheduled_commands = NULL;
         int    i_scheduled_commands = 0;
         bool scheduled_command = false;
 
         vlc_mutex_lock( &vlm->lock_manage );
+        mutex_cleanup_push( &vlm->lock_manage );
         while( !vlm->input_state_changed && !scheduled_command )
         {
             if( i_nextschedule )
@@ -402,7 +412,9 @@ static void* Manage( void* p_object )
                 vlc_cond_wait( &vlm->wait_manage, &vlm->lock_manage );
         }
         vlm->input_state_changed = false;
-        vlc_mutex_unlock( &vlm->lock_manage );
+        vlc_cleanup_run( );
+
+        int canc = vlc_savecancel ();
         /* destroy the inputs that wants to die, and launch the next input */
         vlc_mutex_lock( &vlm->lock );
         for( i = 0; i < vlm->i_media; i++ )
@@ -445,7 +457,7 @@ static void* Manage( void* p_object )
         {
             mtime_t i_real_date = vlm->schedule[i]->i_date;
 
-            if( vlm->schedule[i]->b_enabled == true )
+            if( vlm->schedule[i]->b_enabled )
             {
                 if( vlm->schedule[i]->i_date == 0 ) // now !
                 {
@@ -479,7 +491,7 @@ static void* Manage( void* p_object )
                         }
                     }
                 }
-                else
+                else if( i_nextschedule == 0 || i_real_date < i_nextschedule )
                 {
                     i_nextschedule = i_real_date;
                 }
@@ -502,10 +514,9 @@ static void* Manage( void* p_object )
 
         i_lastcheck = i_time;
         vlc_mutex_unlock( &vlm->lock );
-
+        vlc_restorecancel (canc);
     }
 
-    vlc_restorecancel (canc);
     return NULL;
 }
 
@@ -594,10 +605,16 @@ static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
 
             vlc_gc_decref( p_media->vod.p_item );
 
-            char *psz_uri = make_URI( p_cfg->ppsz_input[0], NULL );
-            p_media->vod.p_item = input_item_New( p_vlm, psz_uri,
-                p_cfg->psz_name );
-            free( psz_uri );
+            if( strstr( p_cfg->ppsz_input[0], "://" ) == NULL )
+            {
+                char *psz_uri = vlc_path2uri( p_cfg->ppsz_input[0], NULL );
+                p_media->vod.p_item = input_item_New( psz_uri,
+                                                      p_cfg->psz_name );
+                free( psz_uri );
+            }
+            else
+                p_media->vod.p_item = input_item_New( p_cfg->ppsz_input[0],
+                                                      p_cfg->psz_name );
 
             if( p_cfg->psz_output )
             {
@@ -645,9 +662,8 @@ static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
                 var_DelCallback( p_input, "intf-event", InputEventPreparse,
                                  &preparse );
 
-                input_Stop( p_input, true );
-                vlc_thread_join( p_input );
-                vlc_object_release( p_input );
+                input_Stop( p_input );
+                input_Close( p_input );
                 vlc_sem_destroy( &sem_preparse );
             }
             free( psz_header );
@@ -665,13 +681,15 @@ static int vlm_OnMediaUpdate( vlm_t *p_vlm, vlm_media_sys_t *p_media )
                     psz_mux = p_cfg->vod.psz_mux;
 
                 es_format_t es, *p_es = &es;
-                union { char text[5]; uint32_t value; } fourcc;
+                union {
+                    char text[5];
+                    unsigned char utext[5];
+                    uint32_t value;
+                } fourcc;
 
                 sprintf( fourcc.text, "%4.4s", psz_mux );
-                fourcc.text[0] = tolower(fourcc.text[0]);
-                fourcc.text[1] = tolower(fourcc.text[1]);
-                fourcc.text[2] = tolower(fourcc.text[2]);
-                fourcc.text[3] = tolower(fourcc.text[3]);
+                for( int i = 0; i < 4; i++ )
+                    fourcc.utext[i] = tolower(fourcc.utext[i]);
 
                 item.i_es = 1;
                 item.es = &p_es;
@@ -734,8 +752,7 @@ static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id
     if( p_cfg->b_vod && !p_vlm->p_vod )
     {
         p_vlm->p_vod = vlc_custom_create( VLC_OBJECT(p_vlm), sizeof( vod_t ),
-                                          VLC_OBJECT_GENERIC, "vod server" );
-        vlc_object_attach( p_vlm->p_vod, p_vlm->p_libvlc );
+                                          "vod server" );
         p_vlm->p_vod->p_module = module_need( p_vlm->p_vod, "vod server", "$vod-server", false );
         if( !p_vlm->p_vod->p_module )
         {
@@ -757,7 +774,7 @@ static int vlm_ControlMediaAdd( vlm_t *p_vlm, vlm_media_t *p_cfg, int64_t *p_id
     p_media->cfg.id = p_vlm->i_id++;
     /* FIXME do we do something here if enabled is true ? */
 
-    p_media->vod.p_item = input_item_New( p_vlm, NULL, NULL );
+    p_media->vod.p_item = input_item_New( NULL, NULL );
 
     p_media->vod.p_media = NULL;
     TAB_INIT( p_media->i_instance, p_media->instance );
@@ -872,14 +889,13 @@ static vlm_media_instance_sys_t *vlm_MediaInstanceNew( vlm_t *p_vlm, const char
     if( psz_name )
         p_instance->psz_name = strdup( psz_name );
 
-    p_instance->p_item = input_item_New( p_vlm, NULL, NULL );
+    p_instance->p_item = input_item_New( NULL, NULL );
 
     p_instance->i_index = 0;
     p_instance->b_sout_keep = false;
     p_instance->p_parent = vlc_object_create( p_vlm, sizeof (vlc_object_t) );
-    vlc_object_attach( p_instance->p_parent, p_vlm->p_libvlc );
     p_instance->p_input = NULL;
-    p_instance->p_input_resource = NULL;
+    p_instance->p_input_resource = input_resource_New( p_instance->p_parent );
 
     return p_instance;
 }
@@ -888,19 +904,15 @@ static void vlm_MediaInstanceDelete( vlm_t *p_vlm, int64_t id, vlm_media_instanc
     input_thread_t *p_input = p_instance->p_input;
     if( p_input )
     {
-        input_Stop( p_input, true );
-        vlc_thread_join( p_input );
-
+        input_Stop( p_input );
+        input_Join( p_input );
         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
-        vlc_object_release( p_input );
+        input_Release( p_input );
 
         vlm_SendEventMediaInstanceStopped( p_vlm, id, p_media->cfg.psz_name );
     }
-    if( p_instance->p_input_resource )
-    {
-        input_resource_Terminate( p_instance->p_input_resource );
-        input_resource_Release( p_instance->p_input_resource );
-    }
+    input_resource_Terminate( p_instance->p_input_resource );
+    input_resource_Release( p_instance->p_input_resource );
     vlc_object_release( p_instance->p_parent );
 
     TAB_REMOVE( p_media->i_instance, p_media->instance, p_instance );
@@ -984,11 +996,10 @@ static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *
         }
 
 
-        input_Stop( p_input, true );
-        vlc_thread_join( p_input );
-
+        input_Stop( p_input );
+        input_Join( p_input );
         var_DelCallback( p_instance->p_input, "intf-event", InputEvent, p_media );
-        vlc_object_release( p_input );
+        input_Release( p_input );
 
         if( !p_instance->b_sout_keep )
             input_resource_TerminateSout( p_instance->p_input_resource );
@@ -999,16 +1010,18 @@ static int vlm_ControlMediaInstanceStart( vlm_t *p_vlm, int64_t id, const char *
 
     /* Start new one */
     p_instance->i_index = i_input_index;
-    char *psz_uri = make_URI( p_media->cfg.ppsz_input[p_instance->i_index],
-                              NULL );
-    input_item_SetURI( p_instance->p_item, psz_uri ) ;
-    free( psz_uri );
+    if( strstr( p_media->cfg.ppsz_input[p_instance->i_index], "://" ) == NULL )
+    {
+        char *psz_uri = vlc_path2uri(
+                          p_media->cfg.ppsz_input[p_instance->i_index], NULL );
+        input_item_SetURI( p_instance->p_item, psz_uri ) ;
+        free( psz_uri );
+    }
+    else
+        input_item_SetURI( p_instance->p_item, p_media->cfg.ppsz_input[p_instance->i_index] ) ;
 
     if( asprintf( &psz_log, _("Media: %s"), p_media->cfg.psz_name ) != -1 )
     {
-        if( !p_instance->p_input_resource )
-            p_instance->p_input_resource = input_resource_New( p_instance->p_parent );
-
         p_instance->p_input = input_Create( p_instance->p_parent,
                                             p_instance->p_item, psz_log,
                                             p_instance->p_input_resource );