]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
* src/input/input.c: fixed memleaks.
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: standard.c,v 1.17 2004/01/15 23:40:44 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/sout.h>
33 #include "announce.h"
34
35 #define DEFAULT_IPV6_SCOPE "8"
36
37 /*****************************************************************************
38  * Exported prototypes
39  *****************************************************************************/
40 static int      Open    ( vlc_object_t * );
41 static void     Close   ( vlc_object_t * );
42
43 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
44 static int               Del ( sout_stream_t *, sout_stream_id_t * );
45 static int               Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 vlc_module_begin();
51     set_description( _("Standard stream") );
52     set_capability( "sout stream", 50 );
53     add_shortcut( "standard" );
54     add_shortcut( "std" );
55     set_callbacks( Open, Close );
56 vlc_module_end();
57
58 struct sout_stream_sys_t
59 {
60     sout_mux_t           *p_mux;
61     slp_session_t        *p_slp;
62     sap_session_t        *p_sap;
63 };
64
65 /*****************************************************************************
66  * Open:
67  *****************************************************************************/
68 static int Open( vlc_object_t *p_this )
69 {
70     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
71     sout_instance_t     *p_sout = p_stream->p_sout;
72     slp_session_t       *p_slp = NULL;
73     sap_session_t       *p_sap = NULL;
74
75     char *psz_mux      = sout_cfg_find_value( p_stream->p_cfg, "mux" );
76     char *psz_access   = sout_cfg_find_value( p_stream->p_cfg, "access" );
77     char *psz_url      = sout_cfg_find_value( p_stream->p_cfg, "url" );
78     char *psz_ipv      = sout_cfg_find_value( p_stream->p_cfg, "sap_ipv" );
79     char *psz_v6_scope = sout_cfg_find_value( p_stream->p_cfg, "sap_v6scope" );
80
81     sout_cfg_t *p_sap_cfg = sout_cfg_find( p_stream->p_cfg, "sap" );
82 #ifdef HAVE_SLP_H
83     sout_cfg_t *p_slp_cfg = sout_cfg_find( p_stream->p_cfg, "slp" );
84 #endif
85
86     sout_access_out_t   *p_access;
87     sout_mux_t          *p_mux;
88
89     char                *psz_mux_byext = NULL;
90
91     msg_Dbg( p_this, "creating `%s/%s://%s'",
92              psz_access, psz_mux, psz_url );
93
94     /* ext -> muxer name */
95     if( psz_url && strrchr( psz_url, '.' ) )
96     {
97         /* by extention */
98         static struct { char *ext; char *mux; } exttomux[] =
99         {
100             { "avi", "avi" },
101             { "ogg", "ogg" },
102             { "ogm", "ogg" },
103             { "mp4", "mp4" },
104             { "mov", "mov" },
105             { "moov","mov" },
106             { "asf", "asf" },
107             { "wma", "asf" },
108             { "wmv", "asf" },
109             { "trp", "ts" },
110             { "ts",  "ts" },
111             { "mpg", "ps" },
112             { "mpeg","ps" },
113             { "ps",  "ps" },
114             { "mpeg1","mpeg1" },
115             { NULL,  NULL }
116         };
117         char *psz_ext = strrchr( psz_url, '.' ) + 1;
118         int  i;
119
120         msg_Dbg( p_this, "extention is %s", psz_ext );
121         for( i = 0; exttomux[i].ext != NULL; i++ )
122         {
123             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
124             {
125                 psz_mux_byext = exttomux[i].mux;
126                 break;
127             }
128         }
129         msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
130     }
131
132     /* We fix access/mux to valid couple */
133
134     if( ( psz_access == NULL || *psz_access == '\0' )&&
135         ( psz_mux == NULL ||  *psz_mux == '\0' ) )
136     {
137         if( psz_mux_byext )
138         {
139             msg_Warn( p_stream,
140                       "no access _and_ no muxer, extention gives file/%s",
141                       psz_mux_byext );
142             psz_access = "file";
143             psz_mux    = psz_mux_byext;
144         }
145         else
146         {
147             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
148             return VLC_EGENERIC;
149         }
150     }
151
152     if( psz_access && *psz_access &&
153         ( psz_mux == NULL || *psz_mux == '\0' ) )
154     {
155         /* access given, no mux */
156         if( !strncmp( psz_access, "mmsh", 4 ) )
157         {
158             psz_mux = "asfh";
159         }
160         else if( !strncmp( psz_access, "udp", 3 ) )
161         {
162             psz_mux = "ts";
163         }
164         else
165         {
166             psz_mux = psz_mux_byext;
167         }
168     }
169     else if( psz_mux && *psz_mux &&
170              ( psz_access == NULL || *psz_access == '\0' ) )
171     {
172         /* mux given, no access */
173         if( !strncmp( psz_mux, "asfh", 4 ) )
174         {
175             psz_access = "mmsh";
176         }
177         else
178         {
179             /* default file */
180             psz_access = "file";
181         }
182     }
183
184     /* fix or warm of incompatible couple */
185     if( psz_mux && *psz_mux && psz_access && *psz_access )
186     {
187         if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
188         {
189             char *p = strchr( psz_mux,'{' );
190
191             msg_Warn( p_stream, "fixing to mmsh/asfh" );
192             if( p )
193             {
194                 /* -> a little memleak but ... */
195                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
196                 sprintf( psz_mux, "asfh%s", p );
197             }
198             else
199             {
200                 psz_mux = "asfh";
201             }
202         }
203         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
204                    !strncmp( psz_access, "udp", 3 ) ) &&
205                  strncmp( psz_mux, "ts", 2 ) )
206         {
207             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
208         }
209         else if( strncmp( psz_access, "file", 4 ) &&
210                  ( !strncmp( psz_mux, "mov", 3 ) ||
211                    !strncmp( psz_mux, "mp4", 3 ) ) )
212         {
213             msg_Err( p_stream, "mov and mp4 work only with file output" );
214         }
215     }
216
217     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
218
219     /* *** find and open appropriate access module *** */
220     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
221     if( p_access == NULL )
222     {
223         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
224                  psz_access, psz_mux, psz_url );
225         return( VLC_EGENERIC );
226     }
227     msg_Dbg( p_stream, "access opened" );
228
229     /* *** find and open appropriate mux module *** */
230     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
231     if( p_mux == NULL )
232     {
233         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
234                  psz_access, psz_mux, psz_url );
235
236         sout_AccessOutDelete( p_access );
237         return( VLC_EGENERIC );
238     }
239     msg_Dbg( p_stream, "mux opened" );
240
241     /*  *** Create the SAP Session structure *** */
242     if( psz_access &&
243         p_sap_cfg &&
244         ( strstr( psz_access, "udp" ) || strstr( psz_access ,  "rtp" ) ) )
245     {
246         msg_Info( p_this, "SAP Enabled");
247
248         if( psz_ipv == NULL )
249         {
250             psz_ipv = "4";
251         }
252         if( psz_v6_scope == NULL )
253         {
254             psz_v6_scope = DEFAULT_IPV6_SCOPE;
255         }
256         msg_Dbg( p_sout , "Creating SAP with IPv%i", atoi(psz_ipv) );
257
258         p_sap = sout_SAPNew( p_sout , psz_url ,
259             p_sap_cfg->psz_value ? p_sap_cfg->psz_value : psz_url,
260             atoi(psz_ipv), psz_v6_scope );
261
262         if( !p_sap )
263             msg_Err( p_sout,"Unable to initialize SAP. SAP disabled");
264     }
265
266     /* *** Register with slp *** */
267 #ifdef HAVE_SLP_H
268     if( p_slp_cfg && ( strstr( psz_access, "udp" ) ||
269                        strstr( psz_access ,  "rtp" ) ) )
270     {
271         msg_Info( p_this, "SLP Enabled");
272         if( sout_SLPReg( p_sout, psz_url,
273             p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url) )
274         {
275            msg_Warn( p_sout, "SLP Registering failed");
276         }
277         else
278         {
279             p_slp = (slp_session_t*)malloc(sizeof(slp_session_t));
280             if(!p_slp)
281             {
282                 msg_Warn(p_sout,"Out of memory");
283                 if( p_sap ) free( p_sap );
284                 return -1;
285             }
286             p_slp->psz_url= strdup(psz_url);
287             p_slp->psz_name = strdup(
288                     p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
289         }
290     }
291 #endif
292
293     /* XXX beurk */
294     p_sout->i_preheader = __MAX( p_sout->i_preheader, p_mux->i_preheader );
295
296     p_stream->pf_add    = Add;
297     p_stream->pf_del    = Del;
298     p_stream->pf_send   = Send;
299
300     p_stream->p_sys        = malloc( sizeof( sout_stream_sys_t) );
301     p_stream->p_sys->p_mux = p_mux;
302     p_stream->p_sys->p_slp = p_slp;
303     p_stream->p_sys->p_sap = p_sap;
304
305     return VLC_SUCCESS;
306 }
307
308 /*****************************************************************************
309  * Close:
310  *****************************************************************************/
311
312 static void Close( vlc_object_t * p_this )
313 {
314     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
315     sout_stream_sys_t *p_sys    = p_stream->p_sys;
316     sout_access_out_t *p_access = p_sys->p_mux->p_access;
317
318     if( p_sys->p_sap )
319         sout_SAPDelete( (sout_instance_t *)p_this , p_sys->p_sap );
320
321 #ifdef HAVE_SLP_H
322     if( p_sys->p_slp )
323     {
324             sout_SLPDereg( (sout_instance_t *)p_this,
325                         p_sys->p_slp->psz_url,
326                         p_sys->p_slp->psz_name);
327             free( p_sys->p_slp);
328     }
329 #endif
330
331
332     sout_MuxDelete( p_sys->p_mux );
333     sout_AccessOutDelete( p_access );
334
335     free( p_sys );
336 }
337
338 struct sout_stream_id_t
339 {
340     sout_input_t *p_input;
341 };
342
343
344 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
345 {
346     sout_stream_sys_t *p_sys = p_stream->p_sys;
347     sout_stream_id_t  *id;
348
349     id = malloc( sizeof( sout_stream_id_t ) );
350     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
351     {
352         free( id );
353
354         return NULL;
355     }
356
357     return id;
358 }
359
360 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
361 {
362     sout_stream_sys_t *p_sys = p_stream->p_sys;
363
364     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
365
366     free( id );
367
368     return VLC_SUCCESS;
369 }
370
371 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
372                  sout_buffer_t *p_buffer )
373 {
374     sout_stream_sys_t *p_sys = p_stream->p_sys;
375     sout_instance_t   *p_sout = p_stream->p_sout;
376
377     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
378
379     if( p_sys->p_sap )
380        sout_SAPSend( p_sout , p_sys->p_sap );
381
382     return VLC_SUCCESS;
383 }