[vlc-commits] lib: media_player: rework multiple track selection API

Thomas Guillem git at videolan.org
Wed Jun 17 10:14:53 CEST 2020


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Mon Jun 15 15:02:18 2020 +0200| [17730137be9caa97c9ef88180e2d128af08ae5be] | committer: Thomas Guillem

lib: media_player: rework multiple track selection API

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).

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=17730137be9caa97c9ef88180e2d128af08ae5be
---

 include/vlc/libvlc_media_player.h | 46 ++++++++++++++++----------------
 lib/libvlc.sym                    |  2 +-
 lib/media_player.c                | 55 ++++++++++++++++++---------------------
 test/libvlc/media_player.c        | 20 +++++++++++---
 4 files changed, 65 insertions(+), 58 deletions(-)

diff --git a/include/vlc/libvlc_media_player.h b/include/vlc/libvlc_media_player.h
index bd9f220ca1..e861cd5fca 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 3753ddb89b..e70d6b2e71 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 79550a271e..2f390468c4 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 088f9153d2..d649a7688c 100644
--- a/test/libvlc/media_player.c
+++ b/test/libvlc/media_player.c
@@ -451,13 +451,25 @@ 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);
+
+    libvlc_media_track_t *vtrack1 =
+        libvlc_media_track_hold(libvlc_media_tracklist_at(tracklist, 1));
+    libvlc_media_track_t *vtrack2 =
+        libvlc_media_track_hold(libvlc_media_tracklist_at(tracklist, 2));
     libvlc_media_tracklist_delete(tracklist);
+
+    const libvlc_media_track_t *selecttracks[] = {
+        vtrack1, vtrack2,
+    };
+    assert(vtrack1->selected); /* video/1 already selected */
+    assert(!vtrack2->selected); /* select video/2 */
+
+    libvlc_media_player_select_tracks(mp, libvlc_track_video, selecttracks, 2);
     vtracks[2].toselect = true;
 
+    libvlc_media_track_release(vtrack1);
+    libvlc_media_track_release(vtrack2);
+
     /* Unselect all spu tracks */
     libvlc_media_player_select_track(mp, libvlc_track_text, NULL);
     stracks[0].toselect = stracks[1].toselect = false;



More information about the vlc-commits mailing list