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