]> git.sesse.net Git - vlc/blobdiff - src/misc/vlm.c
Don't add MPEG-TS program data for programs that don't exist. Patch by Dnumgis. This...
[vlc] / src / misc / vlm.c
index 4f325319b69306608b4769c9f60429497174ad60..6a517d53c1ce918e1a01cda506d8d3032b978fe8 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+#include <vlc/vlc.h>
+
+#include <stdio.h>
 #include <stdlib.h>                                      /* malloc(), free() */
 #include <ctype.h>                                              /* tolower() */
 
-#include <vlc/vlc.h>
-
 #ifdef ENABLE_VLM
 
 #include <vlc/intf.h>
@@ -45,9 +46,6 @@
 #include <vlc_vod.h>
 #include <charset.h>
 
-#define FREE( p ) \
-        if( p ) { free( p ); (p) = NULL; }
-
 /*****************************************************************************
  * Local prototypes.
  *****************************************************************************/
@@ -76,20 +74,20 @@ vlm_t *__vlm_New ( vlc_object_t *p_this )
     char *psz_vlmconf;
 
     /* to be sure to avoid multiple creation */
-    var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
-    var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
+    var_Create( p_this->p_libvlc_global, "vlm_mutex", VLC_VAR_MUTEX );
+    var_Get( p_this->p_libvlc_global, "vlm_mutex", &lockval );
     vlc_mutex_lock( lockval.p_address );
 
     if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
     {
-        msg_Info( p_this, "creating vlm" );
+        msg_Info( p_this, "creating VLM" );
         if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
         {
             vlc_mutex_unlock( lockval.p_address );
             return NULL;
         }
 
-        vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
+        vlc_mutex_init( p_this->p_libvlc, &p_vlm->lock );
         p_vlm->i_media      = 0;
         p_vlm->media        = NULL;
         p_vlm->i_vod        = 0;
@@ -97,7 +95,7 @@ vlm_t *__vlm_New ( vlc_object_t *p_this )
         p_vlm->schedule     = NULL;
 
         vlc_object_yield( p_vlm );
-        vlc_object_attach( p_vlm, p_this->p_vlc );
+        vlc_object_attach( p_vlm, p_this->p_libvlc );
     }
     vlc_mutex_unlock( lockval.p_address );
 
@@ -117,13 +115,13 @@ vlm_t *__vlm_New ( vlc_object_t *p_this )
         vlm_message_t *p_message = NULL;
         char *psz_buffer = NULL;
 
-        msg_Dbg( p_this, "loading vlm conf ..." );
+        msg_Dbg( p_this, "loading VLM configuration" );
         asprintf(&psz_buffer, "load %s", psz_vlmconf );
         if( psz_buffer )
         {
             msg_Dbg( p_this, psz_buffer);
             if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
-                msg_Warn( p_this, "error while loading the vlm conf file" );
+                msg_Warn( p_this, "error while loading the configuration file" );
             }
             vlm_MessageDelete(p_message);
             free(psz_buffer);
@@ -141,7 +139,7 @@ void vlm_Delete( vlm_t *p_vlm )
 {
     vlc_value_t lockval;
 
-    var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
+    var_Get( p_vlm->p_libvlc_global, "vlm_mutex", &lockval );
     vlc_mutex_lock( lockval.p_address );
 
     vlc_object_release( p_vlm );
@@ -158,11 +156,11 @@ void vlm_Delete( vlm_t *p_vlm )
     vlc_mutex_destroy( &p_vlm->lock );
 
     while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
-    FREE( p_vlm->media );
+    FREENULL( p_vlm->media );
 
     while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
                                                    p_vlm->schedule[0], NULL );
-    FREE( p_vlm->schedule );
+    FREENULL( p_vlm->schedule );
 
     vlc_object_detach( p_vlm );
     vlc_object_destroy( p_vlm );
@@ -260,19 +258,24 @@ int vlm_Load( vlm_t *p_vlm, const char *psz_file )
  *****************************************************************************/
 static const char *FindEndCommand( const char *psz_sent )
 {
+    vlc_bool_t b_escape = VLC_FALSE;
+
     switch( *psz_sent )
     {
     case '\"':
         psz_sent++;
-
-        while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
+        while( ( *psz_sent != '\"' || b_escape == VLC_TRUE )
+               && ( *psz_sent != '\0' ) )
         {
-            if( *psz_sent == '\'' )
+            if( *psz_sent == '\'' && b_escape == VLC_FALSE )
             {
                 psz_sent = FindEndCommand( psz_sent );
                 if( psz_sent == NULL ) return NULL;
             }
-            else psz_sent++;
+            else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
+                b_escape = VLC_TRUE;
+            else
+                b_escape = VLC_FALSE;
         }
 
         if( *psz_sent == '\"' )
@@ -288,15 +291,18 @@ static const char *FindEndCommand( const char *psz_sent )
 
     case '\'':
         psz_sent++;
-
-        while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
+        while( ( *psz_sent != '\'' || b_escape == VLC_TRUE )
+                 && ( *psz_sent != '\0' ) )
         {
-            if( *psz_sent == '\"' )
+            if( *psz_sent == '\"' && b_escape == VLC_FALSE )
             {
                 psz_sent = FindEndCommand( psz_sent );
                 if( psz_sent == NULL ) return NULL;
             }
-            else psz_sent++;
+            else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
+                b_escape = VLC_TRUE;
+            else
+                b_escape = VLC_FALSE;
         }
 
         if( *psz_sent == '\'' )
@@ -313,12 +319,17 @@ static const char *FindEndCommand( const char *psz_sent )
     default: /* now we can look for spaces */
         while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
         {
-            if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
+            if( ( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
+                && b_escape == VLC_FALSE )
             {
                 psz_sent = FindEndCommand( psz_sent );
                 if( psz_sent == NULL ) return NULL;
             }
-            else psz_sent++;
+            else if( *psz_sent++ == '\\' && b_escape == VLC_FALSE )
+                b_escape = VLC_TRUE;
+            else
+                b_escape = VLC_FALSE;
+
         }
 
         return psz_sent;
@@ -350,6 +361,9 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
         else
         {
             const char *psz_temp;
+            const char *psz_buf;
+            char *psz_dst;
+            vlc_bool_t b_escape = VLC_FALSE;
             int   i_temp;
 
             /* support for comments */
@@ -372,8 +386,23 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
             ppsz_command = realloc( ppsz_command, (i_command + 1) *
                                     sizeof(char*) );
             ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
-            strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
-            ppsz_command[ i_command ][ i_temp ] = '\0';
+
+            /* unescape ", ' and \ ... and everything else */
+            psz_buf = psz_cmd;
+            psz_dst = ppsz_command[ i_command ];
+            while( i_temp-- )
+            {
+                if( *psz_buf == '\\' && b_escape == VLC_FALSE )
+                    b_escape = VLC_TRUE;
+                else
+                {
+                    b_escape = VLC_FALSE;
+                    *psz_dst = *psz_buf;
+                    psz_dst++;
+                }
+                psz_buf++;
+            }
+            *psz_dst = '\0';
 
             i_command++;
 
@@ -641,11 +670,11 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
                 p_message = vlm_MessageNew( "load", NULL );
                 goto success;
             case 2:
-                p_message = vlm_MessageNew( "load", "read file error" );
+                p_message = vlm_MessageNew( "load", "Read file error" );
                 goto error;
             case 3:
                 p_message =
-                    vlm_MessageNew( "load", "error while loading file" );
+                    vlm_MessageNew( "load", "Error while loading file" );
                 goto error;
             default:
                 p_message =
@@ -802,8 +831,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
     }
 
 success:
-    for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
-    FREE( ppsz_command );
+    for( i = 0 ; i < i_command ; i++ ) FREENULL( ppsz_command[i] );
+    FREENULL( ppsz_command );
     *pp_message = p_message;
 
     return VLC_SUCCESS;
@@ -812,8 +841,8 @@ syntax_error:
     p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
 
 error:
-    for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
-    FREE( ppsz_command );
+    for( i = 0 ; i < i_command ; i++ ) FREENULL( ppsz_command[i] );
+    FREENULL( ppsz_command );
     *pp_message = p_message;
 
     return VLC_EGENERIC;
@@ -903,7 +932,7 @@ vlm_media_t *vlm_MediaNew( vlm_t *vlm, const char *psz_name, int i_type )
     media->i_instance = 0;
     media->instance = NULL;
 
-    vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
+    input_ItemInit( VLC_OBJECT(vlm), &media->item );
 
     TAB_APPEND( vlm->i_media, vlm->media, media );
 
@@ -951,7 +980,7 @@ void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, const char *psz_name )
     while( media->i_option-- ) free( media->option[media->i_option] );
     if( media->option ) free( media->option );
 
-    vlc_input_item_Clean( &media->item );
+    input_ItemClean( &media->item );
 
     free( media );
 }
@@ -1084,8 +1113,8 @@ int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, const char *psz_cmd,
             char *psz_header;
             int i;
 
-            vlc_input_item_Clean( &media->item );
-            vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
+            input_ItemClean( &media->item );
+            input_ItemInit( VLC_OBJECT(vlm), &media->item );
 
             if( media->psz_output )
                 asprintf( &psz_output, "%s:description", media->psz_output );
@@ -1168,7 +1197,7 @@ int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
             p_instance = malloc( sizeof(vlm_media_instance_t) );
             if( !p_instance ) return VLC_EGENERIC;
             memset( p_instance, 0, sizeof(vlm_media_instance_t) );
-            vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
+            input_ItemInit( VLC_OBJECT(vlm), &p_instance->item );
             p_instance->p_input = NULL;
 
             if( ( media->psz_output != NULL ) || ( media->psz_vod_output != NULL ) )
@@ -1219,7 +1248,7 @@ int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
         if( !p_instance->p_input )
         {
             TAB_REMOVE( media->i_instance, media->instance, p_instance );
-            vlc_input_item_Clean( &p_instance->item );
+            input_ItemClean( &p_instance->item );
             if( p_instance->psz_name ) free( p_instance->psz_name );
         }
         free( psz_header );
@@ -1245,6 +1274,40 @@ int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
             }
         }
     }
+    else if( !strcmp( psz_command, "rewind" ) )
+    {
+        float f_pos;
+        float f_scale;
+        vlc_value_t val;
+
+        if( psz_args )
+        {
+            f_scale = i18n_atof( psz_args );
+            f_pos = var_GetFloat( p_instance->p_input, "position" );
+            val.f_float = f_pos - (f_scale / 1000.0);
+            if( val.f_float < 0.0 )
+                val.f_float = 0.0;
+            var_Set( p_instance->p_input, "position", val );
+            return VLC_SUCCESS;
+        }
+    }
+    else if( !strcmp( psz_command, "forward" ) )
+    {
+        float f_pos;
+        float f_scale;
+        vlc_value_t val;
+
+        if( psz_args )
+        {
+            f_scale = i18n_atof( psz_args );
+            f_pos = var_GetFloat( p_instance->p_input, "position" );
+            val.f_float = f_pos + (f_scale / 1000.0);
+            if( val.f_float > 1.0 )
+                val.f_float = 1.0;
+            var_Set( p_instance->p_input, "position", val );
+            return VLC_SUCCESS;
+        }
+    }
     else if( !strcmp( psz_command, "stop" ) )
     {
         TAB_REMOVE( media->i_instance, media->instance, p_instance );
@@ -1257,7 +1320,7 @@ int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, const char *psz_id,
             vlc_object_destroy( p_instance->p_input );
         }
 
-        vlc_input_item_Clean( &p_instance->item );
+        input_ItemClean( &p_instance->item );
         if( p_instance->psz_name ) free( p_instance->psz_name );
         free( p_instance );
 
@@ -2322,6 +2385,26 @@ int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
         break;
     }
 
+    case VOD_MEDIA_REWIND:
+    {
+        double f_scale = (double)va_arg( args, double );
+        char psz_scale[50];
+        lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
+        sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
+        i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "rewind", psz_scale );
+        break;
+    }
+
+    case VOD_MEDIA_FORWARD:
+    {
+        double f_scale = (double)va_arg( args, double );
+        char psz_scale[50];
+        lldiv_t div = lldiv( f_scale * 10000000, 10000000 );
+        sprintf( psz_scale, I64Fd".%07u", div.quot, (unsigned int) div.rem );
+        i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "forward", psz_scale );
+        break;
+    }
+
     default:
         break;
     }
@@ -2331,6 +2414,7 @@ int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
     return i_ret;
 }
 
+
 /*****************************************************************************
  * Manage:
  *****************************************************************************/
@@ -2384,7 +2468,10 @@ static int Manage( vlc_object_t* p_object )
                 else
                 {
                     if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
-                                          "stop", 0 ) == VLC_SUCCESS ) i--;
+                                          "stop", 0 ) == VLC_SUCCESS )
+                    {
+                        j--; /* the aray is one element smaller now. */
+                   }
                 }
             }
         }