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