]> git.sesse.net Git - vlc/blobdiff - src/input/control.c
Fix a memory leak.
[vlc] / src / input / control.c
index 47d5335679783395edcf8f2ecf8a40ce03af10f9..061d8ccf88a1998543cee2f85ec77c223d75949e 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "input_internal.h"
-#include "vlc_playlist.h"
 
 
 static void UpdateBookmarksOption( input_thread_t * );
-static void NotifyPlaylist( input_thread_t * );
 
 /****************************************************************************
  * input_Control
@@ -152,7 +154,7 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
                 p_cat = malloc( sizeof( info_category_t ) );
                 if( !p_cat )
                 {
-                    vlc_mutex_lock( &p_input->p->input.p_item->lock );
+                    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
                     return VLC_EGENERIC;
                 }
                 p_cat->psz_name = strdup( psz_cat );
@@ -180,7 +182,7 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
                 p_info = malloc( sizeof( info_t ) );
                 if( !p_info )
                 {
-                    vlc_mutex_lock( &p_input->p->input.p_item->lock );
+                    vlc_mutex_unlock( &p_input->p->input.p_item->lock );
                     return VLC_EGENERIC;
                 }
 
@@ -190,12 +192,17 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
             }
 
             p_info = p_cat->pp_infos[i];
-            vasprintf( &p_info->psz_value, psz_format, args );
+            if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
+                p_info->psz_value = NULL;
 
             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
 
             if( !p_input->b_preparsing )
-                NotifyPlaylist( p_input );
+            {
+                vlc_event_t event;
+                event.type = vlc_InputItemInfoChanged;
+                vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
+            }
         }
         return VLC_SUCCESS;
 
@@ -262,8 +269,11 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
 
             if( !p_input->b_preparsing )
-                NotifyPlaylist( p_input );
-
+            {
+                vlc_event_t event;
+                event.type = vlc_InputItemInfoChanged;
+                vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
+            }
             return VLC_SUCCESS;
         }
 
@@ -294,8 +304,12 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
 
             if( !p_input->b_preparsing )
-                NotifyPlaylist( p_input );
-
+            {
+                vlc_event_t event;
+                event.type = vlc_InputItemNameChanged;
+                event.u.input_item_name_changed.new_name = psz_name;
+                vlc_event_send( &p_input->p->input.p_item->event_manager, &event );
+            }
             return VLC_SUCCESS;
         }
 
@@ -306,8 +320,9 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
             vlc_mutex_lock( &p_input->p->input.p_item->lock );
             if( !p_bkmk->psz_name )
             {
-                 asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
-                           p_input->p->i_bookmark );
+                 if( asprintf( &p_bkmk->psz_name, _("Bookmark %i"),
+                               p_input->p->i_bookmark ) == -1 )
+                     p_bkmk->psz_name = NULL;
             }
 
             TAB_APPEND( p_input->p->i_bookmark, p_input->p->bookmark, p_bkmk );
@@ -492,37 +507,18 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
 
         case INPUT_ADD_OPTION:
         {
-            char *psz_option = (char *)va_arg( args, char * );
-            char *psz_value = (char *)va_arg( args, char * );
+            const char *psz_option = va_arg( args, const char * );
+            const char *psz_value = va_arg( args, const char * );
+            char *str;
             int i;
 
-            vlc_mutex_lock( &p_input->p->input.p_item->lock );
-            /* Check if option already exists */
-            for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
-            {
-                if( !strncmp( p_input->p->input.p_item->ppsz_options[i],
-                              psz_option, strlen( psz_option ) ) &&
-                    p_input->p->input.p_item->ppsz_options[i][strlen(psz_option)]
-                      == '=' )
-                {
-                    free( p_input->p->input.p_item->ppsz_options[i] );
-                    break;
-                }
-            }
-            if( i == p_input->p->input.p_item->i_options )
-            {
-                p_input->p->input.p_item->i_options++;
-                p_input->p->input.p_item->ppsz_options =
-                    realloc( p_input->p->input.p_item->ppsz_options,
-                             p_input->p->input.p_item->i_options *
-                             sizeof(char **) );
-            }
+            if( asprintf( &str, "%s=%s", psz_option, psz_value ) == -1 )
+                return VLC_ENOMEM;
 
-            asprintf( &p_input->p->input.p_item->ppsz_options[i],
-                      "%s=%s", psz_option, psz_value ) ;
-            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
-
-            return VLC_SUCCESS;
+            i = input_ItemAddOpt( p_input->p->input.p_item, str,
+                                  VLC_INPUT_OPTION_UNIQUE );
+            free( str );
+            return i;
         }
 
         case INPUT_GET_BYTE_POSITION:
@@ -537,6 +533,18 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
                 stream_Size( p_input->p->input.p_stream );
             return VLC_SUCCESS;
 
+        case INPUT_GET_VIDEO_FPS:
+        {
+            int i;
+            pf = (double*)va_arg( args, double * );
+            vlc_mutex_lock( &p_input->p->input.p_item->lock );
+            *pf = p_input->p->input.f_fps;
+            for( i = 0; i < p_input->p->i_slave && *pf <= 0.001; i++ )
+                *pf = p_input->p->slave[i]->f_fps;
+            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
+            return VLC_SUCCESS;
+        }
+
         case INPUT_ADD_SLAVE:
             psz = (char*)va_arg( args, char * );
             if( psz && *psz )
@@ -597,46 +605,39 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
     }
 }
 
-static void NotifyPlaylist( input_thread_t *p_input )
-{
-    playlist_t *p_playlist = pl_Yield( p_input );
-    var_SetInteger( p_playlist, "item-change",
-                    p_input->p->input.p_item->i_id );
-    pl_Release( p_input );
-}
-
 static void UpdateBookmarksOption( input_thread_t *p_input )
 {
     int i, i_len = 0;
     char *psz_value = NULL, *psz_next = NULL;
 
     vlc_mutex_lock( &p_input->p->input.p_item->lock );
-    for( i = 0; i < p_input->p->i_bookmark; i++ )
-    {
-        asprintf( &psz_value, "{name=%s,bytes="I64Fd",time="I64Fd"}",
-                  p_input->p->bookmark[i]->psz_name,
-                  p_input->p->bookmark[i]->i_byte_offset,
-                  p_input->p->bookmark[i]->i_time_offset/1000000 );
-        i_len += strlen( psz_value );
-        free( psz_value );
-    }
-    for( i = 0; i < p_input->p->i_bookmark; i++ )
+    if( p_input->p->i_bookmark > 0 )
     {
-        if( !i ) psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
-
-        sprintf( psz_next, "{name=%s,bytes="I64Fd",time="I64Fd"}",
-                 p_input->p->bookmark[i]->psz_name,
-                 p_input->p->bookmark[i]->i_byte_offset,
-                 p_input->p->bookmark[i]->i_time_offset/1000000 );
+        for( i = 0; i < p_input->p->i_bookmark; i++ )
+        {
+            i_len += snprintf( NULL, 0, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
+                               p_input->p->bookmark[i]->psz_name,
+                               p_input->p->bookmark[i]->i_byte_offset,
+                               p_input->p->bookmark[i]->i_time_offset/1000000 );
+        }
+        psz_value = psz_next = malloc( i_len + p_input->p->i_bookmark );
 
-        psz_next += strlen( psz_next );
-        if( i < p_input->p->i_bookmark - 1)
+        for( i = 0; i < p_input->p->i_bookmark; i++ )
         {
-            *psz_next = ','; psz_next++;
+            sprintf( psz_next, "{name=%s,bytes=%"PRId64",time=%"PRId64"}",
+                     p_input->p->bookmark[i]->psz_name,
+                     p_input->p->bookmark[i]->i_byte_offset,
+                     p_input->p->bookmark[i]->i_time_offset/1000000 );
+
+            psz_next += strlen( psz_next );
+            if( i < p_input->p->i_bookmark - 1)
+                *psz_next = ','; psz_next++;
         }
     }
     vlc_mutex_unlock( &p_input->p->input.p_item->lock );
 
     input_Control( p_input, INPUT_ADD_OPTION, "bookmarks",
                    psz_value ? psz_value : "" );
+    free( psz_value );
 }
+