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