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