contrib: gpg-error: Provide a lock obj for NACL
[vlc.git] / test / libvlc / slaves.c
blob7b2c24fa43b281bd0ba911748b38254192abb29e
1 /*****************************************************************************
2 * slaves.c: test libvlc_media_t and libvlc_media_player_t slaves API
3 *****************************************************************************
4 * Copyright © 2016 VLC authors, VideoLAN and VideoLabs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #include "test.h"
23 #include <vlc_common.h>
24 #include <vlc_threads.h>
26 #define SLAVES_DIR SRCDIR "/samples/slaves"
28 static void
29 finished_event(const libvlc_event_t *p_ev, void *p_data)
31 (void) p_ev;
32 vlc_sem_t *p_sem = p_data;
33 vlc_sem_post(p_sem);
36 static void
37 media_parse_sync(libvlc_media_t *p_m)
39 vlc_sem_t sem;
40 vlc_sem_init(&sem, 0);
42 libvlc_event_manager_t *p_em = libvlc_media_event_manager(p_m);
43 libvlc_event_attach(p_em, libvlc_MediaParsedChanged, finished_event, &sem);
45 int i_ret = libvlc_media_parse_with_options(p_m, libvlc_media_parse_local, -1);
46 assert(i_ret == 0);
48 vlc_sem_wait (&sem);
50 libvlc_event_detach(p_em, libvlc_MediaParsedChanged, finished_event, &sem);
52 vlc_sem_destroy (&sem);
55 static char *
56 path_to_mrl(libvlc_instance_t *p_vlc, const char *psz_path)
58 libvlc_media_t *p_m = libvlc_media_new_path(p_vlc, psz_path);
59 char *psz_mrl = libvlc_media_get_mrl(p_m);
60 libvlc_media_release(p_m);
61 return psz_mrl;
64 static void
65 test_expected_slaves(libvlc_media_t *p_m,
66 libvlc_media_slave_t *p_expected_slaves,
67 unsigned int i_expected_slaves)
69 printf("Check if slaves are correclty attached to media\n");
71 libvlc_media_slave_t **pp_slaves;
72 unsigned int i_slave_count = libvlc_media_slaves_get(p_m, &pp_slaves);
73 assert(i_expected_slaves == i_slave_count);
75 unsigned i_found_slaves = 0;
76 bool *p_found_list = calloc(i_expected_slaves, sizeof(bool));
77 assert(p_found_list != NULL);
78 for (unsigned int i = 0; i < i_slave_count; ++i)
80 libvlc_media_slave_t *p_slave1 = pp_slaves[i];
81 for (unsigned int j = 0; j < i_expected_slaves; ++j)
83 libvlc_media_slave_t *p_slave2 = &p_expected_slaves[j];
84 if (strcmp(p_slave1->psz_uri, p_slave2->psz_uri) == 0)
86 assert(p_found_list[j] == false);
87 assert(p_slave1->i_type == p_slave2->i_type);
88 assert(p_slave1->i_priority == p_slave2->i_priority);
89 p_found_list[j] = true;
90 i_found_slaves++;
91 break;
95 assert(i_expected_slaves == i_found_slaves);
96 for (unsigned int i = 0; i < i_expected_slaves; ++i)
98 printf("Check if slaves[%d] is found\n", i);
99 assert(p_found_list[i]);
101 free(p_found_list);
103 libvlc_media_slaves_release(pp_slaves, i_slave_count);
106 static void
107 test_media_has_slaves_from_parent(libvlc_instance_t *p_vlc,
108 const char *psz_main_media,
109 libvlc_media_slave_t *p_expected_slaves,
110 unsigned i_expected_slaves)
112 libvlc_media_t *p_m = libvlc_media_new_path(p_vlc, SLAVES_DIR);
113 assert(p_m != NULL);
115 printf("Parse media dir to get subitems\n");
116 media_parse_sync(p_m);
118 char *psz_main_media_mrl = path_to_mrl(p_vlc, psz_main_media);
119 assert(psz_main_media_mrl != NULL);
120 printf("Main media mrl: '%s'\n", psz_main_media_mrl);
122 printf("Fetch main media from subitems\n");
123 libvlc_media_list_t *p_ml = libvlc_media_subitems(p_m);
124 assert(p_ml != NULL);
125 libvlc_media_list_lock(p_ml);
126 int i_count = libvlc_media_list_count(p_ml);
127 assert(i_count > 0);
128 libvlc_media_t *p_subm = NULL;
129 for (int i = 0; i < i_count; ++i)
131 p_subm = libvlc_media_list_item_at_index(p_ml, i);
132 assert(p_subm != NULL);
133 char *psz_mrl = libvlc_media_get_mrl(p_subm);
134 assert(psz_mrl != NULL);
135 if (strcmp(psz_main_media_mrl, psz_mrl) == 0)
137 printf("Found main media\n");
138 free(psz_mrl);
139 break;
141 free(psz_mrl);
142 libvlc_media_release(p_subm);
143 p_subm = NULL;
145 free(psz_main_media_mrl);
146 libvlc_media_list_unlock(p_ml);
147 libvlc_media_list_release(p_ml);
149 assert(p_subm != NULL);
150 test_expected_slaves(p_subm, p_expected_slaves, i_expected_slaves);
151 libvlc_media_release(p_subm);
153 libvlc_media_release(p_m);
157 main (void)
159 test_init();
161 const char *pp_slave_paths[] = {
162 SLAVES_DIR "/test.aac",
163 SLAVES_DIR "/test.rt.srt",
164 SLAVES_DIR "/lt-test.srt",
165 SLAVES_DIR "/nomatch.srt",
168 libvlc_media_slave_t p_expected_slaves[] = {
169 { NULL, libvlc_media_slave_type_audio, 3 /* all */ },
170 { NULL, libvlc_media_slave_type_subtitle, 2 /* right */ },
171 { NULL, libvlc_media_slave_type_subtitle, 1 /* left */ },
172 { NULL, libvlc_media_slave_type_subtitle, 0 /* none */ },
175 #define EXPECTED_SLAVES_COUNT (sizeof(p_expected_slaves) / sizeof(*p_expected_slaves))
176 static_assert((sizeof(pp_slave_paths) / sizeof(*pp_slave_paths)) == EXPECTED_SLAVES_COUNT,
177 "pp_slave_paths and p_expected_slaves mismatch");
179 const char *pp_args[] = {
180 "-v", "--sub-autodetect-fuzzy", "1",
181 "--no-video", "--no-audio",
182 "--codec", "none", /* to ensure we don't depend on codec modules */
183 NULL /* "sub-autodetect-file" place holder */
185 #define ARGC (sizeof(pp_args) / sizeof(*pp_args))
187 libvlc_instance_t *p_vlc = libvlc_new(ARGC - 1, pp_args);
188 assert(p_vlc != NULL);
190 /* Fill p_expected_slaves with correct VLC mrls */
191 for (unsigned int i = 0; i < EXPECTED_SLAVES_COUNT; ++i)
193 p_expected_slaves[i].psz_uri = path_to_mrl(p_vlc, pp_slave_paths[i]);
194 assert(p_expected_slaves[i].psz_uri != NULL);
197 printf("== Testing --sub-autodetect-fuzzy 1 (everything) ==\n");
198 test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
199 p_expected_slaves,
200 EXPECTED_SLAVES_COUNT);
201 libvlc_release(p_vlc);
203 printf("== Testing --sub-autodetect-fuzzy 2 (full, left, and right match) ==\n");
204 pp_args[2] = "2";
205 p_vlc = libvlc_new(ARGC - 1, pp_args);
206 assert(p_vlc != NULL);
207 test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
208 p_expected_slaves, 3);
210 printf("== Testing if the matching is not too permissive ==\n");
211 test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/t.mp4",
212 NULL, 0);
213 libvlc_release(p_vlc);
215 printf("== Testing --sub-autodetect-fuzzy 3 (full and left match) ==\n");
216 pp_args[2] = "3";
217 p_vlc = libvlc_new(ARGC - 1, pp_args);
218 assert(p_vlc != NULL);
219 test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
220 p_expected_slaves, 2);
221 libvlc_release(p_vlc);
223 printf("== Testing --sub-autodetect-fuzzy 4 (full match) ==\n");
224 pp_args[2] = "4";
225 p_vlc = libvlc_new(ARGC - 1, pp_args);
226 assert(p_vlc != NULL);
227 test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4",
228 p_expected_slaves, 1);
229 libvlc_release(p_vlc);
231 printf("== Testing --no-sub-autodetect-file (no match) ==\n");
232 pp_args[ARGC - 1] = "--no-sub-autodetect-file";
233 p_vlc = libvlc_new(ARGC, pp_args);
234 assert(p_vlc != NULL);
235 test_media_has_slaves_from_parent(p_vlc, SLAVES_DIR "/test.mp4", NULL, 0);
236 libvlc_release(p_vlc);
238 for (unsigned int i = 0; i < EXPECTED_SLAVES_COUNT; ++i)
239 free(p_expected_slaves[i].psz_uri);
241 return 0;