]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
sout: access_out and SDP do not need a sout instance
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c: standard stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34
35 #include <vlc_network.h>
36 #include <vlc_url.h>
37
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)." )
79
80
81 #define SAP_TEXT N_("SAP announcing")
82 #define SAP_LONGTEXT N_("Announce this session with SAP.")
83
84 static int      Open    ( vlc_object_t * );
85 static void     Close   ( vlc_object_t * );
86
87 #define SOUT_CFG_PREFIX "sout-standard-"
88
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 )
96
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 )
109
110     set_callbacks( Open, Close )
111 vlc_module_end ()
112
113
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
121 };
122
123 #define DEFAULT_PORT 1234
124
125 struct sout_stream_sys_t
126 {
127     sout_mux_t           *p_mux;
128     session_descriptor_t *p_session;
129 };
130
131 struct sout_stream_id_t
132 {
133 };
134
135 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
136 {
137     return (sout_stream_id_t*)sout_MuxAddStream( p_stream->p_sys->p_mux, p_fmt );
138 }
139
140 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
141 {
142     sout_MuxDeleteStream( p_stream->p_sys->p_mux, (sout_input_t*)id );
143     return VLC_SUCCESS;
144 }
145
146 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
147                  block_t *p_buffer )
148 {
149     sout_MuxSendBuffer( p_stream->p_sys->p_mux, (sout_input_t*)id, p_buffer );
150     return VLC_SUCCESS;
151 }
152 static void create_SDP(sout_stream_t *p_stream, sout_access_out_t *p_access)
153 {
154     sout_stream_sys_t   *p_sys = p_stream->p_sys;
155
156     static const struct addrinfo hints = {
157         .ai_family = AF_UNSPEC,
158         .ai_socktype = SOCK_DGRAM,
159         .ai_protocol = 0,
160         .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_IDN,
161     };
162     char *shost = var_GetNonEmptyString (p_access, "src-addr");
163     char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
164     int sport = var_GetInteger (p_access, "src-port");
165     int dport = var_GetInteger (p_access, "dst-port");
166     struct sockaddr_storage src, dst;
167     socklen_t srclen = 0, dstlen = 0;
168     struct addrinfo *res;
169
170     if (!vlc_getaddrinfo (dhost, dport, &hints, &res))
171     {
172         memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
173         freeaddrinfo (res);
174     }
175
176     if (!vlc_getaddrinfo (shost, sport, &hints, &res))
177     {
178         memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
179         freeaddrinfo (res);
180     }
181
182     char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
183             (struct sockaddr *)&src, srclen,
184             (struct sockaddr *)&dst, dstlen);
185     free (shost);
186
187     if (head != NULL)
188     {
189         char *psz_sdp = NULL;
190         if (asprintf (&psz_sdp, "%s"
191                     "m=video %d udp mpeg\r\n", head, dport) == -1)
192             psz_sdp = NULL;
193         free (head);
194
195         /* Register the SDP with the SAP thread */
196         if (psz_sdp)
197         {
198             msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
199             p_sys->p_session =
200                 sout_AnnounceRegisterSDP (p_stream, psz_sdp, dhost);
201             free( psz_sdp );
202         }
203     }
204     free (dhost);
205 }
206
207 static const char *getMuxFromAlias( const char *psz_alias )
208 {
209     static struct { const char alias[6]; const char mux[32]; } mux_alias[] =
210     {
211         { "avi", "avi" },
212         { "ogg", "ogg" },
213         { "ogm", "ogg" },
214         { "ogv", "ogg" },
215         { "flac","raw" },
216         { "mp3", "raw" },
217         { "mp4", "mp4" },
218         { "mov", "mov" },
219         { "moov","mov" },
220         { "asf", "asf" },
221         { "wma", "asf" },
222         { "wmv", "asf" },
223         { "trp", "ts" },
224         { "ts",  "ts" },
225         { "mpg", "ps" },
226         { "mpeg","ps" },
227         { "ps",  "ps" },
228         { "mpeg1","mpeg1" },
229         { "wav", "wav" },
230         { "flv", "avformat{mux=flv}" },
231         { "mkv", "avformat{mux=matroska}"},
232         { "webm", "avformat{mux=webm}"},
233     };
234
235     if( !psz_alias )
236         return NULL;
237
238     for( size_t i = 0; i < sizeof mux_alias / sizeof *mux_alias; i++ )
239         if( !strcasecmp( psz_alias, mux_alias[i].alias ) )
240             return mux_alias[i].mux;
241
242     return NULL;
243 }
244
245 static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux,
246                           char **ppsz_access, const char *psz_url )
247 {
248     char *psz_mux = *ppsz_mux;
249     char *psz_access = *ppsz_access;
250     if( !psz_mux )
251     {
252         const char *psz_ext = psz_url ? strrchr( psz_url, '.' ) : NULL;
253         if( psz_ext )
254             psz_ext++; /* use extension */
255         const char *psz_mux_byext = getMuxFromAlias( psz_ext );
256
257         if( !psz_access )
258         {
259             if( !psz_mux_byext )
260             {
261                 msg_Err( p_stream, "no access _and_ no muxer" );
262                 return 1;
263             }
264
265             msg_Warn( p_stream,
266                     "no access _and_ no muxer, extension gives file/%s",
267                     psz_mux_byext );
268             *ppsz_access = strdup("file");
269             *ppsz_mux    = strdup(psz_mux_byext);
270         }
271         else
272         {
273             if( !strncmp( psz_access, "mmsh", 4 ) )
274                 *ppsz_mux = strdup("asfh");
275             else if (!strcmp (psz_access, "udp"))
276                 *ppsz_mux = strdup("ts");
277             else if( psz_mux_byext )
278                 *ppsz_mux = strdup(psz_mux_byext);
279             else
280             {
281                 msg_Err( p_stream, "no mux specified or found by extension" );
282                 return 1;
283             }
284         }
285     }
286     else if( !psz_access )
287     {
288         if( !strncmp( psz_mux, "asfh", 4 ) )
289             *ppsz_access = strdup("mmsh");
290         else /* default file */
291             *ppsz_access = strdup("file");
292     }
293     return 0;
294 }
295
296 static void checkAccessMux( sout_stream_t *p_stream, char *psz_access,
297                             char *psz_mux )
298 {
299     if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
300         msg_Err( p_stream, "mmsh output is only valid with asfh mux" );
301     else if( strncmp( psz_access, "file", 4 ) &&
302             ( !strncmp( psz_mux, "mov", 3 ) || !strncmp( psz_mux, "mp4", 3 ) ) )
303         msg_Err( p_stream, "mov and mp4 mux are only valid with file output" );
304     else if( !strncmp( psz_access, "udp", 3 ) )
305     {
306         if( !strncmp( psz_mux, "ffmpeg", 6 ) || !strncmp( psz_mux, "avformat", 8 ) )
307         {   /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
308             char *psz_ffmpeg_mux = var_CreateGetString( p_stream, "sout-avformat-mux" );
309             if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
310                 msg_Err( p_stream, "UDP output is only valid with TS mux" );
311             free( psz_ffmpeg_mux );
312         }
313         else if( strncmp( psz_mux, "ts", 2 ) )
314             msg_Err( p_stream, "UDP output is only valid with TS mux" );
315     }
316 }
317
318 /*****************************************************************************
319  * Open:
320  *****************************************************************************/
321 static int Open( vlc_object_t *p_this )
322 {
323     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
324     sout_stream_sys_t   *p_sys;
325     char *psz_mux, *psz_access, *psz_url;
326     sout_access_out_t   *p_access;
327     int                 ret = VLC_EGENERIC;
328
329     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
330                    p_stream->p_cfg );
331
332     psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
333
334     psz_access = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "access" );
335     if( !psz_access )
336     {
337         if( !strcmp( p_stream->psz_name, "http" ) )
338             psz_access = strdup("http");
339         else if (!strcmp (p_stream->psz_name, "udp"))
340             psz_access = strdup("udp");
341         else if (!strcmp (p_stream->psz_name, "file"))
342             psz_access = strdup("file");
343     }
344
345     psz_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
346     if (!psz_url)
347     {
348         char *psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" );
349         if( psz_bind )
350         {
351             char *psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" );
352             if( psz_path )
353             {
354                 if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
355                     psz_url = NULL;
356                 free(psz_bind);
357                 free( psz_path );
358             }
359             else
360                 psz_url = psz_bind;
361         }
362     }
363
364     p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
365     if( !p_sys )
366     {
367         ret = VLC_ENOMEM;
368         goto end;
369     }
370     p_sys->p_session = NULL;
371
372     if( fixAccessMux( p_stream, &psz_mux, &psz_access, psz_url ) )
373         goto end;
374
375     checkAccessMux( p_stream, psz_access, psz_mux );
376
377     p_access = sout_AccessOutNew( p_stream, psz_access, psz_url );
378     if( p_access == NULL )
379     {
380         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
381                  psz_access, psz_mux, psz_url );
382         goto end;
383     }
384
385     p_sys->p_mux = sout_MuxNew( p_stream->p_sout, psz_mux, p_access );
386     if( !p_sys->p_mux )
387     {
388         const char *psz_mux_guess = getMuxFromAlias( psz_mux );
389         if( psz_mux_guess && strcmp( psz_mux_guess, psz_mux ) )
390         {
391             msg_Dbg( p_stream, "Couldn't open mux `%s', trying `%s' instead",
392                 psz_mux, psz_mux_guess );
393             p_sys->p_mux = sout_MuxNew( p_stream->p_sout, psz_mux_guess, p_access );
394         }
395
396         if( !p_sys->p_mux )
397         {
398             msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
399                 psz_access, psz_mux, psz_url );
400
401             sout_AccessOutDelete( p_access );
402             goto end;
403         }
404     }
405
406     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
407         create_SDP( p_stream, p_access );
408
409     if( !sout_AccessOutCanControlPace( p_access ) )
410         p_stream->p_sout->i_out_pace_nocontrol++;
411
412     p_stream->pf_add    = Add;
413     p_stream->pf_del    = Del;
414     p_stream->pf_send   = Send;
415
416     ret = VLC_SUCCESS;
417
418     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
419
420 end:
421     if( ret != VLC_SUCCESS )
422         free( p_sys );
423     free( psz_access );
424     free( psz_mux );
425     free( psz_url );
426
427     return ret;
428 }
429
430 /*****************************************************************************
431  * Close:
432  *****************************************************************************/
433 static void Close( vlc_object_t * p_this )
434 {
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;
438
439     if( p_sys->p_session != NULL )
440         sout_AnnounceUnRegister( p_stream, p_sys->p_session );
441
442     sout_MuxDelete( p_sys->p_mux );
443     if( !sout_AccessOutCanControlPace( p_access ) )
444         p_stream->p_sout->i_out_pace_nocontrol--;
445     sout_AccessOutDelete( p_access );
446
447     free( p_sys );
448 }