]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
Do not implicitly encourage raw TS over UDP-Lite, DCCP and friends
[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 descriptipn")
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
99     add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
100     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
101                                         VLC_TRUE );
102     add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
103                                         VLC_TRUE );
104     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
105                                         VLC_TRUE );
106     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
107                                         VLC_TRUE );
108     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
109                                         VLC_TRUE );
110     add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
111                                         VLC_TRUE );
112     add_obsolete_bool( SOUT_CFG_PREFIX "sap-ipv6" );
113
114     set_callbacks( Open, Close );
115 vlc_module_end();
116
117
118 /*****************************************************************************
119  * Exported prototypes
120  *****************************************************************************/
121 static const char *ppsz_sout_options[] = {
122     "access", "mux", "url", "dst",
123     "sap", "name", "group", "description", "url", "email", "phone", NULL
124 };
125
126 #define DEFAULT_PORT 1234
127
128 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
129 static int               Del ( sout_stream_t *, sout_stream_id_t * );
130 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
131
132 struct sout_stream_sys_t
133 {
134     sout_mux_t           *p_mux;
135     session_descriptor_t *p_session;
136 };
137
138 /*****************************************************************************
139  * Open:
140  *****************************************************************************/
141 static int Open( vlc_object_t *p_this )
142 {
143     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
144     sout_instance_t     *p_sout = p_stream->p_sout;
145
146     char *psz_mux;
147     char *psz_access;
148     char *psz_url;
149
150     vlc_value_t val;
151
152     sout_access_out_t   *p_access;
153     sout_mux_t          *p_mux;
154
155     const char          *psz_mux_byext = NULL;
156
157     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
158                    p_stream->p_cfg );
159
160     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
161     psz_access = *val.psz_string ? val.psz_string : NULL;
162     if( !*val.psz_string ) free( val.psz_string );
163
164     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
165     psz_mux = *val.psz_string ? val.psz_string : NULL;
166     if( !*val.psz_string ) free( val.psz_string );
167
168
169     var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
170     psz_url = *val.psz_string ? val.psz_string : NULL;
171     if( !*val.psz_string ) free( val.psz_string );
172
173     p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
174     p_stream->p_sys->p_session = NULL;
175
176     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
177
178     /* ext -> muxer name */
179     if( psz_url && strrchr( psz_url, '.' ) )
180     {
181         /* by extension */
182         static struct { const char *ext; const char *mux; } exttomux[] =
183         {
184             { "avi", "avi" },
185             { "ogg", "ogg" },
186             { "ogm", "ogg" },
187             { "mp4", "mp4" },
188             { "mov", "mov" },
189             { "moov","mov" },
190             { "asf", "asf" },
191             { "wma", "asf" },
192             { "wmv", "asf" },
193             { "trp", "ts" },
194             { "ts",  "ts" },
195             { "mpg", "ps" },
196             { "mpeg","ps" },
197             { "ps",  "ps" },
198             { "mpeg1","mpeg1" },
199             { "wav","wav" },
200             { "flv", "ffmpeg{mux=flv}" },
201             { NULL,  NULL }
202         };
203         const char *psz_ext = strrchr( psz_url, '.' ) + 1;
204         int  i;
205
206         msg_Dbg( p_this, "extension is %s", psz_ext );
207         for( i = 0; exttomux[i].ext != NULL; i++ )
208         {
209             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
210             {
211                 psz_mux_byext = exttomux[i].mux;
212                 break;
213             }
214         }
215         msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
216     }
217
218     /* We fix access/mux to valid couple */
219
220     if( !psz_access && !psz_mux )
221     {
222         if( psz_mux_byext )
223         {
224             msg_Warn( p_stream,
225                       "no access _and_ no muxer, extension gives file/%s",
226                       psz_mux_byext );
227             psz_access = strdup("file");
228             psz_mux    = strdup(psz_mux_byext);
229         }
230         else
231         {
232             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
233             return VLC_EGENERIC;
234         }
235     }
236
237     if( psz_access && !psz_mux )
238     {
239         /* access given, no mux */
240         if( !strncmp( psz_access, "mmsh", 4 ) )
241         {
242             psz_mux = strdup("asfh");
243         }
244         else if (!strcmp (psz_access, "udp")
245               || !strcmp (psz_access, "rtp"))
246         {
247             psz_mux = strdup("ts");
248         }
249         else if( psz_mux_byext )
250         {
251             psz_mux = strdup(psz_mux_byext);
252         }
253         else
254         {
255             msg_Err( p_stream, "no mux specified or found by extension" );
256             return VLC_EGENERIC;
257         }
258     }
259     else if( psz_mux && !psz_access )
260     {
261         /* mux given, no access */
262         if( !strncmp( psz_mux, "asfh", 4 ) )
263         {
264             psz_access = strdup("mmsh");
265         }
266         else
267         {
268             /* default file */
269             psz_access = strdup("file");
270         }
271     }
272
273     /* fix or warn of incompatible couple */
274     if( psz_mux && psz_access )
275     {
276         if( !strncmp( psz_access, "mmsh", 4 ) &&
277             strncmp( psz_mux, "asfh", 4 ) )
278         {
279             char *p = strchr( psz_mux,'{' );
280
281             msg_Warn( p_stream, "fixing to mmsh/asfh" );
282             if( p )
283             {
284                 /* -> a little memleak but ... */
285                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
286                 sprintf( psz_mux, "asfh%s", p );
287             }
288             else
289             {
290                 psz_mux = strdup("asfh");
291             }
292         }
293         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
294                    !strncmp( psz_access, "udp", 3 ) ) &&
295                  strncmp( psz_mux, "ts", 2 ) )
296         {
297             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
298         }
299         else if( strncmp( psz_access, "file", 4 ) &&
300                  ( !strncmp( psz_mux, "mov", 3 ) ||
301                    !strncmp( psz_mux, "mp4", 3 ) ) )
302         {
303             msg_Err( p_stream, "mov and mp4 work only with file output" );
304         }
305     }
306
307     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
308
309     /* *** find and open appropriate access module *** */
310     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
311     if( p_access == NULL )
312     {
313         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
314                  psz_access, psz_mux, psz_url );
315         if( psz_access ) free( psz_access );
316         if( psz_mux ) free( psz_mux );
317         return VLC_EGENERIC;
318     }
319     msg_Dbg( p_stream, "access opened" );
320
321     /* *** find and open appropriate mux module *** */
322     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
323     if( p_mux == NULL )
324     {
325         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
326                  psz_access, psz_mux, psz_url );
327
328         sout_AccessOutDelete( p_access );
329         if( psz_access ) free( psz_access );
330         if( psz_mux ) free( psz_mux );
331         return VLC_EGENERIC;
332     }
333     msg_Dbg( p_stream, "mux opened" );
334
335     /* *** Create the SAP Session structure *** */
336     if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
337     {
338         session_descriptor_t *p_session;
339         announce_method_t *p_method = sout_SAPMethod ();
340         const int payload_type = 33;
341
342         static const struct { const char *access; const char *fmt; } fmts[] =
343             {
344                 /* TLS/DTLS variants (none implemented): */
345                 { "dtlslite", "UDPLite/TLS/RTP/AVP %d" },
346                 { "dtls",     "UDP/TLS/RTP/AVP %d" },
347                 { "dccps",    "DCCP/TLS/RTP/AVP %d" },
348                 { "tls",      "TCP/TLS/RTP/AVP %d" },
349                 /* Plain text: */
350                 { "udplite",  "UDPLite/RTP/AVP %d" },
351                 { "udp",      "udp mpeg" },
352                 { "rtp",      "RTP/AVP %d" },
353                 /* Currently unsupported access outputs: */
354                 { "dccp",     "DCCP/RTP/AVP %d" },
355                 { "tcp",      "TCP/RTP/AVP %d" },
356                 /* SRTP (none implemented): */
357                 { "srtp",     "RTP/SAVP %d" },
358                 { "sudplite", "UDPLite/RTP/SAVP %d" },
359                 { "sdccp",    "DCCP/RTP/SAVP %d" },
360                 { "stcp",     "TCP/RTP/SAVP %d" },
361                 { NULL,       NULL }
362             };
363         const char *psz_sdp_fmt = NULL;
364         char *fmt, *src, *dst;
365         int sport, dport;
366
367         for (unsigned i = 0; fmts[i].access != NULL; i++)
368             if (strncasecmp (fmts[i].access, psz_access, strlen (fmts[i].access)) == 0)
369             {
370                 psz_sdp_fmt = fmts[i].fmt;
371                 break;
372             }
373
374         src = var_GetNonEmptyString (p_access, "src-addr");
375         dst = var_GetNonEmptyString (p_access, "dst-addr");
376         sport = var_GetInteger (p_access, "src-port");
377         dport = var_GetInteger (p_access, "dst-port");
378
379         if ((psz_sdp_fmt == NULL)
380          || (asprintf (&fmt, psz_sdp_fmt, payload_type) == -1))
381             fmt = NULL;
382
383         msg_Dbg( p_stream, "SAP advertized format: %s", fmt);
384         if ((fmt == NULL) || ((src == NULL) && (dst == NULL)))
385         {
386             msg_Err (p_access, "SAP announces not supported for access %s",
387                      psz_access);
388             goto nosap;
389         }
390
391         p_session = sout_AnnounceSessionCreate (VLC_OBJECT (p_stream),
392                                                 SOUT_CFG_PREFIX);
393         sout_SessionSetMedia (VLC_OBJECT (p_stream), p_session, fmt,
394                               src, sport, dst, dport);
395         sout_AnnounceRegister( p_sout, p_session, p_method );
396         p_stream->p_sys->p_session = p_session;
397         sout_MethodRelease (p_method);
398
399 nosap:
400         free (fmt);
401         free (src);
402         free (dst);
403     }
404
405     p_stream->pf_add    = Add;
406     p_stream->pf_del    = Del;
407     p_stream->pf_send   = Send;
408
409     p_stream->p_sys->p_mux = p_mux;
410
411     if( psz_access ) free( psz_access );
412     if( psz_mux ) free( psz_mux );
413     if( psz_url ) free( psz_url );
414
415
416     return VLC_SUCCESS;
417 }
418
419 /*****************************************************************************
420  * Close:
421  *****************************************************************************/
422 static void Close( vlc_object_t * p_this )
423 {
424     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
425     sout_stream_sys_t *p_sys    = p_stream->p_sys;
426     sout_access_out_t *p_access = p_sys->p_mux->p_access;
427
428     if( p_sys->p_session != NULL )
429     {
430         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
431         sout_AnnounceSessionDestroy( p_sys->p_session );
432     }
433
434
435     sout_MuxDelete( p_sys->p_mux );
436     sout_AccessOutDelete( p_access );
437
438     free( p_sys );
439 }
440
441 struct sout_stream_id_t
442 {
443     sout_input_t *p_input;
444 };
445
446
447 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
448 {
449     sout_stream_sys_t *p_sys = p_stream->p_sys;
450     sout_stream_id_t  *id;
451
452     id = malloc( sizeof( sout_stream_id_t ) );
453     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
454     {
455         free( id );
456
457         return NULL;
458     }
459
460     return id;
461 }
462
463 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
464 {
465     sout_stream_sys_t *p_sys = p_stream->p_sys;
466
467     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
468
469     free( id );
470
471     return VLC_SUCCESS;
472 }
473
474 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
475                  block_t *p_buffer )
476 {
477     sout_stream_sys_t *p_sys = p_stream->p_sys;
478
479     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
480
481     return VLC_SUCCESS;
482 }