From bb6fe6ffc7032abceb05bc44f51a093200940d3d Mon Sep 17 00:00:00 2001 From: Thomas Guillem Date: Mon, 30 May 2016 18:49:52 +0200 Subject: [PATCH] input: test slaves test_media_has_slaves_from_player is deactivated for now since there is now way to test inputs in a reliable way. Indeed, demux modules may depend on configuration and systems libs. TODO: test libvlc_media_player_add_slave --- test/Makefile.am | 3 + test/libvlc/slaves.c | 265 +++++++++++++++++++++++++++++ test/samples/slaves/left-test.srt | 0 test/samples/slaves/nomatch.srt | 1 + test/samples/slaves/test-right.srt | 0 test/samples/slaves/test.aac | 0 test/samples/slaves/test.mp4 | 0 7 files changed, 269 insertions(+) create mode 100644 test/libvlc/slaves.c create mode 100644 test/samples/slaves/left-test.srt create mode 100644 test/samples/slaves/nomatch.srt create mode 100644 test/samples/slaves/test-right.srt create mode 100644 test/samples/slaves/test.aac create mode 100644 test/samples/slaves/test.mp4 diff --git a/test/Makefile.am b/test/Makefile.am index f8d4125e74..a7b1978978 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -18,6 +18,7 @@ check_PROGRAMS = \ test_libvlc_media \ test_libvlc_media_list \ test_libvlc_media_player \ + test_libvlc_slaves \ test_src_config_chain \ test_src_misc_variables \ test_src_crypto_update \ @@ -80,6 +81,8 @@ test_libvlc_media_list_SOURCES = libvlc/media_list.c test_libvlc_media_list_LDADD = $(LIBVLC) test_libvlc_media_player_SOURCES = libvlc/media_player.c test_libvlc_media_player_LDADD = $(LIBVLC) +test_libvlc_slaves_SOURCES = libvlc/slaves.c +test_libvlc_slaves_LDADD = $(LIBVLCCORE) $(LIBVLC) test_libvlc_meta_SOURCES = libvlc/meta.c test_libvlc_meta_LDADD = $(LIBVLC) test_src_misc_variables_SOURCES = src/misc/variables.c diff --git a/test/libvlc/slaves.c b/test/libvlc/slaves.c new file mode 100644 index 0000000000..b9c9d46f4d --- /dev/null +++ b/test/libvlc/slaves.c @@ -0,0 +1,265 @@ +/***************************************************************************** + * slaves.c: test libvlc_media_t and libvlc_media_player_t slaves API + ***************************************************************************** + * Copyright © 2016 VLC authors, VideoLAN and VideoLabs + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include "test.h" + +#include +#include + +#define SLAVES_DIR SRCDIR "/samples/slaves" +#define MAIN_MEDIA_PATH SLAVES_DIR "/test.mp4" + +static void +finished_event(const libvlc_event_t *p_ev, void *p_data) +{ + (void) p_ev; + vlc_sem_t *p_sem = p_data; + vlc_sem_post(p_sem); +} + +static void +media_parse_sync(libvlc_media_t *p_m) +{ + vlc_sem_t sem; + vlc_sem_init(&sem, 0); + + libvlc_event_manager_t *p_em = libvlc_media_event_manager(p_m); + libvlc_event_attach(p_em, libvlc_MediaParsedChanged, finished_event, &sem); + + int i_ret = libvlc_media_parse_with_options(p_m, libvlc_media_parse_local); + assert(i_ret == 0); + + vlc_sem_wait (&sem); + + libvlc_event_detach(p_em, libvlc_MediaParsedChanged, finished_event, &sem); + + vlc_sem_destroy (&sem); +} + +static char * +path_to_mrl(libvlc_instance_t *p_vlc, const char *psz_path) +{ + libvlc_media_t *p_m = libvlc_media_new_path(p_vlc, psz_path); + char *psz_mrl = libvlc_media_get_mrl(p_m); + libvlc_media_release(p_m); + return psz_mrl; +} + +static void +test_expected_slaves(libvlc_media_t *p_m, + libvlc_media_slave_t *p_expected_slaves, + unsigned int i_expected_slaves) +{ + printf("Check if slaves are correclty attached to media\n"); + + libvlc_media_slave_t **pp_slaves; + unsigned int i_slave_count = libvlc_media_slaves_get(p_m, &pp_slaves); + assert(i_expected_slaves == i_slave_count); + + if (i_expected_slaves > 0) + { + bool *p_found_list = calloc(i_expected_slaves, sizeof(bool)); + assert(p_found_list != NULL); + for (unsigned int i = 0; i < i_slave_count; ++i) + { + libvlc_media_slave_t *p_slave1 = pp_slaves[i]; + for (unsigned int j = 0; i < i_expected_slaves; ++j) + { + libvlc_media_slave_t *p_slave2 = &p_expected_slaves[j]; + if (strcmp(p_slave1->psz_uri, p_slave2->psz_uri) == 0) + { + assert(p_found_list[j] == false); + assert(p_slave1->i_type == p_slave2->i_type); + assert(p_slave1->i_priority == p_slave2->i_priority); + p_found_list[j] = true; + break; + } + } + } + for (unsigned int i = 0; i < i_expected_slaves; ++i) + { + printf("Check if slaves[%d] is found\n", i); + assert(p_found_list[i]); + } + free(p_found_list); + } + + libvlc_media_slaves_release(pp_slaves, i_slave_count); +} + +#if 0 +static void +mediaplayer_play_sync(libvlc_media_player_t *p_mp) +{ + vlc_sem_t sem; + vlc_sem_init(&sem, 0); + + libvlc_event_manager_t *p_em = libvlc_media_player_event_manager(p_mp); + libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, finished_event, &sem); + libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, finished_event, &sem); + libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, finished_event, &sem); + + int i_ret = libvlc_media_player_play(p_mp); + assert(i_ret == 0); + + vlc_sem_wait (&sem); + + libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, finished_event, &sem); + libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, finished_event, &sem); + libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, finished_event, &sem); + + libvlc_media_player_stop(p_mp); + + vlc_sem_destroy (&sem); +} + +static void +test_media_has_slaves_from_player(libvlc_instance_t *p_vlc, + libvlc_media_slave_t *p_expected_slaves, + unsigned i_expected_slaves) +{ + /* This function test subtitles_Detect() when playing a local file */ + libvlc_media_t *p_m = libvlc_media_new_path(p_vlc, MAIN_MEDIA_PATH); + assert(p_m != NULL); + + libvlc_media_player_t *p_mp = libvlc_media_player_new_from_media(p_m); + assert(p_mp != NULL); + mediaplayer_play_sync(p_mp); + + test_expected_slaves(p_m, p_expected_slaves, i_expected_slaves); + + libvlc_media_release(p_m); + libvlc_media_player_release(p_mp); +} +#endif + +static void +test_media_has_slaves_from_parent(libvlc_instance_t *p_vlc, + libvlc_media_slave_t *p_expected_slaves, + unsigned i_expected_slaves) +{ + libvlc_media_t *p_m = libvlc_media_new_path(p_vlc, SLAVES_DIR); + assert(p_m != NULL); + + printf("Parse media dir to get subitems\n"); + media_parse_sync(p_m); + + char *psz_main_media_mrl = path_to_mrl(p_vlc, MAIN_MEDIA_PATH); + assert(psz_main_media_mrl != NULL); + printf("Main media mrl: '%s'\n", psz_main_media_mrl); + + printf("Fetch main media from subitems\n"); + libvlc_media_list_t *p_ml = libvlc_media_subitems(p_m); + assert(p_ml != NULL); + libvlc_media_list_lock(p_ml); + int i_count = libvlc_media_list_count(p_ml); + assert(i_count > 0); + libvlc_media_t *p_subm = NULL; + for (int i = 0; i < i_count; ++i) + { + p_subm = libvlc_media_list_item_at_index(p_ml, i); + assert(p_subm != NULL); + char *psz_mrl = libvlc_media_get_mrl(p_subm); + assert(psz_mrl != NULL); + if (strcmp(psz_main_media_mrl, psz_mrl) == 0) + { + printf("Found main media\n"); + free(psz_mrl); + break; + } + free(psz_mrl); + libvlc_media_release(p_subm); + p_subm = NULL; + } + free(psz_main_media_mrl); + libvlc_media_list_unlock(p_ml); + libvlc_media_list_release(p_ml); + + assert(p_subm != NULL); + test_expected_slaves(p_subm, p_expected_slaves, i_expected_slaves); + libvlc_media_release(p_subm); + + libvlc_media_release(p_m); +} + +int +main (void) +{ + test_init(); + + const char *pp_slave_paths[] = { + SLAVES_DIR "/nomatch.srt", + SLAVES_DIR "/left-test.srt", + SLAVES_DIR "/test-right.srt", + SLAVES_DIR "/test.aac", + }; + + libvlc_media_slave_t p_expected_slaves[] = { + { NULL, libvlc_media_slave_type_subtitle, 0 /* none */ }, + { NULL, libvlc_media_slave_type_subtitle, 1 /* left */ }, + { NULL, libvlc_media_slave_type_subtitle, 2 /* right */ }, + { NULL, libvlc_media_slave_type_audio, 3 /* all */ }, + }; + + #define EXPECTED_SLAVES_COUNT (sizeof(p_expected_slaves) / sizeof(*p_expected_slaves)) + static_assert((sizeof(pp_slave_paths) / sizeof(*pp_slave_paths)) == EXPECTED_SLAVES_COUNT, + "pp_slave_paths and p_expected_slaves mismatch"); + + const char *pp_args[] = { + "-v", "--sub-autodetect-fuzzy", "1", + "--no-video", "--no-audio", + "--codec", "none", /* to ensure we don't depend on codec modules */ + NULL /* "sub-autodetect-file" place holder */ + }; + #define ARGC (sizeof(pp_args) / sizeof(*pp_args)) + + libvlc_instance_t *p_vlc = libvlc_new(ARGC - 1, pp_args); + assert(p_vlc != NULL); + + /* Fill p_expected_slaves with correct VLC mrls */ + for (unsigned int i = 0; i < EXPECTED_SLAVES_COUNT; ++i) + { + p_expected_slaves[i].psz_uri = path_to_mrl(p_vlc, pp_slave_paths[i]); + assert(p_expected_slaves[i].psz_uri != NULL); + } + +#if 0 + printf("== Test if a media has slaves from a media player ==\n"); + test_media_has_slaves_from_player(p_vlc, p_expected_slaves, + EXPECTED_SLAVES_COUNT - 1); +#endif + + printf("== Test if a media has slaves from its parent ==\n"); + test_media_has_slaves_from_parent(p_vlc, p_expected_slaves, + EXPECTED_SLAVES_COUNT); + libvlc_release(p_vlc); + + printf("== Test if a media doesn't have slaves from its parent ==\n"); + pp_args[ARGC - 1] = "--no-sub-autodetect-file"; + p_vlc = libvlc_new(ARGC, pp_args); + assert(p_vlc != NULL); + test_media_has_slaves_from_parent(p_vlc, NULL, 0); + libvlc_release(p_vlc); + + for (unsigned int i = 0; i < EXPECTED_SLAVES_COUNT; ++i) + free(p_expected_slaves[i].psz_uri); + + return 0; +} diff --git a/test/samples/slaves/left-test.srt b/test/samples/slaves/left-test.srt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/samples/slaves/nomatch.srt b/test/samples/slaves/nomatch.srt new file mode 100644 index 0000000000..d3f5a12faa --- /dev/null +++ b/test/samples/slaves/nomatch.srt @@ -0,0 +1 @@ + diff --git a/test/samples/slaves/test-right.srt b/test/samples/slaves/test-right.srt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/samples/slaves/test.aac b/test/samples/slaves/test.aac new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/samples/slaves/test.mp4 b/test/samples/slaves/test.mp4 new file mode 100644 index 0000000000..e69de29bb2 -- GitLab