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