2 * media_player.c - libvlc smoke test
7 /**********************************************************************
8 * Copyright (C) 2010 Pierre d'Herbemont. *
9 * This program is free software; you can redistribute and/or modify *
10 * it under the terms of the GNU General Public License as published *
11 * by the Free Software Foundation; version 2 of the license, or (at *
12 * your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17 * See the GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, you can get it from: *
21 * http://www.gnu.org/copyleft/gpl.html *
22 **********************************************************************/
25 #include "../lib/libvlc_internal.h"
27 #include <sys/types.h>
31 #include <vlc_threads.h>
33 #include <vlc_input_item.h>
34 #include <vlc_events.h>
36 static void media_parse_ended(const libvlc_event_t
*event
, void *user_data
)
39 vlc_sem_t
*sem
= user_data
;
43 static void print_media(libvlc_media_t
*media
)
45 libvlc_media_track_t
**pp_tracks
;
46 unsigned i_count
= libvlc_media_tracks_get(media
, &pp_tracks
);
49 for (unsigned i
= 0; i
< i_count
; ++i
)
51 libvlc_media_track_t
*p_track
= pp_tracks
[i
];
52 test_log("\ttrack(%d/%d): codec: %4.4s/%4.4s, ", i
, p_track
->i_id
,
53 (const char *)&p_track
->i_codec
,
54 (const char *)&p_track
->i_original_fourcc
);
55 switch (p_track
->i_type
)
57 case libvlc_track_audio
:
58 printf("audio: channels: %u, rate: %u\n",
59 p_track
->audio
->i_channels
, p_track
->audio
->i_rate
);
61 case libvlc_track_video
:
62 printf("video: %ux%u, sar: %u/%u, fps: %u/%u\n",
63 p_track
->video
->i_width
, p_track
->video
->i_height
,
64 p_track
->video
->i_sar_num
, p_track
->video
->i_sar_den
,
65 p_track
->video
->i_frame_rate_num
, p_track
->video
->i_frame_rate_den
);
67 case libvlc_track_text
:
68 printf("text: %s\n", p_track
->subtitle
->psz_encoding
);
70 case libvlc_track_unknown
:
74 vlc_assert_unreachable();
77 libvlc_media_tracks_release(pp_tracks
, i_count
);
80 test_log("\tmedia doesn't have any tracks\n");
82 for (enum libvlc_meta_t i
= libvlc_meta_Title
;
83 i
<= libvlc_meta_DiscTotal
; ++i
)
85 char *psz_meta
= libvlc_media_get_meta(media
, i
);
87 test_log("\tmeta(%d): '%s'\n", i
, psz_meta
);
92 static void test_media_preparsed(libvlc_instance_t
*vlc
, const char *path
,
94 libvlc_media_parse_flag_t parse_flags
,
95 libvlc_media_parsed_status_t i_expected_status
)
97 test_log ("test_media_preparsed: %s, expected: %d\n", path
? path
: location
,
100 libvlc_media_t
*media
;
102 media
= libvlc_media_new_path (vlc
, path
);
104 media
= libvlc_media_new_location (vlc
, location
);
105 assert (media
!= NULL
);
108 vlc_sem_init (&sem
, 0);
110 // Check to see if we are properly receiving the event.
111 libvlc_event_manager_t
*em
= libvlc_media_event_manager (media
);
112 libvlc_event_attach (em
, libvlc_MediaParsedChanged
, media_parse_ended
, &sem
);
114 // Parse the media. This is synchronous.
115 int i_ret
= libvlc_media_parse_with_options(media
, parse_flags
, -1);
118 // Wait for preparsed event
120 vlc_sem_destroy (&sem
);
122 // We are good, now check Elementary Stream info.
123 assert (libvlc_media_get_parsed_status(media
) == i_expected_status
);
124 if (i_expected_status
== libvlc_media_parsed_status_done
)
127 libvlc_media_release (media
);
130 static void input_item_preparse_timeout( input_item_t
*item
,
131 enum input_item_preparse_status status
,
135 vlc_sem_t
*p_sem
= user_data
;
137 assert( status
== ITEM_PREPARSE_TIMEOUT
);
141 static void test_input_metadata_timeout(libvlc_instance_t
*vlc
, int timeout
,
144 test_log ("test_input_metadata_timeout: timeout: %d, wait_and_cancel: %d ms\n",
145 timeout
, wait_and_cancel
);
147 int i_ret
, p_pipe
[2];
148 i_ret
= vlc_pipe(p_pipe
);
149 assert(i_ret
== 0 && p_pipe
[1] >= 0);
151 char psz_fd_uri
[strlen("fd://") + 11];
152 sprintf(psz_fd_uri
, "fd://%u", (unsigned) p_pipe
[1]);
153 input_item_t
*p_item
= input_item_NewFile(psz_fd_uri
, "test timeout", 0,
155 assert(p_item
!= NULL
);
158 vlc_sem_init (&sem
, 0);
159 const struct input_preparser_callbacks_t cbs
= {
160 .on_preparse_ended
= input_item_preparse_timeout
,
162 i_ret
= libvlc_MetadataRequest(vlc
->p_libvlc_int
, p_item
,
163 META_REQUEST_OPTION_SCOPE_LOCAL
,
164 &cbs
, &sem
, timeout
, vlc
);
167 if (wait_and_cancel
> 0)
169 vlc_tick_sleep( VLC_TICK_FROM_MS(wait_and_cancel
) );
170 libvlc_MetadataCancel(vlc
->p_libvlc_int
, vlc
);
175 input_item_Release(p_item
);
176 vlc_sem_destroy(&sem
);
177 vlc_close(p_pipe
[0]);
178 vlc_close(p_pipe
[1]);
181 #define TEST_SUBITEMS_COUNT 6
185 libvlc_media_type_t type
;
186 } test_media_subitems_list
[TEST_SUBITEMS_COUNT
] =
188 { "directory", libvlc_media_type_directory
, },
189 { "file.jpg", libvlc_media_type_file
},
190 { "file.mkv", libvlc_media_type_file
},
191 { "file.mp3", libvlc_media_type_file
},
192 { "file.png", libvlc_media_type_file
},
193 { "file.ts", libvlc_media_type_file
},
196 static void subitem_parse_ended(const libvlc_event_t
*event
, void *user_data
)
199 vlc_sem_t
*sem
= user_data
;
203 static void subitem_added(const libvlc_event_t
*event
, void *user_data
)
206 #define FILE_SEPARATOR '\\'
208 #define FILE_SEPARATOR '/'
210 bool *subitems_found
= user_data
;
211 libvlc_media_t
*m
= event
->u
.media_subitem_added
.new_child
;
214 char *mrl
= libvlc_media_get_mrl (m
);
217 const char *file
= strrchr (mrl
, FILE_SEPARATOR
);
220 test_log ("subitem_added, file: %s\n", file
);
222 for (unsigned i
= 0; i
< TEST_SUBITEMS_COUNT
; ++i
)
224 if (strcmp (test_media_subitems_list
[i
].file
, file
) == 0)
226 assert (!subitems_found
[i
]);
227 assert (libvlc_media_get_type(m
) == test_media_subitems_list
[i
].type
);
228 subitems_found
[i
] = true;
232 #undef FILE_SEPARATOR
235 static void test_media_subitems_media(libvlc_media_t
*media
, bool play
,
236 bool b_items_expected
)
238 libvlc_media_add_option(media
, ":ignore-filetypes= ");
239 libvlc_media_add_option(media
, ":no-sub-autodetect-file");
241 bool subitems_found
[TEST_SUBITEMS_COUNT
] = { 0 };
243 vlc_sem_init (&sem
, 0);
245 libvlc_event_manager_t
*em
= libvlc_media_event_manager (media
);
246 libvlc_event_attach (em
, libvlc_MediaSubItemAdded
, subitem_added
, subitems_found
);
250 /* XXX: libvlc_media_parse_with_options won't work with fd, since it
251 * won't be preparsed because fd:// is an unknown type, so play the
252 * file to force parsing. */
253 libvlc_event_attach (em
, libvlc_MediaSubItemTreeAdded
, subitem_parse_ended
, &sem
);
255 libvlc_media_player_t
*mp
= libvlc_media_player_new_from_media (media
);
257 assert (libvlc_media_player_play (mp
) != -1);
259 libvlc_media_player_release (mp
);
263 libvlc_event_attach (em
, libvlc_MediaParsedChanged
, subitem_parse_ended
, &sem
);
265 int i_ret
= libvlc_media_parse_with_options(media
, libvlc_media_parse_local
, -1);
270 vlc_sem_destroy (&sem
);
272 if (!b_items_expected
)
275 for (unsigned i
= 0; i
< TEST_SUBITEMS_COUNT
; ++i
)
277 test_log ("test if %s was added\n", test_media_subitems_list
[i
].file
);
278 assert (subitems_found
[i
]);
282 static void test_media_subitems(libvlc_instance_t
*vlc
)
284 const char *subitems_path
= SRCDIR
"/samples/subitems";
286 libvlc_media_t
*media
;
288 test_log ("Testing media_subitems: path: '%s'\n", subitems_path
);
289 media
= libvlc_media_new_path (vlc
, subitems_path
);
290 assert (media
!= NULL
);
291 test_media_subitems_media (media
, false, true);
292 libvlc_media_release (media
);
294 #define NB_LOCATIONS 2
295 char *subitems_realpath
= realpath (subitems_path
, NULL
);
296 assert (subitems_realpath
!= NULL
);
297 const char *schemes
[NB_LOCATIONS
] = { "file://", "dir://" };
298 for (unsigned i
= 0; i
< NB_LOCATIONS
; ++i
)
301 assert (asprintf (&location
, "%s%s", schemes
[i
], subitems_realpath
) != -1);
302 test_log ("Testing media_subitems: location: '%s'\n", location
);
303 media
= libvlc_media_new_location (vlc
, location
);
304 assert (media
!= NULL
);
305 test_media_subitems_media (media
, false, true);
307 libvlc_media_release (media
);
309 free (subitems_realpath
);
312 /* listing directory via a fd works only if HAVE_OPENAT is defined */
313 int fd
= open (subitems_path
, O_RDONLY
);
314 test_log ("Testing media_subitems: fd: '%d'\n", fd
);
316 media
= libvlc_media_new_fd (vlc
, fd
);
317 assert (media
!= NULL
);
318 test_media_subitems_media (media
, true, true);
319 libvlc_media_release (media
);
322 #warning not testing subitems list via a fd location
325 test_log ("Testing media_subitems failure\n");
326 media
= libvlc_media_new_location (vlc
, "wrongfile://test");
327 assert (media
!= NULL
);
328 test_media_subitems_media (media
, false, false);
329 libvlc_media_release (media
);
332 int main(int i_argc
, char *ppsz_argv
[])
336 libvlc_instance_t
*vlc
= libvlc_new (test_defaults_nargs
,
338 assert (vlc
!= NULL
);
340 char *psz_test_arg
= i_argc
> 1 ? ppsz_argv
[1] : NULL
;
341 if (psz_test_arg
!= NULL
)
344 const char *psz_test_url
;
345 const char *psz_test_path
;
346 if (strstr(psz_test_arg
, "://") != NULL
)
348 psz_test_url
= psz_test_arg
;
349 psz_test_path
= NULL
;
354 psz_test_path
= psz_test_arg
;
356 test_media_preparsed (vlc
, psz_test_path
, psz_test_url
,
357 libvlc_media_parse_network
,
358 libvlc_media_parsed_status_done
);
362 test_media_preparsed (vlc
, SRCDIR
"/samples/image.jpg", NULL
,
363 libvlc_media_parse_local
,
364 libvlc_media_parsed_status_done
);
365 test_media_preparsed (vlc
, NULL
, "http://parsing_should_be_skipped.org/video.mp4",
366 libvlc_media_parse_local
,
367 libvlc_media_parsed_status_skipped
);
368 test_media_preparsed (vlc
, NULL
, "unknown://parsing_should_be_skipped.org/video.mp4",
369 libvlc_media_parse_local
,
370 libvlc_media_parsed_status_skipped
);
371 test_media_subitems (vlc
);
373 /* Testing libvlc_MetadataRequest timeout and libvlc_MetadataCancel. For
374 * that, we need to create a local input_item_t based on a pipe. There is
375 * no way to do that with a libvlc_media_t, that's why we don't use
376 * libvlc_media_parse*() */
378 test_input_metadata_timeout (vlc
, 100, 0);
379 test_input_metadata_timeout (vlc
, 0, 100);
381 libvlc_release (vlc
);