standard.c 7.93 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * standard.c
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
5
 * $Id: standard.c,v 1.12 2003/08/14 20:02:55 zorglub Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <string.h>
29
#include <unistd.h>
30 31 32

#include <vlc/vlc.h>
#include <vlc/sout.h>
33
#include "announce.h"
34

35 36
#define DEFAULT_IPV6_SCOPE "8"

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/
static int      Open    ( vlc_object_t * );
static void     Close   ( vlc_object_t * );

static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
static int               Del ( sout_stream_t *, sout_stream_id_t * );
static int               Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("Standard stream") );
    set_capability( "sout stream", 50 );
    add_shortcut( "standard" );
    add_shortcut( "std" );
    set_callbacks( Open, Close );
vlc_module_end();

struct sout_stream_sys_t
{
60
    sout_mux_t           *p_mux;
61
    slp_session_t        *p_slp;
62
    sap_session_t        *p_sap;
63 64 65 66 67 68 69 70 71
};

/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    sout_stream_t       *p_stream = (sout_stream_t*)p_this;
    sout_instance_t     *p_sout = p_stream->p_sout;
72
    sout_stream_sys_t   *p_sys = malloc( sizeof( sout_stream_sys_t) );
73

74 75 76 77 78
    char *psz_mux      = sout_cfg_find_value( p_stream->p_cfg, "mux" );
    char *psz_access   = sout_cfg_find_value( p_stream->p_cfg, "access" );
    char *psz_url      = sout_cfg_find_value( p_stream->p_cfg, "url" );
    char *psz_ipv      = sout_cfg_find_value( p_stream->p_cfg, "sap_ipv" );
    char *psz_v6_scope = sout_cfg_find_value( p_stream->p_cfg, "sap_v6scope" );
79
    
80
    sout_cfg_t *p_sap_cfg = sout_cfg_find( p_stream->p_cfg, "sap" );
Gildas Bazin's avatar
 
Gildas Bazin committed
81
#ifdef HAVE_SLP_H
82
    sout_cfg_t *p_slp_cfg = sout_cfg_find( p_stream->p_cfg, "slp" );
Gildas Bazin's avatar
 
Gildas Bazin committed
83
#endif
84
    
85 86 87 88 89 90 91 92 93 94
    sout_access_out_t   *p_access;
    sout_mux_t          *p_mux;    

    /* SAP is only valid for UDP or RTP streaming */
    if( psz_access == NULL ) psz_access = "udp";

    /* Get SAP IP version to use */
    if(psz_ipv == NULL) psz_ipv = "4";
    if(psz_v6_scope == NULL) psz_v6_scope = DEFAULT_IPV6_SCOPE;
    p_sys->p_sap = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
95
    p_sys->p_slp = NULL;
96 97

    msg_Dbg( p_this, "creating `%s/%s://%s'",
98
             psz_access, psz_mux, psz_url );
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    /* *** find and open appropriate access module *** */
    p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
    if( p_access == NULL )
    {
        msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
                 psz_access, psz_mux, psz_url );
        return( VLC_EGENERIC );
    }
    msg_Dbg( p_stream, "access opened" );

    /* *** find and open appropriate mux module *** */
    p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
    if( p_mux == NULL )
    {
        msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
                 psz_access, psz_mux, psz_url );

        sout_AccessOutDelete( p_access );
        return( VLC_EGENERIC );
    }
    msg_Dbg( p_stream, "mux opened" );

122
    /*  *** Create the SAP Session structure *** */
123 124 125 126 127 128 129 130 131 132 133 134
    if( p_sap_cfg && ( strstr( psz_access, "udp" ) ||
                       strstr( psz_access ,  "rtp" ) ) )
    {
        msg_Info( p_this, "SAP Enabled");
        msg_Dbg( p_sout , "Creating SAP with IPv%i", atoi(psz_ipv) );

        p_sys->p_sap = sout_SAPNew( p_sout , psz_url ,
            p_sap_cfg->psz_value ? p_sap_cfg->psz_value : psz_url,
            atoi(psz_ipv), psz_v6_scope );

        if( !p_sys->p_sap )
            msg_Err( p_sout,"Unable to initialize SAP. SAP disabled");
135
    }   
136

137
    /* *** Register with slp *** */
Gildas Bazin's avatar
 
Gildas Bazin committed
138
#ifdef HAVE_SLP_H
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    if( p_slp_cfg && ( strstr( psz_access, "udp" ) ||
                       strstr( psz_access ,  "rtp" ) ) )
    {
        msg_Info( p_this, "SLP Enabled");
        if( sout_SLPReg( p_sout, psz_url, 
            p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url) )
        {
           msg_Warn( p_sout, "SLP Registering failed");
        }
        else
        {
            p_sys->p_slp = (slp_session_t*)malloc(sizeof(slp_session_t));
            if(!p_sys->p_slp)
            {
                msg_Warn(p_sout,"Out of memory");
                return -1;        
            }
            p_sys->p_slp->psz_url= strdup(psz_url);
            p_sys->p_slp->psz_name = strdup(
                    p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
        }
    }        
Gildas Bazin's avatar
 
Gildas Bazin committed
161
#endif
162
    
163 164 165 166
    /* XXX beurk */
    p_sout->i_preheader = __MAX( p_sout->i_preheader, p_mux->i_preheader );


167
    p_sys->p_mux = p_mux;
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    p_stream->pf_add    = Add;
    p_stream->pf_del    = Del;
    p_stream->pf_send   = Send;

    p_stream->p_sys     = p_sys;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/

static void Close( vlc_object_t * p_this )
{
    sout_stream_t     *p_stream = (sout_stream_t*)p_this;
184
    sout_stream_sys_t *p_sys    = p_stream->p_sys;
185 186
    sout_access_out_t *p_access = p_sys->p_mux->p_access;

187 188
    if( p_sys->p_sap )
        sout_SAPDelete( (sout_instance_t *)p_this , p_sys->p_sap ); 
189

Gildas Bazin's avatar
 
Gildas Bazin committed
190
#ifdef HAVE_SLP_H
191 192 193 194 195 196 197
    if( p_sys->p_slp )
    {
            sout_SLPDereg( (sout_instance_t *)p_this, 
                        p_sys->p_slp->psz_url,
                        p_sys->p_slp->psz_name);
            free( p_sys->p_slp);
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
198
#endif
199 200
    
    
201 202 203 204 205 206 207 208 209 210 211 212
    sout_MuxDelete( p_sys->p_mux );
    sout_AccessOutDelete( p_access );

    free( p_sys );
}

struct sout_stream_id_t
{
    sout_input_t *p_input;
};


213
static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    sout_stream_id_t  *id;

    id = malloc( sizeof( sout_stream_id_t ) );
    if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
    {
        free( id );

        return NULL;
    }

    return id;
}

229
static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
230 231 232 233 234 235 236 237 238 239
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;

    sout_MuxDeleteStream( p_sys->p_mux, id->p_input );

    free( id );

    return VLC_SUCCESS;
}

240 241
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
                 sout_buffer_t *p_buffer )
242 243
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
244
    sout_instance_t   *p_sout = p_stream->p_sout;
245 246 247

    sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );

248
    if( p_sys->p_sap )
249
       sout_SAPSend( p_sout , p_sys->p_sap );
250

251 252
    return VLC_SUCCESS;
}