1 /* libquvi
2  * Copyright (C) 2012,2013  Toni Gundogdu <legatvs@gmail.com>
3  *
4  * This file is part of libquvi <http://quvi.sourceforge.net/>.
5  *
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.
10  *
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.
15  *
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/>.
19  */
20 
21 /** @file media_get.c */
22 
23 #include "config.h"
24 
25 #include <glib.h>
26 
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_media_s.h"
31 
32 /* Advances the current stream pointer to the first stream if undefined. */
_chk_curr_stream(_quvi_media_t qm,_quvi_media_stream_t * qms)33 static void _chk_curr_stream(_quvi_media_t qm, _quvi_media_stream_t *qms)
34 {
35   if (qm->curr.stream == NULL)
36     {
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.*/
40     }
41   g_assert(qm->curr.stream != NULL);
42   *qms = (_quvi_media_stream_t) qm->curr.stream->data;
43   g_assert(*qms != NULL);
44 }
45 
_media_get(_quvi_media_t qm,const QuviMediaProperty n,...)46 static QuviError _media_get(_quvi_media_t qm, const QuviMediaProperty n, ...)
47 {
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;
55 
56   va_start(arg, n);
57   type = QUVI_MEDIA_PROPERTY_TYPE_MASK & (gint) n;
58 
59   dp = NULL;
60   sp = NULL;
61   lp = NULL;
62 
63   rc = QUVI_OK;
64   qms = NULL;
65 
66   switch (type)
67     {
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;
86     }
87   va_end(arg);
88 
89   if (rc != QUVI_OK)
90     return (rc);
91 
92   switch (n)
93     {
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;
109 
110       /*
111        * Stream properties.
112        *
113        * NOTE: These must advance current stream pointer if it is still NULL.
114        */
115 
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;
152 
153     default:
154       rc = QUVI_ERROR_INVALID_ARG;
155       break;
156     }
157   return (rc);
158 }
159 
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
170 */
quvi_media_get(quvi_media_t handle,QuviMediaProperty property,...)171 void quvi_media_get(quvi_media_t handle, QuviMediaProperty property, ...)
172 {
173   _quvi_media_t qm;
174   va_list arg;
175   gpointer p;
176   _quvi_t q;
177 
178   /* If G_DISABLE_CHECKS is defined then the check is not performed. */
179   g_return_if_fail(handle != NULL);
180 
181   va_start(arg, property);
182   p = va_arg(arg, gpointer);
183   va_end(arg);
184 
185   qm = (_quvi_media_t) handle;
186   q = qm->handle.quvi;
187 
188   q->status.rc = _media_get(qm, property, p);
189 }
190 
191 /* vim: set ts=2 sw=2 tw=72 expandtab: */
192