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