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_stream_select.c */
22
23 #include "config.h"
24
25 #include <glib/gi18n-lib.h>
26 #include <glib.h>
27
28 #include "quvi.h"
29 /* -- */
30 #include "_quvi_s.h"
31 #include "_quvi_media_s.h"
32 /* -- */
33 #include "misc/re.h"
34
_select(_quvi_media_t qm,const gchar * id)35 static QuviError _select(_quvi_media_t qm, const gchar *id)
36 {
37 _quvi_media_stream_t qms;
38 gboolean found_flag;
39 QuviError rc;
40 gchar **r;
41 gint i;
42
43 quvi_media_stream_reset(qm);
44
45 r = g_strsplit(id, ",", 0);
46 found_flag = FALSE;
47 rc = QUVI_OK;
48
49 for (i=0; (r[i] != NULL && found_flag == FALSE); ++i)
50 {
51 if (g_strcmp0(r[i], "croak") ==0)
52 {
53 rc = QUVI_ERROR_KEYWORD_CROAK;
54 break;
55 }
56 else if (g_strcmp0(r[i], "best") == 0)
57 {
58 quvi_media_stream_choose_best(qm);
59 break;
60 }
61 else
62 {
63 while (quvi_media_stream_next(qm) == QUVI_TRUE)
64 {
65 /* TODO: Use quvi_media_get? */
66 qms = (_quvi_media_stream_t) qm->curr.stream->data;
67
68 found_flag = m_match(qms->id->str, r[i]);
69 if (found_flag == TRUE)
70 break;
71 }
72
73 if (found_flag == FALSE) /* Use the first stream as a fallback. */
74 quvi_media_stream_reset(qm);
75 }
76 }
77 g_strfreev(r);
78 return (rc);
79 }
80
81 /** @brief Select a @ref m_stream matching a @ref m_stream_id
82
83 Matches the @ref m_stream_id (pattern) to the available media stream
84 IDs and selects the stream. This function returns immediately
85 if a matching ID was found. The ID value may be a comma-separated value
86 (e.g. "foo,bar,baz"). The ID may also contain the keywords 'croak' and
87 'best' (see the notes below).
88 @note
89 - ID value is used as regular expression pattern
90 - ID may contain the reserved keyword 'best'
91 - Defining this in the ID is identical to calling
92 @ref quvi_media_stream_choose_best, refer to it for details
93 - ID may contain the reserved keyword 'croak'
94 - This will cause the function to exit immediately when it is reached
95 - The result may be checked with @ref quvi_ok
96 - The code may be retrieved using @ref quvi_get
97 - The error message may be retrieved using @ref quvi_errmsg
98 - If nothing matched (and the 'croak' keyword was specified) the
99 function will return the first (default) available language
100 - Always confirm the result with @ref quvi_ok
101 @sa @ref parse_media
102 @ingroup mediaprop
103 */
quvi_media_stream_select(quvi_media_t handle,const char * id)104 void quvi_media_stream_select(quvi_media_t handle, const char *id)
105 {
106 _quvi_media_t qm;
107 _quvi_t q;
108
109 /* If G_DISABLE_CHECKS is defined then the check is not performed. */
110 g_return_if_fail(handle != NULL);
111
112 qm = (_quvi_media_t) handle;
113 q = qm->handle.quvi;
114
115 q->status.rc = _select(qm, id);
116 }
117
118 /* vim: set ts=2 sw=2 tw=72 expandtab: */
119