2 * media_player.c - libvlc smoke test
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. *
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. *
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 **********************************************************************/
24 #include "../lib/libvlc_internal.h"
26 #include <sys/types.h>
30 #include <vlc_threads.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
)
38 vlc_sem_t
*sem
= user_data
;
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
};
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
);
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
);
62 assert(p_track
->i_type
== type
);
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
);
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
);
80 case libvlc_track_text
:
81 printf("text: %s\n", p_track
->subtitle
->psz_encoding
);
83 case libvlc_track_unknown
:
87 vlc_assert_unreachable();
90 libvlc_media_tracklist_delete(tracklist
);
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
);
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
,
114 libvlc_media_t
*media
;
116 media
= libvlc_media_new_path (vlc
, path
);
118 media
= libvlc_media_new_location (vlc
, location
);
119 assert (media
!= NULL
);
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);
132 // Wait for preparsed event
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
)
140 libvlc_media_release (media
);
143 static void input_item_preparse_timeout( input_item_t
*item
,
144 enum input_item_preparse_status status
,
148 vlc_sem_t
*p_sem
= user_data
;
150 assert( status
== ITEM_PREPARSE_TIMEOUT
);
154 static void test_input_metadata_timeout(libvlc_instance_t
*vlc
, int timeout
,
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,
168 assert(p_item
!= NULL
);
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
);
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
);
189 input_item_Release(p_item
);
190 vlc_close(p_pipe
[0]);
191 vlc_close(p_pipe
[1]);
194 #define TEST_SUBITEMS_COUNT 6
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
)
212 vlc_sem_t
*sem
= user_data
;
216 static void subitem_added(const libvlc_event_t
*event
, void *user_data
)
219 #define FILE_SEPARATOR '\\'
221 #define FILE_SEPARATOR '/'
223 bool *subitems_found
= user_data
;
224 libvlc_media_t
*m
= event
->u
.media_subitem_added
.new_child
;
227 char *mrl
= libvlc_media_get_mrl (m
);
230 const char *file
= strrchr (mrl
, FILE_SEPARATOR
);
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;
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 };
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
);
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
);
270 assert (libvlc_media_player_play (mp
) != -1);
272 libvlc_media_player_release (mp
);
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);
283 if (!b_items_expected
)
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
)
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);
318 libvlc_media_release (media
);
320 free (subitems_realpath
);
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
);
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
);
333 #warning not testing subitems list via a fd location
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
[])
347 libvlc_instance_t
*vlc
= libvlc_new (test_defaults_nargs
,
349 assert (vlc
!= NULL
);
351 char *psz_test_arg
= i_argc
> 1 ? ppsz_argv
[1] : NULL
;
352 if (psz_test_arg
!= NULL
)
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
;
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
);
373 test_media_preparsed (vlc
, SRCDIR
"/samples/image.jpg", NULL
,
374 libvlc_media_parse_local
,
375 libvlc_media_parsed_status_done
);
376 test_media_preparsed (vlc
, NULL
, "http://parsing_should_be_skipped.org/video.mp4",
377 libvlc_media_parse_local
,
378 libvlc_media_parsed_status_skipped
);
379 test_media_preparsed (vlc
, NULL
, "unknown://parsing_should_be_skipped.org/video.mp4",
380 libvlc_media_parse_local
,
381 libvlc_media_parsed_status_skipped
);
382 test_media_subitems (vlc
);
384 /* Testing libvlc_MetadataRequest timeout and libvlc_MetadataCancel. For
385 * that, we need to create a local input_item_t based on a pipe. There is
386 * no way to do that with a libvlc_media_t, that's why we don't use
387 * libvlc_media_parse*() */
389 test_input_metadata_timeout (vlc
, 100, 0);
390 test_input_metadata_timeout (vlc
, 0, 100);
392 libvlc_release (vlc
);