quvi_set: Add support for QUVI_OPTION_CALLBACK_STATUS_USERDATA
[libquvi.git] / src / api / media_stream_select.c
blob62eb9c3a4dc9e4b5092138d54a7262fc0069b984
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_stream_select.c */
23 #include "config.h"
25 #include <glib/gi18n-lib.h>
26 #include <glib.h>
28 #include "quvi.h"
29 /* -- */
30 #include "_quvi_s.h"
31 #include "_quvi_media_s.h"
32 /* -- */
33 #include "misc/re.h"
35 static QuviError _select(_quvi_media_t qm, const gchar *id)
37 _quvi_media_stream_t qms;
38 gboolean found_flag;
39 QuviError rc;
40 gchar **r;
41 gint i;
43 quvi_media_stream_reset(qm);
45 r = g_strsplit(id, ",", 0);
46 found_flag = FALSE;
47 rc = QUVI_OK;
49 for (i=0; (r[i] != NULL && found_flag == FALSE); ++i)
51 if (g_strcmp0(r[i], "croak") ==0)
53 rc = QUVI_ERROR_KEYWORD_CROAK;
54 break;
56 else if (g_strcmp0(r[i], "best") == 0)
58 quvi_media_stream_choose_best(qm);
59 break;
61 else
63 while (quvi_media_stream_next(qm) == QUVI_TRUE)
65 /* TODO: Use quvi_media_get? */
66 qms = (_quvi_media_stream_t) qm->curr.stream->data;
68 found_flag = m_match(qms->id->str, r[i]);
69 if (found_flag == TRUE)
70 break;
73 if (found_flag == FALSE) /* Use the first stream as a fallback. */
74 quvi_media_stream_reset(qm);
77 g_strfreev(r);
78 return (rc);
81 /** @brief Select a @ref m_stream matching a @ref m_stream_id
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
104 void quvi_media_stream_select(quvi_media_t handle, const char *id)
106 _quvi_media_t qm;
107 _quvi_t q;
109 /* If G_DISABLE_CHECKS is defined then the check is not performed. */
110 g_return_if_fail(handle != NULL);
112 qm = (_quvi_media_t) handle;
113 q = qm->handle.quvi;
115 q->status.rc = _select(qm, id);
118 /* vim: set ts=2 sw=2 tw=72 expandtab: */