1 /*****************************************************************************
2 * standard.c: standard stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 # define _WIN32_WINNT 0x0500
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
39 #include <vlc_network.h>
42 /*****************************************************************************
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)." )
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." )
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)." )
91 #define SAP_TEXT N_("SAP announcing")
92 #define SAP_LONGTEXT N_("Announce this session with SAP.")
94 static int Open ( vlc_object_t * );
95 static void Close ( vlc_object_t * );
97 #define SOUT_CFG_PREFIX "sout-standard-"
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 )
107 add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT,
108 ACCESS_LONGTEXT, false )
109 add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT,
110 MUX_LONGTEXT, false )
111 add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT,
112 DEST_LONGTEXT, false )
113 add_string( SOUT_CFG_PREFIX "bind", "", BIND_TEXT,
114 BIND_LONGTEXT, false )
115 add_string( SOUT_CFG_PREFIX "path", "", PATH_TEXT,
116 PATH_LONGTEXT, false )
118 add_bool( SOUT_CFG_PREFIX "sap", false, SAP_TEXT, SAP_LONGTEXT,
120 add_string( SOUT_CFG_PREFIX "name", "", NAME_TEXT, NAME_LONGTEXT,
122 add_string( SOUT_CFG_PREFIX "group", "", GROUP_TEXT, GROUP_LONGTEXT,
124 add_string( SOUT_CFG_PREFIX "description", "", DESC_TEXT, DESC_LONGTEXT,
126 add_string( SOUT_CFG_PREFIX "url", "", URL_TEXT, URL_LONGTEXT,
128 add_string( SOUT_CFG_PREFIX "email", "", EMAIL_TEXT, EMAIL_LONGTEXT,
130 add_string( SOUT_CFG_PREFIX "phone", "", PHONE_TEXT, PHONE_LONGTEXT,
132 add_obsolete_bool( SOUT_CFG_PREFIX "sap-ipv6" )
134 set_callbacks( Open, Close )
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",
147 #define DEFAULT_PORT 1234
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* );
153 struct sout_stream_sys_t
156 session_descriptor_t *p_session;
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
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;
176 sout_access_out_t *p_access;
179 const char *psz_mux_byext = NULL;
181 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
184 psz_access = var_GetString( p_stream, SOUT_CFG_PREFIX "access" );
185 if( EMPTY_STR(psz_access) )
187 if( !strcmp( p_stream->psz_name, "http" ) )
189 psz_access = strdup("http");
191 else if (!strcmp (p_stream->psz_name, "udp"))
193 psz_access = strdup("udp");
195 else if (!strcmp (p_stream->psz_name, "file"))
197 psz_access = strdup("file");
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" );
205 if( psz_bind && psz_path )
207 if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
217 var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
218 if( *val.psz_string )
221 psz_url = val.psz_string;
224 free( val.psz_string );
226 p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
235 p_stream->p_sys->p_session = NULL;
237 msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
239 /* ext -> muxer name */
240 if( psz_url && strrchr( psz_url, '.' ) )
243 static struct { const char ext[6]; const char mux[32]; } exttomux[] =
264 { "flv", "ffmpeg{mux=flv}" },
265 { "mkv", "ffmpeg{mux=matroska}"},
266 { "webm", "ffmpeg{mux=webm}"},
269 const char *psz_ext = strrchr( psz_url, '.' ) + 1;
271 msg_Dbg( p_this, "extension is %s", psz_ext );
272 for( int i = 0; exttomux[i].ext[0]; i++ )
274 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
276 psz_mux_byext = exttomux[i].mux;
280 msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
283 /* We fix access/mux to valid couple */
285 if( !psz_access && !psz_mux )
290 "no access _and_ no muxer, extension gives file/%s",
292 psz_access = strdup("file");
293 psz_mux = strdup(psz_mux_byext);
297 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
305 if( psz_access && !psz_mux )
307 /* access given, no mux */
308 if( !strncmp( psz_access, "mmsh", 4 ) )
310 psz_mux = strdup("asfh");
312 else if (!strcmp (psz_access, "udp"))
314 psz_mux = strdup("ts");
316 else if( psz_mux_byext )
318 psz_mux = strdup(psz_mux_byext);
322 msg_Err( p_stream, "no mux specified or found by extension" );
330 else if( psz_mux && !psz_access )
332 /* mux given, no access */
333 if( !strncmp( psz_mux, "asfh", 4 ) )
335 psz_access = strdup("mmsh");
340 psz_access = strdup("file");
344 /* fix or warn of incompatible couple */
345 if( !strncmp( psz_access, "mmsh", 4 ) &&
346 strncmp( psz_mux, "asfh", 4 ) )
348 char *p = strchr( psz_mux,'{' );
350 msg_Warn( p_stream, "fixing to mmsh/asfh" );
353 if( asprintf( &p, "asfh%s", p ) == -1 )
361 psz_mux = strdup("asfh");
364 else if( !strncmp( psz_access, "udp", 3 ) )
366 if( !strncmp( psz_mux, "ffmpeg", 6 ) )
367 { /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
368 char *psz_ffmpeg_mux = var_CreateGetString( p_this, "ffmpeg-mux" );
369 if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
370 msg_Err( p_stream, "UDP is only valid with TS" );
371 free( psz_ffmpeg_mux );
373 else if( strncmp( psz_mux, "ts", 2 ) )
375 msg_Err( p_stream, "UDP is only valid with TS" );
378 else if( strncmp( psz_access, "file", 4 ) &&
379 ( !strncmp( psz_mux, "mov", 3 ) ||
380 !strncmp( psz_mux, "mp4", 3 ) ) )
382 msg_Err( p_stream, "mov and mp4 work only with file output" );
385 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
387 /* *** find and open appropriate access module *** */
388 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
389 if( p_access == NULL )
391 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
392 psz_access, psz_mux, psz_url );
400 msg_Dbg( p_stream, "access opened" );
402 /* *** find and open appropriate mux module *** */
403 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
406 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
407 psz_access, psz_mux, psz_url );
409 sout_AccessOutDelete( p_access );
417 msg_Dbg( p_stream, "mux opened" );
419 /* *** Create the SAP Session structure *** */
420 if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
423 static const struct addrinfo hints = {
424 .ai_family = AF_UNSPEC,
425 .ai_socktype = SOCK_DGRAM,
427 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
429 char *shost = var_GetNonEmptyString (p_access, "src-addr");
430 char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
431 int sport = var_GetInteger (p_access, "src-port");
432 int dport = var_GetInteger (p_access, "dst-port");
433 struct sockaddr_storage src, dst;
434 socklen_t srclen = 0, dstlen = 0;
435 struct addrinfo *res;
437 if ( vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res) == 0)
439 memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
443 if (vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res) == 0)
445 memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
449 char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
450 (struct sockaddr *)&src, srclen,
451 (struct sockaddr *)&dst, dstlen);
454 char *psz_sdp = NULL;
457 if (asprintf (&psz_sdp, "%s"
458 "m=video %d udp mpeg\r\n", head, dport) == -1)
463 /* Register the SDP with the SAP thread */
466 msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
468 sout_AnnounceRegisterSDP (p_sout, psz_sdp, dhost);
474 p_stream->pf_add = Add;
475 p_stream->pf_del = Del;
476 p_stream->pf_send = Send;
478 p_sys->p_mux = p_mux;
485 if( !sout_AccessOutCanControlPace( p_access ) )
486 p_sout->i_out_pace_nocontrol++;
491 /*****************************************************************************
493 *****************************************************************************/
494 static void Close( vlc_object_t * p_this )
496 sout_stream_t *p_stream = (sout_stream_t*)p_this;
497 sout_stream_sys_t *p_sys = p_stream->p_sys;
498 sout_access_out_t *p_access = p_sys->p_mux->p_access;
500 if( p_sys->p_session != NULL )
501 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
503 sout_MuxDelete( p_sys->p_mux );
504 if( !sout_AccessOutCanControlPace( p_access ) )
505 p_stream->p_sout->i_out_pace_nocontrol--;
506 sout_AccessOutDelete( p_access );
511 struct sout_stream_id_t
513 sout_input_t *p_input;
517 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
519 sout_stream_sys_t *p_sys = p_stream->p_sys;
520 sout_stream_id_t *id;
522 id = malloc( sizeof( sout_stream_id_t ) );
526 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
535 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
537 sout_stream_sys_t *p_sys = p_stream->p_sys;
539 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
546 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
549 sout_stream_sys_t *p_sys = p_stream->p_sys;
551 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );