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 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 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 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 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( const vlc_event_t
*p_event
,
133 vlc_sem_t
*p_sem
= user_data
;
135 assert( p_event
->u
.input_item_preparse_ended
.new_status
== ITEM_PREPARSE_TIMEOUT
);
139 static void test_input_metadata_timeout(libvlc_instance_t
*vlc
, int timeout
,
142 log ("test_input_metadata_timeout: timeout: %d, wait_and_cancel: %d\n",
143 timeout
, wait_and_cancel
);
145 int i_ret
, p_pipe
[2];
146 i_ret
= vlc_pipe(p_pipe
);
147 assert(i_ret
== 0 && p_pipe
[1] >= 0);
149 char psz_fd_uri
[strlen("fd://") + 11];
150 sprintf(psz_fd_uri
, "fd://%u", (unsigned) p_pipe
[1]);
151 input_item_t
*p_item
= input_item_NewFile(psz_fd_uri
, "test timeout", 0,
153 assert(p_item
!= NULL
);
156 vlc_sem_init (&sem
, 0);
157 i_ret
= vlc_event_attach(&p_item
->event_manager
, vlc_InputItemPreparseEnded
,
158 input_item_preparse_timeout
, &sem
);
160 i_ret
= libvlc_MetadataRequest(vlc
->p_libvlc_int
, p_item
,
161 META_REQUEST_OPTION_SCOPE_LOCAL
, timeout
, vlc
);
164 if (wait_and_cancel
> 0)
166 msleep(wait_and_cancel
* 1000);
167 libvlc_MetadataCancel(vlc
->p_libvlc_int
, vlc
);
172 input_item_Release(p_item
);
173 vlc_sem_destroy(&sem
);
174 vlc_close(p_pipe
[0]);
175 vlc_close(p_pipe
[1]);
178 #define TEST_SUBITEMS_COUNT 6
182 libvlc_media_type_t type
;
183 } test_media_subitems_list
[TEST_SUBITEMS_COUNT
] =
185 { "directory", libvlc_media_type_directory
, },
186 { "file.jpg", libvlc_media_type_file
},
187 { "file.mkv", libvlc_media_type_file
},
188 { "file.mp3", libvlc_media_type_file
},
189 { "file.png", libvlc_media_type_file
},
190 { "file.ts", libvlc_media_type_file
},
193 static void subitem_parse_ended(const libvlc_event_t
*event
, void *user_data
)
196 vlc_sem_t
*sem
= user_data
;
200 static void subitem_added(const libvlc_event_t
*event
, void *user_data
)
203 #define FILE_SEPARATOR '\\'
205 #define FILE_SEPARATOR '/'
207 bool *subitems_found
= user_data
;
208 libvlc_media_t
*m
= event
->u
.media_subitem_added
.new_child
;
211 char *mrl
= libvlc_media_get_mrl (m
);
214 const char *file
= strrchr (mrl
, FILE_SEPARATOR
);
217 log ("subitem_added, file: %s\n", file
);
219 for (unsigned i
= 0; i
< TEST_SUBITEMS_COUNT
; ++i
)
221 if (strcmp (test_media_subitems_list
[i
].file
, file
) == 0)
223 assert (!subitems_found
[i
]);
224 assert (libvlc_media_get_type(m
) == test_media_subitems_list
[i
].type
);
225 subitems_found
[i
] = true;
229 #undef FILE_SEPARATOR
232 static void test_media_subitems_media(libvlc_media_t
*media
, bool play
,
233 bool b_items_expected
)
235 libvlc_media_add_option(media
, ":ignore-filetypes= ");
237 bool subitems_found
[TEST_SUBITEMS_COUNT
] = { 0 };
239 vlc_sem_init (&sem
, 0);
241 libvlc_event_manager_t
*em
= libvlc_media_event_manager (media
);
242 libvlc_event_attach (em
, libvlc_MediaSubItemAdded
, subitem_added
, subitems_found
);
246 /* XXX: libvlc_media_parse_with_options won't work with fd, since it
247 * won't be preparsed because fd:// is an unknown type, so play the
248 * file to force parsing. */
249 libvlc_event_attach (em
, libvlc_MediaSubItemTreeAdded
, subitem_parse_ended
, &sem
);
251 libvlc_media_player_t
*mp
= libvlc_media_player_new_from_media (media
);
253 assert (libvlc_media_player_play (mp
) != -1);
255 libvlc_media_player_release (mp
);
259 libvlc_event_attach (em
, libvlc_MediaParsedChanged
, subitem_parse_ended
, &sem
);
261 int i_ret
= libvlc_media_parse_with_options(media
, libvlc_media_parse_local
, -1);
266 vlc_sem_destroy (&sem
);
268 if (!b_items_expected
)
271 for (unsigned i
= 0; i
< TEST_SUBITEMS_COUNT
; ++i
)
273 log ("test if %s was added\n", test_media_subitems_list
[i
].file
);
274 assert (subitems_found
[i
]);
278 static void test_media_subitems(libvlc_instance_t
*vlc
)
280 const char *subitems_path
= SRCDIR
"/samples/subitems";
282 libvlc_media_t
*media
;
284 log ("Testing media_subitems: path: '%s'\n", subitems_path
);
285 media
= libvlc_media_new_path (vlc
, subitems_path
);
286 assert (media
!= NULL
);
287 test_media_subitems_media (media
, false, true);
288 libvlc_media_release (media
);
290 #define NB_LOCATIONS 2
291 char *subitems_realpath
= realpath (subitems_path
, NULL
);
292 assert (subitems_realpath
!= NULL
);
293 const char *schemes
[NB_LOCATIONS
] = { "file://", "dir://" };
294 for (unsigned i
= 0; i
< NB_LOCATIONS
; ++i
)
297 assert (asprintf (&location
, "%s%s", schemes
[i
], subitems_realpath
) != -1);
298 log ("Testing media_subitems: location: '%s'\n", location
);
299 media
= libvlc_media_new_location (vlc
, location
);
300 assert (media
!= NULL
);
301 test_media_subitems_media (media
, false, true);
303 libvlc_media_release (media
);
305 free (subitems_realpath
);
308 /* listing directory via a fd works only if HAVE_OPENAT is defined */
309 int fd
= open (subitems_path
, O_RDONLY
);
310 log ("Testing media_subitems: fd: '%d'\n", fd
);
312 media
= libvlc_media_new_fd (vlc
, fd
);
313 assert (media
!= NULL
);
314 test_media_subitems_media (media
, true, true);
315 libvlc_media_release (media
);
318 #warning not testing subitems list via a fd location
321 log ("Testing media_subitems failure\n");
322 media
= libvlc_media_new_location (vlc
, "wrongfile://test");
323 assert (media
!= NULL
);
324 test_media_subitems_media (media
, false, false);
325 libvlc_media_release (media
);
328 int main(int i_argc
, char *ppsz_argv
[])
332 libvlc_instance_t
*vlc
= libvlc_new (test_defaults_nargs
,
334 assert (vlc
!= NULL
);
336 char *psz_test_arg
= i_argc
> 1 ? ppsz_argv
[1] : NULL
;
337 if (psz_test_arg
!= NULL
)
340 const char *psz_test_url
;
341 const char *psz_test_path
;
342 if (strstr(psz_test_arg
, "://") != NULL
)
344 psz_test_url
= psz_test_arg
;
345 psz_test_path
= NULL
;
350 psz_test_path
= psz_test_arg
;
352 test_media_preparsed (vlc
, psz_test_path
, psz_test_url
,
353 libvlc_media_parse_network
,
354 libvlc_media_parsed_status_done
);
358 test_media_preparsed (vlc
, SRCDIR
"/samples/image.jpg", NULL
,
359 libvlc_media_parse_local
,
360 libvlc_media_parsed_status_done
);
361 test_media_preparsed (vlc
, NULL
, "http://parsing_should_be_skipped.org/video.mp4",
362 libvlc_media_parse_local
,
363 libvlc_media_parsed_status_skipped
);
364 test_media_preparsed (vlc
, NULL
, "unknown://parsing_should_be_skipped.org/video.mp4",
365 libvlc_media_parse_local
,
366 libvlc_media_parsed_status_skipped
);
367 test_media_subitems (vlc
);
369 /* Testing libvlc_MetadataRequest timeout and libvlc_MetadataCancel. For
370 * that, we need to create a local input_item_t based on a pipe. There is
371 * no way to do that with a libvlc_media_t, that's why we don't use
372 * libvlc_media_parse*() */
374 test_input_metadata_timeout (vlc
, 100, 0);
375 test_input_metadata_timeout (vlc
, 0, 100);
377 libvlc_release (vlc
);