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