qt: playlist: use item title if available
[vlc.git] / test / libvlc / media.c
blob2d27cbb1d6199b3dc4058b57e14c42d98aeca31c
1 /*
2 * media_player.c - libvlc smoke test
4 */
6 /**********************************************************************
7 * Copyright (C) 2010 Pierre d'Herbemont. *
8 * This program is free software; you can redistribute and/or modify *
9 * it under the terms of the GNU General Public License as published *
10 * by the Free Software Foundation; version 2 of the license, or (at *
11 * your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
16 * See the GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, you can get it from: *
20 * http://www.gnu.org/copyleft/gpl.html *
21 **********************************************************************/
23 #include "test.h"
24 #include "../lib/libvlc_internal.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
30 #include <vlc_threads.h>
31 #include <vlc_fs.h>
32 #include <vlc_input_item.h>
33 #include <vlc_events.h>
35 static void media_parse_ended(const libvlc_event_t *event, void *user_data)
37 (void)event;
38 vlc_sem_t *sem = user_data;
39 vlc_sem_post (sem);
42 static void print_media(libvlc_media_t *media)
44 static const libvlc_track_type_t types[] =
45 { libvlc_track_audio, libvlc_track_video, libvlc_track_text,
46 libvlc_track_unknown };
47 size_t nb_tracks = 0;
49 for (size_t i = 0; i < ARRAY_SIZE(types); ++i)
51 const libvlc_track_type_t type = types[i];
52 libvlc_media_tracklist_t *tracklist =
53 libvlc_media_get_tracklist(media, type);
54 assert(tracklist);
56 for (size_t j = 0; j < libvlc_media_tracklist_count(tracklist); ++j)
58 const libvlc_media_track_t *p_track =
59 libvlc_media_tracklist_at(tracklist, j);
61 assert(p_track);
62 assert(p_track->i_type == type);
63 nb_tracks ++;
65 test_log("\ttrack(%zu/%d): codec: %4.4s/%4.4s, ", j, p_track->i_id,
66 (const char *)&p_track->i_codec,
67 (const char *)&p_track->i_original_fourcc);
68 switch (p_track->i_type)
70 case libvlc_track_audio:
71 printf("audio: channels: %u, rate: %u\n",
72 p_track->audio->i_channels, p_track->audio->i_rate);
73 break;
74 case libvlc_track_video:
75 printf("video: %ux%u, sar: %u/%u, fps: %u/%u\n",
76 p_track->video->i_width, p_track->video->i_height,
77 p_track->video->i_sar_num, p_track->video->i_sar_den,
78 p_track->video->i_frame_rate_num, p_track->video->i_frame_rate_den);
79 break;
80 case libvlc_track_text:
81 printf("text: %s\n", p_track->subtitle->psz_encoding);
82 break;
83 case libvlc_track_unknown:
84 printf("unknown\n");
85 break;
86 default:
87 vlc_assert_unreachable();
90 libvlc_media_tracklist_delete(tracklist);
93 if (nb_tracks == 0)
94 test_log("\tmedia doesn't have any tracks\n");
96 for (enum libvlc_meta_t i = libvlc_meta_Title;
97 i <= libvlc_meta_DiscTotal; ++i)
99 char *psz_meta = libvlc_media_get_meta(media, i);
100 if (psz_meta != NULL)
101 test_log("\tmeta(%d): '%s'\n", i, psz_meta);
102 free(psz_meta);
106 static void test_media_preparsed(libvlc_instance_t *vlc, const char *path,
107 const char *location,
108 libvlc_media_parse_flag_t parse_flags,
109 libvlc_media_parsed_status_t i_expected_status)
111 test_log ("test_media_preparsed: %s, expected: %d\n", path ? path : location,
112 i_expected_status);
114 libvlc_media_t *media;
115 if (path != NULL)
116 media = libvlc_media_new_path (vlc, path);
117 else
118 media = libvlc_media_new_location (vlc, location);
119 assert (media != NULL);
121 vlc_sem_t sem;
122 vlc_sem_init (&sem, 0);
124 // Check to see if we are properly receiving the event.
125 libvlc_event_manager_t *em = libvlc_media_event_manager (media);
126 libvlc_event_attach (em, libvlc_MediaParsedChanged, media_parse_ended, &sem);
128 // Parse the media. This is synchronous.
129 int i_ret = libvlc_media_parse_with_options(media, parse_flags, -1);
130 assert(i_ret == 0);
132 // Wait for preparsed event
133 vlc_sem_wait (&sem);
135 // We are good, now check Elementary Stream info.
136 assert (libvlc_media_get_parsed_status(media) == i_expected_status);
137 if (i_expected_status == libvlc_media_parsed_status_done)
138 print_media(media);
140 libvlc_media_release (media);
143 static void input_item_preparse_timeout( input_item_t *item,
144 enum input_item_preparse_status status,
145 void *user_data )
147 VLC_UNUSED(item);
148 vlc_sem_t *p_sem = user_data;
150 assert( status == ITEM_PREPARSE_TIMEOUT );
151 vlc_sem_post(p_sem);
154 static void test_input_metadata_timeout(libvlc_instance_t *vlc, int timeout,
155 int wait_and_cancel)
157 test_log ("test_input_metadata_timeout: timeout: %d, wait_and_cancel: %d ms\n",
158 timeout, wait_and_cancel);
160 int i_ret, p_pipe[2];
161 i_ret = vlc_pipe(p_pipe);
162 assert(i_ret == 0 && p_pipe[1] >= 0);
164 char psz_fd_uri[strlen("fd://") + 11];
165 sprintf(psz_fd_uri, "fd://%u", (unsigned) p_pipe[1]);
166 input_item_t *p_item = input_item_NewFile(psz_fd_uri, "test timeout", 0,
167 ITEM_LOCAL);
168 assert(p_item != NULL);
170 vlc_sem_t sem;
171 vlc_sem_init (&sem, 0);
172 const struct input_preparser_callbacks_t cbs = {
173 .on_preparse_ended = input_item_preparse_timeout,
175 i_ret = libvlc_MetadataRequest(vlc->p_libvlc_int, p_item,
176 META_REQUEST_OPTION_SCOPE_LOCAL |
177 META_REQUEST_OPTION_FETCH_LOCAL,
178 &cbs, &sem, timeout, vlc);
179 assert(i_ret == 0);
181 if (wait_and_cancel > 0)
183 vlc_tick_sleep( VLC_TICK_FROM_MS(wait_and_cancel) );
184 libvlc_MetadataCancel(vlc->p_libvlc_int, vlc);
187 vlc_sem_wait(&sem);
189 input_item_Release(p_item);
190 vlc_close(p_pipe[0]);
191 vlc_close(p_pipe[1]);
194 #define TEST_SUBITEMS_COUNT 6
195 static struct
197 const char *file;
198 libvlc_media_type_t type;
199 } test_media_subitems_list[TEST_SUBITEMS_COUNT] =
201 { "directory", libvlc_media_type_directory, },
202 { "file.jpg", libvlc_media_type_file },
203 { "file.mkv", libvlc_media_type_file },
204 { "file.mp3", libvlc_media_type_file },
205 { "file.png", libvlc_media_type_file },
206 { "file.ts", libvlc_media_type_file },
209 static void subitem_parse_ended(const libvlc_event_t *event, void *user_data)
211 (void)event;
212 vlc_sem_t *sem = user_data;
213 vlc_sem_post (sem);
216 static void subitem_added(const libvlc_event_t *event, void *user_data)
218 #ifdef _WIN32
219 #define FILE_SEPARATOR '\\'
220 #else
221 #define FILE_SEPARATOR '/'
222 #endif
223 bool *subitems_found = user_data;
224 libvlc_media_t *m = event->u.media_subitem_added.new_child;
225 assert (m);
227 char *mrl = libvlc_media_get_mrl (m);
228 assert (mrl);
230 const char *file = strrchr (mrl, FILE_SEPARATOR);
231 assert (file);
232 file++;
233 test_log ("subitem_added, file: %s\n", file);
235 for (unsigned i = 0; i < TEST_SUBITEMS_COUNT; ++i)
237 if (strcmp (test_media_subitems_list[i].file, file) == 0)
239 assert (!subitems_found[i]);
240 assert (libvlc_media_get_type(m) == test_media_subitems_list[i].type);
241 subitems_found[i] = true;
244 free (mrl);
245 #undef FILE_SEPARATOR
248 static void test_media_subitems_media(libvlc_media_t *media, bool play,
249 bool b_items_expected)
251 libvlc_media_add_option(media, ":ignore-filetypes= ");
252 libvlc_media_add_option(media, ":no-sub-autodetect-file");
254 bool subitems_found[TEST_SUBITEMS_COUNT] = { 0 };
255 vlc_sem_t sem;
256 vlc_sem_init (&sem, 0);
258 libvlc_event_manager_t *em = libvlc_media_event_manager (media);
259 libvlc_event_attach (em, libvlc_MediaSubItemAdded, subitem_added, subitems_found);
261 if (play)
263 /* XXX: libvlc_media_parse_with_options won't work with fd, since it
264 * won't be preparsed because fd:// is an unknown type, so play the
265 * file to force parsing. */
266 libvlc_event_attach (em, libvlc_MediaSubItemTreeAdded, subitem_parse_ended, &sem);
268 libvlc_media_player_t *mp = libvlc_media_player_new_from_media (media);
269 assert (mp);
270 assert (libvlc_media_player_play (mp) != -1);
271 vlc_sem_wait (&sem);
272 libvlc_media_player_release (mp);
274 else
276 libvlc_event_attach (em, libvlc_MediaParsedChanged, subitem_parse_ended, &sem);
278 int i_ret = libvlc_media_parse_with_options(media, libvlc_media_parse_local, -1);
279 assert(i_ret == 0);
280 vlc_sem_wait (&sem);
283 if (!b_items_expected)
284 return;
286 for (unsigned i = 0; i < TEST_SUBITEMS_COUNT; ++i)
288 test_log ("test if %s was added\n", test_media_subitems_list[i].file);
289 assert (subitems_found[i]);
293 static void test_media_subitems(libvlc_instance_t *vlc)
295 const char *subitems_path = SRCDIR"/samples/subitems";
297 libvlc_media_t *media;
299 test_log ("Testing media_subitems: path: '%s'\n", subitems_path);
300 media = libvlc_media_new_path (vlc, subitems_path);
301 assert (media != NULL);
302 test_media_subitems_media (media, false, true);
303 libvlc_media_release (media);
305 #define NB_LOCATIONS 2
306 char *subitems_realpath = realpath (subitems_path, NULL);
307 assert (subitems_realpath != NULL);
308 const char *schemes[NB_LOCATIONS] = { "file://", "dir://" };
309 for (unsigned i = 0; i < NB_LOCATIONS; ++i)
311 char *location;
312 assert (asprintf (&location, "%s%s", schemes[i], subitems_realpath) != -1);
313 test_log ("Testing media_subitems: location: '%s'\n", location);
314 media = libvlc_media_new_location (vlc, location);
315 assert (media != NULL);
316 test_media_subitems_media (media, false, true);
317 free (location);
318 libvlc_media_release (media);
320 free (subitems_realpath);
322 #ifdef HAVE_FSTATAT
323 /* listing directory via a fd works only if fstatat() exists */
324 int fd = open (subitems_path, O_RDONLY);
325 test_log ("Testing media_subitems: fd: '%d'\n", fd);
326 assert (fd >= 0);
327 media = libvlc_media_new_fd (vlc, fd);
328 assert (media != NULL);
329 test_media_subitems_media (media, true, true);
330 libvlc_media_release (media);
331 vlc_close (fd);
332 #else
333 #warning not testing subitems list via a fd location
334 #endif
336 test_log ("Testing media_subitems failure\n");
337 media = libvlc_media_new_location (vlc, "wrongfile://test");
338 assert (media != NULL);
339 test_media_subitems_media (media, false, false);
340 libvlc_media_release (media);
343 int main(int i_argc, char *ppsz_argv[])
345 test_init();
347 libvlc_instance_t *vlc = libvlc_new (test_defaults_nargs,
348 test_defaults_args);
349 assert (vlc != NULL);
351 char *psz_test_arg = i_argc > 1 ? ppsz_argv[1] : NULL;
352 if (psz_test_arg != NULL)
354 alarm(0);
355 const char *psz_test_url;
356 const char *psz_test_path;
357 if (strstr(psz_test_arg, "://") != NULL)
359 psz_test_url = psz_test_arg;
360 psz_test_path = NULL;
362 else
364 psz_test_url = NULL;
365 psz_test_path = psz_test_arg;
367 test_media_preparsed (vlc, psz_test_path, psz_test_url,
368 libvlc_media_parse_network,
369 libvlc_media_parsed_status_done);
370 libvlc_release(vlc);
371 return 0;
374 test_media_preparsed (vlc, SRCDIR"/samples/image.jpg", NULL,
375 libvlc_media_parse_local,
376 libvlc_media_parsed_status_done);
377 test_media_preparsed (vlc, NULL, "http://parsing_should_be_skipped.org/video.mp4",
378 libvlc_media_parse_local,
379 libvlc_media_parsed_status_skipped);
380 test_media_preparsed (vlc, NULL, "unknown://parsing_should_be_skipped.org/video.mp4",
381 libvlc_media_parse_local,
382 libvlc_media_parsed_status_skipped);
383 test_media_subitems (vlc);
385 /* Testing libvlc_MetadataRequest timeout and libvlc_MetadataCancel. For
386 * that, we need to create a local input_item_t based on a pipe. There is
387 * no way to do that with a libvlc_media_t, that's why we don't use
388 * libvlc_media_parse*() */
390 test_input_metadata_timeout (vlc, 100, 0);
391 test_input_metadata_timeout (vlc, 0, 100);
393 libvlc_release (vlc);
395 return 0;