]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
* modules/stream_out/standard.c: fixed a few small mem leaks.
[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_ParseCfg( 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
227         {
228             psz_mux = strdup(psz_mux_byext);
229         }
230     }
231     else if( psz_mux && !psz_access )
232     {
233         /* mux given, no access */
234         if( !strncmp( psz_mux, "asfh", 4 ) )
235         {
236             psz_access = strdup("mmsh");
237         }
238         else
239         {
240             /* default file */
241             psz_access = strdup("file");
242         }
243     }
244
245     /* fix or warm of incompatible couple */
246     if( psz_mux && psz_access )
247     {
248         if( !strncmp( psz_access, "mmsh", 4 ) &&
249             strncmp( psz_mux, "asfh", 4 ) )
250         {
251             char *p = strchr( psz_mux,'{' );
252
253             msg_Warn( p_stream, "fixing to mmsh/asfh" );
254             if( p )
255             {
256                 /* -> a little memleak but ... */
257                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
258                 sprintf( psz_mux, "asfh%s", p );
259             }
260             else
261             {
262                 psz_mux = strdup("asfh");
263             }
264         }
265         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
266                    !strncmp( psz_access, "udp", 3 ) ) &&
267                  strncmp( psz_mux, "ts", 2 ) )
268         {
269             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
270         }
271         else if( strncmp( psz_access, "file", 4 ) &&
272                  ( !strncmp( psz_mux, "mov", 3 ) ||
273                    !strncmp( psz_mux, "mp4", 3 ) ) )
274         {
275             msg_Err( p_stream, "mov and mp4 work only with file output" );
276         }
277     }
278
279     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
280
281     /* *** find and open appropriate access module *** */
282     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
283     if( p_access == NULL )
284     {
285         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
286                  psz_access, psz_mux, psz_url );
287         if( psz_access ) free( psz_access );
288         if( psz_mux ) free( psz_mux );
289         return VLC_EGENERIC;
290     }
291     msg_Dbg( p_stream, "access opened" );
292
293     /* *** find and open appropriate mux module *** */
294     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
295     if( p_mux == NULL )
296     {
297         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
298                  psz_access, psz_mux, psz_url );
299
300         sout_AccessOutDelete( p_access );
301         if( psz_access ) free( psz_access );
302         if( psz_mux ) free( psz_mux );
303         return VLC_EGENERIC;
304     }
305     msg_Dbg( p_stream, "mux opened" );
306
307     /*  *** Create the SAP Session structure *** */
308     var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
309     if( val.b_bool &&
310         ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
311     {
312         session_descriptor_t *p_session = sout_AnnounceSessionCreate();
313         announce_method_t *p_method =
314             sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
315         vlc_url_t url;
316
317         var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
318         if( *val.psz_string )
319         {
320             p_session->psz_name = val.psz_string;
321         }
322         else
323         {
324             free( val.psz_string );
325             p_session->psz_name = strdup( psz_url );
326         }
327         var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
328         p_method->i_ip_version = val.b_bool ? 6 : 4;
329
330         /* Now, parse the URL to extract host and port */
331         vlc_UrlParse( &url, psz_url , 0);
332
333         if( url.psz_host )
334         {
335             if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
336
337             p_session->psz_uri = url.psz_host;
338             p_session->i_port = url.i_port;
339             p_session->psz_sdp = NULL;
340
341             p_session->i_ttl = config_GetInt( p_sout, "ttl" );
342             p_session->i_payload = 33;
343
344             msg_Info( p_this, "SAP Enabled");
345
346             sout_AnnounceRegister( p_sout, p_session, p_method );
347
348             /* FIXME: Free p_method */
349
350             p_stream->p_sys->p_session = p_session;
351         }
352         vlc_UrlClean( &url );
353     }
354
355     /* *** Register with slp *** */
356 #ifdef HAVE_SLP_H
357     var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
358     if( val.b_bool &&
359         ( strstr( psz_access, "udp" ) || strstr( psz_access ,  "rtp" ) ) )
360     {
361         int i_ret;
362
363         msg_Info( p_this, "SLP Enabled");
364         var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
365         if( *val.psz_string )
366         {
367             i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
368         }
369         else
370         {
371             i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
372         }
373
374         if( i_ret )
375         {
376            msg_Warn( p_sout, "SLP Registering failed");
377         }
378         else
379         {
380             p_slp = malloc(sizeof(slp_session_t));
381             p_slp->psz_url = strdup( psz_url );
382             p_slp->psz_name =
383                 strdup( *val.psz_string ? val.psz_string : psz_url );
384         }
385         free( val.psz_string );
386     }
387 #endif
388
389     p_stream->pf_add    = Add;
390     p_stream->pf_del    = Del;
391     p_stream->pf_send   = Send;
392
393     p_stream->p_sys->p_mux = p_mux;
394     p_stream->p_sys->p_slp = p_slp;
395
396     if( psz_access ) free( psz_access );
397     if( psz_mux ) free( psz_mux );
398     if( psz_url ) free( psz_url );
399
400     return VLC_SUCCESS;
401 }
402
403 /*****************************************************************************
404  * Close:
405  *****************************************************************************/
406 static void Close( vlc_object_t * p_this )
407 {
408     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
409     sout_stream_sys_t *p_sys    = p_stream->p_sys;
410     sout_access_out_t *p_access = p_sys->p_mux->p_access;
411
412     if( p_sys->p_session != NULL )
413     {
414         sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
415     }
416
417 #ifdef HAVE_SLP_H
418     if( p_sys->p_slp )
419     {
420             sout_SLPDereg( (sout_instance_t *)p_this,
421                         p_sys->p_slp->psz_url,
422                         p_sys->p_slp->psz_name);
423             free( p_sys->p_slp);
424     }
425 #endif
426
427
428     sout_MuxDelete( p_sys->p_mux );
429     sout_AccessOutDelete( p_access );
430
431     free( p_sys );
432 }
433
434 struct sout_stream_id_t
435 {
436     sout_input_t *p_input;
437 };
438
439
440 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
441 {
442     sout_stream_sys_t *p_sys = p_stream->p_sys;
443     sout_stream_id_t  *id;
444
445     id = malloc( sizeof( sout_stream_id_t ) );
446     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
447     {
448         free( id );
449
450         return NULL;
451     }
452
453     return id;
454 }
455
456 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
457 {
458     sout_stream_sys_t *p_sys = p_stream->p_sys;
459
460     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
461
462     free( id );
463
464     return VLC_SUCCESS;
465 }
466
467 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
468                  block_t *p_buffer )
469 {
470     sout_stream_sys_t *p_sys = p_stream->p_sys;
471
472     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
473
474     return VLC_SUCCESS;
475 }