/*****************************************************************************
 * sdi.c: Serial Digital Interface
 *****************************************************************************
 * Copyright (C) 2010 M2X BV
 * $Id: b8bf3f2eff69feab05d889f478a63170c306d107 $
 *
 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_demux.h>

#if defined(HAVE_SDI_BMSDK_API)
#include "sdi_declink.h"
#endif

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open  (vlc_object_t *);
static void Close (vlc_object_t *);

#define SDI_CARD_TEXT N_("SDI Card name (default declink)")
#define SDI_CARD_LONGTEXT N_( \
    "Specific the name of SDI card to use. Currently supported cards are: " \
    "declink")

static const char *const ppsz_sdi_card_list[] = {
    "declink"
};

#define SDI "sdi-"

vlc_module_begin()
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_DEMUX)
    set_description(N_("SDI demuxer"))
    set_capability("demux", 10)
    set_callbacks(Open, Close)
    add_shortcut("sdi")

    add_string(SDI"card", NULL, NULL, SDI_CARD_TEXT, SDI_CARD_LONGTEXT, true)
        change_string_list( ppsz_sdi_card_list, ppsz_sdi_card_list, 0 );

vlc_module_end()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

struct demux_sys_t
{
    es_out_id_t     *p_es;

    char *psz_card;

    /* card specific options */
#if defined(HAVE_SDI_BMSDK_API)
    IDeckLinkIterator *deckLinkIterator;
#endif
};

/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open(vlc_object_t *p_this)
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = NULL;

    p_demux->p_sys = p_sys = calloc(1, sizeof(demux_sys_t));
    if (!p_sys)
       return VLC_ENOMEM;

    p_sys->p_es = NULL;

    p_sys->psz_card = var_GetString(p_demux, "card");
    if( !p_sys->psz_card )
    {
        msg_Err(p_demux, "No SDI card specified.");
        goto error_card;
    }

    /* use cardspecific routines */
    if (strncmp(p_sys->psz_card. "declink") == 0)
    {
        p_demux->pf_demux = declink_Demux;
        p_demux->pf_control = declink_Control;
        if (declink_Open(p_demux) != VLC_SUCCESS)
            goto error_card;
    }

    return VLC_SUCCESS;

error_card:
    free(p_sys->psz_card);
    free(p_sys);
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

    if (strncmp(p_sys->psz_card. "declink") == 0)
    {
        declink_Close(p_this);
    }

    free(p_sys->psz_card);
    free(p_sys);
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Other:
 *****************************************************************************/
