[vlc-commits] [Git][videolan/vlc][master] 5 commits: lib: media_discoverer: remove useless variable initialisation

Thomas Guillem (@tguillem) gitlab at videolan.org
Fri Dec 22 06:03:37 UTC 2023



Thomas Guillem pushed to branch master at VideoLAN / VLC


Commits:
468fe700 by Thomas Guillem at 2023-12-22T05:47:54+00:00
lib: media_discoverer: remove useless variable initialisation

libvlc_media_subitems() already returns a read-only media list.

- - - - -
07b01add by Thomas Guillem at 2023-12-22T05:47:54+00:00
lib: media: always initialize subitems

Avoid the NULL check protected by a mutex.

- - - - -
8f6d3910 by Thomas Guillem at 2023-12-22T05:47:54+00:00
lib: media_list: fix leak

- - - - -
43383a58 by Thomas Guillem at 2023-12-22T05:47:54+00:00
lib: media_list_player: check set_current_playing_item() return

This allows to fix a leak by only checking the return code instead of
fetching the media (and forgetting to release it...)

- - - - -
b905473e by Thomas Guillem at 2023-12-22T05:47:54+00:00
lib: media_list_player: fix leaks

- - - - -


5 changed files:

- lib/media.c
- lib/media_discoverer.c
- lib/media_internal.h
- lib/media_list_path.h
- lib/media_list_player.c


Changes:

=====================================
lib/media.c
=====================================
@@ -135,26 +135,6 @@ static_assert(
     MULTIVIEW_STEREO_CHECKERBOARD   == (int) libvlc_video_multiview_stereo_checkerboard,
     "Mismatch between libvlc_video_multiview_t and video_multiview_mode_t");
 
-static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
-                                                bool b_create )
-{
-    libvlc_media_list_t *p_subitems = NULL;
-
-    vlc_mutex_lock( &p_md->subitems_lock );
-    if( p_md->p_subitems == NULL && b_create )
-    {
-        p_md->p_subitems = libvlc_media_list_new();
-        if( p_md->p_subitems != NULL )
-        {
-            p_md->p_subitems->b_read_only = true;
-            p_md->p_subitems->p_internal_md = p_md;
-        }
-    }
-    p_subitems = p_md->p_subitems;
-    vlc_mutex_unlock( &p_md->subitems_lock );
-    return p_subitems;
-}
-
 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
                                                input_item_t *item )
 {
@@ -165,13 +145,10 @@ static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
     p_md_child = libvlc_media_new_from_input_item( item );
 
     /* Add this to our media list */
-    p_subitems = media_get_subitems( p_md, true );
-    if( p_subitems != NULL )
-    {
-        libvlc_media_list_lock( p_subitems );
-        libvlc_media_list_internal_add_media( p_subitems, p_md_child );
-        libvlc_media_list_unlock( p_subitems );
-    }
+    p_subitems = p_md->p_subitems;
+    libvlc_media_list_lock( p_subitems );
+    libvlc_media_list_internal_add_media( p_subitems, p_md_child );
+    libvlc_media_list_unlock( p_subitems );
 
     /* Construct the event */
     event.type = libvlc_MediaSubItemAdded;
@@ -375,14 +352,11 @@ static void send_parsed_changed( libvlc_media_t *p_md,
     /* Send the event */
     libvlc_event_send( &p_md->event_manager, &event );
 
-    libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
-    if( p_subitems != NULL )
-    {
-        /* notify the media list */
-        libvlc_media_list_lock( p_subitems );
-        libvlc_media_list_internal_end_reached( p_subitems );
-        libvlc_media_list_unlock( p_subitems );
-    }
+    libvlc_media_list_t *p_subitems = p_md->p_subitems;
+    /* notify the media list */
+    libvlc_media_list_lock( p_subitems );
+    libvlc_media_list_internal_end_reached( p_subitems );
+    libvlc_media_list_unlock( p_subitems );
 }
 
 /**
@@ -484,15 +458,20 @@ libvlc_media_t * libvlc_media_new_from_input_item(input_item_t *p_input_item )
         return NULL;
     }
 
+    p_md->p_subitems = libvlc_media_list_new();
+    if( p_md->p_subitems == NULL )
+    {
+        free( p_md );
+        return NULL;
+    }
+    p_md->p_subitems->b_read_only = true;
+    p_md->p_subitems->p_internal_md = p_md;
+
     p_md->p_input_item      = p_input_item;
     vlc_atomic_rc_init(&p_md->rc);
 
-    vlc_mutex_init(&p_md->subitems_lock);
     atomic_init(&p_md->worker_count, 0);
 
-    /* A media descriptor can be a playlist. When you open a playlist
-     * It can give a bunch of item to read. */
-    p_md->p_subitems        = NULL;
     p_md->p_input_item->libvlc_owner = p_md;
     atomic_init(&p_md->parsed_status, libvlc_media_parsed_status_none);
 
@@ -575,7 +554,6 @@ libvlc_media_t * libvlc_media_new_as_node(const char *psz_name)
 {
     input_item_t * p_input_item;
     libvlc_media_t * p_md;
-    libvlc_media_list_t * p_subitems;
 
     p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
 
@@ -588,12 +566,6 @@ libvlc_media_t * libvlc_media_new_as_node(const char *psz_name)
     p_md = libvlc_media_new_from_input_item( p_input_item );
     input_item_Release( p_input_item );
 
-    p_subitems = media_get_subitems( p_md, true );
-    if( p_subitems == NULL) {
-        libvlc_media_release( p_md );
-        return NULL;
-    }
-
     return p_md;
 }
 
@@ -740,10 +712,8 @@ int libvlc_media_save_meta( libvlc_instance_t *inst, libvlc_media_t *p_md )
 libvlc_media_list_t *
 libvlc_media_subitems( libvlc_media_t * p_md )
 {
-    libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
-    if( p_subitems )
-        libvlc_media_list_retain( p_subitems );
-    return p_subitems;
+    libvlc_media_list_retain( p_md->p_subitems );
+    return p_md->p_subitems;
 }
 
 // Getter for statistics information


=====================================
lib/media_discoverer.c
=====================================
@@ -83,7 +83,6 @@ static void services_discovery_item_added( services_discovery_t *sd,
             libvlc_media_t * p_catmd;
             p_catmd = libvlc_media_new_as_node( psz_cat );
             p_mlist = libvlc_media_subitems( p_catmd );
-            p_mlist->b_read_only = true;
 
             /* Insert the newly created mlist in our dictionary */
             vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );


=====================================
lib/media_internal.h
=====================================
@@ -42,8 +42,6 @@ struct libvlc_media_t
     VLC_FORWARD_DECLARE_OBJECT(libvlc_media_list_t*) p_subitems; /* A media descriptor can have Sub items. This is the only dependency we really have on media_list */
     void *p_user_data;
 
-    vlc_mutex_t subitems_lock;
-
     /* Idle protection to prevent the media from being released during
      * preparsing. The preparse will be cancelled but the release will
      * be blocking until no async code is using the media anymore. */


=====================================
lib/media_list_path.h
=====================================
@@ -123,7 +123,10 @@ get_path_rec( const libvlc_media_list_path_t path, libvlc_media_list_t * p_curre
         libvlc_media_t * p_md = libvlc_media_list_item_at_index( p_current_mlist, i );
 
         if( p_md == p_searched_md )
+        {
+            libvlc_media_release( p_md );
             return libvlc_media_list_path_copy_by_appending( path, i ); /* Found! */
+        }
 
         libvlc_media_list_t * p_subitems = libvlc_media_subitems( p_md );
         libvlc_media_release( p_md );


=====================================
lib/media_list_player.c
=====================================
@@ -169,7 +169,11 @@ get_next_path(libvlc_media_list_player_t * p_mlp, bool b_loop)
 
     int depth = libvlc_media_list_path_depth(p_mlp->current_playing_item_path);
     if (depth < 1 || !p_parent_of_playing_item)
+    {
+        if (p_parent_of_playing_item)
+            libvlc_media_list_release(p_parent_of_playing_item);
         return NULL;
+    }
 
     ret = libvlc_media_list_path_copy(p_mlp->current_playing_item_path);
     ret[depth - 1]++; /* set to next element */
@@ -195,6 +199,7 @@ get_next_path(libvlc_media_list_player_t * p_mlp, bool b_loop)
         }
         ret[depth] = -1;
         ret[depth-1]++;
+        libvlc_media_list_release(p_parent_of_playing_item);
         p_parent_of_playing_item  = libvlc_media_list_parentlist_at_path(
                                         p_mlp->p_mlist,
                                         ret);
@@ -431,7 +436,7 @@ uninstall_media_player_observer(libvlc_media_list_player_t * p_mlp)
  *
  * Playlist lock should be held
  **************************************************************************/
-static void
+static int
 set_current_playing_item(libvlc_media_list_player_t * p_mlp, libvlc_media_list_path_t path)
 {
     assert_locked(p_mlp);
@@ -444,12 +449,12 @@ set_current_playing_item(libvlc_media_list_player_t * p_mlp, libvlc_media_list_p
     }
 
     if (!path)
-        return;
+        return -1;
 
     libvlc_media_t * p_md;
     p_md = libvlc_media_list_item_at_path(p_mlp->p_mlist, path);
     if (!p_md)
-        return;
+        return -1;
 
     /* Make sure media_player_reached_end() won't get called */
     uninstall_media_player_observer(p_mlp);
@@ -458,6 +463,8 @@ set_current_playing_item(libvlc_media_list_player_t * p_mlp, libvlc_media_list_p
 
     install_media_player_observer(p_mlp);
     libvlc_media_release(p_md); /* for libvlc_media_list_item_at_index */
+
+    return 0;
 }
 
 /*
@@ -670,21 +677,21 @@ int libvlc_media_list_player_play_item_at_index(libvlc_media_list_player_t * p_m
 {
     lock(p_mlp);
     libvlc_media_list_path_t path = libvlc_media_list_path_with_root_index(i_index);
-    set_current_playing_item(p_mlp, path);
-    libvlc_media_t *p_md = libvlc_media_player_get_media(p_mlp->p_mi);
+    int ret = set_current_playing_item(p_mlp, path);
     libvlc_media_player_play(p_mlp->p_mi);
     unlock(p_mlp);
 
-    if (!p_md)
+    if (ret != 0)
         return -1;
 
     /* Send the next item event */
+    libvlc_media_t *p_md = libvlc_media_player_get_media(p_mlp->p_mi);
     libvlc_event_t event;
     event.type = libvlc_MediaListPlayerNextItemSet;
     event.u.media_list_player_next_item_set.item = p_md;
     libvlc_event_send(&p_mlp->event_manager, &event);
     libvlc_media_release(p_md);
-    return 0;
+    return ret;
 }
 
 /**************************************************************************
@@ -701,10 +708,10 @@ int libvlc_media_list_player_play_item(libvlc_media_list_player_t * p_mlp, libvl
         return -1;
     }
 
-    set_current_playing_item(p_mlp, path);
+    int ret = set_current_playing_item(p_mlp, path);
     libvlc_media_player_play(p_mlp->p_mi);
     unlock(p_mlp);
-    return 0;
+    return ret;
 }
 
 /**************************************************************************
@@ -763,27 +770,28 @@ static int set_relative_playlist_position_and_play(
 
     libvlc_media_list_path_t path = p_mlp->current_playing_item_path;
 
+    int ret = 0;
     if(p_mlp->e_playback_mode != libvlc_playback_mode_repeat)
     {
         bool b_loop = (p_mlp->e_playback_mode == libvlc_playback_mode_loop);
 
-        while (i_relative_position > 0)
+        while (ret == 0 && i_relative_position > 0)
         {
             path = get_next_path(p_mlp, b_loop);
-            set_current_playing_item(p_mlp, path);
+            ret = set_current_playing_item(p_mlp, path);
             --i_relative_position;
         }
 
-        while (i_relative_position < 0)
+        while (ret == 0 && i_relative_position < 0)
         {
             path = get_previous_path(p_mlp, b_loop);
-            set_current_playing_item(p_mlp, path);
+            ret = set_current_playing_item(p_mlp, path);
             ++i_relative_position;
         }
     }
     else
     {
-        set_current_playing_item(p_mlp, path);
+        ret = set_current_playing_item(p_mlp, path);
     }
 
 #ifdef DEBUG_MEDIA_LIST_PLAYER
@@ -791,7 +799,7 @@ static int set_relative_playlist_position_and_play(
     libvlc_media_list_path_dump(path);
 #endif
 
-    if (!path)
+    if (!path || ret != 0)
     {
         libvlc_media_list_unlock(p_mlp->p_mlist);
         /* Send list played event */



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/745c1bf5c50154e59aad18d9731a3e1756bbd6a0...b905473ea05e2d4341bbf14b9e884c8ab003e723

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/745c1bf5c50154e59aad18d9731a3e1756bbd6a0...b905473ea05e2d4341bbf14b9e884c8ab003e723
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list