Blame src/api/media_stream_select.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2012-2013  Toni Gundogdu <legatvs@gmail.com>
Packit 3ff1e7
 *
Packit 3ff1e7
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is free software: you can redistribute it and/or
Packit 3ff1e7
 * modify it under the terms of the GNU Affero General Public
Packit 3ff1e7
 * License as published by the Free Software Foundation, either
Packit 3ff1e7
 * version 3 of the License, or (at your option) any later version.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is distributed in the hope that it will be useful,
Packit 3ff1e7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff1e7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3ff1e7
 * GNU Affero General Public License for more details.
Packit 3ff1e7
 *
Packit 3ff1e7
 * You should have received a copy of the GNU Affero General
Packit 3ff1e7
 * Public License along with this library.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
/** @file media_stream_select.c */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <glib/gi18n-lib.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
#include "_quvi_media_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "misc/re.h"
Packit 3ff1e7
Packit 3ff1e7
static QuviError _select(_quvi_media_t qm, const gchar *id)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_media_stream_t qms;
Packit 3ff1e7
  gboolean found_flag;
Packit 3ff1e7
  QuviError rc;
Packit 3ff1e7
  gchar **r;
Packit 3ff1e7
  gint i;
Packit 3ff1e7
Packit 3ff1e7
  quvi_media_stream_reset(qm);
Packit 3ff1e7
Packit 3ff1e7
  r = g_strsplit(id, ",", 0);
Packit 3ff1e7
  found_flag = FALSE;
Packit 3ff1e7
  rc = QUVI_OK;
Packit 3ff1e7
Packit 3ff1e7
  for (i=0; (r[i] != NULL && found_flag == FALSE); ++i)
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(r[i], "croak") ==0)
Packit 3ff1e7
        {
Packit 3ff1e7
          rc = QUVI_ERROR_KEYWORD_CROAK;
Packit 3ff1e7
          break;
Packit 3ff1e7
        }
Packit 3ff1e7
      else if (g_strcmp0(r[i], "best") == 0)
Packit 3ff1e7
        {
Packit 3ff1e7
          quvi_media_stream_choose_best(qm);
Packit 3ff1e7
          break;
Packit 3ff1e7
        }
Packit 3ff1e7
      else
Packit 3ff1e7
        {
Packit 3ff1e7
          while (quvi_media_stream_next(qm) == QUVI_TRUE)
Packit 3ff1e7
            {
Packit 3ff1e7
              /* TODO: Use quvi_media_get? */
Packit 3ff1e7
              qms = (_quvi_media_stream_t) qm->curr.stream->data;
Packit 3ff1e7
Packit 3ff1e7
              found_flag = m_match(qms->id->str, r[i]);
Packit 3ff1e7
              if (found_flag == TRUE)
Packit 3ff1e7
                break;
Packit 3ff1e7
            }
Packit 3ff1e7
Packit 3ff1e7
          if (found_flag == FALSE) /* Use the first stream as a fallback. */
Packit 3ff1e7
            quvi_media_stream_reset(qm);
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_strfreev(r);
Packit 3ff1e7
  return (rc);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/** @brief Select a @ref m_stream matching a @ref m_stream_id
Packit 3ff1e7
Packit 3ff1e7
Matches the @ref m_stream_id (pattern) to the available media stream
Packit 3ff1e7
IDs and selects the stream. This function returns immediately
Packit 3ff1e7
if a matching ID was found.  The ID value may be a comma-separated value
Packit 3ff1e7
(e.g. "foo,bar,baz"). The ID may also contain the keywords 'croak' and
Packit 3ff1e7
'best' (see the notes below).
Packit 3ff1e7
@note
Packit 3ff1e7
  - ID value is used as regular expression pattern
Packit 3ff1e7
  - ID may contain the reserved keyword 'best'
Packit 3ff1e7
    - Defining this in the ID is identical to calling
Packit 3ff1e7
      @ref quvi_media_stream_choose_best, refer to it for details
Packit 3ff1e7
  - ID may contain the reserved keyword 'croak'
Packit 3ff1e7
    - This will cause the function to exit immediately when it is reached
Packit 3ff1e7
    - The result may be checked with @ref quvi_ok
Packit 3ff1e7
      - The code may be retrieved using @ref quvi_get
Packit 3ff1e7
      - The error message may be retrieved using @ref quvi_errmsg
Packit 3ff1e7
  - If nothing matched (and the 'croak' keyword was specified) the
Packit 3ff1e7
    function will return the first (default) available language
Packit 3ff1e7
  - Always confirm the result with @ref quvi_ok
Packit 3ff1e7
@sa @ref parse_media
Packit 3ff1e7
@ingroup mediaprop
Packit 3ff1e7
*/
Packit 3ff1e7
void quvi_media_stream_select(quvi_media_t handle, const char *id)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_media_t qm;
Packit 3ff1e7
  _quvi_t q;
Packit 3ff1e7
Packit 3ff1e7
  /* If G_DISABLE_CHECKS is defined then the check is not performed. */
Packit 3ff1e7
  g_return_if_fail(handle != NULL);
Packit 3ff1e7
Packit 3ff1e7
  qm = (_quvi_media_t) handle;
Packit 3ff1e7
  q = qm->handle.quvi;
Packit 3ff1e7
Packit 3ff1e7
  q->status.rc = _select(qm, id);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */