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