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