Fix many ES_OUT_RESET_PCR problems
[vlc.git] / modules / stream_out / standard.c
blob4e8f29c5dbab5cf7c5ba647827663443145d487b
1 /*****************************************************************************
2 * standard.c: standard stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2011 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
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)." )
63 #define GROUP_TEXT N_("Session groupname")
64 #define GROUP_LONGTEXT N_( \
65 "This allows you to specify a group for the session, that will be announced "\
66 "if you choose to use SAP." )
68 #define DESC_TEXT N_("Session description")
69 #define DESC_LONGTEXT N_( \
70 "This allows you to give a short description with details about the stream, " \
71 "that will be announced in the SDP (Session Descriptor)." )
72 #define URL_TEXT N_("Session URL")
73 #define URL_LONGTEXT N_( \
74 "This allows you to give a URL with more details about the stream " \
75 "(often the website of the streaming organization), that will " \
76 "be announced in the SDP (Session Descriptor)." )
77 #define EMAIL_TEXT N_("Session email")
78 #define EMAIL_LONGTEXT N_( \
79 "This allows you to give a contact mail address for the stream, that will " \
80 "be announced in the SDP (Session Descriptor)." )
81 #define PHONE_TEXT N_("Session phone number")
82 #define PHONE_LONGTEXT N_( \
83 "This allows you to give a contact telephone number for the stream, that will " \
84 "be announced in the SDP (Session Descriptor)." )
87 #define SAP_TEXT N_("SAP announcing")
88 #define SAP_LONGTEXT N_("Announce this session with SAP.")
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
93 #define SOUT_CFG_PREFIX "sout-standard-"
95 vlc_module_begin ()
96 set_shortname( N_("Standard"))
97 set_description( N_("Standard stream output") )
98 set_capability( "sout stream", 50 )
99 add_shortcut( "standard", "std", "file", "http", "udp" )
100 set_category( CAT_SOUT )
101 set_subcategory( SUBCAT_SOUT_STREAM )
103 add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT, ACCESS_LONGTEXT, false )
104 add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT, MUX_LONGTEXT, false )
105 add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT, DEST_LONGTEXT, false )
106 add_string( SOUT_CFG_PREFIX "bind", "", BIND_TEXT, BIND_LONGTEXT, false )
107 add_string( SOUT_CFG_PREFIX "path", "", PATH_TEXT, PATH_LONGTEXT, false )
108 add_bool( SOUT_CFG_PREFIX "sap", false, SAP_TEXT, SAP_LONGTEXT, true )
109 add_string( SOUT_CFG_PREFIX "name", "", NAME_TEXT, NAME_LONGTEXT, true )
110 add_string( SOUT_CFG_PREFIX "group", "", GROUP_TEXT, GROUP_LONGTEXT, true )
111 add_string( SOUT_CFG_PREFIX "description", "", DESC_TEXT, DESC_LONGTEXT, true )
112 add_string( SOUT_CFG_PREFIX "url", "", URL_TEXT, URL_LONGTEXT, true )
113 add_string( SOUT_CFG_PREFIX "email", "", EMAIL_TEXT, EMAIL_LONGTEXT, true )
114 add_string( SOUT_CFG_PREFIX "phone", "", PHONE_TEXT, PHONE_LONGTEXT, true )
116 set_callbacks( Open, Close )
117 vlc_module_end ()
120 /*****************************************************************************
121 * Exported prototypes
122 *****************************************************************************/
123 static const char *const ppsz_sout_options[] = {
124 "access", "mux", "url", "dst",
125 "sap", "name", "group", "description", "url", "email", "phone",
126 "bind", "path", NULL
129 #define DEFAULT_PORT 1234
131 struct sout_stream_sys_t
133 sout_mux_t *p_mux;
134 session_descriptor_t *p_session;
137 struct sout_stream_id_t
141 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
143 return (sout_stream_id_t*)sout_MuxAddStream( p_stream->p_sys->p_mux, p_fmt );
146 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
148 sout_MuxDeleteStream( p_stream->p_sys->p_mux, (sout_input_t*)id );
149 return VLC_SUCCESS;
152 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
153 block_t *p_buffer )
155 sout_MuxSendBuffer( p_stream->p_sys->p_mux, (sout_input_t*)id, p_buffer );
156 return VLC_SUCCESS;
158 static void create_SDP(sout_stream_t *p_stream, sout_access_out_t *p_access)
160 sout_stream_sys_t *p_sys = p_stream->p_sys;
162 static const struct addrinfo hints = {
163 .ai_family = AF_UNSPEC,
164 .ai_socktype = SOCK_DGRAM,
165 .ai_protocol = 0,
166 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
168 char *shost = var_GetNonEmptyString (p_access, "src-addr");
169 char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
170 int sport = var_GetInteger (p_access, "src-port");
171 int dport = var_GetInteger (p_access, "dst-port");
172 struct sockaddr_storage src, dst;
173 socklen_t srclen = 0, dstlen = 0;
174 struct addrinfo *res;
176 if (!vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res))
178 memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
179 freeaddrinfo (res);
182 if (!vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res))
184 memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
185 freeaddrinfo (res);
188 char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
189 (struct sockaddr *)&src, srclen,
190 (struct sockaddr *)&dst, dstlen);
191 free (shost);
193 if (head != NULL)
195 char *psz_sdp = NULL;
196 if (asprintf (&psz_sdp, "%s"
197 "m=video %d udp mpeg\r\n", head, dport) == -1)
198 psz_sdp = NULL;
199 free (head);
201 /* Register the SDP with the SAP thread */
202 if (psz_sdp)
204 msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
205 p_sys->p_session =
206 sout_AnnounceRegisterSDP (p_stream->p_sout, psz_sdp, dhost);
207 free( psz_sdp );
210 free (dhost);
213 static const char *getMuxFromExt( const char *psz_url )
215 static struct { const char ext[6]; const char mux[32]; } exttomux[] =
217 { "avi", "avi" },
218 { "ogg", "ogg" },
219 { "ogm", "ogg" },
220 { "ogv", "ogg" },
221 { "flac","raw" },
222 { "mp3", "raw" },
223 { "mp4", "mp4" },
224 { "mov", "mov" },
225 { "moov","mov" },
226 { "asf", "asf" },
227 { "wma", "asf" },
228 { "wmv", "asf" },
229 { "trp", "ts" },
230 { "ts", "ts" },
231 { "mpg", "ps" },
232 { "mpeg","ps" },
233 { "ps", "ps" },
234 { "mpeg1","mpeg1" },
235 { "wav", "wav" },
236 { "flv", "ffmpeg{mux=flv}" },
237 { "mkv", "ffmpeg{mux=matroska}"},
238 { "webm", "ffmpeg{mux=webm}"},
241 if( !psz_url )
242 return NULL;
243 const char *psz_ext = strrchr( psz_url, '.' );
244 if( !psz_ext )
245 return NULL;
246 psz_ext++;
248 for( size_t i = 0; i < sizeof exttomux / sizeof *exttomux; i++ )
249 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
250 return exttomux[i].mux;
252 return NULL;
255 static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux,
256 char **ppsz_access, const char *psz_url )
258 char *psz_mux = *ppsz_mux;
259 char *psz_access = *ppsz_access;
260 if( !psz_mux )
262 const char *psz_mux_byext = getMuxFromExt( psz_url );
264 if( !psz_access )
266 if( !psz_mux_byext )
268 msg_Err( p_stream, "no access _and_ no muxer" );
269 return 1;
272 msg_Warn( p_stream,
273 "no access _and_ no muxer, extension gives file/%s",
274 psz_mux_byext );
275 *ppsz_access = strdup("file");
276 *ppsz_mux = strdup(psz_mux_byext);
278 else
280 if( !strncmp( psz_access, "mmsh", 4 ) )
281 *ppsz_mux = strdup("asfh");
282 else if (!strcmp (psz_access, "udp"))
283 *ppsz_mux = strdup("ts");
284 else if( psz_mux_byext )
285 *ppsz_mux = strdup(psz_mux_byext);
286 else
288 msg_Err( p_stream, "no mux specified or found by extension" );
289 return 1;
293 else if( !psz_access )
295 if( !strncmp( psz_mux, "asfh", 4 ) )
296 *ppsz_access = strdup("mmsh");
297 else /* default file */
298 *ppsz_access = strdup("file");
300 return 0;
303 static void checkAccessMux( sout_stream_t *p_stream, char *psz_access,
304 char *psz_mux )
306 if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
307 msg_Err( p_stream, "mmsh output is only valid with asfh mux" );
308 else if( strncmp( psz_access, "file", 4 ) &&
309 ( !strncmp( psz_mux, "mov", 3 ) || !strncmp( psz_mux, "mp4", 3 ) ) )
310 msg_Err( p_stream, "mov and mp4 mux are only valid with file output" );
311 else if( !strncmp( psz_access, "udp", 3 ) )
313 if( !strncmp( psz_mux, "ffmpeg", 6 ) )
314 { /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
315 char *psz_ffmpeg_mux = var_CreateGetString( p_stream, "ffmpeg-mux" );
316 if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
317 msg_Err( p_stream, "UDP output is only valid with TS mux" );
318 free( psz_ffmpeg_mux );
320 else if( strncmp( psz_mux, "ts", 2 ) )
321 msg_Err( p_stream, "UDP output is only valid with TS mux" );
325 /*****************************************************************************
326 * Open:
327 *****************************************************************************/
328 static int Open( vlc_object_t *p_this )
330 sout_stream_t *p_stream = (sout_stream_t*)p_this;
331 sout_instance_t *p_sout = p_stream->p_sout;
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 )
345 if( !strcmp( p_stream->psz_name, "http" ) )
346 psz_access = strdup("http");
347 else if (!strcmp (p_stream->psz_name, "udp"))
348 psz_access = strdup("udp");
349 else if (!strcmp (p_stream->psz_name, "file"))
350 psz_access = strdup("file");
353 psz_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
354 if (!psz_url)
356 char *psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" );
357 if( psz_bind )
359 char *psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" );
360 if( psz_path )
362 if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
363 psz_url = NULL;
364 free(psz_bind);
365 free( psz_path );
367 else
368 psz_url = psz_bind;
372 p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
373 if( !p_sys )
375 ret = VLC_ENOMEM;
376 goto end;
378 p_sys->p_session = NULL;
380 if( fixAccessMux( p_stream, &psz_mux, &psz_access, psz_url ) )
381 goto end;
383 checkAccessMux( p_stream, psz_access, psz_mux );
385 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
386 if( p_access == NULL )
388 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
389 psz_access, psz_mux, psz_url );
390 goto end;
393 p_sys->p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
394 if( !p_sys->p_mux )
396 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
397 psz_access, psz_mux, psz_url );
399 sout_AccessOutDelete( p_access );
400 goto end;
403 if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
404 create_SDP( p_stream, p_access );
406 if( !sout_AccessOutCanControlPace( p_access ) )
407 p_sout->i_out_pace_nocontrol++;
409 p_stream->pf_add = Add;
410 p_stream->pf_del = Del;
411 p_stream->pf_send = Send;
413 ret = VLC_SUCCESS;
415 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
417 end:
418 if( ret != VLC_SUCCESS )
419 free( p_sys );
420 free( psz_access );
421 free( psz_mux );
422 free( psz_url );
424 return ret;
427 /*****************************************************************************
428 * Close:
429 *****************************************************************************/
430 static void Close( vlc_object_t * p_this )
432 sout_stream_t *p_stream = (sout_stream_t*)p_this;
433 sout_stream_sys_t *p_sys = p_stream->p_sys;
434 sout_access_out_t *p_access = p_sys->p_mux->p_access;
436 if( p_sys->p_session != NULL )
437 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
439 sout_MuxDelete( p_sys->p_mux );
440 if( !sout_AccessOutCanControlPace( p_access ) )
441 p_stream->p_sout->i_out_pace_nocontrol--;
442 sout_AccessOutDelete( p_access );
444 free( p_sys );