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