doc: Add parse_subtitle page
[libquvi.git] / tests / media.c
blob7f3497899fbc1da8ebf313302434d41236d514d2
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
22 * TODO: When a suitable test URL is found
23 * - Add check for QUVI_MEDIA_STREAM_PROPERTY_VIDEO_BITRATE_KBIT_S
24 * - Add check for QUVI_MEDIA_STREAM_PROPERTY_AUDIO_BITRATE_KBIT_S
27 #include "config.h"
29 #include <string.h>
30 #include <glib.h>
31 #include <quvi.h>
33 #include "tests.h"
35 static gchar *URLs[] =
37 "http://www.gaskrank.tv/tv/motorrad-oldtimer/1928-henderson-deluxe-alt-und--19115.htm",
38 "http://www.gaskrank.tv/tv/motorrad-oldtimer/1912-harley-davidson-superscho-9481.htm",
39 NULL
42 static gchar *TITLEs[] =
44 "1928 Henderson Deluxe - alt und mit der Patina des Alters aber läuft",
45 "1912 Harley Davidson - superschön restauriert",
46 NULL
49 #define chk_len(p) \
50 do {\
51 gchar *s = NULL; \
52 quvi_media_get(qm, p, &s); \
53 g_assert(s != NULL); \
54 g_test_message("%s=%s", #p, s); \
55 g_assert_cmpint(strlen(s), >, 1); \
56 } while (0)
58 #define chk_val(p) \
59 do {\
60 gdouble n = -1; \
61 quvi_media_get(qm, p, &n); \
62 g_test_message("%s=%f", #p, n); \
63 g_assert_cmpfloat(n, >, -1);\
64 } while (0)
66 static void test_media_core()
68 quvi_media_t qm;
69 quvi_t q;
70 gchar *s;
72 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
73 return;
75 q = quvi_new();
76 g_assert(q != NULL);
77 g_assert_cmpint(qerr(q), ==, QUVI_OK);
79 chk_verbose(q);
81 qm = quvi_media_new(q, URLs[0]);
82 g_assert_cmpint(qerr_m(q, URLs[0]), ==, QUVI_OK);
83 g_assert(qm != NULL);
85 /* Boundary check: the first -1 */
86 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_THUMBNAIL_URL-1, &s);
87 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_INVALID_ARG);
89 /* Boundary check: the last +1 */
90 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_VIDEO_WIDTH+1, &s);
91 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_INVALID_ARG);
93 /* string */
94 chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL);
95 chk_len(QUVI_MEDIA_PROPERTY_TITLE);
96 chk_len(QUVI_MEDIA_PROPERTY_ID);
98 chk_len(QUVI_MEDIA_STREAM_PROPERTY_URL);
100 /* double */
101 chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS);
102 chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS);
104 quvi_media_free(qm);
105 quvi_free(q);
108 static void test_media_multi()
110 static const gchar URL[] =
111 "http://www.dailymotion.com/video/xm8t99_king-kong-vs-godzilla_shortfilms";
113 quvi_media_t qm;
114 GString *b;
115 gchar *s;
116 quvi_t q;
117 gint i;
119 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
120 return;
122 q = quvi_new();
123 g_assert(q != NULL);
124 g_assert_cmpint(qerr(q), ==, QUVI_OK);
126 chk_verbose(q);
128 qm = quvi_media_new(q, URL);
129 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
130 g_assert(qm != NULL);
132 /* string */
133 chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL);
134 chk_len(QUVI_MEDIA_PROPERTY_TITLE);
135 chk_len(QUVI_MEDIA_PROPERTY_ID);
137 /* double */
138 chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS);
139 chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS);
141 b = g_string_new(NULL);
142 i = 0;
144 while (quvi_media_stream_next(qm) == QUVI_TRUE)
146 chk_len(QUVI_MEDIA_STREAM_PROPERTY_URL);
147 chk_len(QUVI_MEDIA_STREAM_PROPERTY_ID);
149 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &s);
150 g_assert_cmpint(qerr(q), ==, QUVI_OK);
152 if (b->len >0) /* Make sure each media stream URL is unique */
153 g_assert_cmpstr(b->str, !=, s);
155 g_string_assign(b, s);
157 chk_len(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_ENCODING);
158 chk_len(QUVI_MEDIA_STREAM_PROPERTY_CONTAINER);
160 chk_val(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_HEIGHT);
161 chk_val(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_WIDTH);
163 ++i;
165 g_assert_cmpint(i, >, 1);
167 g_string_free(b, TRUE);
168 b = NULL;
170 quvi_media_free(qm);
171 quvi_free(q);
174 static void test_media_select()
176 static const gchar URL[] =
177 "http://www.dailymotion.com/video/xm8t99_king-kong-vs-godzilla_shortfilms";
179 quvi_media_t qm;
180 GSList *curr;
181 GSList *ids;
182 gchar *s;
183 quvi_t q;
185 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
186 return;
188 q = quvi_new();
189 g_assert(q != NULL);
190 g_assert_cmpint(qerr(q), ==, QUVI_OK);
192 chk_verbose(q);
194 qm = quvi_media_new(q, URL);
195 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
196 g_assert(qm != NULL);
198 ids = NULL;
199 while (quvi_media_stream_next(qm) == QUVI_TRUE)
201 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
202 ids = g_slist_prepend(ids, g_strdup(s));
204 ids = g_slist_reverse(ids);
206 /* Default stream. This should also advance the current list pointer,
207 * so that when quvi_media_stream_next is called the next time, it
208 * should return the 2nd stream in the list, not the first one. */
210 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
211 curr = g_slist_nth(ids, 0);
212 g_assert_cmpstr(curr->data, ==, s);
214 quvi_media_stream_next(qm);
215 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
216 curr = g_slist_nth(ids, 1);
217 g_assert_cmpstr(curr->data, ==, s);
219 /* Reset. */
221 quvi_media_stream_reset(qm);
222 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
223 curr = g_slist_nth(ids, 0);
224 g_assert_cmpstr(curr->data, ==, s);
226 /* Best stream. Assumes this to be the last. */
228 quvi_media_stream_choose_best(qm);
229 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
230 curr = g_slist_last(ids);
231 g_assert_cmpstr(curr->data, ==, s);
233 /* Select. */
235 quvi_media_stream_select(qm, "foo,bar,baz,best,croak");
236 g_assert_cmpint(qerr(q), ==, QUVI_OK);
237 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
238 curr = g_slist_last(ids); /* Assumes the best is the last. */
239 g_assert_cmpstr(curr->data, ==, s);
241 quvi_media_stream_select(qm, "foo,bar,baz,croak");
242 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_KEYWORD_CROAK);
243 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
244 curr = g_slist_nth(ids, 0);
245 g_assert_cmpstr(curr->data, ==, s);
247 quvi_media_stream_select(qm, "foo,bar,baz");
248 g_assert_cmpint(qerr(q), ==, QUVI_OK);
249 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
250 curr = g_slist_nth(ids, 0); /* Should be the default stream (first) */
251 g_assert_cmpstr(curr->data, ==, s);
253 quvi_media_stream_select(qm, "^\\w\\w_\\w+_\\w+_\\d40p$,bar,baz,croak");
254 g_assert_cmpint(qerr(q), ==, QUVI_OK);
255 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
256 curr = g_slist_nth(ids, 0); /* Should be "ld_mp4_h264_240p". */
257 g_assert_cmpstr(curr->data, ==, s);
259 quvi_media_stream_select(qm, "foo,^\\w\\w_\\w+_\\w+_\\d+4p$,baz,croak");
260 g_assert_cmpint(qerr(q), ==, QUVI_OK);
261 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
262 curr = g_slist_nth(ids, 1); /* Should be "sd_mp4_h264_384p". */
263 g_assert_cmpstr(curr->data, ==, s);
265 quvi_media_free(qm);
266 quvi_free(q);
269 static void test_media_short()
271 static const gchar URL[] = "http://tinyurl.com/d8s3y54";
273 quvi_media_t qm;
274 quvi_t q;
275 gchar *s;
277 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
278 return;
280 q = quvi_new();
281 g_assert(q != NULL);
282 g_assert_cmpint(qerr(q), ==, QUVI_OK);
284 chk_verbose(q);
286 qm = quvi_media_new(q, URL);
287 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
288 g_assert(qm != NULL);
290 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
291 g_assert_cmpint(qerr(q), ==, QUVI_OK);
292 g_assert_cmpstr(s, ==, TITLEs[1]);
294 quvi_media_free(qm);
295 quvi_free(q);
298 static void test_media_starttime()
300 static const gchar *URL =
301 "http://www.youtube.com/watch?v=LWxTGJ3TK1U#t=2m22s";
303 quvi_media_t qm;
304 gdouble st = 0;
305 quvi_t q;
307 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
308 return;
310 q = quvi_new();
311 g_assert(q != NULL);
312 g_assert_cmpint(qerr(q), ==, QUVI_OK);
314 chk_verbose(q);
316 qm = quvi_media_new(q, URL);
317 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
318 g_assert(qm != NULL);
320 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_START_TIME_MS, &st);
321 g_assert_cmpint(qerr(q), ==, QUVI_OK);
322 g_assert_cmpint(st, ==, 142000);
324 /* YouTube videos should give us this data. */
325 chk_len(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING);
327 quvi_media_free(qm);
328 quvi_free(q);
331 static void test_media_escaped_url()
333 static gchar URL[] =
334 "http://youtube.com/watch%3Fv%3DG4evlxq34og%23t%3D3m20s";
336 quvi_media_t qm;
337 quvi_t q;
339 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
340 return;
342 q = quvi_new();
343 g_assert(q != NULL);
344 g_assert_cmpint(qerr(q), ==, QUVI_OK);
346 chk_verbose(q);
348 qm = quvi_media_new(q, URL);
349 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
350 g_assert(qm != NULL);
352 quvi_media_free(qm);
353 quvi_free(q);
356 static void test_media_nosupport()
358 static const gchar URL[] = "http://example.com";
360 quvi_media_t qm;
361 quvi_t q;
362 gchar *s;
364 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
365 return;
367 q = quvi_new();
368 g_assert(q != NULL);
369 g_assert_cmpint(qerr(q), ==, QUVI_OK);
371 chk_verbose(q);
373 qm = quvi_media_new(q, URL);
374 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_ERROR_NO_SUPPORT);
375 g_assert(qm != NULL);
377 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
378 g_assert_cmpint(qerr(q), ==, QUVI_OK);
379 g_assert_cmpstr(s, ==, "");
381 quvi_media_free(qm);
382 quvi_free(q);
385 static void test_media_same_q()
387 quvi_media_t qm;
388 gint i = 0;
389 quvi_t q;
391 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
392 return;
394 q = quvi_new();
395 g_assert(q != NULL);
396 g_assert_cmpint(qerr(q), ==, QUVI_OK);
398 chk_verbose(q);
400 for (; URLs[i] != NULL; ++i)
402 gchar *s = NULL;
404 qm = quvi_media_new(q, URLs[i]);
405 g_assert_cmpint(qerr_m(q, URLs[i]), ==, QUVI_OK);
406 g_assert(qm != NULL);
408 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
409 g_assert_cmpint(qerr(q), ==, QUVI_OK);
410 g_assert_cmpstr(s, ==, TITLEs[i]);
412 quvi_media_free(qm);
414 quvi_free(q);
417 gint main(gint argc, gchar **argv)
419 g_test_init(&argc, &argv, NULL);
420 g_test_add_func("/quvi/media (core)", test_media_core);
421 g_test_add_func("/quvi/media (>1 streams)", test_media_multi);
422 g_test_add_func("/quvi/media (select)", test_media_select);
423 g_test_add_func("/quvi/media (short)", test_media_short);
424 g_test_add_func("/quvi/media (escaped URL)", test_media_escaped_url);
425 g_test_add_func("/quvi/media (nosupport)", test_media_nosupport);
426 g_test_add_func("/quvi/media (start_time)", test_media_starttime);
427 g_test_add_func("/quvi/media (same handle)", test_media_same_q);
428 return (g_test_run());
431 /* vim: set ts=2 sw=2 tw=72 expandtab: */