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