File: media.c

package info (click to toggle)
libquvi 0.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,960 kB
  • sloc: sh: 12,286; ansic: 9,521; makefile: 653; sed: 16
file content (459 lines) | stat: -rw-r--r-- 11,836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* libquvi
 * Copyright (C) 2012,2013  Toni Gundogdu <legatvs@gmail.com>
 *
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General
 * Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/*
 * TODO: When a suitable test URL is found
 * - Add check for QUVI_MEDIA_STREAM_PROPERTY_VIDEO_BITRATE_KBIT_S
 * - Add check for QUVI_MEDIA_STREAM_PROPERTY_AUDIO_BITRATE_KBIT_S
 */

#include "config.h"

#include <string.h>
#include <glib.h>
#include <quvi.h>

#include "tests.h"

static gchar *URLs[] =
{
  "http://www.gaskrank.tv/tv/motorrad-oldtimer/1928-henderson-deluxe-alt-und--19115.htm",
  "http://www.gaskrank.tv/tv/motorrad-oldtimer/1912-harley-davidson-superscho-9481.htm",
  NULL
};

static gchar *TITLEs[] =
{
  "1928 Henderson Deluxe - alt und mit der Patina des Alters aber läuft",
  "1912 Harley Davidson - superschön restauriert",
  NULL
};

#define chk_len(p) \
  do {\
    gchar *s = NULL; \
    quvi_media_get(qm, p, &s); \
    g_assert(s != NULL); \
    g_test_message("%s=%s", #p, s); \
    g_assert_cmpint(strlen(s), >, 1); \
  } while (0)

#define chk_val(p) \
  do {\
    gdouble n = -1; \
    quvi_media_get(qm, p, &n); \
    g_test_message("%s=%f", #p, n); \
    g_assert_cmpfloat(n, >, -1);\
  } while (0)

static void test_media_core()
{
  quvi_media_t qm;
  quvi_t q;
  gchar *s;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URLs[0]);
  g_assert_cmpint(qerr_m(q, URLs[0]), ==, QUVI_OK);
  g_assert(qm != NULL);

  /* Boundary check: the first -1 */
  quvi_media_get(qm, QUVI_MEDIA_PROPERTY_THUMBNAIL_URL-1, &s);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_ERROR_INVALID_ARG);

  /* Boundary check: the last +1 */
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_VIDEO_WIDTH+1, &s);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_ERROR_INVALID_ARG);

  /* string */
  chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL);
  chk_len(QUVI_MEDIA_PROPERTY_TITLE);
  chk_len(QUVI_MEDIA_PROPERTY_ID);

  chk_len(QUVI_MEDIA_STREAM_PROPERTY_URL);

  /* double */
  chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS);
  chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS);

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_multi()
{
  static const gchar URL[] = "http://www.youtube.com/watch?v=G4evlxq34og";

  quvi_media_t qm;
  GString *b;
  gchar *s;
  quvi_t q;
  gint i;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
  g_assert(qm != NULL);

  /* string */
  chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL);
  chk_len(QUVI_MEDIA_PROPERTY_TITLE);
  chk_len(QUVI_MEDIA_PROPERTY_ID);

  /* double */
  chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS);
  chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS);

  b = g_string_new(NULL);
  i = 0;

  while (quvi_media_stream_next(qm) == QUVI_TRUE)
    {
      chk_len(QUVI_MEDIA_STREAM_PROPERTY_URL);
      chk_len(QUVI_MEDIA_STREAM_PROPERTY_ID);

      quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &s);
      g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

      if (b->len >0) /* Make sure each media stream URL is unique */
        g_assert_cmpstr(b->str, !=, s);
      g_string_assign(b, s);

      chk_len(QUVI_MEDIA_STREAM_PROPERTY_CONTAINER);
      quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_CONTAINER, &s);
      g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

      if (strstr(s, "flv") ==NULL) /* FLVs do not come with these */
        {
          chk_len(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_ENCODING);
          chk_len(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING);
        }
      ++i;
    }
  g_assert_cmpint(i, >, 1);

  g_string_free(b, TRUE);
  b = NULL;

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_select()
{
  static const gchar URL[] = "http://www.youtube.com/watch?v=G4evlxq34og";

  quvi_media_t qm;
  gchar *s, *best;
  GSList *curr;
  GSList *ids;
  quvi_t q;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
  g_assert(qm != NULL);

  ids = NULL;
  while (quvi_media_stream_next(qm) == QUVI_TRUE)
    {
      quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
      ids = g_slist_prepend(ids, g_strdup(s));
    }
  ids = g_slist_reverse(ids);

  /*
   * Default stream. This should also advance the current list pointer,
   * so that when quvi_media_stream_next is called the next time, it
   * should return the 2nd stream in the list, not the first one.
   */

  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  curr = g_slist_nth(ids, 0);
  g_assert_cmpstr(curr->data, ==, s);

  quvi_media_stream_next(qm);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  curr = g_slist_nth(ids, 1);
  g_assert_cmpstr(curr->data, ==, s);

  /* Reset. */

  quvi_media_stream_reset(qm);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  curr = g_slist_nth(ids, 0);
  g_assert_cmpstr(curr->data, ==, s);

  /* The best stream, same as the first stream. */

  quvi_media_stream_choose_best(qm);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  curr = g_slist_nth(ids, 0);
  g_assert_cmpstr(curr->data, ==, s);
  best = g_strdup(s);

  /* Select. */

  quvi_media_stream_select(qm, "foo,bar,baz,best,croak");
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  g_assert_cmpstr(s, ==, best);
  g_free(best);

  quvi_media_stream_select(qm, "foo,bar,baz,croak");
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_ERROR_KEYWORD_CROAK);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  curr = g_slist_nth(ids, 0);
  g_assert_cmpstr(curr->data, ==, s);

  quvi_media_stream_select(qm, "foo,bar,baz");
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  curr = g_slist_nth(ids, 0); /* Should be the default stream (first) */
  g_assert_cmpstr(curr->data, ==, s);

  quvi_media_stream_select(qm, "^\\w+_\\w+_i\\d+_\\d40p$,bar,baz,croak");
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  g_assert_cmpstr(s, ==, "medium_webm_i43_240p");

  quvi_media_stream_select(qm, "foo,144p$,baz,croak");
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
  g_assert_cmpstr(s, ==, "small_3gpp_i17_144p");

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_short()
{
  static const gchar URL[] = "http://tinyurl.com/d8s3y54";

  quvi_media_t qm;
  quvi_t q;
  gchar *s;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
  g_assert(qm != NULL);

  quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  g_assert_cmpstr(s, ==, TITLEs[1]);

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_starttime()
{
  static const gchar *URL =
    "http://www.youtube.com/watch?v=LWxTGJ3TK1U#t=2m22s";

  quvi_media_t qm;
  gdouble st = 0;
  quvi_t q;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
  g_assert(qm != NULL);

  quvi_media_get(qm, QUVI_MEDIA_PROPERTY_START_TIME_MS, &st);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  g_assert_cmpint(st, ==, 142000);

  /* YouTube videos should give us this data. */
  chk_len(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING);

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_escaped_url()
{
  static gchar URL[] =
    "http%3A//www.youtube.com/watch%3Fv%3DLWxTGJ3TK1U%23t%3D2m22s";

  quvi_media_t qm;
  quvi_t q;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
  g_assert(qm != NULL);

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_escaped_utf8_url()
{
  static gchar URL[] =
    "http://flix.tapuz.co.il/v/watch-4323227-%D7%9C%D7%A2%D7%A7%D7%95%D7%A8%20%D7%A9%D7%9F%20%D7%9C%D7%98%D7%99%D7%92%D7%A8%D7%99%D7%A1.html";

  quvi_media_t qm;
  quvi_t q;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
  g_assert(qm != NULL);

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_nosupport()
{
  static const gchar URL[] = "http://example.com";

  quvi_media_t qm;
  quvi_t q;
  gchar *s;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  qm = quvi_media_new(q, URL);
  g_assert_cmpint(qerr_m(q, URL), ==, QUVI_ERROR_NO_SUPPORT);
  g_assert(qm != NULL);

  quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
  g_assert_cmpstr(s, ==, "");

  quvi_media_free(qm);
  quvi_free(q);
}

static void test_media_same_q()
{
  quvi_media_t qm;
  gint i = 0;
  quvi_t q;

  if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
    return;

  q = quvi_new();
  g_assert(q != NULL);
  g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);

  chk_verbose(q);

  for (; URLs[i] != NULL; ++i)
    {
      gchar *s = NULL;

      qm = quvi_media_new(q, URLs[i]);
      g_assert_cmpint(qerr_m(q, URLs[i]), ==, QUVI_OK);
      g_assert(qm != NULL);

      quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
      g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
      g_assert_cmpstr(s, ==, TITLEs[i]);

      quvi_media_free(qm);
    }
  quvi_free(q);
}

gint main(gint argc, gchar **argv)
{
  g_test_init(&argc, &argv, NULL);
  g_test_add_func("/quvi/media (core)", test_media_core);
  g_test_add_func("/quvi/media (>1 streams)", test_media_multi);
  g_test_add_func("/quvi/media (select)", test_media_select);
  g_test_add_func("/quvi/media (short)", test_media_short);
  g_test_add_func("/quvi/media (escaped URL)", test_media_escaped_url);
  g_test_add_func("/quvi/media (escaped UTF8 URL)",
                  test_media_escaped_utf8_url);
  g_test_add_func("/quvi/media (nosupport)", test_media_nosupport);
  g_test_add_func("/quvi/media (start_time)", test_media_starttime);
  g_test_add_func("/quvi/media (same handle)", test_media_same_q);
  return (g_test_run());
}

/* vim: set ts=2 sw=2 tw=72 expandtab: */