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