]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
Fix memleaks
[vlc] / modules / stream_out / standard.c
1 /*****************************************************************************
2  * standard.c: standard stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/sout.h>
33
34 #include "announce.h"
35 #include "network.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 #define ACCESS_TEXT N_("Output access method")
41 #define ACCESS_LONGTEXT N_( \
42     "Allows you to specify the output access method used for the streaming " \
43     "output." )
44 #define MUX_TEXT N_("Output muxer")
45 #define MUX_LONGTEXT N_( \
46     "Allows you to specify the output muxer method used for the streaming " \
47     "output." )
48 #define URL_TEXT N_("Output URL")
49 #define URL_LONGTEXT N_( \
50     "Allows you to specify the output URL used for the streaming output." )
51
52 #define NAME_TEXT N_("Session name")
53 #define NAME_LONGTEXT N_( \
54     "Name of the session that will be announced with SAP or SLP" )
55
56 #define SAP_TEXT N_("SAP announcing")
57 #define SAP_LONGTEXT N_("Announce this session with SAP")
58
59 #define SAPv6_TEXT N_("SAP IPv6 announcing")
60 #define SAPv6_LONGTEXT N_("Use IPv6 to announce this session with SAP")
61
62 #define SLP_TEXT N_("SLP announcing")
63 #define SLP_LONGTEXT N_("Announce this session with SLP")
64
65 static int      Open    ( vlc_object_t * );
66 static void     Close   ( vlc_object_t * );
67
68 #define SOUT_CFG_PREFIX "sout-standard-"
69
70 vlc_module_begin();
71     set_description( _("Standard stream output") );
72     set_capability( "sout stream", 50 );
73     add_shortcut( "standard" );
74     add_shortcut( "std" );
75
76     add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
77                 ACCESS_LONGTEXT, VLC_FALSE );
78     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
79                 MUX_LONGTEXT, VLC_FALSE );
80     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
81                 URL_LONGTEXT, VLC_FALSE );
82
83     add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
84     add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT,NAME_LONGTEXT
85                                      , VLC_TRUE );
86     add_bool( SOUT_CFG_PREFIX "sap-ipv6", 0, NULL, SAPv6_TEXT, SAPv6_LONGTEXT,
87                                         VLC_TRUE );
88
89     add_bool( SOUT_CFG_PREFIX "slp", 0, NULL, SLP_TEXT, SLP_LONGTEXT, VLC_TRUE );
90
91     set_callbacks( Open, Close );
92 vlc_module_end();
93
94
95 /*****************************************************************************
96  * Exported prototypes
97  *****************************************************************************/
98 static const char *ppsz_sout_options[] = {
99     "access", "mux", "url",
100     "sap", "name", "sap-ipv6",
101     "slp",NULL
102 };
103
104 #define DEFAULT_IPV6_SCOPE '8'
105 #define DEFAULT_PORT 1234
106
107 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
108 static int               Del ( sout_stream_t *, sout_stream_id_t * );
109 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
110
111 struct sout_stream_sys_t
112 {
113     sout_mux_t           *p_mux;
114     slp_session_t        *p_slp;
115     session_descriptor_t *p_session;
116 };
117
118 /*****************************************************************************
119  * Open:
120  *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
122 {
123     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
124     sout_instance_t     *p_sout = p_stream->p_sout;
125     slp_session_t       *p_slp = NULL;
126
127     char *psz_mux;
128     char *psz_access;
129     char *psz_url;
130
131     vlc_value_t val;
132
133     sout_access_out_t   *p_access;
134     sout_mux_t          *p_mux;
135
136     char                *psz_mux_byext = NULL;
137
138     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
139                    p_stream->p_cfg );
140
141     var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
142     psz_access = *val.psz_string ? val.psz_string : NULL;
143     if( val.psz_string && !*val.psz_string ) free( val.psz_string );
144
145     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
146     psz_mux = *val.psz_string ? val.psz_string : NULL;
147     if( val.psz_string && !*val.psz_string ) free( val.psz_string );
148
149     var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
150     psz_url = *val.psz_string ? val.psz_string : NULL;
151     if( val.psz_string && !*val.psz_string ) free( val.psz_string );
152
153     p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
154     p_stream->p_sys->p_session = NULL;
155
156     msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
157
158     /* ext -> muxer name */
159     if( psz_url && strrchr( psz_url, '.' ) )
160     {
161         /* by extention */
162         static struct { char *ext; char *mux; } exttomux[] =
163         {
164             { "avi", "avi" },
165             { "ogg", "ogg" },
166             { "ogm", "ogg" },
167             { "mp4", "mp4" },
168             { "mov", "mov" },
169             { "moov","mov" },
170             { "asf", "asf" },
171             { "wma", "asf" },
172             { "wmv", "asf" },
173             { "trp", "ts" },
174             { "ts",  "ts" },
175             { "mpg", "ps" },
176             { "mpeg","ps" },
177             { "ps",  "ps" },
178             { "mpeg1","mpeg1" },
179             { NULL,  NULL }
180         };
181         char *psz_ext = strrchr( psz_url, '.' ) + 1;
182         int  i;
183
184         msg_Dbg( p_this, "extention is %s", psz_ext );
185         for( i = 0; exttomux[i].ext != NULL; i++ )
186         {
187             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
188             {
189                 psz_mux_byext = exttomux[i].mux;
190                 break;
191             }
192         }
193         msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
194     }
195
196     /* We fix access/mux to valid couple */
197
198     if( !psz_access && !psz_mux )
199     {
200         if( psz_mux_byext )
201         {
202             msg_Warn( p_stream,
203                       "no access _and_ no muxer, extention gives file/%s",
204                       psz_mux_byext );
205             psz_access = strdup("file");
206             psz_mux    = strdup(psz_mux_byext);
207         }
208         else
209         {
210             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
211             return VLC_EGENERIC;
212         }
213     }
214
215     if( psz_access && !psz_mux )
216     {
217         /* access given, no mux */
218         if( !strncmp( psz_access, "mmsh", 4 ) )
219         {
220             psz_mux = strdup("asfh");
221         }
222         else if( !strncmp( psz_access, "udp", 3 ) )
223         {
224             psz_mux = strdup("ts");
225         }
226         else if( psz_mux_byext )
227         {
228             psz_mux = strdup(psz_mux_byext);
229         }
230         else
231         {
232             msg_Err( p_stream, "no mux specified or found by extention" );
233             return VLC_EGENERIC;
234         }
235     }
236     else if( psz_mux && !psz_access )
237     {
238         /* mux given, no access */
239         if( !strncmp( psz_mux, "asfh", 4 ) )
240         {
241             psz_access = strdup("mmsh");
242         }
243         else
244         {
245             /* default file */
246             psz_access = strdup("file");
247         }
248     }
249
250     /* fix or warm of incompatible couple */
251     if( psz_mux && psz_access )
252     {
253         if( !strncmp( psz_access, "mmsh", 4 ) &&
254             strncmp( psz_mux, "asfh", 4 ) )
255         {
256             char *p = strchr( psz_mux,'{' );
257
258             msg_Warn( p_stream, "fixing to mmsh/asfh" );
259             if( p )
260             {
261                 /* -> a little memleak but ... */
262                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
263                 sprintf( psz_mux, "asfh%s", p );
264             }
265             else
266             {
267                 psz_mux = strdup("asfh");
268             }
269         }
270         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
271                    !strncmp( psz_access, "udp", 3 ) ) &&
272                  strncmp( psz_mux, "ts", 2 ) )
273         {
274             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
275         }
276         else if( strncmp( psz_access, "file", 4 ) &&
277                  ( !strncmp( psz_mux, "mov", 3 ) ||
278                    !strncmp( psz_mux, "mp4", 3 ) ) )
279         {
280             msg_Err( p_stream, "mov and mp4 work only with file output" );
281         }
282     }
283
284     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
285
286     /* *** find and open appropriate access module *** */
287     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
288     if( p_access == NULL )
289     {
290         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
291                  psz_access, psz_mux, psz_url );
292         if( psz_access ) free( psz_access );
293         if( psz_mux ) free( psz_mux );
294         return VLC_EGENERIC;
295     }
296     msg_Dbg( p_stream, "access opened" );
297
298     /* *** find and open appropriate mux module *** */
299     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
300     if( p_mux == NULL )
301     {
302         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
303                  psz_access, psz_mux, psz_url );
304
305         sout_AccessOutDelete( p_access );
306         if( psz_access ) free( psz_access );
307         if( psz_mux ) free( psz_mux );
308         return VLC_EGENERIC;
309     }
310     msg_Dbg( p_stream, "mux opened" );
311
312     /*  *** Create the SAP Session structure *** */
313     var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
314     if( val.b_bool &&
315         ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
316     {
317         session_descriptor_t *p_session = sout_AnnounceSessionCreate();
318         announce_method_t *p_method =
319             sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
320         vlc_url_t url;
321
322         var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
323         if( *val.psz_string )
324         {
325             p_session->psz_name = val.psz_string;
326         }
327         else
328         {
329             free( val.psz_string );
330             p_session->psz_name = strdup( psz_url );
331         }
332         var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
333         p_method->i_ip_version = val.b_bool ? 6 : 4;
334
335         /* Now, parse the URL to extract host and port */
336         vlc_UrlParse( &url, psz_url , 0);
337
338         if( url.psz_host )
339         {
340             if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
341
342             p_session->psz_uri = url.psz_host;
343             p_session->i_port = url.i_port;
344             p_session->psz_sdp = NULL;
345
346             p_session->i_ttl = config_GetInt( p_sout, "ttl" );
347             p_session->i_payload = 33;
348
349             msg_Info( p_this, "SAP Enabled");
350
351             sout_AnnounceRegister( p_sout, p_session, p_method );
352
353             /* FIXME: Free p_method */
354
355             p_stream->p_sys->p_session = p_session;
356         }
357         vlc_UrlClean( &url );
358
359         if( p_method->psz_address) free( p_method->psz_address );
360         free( p_method );
361     }
362
363     /* *** Register with slp *** */
364 #ifdef HAVE_SLP_H
365     var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
366     if( val.b_bool &&
367         ( strstr( psz_access, "udp" ) || strstr( psz_access ,  "rtp" ) ) )
368     {
369         int i_ret;
370
371         msg_Info( p_this, "SLP Enabled");
372         var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
373         if( *val.psz_string )
374         {
375             i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
376         }
377         else
378         {
379             i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
380         }
381
382         if( i_ret )
383         {
384            msg_Warn( p_sout, "SLP Registering failed");
385         }
386         else
387         {
388             p_slp = malloc(sizeof(slp_session_t));
389             p_slp->psz_url = strdup( psz_url );
390             p_slp->psz_name =
391                 strdup( *val.psz_string ? val.psz_string : psz_url );
392         }
393         free( val.psz_string );
394     }
395 #endif
396
397     p_stream->pf_add    = Add;
398     p_stream->pf_del    = Del;
399     p_stream->pf_send   = Send;
400
401     p_stream->p_sys->p_mux = p_mux;
402     p_stream->p_sys->p_slp = p_slp;
403
404     if( psz_access ) free( psz_access );
405     if( psz_mux ) free( psz_mux );
406     if( psz_url ) free( psz_url );
407
408
409     return VLC_SUCCESS;
410 }
411
412 /*****************************************************************************
413  * Close:
414  *****************************************************************************/
415 static void Close( vlc_object_t * p_this )
416 {
417     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
418     sout_stream_sys_t *p_sys    = p_stream->p_sys;
419     sout_access_out_t *p_access = p_sys->p_mux->p_access;
420
421     if( p_sys->p_session != NULL )
422     {
423         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
424         sout_AnnounceSessionDestroy( p_sys->p_session );
425     }
426
427 #ifdef HAVE_SLP_H
428     if( p_sys->p_slp )
429     {
430             sout_SLPDereg( (sout_instance_t *)p_this,
431                         p_sys->p_slp->psz_url,
432                         p_sys->p_slp->psz_name);
433             free( p_sys->p_slp);
434     }
435 #endif
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 }