API: Return URLs in escaped (percent-encoded) form
[libquvi.git] / src / api / media_get.c
blobf1913e3ad25ed0442b8d25367e53357e2477ef77
1 /* libquvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library 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 library 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 library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 /** @file media_get.c */
23 #include "config.h"
25 #include <glib.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_media_s.h"
32 /* Advances the current stream pointer to the first stream if undefined. */
33 static void _chk_curr_stream(_quvi_media_t qm, _quvi_media_stream_t *qms)
35 if (qm->curr.stream == NULL)
37 const QuviBoolean r = quvi_media_stream_next(qm);
38 g_assert(r == QUVI_TRUE); /* If the stream list is still empty,
39 * quvi_media_get is being used incorrectly.*/
41 g_assert(qm->curr.stream != NULL);
42 *qms = (_quvi_media_stream_t) qm->curr.stream->data;
43 g_assert(*qms != NULL);
46 static QuviError _media_get(_quvi_media_t qm, const QuviMediaProperty n, ...)
48 _quvi_media_stream_t qms;
49 QuviError rc;
50 gdouble *dp;
51 va_list arg;
52 gchar **sp;
53 glong *lp;
54 gint type;
56 va_start(arg, n);
57 type = QUVI_MEDIA_PROPERTY_TYPE_MASK & (gint) n;
59 dp = NULL;
60 sp = NULL;
61 lp = NULL;
63 rc = QUVI_OK;
64 qms = NULL;
66 switch (type)
68 case QUVI_MEDIA_PROPERTY_TYPE_STRING:
69 sp = va_arg(arg, gchar**);
70 if (sp == NULL)
71 rc = QUVI_ERROR_INVALID_ARG;
72 break;
73 case QUVI_MEDIA_PROPERTY_TYPE_LONG:
74 lp = va_arg(arg, glong*);
75 if (lp == NULL)
76 rc = QUVI_ERROR_INVALID_ARG;
77 break;
78 case QUVI_MEDIA_PROPERTY_TYPE_DOUBLE:
79 dp = va_arg(arg, gdouble*);
80 if (dp == NULL)
81 rc = QUVI_ERROR_INVALID_ARG;
82 break;
83 default:
84 rc = QUVI_ERROR_INVALID_ARG;
85 break;
87 va_end(arg);
89 if (rc != QUVI_OK)
90 return (rc);
92 switch (n)
94 case QUVI_MEDIA_PROPERTY_TITLE:
95 *sp = qm->title->str;
96 break;
97 case QUVI_MEDIA_PROPERTY_ID:
98 *sp = qm->id->str;
99 break;
100 case QUVI_MEDIA_PROPERTY_START_TIME_MS:
101 *dp = qm->start_time_ms;
102 break;
103 case QUVI_MEDIA_PROPERTY_THUMBNAIL_URL:
104 *sp = qm->url.thumbnail->str;
105 break;
106 case QUVI_MEDIA_PROPERTY_DURATION_MS:
107 *dp = qm->duration_ms;
108 break;
111 * Stream properties.
113 * NOTE: These must advance current stream pointer if it is still NULL.
116 case QUVI_MEDIA_STREAM_PROPERTY_VIDEO_ENCODING:
117 _chk_curr_stream(qm, &qms);
118 *sp = qms->video.encoding->str;
119 break;
120 case QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING:
121 _chk_curr_stream(qm, &qms);
122 *sp = qms->audio.encoding->str;
123 break;
124 case QUVI_MEDIA_STREAM_PROPERTY_CONTAINER:
125 _chk_curr_stream(qm, &qms);
126 *sp = qms->container->str;
127 break;
128 case QUVI_MEDIA_STREAM_PROPERTY_URL:
129 _chk_curr_stream(qm, &qms);
130 *sp = qms->url->str;
131 break;
132 case QUVI_MEDIA_STREAM_PROPERTY_ID:
133 _chk_curr_stream(qm, &qms);
134 *sp = qms->id->str;
135 break;
136 case QUVI_MEDIA_STREAM_PROPERTY_VIDEO_BITRATE_KBIT_S:
137 _chk_curr_stream(qm, &qms);
138 *dp = qms->video.bitrate_kbit_s;
139 break;
140 case QUVI_MEDIA_STREAM_PROPERTY_AUDIO_BITRATE_KBIT_S:
141 _chk_curr_stream(qm, &qms);
142 *dp = qms->audio.bitrate_kbit_s;
143 break;
144 case QUVI_MEDIA_STREAM_PROPERTY_VIDEO_HEIGHT:
145 _chk_curr_stream(qm, &qms);
146 *dp = qms->video.height;
147 break;
148 case QUVI_MEDIA_STREAM_PROPERTY_VIDEO_WIDTH:
149 _chk_curr_stream(qm, &qms);
150 *dp = qms->video.width;
151 break;
153 default:
154 rc = QUVI_ERROR_INVALID_ARG;
155 break;
157 return (rc);
160 /** @brief Return a media property
161 @sa @ref parse_media
162 @ingroup mediaprop
163 @note
164 - Accessing any of the QUVI_MEDIA_STREAM_PROPERTY_* values using this
165 function will cause the library to advance to the first stream in
166 the list, this will conflict with @ref quvi_media_stream_next,
167 causing @ref quvi_media_stream_next to advance from the second
168 stream, not the first stream
169 - URLs will be returned in the escaped form
171 void quvi_media_get(quvi_media_t handle, QuviMediaProperty property, ...)
173 _quvi_media_t qm;
174 va_list arg;
175 gpointer p;
176 _quvi_t q;
178 /* If G_DISABLE_CHECKS is defined then the check is not performed. */
179 g_return_if_fail(handle != NULL);
181 va_start(arg, property);
182 p = va_arg(arg, gpointer);
183 va_end(arg);
185 qm = (_quvi_media_t) handle;
186 q = qm->handle.quvi;
188 q->status.rc = _media_get(qm, property, p);
191 /* vim: set ts=2 sw=2 tw=72 expandtab: */