]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
s/informations/information/
[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", "std", "file", "http", "udp" )
104     set_category( CAT_SOUT )
105     set_subcategory( SUBCAT_SOUT_STREAM )
106
107     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
108                 ACCESS_LONGTEXT, false )
109     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
110                 MUX_LONGTEXT, false )
111     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
112                 DEST_LONGTEXT, false )
113     add_string( SOUT_CFG_PREFIX "bind", "", NULL, BIND_TEXT,
114                 BIND_LONGTEXT, false )
115     add_string( SOUT_CFG_PREFIX "path", "", NULL, PATH_TEXT,
116                 PATH_LONGTEXT, false )
117
118     add_bool( SOUT_CFG_PREFIX "sap", false, NULL, SAP_TEXT, SAP_LONGTEXT,
119               true )
120     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
121                                         true )
122     add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
123                                         true )
124     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
125                                         true )
126     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
127                                         true )
128     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
129                                         true )
130     add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
131                                         true )
132     add_obsolete_bool( SOUT_CFG_PREFIX "sap-ipv6" )
133
134     set_callbacks( Open, Close )
135 vlc_module_end ()
136
137
138 /*****************************************************************************
139  * Exported prototypes
140  *****************************************************************************/
141 static const char *const ppsz_sout_options[] = {
142     "access", "mux", "url", "dst",
143     "sap", "name", "group", "description", "url", "email", "phone",
144     "bind", "path", NULL
145 };
146
147 #define DEFAULT_PORT 1234
148
149 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
150 static int               Del ( sout_stream_t *, sout_stream_id_t * );
151 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
152
153 struct sout_stream_sys_t
154 {
155     sout_mux_t           *p_mux;
156     session_descriptor_t *p_session;
157 };
158
159 /*****************************************************************************
160  * Open:
161  *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
163 {
164     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
165     sout_instance_t     *p_sout = p_stream->p_sout;
166     sout_stream_sys_t   *p_sys;
167
168     char *psz_mux;
169     char *psz_access;
170     char *psz_url=NULL;
171     char *psz_bind;
172     char *psz_path;
173
174     vlc_value_t val;
175
176     sout_access_out_t   *p_access;
177     sout_mux_t          *p_mux;
178
179     const char          *psz_mux_byext = NULL;
180
181     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
182                    p_stream->p_cfg );
183
184     psz_access = var_GetString( p_stream, SOUT_CFG_PREFIX "access" );
185     if( EMPTY_STR(psz_access) )
186     {
187         if( !strcmp( p_stream->psz_name, "http" ) )
188         {
189             psz_access = strdup("http");
190         }
191         else if (!strcmp (p_stream->psz_name, "udp"))
192         {
193             psz_access = strdup("udp");
194         }
195         else if (!strcmp (p_stream->psz_name, "file"))
196         {
197             psz_access = strdup("file");
198         }
199     }
200
201     psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
202     psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" );
203     psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" );
204
205     if( psz_bind && psz_path )
206     {
207         if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
208             psz_url = NULL;
209     }
210     else if( psz_bind )
211     {
212         psz_url = psz_bind;
213         psz_bind = NULL;
214     }
215     free( psz_path );
216
217     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
218     if( *val.psz_string )
219     {
220         free( psz_url);
221         psz_url = val.psz_string;
222     }
223     else
224         free( val.psz_string );
225
226     p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
227     if( !p_sys )
228     {
229         free( psz_access );
230         free( psz_mux );
231         free( psz_bind );
232         free( psz_url );
233         return VLC_ENOMEM;
234     }
235     p_stream->p_sys->p_session = NULL;
236
237     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
238
239     /* ext -> muxer name */
240     if( psz_url && strrchr( psz_url, '.' ) )
241     {
242         /* by extension */
243         static struct { const char ext[6]; const char mux[32]; } exttomux[] =
244         {
245             { "avi", "avi" },
246             { "ogg", "ogg" },
247             { "ogm", "ogg" },
248             { "ogv", "ogg" },
249             { "mp3", "raw" },
250             { "mp4", "mp4" },
251             { "mov", "mov" },
252             { "moov","mov" },
253             { "asf", "asf" },
254             { "wma", "asf" },
255             { "wmv", "asf" },
256             { "trp", "ts" },
257             { "ts",  "ts" },
258             { "mpg", "ps" },
259             { "mpeg","ps" },
260             { "ps",  "ps" },
261             { "mpeg1","mpeg1" },
262             { "wav", "wav" },
263             { "flv", "ffmpeg{mux=flv}" },
264             { "mkv", "ffmpeg{mux=matroska}"},
265             { "webm", "ffmpeg{mux=webm}"},
266             { "",    "" }
267         };
268         const char *psz_ext = strrchr( psz_url, '.' ) + 1;
269
270         msg_Dbg( p_this, "extension is %s", psz_ext );
271         for( int i = 0; exttomux[i].ext[0]; i++ )
272         {
273             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
274             {
275                 psz_mux_byext = exttomux[i].mux;
276                 break;
277             }
278         }
279         msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
280     }
281
282     /* We fix access/mux to valid couple */
283
284     if( !psz_access && !psz_mux )
285     {
286         if( psz_mux_byext )
287         {
288             msg_Warn( p_stream,
289                       "no access _and_ no muxer, extension gives file/%s",
290                       psz_mux_byext );
291             psz_access = strdup("file");
292             psz_mux    = strdup(psz_mux_byext);
293         }
294         else
295         {
296             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
297             free( psz_bind );
298             free( psz_url );
299             free( p_sys );
300             return VLC_EGENERIC;
301         }
302     }
303
304     if( psz_access && !psz_mux )
305     {
306         /* access given, no mux */
307         if( !strncmp( psz_access, "mmsh", 4 ) )
308         {
309             psz_mux = strdup("asfh");
310         }
311         else if (!strcmp (psz_access, "udp"))
312         {
313             psz_mux = strdup("ts");
314         }
315         else if( psz_mux_byext )
316         {
317             psz_mux = strdup(psz_mux_byext);
318         }
319         else
320         {
321             msg_Err( p_stream, "no mux specified or found by extension" );
322             free( psz_access );
323             free( psz_bind );
324             free( psz_url );
325             free( p_sys );
326             return VLC_EGENERIC;
327         }
328     }
329     else if( psz_mux && !psz_access )
330     {
331         /* mux given, no access */
332         if( !strncmp( psz_mux, "asfh", 4 ) )
333         {
334             psz_access = strdup("mmsh");
335         }
336         else
337         {
338             /* default file */
339             psz_access = strdup("file");
340         }
341     }
342
343     /* fix or warn of incompatible couple */
344     if( !strncmp( psz_access, "mmsh", 4 ) &&
345         strncmp( psz_mux, "asfh", 4 ) )
346     {
347         char *p = strchr( psz_mux,'{' );
348
349         msg_Warn( p_stream, "fixing to mmsh/asfh" );
350         if( p )
351         {
352             if( asprintf( &p, "asfh%s", p ) == -1 )
353                 p = NULL;
354             free( psz_mux );
355             psz_mux = p;
356         }
357         else
358         {
359             free( psz_mux );
360             psz_mux = strdup("asfh");
361         }
362     }
363     else if( !strncmp( psz_access, "udp", 3 ) )
364     {
365         if( !strncmp( psz_mux, "ffmpeg", 6 ) )
366         {   /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
367             char *psz_ffmpeg_mux = var_CreateGetString( p_this, "ffmpeg-mux" );
368             if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
369                 msg_Err( p_stream, "UDP is only valid with TS" );
370             free( psz_ffmpeg_mux );
371         }
372         else if( strncmp( psz_mux, "ts", 2 ) )
373         {
374             msg_Err( p_stream, "UDP is only valid with TS" );
375         }
376     }
377     else if( strncmp( psz_access, "file", 4 ) &&
378              ( !strncmp( psz_mux, "mov", 3 ) ||
379                !strncmp( psz_mux, "mp4", 3 ) ) )
380     {
381         msg_Err( p_stream, "mov and mp4 work only with file output" );
382     }
383
384     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
385
386     /* *** find and open appropriate access module *** */
387     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
388     if( p_access == NULL )
389     {
390         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
391                  psz_access, psz_mux, psz_url );
392         free( psz_access );
393         free( psz_mux );
394         free( psz_bind );
395         free( psz_url );
396         free( p_sys );
397         return VLC_EGENERIC;
398     }
399     msg_Dbg( p_stream, "access opened" );
400
401     /* *** find and open appropriate mux module *** */
402     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
403     if( p_mux == NULL )
404     {
405         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
406                  psz_access, psz_mux, psz_url );
407
408         sout_AccessOutDelete( p_access );
409         free( psz_access );
410         free( psz_mux );
411         free( psz_bind );
412         free( psz_url );
413         free( p_sys );
414         return VLC_EGENERIC;
415     }
416     msg_Dbg( p_stream, "mux opened" );
417
418     /* *** Create the SAP Session structure *** */
419     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
420     {
421         /* Create the SDP */
422         static const struct addrinfo hints = {
423             .ai_family = AF_UNSPEC,
424             .ai_socktype = SOCK_DGRAM,
425             .ai_protocol = 0,
426             .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
427         };
428         char *shost = var_GetNonEmptyString (p_access, "src-addr");
429         char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
430         int sport = var_GetInteger (p_access, "src-port");
431         int dport = var_GetInteger (p_access, "dst-port");
432         struct sockaddr_storage src, dst;
433         socklen_t srclen = 0, dstlen = 0;
434         struct addrinfo *res;
435
436         if ( vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res) == 0)
437         {
438             memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
439             freeaddrinfo (res);
440         }
441
442         if (vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res) == 0)
443         {
444             memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
445             freeaddrinfo (res);
446         }
447
448         char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
449                                     (struct sockaddr *)&src, srclen,
450                                     (struct sockaddr *)&dst, dstlen);
451         free (shost);
452
453         char *psz_sdp = NULL;
454         if (head != NULL)
455         {
456             if (asprintf (&psz_sdp, "%s"
457                           "m=video %d udp mpeg\r\n", head, dport) == -1)
458                 psz_sdp = NULL;
459             free (head);
460         }
461
462         /* Register the SDP with the SAP thread */
463         if (psz_sdp != NULL)
464         {
465             announce_method_t *p_method = sout_SAPMethod ();
466             msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
467
468             p_sys->p_session =
469                 sout_AnnounceRegisterSDP (p_sout, psz_sdp, dhost, p_method);
470             sout_MethodRelease (p_method);
471             free( psz_sdp );
472         }
473         free (dhost);
474     }
475
476     p_stream->pf_add    = Add;
477     p_stream->pf_del    = Del;
478     p_stream->pf_send   = Send;
479
480     p_sys->p_mux = p_mux;
481
482     free( psz_access );
483     free( psz_mux );
484     free( psz_bind );
485     free( psz_url );
486
487     if( !sout_AccessOutCanControlPace( p_access ) )
488         p_sout->i_out_pace_nocontrol++;
489
490     return VLC_SUCCESS;
491 }
492
493 /*****************************************************************************
494  * Close:
495  *****************************************************************************/
496 static void Close( vlc_object_t * p_this )
497 {
498     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
499     sout_stream_sys_t *p_sys    = p_stream->p_sys;
500     sout_access_out_t *p_access = p_sys->p_mux->p_access;
501
502     if( p_sys->p_session != NULL )
503         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
504
505     sout_MuxDelete( p_sys->p_mux );
506     if( !sout_AccessOutCanControlPace( p_access ) )
507         p_stream->p_sout->i_out_pace_nocontrol--;
508     sout_AccessOutDelete( p_access );
509
510     free( p_sys );
511 }
512
513 struct sout_stream_id_t
514 {
515     sout_input_t *p_input;
516 };
517
518
519 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
520 {
521     sout_stream_sys_t *p_sys = p_stream->p_sys;
522     sout_stream_id_t  *id;
523
524     id = malloc( sizeof( sout_stream_id_t ) );
525     if( !id )
526         return NULL;
527
528     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
529     {
530         free( id );
531         return NULL;
532     }
533
534     return id;
535 }
536
537 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
538 {
539     sout_stream_sys_t *p_sys = p_stream->p_sys;
540
541     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
542
543     free( id );
544
545     return VLC_SUCCESS;
546 }
547
548 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
549                  block_t *p_buffer )
550 {
551     sout_stream_sys_t *p_sys = p_stream->p_sys;
552
553     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
554
555     return VLC_SUCCESS;
556 }