]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
* all: use sout_ParseCfg. But "standard" can't use it for sap/slp
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c: standard stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
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
34 #include "announce.h"
35 #include "network.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int      Open    ( vlc_object_t * );
41 static void     Close   ( vlc_object_t * );
42
43 #define SOUT_CFG_PREFIX "sout-standard-"
44
45 vlc_module_begin();
46     set_description( _("Standard stream output") );
47     set_capability( "sout stream", 50 );
48     add_shortcut( "standard" );
49     add_shortcut( "std" );
50
51     add_string( SOUT_CFG_PREFIX "access", "", NULL, "access", "", VLC_TRUE );
52     add_string( SOUT_CFG_PREFIX "mux", "", NULL, "mux", "", VLC_TRUE );
53     add_string( SOUT_CFG_PREFIX "url", "", NULL, "url", "", VLC_TRUE );
54
55     set_callbacks( Open, Close );
56 vlc_module_end();
57
58
59 /*****************************************************************************
60  * Exported prototypes
61  *****************************************************************************/
62 static const char *ppsz_sout_options[] = {
63     "access", "mux", "url", NULL
64 };
65
66 #define DEFAULT_IPV6_SCOPE '8'
67 #define DEFAULT_PORT 1234
68
69 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
70 static int               Del ( sout_stream_t *, sout_stream_id_t * );
71 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
72
73 struct sout_stream_sys_t
74 {
75     sout_mux_t           *p_mux;
76     slp_session_t        *p_slp;
77     session_descriptor_t *p_session;
78 };
79
80 /*****************************************************************************
81  * Open:
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
86     sout_instance_t     *p_sout = p_stream->p_sout;
87     slp_session_t       *p_slp = NULL;
88
89     char *psz_mux;
90     char *psz_access;
91     char *psz_url;
92
93     sout_cfg_t *p_sap_cfg = sout_cfg_find( p_stream->p_cfg, "sap" );
94 #ifdef HAVE_SLP_H
95     sout_cfg_t *p_slp_cfg = sout_cfg_find( p_stream->p_cfg, "slp" );
96 #endif
97
98     vlc_value_t val;
99
100     sout_access_out_t   *p_access;
101     sout_mux_t          *p_mux;
102
103     char                *psz_mux_byext = NULL;
104
105     sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
106
107     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
108     psz_access = *val.psz_string ? val.psz_string : NULL;
109
110     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
111     psz_mux = *val.psz_string ? val.psz_string : NULL;
112
113     var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
114     psz_url = *val.psz_string ? val.psz_string : NULL;
115
116     p_stream->p_sys        = malloc( sizeof( sout_stream_sys_t) );
117     p_stream->p_sys->p_session = NULL;
118
119     msg_Dbg( p_this, "creating `%s/%s://%s'",
120              psz_access, psz_mux, psz_url );
121
122     /* ext -> muxer name */
123     if( psz_url && strrchr( psz_url, '.' ) )
124     {
125         /* by extention */
126         static struct { char *ext; char *mux; } exttomux[] =
127         {
128             { "avi", "avi" },
129             { "ogg", "ogg" },
130             { "ogm", "ogg" },
131             { "mp4", "mp4" },
132             { "mov", "mov" },
133             { "moov","mov" },
134             { "asf", "asf" },
135             { "wma", "asf" },
136             { "wmv", "asf" },
137             { "trp", "ts" },
138             { "ts",  "ts" },
139             { "mpg", "ps" },
140             { "mpeg","ps" },
141             { "ps",  "ps" },
142             { "mpeg1","mpeg1" },
143             { NULL,  NULL }
144         };
145         char *psz_ext = strrchr( psz_url, '.' ) + 1;
146         int  i;
147
148         msg_Dbg( p_this, "extention is %s", psz_ext );
149         for( i = 0; exttomux[i].ext != NULL; i++ )
150         {
151             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
152             {
153                 psz_mux_byext = exttomux[i].mux;
154                 break;
155             }
156         }
157         msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
158     }
159
160     /* We fix access/mux to valid couple */
161
162     if( psz_access == NULL && psz_mux == NULL )
163     {
164         if( psz_mux_byext )
165         {
166             msg_Warn( p_stream,
167                       "no access _and_ no muxer, extention gives file/%s",
168                       psz_mux_byext );
169             psz_access = "file";
170             psz_mux    = psz_mux_byext;
171         }
172         else
173         {
174             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
175             return VLC_EGENERIC;
176         }
177     }
178
179     if( psz_access && psz_mux == NULL )
180     {
181         /* access given, no mux */
182         if( !strncmp( psz_access, "mmsh", 4 ) )
183         {
184             psz_mux = "asfh";
185         }
186         else if( !strncmp( psz_access, "udp", 3 ) )
187         {
188             psz_mux = "ts";
189         }
190         else
191         {
192             psz_mux = psz_mux_byext;
193         }
194     }
195     else if( psz_mux && psz_access == NULL )
196     {
197         /* mux given, no access */
198         if( !strncmp( psz_mux, "asfh", 4 ) )
199         {
200             psz_access = "mmsh";
201         }
202         else
203         {
204             /* default file */
205             psz_access = "file";
206         }
207     }
208
209     /* fix or warm of incompatible couple */
210     if( psz_mux && psz_access )
211     {
212         if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
213         {
214             char *p = strchr( psz_mux,'{' );
215
216             msg_Warn( p_stream, "fixing to mmsh/asfh" );
217             if( p )
218             {
219                 /* -> a little memleak but ... */
220                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
221                 sprintf( psz_mux, "asfh%s", p );
222             }
223             else
224             {
225                 psz_mux = "asfh";
226             }
227         }
228         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
229                    !strncmp( psz_access, "udp", 3 ) ) &&
230                  strncmp( psz_mux, "ts", 2 ) )
231         {
232             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
233         }
234         else if( strncmp( psz_access, "file", 4 ) &&
235                  ( !strncmp( psz_mux, "mov", 3 ) ||
236                    !strncmp( psz_mux, "mp4", 3 ) ) )
237         {
238             msg_Err( p_stream, "mov and mp4 work only with file output" );
239         }
240     }
241
242     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
243
244     /* *** find and open appropriate access module *** */
245     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
246     if( p_access == NULL )
247     {
248         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
249                  psz_access, psz_mux, psz_url );
250         return( VLC_EGENERIC );
251     }
252     msg_Dbg( p_stream, "access opened" );
253
254     /* *** find and open appropriate mux module *** */
255     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
256     if( p_mux == NULL )
257     {
258         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
259                  psz_access, psz_mux, psz_url );
260
261         sout_AccessOutDelete( p_access );
262         return( VLC_EGENERIC );
263     }
264     msg_Dbg( p_stream, "mux opened" );
265
266     /*  *** Create the SAP Session structure *** */
267     if( psz_access &&  p_sap_cfg &&
268         ( strstr( psz_access, "udp" ) || strstr( psz_access ,  "rtp" ) ) )
269     {
270         session_descriptor_t *p_session = sout_AnnounceSessionCreate();
271         announce_method_t *p_method =
272             sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
273         vlc_url_t url;
274
275         /* Parse user input */
276         if( p_sap_cfg->psz_value )
277         {
278             char *psz_sap = p_sap_cfg->psz_value;
279             /* subconfig */
280             if( ! strncmp(psz_sap, "sap{", 4 ) )
281             {
282                 sout_cfg_t *p_cfg;
283                 char *psz_curr,*psz_null;
284                 sout_cfg_parser( &psz_null, &p_cfg, psz_sap );
285                 psz_curr =  sout_cfg_find_value( p_cfg,"name");
286                 if( psz_curr != NULL)
287                 {
288                     p_session->psz_name = strdup( psz_curr );
289                 }
290                 else
291                 {
292                     p_session->psz_name = strdup( psz_url );
293
294                 }
295
296                 psz_curr = sout_cfg_find_value( p_cfg,"ip_version");
297                 if( psz_curr != NULL)
298                 {
299                     p_method->i_ip_version =
300                         atoi( psz_curr ) != 0 ? atoi(psz_curr) : 4;
301                 }
302             }
303             else
304             {
305                 p_session->psz_name = strdup( p_sap_cfg->psz_value );
306             }
307         }
308         else
309         {
310             p_session->psz_name = strdup( psz_url );
311         }
312
313         /* Now, parse the URL to extract host and port */
314         vlc_UrlParse( &url, psz_url , 0);
315
316         if( url.psz_host )
317         {
318             if( url.i_port == 0 )
319             {
320                 url.i_port = DEFAULT_PORT;
321             }
322
323             p_session->psz_uri = url.psz_host;
324             p_session->i_port = url.i_port;
325             p_session->psz_sdp = NULL;
326
327             p_session->i_ttl = config_GetInt( p_sout, "ttl" );
328             p_session->i_payload = 33;
329
330             msg_Info( p_this, "SAP Enabled");
331
332             sout_AnnounceRegister( p_sout, p_session, p_method );
333
334             /* FIXME: Free p_method */
335
336             p_stream->p_sys->p_session = p_session;
337         }
338         vlc_UrlClean( &url );
339     }
340
341     /* *** Register with slp *** */
342 #ifdef HAVE_SLP_H
343     if( p_slp_cfg && ( strstr( psz_access, "udp" ) ||
344                        strstr( psz_access ,  "rtp" ) ) )
345     {
346         msg_Info( p_this, "SLP Enabled");
347         if( sout_SLPReg( p_sout, psz_url,
348             p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url) )
349         {
350            msg_Warn( p_sout, "SLP Registering failed");
351         }
352         else
353         {
354             p_slp = malloc(sizeof(slp_session_t));
355             p_slp->psz_url= strdup(psz_url);
356             p_slp->psz_name = strdup(
357                     p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
358         }
359     }
360 #endif
361
362     p_stream->pf_add    = Add;
363     p_stream->pf_del    = Del;
364     p_stream->pf_send   = Send;
365
366     p_stream->p_sys->p_mux = p_mux;
367     p_stream->p_sys->p_slp = p_slp;
368
369     return VLC_SUCCESS;
370 }
371
372 /*****************************************************************************
373  * Close:
374  *****************************************************************************/
375 static void Close( vlc_object_t * p_this )
376 {
377     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
378     sout_stream_sys_t *p_sys    = p_stream->p_sys;
379     sout_access_out_t *p_access = p_sys->p_mux->p_access;
380
381     if( p_sys->p_session != NULL )
382     {
383         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
384     }
385
386 #ifdef HAVE_SLP_H
387     if( p_sys->p_slp )
388     {
389             sout_SLPDereg( (sout_instance_t *)p_this,
390                         p_sys->p_slp->psz_url,
391                         p_sys->p_slp->psz_name);
392             free( p_sys->p_slp);
393     }
394 #endif
395
396
397     sout_MuxDelete( p_sys->p_mux );
398     sout_AccessOutDelete( p_access );
399
400     free( p_sys );
401 }
402
403 struct sout_stream_id_t
404 {
405     sout_input_t *p_input;
406 };
407
408
409 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
410 {
411     sout_stream_sys_t *p_sys = p_stream->p_sys;
412     sout_stream_id_t  *id;
413
414     id = malloc( sizeof( sout_stream_id_t ) );
415     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
416     {
417         free( id );
418
419         return NULL;
420     }
421
422     return id;
423 }
424
425 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
426 {
427     sout_stream_sys_t *p_sys = p_stream->p_sys;
428
429     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
430
431     free( id );
432
433     return VLC_SUCCESS;
434 }
435
436 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
437                  block_t *p_buffer )
438 {
439     sout_stream_sys_t *p_sys = p_stream->p_sys;
440
441     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
442
443     return VLC_SUCCESS;
444 }