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