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