]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
vlc security: As i've seen very little improvement on that front, i've decided to...
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c: standard stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2007 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 #include <vlc/vlc.h>
28 #include <vlc_sout.h>
29
30
31 #include <vlc_network.h>
32 #include "vlc_url.h"
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 #define ACCESS_TEXT N_("Output access method")
38 #define ACCESS_LONGTEXT N_( \
39     "Output method to use for the stream." )
40 #define MUX_TEXT N_("Output muxer")
41 #define MUX_LONGTEXT N_( \
42     "Muxer to use for the stream." )
43 #define DST_TEXT N_("Output destination")
44 #define DST_LONGTEXT N_( \
45     "Destination (URL) to use for the stream." )
46 #define NAME_TEXT N_("Session name")
47 #define NAME_LONGTEXT N_( \
48   "This allows you to specify a name for the session, that will be announced "\
49   "if you choose to use SAP." )
50
51 #define GROUP_TEXT N_("Session groupname")
52 #define GROUP_LONGTEXT N_( \
53   "This allows you to specify a group for the session, that will be announced "\
54   "if you choose to use SAP." )
55
56 #define DESC_TEXT N_("Session description")
57 #define DESC_LONGTEXT N_( \
58     "This allows you to give a short description with details about the stream, " \
59     "that will be announced in the SDP (Session Descriptor)." )
60 #define URL_TEXT N_("Session URL")
61 #define URL_LONGTEXT N_( \
62     "This allows you to give an URL with more details about the stream " \
63     "(often the website of the streaming organization), that will " \
64     "be announced in the SDP (Session Descriptor)." )
65 #define EMAIL_TEXT N_("Session email")
66 #define EMAIL_LONGTEXT N_( \
67     "This allows you to give a contact mail address for the stream, that will " \
68     "be announced in the SDP (Session Descriptor)." )
69 #define PHONE_TEXT N_("Session phone number")
70 #define PHONE_LONGTEXT N_( \
71     "This allows you to give a contact telephone number for the stream, that will " \
72     "be announced in the SDP (Session Descriptor)." )
73
74
75 #define SAP_TEXT N_("SAP announcing")
76 #define SAP_LONGTEXT N_("Announce this session with SAP.")
77
78 static int      Open    ( vlc_object_t * );
79 static void     Close   ( vlc_object_t * );
80
81 #define SOUT_CFG_PREFIX "sout-standard-"
82
83 vlc_module_begin();
84     set_shortname( _("Standard"));
85     set_description( _("Standard stream output") );
86     set_capability( "sout stream", 50 );
87     add_shortcut( "standard" );
88     add_shortcut( "std" );
89     set_category( CAT_SOUT );
90     set_subcategory( SUBCAT_SOUT_STREAM );
91
92     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
93                 ACCESS_LONGTEXT, VLC_FALSE );
94     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
95                 MUX_LONGTEXT, VLC_FALSE );
96     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
97                 DST_LONGTEXT, VLC_FALSE );
98         change_unsafe();
99
100     add_bool( SOUT_CFG_PREFIX "sap", VLC_FALSE, NULL, SAP_TEXT, SAP_LONGTEXT,
101               VLC_TRUE );
102     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
103                                         VLC_TRUE );
104     add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
105                                         VLC_TRUE );
106     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
107                                         VLC_TRUE );
108     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
109                                         VLC_TRUE );
110     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
111                                         VLC_TRUE );
112     add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
113                                         VLC_TRUE );
114     add_obsolete_bool( SOUT_CFG_PREFIX "sap-ipv6" );
115
116     set_callbacks( Open, Close );
117 vlc_module_end();
118
119
120 /*****************************************************************************
121  * Exported prototypes
122  *****************************************************************************/
123 static const char *ppsz_sout_options[] = {
124     "access", "mux", "url", "dst",
125     "sap", "name", "group", "description", "url", "email", "phone", NULL
126 };
127
128 #define DEFAULT_PORT 1234
129
130 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
131 static int               Del ( sout_stream_t *, sout_stream_id_t * );
132 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
133
134 struct sout_stream_sys_t
135 {
136     sout_mux_t           *p_mux;
137     session_descriptor_t *p_session;
138 };
139
140 /*****************************************************************************
141  * Open:
142  *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
144 {
145     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
146     sout_instance_t     *p_sout = p_stream->p_sout;
147     sout_stream_sys_t   *p_sys;
148
149     char *psz_mux;
150     char *psz_access;
151     char *psz_url;
152
153     vlc_value_t val;
154
155     sout_access_out_t   *p_access;
156     sout_mux_t          *p_mux;
157
158     const char          *psz_mux_byext = NULL;
159
160     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
161                    p_stream->p_cfg );
162
163     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
164     psz_access = *val.psz_string ? val.psz_string : NULL;
165     if( !*val.psz_string ) free( val.psz_string );
166
167     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
168     psz_mux = *val.psz_string ? val.psz_string : NULL;
169     if( !*val.psz_string ) free( val.psz_string );
170
171
172     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
173     psz_url = *val.psz_string ? val.psz_string : NULL;
174     if( !*val.psz_string ) free( val.psz_string );
175
176     p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
177     p_stream->p_sys->p_session = NULL;
178
179     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
180
181     /* ext -> muxer name */
182     if( psz_url && strrchr( psz_url, '.' ) )
183     {
184         /* by extension */
185         static struct { const char ext[6]; const char mux[32]; } exttomux[] =
186         {
187             { "avi", "avi" },
188             { "ogg", "ogg" },
189             { "ogm", "ogg" },
190             { "mp4", "mp4" },
191             { "mov", "mov" },
192             { "moov","mov" },
193             { "asf", "asf" },
194             { "wma", "asf" },
195             { "wmv", "asf" },
196             { "trp", "ts" },
197             { "ts",  "ts" },
198             { "mpg", "ps" },
199             { "mpeg","ps" },
200             { "ps",  "ps" },
201             { "mpeg1","mpeg1" },
202             { "wav", "wav" },
203             { "flv", "ffmpeg{mux=flv}" },
204             { "mkv", "ffmpeg{mux=matroska}"},
205             { "",    "" }
206         };
207         const char *psz_ext = strrchr( psz_url, '.' ) + 1;
208         int  i;
209
210         msg_Dbg( p_this, "extension is %s", psz_ext );
211         for( i = 0; exttomux[i].ext[0]; i++ )
212         {
213             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
214             {
215                 psz_mux_byext = exttomux[i].mux;
216                 break;
217             }
218         }
219         msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
220     }
221
222     /* We fix access/mux to valid couple */
223
224     if( !psz_access && !psz_mux )
225     {
226         if( psz_mux_byext )
227         {
228             msg_Warn( p_stream,
229                       "no access _and_ no muxer, extension gives file/%s",
230                       psz_mux_byext );
231             psz_access = strdup("file");
232             psz_mux    = strdup(psz_mux_byext);
233         }
234         else
235         {
236             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
237             return VLC_EGENERIC;
238         }
239     }
240
241     if( psz_access && !psz_mux )
242     {
243         /* access given, no mux */
244         if( !strncmp( psz_access, "mmsh", 4 ) )
245         {
246             psz_mux = strdup("asfh");
247         }
248         else if (!strcmp (psz_access, "udp")
249               || !strcmp (psz_access, "rtp"))
250         {
251             psz_mux = strdup("ts");
252         }
253         else if( psz_mux_byext )
254         {
255             psz_mux = strdup(psz_mux_byext);
256         }
257         else
258         {
259             msg_Err( p_stream, "no mux specified or found by extension" );
260             return VLC_EGENERIC;
261         }
262     }
263     else if( psz_mux && !psz_access )
264     {
265         /* mux given, no access */
266         if( !strncmp( psz_mux, "asfh", 4 ) )
267         {
268             psz_access = strdup("mmsh");
269         }
270         else
271         {
272             /* default file */
273             psz_access = strdup("file");
274         }
275     }
276
277     /* fix or warn of incompatible couple */
278     if( !strncmp( psz_access, "mmsh", 4 ) &&
279         strncmp( psz_mux, "asfh", 4 ) )
280     {
281         char *p = strchr( psz_mux,'{' );
282
283         msg_Warn( p_stream, "fixing to mmsh/asfh" );
284         if( p )
285         {
286             if( asprintf( &p, "asfh%s", p ) == -1 )
287                 p = NULL;
288             free( psz_mux );
289             psz_mux = p;
290         }
291         else
292         {
293             free( psz_mux );
294             psz_mux = strdup("asfh");
295         }
296     }
297     else if( ( !strncmp( psz_access, "rtp", 3 ) ||
298                !strncmp( psz_access, "udp", 3 ) ) &&
299              strncmp( psz_mux, "ts", 2 ) )
300     {
301         msg_Err( p_stream, "UDP and RTP are only valid with TS" );
302     }
303     else if( strncmp( psz_access, "file", 4 ) &&
304              ( !strncmp( psz_mux, "mov", 3 ) ||
305                !strncmp( psz_mux, "mp4", 3 ) ) )
306     {
307         msg_Err( p_stream, "mov and mp4 work only with file output" );
308     }
309
310     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
311
312     /* *** find and open appropriate access module *** */
313     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
314     if( p_access == NULL )
315     {
316         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
317                  psz_access, psz_mux, psz_url );
318         free( psz_access );
319         free( psz_mux );
320         return VLC_EGENERIC;
321     }
322     msg_Dbg( p_stream, "access opened" );
323
324     /* *** find and open appropriate mux module *** */
325     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
326     if( p_mux == NULL )
327     {
328         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
329                  psz_access, psz_mux, psz_url );
330
331         sout_AccessOutDelete( p_access );
332         free( psz_access );
333         free( psz_mux );
334         return VLC_EGENERIC;
335     }
336     msg_Dbg( p_stream, "mux opened" );
337
338     /* *** Create the SAP Session structure *** */
339     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
340     {
341         /* Create the SDP */
342         char *shost = var_GetNonEmptyString (p_access, "src-addr");
343         char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
344         int sport = var_GetInteger (p_access, "src-port");
345         int dport = var_GetInteger (p_access, "dst-port");
346         struct sockaddr_storage src, dst;
347         socklen_t srclen = 0, dstlen = 0;
348
349         struct addrinfo *res;
350         if (vlc_getaddrinfo (VLC_OBJECT (p_stream), dhost, dport, NULL, &res) == 0)
351         {
352             memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
353             vlc_freeaddrinfo (res);
354         }
355
356         if (vlc_getaddrinfo (VLC_OBJECT (p_stream), shost, sport, NULL, &res) == 0)
357         {
358             memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
359             vlc_freeaddrinfo (res);
360         }
361
362         char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
363                                     (struct sockaddr *)&src, srclen,
364                                     (struct sockaddr *)&dst, dstlen);
365         free (shost);
366
367         char *psz_sdp = NULL;
368         if (head != NULL)
369         {
370             if (asprintf (&psz_sdp, "%s"
371                           "m=video %d udp mpeg\r\n", head, dport) == -1)
372                 psz_sdp = NULL;
373             free (head);
374         }
375
376         /* Register the SDP with the SAP thread */
377         if (psz_sdp != NULL)
378         {
379             announce_method_t *p_method = sout_SAPMethod ();
380             msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
381
382             p_sys->p_session =
383                 sout_AnnounceRegisterSDP (p_sout, SOUT_CFG_PREFIX, psz_sdp,
384                                           dhost, p_method);
385             sout_MethodRelease (p_method);
386         }
387         free (dhost);
388     }
389
390     p_stream->pf_add    = Add;
391     p_stream->pf_del    = Del;
392     p_stream->pf_send   = Send;
393
394     p_sys->p_mux = p_mux;
395
396     free( psz_access );
397     free( psz_mux );
398     free( psz_url );
399
400     return VLC_SUCCESS;
401 }
402
403 /*****************************************************************************
404  * Close:
405  *****************************************************************************/
406 static void Close( vlc_object_t * p_this )
407 {
408     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
409     sout_stream_sys_t *p_sys    = p_stream->p_sys;
410     sout_access_out_t *p_access = p_sys->p_mux->p_access;
411
412     if( p_sys->p_session != NULL )
413         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
414
415     sout_MuxDelete( p_sys->p_mux );
416     sout_AccessOutDelete( p_access );
417
418     free( p_sys );
419 }
420
421 struct sout_stream_id_t
422 {
423     sout_input_t *p_input;
424 };
425
426
427 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
428 {
429     sout_stream_sys_t *p_sys = p_stream->p_sys;
430     sout_stream_id_t  *id;
431
432     id = malloc( sizeof( sout_stream_id_t ) );
433     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
434     {
435         free( id );
436
437         return NULL;
438     }
439
440     return id;
441 }
442
443 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
444 {
445     sout_stream_sys_t *p_sys = p_stream->p_sys;
446
447     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
448
449     free( id );
450
451     return VLC_SUCCESS;
452 }
453
454 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
455                  block_t *p_buffer )
456 {
457     sout_stream_sys_t *p_sys = p_stream->p_sys;
458
459     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
460
461     return VLC_SUCCESS;
462 }