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