[vlc-devel] [PATCH] core: lazy initialization of the new (main) playlist

Thomas Guillem thomas at gllm.fr
Wed Feb 20 18:02:19 CET 2019


This avoid to always create a player, vout, and an aout.
---
 src/interface/interface.c | 60 ++++++++++++++++++++++++++++++++++++++-
 src/libvlc.c              | 51 ---------------------------------
 2 files changed, 59 insertions(+), 52 deletions(-)

diff --git a/src/interface/interface.c b/src/interface/interface.c
index d86e1a1c20..663f8734be 100644
--- a/src/interface/interface.c
+++ b/src/interface/interface.c
@@ -42,9 +42,11 @@
 #include <vlc_modules.h>
 #include <vlc_interface.h>
 #include <vlc_playlist_legacy.h>
+#include <vlc_playlist.h>
 #include "libvlc.h"
 #include "playlist_legacy/playlist_internal.h"
 #include "../lib/libvlc_internal.h"
+#include "input/player.h"
 
 static int AddIntfCallback( vlc_object_t *, char const *,
                             vlc_value_t , vlc_value_t , void * );
@@ -145,10 +147,66 @@ static playlist_t *intf_GetPlaylist(libvlc_int_t *libvlc)
     return playlist;
 }
 
+static void
+PlaylistConfigureFromVariables(vlc_playlist_t *playlist, vlc_object_t *obj)
+{
+    enum vlc_playlist_playback_order order;
+    if (var_InheritBool(obj, "random"))
+        order = VLC_PLAYLIST_PLAYBACK_ORDER_RANDOM;
+    else
+        order = VLC_PLAYLIST_PLAYBACK_ORDER_NORMAL;
+
+    /* repeat = repeat current; loop = repeat all */
+    enum vlc_playlist_playback_repeat repeat;
+    if (var_InheritBool(obj, "repeat"))
+        repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_CURRENT;
+    else if (var_InheritBool(obj, "loop"))
+        repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_ALL;
+    else
+        repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_NONE;
+
+    enum vlc_player_media_stopped_action media_stopped_action;
+    if (var_InheritBool(obj, "play-and-exit"))
+        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_EXIT;
+    else if (var_InheritBool(obj, "play-and-stop"))
+        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_STOP;
+    else if (var_InheritBool(obj, "play-and-pause"))
+        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_PAUSE;
+    else
+        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_CONTINUE;
+
+    bool start_paused = var_InheritBool(obj, "start-paused");
+
+    vlc_playlist_Lock(playlist);
+    vlc_playlist_SetPlaybackOrder(playlist, order);
+    vlc_playlist_SetPlaybackRepeat(playlist, repeat);
+
+    vlc_player_t *player = vlc_playlist_GetPlayer(playlist);
+
+    /* the playlist and the player share the same lock, and this is not an
+     * implementation detail */
+    vlc_player_SetMediaStoppedAction(player, media_stopped_action);
+    vlc_player_SetStartPaused(player, start_paused);
+
+    vlc_playlist_Unlock(playlist);
+}
+
 vlc_playlist_t *
 vlc_intf_GetMainPlaylist(intf_thread_t *intf)
 {
-    return libvlc_priv(vlc_object_instance(intf))->main_playlist;
+    libvlc_priv_t *priv = libvlc_priv(vlc_object_instance(intf));
+
+    vlc_mutex_lock(&lock);
+    vlc_playlist_t *playlist = priv->main_playlist;
+    if (priv->main_playlist == NULL)
+    {
+        vlc_object_t *libvlc_obj = VLC_OBJECT(vlc_object_instance(intf));
+        playlist = priv->main_playlist = vlc_playlist_New(libvlc_obj);
+        PlaylistConfigureFromVariables(playlist, libvlc_obj);
+    }
+    vlc_mutex_unlock(&lock);
+
+    return playlist;
 }
 
 /**
diff --git a/src/libvlc.c b/src/libvlc.c
index f183d3e978..b0789eb7fa 100644
--- a/src/libvlc.c
+++ b/src/libvlc.c
@@ -69,7 +69,6 @@
 #include "libvlc.h"
 #include "playlist_legacy/playlist_internal.h"
 #include "misc/variables.h"
-#include "input/player.h"
 
 #include <vlc_vlm.h>
 
@@ -105,50 +104,6 @@ libvlc_int_t * libvlc_InternalCreate( void )
     return p_libvlc;
 }
 
-static void
-PlaylistConfigureFromVariables(vlc_playlist_t *playlist, vlc_object_t *obj)
-{
-    enum vlc_playlist_playback_order order;
-    if (var_InheritBool(obj, "random"))
-        order = VLC_PLAYLIST_PLAYBACK_ORDER_RANDOM;
-    else
-        order = VLC_PLAYLIST_PLAYBACK_ORDER_NORMAL;
-
-    /* repeat = repeat current; loop = repeat all */
-    enum vlc_playlist_playback_repeat repeat;
-    if (var_InheritBool(obj, "repeat"))
-        repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_CURRENT;
-    else if (var_InheritBool(obj, "loop"))
-        repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_ALL;
-    else
-        repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_NONE;
-
-    enum vlc_player_media_stopped_action media_stopped_action;
-    if (var_InheritBool(obj, "play-and-exit"))
-        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_EXIT;
-    else if (var_InheritBool(obj, "play-and-stop"))
-        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_STOP;
-    else if (var_InheritBool(obj, "play-and-pause"))
-        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_PAUSE;
-    else
-        media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_CONTINUE;
-
-    bool start_paused = var_InheritBool(obj, "start-paused");
-
-    vlc_playlist_Lock(playlist);
-    vlc_playlist_SetPlaybackOrder(playlist, order);
-    vlc_playlist_SetPlaybackRepeat(playlist, repeat);
-
-    vlc_player_t *player = vlc_playlist_GetPlayer(playlist);
-
-    /* the playlist and the player share the same lock, and this is not an
-     * implementation detail */
-    vlc_player_SetMediaStoppedAction(player, media_stopped_action);
-    vlc_player_SetStartPaused(player, start_paused);
-
-    vlc_playlist_Unlock(playlist);
-}
-
 /**
  * Initialize a libvlc instance
  * This function initializes a previously allocated libvlc instance:
@@ -334,12 +289,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     }
 #endif
 
-    priv->main_playlist = vlc_playlist_New(VLC_OBJECT(p_libvlc));
-    if (unlikely(!priv->main_playlist))
-        goto error;
-
-    PlaylistConfigureFromVariables(priv->main_playlist, VLC_OBJECT(p_libvlc));
-
     /*
      * Load background interfaces
      */
-- 
2.20.1



More information about the vlc-devel mailing list