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