Merge branch 'tg/next/1.0__asciidoc.conf' into tg/next/1.0
[libquvi.git] / examples / media.c
blob6acf3ad9da876ba2fe2863dcc4d852d21756bcba
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/>.
21 #include "config.h"
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <quvi.h>
29 #include "examples.h"
31 struct _opts_s
33 gboolean autoproxy;
34 gboolean verbose;
35 gboolean best;
36 gchar *stream;
37 gchar **url;
40 static struct _opts_s opts;
42 static const GOptionEntry entries[] =
45 "stream", 's', 0, G_OPTION_ARG_STRING, &opts.stream,
46 "Select stream ID, or a comma-separated list of IDs", "ID"
49 "best", 'b', 0, G_OPTION_ARG_NONE, &opts.best,
50 "Choose the best available stream, negates --stream", NULL
53 "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
54 "Enable the autoproxy feature", NULL
57 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
58 "Verbose libcurl output", NULL
61 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
63 {NULL, 0, 0, 0, NULL, NULL, NULL}
66 static void dump_stream()
68 gchar *url, *id;
70 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_URL, &url);
71 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &id);
73 g_print(" id='%s', url='%s'\n", id, url);
76 static void dump_streams()
78 while (quvi_media_stream_next(qm) == QUVI_TRUE)
79 dump_stream();
82 static gint opts_new(gint argc, gchar **argv)
84 GOptionContext *c;
85 GError *e;
86 gint r;
88 c = g_option_context_new("URL");
89 r = EXIT_SUCCESS;
90 e = NULL;
92 g_option_context_set_help_enabled(c, TRUE);
93 g_option_context_add_main_entries(c, entries, NULL);
95 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
97 g_printerr("error: %s\n", e->message);
98 g_error_free(e);
99 r = EXIT_FAILURE;
100 e = NULL;
103 g_option_context_free(c);
104 c = NULL;
106 /* Check input. */
108 if (opts.url == NULL)
110 g_printerr("error: no input URL\n");
111 return (EXIT_FAILURE);
114 return (r);
117 static void opts_free()
119 g_free(opts.stream);
120 opts.stream = NULL;
122 g_strfreev(opts.url);
123 opts.url = NULL;
126 typedef quvi_callback_status qcs;
128 gint main(gint argc, gchar **argv)
130 gint i,r;
131 gchar *s;
133 g_assert(qm == NULL);
134 g_assert(q == NULL);
136 memset(&opts, 0, sizeof(struct _opts_s));
137 setlocale(LC_ALL, "");
139 r = opts_new(argc, argv);
140 if (r != EXIT_SUCCESS)
141 return (r);
143 q = quvi_new();
144 examples_exit_if_error();
146 if (opts.autoproxy == TRUE)
147 examples_enable_autoproxy();
149 if (opts.verbose == TRUE)
150 examples_enable_verbose();
152 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
154 for (i=0; opts.url[i] != NULL; ++i)
156 qm = quvi_media_new(q, opts.url[i]);
157 examples_exit_if_error();
159 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
160 g_print("[%s]\n title='%s'\n", __func__, s);
162 if (opts.best == TRUE)
164 quvi_media_stream_choose_best(qm);
165 dump_stream();
167 else if (opts.stream != NULL)
169 quvi_media_stream_select(qm, opts.stream);
170 examples_exit_if_error();
171 dump_stream();
173 else
174 dump_streams();
176 quvi_media_free(qm);
177 qm = NULL;
180 opts_free();
181 examples_cleanup();
183 g_assert(qm == NULL);
184 g_assert(q == NULL);
186 return (r);
189 /* vim: set ts=2 sw=2 tw=72 expandtab: */