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 *****************************************************************************/
34 #include <vlc_network.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 #define ACCESS_TEXT N_("Output access method")
41 /// \bug [String] "Output method to use for the stream."
42 #define ACCESS_LONGTEXT N_( \
43 "This is the output access method that will be used." )
44 #define MUX_TEXT N_("Output muxer")
45 /// \bug [String] "Muxer to use for the stream."
46 #define MUX_LONGTEXT N_( \
47 "This is the muxer that will be used." )
48 #define DST_TEXT N_("Output destination")
49 /// \bug [String] "Destination (URL) to use for the stream."
50 #define DST_LONGTEXT N_( \
51 "This is the destination (URL) that will be used for the stream." )
52 #define NAME_TEXT N_("Session name")
53 #define NAME_LONGTEXT N_( \
54 "This allows you to specify a name for the session, that will be announced "\
55 "if you choose to use SAP." )
57 #define GROUP_TEXT N_("Session groupname")
58 #define GROUP_LONGTEXT N_( \
59 "This allows you to specify a group for the session, that will be announced "\
60 "if you choose to use SAP." )
62 #define DESC_TEXT N_("Session descriptipn")
63 #define DESC_LONGTEXT N_( \
64 "This allows you to give a short description with details about the stream, " \
65 "that will be announced in the SDP (Session Descriptor)." )
66 #define URL_TEXT N_("Session URL")
67 #define URL_LONGTEXT N_( \
68 "This allows you to give an URL with more details about the stream " \
69 "(often the website of the streaming organization), that will " \
70 "be announced in the SDP (Session Descriptor)." )
71 #define EMAIL_TEXT N_("Session email")
72 #define EMAIL_LONGTEXT N_( \
73 "This allows you to give a contact mail address for the stream, that will " \
74 "be announced in the SDP (Session Descriptor)." )
75 #define PHONE_TEXT N_("Session phone number")
76 #define PHONE_LONGTEXT N_( \
77 "This allows you to give a contact telephone number for the stream, that will " \
78 "be announced in the SDP (Session Descriptor)." )
81 #define SAP_TEXT N_("SAP announcing")
82 #define SAP_LONGTEXT N_("Announce this session with SAP.")
84 static int Open ( vlc_object_t * );
85 static void Close ( vlc_object_t * );
87 #define SOUT_CFG_PREFIX "sout-standard-"
90 set_shortname( _("Standard"));
91 set_description( _("Standard stream output") );
92 set_capability( "sout stream", 50 );
93 add_shortcut( "standard" );
94 add_shortcut( "std" );
95 set_category( CAT_SOUT );
96 set_subcategory( SUBCAT_SOUT_STREAM );
98 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
99 ACCESS_LONGTEXT, VLC_FALSE );
100 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
101 MUX_LONGTEXT, VLC_FALSE );
102 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
103 DST_LONGTEXT, VLC_FALSE );
105 add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
106 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
108 add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
110 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
112 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
114 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
116 add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
118 add_suppressed_bool( SOUT_CFG_PREFIX "sap-ipv6" );
120 set_callbacks( Open, Close );
124 /*****************************************************************************
125 * Exported prototypes
126 *****************************************************************************/
127 static const char *ppsz_sout_options[] = {
128 "access", "mux", "url", "dst",
129 "sap", "name", "group", "description", "url", "email", "phone", NULL
132 #define DEFAULT_PORT 1234
134 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
135 static int Del ( sout_stream_t *, sout_stream_id_t * );
136 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
138 struct sout_stream_sys_t
141 session_descriptor_t *p_session;
144 /*****************************************************************************
146 *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
149 sout_stream_t *p_stream = (sout_stream_t*)p_this;
150 sout_instance_t *p_sout = p_stream->p_sout;
158 sout_access_out_t *p_access;
161 const char *psz_mux_byext = NULL;
163 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
166 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
167 psz_access = *val.psz_string ? val.psz_string : NULL;
168 if( !*val.psz_string ) free( val.psz_string );
170 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
171 psz_mux = *val.psz_string ? val.psz_string : NULL;
172 if( !*val.psz_string ) free( val.psz_string );
175 var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
176 psz_url = *val.psz_string ? val.psz_string : NULL;
177 if( !*val.psz_string ) free( val.psz_string );
179 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
180 p_stream->p_sys->p_session = NULL;
182 msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
184 /* ext -> muxer name */
185 if( psz_url && strrchr( psz_url, '.' ) )
188 static struct { const char *ext; const char *mux; } exttomux[] =
206 { "flv", "ffmpeg{mux=flv}" },
209 const char *psz_ext = strrchr( psz_url, '.' ) + 1;
212 msg_Dbg( p_this, "extension is %s", psz_ext );
213 for( i = 0; exttomux[i].ext != NULL; i++ )
215 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
217 psz_mux_byext = exttomux[i].mux;
221 msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
224 /* We fix access/mux to valid couple */
226 if( !psz_access && !psz_mux )
231 "no access _and_ no muxer, extension gives file/%s",
233 psz_access = strdup("file");
234 psz_mux = strdup(psz_mux_byext);
238 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
243 if( psz_access && !psz_mux )
245 /* access given, no mux */
246 if( !strncmp( psz_access, "mmsh", 4 ) )
248 psz_mux = strdup("asfh");
250 else if (!strcmp (psz_access, "udp")
251 || !strcmp (psz_access, "rtp") || !strcmp (psz_access, "udplite")
252 || !strcmp (psz_access, "tcp") || !strcmp (psz_access, "sctp")
253 || !strcmp (psz_access, "dccp"))
255 psz_mux = strdup("ts");
257 else if( psz_mux_byext )
259 psz_mux = strdup(psz_mux_byext);
263 msg_Err( p_stream, "no mux specified or found by extension" );
267 else if( psz_mux && !psz_access )
269 /* mux given, no access */
270 if( !strncmp( psz_mux, "asfh", 4 ) )
272 psz_access = strdup("mmsh");
277 psz_access = strdup("file");
281 /* fix or warn of incompatible couple */
282 if( psz_mux && psz_access )
284 if( !strncmp( psz_access, "mmsh", 4 ) &&
285 strncmp( psz_mux, "asfh", 4 ) )
287 char *p = strchr( psz_mux,'{' );
289 msg_Warn( p_stream, "fixing to mmsh/asfh" );
292 /* -> a little memleak but ... */
293 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
294 sprintf( psz_mux, "asfh%s", p );
298 psz_mux = strdup("asfh");
301 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
302 !strncmp( psz_access, "udp", 3 ) ) &&
303 strncmp( psz_mux, "ts", 2 ) )
305 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
307 else if( strncmp( psz_access, "file", 4 ) &&
308 ( !strncmp( psz_mux, "mov", 3 ) ||
309 !strncmp( psz_mux, "mp4", 3 ) ) )
311 msg_Err( p_stream, "mov and mp4 work only with file output" );
315 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
317 /* *** find and open appropriate access module *** */
318 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
319 if( p_access == NULL )
321 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
322 psz_access, psz_mux, psz_url );
323 if( psz_access ) free( psz_access );
324 if( psz_mux ) free( psz_mux );
327 msg_Dbg( p_stream, "access opened" );
329 /* *** find and open appropriate mux module *** */
330 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
333 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
334 psz_access, psz_mux, psz_url );
336 sout_AccessOutDelete( p_access );
337 if( psz_access ) free( psz_access );
338 if( psz_mux ) free( psz_mux );
341 msg_Dbg( p_stream, "mux opened" );
343 /* *** Create the SAP Session structure *** */
344 if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
346 session_descriptor_t *p_session;
347 announce_method_t *p_method = sout_SAPMethod ();
348 const int payload_type = 33;
350 static const struct { const char *access; const char *fmt; } fmts[] =
352 /* TLS/DTLS variants (none implemented): */
353 { "dtlslite", "UDPLite/TLS/RTP/AVP %d" },
354 { "dtls", "UDP/TLS/RTP/AVP %d" },
355 { "dccps", "DCCP/TLS/RTP/AVP %d" },
356 { "tls", "TCP/TLS/RTP/AVP %d" },
358 { "udplite", "UDPLite/RTP/AVP %d" },
359 { "udp", "udp mpeg" },
360 { "rtp", "RTP/AVP %d" },
361 /* Currently unsupported access outputs: */
362 { "dccp", "DCCP/RTP/AVP %d" },
363 { "tcp", "TCP/RTP/AVP %d" },
364 /* SRTP (none implemented): */
365 { "srtp", "RTP/SAVP %d" },
366 { "sudplite", "UDPLite/RTP/SAVP %d" },
367 { "sdccp", "DCCP/RTP/SAVP %d" },
368 { "stcp", "TCP/RTP/SAVP %d" },
371 const char *psz_sdp_fmt = NULL;
372 char *fmt, *src, *dst;
375 for (unsigned i = 0; fmts[i].access != NULL; i++)
376 if (strncasecmp (fmts[i].access, psz_access, strlen (fmts[i].access)) == 0)
378 psz_sdp_fmt = fmts[i].fmt;
382 src = var_GetNonEmptyString (p_access, "src-addr");
383 dst = var_GetNonEmptyString (p_access, "dst-addr");
384 sport = var_GetInteger (p_access, "src-port");
385 dport = var_GetInteger (p_access, "dst-port");
387 if ((psz_sdp_fmt == NULL)
388 || (asprintf (&fmt, psz_sdp_fmt, payload_type) == -1))
391 msg_Dbg( p_stream, "SAP advertized format: %s", fmt);
392 if ((fmt == NULL) || ((src == NULL) && (dst == NULL)))
394 msg_Err (p_access, "SAP announces not supported for access %s",
402 p_session = sout_AnnounceSessionCreate (VLC_OBJECT (p_stream),
404 sout_SessionSetMedia (VLC_OBJECT (p_stream), p_session, fmt,
405 src, sport, dst, dport);
406 sout_AnnounceRegister( p_sout, p_session, p_method );
407 p_stream->p_sys->p_session = p_session;
408 sout_MethodRelease (p_method);
412 p_stream->pf_add = Add;
413 p_stream->pf_del = Del;
414 p_stream->pf_send = Send;
416 p_stream->p_sys->p_mux = p_mux;
418 if( psz_access ) free( psz_access );
419 if( psz_mux ) free( psz_mux );
420 if( psz_url ) free( psz_url );
426 /*****************************************************************************
428 *****************************************************************************/
429 static void Close( vlc_object_t * p_this )
431 sout_stream_t *p_stream = (sout_stream_t*)p_this;
432 sout_stream_sys_t *p_sys = p_stream->p_sys;
433 sout_access_out_t *p_access = p_sys->p_mux->p_access;
435 if( p_sys->p_session != NULL )
437 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
438 sout_AnnounceSessionDestroy( p_sys->p_session );
442 sout_MuxDelete( p_sys->p_mux );
443 sout_AccessOutDelete( p_access );
448 struct sout_stream_id_t
450 sout_input_t *p_input;
454 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
456 sout_stream_sys_t *p_sys = p_stream->p_sys;
457 sout_stream_id_t *id;
459 id = malloc( sizeof( sout_stream_id_t ) );
460 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
470 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
472 sout_stream_sys_t *p_sys = p_stream->p_sys;
474 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
481 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
484 sout_stream_sys_t *p_sys = p_stream->p_sys;
486 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );