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