demux: heif: send extradata with avif
[vlc.git] / modules / stream_out / standard.c
blob2038339799b0679998e89eb458566a0017000e88
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>
37 #include <vlc_memstream.h>
39 /*****************************************************************************
40 * Module descriptor
41 *****************************************************************************/
42 #define ACCESS_TEXT N_("Output access method")
43 #define ACCESS_LONGTEXT N_( \
44 "Output method to use for the stream." )
45 #define MUX_TEXT N_("Output muxer")
46 #define MUX_LONGTEXT N_( \
47 "Muxer to use for the stream." )
48 #define DEST_TEXT N_("Output destination")
49 #define DEST_LONGTEXT N_( \
50 "Destination (URL) to use for the stream. Overrides path and bind parameters" )
51 #define BIND_TEXT N_("Address to bind to (helper setting for dst)")
52 #define BIND_LONGTEXT N_( \
53 "address:port to bind vlc to listening incoming streams. "\
54 "Helper setting for dst, dst=bind+'/'+path. dst-parameter overrides this." )
55 #define PATH_TEXT N_("Filename for stream (helper setting for dst)")
56 #define PATH_LONGTEXT N_( \
57 "Filename for stream. "\
58 "Helper setting for dst, dst=bind+'/'+path. dst-parameter overrides this." )
59 #define NAME_TEXT N_("Session name")
60 #define NAME_LONGTEXT N_( \
61 "This is the name of the session that will be announced in the SDP " \
62 "(Session Descriptor)." )
63 #define DESC_TEXT N_("Session description")
64 #define DESC_LONGTEXT N_( \
65 "This allows you to give a short description with details about the stream, " \
66 "that will be announced in the SDP (Session Descriptor)." )
67 #define URL_TEXT N_("Session URL")
68 #define URL_LONGTEXT N_( \
69 "This allows you to give a URL with more details about the stream " \
70 "(often the website of the streaming organization), that will " \
71 "be announced in the SDP (Session Descriptor)." )
72 #define EMAIL_TEXT N_("Session email")
73 #define EMAIL_LONGTEXT N_( \
74 "This allows you to give a contact mail address for the stream, that will " \
75 "be announced in the SDP (Session Descriptor)." )
77 #define SAP_TEXT N_("SAP announcing")
78 #define SAP_LONGTEXT N_("Announce this session with SAP.")
80 static int Open ( vlc_object_t * );
81 static void Close ( vlc_object_t * );
83 #define SOUT_CFG_PREFIX "sout-standard-"
85 #ifdef ENABLE_SRT
86 #define SRT_SHORTCUT "srt"
87 #else
88 #define SRT_SHORTCUT
89 #endif
91 vlc_module_begin ()
92 set_shortname( N_("Standard"))
93 set_description( N_("Standard stream output") )
94 set_capability( "sout stream", 50 )
95 add_shortcut( "standard", "std", "file", "http", "udp", SRT_SHORTCUT )
96 set_category( CAT_SOUT )
97 set_subcategory( SUBCAT_SOUT_STREAM )
99 add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT, ACCESS_LONGTEXT, false )
100 add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT, MUX_LONGTEXT, false )
101 add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT, DEST_LONGTEXT, false )
102 add_string( SOUT_CFG_PREFIX "bind", "", BIND_TEXT, BIND_LONGTEXT, false )
103 add_string( SOUT_CFG_PREFIX "path", "", PATH_TEXT, PATH_LONGTEXT, false )
104 add_bool( SOUT_CFG_PREFIX "sap", false, SAP_TEXT, SAP_LONGTEXT, true )
105 add_string( SOUT_CFG_PREFIX "name", "", NAME_TEXT, NAME_LONGTEXT, true )
106 add_obsolete_string( SOUT_CFG_PREFIX "group" ) /* since 2.1.0 */
107 add_string( SOUT_CFG_PREFIX "description", "", DESC_TEXT, DESC_LONGTEXT, true )
108 add_string( SOUT_CFG_PREFIX "url", "", URL_TEXT, URL_LONGTEXT, true )
109 add_string( SOUT_CFG_PREFIX "email", "", EMAIL_TEXT, EMAIL_LONGTEXT, true )
110 add_obsolete_string( SOUT_CFG_PREFIX "phone" ) /* since 3.0.0 */
112 set_callbacks( Open, Close )
113 vlc_module_end ()
116 /*****************************************************************************
117 * Exported prototypes
118 *****************************************************************************/
119 static const char *const ppsz_sout_options[] = {
120 "access", "mux", "url", "dst",
121 "sap", "name", "description", "url", "email",
122 "bind", "path", NULL
125 #define DEFAULT_PORT 1234
127 typedef struct
129 sout_mux_t *p_mux;
130 session_descriptor_t *p_session;
131 } sout_stream_sys_t;
133 static void *Add( sout_stream_t *p_stream, const es_format_t *p_fmt )
135 sout_stream_sys_t *p_sys = p_stream->p_sys;
136 return sout_MuxAddStream( p_sys->p_mux, p_fmt );
139 static void Del( sout_stream_t *p_stream, void *id )
141 sout_stream_sys_t *p_sys = p_stream->p_sys;
142 sout_MuxDeleteStream( p_sys->p_mux, (sout_input_t*)id );
145 static int Send( sout_stream_t *p_stream, void *id, block_t *p_buffer )
147 sout_stream_sys_t *p_sys = p_stream->p_sys;
148 return sout_MuxSendBuffer( p_sys->p_mux, (sout_input_t*)id, p_buffer );
151 static void Flush( sout_stream_t *p_stream, void *id )
153 sout_stream_sys_t *p_sys = p_stream->p_sys;
154 sout_MuxFlush( p_sys->p_mux, (sout_input_t*)id );
157 static void create_SDP(sout_stream_t *p_stream, sout_access_out_t *p_access)
159 sout_stream_sys_t *p_sys = p_stream->p_sys;
161 static const struct addrinfo hints = {
162 .ai_family = AF_UNSPEC,
163 .ai_socktype = SOCK_DGRAM,
164 .ai_protocol = 0,
165 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_IDN,
167 char *shost = var_GetNonEmptyString (p_access, "src-addr");
168 char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
169 int sport = var_GetInteger (p_access, "src-port");
170 int dport = var_GetInteger (p_access, "dst-port");
171 struct sockaddr_storage src, dst;
172 socklen_t srclen = 0, dstlen = 0;
173 struct addrinfo *res;
175 if (!vlc_getaddrinfo (dhost, dport, &hints, &res))
177 memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
178 freeaddrinfo (res);
181 if (!vlc_getaddrinfo (shost, sport, &hints, &res))
183 memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
184 freeaddrinfo (res);
187 struct vlc_memstream sdp;
189 if (vlc_sdp_Start(&sdp, VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
190 (struct sockaddr *)&src, srclen,
191 (struct sockaddr *)&dst, dstlen) == 0)
193 vlc_memstream_printf(&sdp, "m=video %d udp mpeg\r\n", dport);
195 /* Register the SDP with the SAP thread */
196 if (vlc_memstream_close(&sdp) == 0)
198 msg_Dbg(p_stream, "Generated SDP:\n%s", sdp.ptr);
199 p_sys->p_session =
200 sout_AnnounceRegisterSDP(p_stream, sdp.ptr, dhost);
201 free(sdp.ptr);
204 free(shost);
205 free(dhost);
208 static const char *getMuxFromAlias( const char *psz_alias )
210 static struct { const char alias[6]; const char mux[32]; } mux_alias[] =
212 { "avi", "avi" },
213 { "ogg", "ogg" },
214 { "ogm", "ogg" },
215 { "ogv", "ogg" },
216 { "flac","raw" },
217 { "mp3", "raw" },
218 { "mp4", "mp4" },
219 { "mov", "mov" },
220 { "moov","mov" },
221 { "asf", "asf" },
222 { "wma", "asf" },
223 { "wmv", "asf" },
224 { "trp", "ts" },
225 { "ts", "ts" },
226 { "mpg", "ps" },
227 { "mpeg","ps" },
228 { "ps", "ps" },
229 { "mpeg1","mpeg1" },
230 { "wav", "wav" },
231 { "flv", "avformat{mux=flv}" },
232 { "mkv", "avformat{mux=matroska}"},
233 { "webm", "avformat{mux=webm}"},
236 if( !psz_alias )
237 return NULL;
239 for( size_t i = 0; i < sizeof mux_alias / sizeof *mux_alias; i++ )
240 if( !strcasecmp( psz_alias, mux_alias[i].alias ) )
241 return mux_alias[i].mux;
243 return NULL;
246 static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux,
247 char **ppsz_access, const char *psz_url )
249 char *psz_mux = *ppsz_mux;
250 char *psz_access = *ppsz_access;
251 if( !psz_mux )
253 const char *psz_ext = psz_url ? strrchr( psz_url, '.' ) : NULL;
254 if( psz_ext )
255 psz_ext++; /* use extension */
256 const char *psz_mux_byext = getMuxFromAlias( psz_ext );
258 if( !psz_access )
260 if( !psz_mux_byext )
262 msg_Err( p_stream, "no access _and_ no muxer" );
263 return 1;
266 msg_Warn( p_stream,
267 "no access _and_ no muxer, extension gives file/%s",
268 psz_mux_byext );
269 *ppsz_access = strdup("file");
270 *ppsz_mux = strdup(psz_mux_byext);
272 else
274 if( !strncmp( psz_access, "mmsh", 4 ) )
275 *ppsz_mux = strdup("asfh");
276 else if (!strcmp (psz_access, "udp"))
277 *ppsz_mux = strdup("ts");
278 else if( psz_mux_byext )
279 *ppsz_mux = strdup(psz_mux_byext);
280 else
282 msg_Err( p_stream, "no mux specified or found by extension" );
283 return 1;
287 else if( !psz_access )
289 if( !strncmp( psz_mux, "asfh", 4 ) )
290 *ppsz_access = strdup("mmsh");
291 else /* default file */
292 *ppsz_access = strdup("file");
294 return 0;
297 static bool exactMatch( const char *psz_target, const char *psz_string,
298 size_t i_len )
300 if ( strncmp( psz_target, psz_string, i_len ) )
301 return false;
302 else
303 return ( psz_target[i_len] < 'a' || psz_target[i_len] > 'z' );
306 static void checkAccessMux( sout_stream_t *p_stream, char *psz_access,
307 char *psz_mux )
309 if( exactMatch( psz_access, "mmsh", 4 ) && !exactMatch( psz_mux, "asfh", 4 ) )
310 msg_Err( p_stream, "mmsh output is only valid with asfh mux" );
311 else if( !exactMatch( psz_access, "file", 4 ) &&
312 ( exactMatch( psz_mux, "mov", 3 ) || exactMatch( psz_mux, "mp4", 3 ) ) )
313 msg_Err( p_stream, "mov and mp4 mux are only valid with file output" );
314 else if( exactMatch( psz_access, "udp", 3 ) )
316 if( exactMatch( psz_mux, "ffmpeg", 6 ) || exactMatch( psz_mux, "avformat", 8 ) )
317 { /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
318 char *psz_ffmpeg_mux = var_CreateGetString( p_stream, "sout-avformat-mux" );
319 if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
320 msg_Err( p_stream, "UDP output is only valid with TS mux" );
321 free( psz_ffmpeg_mux );
323 else if( !exactMatch( psz_mux, "ts", 2 ) )
324 msg_Err( p_stream, "UDP output is only valid with TS mux" );
328 /*****************************************************************************
329 * Open:
330 *****************************************************************************/
331 static int Open( vlc_object_t *p_this )
333 sout_stream_t *p_stream = (sout_stream_t*)p_this;
334 sout_stream_sys_t *p_sys;
335 char *psz_mux, *psz_access, *psz_url;
336 sout_access_out_t *p_access;
337 int ret = VLC_EGENERIC;
339 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
340 p_stream->p_cfg );
342 psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
344 psz_access = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "access" );
345 if( !psz_access )
346 psz_access = strdup(p_stream->psz_name);
348 psz_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
349 if (!psz_url)
351 char *psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" );
352 if( psz_bind )
354 char *psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" );
355 if( psz_path )
357 if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
358 psz_url = NULL;
359 free(psz_bind);
360 free( psz_path );
362 else
363 psz_url = psz_bind;
367 p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
368 if( !p_sys )
370 ret = VLC_ENOMEM;
371 goto end;
373 p_sys->p_session = NULL;
375 if( fixAccessMux( p_stream, &psz_mux, &psz_access, psz_url ) )
376 goto end;
378 checkAccessMux( p_stream, psz_access, psz_mux );
380 p_access = sout_AccessOutNew( p_stream, psz_access, psz_url );
381 if( p_access == NULL )
383 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
384 psz_access, psz_mux, psz_url );
385 goto end;
388 p_sys->p_mux = sout_MuxNew( p_stream->p_sout, psz_mux, p_access );
389 if( !p_sys->p_mux )
391 const char *psz_mux_guess = getMuxFromAlias( psz_mux );
392 if( psz_mux_guess && strcmp( psz_mux_guess, psz_mux ) )
394 msg_Dbg( p_stream, "Couldn't open mux `%s', trying `%s' instead",
395 psz_mux, psz_mux_guess );
396 p_sys->p_mux = sout_MuxNew( p_stream->p_sout, psz_mux_guess, p_access );
399 if( !p_sys->p_mux )
401 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
402 psz_access, psz_mux, psz_url );
404 sout_AccessOutDelete( p_access );
405 goto end;
409 if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
410 create_SDP( p_stream, p_access );
412 p_stream->pf_add = Add;
413 p_stream->pf_del = Del;
414 p_stream->pf_send = Send;
415 p_stream->pf_flush = Flush;
416 if( !sout_AccessOutCanControlPace( p_access ) )
417 p_stream->pace_nocontrol = true;
419 ret = VLC_SUCCESS;
421 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
423 end:
424 if( ret != VLC_SUCCESS )
425 free( p_sys );
426 free( psz_access );
427 free( psz_mux );
428 free( psz_url );
430 return ret;
433 /*****************************************************************************
434 * Close:
435 *****************************************************************************/
436 static void Close( vlc_object_t * p_this )
438 sout_stream_t *p_stream = (sout_stream_t*)p_this;
439 sout_stream_sys_t *p_sys = p_stream->p_sys;
440 sout_access_out_t *p_access = p_sys->p_mux->p_access;
442 if( p_sys->p_session != NULL )
443 sout_AnnounceUnRegister( p_stream, p_sys->p_session );
445 sout_MuxDelete( p_sys->p_mux );
446 sout_AccessOutDelete( p_access );
448 free( p_sys );