[vlc-devel] [PATCH 5/6] lib: media_player: rework multiple track selection API

Thomas Guillem thomas at gllm.fr
Mon Jun 15 23:10:06 CEST 2020


The libvlc_media_player_update_tracklist() design was not straight-forward and
was not easy to use on some languages. For example, vlcpp will expose a vector
of tracks instead of a tracklist, so requiring a tracklist pointer to select
multiple tracks adds a lot of complexity (returning a vector and keeping a
pointer to the tracklist).
---
 include/vlc/libvlc_media_player.h | 46 +++++++++++++-------------
 lib/libvlc.sym                    |  2 +-
 lib/media_player.c                | 55 +++++++++++++++----------------
 test/libvlc/media_player.c        | 13 +++++---
 4 files changed, 58 insertions(+), 58 deletions(-)

diff --git a/include/vlc/libvlc_media_player.h b/include/vlc/libvlc_media_player.h
index bd9f220ca16..e861cd5fca7 100644
--- a/include/vlc/libvlc_media_player.h
+++ b/include/vlc/libvlc_media_player.h
@@ -1355,8 +1355,7 @@ libvlc_media_player_get_track_from_id( libvlc_media_player_t *p_mi,
  *
  * \version LibVLC 4.0.0 and later.
  *
- * \note Use libvlc_media_player_update_tracklist() for finer track selection
- * control.
+ * \note Use libvlc_media_player_select_tracks() for multiple selection
  *
  * \param p_mi the media player
  * \param type type of the selected track
@@ -1367,6 +1366,27 @@ libvlc_media_player_select_track( libvlc_media_player_t *p_mi,
                                   libvlc_track_type_t type,
                                   const libvlc_media_track_t *track );
 
+/**
+ * Select multiple tracks for one type
+ *
+ * \version LibVLC 4.0.0 and later.
+ *
+ * \note The internal track list can change between the calls of
+ * libvlc_media_player_get_tracklist() and
+ * libvlc_media_player_set_tracks(). If a track selection change but the
+ * track is not present anymore, the player will just ignore it.
+ *
+ * \param p_mi the media player
+ * \param type type of the selected track
+ * \param tracks pointer to the track array
+ * \param track_count number of tracks in the track array
+ */
+LIBVLC_API void
+libvlc_media_player_select_tracks( libvlc_media_player_t *p_mi,
+                                   libvlc_track_type_t type,
+                                   const libvlc_media_track_t **tracks,
+                                   size_t track_count );
+
 /**
  * Select tracks by their string identifier
  *
@@ -1400,28 +1420,6 @@ libvlc_media_player_select_tracks_by_ids( libvlc_media_player_t *p_mi,
                                           libvlc_track_type_t type,
                                           const char *psz_ids );
 
-/**
- * Update the track selection for one type
- *
- * This function allow to select or unselect multiple tracks using the
- * track list returned by libvlc_media_player_get_tracklist(). The user can
- * iterate on all or few libvlc_media_track_t from this list and change the
- * 'selected' boolean before calling this function.
- *
- * \note The internal track list can change between the calls of
- * libvlc_media_player_get_tracklist() and
- * libvlc_media_player_update_tracklist(). If a track selection change but the
- * track is not present anymore, the player will just ignore it.
- *
- * \param p_mi the media player
- * \param type type of the selected track
- * \param list list returned by libvlc_media_player_get_tracklist()
- */
-LIBVLC_API void
-libvlc_media_player_update_tracklist( libvlc_media_player_t *p_mi,
-                                      libvlc_track_type_t type,
-                                      libvlc_media_tracklist_t *list );
-
 /**
  * Add a slave to the current media player.
  *
diff --git a/lib/libvlc.sym b/lib/libvlc.sym
index 3753ddb89be..e70d6b2e712 100644
--- a/lib/libvlc.sym
+++ b/lib/libvlc.sym
@@ -187,8 +187,8 @@ libvlc_media_player_get_tracklist
 libvlc_media_player_get_track_from_id
 libvlc_media_player_get_selected_track
 libvlc_media_player_select_track
+libvlc_media_player_select_tracks
 libvlc_media_player_select_tracks_by_ids
-libvlc_media_player_update_tracklist
 libvlc_media_release
 libvlc_media_retain
 libvlc_media_save_meta
diff --git a/lib/media_player.c b/lib/media_player.c
index 79550a271e2..2f390468c49 100644
--- a/lib/media_player.c
+++ b/lib/media_player.c
@@ -1919,30 +1919,14 @@ libvlc_media_player_select_track(libvlc_media_player_t *p_mi,
 }
 
 void
-libvlc_media_player_select_tracks_by_ids( libvlc_media_player_t *p_mi,
-                                          libvlc_track_type_t type,
-                                          const char *psz_ids )
+libvlc_media_player_select_tracks(libvlc_media_player_t *p_mi,
+                                  libvlc_track_type_t type,
+                                  const libvlc_media_track_t **tracks,
+                                  size_t track_count)
 {
-    const enum es_format_category_e cat = libvlc_track_type_to_escat(type);
-
     vlc_player_t *player = p_mi->player;
 
-    vlc_player_Lock(player);
-
-    vlc_player_SelectTracksByStringIds(player, cat, psz_ids);
-
-    vlc_player_Unlock(player);
-}
-
-void
-libvlc_media_player_update_tracklist(libvlc_media_player_t *p_mi,
-                                     libvlc_track_type_t type,
-                                     libvlc_media_tracklist_t *list)
-{
-    vlc_player_t *player = p_mi->player;
-
-    size_t count = libvlc_media_tracklist_count(list);
-    vlc_es_id_t **es_id_list = vlc_alloc(count + 1, sizeof(vlc_es_id_t *));
+    vlc_es_id_t **es_id_list = vlc_alloc(track_count + 1, sizeof(vlc_es_id_t *));
     size_t es_id_idx = 0;
 
     if (es_id_list == NULL)
@@ -1952,15 +1936,13 @@ libvlc_media_player_update_tracklist(libvlc_media_player_t *p_mi,
 
     vlc_player_Lock(player);
 
-    for (size_t i = 0; i < count; ++i)
+    for (size_t i = 0; i < track_count; ++i)
     {
-        const libvlc_media_track_t *track = libvlc_media_tracklist_at(list, i);
-        if (track->selected)
-        {
-            const libvlc_media_trackpriv_t *trackpriv =
-                libvlc_media_track_to_priv(track);
-            es_id_list[es_id_idx++] = trackpriv->es_id;
-        }
+        const libvlc_media_track_t *track = tracks[i];
+        const libvlc_media_trackpriv_t *trackpriv =
+            libvlc_media_track_to_priv(track);
+
+        es_id_list[es_id_idx++] = trackpriv->es_id;
     }
     es_id_list[es_id_idx++] = NULL;
     vlc_player_SelectEsIdList(player, cat, es_id_list);
@@ -1970,6 +1952,21 @@ libvlc_media_player_update_tracklist(libvlc_media_player_t *p_mi,
     free(es_id_list);
 }
 
+void
+libvlc_media_player_select_tracks_by_ids( libvlc_media_player_t *p_mi,
+                                          libvlc_track_type_t type,
+                                          const char *psz_ids )
+{
+    const enum es_format_category_e cat = libvlc_track_type_to_escat(type);
+
+    vlc_player_t *player = p_mi->player;
+
+    vlc_player_Lock(player);
+
+    vlc_player_SelectTracksByStringIds(player, cat, psz_ids);
+
+    vlc_player_Unlock(player);
+}
 
 int libvlc_media_player_add_slave( libvlc_media_player_t *p_mi,
                                    libvlc_media_slave_type_t i_type,
diff --git a/test/libvlc/media_player.c b/test/libvlc/media_player.c
index 088f9153d2c..8b7d3d0ae4d 100644
--- a/test/libvlc/media_player.c
+++ b/test/libvlc/media_player.c
@@ -451,11 +451,16 @@ static void test_media_player_tracks(const char** argv, int argc)
     libvlc_media_tracklist_t *tracklist =
         libvlc_media_player_get_tracklist(mp, libvlc_track_video);
     assert(tracklist);
-    libtrack = libvlc_media_tracklist_at(tracklist, 2);
-    assert(libtrack && !libtrack->selected);
-    libtrack->selected = true;
-    libvlc_media_player_update_tracklist(mp, libvlc_track_video, tracklist);
+
+    const libvlc_media_track_t *selecttracks[] = {
+        libvlc_media_track_hold(libvlc_media_tracklist_at(tracklist, 1)),
+        libvlc_media_track_hold(libvlc_media_tracklist_at(tracklist, 2)),
+    };
+    assert(selecttracks[0]->selected); /* video/1 already selected */
+    assert(!selecttracks[1]->selected); /* select video/2 */
     libvlc_media_tracklist_delete(tracklist);
+
+    libvlc_media_player_select_tracks(mp, libvlc_track_video, selecttracks, 2);
     vtracks[2].toselect = true;
 
     /* Unselect all spu tracks */
-- 
2.20.1



More information about the vlc-devel mailing list