]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c: standard stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2007 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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifndef _WIN32_WINNT
32 # define _WIN32_WINNT 0x0500
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38
39 #include <vlc_network.h>
40 #include "vlc_url.h"
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 #define ACCESS_TEXT N_("Output access method")
46 #define ACCESS_LONGTEXT N_( \
47     "Output method to use for the stream." )
48 #define MUX_TEXT N_("Output muxer")
49 #define MUX_LONGTEXT N_( \
50     "Muxer to use for the stream." )
51 #define DEST_TEXT N_("Output destination")
52 #define DEST_LONGTEXT N_( \
53     "Destination (URL) to use for the stream. Overrides path and bind parameters" )
54 #define BIND_TEXT N_("address to bind to (helper setting for dst)")
55 #define BIND_LONGTEXT N_( \
56   "address:port to bind vlc to listening incoming streams "\
57   "helper setting for dst,dst=bind+'/'+path. dst-parameter overrides this" )
58 #define PATH_TEXT N_("filename for stream (helper setting for dst)")
59 #define PATH_LONGTEXT N_( \
60   "Filename for stream "\
61   "helper setting for dst, dst=bind+'/'+path, dst-parameter overrides this" )
62 #define NAME_TEXT N_("Session name")
63 #define NAME_LONGTEXT N_( \
64     "This is the name of the session that will be announced in the SDP " \
65     "(Session Descriptor)." )
66
67 #define GROUP_TEXT N_("Session groupname")
68 #define GROUP_LONGTEXT N_( \
69   "This allows you to specify a group for the session, that will be announced "\
70   "if you choose to use SAP." )
71
72 #define DESC_TEXT N_("Session description")
73 #define DESC_LONGTEXT N_( \
74     "This allows you to give a short description with details about the stream, " \
75     "that will be announced in the SDP (Session Descriptor)." )
76 #define URL_TEXT N_("Session URL")
77 #define URL_LONGTEXT N_( \
78     "This allows you to give an URL with more details about the stream " \
79     "(often the website of the streaming organization), that will " \
80     "be announced in the SDP (Session Descriptor)." )
81 #define EMAIL_TEXT N_("Session email")
82 #define EMAIL_LONGTEXT N_( \
83     "This allows you to give a contact mail address for the stream, that will " \
84     "be announced in the SDP (Session Descriptor)." )
85 #define PHONE_TEXT N_("Session phone number")
86 #define PHONE_LONGTEXT N_( \
87     "This allows you to give a contact telephone number for the stream, that will " \
88     "be announced in the SDP (Session Descriptor)." )
89
90
91 #define SAP_TEXT N_("SAP announcing")
92 #define SAP_LONGTEXT N_("Announce this session with SAP.")
93
94 static int      Open    ( vlc_object_t * );
95 static void     Close   ( vlc_object_t * );
96
97 #define SOUT_CFG_PREFIX "sout-standard-"
98
99 vlc_module_begin ()
100     set_shortname( N_("Standard"))
101     set_description( N_("Standard stream output") )
102     set_capability( "sout stream", 50 )
103     add_shortcut( "standard" )
104     add_shortcut( "std" )
105     set_category( CAT_SOUT )
106     set_subcategory( SUBCAT_SOUT_STREAM )
107
108     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
109                 ACCESS_LONGTEXT, false );
110     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
111                 MUX_LONGTEXT, false );
112     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
113                 DEST_LONGTEXT, false );
114     add_string( SOUT_CFG_PREFIX "bind", "", NULL, BIND_TEXT,
115                 BIND_LONGTEXT, false );
116     add_string( SOUT_CFG_PREFIX "path", "", NULL, PATH_TEXT,
117                 PATH_LONGTEXT, false );
118
119     add_bool( SOUT_CFG_PREFIX "sap", false, NULL, SAP_TEXT, SAP_LONGTEXT,
120               true );
121     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
122                                         true );
123     add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
124                                         true );
125     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
126                                         true );
127     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
128                                         true );
129     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
130                                         true );
131     add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
132                                         true );
133     add_obsolete_bool( SOUT_CFG_PREFIX "sap-ipv6" )
134
135     set_callbacks( Open, Close )
136 vlc_module_end ()
137
138
139 /*****************************************************************************
140  * Exported prototypes
141  *****************************************************************************/
142 static const char *const ppsz_sout_options[] = {
143     "access", "mux", "url", "dst",
144     "sap", "name", "group", "description", "url", "email", "phone",
145     "bind", "path", NULL
146 };
147
148 #define DEFAULT_PORT 1234
149
150 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
151 static int               Del ( sout_stream_t *, sout_stream_id_t * );
152 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
153
154 struct sout_stream_sys_t
155 {
156     sout_mux_t           *p_mux;
157     session_descriptor_t *p_session;
158 };
159
160 /*****************************************************************************
161  * Open:
162  *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
164 {
165     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
166     sout_instance_t     *p_sout = p_stream->p_sout;
167     sout_stream_sys_t   *p_sys;
168
169     char *psz_mux;
170     char *psz_access;
171     char *psz_url=NULL;
172     char *psz_bind;
173     char *psz_path;
174
175     vlc_value_t val;
176
177     sout_access_out_t   *p_access;
178     sout_mux_t          *p_mux;
179
180     const char          *psz_mux_byext = NULL;
181
182     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
183                    p_stream->p_cfg );
184
185     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
186     psz_access = *val.psz_string ? val.psz_string : NULL;
187     if( !*val.psz_string ) free( val.psz_string );
188
189     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
190     psz_mux = *val.psz_string ? val.psz_string : NULL;
191     if( !*val.psz_string ) free( val.psz_string );
192
193     var_Get( p_stream, SOUT_CFG_PREFIX "bind", &val );
194     psz_bind = *val.psz_string ? val.psz_string : NULL;
195     if( !*val.psz_string ) free( val.psz_string);
196
197     var_Get( p_stream, SOUT_CFG_PREFIX "path", &val );
198     psz_path = *val.psz_string ? val.psz_string : NULL;
199     if( !*val.psz_string ) free( val.psz_string);
200
201     if( psz_bind ) psz_url = psz_bind;
202     if( psz_url && psz_path ) 
203     {
204         if( asprintf( &psz_url,"%s/%s",psz_url,psz_path ) == -1 )
205             psz_url = NULL;
206         free( psz_path );
207     }
208
209     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
210     if( *val.psz_string ) 
211     {
212         free( psz_url);
213         psz_url = val.psz_string;
214     }
215     else
216         free( val.psz_string );
217
218     p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
219     if( !p_sys )
220     {
221         free( psz_url );
222         return VLC_ENOMEM;
223     }
224     p_stream->p_sys->p_session = NULL;
225
226     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
227
228     /* ext -> muxer name */
229     if( psz_url && strrchr( psz_url, '.' ) )
230     {
231         /* by extension */
232         static struct { const char ext[6]; const char mux[32]; } exttomux[] =
233         {
234             { "avi", "avi" },
235             { "ogg", "ogg" },
236             { "ogm", "ogg" },
237             { "mp4", "mp4" },
238             { "mov", "mov" },
239             { "moov","mov" },
240             { "asf", "asf" },
241             { "wma", "asf" },
242             { "wmv", "asf" },
243             { "trp", "ts" },
244             { "ts",  "ts" },
245             { "mpg", "ps" },
246             { "mpeg","ps" },
247             { "ps",  "ps" },
248             { "mpeg1","mpeg1" },
249             { "wav", "wav" },
250             { "flv", "ffmpeg{mux=flv}" },
251             { "mkv", "ffmpeg{mux=matroska}"},
252             { "",    "" }
253         };
254         const char *psz_ext = strrchr( psz_url, '.' ) + 1;
255         int  i;
256
257         msg_Dbg( p_this, "extension is %s", psz_ext );
258         for( i = 0; exttomux[i].ext[0]; i++ )
259         {
260             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
261             {
262                 psz_mux_byext = exttomux[i].mux;
263                 break;
264             }
265         }
266         msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
267     }
268
269     /* We fix access/mux to valid couple */
270
271     if( !psz_access && !psz_mux )
272     {
273         if( psz_mux_byext )
274         {
275             msg_Warn( p_stream,
276                       "no access _and_ no muxer, extension gives file/%s",
277                       psz_mux_byext );
278             psz_access = strdup("file");
279             psz_mux    = strdup(psz_mux_byext);
280         }
281         else
282         {
283             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
284             free( psz_url );
285             free( p_sys );
286             return VLC_EGENERIC;
287         }
288     }
289
290     if( psz_access && !psz_mux )
291     {
292         /* access given, no mux */
293         if( !strncmp( psz_access, "mmsh", 4 ) )
294         {
295             psz_mux = strdup("asfh");
296         }
297         else if (!strcmp (psz_access, "udp"))
298         {
299             psz_mux = strdup("ts");
300         }
301         else if( psz_mux_byext )
302         {
303             psz_mux = strdup(psz_mux_byext);
304         }
305         else
306         {
307             msg_Err( p_stream, "no mux specified or found by extension" );
308             free( p_sys );
309             return VLC_EGENERIC;
310         }
311     }
312     else if( psz_mux && !psz_access )
313     {
314         /* mux given, no access */
315         if( !strncmp( psz_mux, "asfh", 4 ) )
316         {
317             psz_access = strdup("mmsh");
318         }
319         else
320         {
321             /* default file */
322             psz_access = strdup("file");
323         }
324     }
325
326     /* fix or warn of incompatible couple */
327     if( !strncmp( psz_access, "mmsh", 4 ) &&
328         strncmp( psz_mux, "asfh", 4 ) )
329     {
330         char *p = strchr( psz_mux,'{' );
331
332         msg_Warn( p_stream, "fixing to mmsh/asfh" );
333         if( p )
334         {
335             if( asprintf( &p, "asfh%s", p ) == -1 )
336                 p = NULL;
337             free( psz_mux );
338             psz_mux = p;
339         }
340         else
341         {
342             free( psz_mux );
343             psz_mux = strdup("asfh");
344         }
345     }
346     else if( !strncmp( psz_access, "udp", 3 ) )
347     {
348         if( !strncmp( psz_mux, "ffmpeg", 6 ) )
349         {   /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
350             char *psz_ffmpeg_mux = var_CreateGetString( p_this, "ffmpeg-mux" );
351             if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
352                 msg_Err( p_stream, "UDP is only valid with TS" );
353             free( psz_ffmpeg_mux );
354         }
355         else if( strncmp( psz_mux, "ts", 2 ) )
356         {
357             msg_Err( p_stream, "UDP is only valid with TS" );
358         }
359     }
360     else if( strncmp( psz_access, "file", 4 ) &&
361              ( !strncmp( psz_mux, "mov", 3 ) ||
362                !strncmp( psz_mux, "mp4", 3 ) ) )
363     {
364         msg_Err( p_stream, "mov and mp4 work only with file output" );
365     }
366
367     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
368
369     /* *** find and open appropriate access module *** */
370     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
371     if( p_access == NULL )
372     {
373         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
374                  psz_access, psz_mux, psz_url );
375         free( psz_access );
376         free( psz_mux );
377         free( psz_url );
378         free( p_sys );
379         return VLC_EGENERIC;
380     }
381     msg_Dbg( p_stream, "access opened" );
382
383     /* *** find and open appropriate mux module *** */
384     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
385     if( p_mux == NULL )
386     {
387         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
388                  psz_access, psz_mux, psz_url );
389
390         sout_AccessOutDelete( p_access );
391         free( psz_access );
392         free( psz_mux );
393         free( psz_url );
394         free( p_sys );
395         return VLC_EGENERIC;
396     }
397     msg_Dbg( p_stream, "mux opened" );
398
399     /* *** Create the SAP Session structure *** */
400     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
401     {
402         /* Create the SDP */
403         static const struct addrinfo hints = {
404             .ai_family = AF_UNSPEC,
405             .ai_socktype = SOCK_DGRAM,
406             .ai_protocol = 0,
407             .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
408         };
409         char *shost = var_GetNonEmptyString (p_access, "src-addr");
410         char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
411         int sport = var_GetInteger (p_access, "src-port");
412         int dport = var_GetInteger (p_access, "dst-port");
413         struct sockaddr_storage src, dst;
414         socklen_t srclen = 0, dstlen = 0;
415         struct addrinfo *res;
416
417         if ( vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res) == 0)
418         {
419             memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
420             vlc_freeaddrinfo (res);
421         }
422
423         if (vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res) == 0)
424         {
425             memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
426             vlc_freeaddrinfo (res);
427         }
428
429         char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
430                                     (struct sockaddr *)&src, srclen,
431                                     (struct sockaddr *)&dst, dstlen);
432         free (shost);
433
434         char *psz_sdp = NULL;
435         if (head != NULL)
436         {
437             if (asprintf (&psz_sdp, "%s"
438                           "m=video %d udp mpeg\r\n", head, dport) == -1)
439                 psz_sdp = NULL;
440             free (head);
441         }
442
443         /* Register the SDP with the SAP thread */
444         if (psz_sdp != NULL)
445         {
446             announce_method_t *p_method = sout_SAPMethod ();
447             msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
448
449             p_sys->p_session =
450                 sout_AnnounceRegisterSDP (p_sout, psz_sdp, dhost, p_method);
451             sout_MethodRelease (p_method);
452             free( psz_sdp );
453         }
454         free (dhost);
455     }
456
457     p_stream->pf_add    = Add;
458     p_stream->pf_del    = Del;
459     p_stream->pf_send   = Send;
460
461     p_sys->p_mux = p_mux;
462
463     free( psz_access );
464     free( psz_mux );
465     free( psz_url );
466
467     if( !sout_AccessOutCanControlPace( p_access ) )
468         p_sout->i_out_pace_nocontrol++;
469
470     return VLC_SUCCESS;
471 }
472
473 /*****************************************************************************
474  * Close:
475  *****************************************************************************/
476 static void Close( vlc_object_t * p_this )
477 {
478     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
479     sout_stream_sys_t *p_sys    = p_stream->p_sys;
480     sout_access_out_t *p_access = p_sys->p_mux->p_access;
481
482     if( p_sys->p_session != NULL )
483         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
484
485     sout_MuxDelete( p_sys->p_mux );
486     if( !sout_AccessOutCanControlPace( p_access ) )
487         p_stream->p_sout->i_out_pace_nocontrol--;
488     sout_AccessOutDelete( p_access );
489
490     free( p_sys );
491 }
492
493 struct sout_stream_id_t
494 {
495     sout_input_t *p_input;
496 };
497
498
499 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
500 {
501     sout_stream_sys_t *p_sys = p_stream->p_sys;
502     sout_stream_id_t  *id;
503
504     id = malloc( sizeof( sout_stream_id_t ) );
505     if( !id )
506         return NULL;
507
508     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
509     {
510         free( id );
511         return NULL;
512     }
513
514     return id;
515 }
516
517 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
518 {
519     sout_stream_sys_t *p_sys = p_stream->p_sys;
520
521     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
522
523     free( id );
524
525     return VLC_SUCCESS;
526 }
527
528 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
529                  block_t *p_buffer )
530 {
531     sout_stream_sys_t *p_sys = p_stream->p_sys;
532
533     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
534
535     return VLC_SUCCESS;
536 }