A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / stream_out / standard.c
blob03d7336b0c74a4bcbd44af8261995694cc22d6f5
1 /*****************************************************************************
2 * standard.c: standard stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2011 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
35 #include <vlc_network.h>
36 #include <vlc_url.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 #define ACCESS_TEXT N_("Output access method")
42 #define ACCESS_LONGTEXT N_( \
43 "Output method to use for the stream." )
44 #define MUX_TEXT N_("Output muxer")
45 #define MUX_LONGTEXT N_( \
46 "Muxer to use for the stream." )
47 #define DEST_TEXT N_("Output destination")
48 #define DEST_LONGTEXT N_( \
49 "Destination (URL) to use for the stream. Overrides path and bind parameters" )
50 #define BIND_TEXT N_("address to bind to (helper setting for dst)")
51 #define BIND_LONGTEXT N_( \
52 "address:port to bind vlc to listening incoming streams "\
53 "helper setting for dst,dst=bind+'/'+path. dst-parameter overrides this" )
54 #define PATH_TEXT N_("filename for stream (helper setting for dst)")
55 #define PATH_LONGTEXT N_( \
56 "Filename for stream "\
57 "helper setting for dst, dst=bind+'/'+path, dst-parameter overrides this" )
58 #define NAME_TEXT N_("Session name")
59 #define NAME_LONGTEXT N_( \
60 "This is the name of the session that will be announced in the SDP " \
61 "(Session Descriptor)." )
62 #define DESC_TEXT N_("Session description")
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 a 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-"
89 vlc_module_begin ()
90 set_shortname( N_("Standard"))
91 set_description( N_("Standard stream output") )
92 set_capability( "sout stream", 50 )
93 add_shortcut( "standard", "std", "file", "http", "udp" )
94 set_category( CAT_SOUT )
95 set_subcategory( SUBCAT_SOUT_STREAM )
97 add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT, ACCESS_LONGTEXT, false )
98 add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT, MUX_LONGTEXT, false )
99 add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT, DEST_LONGTEXT, false )
100 add_string( SOUT_CFG_PREFIX "bind", "", BIND_TEXT, BIND_LONGTEXT, false )
101 add_string( SOUT_CFG_PREFIX "path", "", PATH_TEXT, PATH_LONGTEXT, false )
102 add_bool( SOUT_CFG_PREFIX "sap", false, SAP_TEXT, SAP_LONGTEXT, true )
103 add_string( SOUT_CFG_PREFIX "name", "", NAME_TEXT, NAME_LONGTEXT, true )
104 add_obsolete_string( SOUT_CFG_PREFIX "group" ) /* since 2.1.0 */
105 add_string( SOUT_CFG_PREFIX "description", "", DESC_TEXT, DESC_LONGTEXT, true )
106 add_string( SOUT_CFG_PREFIX "url", "", URL_TEXT, URL_LONGTEXT, true )
107 add_string( SOUT_CFG_PREFIX "email", "", EMAIL_TEXT, EMAIL_LONGTEXT, true )
108 add_string( SOUT_CFG_PREFIX "phone", "", PHONE_TEXT, PHONE_LONGTEXT, true )
110 set_callbacks( Open, Close )
111 vlc_module_end ()
114 /*****************************************************************************
115 * Exported prototypes
116 *****************************************************************************/
117 static const char *const ppsz_sout_options[] = {
118 "access", "mux", "url", "dst",
119 "sap", "name", "description", "url", "email", "phone",
120 "bind", "path", NULL
123 #define DEFAULT_PORT 1234
125 struct sout_stream_sys_t
127 sout_mux_t *p_mux;
128 session_descriptor_t *p_session;
131 struct sout_stream_id_sys_t
135 static sout_stream_id_sys_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
137 return (sout_stream_id_sys_t*)sout_MuxAddStream( p_stream->p_sys->p_mux, p_fmt );
140 static int Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
142 sout_MuxDeleteStream( p_stream->p_sys->p_mux, (sout_input_t*)id );
143 return VLC_SUCCESS;
146 static int Send( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
147 block_t *p_buffer )
149 return sout_MuxSendBuffer( p_stream->p_sys->p_mux, (sout_input_t*)id, p_buffer );
151 static void create_SDP(sout_stream_t *p_stream, sout_access_out_t *p_access)
153 sout_stream_sys_t *p_sys = p_stream->p_sys;
155 static const struct addrinfo hints = {
156 .ai_family = AF_UNSPEC,
157 .ai_socktype = SOCK_DGRAM,
158 .ai_protocol = 0,
159 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_IDN,
161 char *shost = var_GetNonEmptyString (p_access, "src-addr");
162 char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
163 int sport = var_GetInteger (p_access, "src-port");
164 int dport = var_GetInteger (p_access, "dst-port");
165 struct sockaddr_storage src, dst;
166 socklen_t srclen = 0, dstlen = 0;
167 struct addrinfo *res;
169 if (!vlc_getaddrinfo (dhost, dport, &hints, &res))
171 memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
172 freeaddrinfo (res);
175 if (!vlc_getaddrinfo (shost, sport, &hints, &res))
177 memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
178 freeaddrinfo (res);
181 char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
182 (struct sockaddr *)&src, srclen,
183 (struct sockaddr *)&dst, dstlen);
184 free (shost);
186 if (head != NULL)
188 char *psz_sdp = NULL;
189 if (asprintf (&psz_sdp, "%s"
190 "m=video %d udp mpeg\r\n", head, dport) == -1)
191 psz_sdp = NULL;
192 free (head);
194 /* Register the SDP with the SAP thread */
195 if (psz_sdp)
197 msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
198 p_sys->p_session =
199 sout_AnnounceRegisterSDP (p_stream, psz_sdp, dhost);
200 free( psz_sdp );
203 free (dhost);
206 static const char *getMuxFromAlias( const char *psz_alias )
208 static struct { const char alias[6]; const char mux[32]; } mux_alias[] =
210 { "avi", "avi" },
211 { "ogg", "ogg" },
212 { "ogm", "ogg" },
213 { "ogv", "ogg" },
214 { "flac","raw" },
215 { "mp3", "raw" },
216 { "mp4", "mp4" },
217 { "mov", "mov" },
218 { "moov","mov" },
219 { "asf", "asf" },
220 { "wma", "asf" },
221 { "wmv", "asf" },
222 { "trp", "ts" },
223 { "ts", "ts" },
224 { "mpg", "ps" },
225 { "mpeg","ps" },
226 { "ps", "ps" },
227 { "mpeg1","mpeg1" },
228 { "wav", "wav" },
229 { "flv", "avformat{mux=flv}" },
230 { "mkv", "avformat{mux=matroska}"},
231 { "webm", "avformat{mux=webm}"},
234 if( !psz_alias )
235 return NULL;
237 for( size_t i = 0; i < sizeof mux_alias / sizeof *mux_alias; i++ )
238 if( !strcasecmp( psz_alias, mux_alias[i].alias ) )
239 return mux_alias[i].mux;
241 return NULL;
244 static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux,
245 char **ppsz_access, const char *psz_url )
247 char *psz_mux = *ppsz_mux;
248 char *psz_access = *ppsz_access;
249 if( !psz_mux )
251 const char *psz_ext = psz_url ? strrchr( psz_url, '.' ) : NULL;
252 if( psz_ext )
253 psz_ext++; /* use extension */
254 const char *psz_mux_byext = getMuxFromAlias( psz_ext );
256 if( !psz_access )
258 if( !psz_mux_byext )
260 msg_Err( p_stream, "no access _and_ no muxer" );
261 return 1;
264 msg_Warn( p_stream,
265 "no access _and_ no muxer, extension gives file/%s",
266 psz_mux_byext );
267 *ppsz_access = strdup("file");
268 *ppsz_mux = strdup(psz_mux_byext);
270 else
272 if( !strncmp( psz_access, "mmsh", 4 ) )
273 *ppsz_mux = strdup("asfh");
274 else if (!strcmp (psz_access, "udp"))
275 *ppsz_mux = strdup("ts");
276 else if( psz_mux_byext )
277 *ppsz_mux = strdup(psz_mux_byext);
278 else
280 msg_Err( p_stream, "no mux specified or found by extension" );
281 return 1;
285 else if( !psz_access )
287 if( !strncmp( psz_mux, "asfh", 4 ) )
288 *ppsz_access = strdup("mmsh");
289 else /* default file */
290 *ppsz_access = strdup("file");
292 return 0;
295 static bool exactMatch( const char *psz_target, const char *psz_string,
296 size_t i_len )
298 if ( strncmp( psz_target, psz_string, i_len ) )
299 return false;
300 else
301 return ( psz_target[i_len] < 'a' || psz_target[i_len] > 'z' );
304 static void checkAccessMux( sout_stream_t *p_stream, char *psz_access,
305 char *psz_mux )
307 if( exactMatch( psz_access, "mmsh", 4 ) && !exactMatch( psz_mux, "asfh", 4 ) )
308 msg_Err( p_stream, "mmsh output is only valid with asfh mux" );
309 else if( exactMatch( psz_access, "file", 4 ) &&
310 ( exactMatch( psz_mux, "mov", 3 ) || exactMatch( psz_mux, "mp4", 3 ) ) )
311 msg_Err( p_stream, "mov and mp4 mux are only valid with file output" );
312 else if( exactMatch( psz_access, "udp", 3 ) )
314 if( exactMatch( psz_mux, "ffmpeg", 6 ) || exactMatch( psz_mux, "avformat", 8 ) )
315 { /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
316 char *psz_ffmpeg_mux = var_CreateGetString( p_stream, "sout-avformat-mux" );
317 if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
318 msg_Err( p_stream, "UDP output is only valid with TS mux" );
319 free( psz_ffmpeg_mux );
321 else if( !exactMatch( psz_mux, "ts", 2 ) )
322 msg_Err( p_stream, "UDP output is only valid with TS mux" );
326 /*****************************************************************************
327 * Open:
328 *****************************************************************************/
329 static int Open( vlc_object_t *p_this )
331 sout_stream_t *p_stream = (sout_stream_t*)p_this;
332 sout_stream_sys_t *p_sys;
333 char *psz_mux, *psz_access, *psz_url;
334 sout_access_out_t *p_access;
335 int ret = VLC_EGENERIC;
337 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
338 p_stream->p_cfg );
340 psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
342 psz_access = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "access" );
343 if( !psz_access )
344 psz_access = strdup(p_stream->psz_name);
346 psz_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
347 if (!psz_url)
349 char *psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" );
350 if( psz_bind )
352 char *psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" );
353 if( psz_path )
355 if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
356 psz_url = NULL;
357 free(psz_bind);
358 free( psz_path );
360 else
361 psz_url = psz_bind;
365 p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
366 if( !p_sys )
368 ret = VLC_ENOMEM;
369 goto end;
371 p_sys->p_session = NULL;
373 if( fixAccessMux( p_stream, &psz_mux, &psz_access, psz_url ) )
374 goto end;
376 checkAccessMux( p_stream, psz_access, psz_mux );
378 p_access = sout_AccessOutNew( p_stream, psz_access, psz_url );
379 if( p_access == NULL )
381 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
382 psz_access, psz_mux, psz_url );
383 goto end;
386 p_sys->p_mux = sout_MuxNew( p_stream->p_sout, psz_mux, p_access );
387 if( !p_sys->p_mux )
389 const char *psz_mux_guess = getMuxFromAlias( psz_mux );
390 if( psz_mux_guess && strcmp( psz_mux_guess, psz_mux ) )
392 msg_Dbg( p_stream, "Couldn't open mux `%s', trying `%s' instead",
393 psz_mux, psz_mux_guess );
394 p_sys->p_mux = sout_MuxNew( p_stream->p_sout, psz_mux_guess, p_access );
397 if( !p_sys->p_mux )
399 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
400 psz_access, psz_mux, psz_url );
402 sout_AccessOutDelete( p_access );
403 goto end;
407 if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
408 create_SDP( p_stream, p_access );
410 p_stream->pf_add = Add;
411 p_stream->pf_del = Del;
412 p_stream->pf_send = Send;
413 if( !sout_AccessOutCanControlPace( p_access ) )
414 p_stream->pace_nocontrol = true;
416 ret = VLC_SUCCESS;
418 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
420 end:
421 if( ret != VLC_SUCCESS )
422 free( p_sys );
423 free( psz_access );
424 free( psz_mux );
425 free( psz_url );
427 return ret;
430 /*****************************************************************************
431 * Close:
432 *****************************************************************************/
433 static void Close( vlc_object_t * p_this )
435 sout_stream_t *p_stream = (sout_stream_t*)p_this;
436 sout_stream_sys_t *p_sys = p_stream->p_sys;
437 sout_access_out_t *p_access = p_sys->p_mux->p_access;
439 if( p_sys->p_session != NULL )
440 sout_AnnounceUnRegister( p_stream, p_sys->p_session );
442 sout_MuxDelete( p_sys->p_mux );
443 sout_AccessOutDelete( p_access );
445 free( p_sys );