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