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