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