[vlc-devel] [PATCHv3 17/17] test: lib: media_player: test track API

Thomas Guillem thomas at gllm.fr
Wed Jun 10 14:57:56 CEST 2020


---
 test/libvlc/media_player.c | 277 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 276 insertions(+), 1 deletion(-)

diff --git a/test/libvlc/media_player.c b/test/libvlc/media_player.c
index cf48b225f7a..5be00110141 100644
--- a/test/libvlc/media_player.c
+++ b/test/libvlc/media_player.c
@@ -247,6 +247,281 @@ static void test_media_player_pause_stop(const char** argv, int argc)
     libvlc_release (vlc);
 }
 
+struct track
+{
+    const char *id;
+    bool toselect;
+    bool selected;
+    bool added;
+    bool removed;
+};
+
+static const libvlc_track_type_t types[] =
+    { libvlc_track_audio, libvlc_track_video, libvlc_track_text };
+
+static struct track *get_track_from_id(struct track *tracks, const char *id)
+{
+    while (tracks->id != NULL)
+    {
+        if (strcmp(tracks->id, id) == 0)
+            return tracks;
+        tracks++;
+    }
+    return NULL;
+}
+
+static bool tracks_check_all_events(struct track *const *tracks, bool removed)
+{
+    for (size_t i = 0; i < ARRAY_SIZE(types); ++i)
+    {
+        libvlc_track_type_t type = types[i];
+        const struct track *ttracks = tracks[type];
+
+        while (ttracks->id != NULL)
+        {
+            if (removed)
+            {
+                if (!ttracks->removed)
+                    return false;
+            }
+            else
+            {
+                if (!ttracks->added || ttracks->toselect != ttracks->selected)
+                    return false;
+            }
+            ttracks++;
+        }
+    }
+
+    return true;
+}
+
+static void test_media_player_tracks(const char** argv, int argc)
+{
+    test_log ("Testing tracks\n");
+
+    const char *file = "mock://video_track_count=3;audio_track_count=3;sub_track_count=3";
+
+    struct track atracks[] = {
+        { "audio/0", false, false, false, false },
+        { "audio/1", false, false, false, false },
+        { "audio/2", true, false, false, false },
+        { NULL, false, false, false, false },
+    };
+    static const char atracks_ids[] = "audio/2";
+
+    struct track vtracks[] = {
+        { "video/0", false, false, false, false },
+        { "video/1", true, false, false, false },
+        { "video/2", false, false, false, false },
+        { NULL, false, false, false, false },
+    };
+    static const char vtracks_ids[] = "video/1";
+
+    struct track stracks[] = {
+        { "spu/0", true, false, false, false },
+        { "spu/1", true, false, false, false },
+        { "spu/2", false, false, false, false },
+        { NULL, false, false, false, false },
+    };
+    static const char stracks_ids[] = "spu/0,spu/1";
+
+    struct track *alltracks[] = {
+        [libvlc_track_audio] = atracks,
+        [libvlc_track_video] = vtracks,
+        [libvlc_track_text] = stracks,
+    };
+    static const size_t alltracks_count[] = {
+        [libvlc_track_audio] = 3,
+        [libvlc_track_video] = 3,
+        [libvlc_track_text] = 3,
+    };
+
+    /* Avoid leaks from various dlopen... */
+    const char *new_argv[argc+1];
+    for (int i = 0; i < argc; ++i)
+        new_argv[i] = argv[i];
+    new_argv[argc++] = "--codec=araw,rawvideo,subsdec,none";
+
+    /* Load the mock media */
+    libvlc_instance_t *vlc = libvlc_new (argc, new_argv);
+    assert (vlc != NULL);
+    libvlc_media_t *md = libvlc_media_new_location (vlc, file);
+    assert (md != NULL);
+    libvlc_media_player_t *mp = libvlc_media_player_new (vlc);
+    assert (mp != NULL);
+    libvlc_media_player_set_media (mp, md);
+    libvlc_media_release (md);
+
+    /* Confiture track selection before first play */
+    libvlc_media_player_select_tracks_by_ids(mp, libvlc_track_audio, atracks_ids);
+    libvlc_media_player_select_tracks_by_ids(mp, libvlc_track_video, vtracks_ids);
+    libvlc_media_player_select_tracks_by_ids(mp, libvlc_track_text, stracks_ids);
+
+    libvlc_media_track_t *libtrack;
+    libvlc_event_manager_t *em = libvlc_media_player_event_manager(mp);
+    struct event_ctx ctx;
+    event_ctx_init(&ctx);
+
+    int res;
+    res = libvlc_event_attach(em, libvlc_MediaPlayerESAdded, on_event, &ctx);
+    assert(!res);
+    res = libvlc_event_attach(em, libvlc_MediaPlayerESDeleted, on_event, &ctx);
+    assert(!res);
+    res = libvlc_event_attach(em, libvlc_MediaPlayerESSelected, on_event, &ctx);
+    assert(!res);
+
+    libvlc_media_player_play (mp);
+
+    /* Check that all tracks are added and selected according to
+     * libvlc_media_player_select_tracks_by_ids */
+    while (!tracks_check_all_events(alltracks, false))
+    {
+        const struct libvlc_event_t *ev = even_ctx_wait_event(&ctx);
+        switch (ev->type)
+        {
+            case libvlc_MediaPlayerESAdded:
+            {
+                libvlc_track_type_t type = ev->u.media_player_es_changed.i_type;
+                struct track *track =
+                    get_track_from_id(alltracks[type],
+                                      ev->u.media_player_es_changed.psz_id);
+                assert(track);
+                track->added = true;
+                break;
+            }
+            case libvlc_MediaPlayerESSelected:
+            {
+                libvlc_track_type_t type = ev->u.media_player_es_selection_changed.i_type;
+                const char *selected_id =
+                    ev->u.media_player_es_selection_changed.psz_selected_id;
+                const char *unselected_id =
+                    ev->u.media_player_es_selection_changed.psz_unselected_id;
+
+                assert(selected_id);
+                assert(!unselected_id);
+
+                struct track *track = get_track_from_id(alltracks[type],
+                                                        selected_id);
+                assert(track);
+                assert(track->toselect);
+                track->selected = true;
+                break;
+            }
+            default:
+                assert(!"Event not expected");
+        }
+
+        event_ctx_release(&ctx);
+    }
+
+    /* Compare with the tracklists */
+    for (size_t i = 0; i < ARRAY_SIZE(types); ++i)
+    {
+        libvlc_track_type_t type = types[i];
+        size_t track_count = alltracks_count[i];
+        struct track *tracks = alltracks[i];
+
+        libvlc_media_tracklist_t *tracklist =
+            libvlc_media_player_get_tracklist(mp, type);
+        assert(tracklist);
+        assert(libvlc_media_tracklist_count(tracklist) == track_count);
+
+        for (size_t j = 0; j < track_count; ++j)
+        {
+            struct track *track = &tracks[j];
+            libtrack = libvlc_media_tracklist_at(tracklist, j);
+            assert(libtrack);
+
+            assert(strcmp(libtrack->psz_id, track->id) == 0);
+            assert(libtrack->selected == track->selected);
+        }
+        libvlc_media_tracklist_release(tracklist);
+    }
+
+    /* Select (replace) a new audio track */
+    libtrack = libvlc_media_player_get_track_from_id(mp, "audio/0");
+    assert(libtrack);
+    libvlc_media_player_select_track(mp, libvlc_track_audio, libtrack);
+    libvlc_media_track_release(libtrack);
+    atracks[0].toselect = true;
+    atracks[2].toselect = false;
+
+    /* Add a new video track */
+    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_tracklist_release(tracklist);
+    vtracks[2].toselect = true;
+
+    /* Unselect all spu tracks */
+    libvlc_media_player_select_track(mp, libvlc_track_text, NULL);
+    stracks[0].toselect = stracks[1].toselect = false;
+
+    /* Check that all tracks are added and selected according to previous
+     * changes. */
+    while (!tracks_check_all_events(alltracks, false))
+    {
+        const struct libvlc_event_t *ev = even_ctx_wait_event(&ctx);
+        assert(ev->type == libvlc_MediaPlayerESSelected);
+
+        libvlc_track_type_t type = ev->u.media_player_es_selection_changed.i_type;
+        const char *selected_id =
+            ev->u.media_player_es_selection_changed.psz_selected_id;
+        const char *unselected_id =
+            ev->u.media_player_es_selection_changed.psz_unselected_id;
+
+        if (unselected_id)
+        {
+            struct track *track =
+                get_track_from_id(alltracks[type], unselected_id);
+            assert(!track->toselect);
+            assert(track->selected);
+            track->selected = false;
+        }
+
+        if (selected_id)
+        {
+            struct track *track =
+                get_track_from_id(alltracks[type], selected_id);
+            assert(track->toselect);
+            assert(!track->selected);
+            track->selected = true;
+        }
+
+        event_ctx_release(&ctx);
+    }
+
+    libvlc_media_player_stop_async (mp);
+
+    /* Check that all tracks are removed */
+    while (!tracks_check_all_events(alltracks, true))
+    {
+        const struct libvlc_event_t *ev = even_ctx_wait_event(&ctx);
+
+        if (ev->type == libvlc_MediaPlayerESDeleted)
+        {
+            libvlc_track_type_t type = ev->u.media_player_es_changed.i_type;
+            struct track *track =
+                get_track_from_id(alltracks[type],
+                                  ev->u.media_player_es_changed.psz_id);
+            assert(track);
+            track->removed = true;
+        }
+        event_ctx_release(&ctx);
+    }
+
+    libvlc_event_detach(em, libvlc_MediaPlayerESAdded, on_event, &ctx);
+    libvlc_event_detach(em, libvlc_MediaPlayerESDeleted, on_event, &ctx);
+    libvlc_event_detach(em, libvlc_MediaPlayerESSelected, on_event, &ctx);
+
+    libvlc_media_player_release (mp);
+    libvlc_release (vlc);
+}
 
 int main (void)
 {
@@ -255,7 +530,7 @@ int main (void)
     test_media_player_set_media (test_defaults_args, test_defaults_nargs);
     test_media_player_play_stop (test_defaults_args, test_defaults_nargs);
     test_media_player_pause_stop (test_defaults_args, test_defaults_nargs);
-//    test_media_player_tracks (test_defaults_args, test_defaults_nargs);
+    test_media_player_tracks (test_defaults_args, test_defaults_nargs);
 
     return 0;
 }
-- 
2.20.1



More information about the vlc-devel mailing list