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