src/api/: Use intended license header
[libquvi.git] / src / api / media_stream_select.c
blob7b0c85cd970be9849d1208707c009db016889b3d
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 /** @file media_stream_select.c */
22 #include "config.h"
24 #include <glib.h>
26 #include "quvi.h"
27 /* -- */
28 #include "_quvi_s.h"
29 #include "_quvi_media_s.h"
30 /* -- */
31 #include "misc/re.h"
33 static QuviError _select(_quvi_media_t qm, const gchar *id)
35 gboolean found_flag;
36 QuviError rc;
37 _quvi_t q;
38 gchar **r;
39 gint i;
41 quvi_media_stream_reset(qm);
42 q = qm->handle.quvi;
44 r = g_strsplit(id, ",", 0);
45 found_flag = FALSE;
46 rc = QUVI_OK;
48 for (i=0; r[i] != NULL && found_flag == FALSE; ++i)
50 if (g_strcmp0(r[i], "croak") == 0)
52 g_string_printf(q->status.errmsg,
53 "Nothing in `%s' matched the available media "
54 "streams, aborted by the croak keyword in the list",
55 id);
56 rc = QUVI_ERROR_NO_STREAM_ID_CROAK;
57 break;
59 else if (g_strcmp0(r[i], "best") == 0)
61 quvi_media_stream_choose_best(qm);
62 break;
64 else
66 _quvi_media_stream_t qms;
68 while (quvi_media_stream_next(qm) == TRUE)
70 qms = (_quvi_media_stream_t) qm->curr.stream->data;
72 found_flag = m_match(qms->id->str, r[i]);
73 if (found_flag == TRUE)
74 break;
77 if (found_flag == FALSE) /* Use the first stream as a fallback. */
78 quvi_media_stream_reset(qm);
81 g_strfreev(r);
82 r = NULL;
84 return (rc);
87 /** @brief Select a @ref m_stream matching a @ref m_stream_id
89 Matches the @ref m_stream_id (pattern) to the available media stream
90 IDs and selects the stream if the IDs matched. The function returns
91 immediately if the IDs matched. The ID value may be a
92 comma-separated value (e.g. "foo,bar,baz"). The ID value may
93 contain the keywords 'croak' and 'best' (see notes below).
94 @note
95 - ID value is used as a regular expression pattern
96 - The only exception to this is when it contains a comma-separated
97 list of patterns
98 - ID may contain the keywords 'best' and 'croak'
99 - These are reserved keywords used by both the library and the
100 scripts
101 - The function returns immediately after reaching either
102 - 'croak' will cause the function register an error when the
103 keyword is reached by the function
104 - The error may be checked with @ref quvi_ok and returned with
105 either @ref quvi_get (for code) and @ref quvi_errmsg
106 - 'best' is identical to calling @ref quvi_media_stream_choose_best
107 - If nothing matched (and the 'croak' keyword was not specified) the
108 function falls back to the first available stream (the default)
109 - Always check the result with @ref quvi_ok
110 @sa @ref parse_media
111 @ingroup mediaprop
113 void quvi_media_stream_select(quvi_media_t handle, const char *id)
115 _quvi_media_t qm;
116 _quvi_t q;
118 qm = (_quvi_media_t) handle;
119 q = qm->handle.quvi;
121 /* If G_DISABLE_CHECKS is defined then the check is not performed. */
122 g_return_if_fail(handle != NULL);
124 q->status.rc = _select(qm, id);
127 /* vim: set ts=2 sw=2 tw=72 expandtab: */