]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
Include vlc_plugin.h as needed
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.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
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." )
67
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 an 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)." )
85
86
87 #define SAP_TEXT N_("SAP announcing")
88 #define SAP_LONGTEXT N_("Announce this session with SAP.")
89
90 static int      Open    ( vlc_object_t * );
91 static void     Close   ( vlc_object_t * );
92
93 #define SOUT_CFG_PREFIX "sout-standard-"
94
95 vlc_module_begin();
96     set_shortname( _("Standard"));
97     set_description( _("Standard stream output") );
98     set_capability( "sout stream", 50 );
99     add_shortcut( "standard" );
100     add_shortcut( "std" );
101     set_category( CAT_SOUT );
102     set_subcategory( SUBCAT_SOUT_STREAM );
103
104     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
105                 ACCESS_LONGTEXT, false );
106     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
107                 MUX_LONGTEXT, false );
108     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
109                 DEST_LONGTEXT, false );
110     add_string( SOUT_CFG_PREFIX "bind", "", NULL, BIND_TEXT,
111                 BIND_LONGTEXT, false );
112     add_string( SOUT_CFG_PREFIX "path", "", NULL, PATH_TEXT,
113                 PATH_LONGTEXT, false );
114         change_unsafe();
115
116     add_bool( SOUT_CFG_PREFIX "sap", false, NULL, SAP_TEXT, SAP_LONGTEXT,
117               true );
118     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
119                                         true );
120     add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
121                                         true );
122     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
123                                         true );
124     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
125                                         true );
126     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
127                                         true );
128     add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
129                                         true );
130     add_obsolete_bool( SOUT_CFG_PREFIX "sap-ipv6" );
131
132     set_callbacks( Open, Close );
133 vlc_module_end();
134
135
136 /*****************************************************************************
137  * Exported prototypes
138  *****************************************************************************/
139 static const char *ppsz_sout_options[] = {
140     "access", "mux", "url", "dst",
141     "sap", "name", "group", "description", "url", "email", "phone",
142     "bind", "path", NULL
143 };
144
145 #define DEFAULT_PORT 1234
146
147 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
148 static int               Del ( sout_stream_t *, sout_stream_id_t * );
149 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
150
151 struct sout_stream_sys_t
152 {
153     sout_mux_t           *p_mux;
154     session_descriptor_t *p_session;
155 };
156
157 /*****************************************************************************
158  * Open:
159  *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
161 {
162     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
163     sout_instance_t     *p_sout = p_stream->p_sout;
164     sout_stream_sys_t   *p_sys;
165
166     char *psz_mux;
167     char *psz_access;
168     char *psz_url=NULL;
169     char *psz_bind;
170     char *psz_path;
171
172     vlc_value_t val;
173
174     sout_access_out_t   *p_access;
175     sout_mux_t          *p_mux;
176
177     const char          *psz_mux_byext = NULL;
178
179     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
180                    p_stream->p_cfg );
181
182     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
183     psz_access = *val.psz_string ? val.psz_string : NULL;
184     if( !*val.psz_string ) free( val.psz_string );
185
186     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
187     psz_mux = *val.psz_string ? val.psz_string : NULL;
188     if( !*val.psz_string ) free( val.psz_string );
189
190     var_Get( p_stream, SOUT_CFG_PREFIX "bind", &val );
191     psz_bind = *val.psz_string ? val.psz_string : NULL;
192     if( !*val.psz_string ) free( val.psz_string);
193
194     var_Get( p_stream, SOUT_CFG_PREFIX "path", &val );
195     psz_path = *val.psz_string ? val.psz_string : NULL;
196     if( !*val.psz_string ) free( val.psz_string);
197
198     if( psz_bind ) psz_url = psz_bind;
199     if( psz_url && psz_path ) 
200     {
201         if( asprintf( &psz_url,"%s/%s",psz_url,psz_path ) == -1 )
202             psz_url = NULL;
203         free( psz_path );
204     }
205
206     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
207     if( *val.psz_string ) 
208     {
209         free( psz_url);
210         psz_url = val.psz_string;
211     }
212     else
213         free( val.psz_string );
214
215     p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
216     if( !p_sys ) return VLC_ENOMEM;
217     p_stream->p_sys->p_session = NULL;
218
219     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
220
221     /* ext -> muxer name */
222     if( psz_url && strrchr( psz_url, '.' ) )
223     {
224         /* by extension */
225         static struct { const char ext[6]; const char mux[32]; } exttomux[] =
226         {
227             { "avi", "avi" },
228             { "ogg", "ogg" },
229             { "ogm", "ogg" },
230             { "mp4", "mp4" },
231             { "mov", "mov" },
232             { "moov","mov" },
233             { "asf", "asf" },
234             { "wma", "asf" },
235             { "wmv", "asf" },
236             { "trp", "ts" },
237             { "ts",  "ts" },
238             { "mpg", "ps" },
239             { "mpeg","ps" },
240             { "ps",  "ps" },
241             { "mpeg1","mpeg1" },
242             { "wav", "wav" },
243             { "flv", "ffmpeg{mux=flv}" },
244             { "mkv", "ffmpeg{mux=matroska}"},
245             { "",    "" }
246         };
247         const char *psz_ext = strrchr( psz_url, '.' ) + 1;
248         int  i;
249
250         msg_Dbg( p_this, "extension is %s", psz_ext );
251         for( i = 0; exttomux[i].ext[0]; i++ )
252         {
253             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
254             {
255                 psz_mux_byext = exttomux[i].mux;
256                 break;
257             }
258         }
259         msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
260     }
261
262     /* We fix access/mux to valid couple */
263
264     if( !psz_access && !psz_mux )
265     {
266         if( psz_mux_byext )
267         {
268             msg_Warn( p_stream,
269                       "no access _and_ no muxer, extension gives file/%s",
270                       psz_mux_byext );
271             psz_access = strdup("file");
272             psz_mux    = strdup(psz_mux_byext);
273         }
274         else
275         {
276             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
277             return VLC_EGENERIC;
278         }
279     }
280
281     if( psz_access && !psz_mux )
282     {
283         /* access given, no mux */
284         if( !strncmp( psz_access, "mmsh", 4 ) )
285         {
286             psz_mux = strdup("asfh");
287         }
288         else if (!strcmp (psz_access, "udp"))
289         {
290             psz_mux = strdup("ts");
291         }
292         else if( psz_mux_byext )
293         {
294             psz_mux = strdup(psz_mux_byext);
295         }
296         else
297         {
298             msg_Err( p_stream, "no mux specified or found by extension" );
299             return VLC_EGENERIC;
300         }
301     }
302     else if( psz_mux && !psz_access )
303     {
304         /* mux given, no access */
305         if( !strncmp( psz_mux, "asfh", 4 ) )
306         {
307             psz_access = strdup("mmsh");
308         }
309         else
310         {
311             /* default file */
312             psz_access = strdup("file");
313         }
314     }
315
316     /* fix or warn of incompatible couple */
317     if( !strncmp( psz_access, "mmsh", 4 ) &&
318         strncmp( psz_mux, "asfh", 4 ) )
319     {
320         char *p = strchr( psz_mux,'{' );
321
322         msg_Warn( p_stream, "fixing to mmsh/asfh" );
323         if( p )
324         {
325             if( asprintf( &p, "asfh%s", p ) == -1 )
326                 p = NULL;
327             free( psz_mux );
328             psz_mux = p;
329         }
330         else
331         {
332             free( psz_mux );
333             psz_mux = strdup("asfh");
334         }
335     }
336     else if( !strncmp( psz_access, "udp", 3 ) )
337     {
338         if( !strncmp( psz_mux, "ffmpeg", 6 ) )
339         {   /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
340             char *psz_ffmpeg_mux = var_CreateGetString( p_this, "ffmpeg-mux" );
341             if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
342                 msg_Err( p_stream, "UDP is only valid with TS" );
343             free( psz_ffmpeg_mux );
344         }
345         else if( strncmp( psz_mux, "ts", 2 ) )
346         {
347             msg_Err( p_stream, "UDP is only valid with TS" );
348         }
349     }
350     else if( strncmp( psz_access, "file", 4 ) &&
351              ( !strncmp( psz_mux, "mov", 3 ) ||
352                !strncmp( psz_mux, "mp4", 3 ) ) )
353     {
354         msg_Err( p_stream, "mov and mp4 work only with file output" );
355     }
356
357     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
358
359     /* *** find and open appropriate access module *** */
360     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
361     if( p_access == NULL )
362     {
363         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
364                  psz_access, psz_mux, psz_url );
365         free( psz_access );
366         free( psz_mux );
367         return VLC_EGENERIC;
368     }
369     msg_Dbg( p_stream, "access opened" );
370
371     /* *** find and open appropriate mux module *** */
372     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
373     if( p_mux == NULL )
374     {
375         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
376                  psz_access, psz_mux, psz_url );
377
378         sout_AccessOutDelete( p_access );
379         free( psz_access );
380         free( psz_mux );
381         return VLC_EGENERIC;
382     }
383     msg_Dbg( p_stream, "mux opened" );
384
385     /* *** Create the SAP Session structure *** */
386     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
387     {
388         /* Create the SDP */
389         char *shost = var_GetNonEmptyString (p_access, "src-addr");
390         char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
391         int sport = var_GetInteger (p_access, "src-port");
392         int dport = var_GetInteger (p_access, "dst-port");
393         struct sockaddr_storage src, dst;
394         socklen_t srclen = 0, dstlen = 0;
395
396         struct addrinfo *res;
397         if (vlc_getaddrinfo (VLC_OBJECT (p_stream), dhost, dport, NULL, &res) == 0)
398         {
399             memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
400             vlc_freeaddrinfo (res);
401         }
402
403         if (vlc_getaddrinfo (VLC_OBJECT (p_stream), shost, sport, NULL, &res) == 0)
404         {
405             memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
406             vlc_freeaddrinfo (res);
407         }
408
409         char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
410                                     (struct sockaddr *)&src, srclen,
411                                     (struct sockaddr *)&dst, dstlen);
412         free (shost);
413
414         char *psz_sdp = NULL;
415         if (head != NULL)
416         {
417             if (asprintf (&psz_sdp, "%s"
418                           "m=video %d udp mpeg\r\n", head, dport) == -1)
419                 psz_sdp = NULL;
420             free (head);
421         }
422
423         /* Register the SDP with the SAP thread */
424         if (psz_sdp != NULL)
425         {
426             announce_method_t *p_method = sout_SAPMethod ();
427             msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
428
429             p_sys->p_session =
430                 sout_AnnounceRegisterSDP (p_sout, psz_sdp, dhost, p_method);
431             sout_MethodRelease (p_method);
432         }
433         free (dhost);
434     }
435
436     p_stream->pf_add    = Add;
437     p_stream->pf_del    = Del;
438     p_stream->pf_send   = Send;
439
440     p_sys->p_mux = p_mux;
441
442     free( psz_access );
443     free( psz_mux );
444     free( psz_url );
445
446     return VLC_SUCCESS;
447 }
448
449 /*****************************************************************************
450  * Close:
451  *****************************************************************************/
452 static void Close( vlc_object_t * p_this )
453 {
454     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
455     sout_stream_sys_t *p_sys    = p_stream->p_sys;
456     sout_access_out_t *p_access = p_sys->p_mux->p_access;
457
458     if( p_sys->p_session != NULL )
459         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
460
461     sout_MuxDelete( p_sys->p_mux );
462     sout_AccessOutDelete( p_access );
463
464     free( p_sys );
465 }
466
467 struct sout_stream_id_t
468 {
469     sout_input_t *p_input;
470 };
471
472
473 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
474 {
475     sout_stream_sys_t *p_sys = p_stream->p_sys;
476     sout_stream_id_t  *id;
477
478     id = malloc( sizeof( sout_stream_id_t ) );
479     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
480     {
481         free( id );
482
483         return NULL;
484     }
485
486     return id;
487 }
488
489 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
490 {
491     sout_stream_sys_t *p_sys = p_stream->p_sys;
492
493     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
494
495     free( id );
496
497     return VLC_SUCCESS;
498 }
499
500 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
501                  block_t *p_buffer )
502 {
503     sout_stream_sys_t *p_sys = p_stream->p_sys;
504
505     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
506
507     return VLC_SUCCESS;
508 }