]> git.sesse.net Git - vlc/blob - modules/stream_out/standard.c
* Little cleanup of sap announces :
[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 #include "announce.h"
34
35 #define DEFAULT_IPV6_SCOPE "8"
36
37 /*****************************************************************************
38  * Exported prototypes
39  *****************************************************************************/
40 static int      Open    ( vlc_object_t * );
41 static void     Close   ( vlc_object_t * );
42
43 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
44 static int               Del ( sout_stream_t *, sout_stream_id_t * );
45 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 vlc_module_begin();
51     set_description( _("Standard stream output") );
52     set_capability( "sout stream", 50 );
53     add_shortcut( "standard" );
54     add_shortcut( "std" );
55     set_callbacks( Open, Close );
56 vlc_module_end();
57
58 struct sout_stream_sys_t
59 {
60     sout_mux_t           *p_mux;
61     slp_session_t        *p_slp;
62     sap_session_t        *p_sap;
63 };
64
65 /*****************************************************************************
66  * Open:
67  *****************************************************************************/
68 static int Open( vlc_object_t *p_this )
69 {
70     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
71     sout_instance_t     *p_sout = p_stream->p_sout;
72     slp_session_t       *p_slp = NULL;
73     sap_session_t       *p_sap = NULL;
74
75     char *psz_mux      = sout_cfg_find_value( p_stream->p_cfg, "mux" );
76     char *psz_access   = sout_cfg_find_value( p_stream->p_cfg, "access" );
77     char *psz_url      = sout_cfg_find_value( p_stream->p_cfg, "url" );
78     char *psz_ipv      = sout_cfg_find_value( p_stream->p_cfg, "sap_ipv" );
79     char *psz_v6_scope = sout_cfg_find_value( p_stream->p_cfg, "sap_v6scope" );
80     char *psz_sdp      = NULL;
81
82     sout_cfg_t *p_sap_cfg = sout_cfg_find( p_stream->p_cfg, "sap" );
83 #ifdef HAVE_SLP_H
84     sout_cfg_t *p_slp_cfg = sout_cfg_find( p_stream->p_cfg, "slp" );
85 #endif
86
87     sout_access_out_t   *p_access;
88     sout_mux_t          *p_mux;
89
90     char                *psz_mux_byext = NULL;
91
92     msg_Dbg( p_this, "creating `%s/%s://%s'",
93              psz_access, psz_mux, psz_url );
94
95     /* ext -> muxer name */
96     if( psz_url && strrchr( psz_url, '.' ) )
97     {
98         /* by extention */
99         static struct { char *ext; char *mux; } exttomux[] =
100         {
101             { "avi", "avi" },
102             { "ogg", "ogg" },
103             { "ogm", "ogg" },
104             { "mp4", "mp4" },
105             { "mov", "mov" },
106             { "moov","mov" },
107             { "asf", "asf" },
108             { "wma", "asf" },
109             { "wmv", "asf" },
110             { "trp", "ts" },
111             { "ts",  "ts" },
112             { "mpg", "ps" },
113             { "mpeg","ps" },
114             { "ps",  "ps" },
115             { "mpeg1","mpeg1" },
116             { NULL,  NULL }
117         };
118         char *psz_ext = strrchr( psz_url, '.' ) + 1;
119         int  i;
120
121         msg_Dbg( p_this, "extention is %s", psz_ext );
122         for( i = 0; exttomux[i].ext != NULL; i++ )
123         {
124             if( !strcasecmp( psz_ext, exttomux[i].ext ) )
125             {
126                 psz_mux_byext = exttomux[i].mux;
127                 break;
128             }
129         }
130         msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
131     }
132
133     /* We fix access/mux to valid couple */
134
135     if( ( psz_access == NULL || *psz_access == '\0' )&&
136         ( psz_mux == NULL ||  *psz_mux == '\0' ) )
137     {
138         if( psz_mux_byext )
139         {
140             msg_Warn( p_stream,
141                       "no access _and_ no muxer, extention gives file/%s",
142                       psz_mux_byext );
143             psz_access = "file";
144             psz_mux    = psz_mux_byext;
145         }
146         else
147         {
148             msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
149             return VLC_EGENERIC;
150         }
151     }
152
153     if( psz_access && *psz_access &&
154         ( psz_mux == NULL || *psz_mux == '\0' ) )
155     {
156         /* access given, no mux */
157         if( !strncmp( psz_access, "mmsh", 4 ) )
158         {
159             psz_mux = "asfh";
160         }
161         else if( !strncmp( psz_access, "udp", 3 ) )
162         {
163             psz_mux = "ts";
164         }
165         else
166         {
167             psz_mux = psz_mux_byext;
168         }
169     }
170     else if( psz_mux && *psz_mux &&
171              ( psz_access == NULL || *psz_access == '\0' ) )
172     {
173         /* mux given, no access */
174         if( !strncmp( psz_mux, "asfh", 4 ) )
175         {
176             psz_access = "mmsh";
177         }
178         else
179         {
180             /* default file */
181             psz_access = "file";
182         }
183     }
184
185     /* fix or warm of incompatible couple */
186     if( psz_mux && *psz_mux && psz_access && *psz_access )
187     {
188         if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
189         {
190             char *p = strchr( psz_mux,'{' );
191
192             msg_Warn( p_stream, "fixing to mmsh/asfh" );
193             if( p )
194             {
195                 /* -> a little memleak but ... */
196                 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
197                 sprintf( psz_mux, "asfh%s", p );
198             }
199             else
200             {
201                 psz_mux = "asfh";
202             }
203         }
204         else if( ( !strncmp( psz_access, "rtp", 3 ) ||
205                    !strncmp( psz_access, "udp", 3 ) ) &&
206                  strncmp( psz_mux, "ts", 2 ) )
207         {
208             msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
209         }
210         else if( strncmp( psz_access, "file", 4 ) &&
211                  ( !strncmp( psz_mux, "mov", 3 ) ||
212                    !strncmp( psz_mux, "mp4", 3 ) ) )
213         {
214             msg_Err( p_stream, "mov and mp4 work only with file output" );
215         }
216     }
217
218     msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
219
220     /* *** find and open appropriate access module *** */
221     p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
222     if( p_access == NULL )
223     {
224         msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
225                  psz_access, psz_mux, psz_url );
226         return( VLC_EGENERIC );
227     }
228     msg_Dbg( p_stream, "access opened" );
229
230     /* *** find and open appropriate mux module *** */
231     p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
232     if( p_mux == NULL )
233     {
234         msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
235                  psz_access, psz_mux, psz_url );
236
237         sout_AccessOutDelete( p_access );
238         return( VLC_EGENERIC );
239     }
240     msg_Dbg( p_stream, "mux opened" );
241
242     /*  *** Create the SAP Session structure *** */
243     if( psz_access &&
244         p_sap_cfg &&
245         ( strstr( psz_access, "udp" ) || strstr( psz_access ,  "rtp" ) ) )
246     {
247         msg_Info( p_this, "SAP Enabled");
248
249         if( psz_ipv == NULL )
250         {
251             psz_ipv = "4";
252         }
253         if( psz_v6_scope == NULL )
254         {
255             psz_v6_scope = DEFAULT_IPV6_SCOPE;
256         }
257         msg_Dbg( p_sout , "Creating SAP with IPv%i", atoi(psz_ipv) );
258
259         psz_sdp = SDPGenerateUDP(p_sap_cfg->psz_value ? p_sap_cfg->psz_value :
260                     psz_url, psz_url);
261
262         p_sap = sout_SAPNew( p_sout , psz_sdp,atoi(psz_ipv), psz_v6_scope );
263
264         if( !p_sap )
265             msg_Err( p_sout,"Unable to initialize SAP. SAP disabled");
266     }
267
268     /* *** Register with slp *** */
269 #ifdef HAVE_SLP_H
270     if( p_slp_cfg && ( strstr( psz_access, "udp" ) ||
271                        strstr( psz_access ,  "rtp" ) ) )
272     {
273         msg_Info( p_this, "SLP Enabled");
274         if( sout_SLPReg( p_sout, psz_url,
275             p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url) )
276         {
277            msg_Warn( p_sout, "SLP Registering failed");
278         }
279         else
280         {
281             p_slp = (slp_session_t*)malloc(sizeof(slp_session_t));
282             if(!p_slp)
283             {
284                 msg_Warn(p_sout,"Out of memory");
285                 if( p_sap ) free( p_sap );
286                 return -1;
287             }
288             p_slp->psz_url= strdup(psz_url);
289             p_slp->psz_name = strdup(
290                     p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
291         }
292     }
293 #endif
294
295     p_stream->pf_add    = Add;
296     p_stream->pf_del    = Del;
297     p_stream->pf_send   = Send;
298
299     p_stream->p_sys        = malloc( sizeof( sout_stream_sys_t) );
300     p_stream->p_sys->p_mux = p_mux;
301     p_stream->p_sys->p_slp = p_slp;
302     p_stream->p_sys->p_sap = p_sap;
303
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * Close:
309  *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
311 {
312     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
313     sout_stream_sys_t *p_sys    = p_stream->p_sys;
314     sout_access_out_t *p_access = p_sys->p_mux->p_access;
315
316     if( p_sys->p_sap )
317         sout_SAPDelete( (sout_instance_t *)p_this , p_sys->p_sap );
318
319 #ifdef HAVE_SLP_H
320     if( p_sys->p_slp )
321     {
322             sout_SLPDereg( (sout_instance_t *)p_this,
323                         p_sys->p_slp->psz_url,
324                         p_sys->p_slp->psz_name);
325             free( p_sys->p_slp);
326     }
327 #endif
328
329
330     sout_MuxDelete( p_sys->p_mux );
331     sout_AccessOutDelete( p_access );
332
333     free( p_sys );
334 }
335
336 struct sout_stream_id_t
337 {
338     sout_input_t *p_input;
339 };
340
341
342 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
343 {
344     sout_stream_sys_t *p_sys = p_stream->p_sys;
345     sout_stream_id_t  *id;
346
347     id = malloc( sizeof( sout_stream_id_t ) );
348     if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
349     {
350         free( id );
351
352         return NULL;
353     }
354
355     return id;
356 }
357
358 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
359 {
360     sout_stream_sys_t *p_sys = p_stream->p_sys;
361
362     sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
363
364     free( id );
365
366     return VLC_SUCCESS;
367 }
368
369 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
370                  block_t *p_buffer )
371 {
372     sout_stream_sys_t *p_sys = p_stream->p_sys;
373     sout_instance_t   *p_sout = p_stream->p_sout;
374
375     sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
376
377     if( p_sys->p_sap )
378        sout_SAPSend( p_sout , p_sys->p_sap );
379
380     return VLC_SUCCESS;
381 }