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