tests/verify.c: chk_val: Test for >0
[libquvi.git] / examples / media.c
blobcf5cd35700587a458a0bbaa3c1604bf04c00f581
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #include "config.h"
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <quvi.h>
28 #include "examples.h"
30 struct _opts_s
32 gboolean autoproxy;
33 gboolean verbose;
34 gboolean best;
35 gchar *stream;
36 gchar **url;
39 static struct _opts_s opts;
41 static const GOptionEntry entries[] =
44 "stream", 's', 0, G_OPTION_ARG_STRING, &opts.stream,
45 "Select stream ID, or a comma-separated list of IDs", "ID"
48 "best", 'b', 0, G_OPTION_ARG_NONE, &opts.best,
49 "Choose the best available stream, negates --stream", NULL
52 "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
53 "Enable the autoproxy feature", NULL
56 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
57 "Verbose libcurl output", NULL
60 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
62 {NULL, 0, 0, 0, NULL, NULL, NULL}
65 static void dump_stream()
67 gchar *url, *id;
69 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &url);
70 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &id);
72 g_print(" id='%s', url='%s'\n", id, url);
75 static void dump_streams()
77 while (quvi_media_stream_next(qm) == QUVI_TRUE)
78 dump_stream();
81 static gint opts_new(gint argc, gchar **argv)
83 GOptionContext *c;
84 GOptionGroup *g;
85 GError *e;
86 gint r;
88 c = g_option_context_new("URL");
89 r = EXIT_SUCCESS;
90 g = NULL;
91 e = NULL;
93 g_option_context_set_help_enabled(c, TRUE);
94 g_option_context_add_main_entries(c, entries, NULL);
96 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
98 g_printerr("error: %s\n", e->message);
99 g_error_free(e);
100 r = EXIT_FAILURE;
101 e = NULL;
104 g_option_context_free(c);
105 c = NULL;
107 /* Check input. */
109 if (opts.url == NULL)
111 g_printerr("error: no input URL\n");
112 return (EXIT_FAILURE);
115 return (r);
118 static void opts_free()
120 g_free(opts.stream);
121 opts.stream = NULL;
123 g_strfreev(opts.url);
124 opts.url = NULL;
127 typedef quvi_callback_status qcs;
129 gint main(gint argc, gchar **argv)
131 gint i,r;
132 gchar *s;
134 g_assert(qm == NULL);
135 g_assert(q == NULL);
137 memset(&opts, 0, sizeof(struct _opts_s));
138 setlocale(LC_ALL, "");
140 r = opts_new(argc, argv);
141 if (r != EXIT_SUCCESS)
142 return (r);
144 q = quvi_new();
145 examples_exit_if_error();
147 if (opts.autoproxy == TRUE)
148 examples_enable_autoproxy();
150 if (opts.verbose == TRUE)
151 examples_enable_verbose();
153 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
155 for (i=0; opts.url[i] != NULL; ++i)
157 qm = quvi_media_new(q, opts.url[i]);
158 examples_exit_if_error();
160 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
161 g_print("[%s]\n title='%s'\n", __func__, s);
163 if (opts.best == TRUE)
165 quvi_media_stream_choose_best(qm);
166 dump_stream();
168 else if (opts.stream != NULL)
170 quvi_media_stream_select(qm, opts.stream);
171 examples_exit_if_error();
172 dump_stream();
174 else
175 dump_streams();
177 quvi_media_free(qm);
178 qm = NULL;
181 opts_free();
182 examples_cleanup();
184 g_assert(qm == NULL);
185 g_assert(q == NULL);
187 return (r);
190 /* vim: set ts=2 sw=2 tw=72 expandtab: */